blob: eea24c9a561d95b7dbc641b262b066c4098e2226 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Common NFSv4 ACL handling code.
3 *
4 * Copyright (c) 2002, 2003 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Marius Aamodt Eriksen <marius@umich.edu>
8 * Jeff Sedlak <jsedlak@umich.edu>
9 * J. Bruce Fields <bfields@umich.edu>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/nfs_fs.h>
Paul Gortmakerafeacc82011-05-26 16:00:52 -040039#include <linux/export.h>
J. Bruce Fields35541162014-01-08 09:49:01 -050040#include "nfsd.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050041#include "acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43
44/* mode bit translations: */
45#define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
46#define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
47#define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
48#define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
49#define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
50
51/* We don't support these bits; insist they be neither allowed nor denied */
52#define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
53 | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
54
55/* flags used to simulate posix default ACLs */
56#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
J. Bruce Fields7bdfa682007-02-16 01:28:28 -080057 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
J. Bruce Fields7bdfa682007-02-16 01:28:28 -080059#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
60 | NFS4_ACE_INHERIT_ONLY_ACE \
61 | NFS4_ACE_IDENTIFIER_GROUP)
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define MASK_EQUAL(mask1, mask2) \
64 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
65
66static u32
67mask_from_posix(unsigned short perm, unsigned int flags)
68{
69 int mask = NFS4_ANYONE_MODE;
70
71 if (flags & NFS4_ACL_OWNER)
72 mask |= NFS4_OWNER_MODE;
73 if (perm & ACL_READ)
74 mask |= NFS4_READ_MODE;
75 if (perm & ACL_WRITE)
76 mask |= NFS4_WRITE_MODE;
77 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
78 mask |= NFS4_ACE_DELETE_CHILD;
79 if (perm & ACL_EXECUTE)
80 mask |= NFS4_EXECUTE_MODE;
81 return mask;
82}
83
84static u32
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -080085deny_mask_from_posix(unsigned short perm, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -080087 u32 mask = 0;
88
89 if (perm & ACL_READ)
90 mask |= NFS4_READ_MODE;
91 if (perm & ACL_WRITE)
92 mask |= NFS4_WRITE_MODE;
93 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
94 mask |= NFS4_ACE_DELETE_CHILD;
95 if (perm & ACL_EXECUTE)
96 mask |= NFS4_EXECUTE_MODE;
97 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100/* XXX: modify functions to return NFS errors; they're only ever
101 * used by nfs code, after all.... */
102
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700103/* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
104 * side of being more restrictive, so the mode bit mapping below is
105 * pessimistic. An optimistic version would be needed to handle DENY's,
106 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
107 * bits. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700109static void
110low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
111{
112 u32 write_mode = NFS4_WRITE_MODE;
113
114 if (flags & NFS4_ACL_DIR)
115 write_mode |= NFS4_ACE_DELETE_CHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *mode = 0;
117 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
118 *mode |= ACL_READ;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700119 if ((perm & write_mode) == write_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *mode |= ACL_WRITE;
121 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
122 *mode |= ACL_EXECUTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125struct ace_container {
126 struct nfs4_ace *ace;
127 struct list_head ace_l;
128};
129
130static short ace2type(struct nfs4_ace *);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800131static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
132 unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134struct nfs4_acl *
135nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
136 unsigned int flags)
137{
138 struct nfs4_acl *acl;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800139 int size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800141 if (pacl) {
142 if (posix_acl_valid(pacl) < 0)
143 return ERR_PTR(-EINVAL);
144 size += 2*pacl->a_count;
145 }
146 if (dpacl) {
147 if (posix_acl_valid(dpacl) < 0)
148 return ERR_PTR(-EINVAL);
149 size += 2*dpacl->a_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800152 /* Allocate for worst case: one (deny, allow) pair each: */
153 acl = nfs4_acl_new(size);
154 if (acl == NULL)
155 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800157 if (pacl)
158 _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800160 if (dpacl)
161 _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 return acl;
164}
165
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800166struct posix_acl_summary {
167 unsigned short owner;
168 unsigned short users;
169 unsigned short group;
170 unsigned short groups;
171 unsigned short other;
172 unsigned short mask;
173};
174
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800175static void
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800176summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800178 struct posix_acl_entry *pa, *pe;
J. Bruce Fieldsf7fede42007-07-17 04:04:36 -0700179
180 /*
181 * Only pas.users and pas.groups need initialization; previous
182 * posix_acl_valid() calls ensure that the other fields will be
183 * initialized in the following loop. But, just to placate gcc:
184 */
185 memset(pas, 0, sizeof(*pas));
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800186 pas->mask = 07;
187
188 pe = acl->a_entries + acl->a_count;
189
190 FOREACH_ACL_ENTRY(pa, acl, pe) {
191 switch (pa->e_tag) {
192 case ACL_USER_OBJ:
193 pas->owner = pa->e_perm;
194 break;
195 case ACL_GROUP_OBJ:
196 pas->group = pa->e_perm;
197 break;
198 case ACL_USER:
199 pas->users |= pa->e_perm;
200 break;
201 case ACL_GROUP:
202 pas->groups |= pa->e_perm;
203 break;
204 case ACL_OTHER:
205 pas->other = pa->e_perm;
206 break;
207 case ACL_MASK:
208 pas->mask = pa->e_perm;
209 break;
210 }
211 }
212 /* We'll only care about effective permissions: */
213 pas->users &= pas->mask;
214 pas->group &= pas->mask;
215 pas->groups &= pas->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/* We assume the acl has been verified with posix_acl_valid. */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800219static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
221 unsigned int flags)
222{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800223 struct posix_acl_entry *pa, *group_owner_entry;
224 struct nfs4_ace *ace;
225 struct posix_acl_summary pas;
226 unsigned short deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
Bruce Fields54c044092007-03-26 21:32:09 -0800228 NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 BUG_ON(pacl->a_count < 3);
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800231 summarize_posix_acl(pacl, &pas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 pa = pacl->a_entries;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800234 ace = acl->aces + acl->naces;
235
236 /* We could deny everything not granted by the owner: */
237 deny = ~pas.owner;
238 /*
239 * but it is equivalent (and simpler) to deny only what is not
240 * granted by later entries:
241 */
242 deny &= pas.users | pas.group | pas.groups | pas.other;
243 if (deny) {
244 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
245 ace->flag = eflag;
246 ace->access_mask = deny_mask_from_posix(deny, flags);
247 ace->whotype = NFS4_ACL_WHO_OWNER;
248 ace++;
249 acl->naces++;
250 }
251
252 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
253 ace->flag = eflag;
254 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
255 ace->whotype = NFS4_ACL_WHO_OWNER;
256 ace++;
257 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 pa++;
259
260 while (pa->e_tag == ACL_USER) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800261 deny = ~(pa->e_perm & pas.mask);
262 deny &= pas.groups | pas.group | pas.other;
263 if (deny) {
264 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
265 ace->flag = eflag;
266 ace->access_mask = deny_mask_from_posix(deny, flags);
267 ace->whotype = NFS4_ACL_WHO_NAMED;
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800268 ace->who_uid = pa->e_uid;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800269 ace++;
270 acl->naces++;
271 }
272 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
273 ace->flag = eflag;
274 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
275 flags);
276 ace->whotype = NFS4_ACL_WHO_NAMED;
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800277 ace->who_uid = pa->e_uid;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800278 ace++;
279 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 pa++;
281 }
282
283 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
284 * since a user can be in more than one group. */
285
286 /* allow ACEs */
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 group_owner_entry = pa;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800289
290 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
291 ace->flag = eflag;
292 ace->access_mask = mask_from_posix(pas.group, flags);
293 ace->whotype = NFS4_ACL_WHO_GROUP;
294 ace++;
295 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 pa++;
297
298 while (pa->e_tag == ACL_GROUP) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800299 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
300 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
301 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
302 flags);
303 ace->whotype = NFS4_ACL_WHO_NAMED;
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800304 ace->who_gid = pa->e_gid;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800305 ace++;
306 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 pa++;
308 }
309
310 /* deny ACEs */
311
312 pa = group_owner_entry;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800313
314 deny = ~pas.group & pas.other;
315 if (deny) {
316 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
Frank Filzd8d0b852009-08-27 17:35:41 -0400317 ace->flag = eflag;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800318 ace->access_mask = deny_mask_from_posix(deny, flags);
319 ace->whotype = NFS4_ACL_WHO_GROUP;
320 ace++;
321 acl->naces++;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 pa++;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 while (pa->e_tag == ACL_GROUP) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800326 deny = ~(pa->e_perm & pas.mask);
327 deny &= pas.other;
328 if (deny) {
329 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
330 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
Frank Filz55bb55d2009-08-14 15:02:30 -0700331 ace->access_mask = deny_mask_from_posix(deny, flags);
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800332 ace->whotype = NFS4_ACL_WHO_NAMED;
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800333 ace->who_gid = pa->e_gid;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800334 ace++;
335 acl->naces++;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 pa++;
338 }
339
340 if (pa->e_tag == ACL_MASK)
341 pa++;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800342 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
343 ace->flag = eflag;
344 ace->access_mask = mask_from_posix(pa->e_perm, flags);
345 ace->whotype = NFS4_ACL_WHO_EVERYONE;
346 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800349static bool
350pace_gt(struct posix_acl_entry *pace1, struct posix_acl_entry *pace2)
351{
352 if (pace1->e_tag != pace2->e_tag)
353 return pace1->e_tag > pace2->e_tag;
354 if (pace1->e_tag == ACL_USER)
355 return uid_gt(pace1->e_uid, pace2->e_uid);
356 if (pace1->e_tag == ACL_GROUP)
357 return gid_gt(pace1->e_gid, pace2->e_gid);
358 return false;
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static void
362sort_pacl_range(struct posix_acl *pacl, int start, int end) {
363 int sorted = 0, i;
364 struct posix_acl_entry tmp;
365
366 /* We just do a bubble sort; easy to do in place, and we're not
367 * expecting acl's to be long enough to justify anything more. */
368 while (!sorted) {
369 sorted = 1;
370 for (i = start; i < end; i++) {
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800371 if (pace_gt(&pacl->a_entries[i],
372 &pacl->a_entries[i+1])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 sorted = 0;
374 tmp = pacl->a_entries[i];
375 pacl->a_entries[i] = pacl->a_entries[i+1];
376 pacl->a_entries[i+1] = tmp;
377 }
378 }
379 }
380}
381
382static void
383sort_pacl(struct posix_acl *pacl)
384{
385 /* posix_acl_valid requires that users and groups be in order
386 * by uid/gid. */
387 int i, j;
388
389 if (pacl->a_count <= 4)
390 return; /* no users or groups */
391 i = 1;
392 while (pacl->a_entries[i].e_tag == ACL_USER)
393 i++;
394 sort_pacl_range(pacl, 1, i-1);
395
396 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
Frank Filzaba24d72009-10-21 16:45:02 -0700397 j = ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 while (pacl->a_entries[j].e_tag == ACL_GROUP)
399 j++;
400 sort_pacl_range(pacl, i, j-1);
401 return;
402}
403
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700404/*
405 * While processing the NFSv4 ACE, this maintains bitmasks representing
406 * which permission bits have been allowed and which denied to a given
407 * entity: */
408struct posix_ace_state {
409 u32 allow;
410 u32 deny;
411};
412
413struct posix_user_ace_state {
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800414 union {
415 kuid_t uid;
416 kgid_t gid;
417 };
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700418 struct posix_ace_state perms;
419};
420
421struct posix_ace_state_array {
422 int n;
423 struct posix_user_ace_state aces[];
424};
425
426/*
427 * While processing the NFSv4 ACE, this maintains the partial permissions
428 * calculated so far: */
429
430struct posix_acl_state {
J. Bruce Fields3160a712007-02-16 01:28:37 -0800431 int empty;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700432 struct posix_ace_state owner;
433 struct posix_ace_state group;
434 struct posix_ace_state other;
435 struct posix_ace_state everyone;
436 struct posix_ace_state mask; /* Deny unused in this case */
437 struct posix_ace_state_array *users;
438 struct posix_ace_state_array *groups;
439};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441static int
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700442init_state(struct posix_acl_state *state, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700444 int alloc;
445
446 memset(state, 0, sizeof(struct posix_acl_state));
J. Bruce Fields3160a712007-02-16 01:28:37 -0800447 state->empty = 1;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700448 /*
449 * In the worst case, each individual acl could be for a distinct
450 * named user or group, but we don't no which, so we allocate
451 * enough space for either:
452 */
453 alloc = sizeof(struct posix_ace_state_array)
J. Bruce Fields91b80962008-08-29 19:18:45 -0400454 + cnt*sizeof(struct posix_user_ace_state);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700455 state->users = kzalloc(alloc, GFP_KERNEL);
456 if (!state->users)
457 return -ENOMEM;
458 state->groups = kzalloc(alloc, GFP_KERNEL);
459 if (!state->groups) {
460 kfree(state->users);
461 return -ENOMEM;
462 }
463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700466static void
467free_state(struct posix_acl_state *state) {
468 kfree(state->users);
469 kfree(state->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700472static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700474 state->mask.allow |= astate->allow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700477/*
478 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
479 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
480 * to traditional read/write/execute permissions.
481 *
482 * It's problematic to reject acls that use certain mode bits, because it
483 * places the burden on users to learn the rules about which bits one
484 * particular server sets, without giving the user a lot of help--we return an
485 * error that could mean any number of different things. To make matters
486 * worse, the problematic bits might be introduced by some application that's
487 * automatically mapping from some other acl model.
488 *
489 * So wherever possible we accept anything, possibly erring on the side of
490 * denying more permissions than necessary.
491 *
492 * However we do reject *explicit* DENY's of a few bits representing
493 * permissions we could never deny:
494 */
495
496static inline int check_deny(u32 mask, int isowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700498 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
499 return -EINVAL;
500 if (!isowner)
501 return 0;
502 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
503 return -EINVAL;
504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700507static struct posix_acl *
508posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700510 struct posix_acl_entry *pace;
511 struct posix_acl *pacl;
512 int nace;
513 int i, error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
J. Bruce Fields3160a712007-02-16 01:28:37 -0800515 /*
516 * ACLs with no ACEs are treated differently in the inheritable
517 * and effective cases: when there are no inheritable ACEs, we
518 * set a zero-length default posix acl:
519 */
520 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
521 pacl = posix_acl_alloc(0, GFP_KERNEL);
522 return pacl ? pacl : ERR_PTR(-ENOMEM);
523 }
524 /*
525 * When there are no effective ACEs, the following will end
526 * up setting a 3-element effective posix ACL with all
527 * permissions zero.
528 */
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700529 nace = 4 + state->users->n + state->groups->n;
530 pacl = posix_acl_alloc(nace, GFP_KERNEL);
531 if (!pacl)
532 return ERR_PTR(-ENOMEM);
533
534 pace = pacl->a_entries;
535 pace->e_tag = ACL_USER_OBJ;
536 error = check_deny(state->owner.deny, 1);
537 if (error)
538 goto out_err;
539 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700540
541 for (i=0; i < state->users->n; i++) {
542 pace++;
543 pace->e_tag = ACL_USER;
544 error = check_deny(state->users->aces[i].perms.deny, 0);
545 if (error)
546 goto out_err;
547 low_mode_from_nfs4(state->users->aces[i].perms.allow,
548 &pace->e_perm, flags);
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800549 pace->e_uid = state->users->aces[i].uid;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700550 add_to_mask(state, &state->users->aces[i].perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700552
553 pace++;
554 pace->e_tag = ACL_GROUP_OBJ;
555 error = check_deny(state->group.deny, 0);
556 if (error)
557 goto out_err;
558 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700559 add_to_mask(state, &state->group);
560
561 for (i=0; i < state->groups->n; i++) {
562 pace++;
563 pace->e_tag = ACL_GROUP;
564 error = check_deny(state->groups->aces[i].perms.deny, 0);
565 if (error)
566 goto out_err;
567 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
568 &pace->e_perm, flags);
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800569 pace->e_gid = state->groups->aces[i].gid;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700570 add_to_mask(state, &state->groups->aces[i].perms);
571 }
572
573 pace++;
574 pace->e_tag = ACL_MASK;
575 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700576
577 pace++;
578 pace->e_tag = ACL_OTHER;
579 error = check_deny(state->other.deny, 0);
580 if (error)
581 goto out_err;
582 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700583
584 return pacl;
585out_err:
586 posix_acl_release(pacl);
587 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700590static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700592 /* Allow all bits in the mask not already denied: */
593 astate->allow |= mask & ~astate->deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700596static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700598 /* Deny all bits in the mask not already allowed: */
599 astate->deny |= mask & ~astate->allow;
600}
601
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800602static int find_uid(struct posix_acl_state *state, kuid_t uid)
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700603{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800604 struct posix_ace_state_array *a = state->users;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700605 int i;
606
607 for (i = 0; i < a->n; i++)
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800608 if (uid_eq(a->aces[i].uid, uid))
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700609 return i;
610 /* Not found: */
611 a->n++;
612 a->aces[i].uid = uid;
613 a->aces[i].perms.allow = state->everyone.allow;
614 a->aces[i].perms.deny = state->everyone.deny;
615
616 return i;
617}
618
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800619static int find_gid(struct posix_acl_state *state, kgid_t gid)
620{
621 struct posix_ace_state_array *a = state->groups;
622 int i;
623
624 for (i = 0; i < a->n; i++)
625 if (gid_eq(a->aces[i].gid, gid))
626 return i;
627 /* Not found: */
628 a->n++;
629 a->aces[i].gid = gid;
630 a->aces[i].perms.allow = state->everyone.allow;
631 a->aces[i].perms.deny = state->everyone.deny;
632
633 return i;
634}
635
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700636static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
637{
638 int i;
639
640 for (i=0; i < a->n; i++)
641 deny_bits(&a->aces[i].perms, mask);
642}
643
644static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
645{
646 int i;
647
648 for (i=0; i < a->n; i++)
649 allow_bits(&a->aces[i].perms, mask);
650}
651
652static void process_one_v4_ace(struct posix_acl_state *state,
653 struct nfs4_ace *ace)
654{
655 u32 mask = ace->access_mask;
656 int i;
657
J. Bruce Fields3160a712007-02-16 01:28:37 -0800658 state->empty = 0;
659
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700660 switch (ace2type(ace)) {
661 case ACL_USER_OBJ:
662 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
663 allow_bits(&state->owner, mask);
664 } else {
665 deny_bits(&state->owner, mask);
666 }
667 break;
668 case ACL_USER:
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800669 i = find_uid(state, ace->who_uid);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700670 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
671 allow_bits(&state->users->aces[i].perms, mask);
672 } else {
673 deny_bits(&state->users->aces[i].perms, mask);
674 mask = state->users->aces[i].perms.deny;
675 deny_bits(&state->owner, mask);
676 }
677 break;
678 case ACL_GROUP_OBJ:
679 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
680 allow_bits(&state->group, mask);
681 } else {
682 deny_bits(&state->group, mask);
683 mask = state->group.deny;
684 deny_bits(&state->owner, mask);
685 deny_bits(&state->everyone, mask);
686 deny_bits_array(state->users, mask);
687 deny_bits_array(state->groups, mask);
688 }
689 break;
690 case ACL_GROUP:
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800691 i = find_gid(state, ace->who_gid);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700692 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
693 allow_bits(&state->groups->aces[i].perms, mask);
694 } else {
695 deny_bits(&state->groups->aces[i].perms, mask);
696 mask = state->groups->aces[i].perms.deny;
697 deny_bits(&state->owner, mask);
698 deny_bits(&state->group, mask);
699 deny_bits(&state->everyone, mask);
700 deny_bits_array(state->users, mask);
701 deny_bits_array(state->groups, mask);
702 }
703 break;
704 case ACL_OTHER:
705 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
706 allow_bits(&state->owner, mask);
707 allow_bits(&state->group, mask);
708 allow_bits(&state->other, mask);
709 allow_bits(&state->everyone, mask);
710 allow_bits_array(state->users, mask);
711 allow_bits_array(state->groups, mask);
712 } else {
713 deny_bits(&state->owner, mask);
714 deny_bits(&state->group, mask);
715 deny_bits(&state->other, mask);
716 deny_bits(&state->everyone, mask);
717 deny_bits_array(state->users, mask);
718 deny_bits_array(state->groups, mask);
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721}
722
J. Bruce Fields575a6292007-02-16 01:28:29 -0800723int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
724 struct posix_acl **dpacl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
J. Bruce Fields575a6292007-02-16 01:28:29 -0800726 struct posix_acl_state effective_acl_state, default_acl_state;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700727 struct nfs4_ace *ace;
728 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
J. Bruce Fields575a6292007-02-16 01:28:29 -0800730 ret = init_state(&effective_acl_state, acl->naces);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700731 if (ret)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800732 return ret;
733 ret = init_state(&default_acl_state, acl->naces);
734 if (ret)
735 goto out_estate;
736 ret = -EINVAL;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800737 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700738 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
739 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800740 goto out_dstate;
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700741 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800742 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800743 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
J. Bruce Fields575a6292007-02-16 01:28:29 -0800744 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 continue;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800746 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800747 if (!(flags & NFS4_ACL_DIR))
748 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800749 /*
750 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
751 * is set, we're effectively turning on the other. That's OK,
752 * according to rfc 3530.
753 */
J. Bruce Fields575a6292007-02-16 01:28:29 -0800754 process_one_v4_ace(&default_acl_state, ace);
755
756 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
757 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800759 *pacl = posix_state_to_acl(&effective_acl_state, flags);
760 if (IS_ERR(*pacl)) {
761 ret = PTR_ERR(*pacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700762 *pacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800763 goto out_dstate;
764 }
J. Bruce Fields3160a712007-02-16 01:28:37 -0800765 *dpacl = posix_state_to_acl(&default_acl_state,
766 flags | NFS4_ACL_TYPE_DEFAULT);
J. Bruce Fields575a6292007-02-16 01:28:29 -0800767 if (IS_ERR(*dpacl)) {
768 ret = PTR_ERR(*dpacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700769 *dpacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800770 posix_acl_release(*pacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700771 *pacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800772 goto out_dstate;
773 }
774 sort_pacl(*pacl);
775 sort_pacl(*dpacl);
776 ret = 0;
777out_dstate:
778 free_state(&default_acl_state);
779out_estate:
780 free_state(&effective_acl_state);
781 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784static short
785ace2type(struct nfs4_ace *ace)
786{
787 switch (ace->whotype) {
788 case NFS4_ACL_WHO_NAMED:
789 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
790 ACL_GROUP : ACL_USER);
791 case NFS4_ACL_WHO_OWNER:
792 return ACL_USER_OBJ;
793 case NFS4_ACL_WHO_GROUP:
794 return ACL_GROUP_OBJ;
795 case NFS4_ACL_WHO_EVERYONE:
796 return ACL_OTHER;
797 }
798 BUG();
799 return -1;
800}
801
802EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
803EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
804
805struct nfs4_acl *
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800806nfs4_acl_new(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct nfs4_acl *acl;
809
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800810 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
811 if (acl == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 acl->naces = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return acl;
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817static struct {
818 char *string;
819 int stringlen;
820 int type;
821} s2t_map[] = {
822 {
823 .string = "OWNER@",
824 .stringlen = sizeof("OWNER@") - 1,
825 .type = NFS4_ACL_WHO_OWNER,
826 },
827 {
828 .string = "GROUP@",
829 .stringlen = sizeof("GROUP@") - 1,
830 .type = NFS4_ACL_WHO_GROUP,
831 },
832 {
833 .string = "EVERYONE@",
834 .stringlen = sizeof("EVERYONE@") - 1,
835 .type = NFS4_ACL_WHO_EVERYONE,
836 },
837};
838
839int
840nfs4_acl_get_whotype(char *p, u32 len)
841{
842 int i;
843
Tobias Klausere8c96f82006-03-24 03:15:34 -0800844 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (s2t_map[i].stringlen == len &&
846 0 == memcmp(s2t_map[i].string, p, len))
847 return s2t_map[i].type;
848 }
849 return NFS4_ACL_WHO_NAMED;
850}
851
J. Bruce Fields35541162014-01-08 09:49:01 -0500852__be32 nfs4_acl_write_who(int who, __be32 **p, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 int i;
J. Bruce Fields35541162014-01-08 09:49:01 -0500855 int bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Tobias Klausere8c96f82006-03-24 03:15:34 -0800857 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
J. Bruce Fields35541162014-01-08 09:49:01 -0500858 if (s2t_map[i].type != who)
859 continue;
860 bytes = 4 + (XDR_QUADLEN(s2t_map[i].stringlen) << 2);
861 if (bytes > *len)
862 return nfserr_resource;
863 *p = xdr_encode_opaque(*p, s2t_map[i].string,
864 s2t_map[i].stringlen);
865 *len -= bytes;
866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
J. Bruce Fields35541162014-01-08 09:49:01 -0500868 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return -1;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872EXPORT_SYMBOL(nfs4_acl_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873EXPORT_SYMBOL(nfs4_acl_get_whotype);
874EXPORT_SYMBOL(nfs4_acl_write_who);