1 /*
2 * 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
37 #include <linux/fs.h>
38 #include <linux/slab.h>
39 #include <linux/posix_acl.h>
40
41 #include "nfsfh.h"
42 #include "nfsd.h"
43 #include "acl.h"
44 #include "vfs.h"
45
46 #define NFS4_ACL_TYPE_DEFAULT 0x01
47 #define NFS4_ACL_DIR 0x02
48 #define NFS4_ACL_OWNER 0x04
49
50 /* mode bit translations: */
51 #define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
52 #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
53 #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
54 #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
55 #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
56
57 /* flags used to simulate posix default ACLs */
58 #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
59 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
60
61 #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
62 | NFS4_ACE_INHERIT_ONLY_ACE \
63 | NFS4_ACE_IDENTIFIER_GROUP)
64
65 static u32
mask_from_posix(unsigned short perm,unsigned int flags)66 mask_from_posix(unsigned short perm, unsigned int flags)
67 {
68 int mask = NFS4_ANYONE_MODE;
69
70 if (flags & NFS4_ACL_OWNER)
71 mask |= NFS4_OWNER_MODE;
72 if (perm & ACL_READ)
73 mask |= NFS4_READ_MODE;
74 if (perm & ACL_WRITE)
75 mask |= NFS4_WRITE_MODE;
76 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
77 mask |= NFS4_ACE_DELETE_CHILD;
78 if (perm & ACL_EXECUTE)
79 mask |= NFS4_EXECUTE_MODE;
80 return mask;
81 }
82
83 static u32
deny_mask_from_posix(unsigned short perm,u32 flags)84 deny_mask_from_posix(unsigned short perm, u32 flags)
85 {
86 u32 mask = 0;
87
88 if (perm & ACL_READ)
89 mask |= NFS4_READ_MODE;
90 if (perm & ACL_WRITE)
91 mask |= NFS4_WRITE_MODE;
92 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
93 mask |= NFS4_ACE_DELETE_CHILD;
94 if (perm & ACL_EXECUTE)
95 mask |= NFS4_EXECUTE_MODE;
96 return mask;
97 }
98
99 /* XXX: modify functions to return NFS errors; they're only ever
100 * used by nfs code, after all.... */
101
102 /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
103 * side of being more restrictive, so the mode bit mapping below is
104 * pessimistic. An optimistic version would be needed to handle DENY's,
105 * but we expect to coalesce all ALLOWs and DENYs before mapping to mode
106 * bits. */
107
108 static void
low_mode_from_nfs4(u32 perm,unsigned short * mode,unsigned int flags)109 low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
110 {
111 u32 write_mode = NFS4_WRITE_MODE;
112
113 if (flags & NFS4_ACL_DIR)
114 write_mode |= NFS4_ACE_DELETE_CHILD;
115 *mode = 0;
116 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
117 *mode |= ACL_READ;
118 if ((perm & write_mode) == write_mode)
119 *mode |= ACL_WRITE;
120 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
121 *mode |= ACL_EXECUTE;
122 }
123
124 static short ace2type(struct nfs4_ace *);
125 static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
126 unsigned int);
127
128 int
nfsd4_get_nfs4_acl(struct svc_rqst * rqstp,struct dentry * dentry,struct nfs4_acl ** acl)129 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry,
130 struct nfs4_acl **acl)
131 {
132 struct inode *inode = d_inode(dentry);
133 int error = 0;
134 struct posix_acl *pacl = NULL, *dpacl = NULL;
135 unsigned int flags = 0;
136 int size = 0;
137
138 pacl = get_inode_acl(inode, ACL_TYPE_ACCESS);
139 if (!pacl)
140 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
141
142 if (IS_ERR(pacl))
143 return PTR_ERR(pacl);
144
145 /* allocate for worst case: one (deny, allow) pair each: */
146 size += 2 * pacl->a_count;
147
148 if (S_ISDIR(inode->i_mode)) {
149 flags = NFS4_ACL_DIR;
150 dpacl = get_inode_acl(inode, ACL_TYPE_DEFAULT);
151 if (IS_ERR(dpacl)) {
152 error = PTR_ERR(dpacl);
153 goto rel_pacl;
154 }
155
156 if (dpacl)
157 size += 2 * dpacl->a_count;
158 }
159
160 *acl = kmalloc(nfs4_acl_bytes(size), GFP_KERNEL);
161 if (*acl == NULL) {
162 error = -ENOMEM;
163 goto out;
164 }
165 (*acl)->naces = 0;
166
167 _posix_to_nfsv4_one(pacl, *acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
168
169 if (dpacl)
170 _posix_to_nfsv4_one(dpacl, *acl, flags | NFS4_ACL_TYPE_DEFAULT);
171
172 out:
173 posix_acl_release(dpacl);
174 rel_pacl:
175 posix_acl_release(pacl);
176 return error;
177 }
178
179 struct posix_acl_summary {
180 unsigned short owner;
181 unsigned short users;
182 unsigned short group;
183 unsigned short groups;
184 unsigned short other;
185 unsigned short mask;
186 };
187
188 static void
summarize_posix_acl(struct posix_acl * acl,struct posix_acl_summary * pas)189 summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
190 {
191 struct posix_acl_entry *pa, *pe;
192
193 /*
194 * Only pas.users and pas.groups need initialization; previous
195 * posix_acl_valid() calls ensure that the other fields will be
196 * initialized in the following loop. But, just to placate gcc:
197 */
198 memset(pas, 0, sizeof(*pas));
199 pas->mask = 07;
200
201 FOREACH_ACL_ENTRY(pa, acl, pe) {
202 switch (pa->e_tag) {
203 case ACL_USER_OBJ:
204 pas->owner = pa->e_perm;
205 break;
206 case ACL_GROUP_OBJ:
207 pas->group = pa->e_perm;
208 break;
209 case ACL_USER:
210 pas->users |= pa->e_perm;
211 break;
212 case ACL_GROUP:
213 pas->groups |= pa->e_perm;
214 break;
215 case ACL_OTHER:
216 pas->other = pa->e_perm;
217 break;
218 case ACL_MASK:
219 pas->mask = pa->e_perm;
220 break;
221 }
222 }
223 /* We'll only care about effective permissions: */
224 pas->users &= pas->mask;
225 pas->group &= pas->mask;
226 pas->groups &= pas->mask;
227 }
228
229 /* We assume the acl has been verified with posix_acl_valid. */
230 static void
_posix_to_nfsv4_one(struct posix_acl * pacl,struct nfs4_acl * acl,unsigned int flags)231 _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
232 unsigned int flags)
233 {
234 struct posix_acl_entry *pa, *group_owner_entry;
235 struct nfs4_ace *ace;
236 struct posix_acl_summary pas;
237 unsigned short deny;
238 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
239 NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
240
241 BUG_ON(pacl->a_count < 3);
242 summarize_posix_acl(pacl, &pas);
243
244 pa = pacl->a_entries;
245 ace = acl->aces + acl->naces;
246
247 /* We could deny everything not granted by the owner: */
248 deny = ~pas.owner;
249 /*
250 * but it is equivalent (and simpler) to deny only what is not
251 * granted by later entries:
252 */
253 deny &= pas.users | pas.group | pas.groups | pas.other;
254 if (deny) {
255 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
256 ace->flag = eflag;
257 ace->access_mask = deny_mask_from_posix(deny, flags);
258 ace->whotype = NFS4_ACL_WHO_OWNER;
259 ace++;
260 acl->naces++;
261 }
262
263 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
264 ace->flag = eflag;
265 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
266 ace->whotype = NFS4_ACL_WHO_OWNER;
267 ace++;
268 acl->naces++;
269 pa++;
270
271 while (pa->e_tag == ACL_USER) {
272 deny = ~(pa->e_perm & pas.mask);
273 deny &= pas.groups | pas.group | pas.other;
274 if (deny) {
275 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
276 ace->flag = eflag;
277 ace->access_mask = deny_mask_from_posix(deny, flags);
278 ace->whotype = NFS4_ACL_WHO_NAMED;
279 ace->who_uid = pa->e_uid;
280 ace++;
281 acl->naces++;
282 }
283 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
284 ace->flag = eflag;
285 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
286 flags);
287 ace->whotype = NFS4_ACL_WHO_NAMED;
288 ace->who_uid = pa->e_uid;
289 ace++;
290 acl->naces++;
291 pa++;
292 }
293
294 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
295 * since a user can be in more than one group. */
296
297 /* allow ACEs */
298
299 group_owner_entry = pa;
300
301 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
302 ace->flag = eflag;
303 ace->access_mask = mask_from_posix(pas.group, flags);
304 ace->whotype = NFS4_ACL_WHO_GROUP;
305 ace++;
306 acl->naces++;
307 pa++;
308
309 while (pa->e_tag == ACL_GROUP) {
310 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
311 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
312 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
313 flags);
314 ace->whotype = NFS4_ACL_WHO_NAMED;
315 ace->who_gid = pa->e_gid;
316 ace++;
317 acl->naces++;
318 pa++;
319 }
320
321 /* deny ACEs */
322
323 pa = group_owner_entry;
324
325 deny = ~pas.group & pas.other;
326 if (deny) {
327 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
328 ace->flag = eflag;
329 ace->access_mask = deny_mask_from_posix(deny, flags);
330 ace->whotype = NFS4_ACL_WHO_GROUP;
331 ace++;
332 acl->naces++;
333 }
334 pa++;
335
336 while (pa->e_tag == ACL_GROUP) {
337 deny = ~(pa->e_perm & pas.mask);
338 deny &= pas.other;
339 if (deny) {
340 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
341 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
342 ace->access_mask = deny_mask_from_posix(deny, flags);
343 ace->whotype = NFS4_ACL_WHO_NAMED;
344 ace->who_gid = pa->e_gid;
345 ace++;
346 acl->naces++;
347 }
348 pa++;
349 }
350
351 if (pa->e_tag == ACL_MASK)
352 pa++;
353 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
354 ace->flag = eflag;
355 ace->access_mask = mask_from_posix(pa->e_perm, flags);
356 ace->whotype = NFS4_ACL_WHO_EVERYONE;
357 acl->naces++;
358 }
359
360 static bool
pace_gt(struct posix_acl_entry * pace1,struct posix_acl_entry * pace2)361 pace_gt(struct posix_acl_entry *pace1, struct posix_acl_entry *pace2)
362 {
363 if (pace1->e_tag != pace2->e_tag)
364 return pace1->e_tag > pace2->e_tag;
365 if (pace1->e_tag == ACL_USER)
366 return uid_gt(pace1->e_uid, pace2->e_uid);
367 if (pace1->e_tag == ACL_GROUP)
368 return gid_gt(pace1->e_gid, pace2->e_gid);
369 return false;
370 }
371
372 /**
373 * sort_pacl_range - sort a range of POSIX ACL entries by tag and id
374 * @pacl: POSIX ACL containing entries to sort
375 * @start: starting index of range to sort
376 * @end: ending index of range to sort (inclusive)
377 *
378 * Sorts ACL entries in place so that USER entries are ordered by UID
379 * and GROUP entries are ordered by GID. Required before calling
380 * posix_acl_valid().
381 */
sort_pacl_range(struct posix_acl * pacl,int start,int end)382 void sort_pacl_range(struct posix_acl *pacl, int start, int end)
383 {
384 int sorted = 0, i;
385
386 /* Bubble sort: acceptable here because ACLs are typically short. */
387 while (!sorted) {
388 sorted = 1;
389 for (i = start; i < end; i++) {
390 if (pace_gt(&pacl->a_entries[i],
391 &pacl->a_entries[i+1])) {
392 sorted = 0;
393 swap(pacl->a_entries[i],
394 pacl->a_entries[i + 1]);
395 }
396 }
397 }
398 }
399
400 static void
sort_pacl(struct posix_acl * pacl)401 sort_pacl(struct posix_acl *pacl)
402 {
403 /* posix_acl_valid requires that users and groups be in order
404 * by uid/gid. */
405 int i, j;
406
407 /* no users or groups */
408 if (!pacl || pacl->a_count <= 4)
409 return;
410
411 i = 1;
412 while (pacl->a_entries[i].e_tag == ACL_USER)
413 i++;
414 sort_pacl_range(pacl, 1, i-1);
415
416 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
417 j = ++i;
418 while (pacl->a_entries[j].e_tag == ACL_GROUP)
419 j++;
420 sort_pacl_range(pacl, i, j-1);
421 return;
422 }
423
424 /*
425 * While processing the NFSv4 ACE, this maintains bitmasks representing
426 * which permission bits have been allowed and which denied to a given
427 * entity: */
428 struct posix_ace_state {
429 u32 allow;
430 u32 deny;
431 };
432
433 struct posix_user_ace_state {
434 union {
435 kuid_t uid;
436 kgid_t gid;
437 };
438 struct posix_ace_state perms;
439 };
440
441 struct posix_ace_state_array {
442 int n;
443 struct posix_user_ace_state aces[];
444 };
445
446 /*
447 * While processing the NFSv4 ACE, this maintains the partial permissions
448 * calculated so far: */
449
450 struct posix_acl_state {
451 unsigned char valid;
452 struct posix_ace_state owner;
453 struct posix_ace_state group;
454 struct posix_ace_state other;
455 struct posix_ace_state everyone;
456 struct posix_ace_state mask; /* Deny unused in this case */
457 struct posix_ace_state_array *users;
458 struct posix_ace_state_array *groups;
459 };
460
461 static int
init_state(struct posix_acl_state * state,int cnt)462 init_state(struct posix_acl_state *state, int cnt)
463 {
464 int alloc;
465
466 memset(state, 0, sizeof(struct posix_acl_state));
467 /*
468 * In the worst case, each individual acl could be for a distinct
469 * named user or group, but we don't know which, so we allocate
470 * enough space for either:
471 */
472 alloc = sizeof(struct posix_ace_state_array)
473 + cnt*sizeof(struct posix_user_ace_state);
474 state->users = kzalloc(alloc, GFP_KERNEL);
475 if (!state->users)
476 return -ENOMEM;
477 state->groups = kzalloc(alloc, GFP_KERNEL);
478 if (!state->groups) {
479 kfree(state->users);
480 return -ENOMEM;
481 }
482 return 0;
483 }
484
485 static void
free_state(struct posix_acl_state * state)486 free_state(struct posix_acl_state *state) {
487 kfree(state->users);
488 kfree(state->groups);
489 }
490
add_to_mask(struct posix_acl_state * state,struct posix_ace_state * astate)491 static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
492 {
493 state->mask.allow |= astate->allow;
494 }
495
496 static struct posix_acl *
posix_state_to_acl(struct posix_acl_state * state,unsigned int flags)497 posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
498 {
499 struct posix_acl_entry *pace;
500 struct posix_acl *pacl;
501 int nace;
502 int i;
503
504 /*
505 * ACLs with no ACEs are treated differently in the inheritable
506 * and effective cases: when there are no inheritable ACEs,
507 * calls ->set_acl with a NULL ACL structure.
508 */
509 if (!state->valid && (flags & NFS4_ACL_TYPE_DEFAULT))
510 return NULL;
511
512 /*
513 * When there are no effective ACEs, the following will end
514 * up setting a 3-element effective posix ACL with all
515 * permissions zero.
516 */
517 if (!state->users->n && !state->groups->n)
518 nace = 3;
519 else /* Note we also include a MASK ACE in this case: */
520 nace = 4 + state->users->n + state->groups->n;
521 pacl = posix_acl_alloc(nace, GFP_KERNEL);
522 if (!pacl)
523 return ERR_PTR(-ENOMEM);
524
525 pace = pacl->a_entries;
526 pace->e_tag = ACL_USER_OBJ;
527 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
528
529 for (i=0; i < state->users->n; i++) {
530 pace++;
531 pace->e_tag = ACL_USER;
532 low_mode_from_nfs4(state->users->aces[i].perms.allow,
533 &pace->e_perm, flags);
534 pace->e_uid = state->users->aces[i].uid;
535 add_to_mask(state, &state->users->aces[i].perms);
536 }
537
538 pace++;
539 pace->e_tag = ACL_GROUP_OBJ;
540 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
541 add_to_mask(state, &state->group);
542
543 for (i=0; i < state->groups->n; i++) {
544 pace++;
545 pace->e_tag = ACL_GROUP;
546 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
547 &pace->e_perm, flags);
548 pace->e_gid = state->groups->aces[i].gid;
549 add_to_mask(state, &state->groups->aces[i].perms);
550 }
551
552 if (state->users->n || state->groups->n) {
553 pace++;
554 pace->e_tag = ACL_MASK;
555 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
556 }
557
558 pace++;
559 pace->e_tag = ACL_OTHER;
560 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
561
562 return pacl;
563 }
564
allow_bits(struct posix_ace_state * astate,u32 mask)565 static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
566 {
567 /* Allow all bits in the mask not already denied: */
568 astate->allow |= mask & ~astate->deny;
569 }
570
deny_bits(struct posix_ace_state * astate,u32 mask)571 static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
572 {
573 /* Deny all bits in the mask not already allowed: */
574 astate->deny |= mask & ~astate->allow;
575 }
576
find_uid(struct posix_acl_state * state,kuid_t uid)577 static int find_uid(struct posix_acl_state *state, kuid_t uid)
578 {
579 struct posix_ace_state_array *a = state->users;
580 int i;
581
582 for (i = 0; i < a->n; i++)
583 if (uid_eq(a->aces[i].uid, uid))
584 return i;
585 /* Not found: */
586 a->n++;
587 a->aces[i].uid = uid;
588 a->aces[i].perms.allow = state->everyone.allow;
589 a->aces[i].perms.deny = state->everyone.deny;
590
591 return i;
592 }
593
find_gid(struct posix_acl_state * state,kgid_t gid)594 static int find_gid(struct posix_acl_state *state, kgid_t gid)
595 {
596 struct posix_ace_state_array *a = state->groups;
597 int i;
598
599 for (i = 0; i < a->n; i++)
600 if (gid_eq(a->aces[i].gid, gid))
601 return i;
602 /* Not found: */
603 a->n++;
604 a->aces[i].gid = gid;
605 a->aces[i].perms.allow = state->everyone.allow;
606 a->aces[i].perms.deny = state->everyone.deny;
607
608 return i;
609 }
610
deny_bits_array(struct posix_ace_state_array * a,u32 mask)611 static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
612 {
613 int i;
614
615 for (i=0; i < a->n; i++)
616 deny_bits(&a->aces[i].perms, mask);
617 }
618
allow_bits_array(struct posix_ace_state_array * a,u32 mask)619 static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
620 {
621 int i;
622
623 for (i=0; i < a->n; i++)
624 allow_bits(&a->aces[i].perms, mask);
625 }
626
process_one_v4_ace(struct posix_acl_state * state,struct nfs4_ace * ace)627 static void process_one_v4_ace(struct posix_acl_state *state,
628 struct nfs4_ace *ace)
629 {
630 u32 mask = ace->access_mask;
631 short type = ace2type(ace);
632 int i;
633
634 state->valid |= type;
635
636 switch (type) {
637 case ACL_USER_OBJ:
638 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
639 allow_bits(&state->owner, mask);
640 } else {
641 deny_bits(&state->owner, mask);
642 }
643 break;
644 case ACL_USER:
645 i = find_uid(state, ace->who_uid);
646 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
647 allow_bits(&state->users->aces[i].perms, mask);
648 } else {
649 deny_bits(&state->users->aces[i].perms, mask);
650 mask = state->users->aces[i].perms.deny;
651 deny_bits(&state->owner, mask);
652 }
653 break;
654 case ACL_GROUP_OBJ:
655 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
656 allow_bits(&state->group, mask);
657 } else {
658 deny_bits(&state->group, mask);
659 mask = state->group.deny;
660 deny_bits(&state->owner, mask);
661 deny_bits(&state->everyone, mask);
662 deny_bits_array(state->users, mask);
663 deny_bits_array(state->groups, mask);
664 }
665 break;
666 case ACL_GROUP:
667 i = find_gid(state, ace->who_gid);
668 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
669 allow_bits(&state->groups->aces[i].perms, mask);
670 } else {
671 deny_bits(&state->groups->aces[i].perms, mask);
672 mask = state->groups->aces[i].perms.deny;
673 deny_bits(&state->owner, mask);
674 deny_bits(&state->group, mask);
675 deny_bits(&state->everyone, mask);
676 deny_bits_array(state->users, mask);
677 deny_bits_array(state->groups, mask);
678 }
679 break;
680 case ACL_OTHER:
681 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
682 allow_bits(&state->owner, mask);
683 allow_bits(&state->group, mask);
684 allow_bits(&state->other, mask);
685 allow_bits(&state->everyone, mask);
686 allow_bits_array(state->users, mask);
687 allow_bits_array(state->groups, mask);
688 } else {
689 deny_bits(&state->owner, mask);
690 deny_bits(&state->group, mask);
691 deny_bits(&state->other, mask);
692 deny_bits(&state->everyone, mask);
693 deny_bits_array(state->users, mask);
694 deny_bits_array(state->groups, mask);
695 }
696 }
697 }
698
nfs4_acl_nfsv4_to_posix(struct nfs4_acl * acl,struct posix_acl ** pacl,struct posix_acl ** dpacl,unsigned int flags)699 static int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl,
700 struct posix_acl **pacl, struct posix_acl **dpacl,
701 unsigned int flags)
702 {
703 struct posix_acl_state effective_acl_state, default_acl_state;
704 struct nfs4_ace *ace;
705 int ret;
706
707 ret = init_state(&effective_acl_state, acl->naces);
708 if (ret)
709 return ret;
710 ret = init_state(&default_acl_state, acl->naces);
711 if (ret)
712 goto out_estate;
713 ret = -EINVAL;
714 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
715 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
716 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
717 goto out_dstate;
718 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
719 goto out_dstate;
720 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
721 process_one_v4_ace(&effective_acl_state, ace);
722 continue;
723 }
724 if (!(flags & NFS4_ACL_DIR))
725 goto out_dstate;
726 /*
727 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
728 * is set, we're effectively turning on the other. That's OK,
729 * according to rfc 3530.
730 */
731 process_one_v4_ace(&default_acl_state, ace);
732
733 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
734 process_one_v4_ace(&effective_acl_state, ace);
735 }
736
737 /*
738 * At this point, the default ACL may have zeroed-out entries for owner,
739 * group and other. That usually results in a non-sensical resulting ACL
740 * that denies all access except to any ACE that was explicitly added.
741 *
742 * The setfacl command solves a similar problem with this logic:
743 *
744 * "If a Default ACL entry is created, and the Default ACL contains
745 * no owner, owning group, or others entry, a copy of the ACL
746 * owner, owning group, or others entry is added to the Default ACL."
747 *
748 * Copy any missing ACEs from the effective set, if any ACEs were
749 * explicitly set.
750 */
751 if (default_acl_state.valid) {
752 if (!(default_acl_state.valid & ACL_USER_OBJ))
753 default_acl_state.owner = effective_acl_state.owner;
754 if (!(default_acl_state.valid & ACL_GROUP_OBJ))
755 default_acl_state.group = effective_acl_state.group;
756 if (!(default_acl_state.valid & ACL_OTHER))
757 default_acl_state.other = effective_acl_state.other;
758 }
759
760 *pacl = posix_state_to_acl(&effective_acl_state, flags);
761 if (IS_ERR(*pacl)) {
762 ret = PTR_ERR(*pacl);
763 *pacl = NULL;
764 goto out_dstate;
765 }
766 *dpacl = posix_state_to_acl(&default_acl_state,
767 flags | NFS4_ACL_TYPE_DEFAULT);
768 if (IS_ERR(*dpacl)) {
769 ret = PTR_ERR(*dpacl);
770 *dpacl = NULL;
771 posix_acl_release(*pacl);
772 *pacl = NULL;
773 goto out_dstate;
774 }
775 sort_pacl(*pacl);
776 sort_pacl(*dpacl);
777 ret = 0;
778 out_dstate:
779 free_state(&default_acl_state);
780 out_estate:
781 free_state(&effective_acl_state);
782 return ret;
783 }
784
nfsd4_acl_to_attr(enum nfs_ftype4 type,struct nfs4_acl * acl,struct nfsd_attrs * attr)785 __be32 nfsd4_acl_to_attr(enum nfs_ftype4 type, struct nfs4_acl *acl,
786 struct nfsd_attrs *attr)
787 {
788 int host_error;
789 unsigned int flags = 0;
790
791 if (!acl)
792 return nfs_ok;
793
794 if (type == NF4DIR)
795 flags = NFS4_ACL_DIR;
796
797 host_error = nfs4_acl_nfsv4_to_posix(acl, &attr->na_pacl,
798 &attr->na_dpacl, flags);
799 if (host_error == -EINVAL)
800 return nfserr_attrnotsupp;
801 else
802 return nfserrno(host_error);
803 }
804
805 static short
ace2type(struct nfs4_ace * ace)806 ace2type(struct nfs4_ace *ace)
807 {
808 switch (ace->whotype) {
809 case NFS4_ACL_WHO_NAMED:
810 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
811 ACL_GROUP : ACL_USER);
812 case NFS4_ACL_WHO_OWNER:
813 return ACL_USER_OBJ;
814 case NFS4_ACL_WHO_GROUP:
815 return ACL_GROUP_OBJ;
816 case NFS4_ACL_WHO_EVERYONE:
817 return ACL_OTHER;
818 }
819 BUG();
820 return -1;
821 }
822
823 /*
824 * return the size of the struct nfs4_acl required to represent an acl
825 * with @entries entries.
826 */
nfs4_acl_bytes(int entries)827 int nfs4_acl_bytes(int entries)
828 {
829 return sizeof(struct nfs4_acl) + entries * sizeof(struct nfs4_ace);
830 }
831
832 static struct {
833 char *string;
834 int stringlen;
835 int type;
836 } s2t_map[] = {
837 {
838 .string = "OWNER@",
839 .stringlen = sizeof("OWNER@") - 1,
840 .type = NFS4_ACL_WHO_OWNER,
841 },
842 {
843 .string = "GROUP@",
844 .stringlen = sizeof("GROUP@") - 1,
845 .type = NFS4_ACL_WHO_GROUP,
846 },
847 {
848 .string = "EVERYONE@",
849 .stringlen = sizeof("EVERYONE@") - 1,
850 .type = NFS4_ACL_WHO_EVERYONE,
851 },
852 };
853
854 int
nfs4_acl_get_whotype(char * p,u32 len)855 nfs4_acl_get_whotype(char *p, u32 len)
856 {
857 int i;
858
859 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
860 if (s2t_map[i].stringlen == len &&
861 0 == memcmp(s2t_map[i].string, p, len))
862 return s2t_map[i].type;
863 }
864 return NFS4_ACL_WHO_NAMED;
865 }
866
nfs4_acl_write_who(struct xdr_stream * xdr,int who)867 __be32 nfs4_acl_write_who(struct xdr_stream *xdr, int who)
868 {
869 __be32 *p;
870 int i;
871
872 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
873 if (s2t_map[i].type != who)
874 continue;
875 p = xdr_reserve_space(xdr, s2t_map[i].stringlen + 4);
876 if (!p)
877 return nfserr_resource;
878 p = xdr_encode_opaque(p, s2t_map[i].string,
879 s2t_map[i].stringlen);
880 return 0;
881 }
882 WARN_ON_ONCE(1);
883 return nfserr_serverfault;
884 }
885