1 /*-
2 * Copyright (c) 2008-2009 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 /*
28 * ACL support routines specific to NFSv4 access control lists. These are
29 * utility routines for code common across file systems implementing NFSv4
30 * ACLs.
31 */
32
33 #ifdef _KERNEL
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: releng/8.2/sys/kern/subr_acl_nfs4.c 214627 2010-11-01 15:43:42Z trasz $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mount.h>
40 #include <sys/priv.h>
41 #include <sys/vnode.h>
42 #include <sys/errno.h>
43 #include <sys/stat.h>
44 #include <sys/acl.h>
45 #else
46 #include <errno.h>
47 #include <assert.h>
48 #include <sys/acl.h>
49 #include <sys/stat.h>
50 #define KASSERT(a, b) assert(a)
51 #define CTASSERT(a)
52 #endif /* _KERNEL */
53
54 #ifdef _KERNEL
55
56 static struct {
57 accmode_t accmode;
58 int mask;
59 } accmode2mask[] = {{VREAD, ACL_READ_DATA},
60 {VWRITE, ACL_WRITE_DATA},
61 {VAPPEND, ACL_APPEND_DATA},
62 {VEXEC, ACL_EXECUTE},
63 {VREAD_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
64 {VWRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
65 {VDELETE_CHILD, ACL_DELETE_CHILD},
66 {VREAD_ATTRIBUTES, ACL_READ_ATTRIBUTES},
67 {VWRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
68 {VDELETE, ACL_DELETE},
69 {VREAD_ACL, ACL_READ_ACL},
70 {VWRITE_ACL, ACL_WRITE_ACL},
71 {VWRITE_OWNER, ACL_WRITE_OWNER},
72 {VSYNCHRONIZE, ACL_SYNCHRONIZE},
73 {0, 0}};
74
75 static int
76 _access_mask_from_accmode(accmode_t accmode)
77 {
78 int access_mask = 0, i;
79
80 for (i = 0; accmode2mask[i].accmode != 0; i++) {
81 if (accmode & accmode2mask[i].accmode)
82 access_mask |= accmode2mask[i].mask;
83 }
84
85 /*
86 * VAPPEND is just a modifier for VWRITE; if the caller asked
87 * for 'VAPPEND | VWRITE', we want to check for ACL_APPEND_DATA only.
88 */
89 if (access_mask & ACL_APPEND_DATA)
90 access_mask &= ~ACL_WRITE_DATA;
91
92 return (access_mask);
93 }
94
95 /*
96 * Return 0, iff access is allowed, 1 otherwise.
97 */
98 static int
99 _acl_denies(const struct acl *aclp, int access_mask, struct ucred *cred,
100 int file_uid, int file_gid, int *denied_explicitly)
101 {
102 int i;
103 const struct acl_entry *entry;
104
105 if (denied_explicitly != NULL)
106 *denied_explicitly = 0;
107
108 KASSERT(aclp->acl_cnt > 0, ("aclp->acl_cnt > 0"));
109 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
110 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
111
112 for (i = 0; i < aclp->acl_cnt; i++) {
113 entry = &(aclp->acl_entry[i]);
114
115 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
116 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
117 continue;
118 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
119 continue;
120 switch (entry->ae_tag) {
121 case ACL_USER_OBJ:
122 if (file_uid != cred->cr_uid)
123 continue;
124 break;
125 case ACL_USER:
126 if (entry->ae_id != cred->cr_uid)
127 continue;
128 break;
129 case ACL_GROUP_OBJ:
130 if (!groupmember(file_gid, cred))
131 continue;
132 break;
133 case ACL_GROUP:
134 if (!groupmember(entry->ae_id, cred))
135 continue;
136 break;
137 default:
138 KASSERT(entry->ae_tag == ACL_EVERYONE,
139 ("entry->ae_tag == ACL_EVERYONE"));
140 }
141
142 if (entry->ae_entry_type == ACL_ENTRY_TYPE_DENY) {
143 if (entry->ae_perm & access_mask) {
144 if (denied_explicitly != NULL)
145 *denied_explicitly = 1;
146 return (1);
147 }
148 }
149
150 access_mask &= ~(entry->ae_perm);
151 if (access_mask == 0)
152 return (0);
153 }
154
155 return (1);
156 }
157
158 int
159 vaccess_acl_nfs4(enum vtype type, uid_t file_uid, gid_t file_gid,
160 struct acl *aclp, accmode_t accmode, struct ucred *cred, int *privused)
161 {
162 accmode_t priv_granted = 0;
163 int denied, explicitly_denied, access_mask, is_directory,
164 must_be_owner = 0;
165
166 if (privused != NULL)
167 *privused = 0;
168
169 if (accmode & VADMIN)
170 must_be_owner = 1;
171
172 /*
173 * Ignore VSYNCHRONIZE permission.
174 */
175 accmode &= ~VSYNCHRONIZE;
176
177 access_mask = _access_mask_from_accmode(accmode);
178
179 if (type == VDIR)
180 is_directory = 1;
181 else
182 is_directory = 0;
183
184 /*
185 * File owner is always allowed to read and write the ACL
186 * and basic attributes. This is to prevent a situation
187 * where user would change ACL in a way that prevents him
188 * from undoing the change.
189 */
190 if (file_uid == cred->cr_uid)
191 access_mask &= ~(ACL_READ_ACL | ACL_WRITE_ACL |
192 ACL_READ_ATTRIBUTES | ACL_WRITE_ATTRIBUTES);
193
194 /*
195 * Ignore append permission for regular files; use write
196 * permission instead.
197 */
198 if (!is_directory && (access_mask & ACL_APPEND_DATA)) {
199 access_mask &= ~ACL_APPEND_DATA;
200 access_mask |= ACL_WRITE_DATA;
201 }
202
203 denied = _acl_denies(aclp, access_mask, cred, file_uid, file_gid,
204 &explicitly_denied);
205
206 if (must_be_owner) {
207 if (file_uid != cred->cr_uid)
208 denied = EPERM;
209 }
210
211 if (!denied)
212 return (0);
213
214 /*
215 * Access failed. Iff it was not denied explicitly and
216 * VEXPLICIT_DENY flag was specified, allow access.
217 */
218 if ((accmode & VEXPLICIT_DENY) && explicitly_denied == 0)
219 return (0);
220
221 accmode &= ~VEXPLICIT_DENY;
222
223 /*
224 * No match. Try to use privileges, if there are any.
225 */
226 if (is_directory) {
227 if ((accmode & VEXEC) && !priv_check_cred(cred,
228 PRIV_VFS_LOOKUP, 0))
229 priv_granted |= VEXEC;
230 } else {
231 if ((accmode & VEXEC) && !priv_check_cred(cred,
232 PRIV_VFS_EXEC, 0))
233 priv_granted |= VEXEC;
234 }
235
236 if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ, 0))
237 priv_granted |= VREAD;
238
239 if ((accmode & (VWRITE | VAPPEND | VDELETE_CHILD)) &&
240 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
241 priv_granted |= (VWRITE | VAPPEND | VDELETE_CHILD);
242
243 if ((accmode & VADMIN_PERMS) &&
244 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
245 priv_granted |= VADMIN_PERMS;
246
247 if ((accmode & VSTAT_PERMS) &&
248 !priv_check_cred(cred, PRIV_VFS_STAT, 0))
249 priv_granted |= VSTAT_PERMS;
250
251 if ((accmode & priv_granted) == accmode) {
252 if (privused != NULL)
253 *privused = 1;
254
255 return (0);
256 }
257
258 if (accmode & (VADMIN_PERMS | VDELETE_CHILD | VDELETE))
259 denied = EPERM;
260 else
261 denied = EACCES;
262
263 return (denied);
264 }
265 #endif /* _KERNEL */
266
267 static int
268 _acl_entry_matches(struct acl_entry *entry, acl_tag_t tag, acl_perm_t perm,
269 acl_entry_type_t entry_type)
270 {
271 if (entry->ae_tag != tag)
272 return (0);
273
274 if (entry->ae_id != ACL_UNDEFINED_ID)
275 return (0);
276
277 if (entry->ae_perm != perm)
278 return (0);
279
280 if (entry->ae_entry_type != entry_type)
281 return (0);
282
283 if (entry->ae_flags != 0)
284 return (0);
285
286 return (1);
287 }
288
289 static struct acl_entry *
290 _acl_append(struct acl *aclp, acl_tag_t tag, acl_perm_t perm,
291 acl_entry_type_t entry_type)
292 {
293 struct acl_entry *entry;
294
295 KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
296 ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
297
298 entry = &(aclp->acl_entry[aclp->acl_cnt]);
299 aclp->acl_cnt++;
300
301 entry->ae_tag = tag;
302 entry->ae_id = ACL_UNDEFINED_ID;
303 entry->ae_perm = perm;
304 entry->ae_entry_type = entry_type;
305 entry->ae_flags = 0;
306
307 return (entry);
308 }
309
310 static struct acl_entry *
311 _acl_duplicate_entry(struct acl *aclp, int entry_index)
312 {
313 int i;
314
315 KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
316 ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
317
318 for (i = aclp->acl_cnt; i > entry_index; i--)
319 aclp->acl_entry[i] = aclp->acl_entry[i - 1];
320
321 aclp->acl_cnt++;
322
323 return (&(aclp->acl_entry[entry_index + 1]));
324 }
325
326 /*
327 * Calculate trivial ACL in a manner compatible with PSARC/2010/029.
328 * Note that this results in an ACL different from (but semantically
329 * equal to) the "canonical six" trivial ACL computed using algorithm
330 * described in draft-ietf-nfsv4-minorversion1-03.txt, 3.16.6.2.
331 */
332 void
333 acl_nfs4_trivial_from_mode(struct acl *aclp, mode_t mode)
334 {
335 acl_perm_t user_allow_first = 0, user_deny = 0, group_deny = 0;
336 acl_perm_t user_allow, group_allow, everyone_allow;
337
338 KASSERT(aclp->acl_cnt == 0, ("aclp->acl_cnt == 0"));
339
340 user_allow = group_allow = everyone_allow = ACL_READ_ACL |
341 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS | ACL_SYNCHRONIZE;
342 user_allow |= ACL_WRITE_ACL | ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
343 ACL_WRITE_NAMED_ATTRS;
344
345 if (mode & S_IRUSR)
346 user_allow |= ACL_READ_DATA;
347 if (mode & S_IWUSR)
348 user_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
349 if (mode & S_IXUSR)
350 user_allow |= ACL_EXECUTE;
351
352 if (mode & S_IRGRP)
353 group_allow |= ACL_READ_DATA;
354 if (mode & S_IWGRP)
355 group_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
356 if (mode & S_IXGRP)
357 group_allow |= ACL_EXECUTE;
358
359 if (mode & S_IROTH)
360 everyone_allow |= ACL_READ_DATA;
361 if (mode & S_IWOTH)
362 everyone_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
363 if (mode & S_IXOTH)
364 everyone_allow |= ACL_EXECUTE;
365
366 user_deny = ((group_allow | everyone_allow) & ~user_allow);
367 group_deny = everyone_allow & ~group_allow;
368 user_allow_first = group_deny & ~user_deny;
369
370 if (user_allow_first != 0)
371 _acl_append(aclp, ACL_USER_OBJ, user_allow_first, ACL_ENTRY_TYPE_ALLOW);
372 if (user_deny != 0)
373 _acl_append(aclp, ACL_USER_OBJ, user_deny, ACL_ENTRY_TYPE_DENY);
374 if (group_deny != 0)
375 _acl_append(aclp, ACL_GROUP_OBJ, group_deny, ACL_ENTRY_TYPE_DENY);
376 _acl_append(aclp, ACL_USER_OBJ, user_allow, ACL_ENTRY_TYPE_ALLOW);
377 _acl_append(aclp, ACL_GROUP_OBJ, group_allow, ACL_ENTRY_TYPE_ALLOW);
378 _acl_append(aclp, ACL_EVERYONE, everyone_allow, ACL_ENTRY_TYPE_ALLOW);
379 }
380
381 void
382 acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode, int file_owner_id)
383 {
384 int i, meets, must_append;
385 struct acl_entry *entry, *copy, *previous,
386 *a1, *a2, *a3, *a4, *a5, *a6;
387 mode_t amode;
388 const int READ = 04;
389 const int WRITE = 02;
390 const int EXEC = 01;
391
392 KASSERT(aclp->acl_cnt >= 0, ("aclp->acl_cnt >= 0"));
393 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
394 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
395
396 /*
397 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
398 *
399 * 3.16.6.3. Applying a Mode to an Existing ACL
400 */
401
402 /*
403 * 1. For each ACE:
404 */
405 for (i = 0; i < aclp->acl_cnt; i++) {
406 entry = &(aclp->acl_entry[i]);
407
408 /*
409 * 1.1. If the type is neither ALLOW or DENY - skip.
410 */
411 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
412 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
413 continue;
414
415 /*
416 * 1.2. If ACL_ENTRY_INHERIT_ONLY is set - skip.
417 */
418 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
419 continue;
420
421 /*
422 * 1.3. If ACL_ENTRY_FILE_INHERIT or ACL_ENTRY_DIRECTORY_INHERIT
423 * are set:
424 */
425 if (entry->ae_flags &
426 (ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT)) {
427 /*
428 * 1.3.1. A copy of the current ACE is made, and placed
429 * in the ACL immediately following the current
430 * ACE.
431 */
432 copy = _acl_duplicate_entry(aclp, i);
433
434 /*
435 * 1.3.2. In the first ACE, the flag
436 * ACL_ENTRY_INHERIT_ONLY is set.
437 */
438 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
439
440 /*
441 * 1.3.3. In the second ACE, the following flags
442 * are cleared:
443 * ACL_ENTRY_FILE_INHERIT,
444 * ACL_ENTRY_DIRECTORY_INHERIT,
445 * ACL_ENTRY_NO_PROPAGATE_INHERIT.
446 */
447 copy->ae_flags &= ~(ACL_ENTRY_FILE_INHERIT |
448 ACL_ENTRY_DIRECTORY_INHERIT |
449 ACL_ENTRY_NO_PROPAGATE_INHERIT);
450
451 /*
452 * The algorithm continues on with the second ACE.
453 */
454 i++;
455 entry = copy;
456 }
457
458 /*
459 * 1.4. If it's owner@, group@ or everyone@ entry, clear
460 * ACL_READ_DATA, ACL_WRITE_DATA, ACL_APPEND_DATA
461 * and ACL_EXECUTE. Continue to the next entry.
462 */
463 if (entry->ae_tag == ACL_USER_OBJ ||
464 entry->ae_tag == ACL_GROUP_OBJ ||
465 entry->ae_tag == ACL_EVERYONE) {
466 entry->ae_perm &= ~(ACL_READ_DATA | ACL_WRITE_DATA |
467 ACL_APPEND_DATA | ACL_EXECUTE);
468 continue;
469 }
470
471 /*
472 * 1.5. Otherwise, if the "who" field did not match one
473 * of OWNER@, GROUP@, EVERYONE@:
474 *
475 * 1.5.1. If the type is ALLOW, check the preceding ACE.
476 * If it does not meet all of the following criteria:
477 */
478 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW)
479 continue;
480
481 meets = 0;
482 if (i > 0) {
483 meets = 1;
484 previous = &(aclp->acl_entry[i - 1]);
485
486 /*
487 * 1.5.1.1. The type field is DENY,
488 */
489 if (previous->ae_entry_type != ACL_ENTRY_TYPE_DENY)
490 meets = 0;
491
492 /*
493 * 1.5.1.2. The "who" field is the same as the current
494 * ACE,
495 *
496 * 1.5.1.3. The flag bit ACE4_IDENTIFIER_GROUP
497 * is the same as it is in the current ACE,
498 * and no other flag bits are set,
499 */
500 if (previous->ae_id != entry->ae_id ||
501 previous->ae_tag != entry->ae_tag)
502 meets = 0;
503
504 if (previous->ae_flags)
505 meets = 0;
506
507 /*
508 * 1.5.1.4. The mask bits are a subset of the mask bits
509 * of the current ACE, and are also subset of
510 * the following: ACL_READ_DATA,
511 * ACL_WRITE_DATA, ACL_APPEND_DATA, ACL_EXECUTE
512 */
513 if (previous->ae_perm & ~(entry->ae_perm))
514 meets = 0;
515
516 if (previous->ae_perm & ~(ACL_READ_DATA |
517 ACL_WRITE_DATA | ACL_APPEND_DATA | ACL_EXECUTE))
518 meets = 0;
519 }
520
521 if (!meets) {
522 /*
523 * Then the ACE of type DENY, with a who equal
524 * to the current ACE, flag bits equal to
525 * (<current ACE flags> & <ACE_IDENTIFIER_GROUP>)
526 * and no mask bits, is prepended.
527 */
528 previous = entry;
529 entry = _acl_duplicate_entry(aclp, i);
530
531 /* Adjust counter, as we've just added an entry. */
532 i++;
533
534 previous->ae_tag = entry->ae_tag;
535 previous->ae_id = entry->ae_id;
536 previous->ae_flags = entry->ae_flags;
537 previous->ae_perm = 0;
538 previous->ae_entry_type = ACL_ENTRY_TYPE_DENY;
539 }
540
541 /*
542 * 1.5.2. The following modifications are made to the prepended
543 * ACE. The intent is to mask the following ACE
544 * to disallow ACL_READ_DATA, ACL_WRITE_DATA,
545 * ACL_APPEND_DATA, or ACL_EXECUTE, based upon the group
546 * permissions of the new mode. As a special case,
547 * if the ACE matches the current owner of the file,
548 * the owner bits are used, rather than the group bits.
549 * This is reflected in the algorithm below.
550 */
551 amode = mode >> 3;
552
553 /*
554 * If ACE4_IDENTIFIER_GROUP is not set, and the "who" field
555 * in ACE matches the owner of the file, we shift amode three
556 * more bits, in order to have the owner permission bits
557 * placed in the three low order bits of amode.
558 */
559 if (entry->ae_tag == ACL_USER && entry->ae_id == file_owner_id)
560 amode = amode >> 3;
561
562 if (entry->ae_perm & ACL_READ_DATA) {
563 if (amode & READ)
564 previous->ae_perm &= ~ACL_READ_DATA;
565 else
566 previous->ae_perm |= ACL_READ_DATA;
567 }
568
569 if (entry->ae_perm & ACL_WRITE_DATA) {
570 if (amode & WRITE)
571 previous->ae_perm &= ~ACL_WRITE_DATA;
572 else
573 previous->ae_perm |= ACL_WRITE_DATA;
574 }
575
576 if (entry->ae_perm & ACL_APPEND_DATA) {
577 if (amode & WRITE)
578 previous->ae_perm &= ~ACL_APPEND_DATA;
579 else
580 previous->ae_perm |= ACL_APPEND_DATA;
581 }
582
583 if (entry->ae_perm & ACL_EXECUTE) {
584 if (amode & EXEC)
585 previous->ae_perm &= ~ACL_EXECUTE;
586 else
587 previous->ae_perm |= ACL_EXECUTE;
588 }
589
590 /*
591 * 1.5.3. If ACE4_IDENTIFIER_GROUP is set in the flags
592 * of the ALLOW ace:
593 *
594 * XXX: This point is not there in the Falkner's draft.
595 */
596 if (entry->ae_tag == ACL_GROUP &&
597 entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) {
598 mode_t extramode, ownermode;
599 extramode = (mode >> 3) & 07;
600 ownermode = mode >> 6;
601 extramode &= ~ownermode;
602
603 if (extramode) {
604 if (extramode & READ) {
605 entry->ae_perm &= ~ACL_READ_DATA;
606 previous->ae_perm &= ~ACL_READ_DATA;
607 }
608
609 if (extramode & WRITE) {
610 entry->ae_perm &=
611 ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
612 previous->ae_perm &=
613 ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
614 }
615
616 if (extramode & EXEC) {
617 entry->ae_perm &= ~ACL_EXECUTE;
618 previous->ae_perm &= ~ACL_EXECUTE;
619 }
620 }
621 }
622 }
623
624 /*
625 * 2. If there at least six ACEs, the final six ACEs are examined.
626 * If they are not equal to what we want, append six ACEs.
627 */
628 must_append = 0;
629 if (aclp->acl_cnt < 6) {
630 must_append = 1;
631 } else {
632 a6 = &(aclp->acl_entry[aclp->acl_cnt - 1]);
633 a5 = &(aclp->acl_entry[aclp->acl_cnt - 2]);
634 a4 = &(aclp->acl_entry[aclp->acl_cnt - 3]);
635 a3 = &(aclp->acl_entry[aclp->acl_cnt - 4]);
636 a2 = &(aclp->acl_entry[aclp->acl_cnt - 5]);
637 a1 = &(aclp->acl_entry[aclp->acl_cnt - 6]);
638
639 if (!_acl_entry_matches(a1, ACL_USER_OBJ, 0,
640 ACL_ENTRY_TYPE_DENY))
641 must_append = 1;
642 if (!_acl_entry_matches(a2, ACL_USER_OBJ, ACL_WRITE_ACL |
643 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
644 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW))
645 must_append = 1;
646 if (!_acl_entry_matches(a3, ACL_GROUP_OBJ, 0,
647 ACL_ENTRY_TYPE_DENY))
648 must_append = 1;
649 if (!_acl_entry_matches(a4, ACL_GROUP_OBJ, 0,
650 ACL_ENTRY_TYPE_ALLOW))
651 must_append = 1;
652 if (!_acl_entry_matches(a5, ACL_EVERYONE, ACL_WRITE_ACL |
653 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
654 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY))
655 must_append = 1;
656 if (!_acl_entry_matches(a6, ACL_EVERYONE, ACL_READ_ACL |
657 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
658 ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW))
659 must_append = 1;
660 }
661
662 if (must_append) {
663 KASSERT(aclp->acl_cnt + 6 <= ACL_MAX_ENTRIES,
664 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
665
666 a1 = _acl_append(aclp, ACL_USER_OBJ, 0, ACL_ENTRY_TYPE_DENY);
667 a2 = _acl_append(aclp, ACL_USER_OBJ, ACL_WRITE_ACL |
668 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
669 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW);
670 a3 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_DENY);
671 a4 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_ALLOW);
672 a5 = _acl_append(aclp, ACL_EVERYONE, ACL_WRITE_ACL |
673 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
674 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY);
675 a6 = _acl_append(aclp, ACL_EVERYONE, ACL_READ_ACL |
676 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
677 ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW);
678
679 KASSERT(a1 != NULL && a2 != NULL && a3 != NULL && a4 != NULL &&
680 a5 != NULL && a6 != NULL, ("couldn't append to ACL."));
681 }
682
683 /*
684 * 3. The final six ACEs are adjusted according to the incoming mode.
685 */
686 if (mode & S_IRUSR)
687 a2->ae_perm |= ACL_READ_DATA;
688 else
689 a1->ae_perm |= ACL_READ_DATA;
690 if (mode & S_IWUSR)
691 a2->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
692 else
693 a1->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
694 if (mode & S_IXUSR)
695 a2->ae_perm |= ACL_EXECUTE;
696 else
697 a1->ae_perm |= ACL_EXECUTE;
698
699 if (mode & S_IRGRP)
700 a4->ae_perm |= ACL_READ_DATA;
701 else
702 a3->ae_perm |= ACL_READ_DATA;
703 if (mode & S_IWGRP)
704 a4->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
705 else
706 a3->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
707 if (mode & S_IXGRP)
708 a4->ae_perm |= ACL_EXECUTE;
709 else
710 a3->ae_perm |= ACL_EXECUTE;
711
712 if (mode & S_IROTH)
713 a6->ae_perm |= ACL_READ_DATA;
714 else
715 a5->ae_perm |= ACL_READ_DATA;
716 if (mode & S_IWOTH)
717 a6->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
718 else
719 a5->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
720 if (mode & S_IXOTH)
721 a6->ae_perm |= ACL_EXECUTE;
722 else
723 a5->ae_perm |= ACL_EXECUTE;
724 }
725
726 void
727 acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp)
728 {
729 int i;
730 mode_t old_mode = *_mode, mode = 0, seen = 0;
731 const struct acl_entry *entry;
732
733 KASSERT(aclp->acl_cnt > 0, ("aclp->acl_cnt > 0"));
734 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
735 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
736
737 /*
738 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
739 *
740 * 3.16.6.1. Recomputing mode upon SETATTR of ACL
741 */
742
743 for (i = 0; i < aclp->acl_cnt; i++) {
744 entry = &(aclp->acl_entry[i]);
745
746 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
747 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
748 continue;
749
750 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
751 continue;
752
753 if (entry->ae_tag == ACL_USER_OBJ) {
754 if ((entry->ae_perm & ACL_READ_DATA) &&
755 ((seen & S_IRUSR) == 0)) {
756 seen |= S_IRUSR;
757 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
758 mode |= S_IRUSR;
759 }
760 if ((entry->ae_perm & ACL_WRITE_DATA) &&
761 ((seen & S_IWUSR) == 0)) {
762 seen |= S_IWUSR;
763 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
764 mode |= S_IWUSR;
765 }
766 if ((entry->ae_perm & ACL_EXECUTE) &&
767 ((seen & S_IXUSR) == 0)) {
768 seen |= S_IXUSR;
769 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
770 mode |= S_IXUSR;
771 }
772 } else if (entry->ae_tag == ACL_GROUP_OBJ) {
773 if ((entry->ae_perm & ACL_READ_DATA) &&
774 ((seen & S_IRGRP) == 0)) {
775 seen |= S_IRGRP;
776 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
777 mode |= S_IRGRP;
778 }
779 if ((entry->ae_perm & ACL_WRITE_DATA) &&
780 ((seen & S_IWGRP) == 0)) {
781 seen |= S_IWGRP;
782 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
783 mode |= S_IWGRP;
784 }
785 if ((entry->ae_perm & ACL_EXECUTE) &&
786 ((seen & S_IXGRP) == 0)) {
787 seen |= S_IXGRP;
788 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
789 mode |= S_IXGRP;
790 }
791 } else if (entry->ae_tag == ACL_EVERYONE) {
792 if (entry->ae_perm & ACL_READ_DATA) {
793 if ((seen & S_IRUSR) == 0) {
794 seen |= S_IRUSR;
795 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
796 mode |= S_IRUSR;
797 }
798 if ((seen & S_IRGRP) == 0) {
799 seen |= S_IRGRP;
800 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
801 mode |= S_IRGRP;
802 }
803 if ((seen & S_IROTH) == 0) {
804 seen |= S_IROTH;
805 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
806 mode |= S_IROTH;
807 }
808 }
809 if (entry->ae_perm & ACL_WRITE_DATA) {
810 if ((seen & S_IWUSR) == 0) {
811 seen |= S_IWUSR;
812 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
813 mode |= S_IWUSR;
814 }
815 if ((seen & S_IWGRP) == 0) {
816 seen |= S_IWGRP;
817 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
818 mode |= S_IWGRP;
819 }
820 if ((seen & S_IWOTH) == 0) {
821 seen |= S_IWOTH;
822 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
823 mode |= S_IWOTH;
824 }
825 }
826 if (entry->ae_perm & ACL_EXECUTE) {
827 if ((seen & S_IXUSR) == 0) {
828 seen |= S_IXUSR;
829 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
830 mode |= S_IXUSR;
831 }
832 if ((seen & S_IXGRP) == 0) {
833 seen |= S_IXGRP;
834 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
835 mode |= S_IXGRP;
836 }
837 if ((seen & S_IXOTH) == 0) {
838 seen |= S_IXOTH;
839 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
840 mode |= S_IXOTH;
841 }
842 }
843 }
844 }
845
846 *_mode = mode | (old_mode & ACL_PRESERVE_MASK);
847 }
848
849 void
850 acl_nfs4_compute_inherited_acl(const struct acl *parent_aclp,
851 struct acl *child_aclp, mode_t mode, int file_owner_id,
852 int is_directory)
853 {
854 int i, flags;
855 const struct acl_entry *parent_entry;
856 struct acl_entry *entry, *copy;
857
858 KASSERT(child_aclp->acl_cnt == 0, ("child_aclp->acl_cnt == 0"));
859 KASSERT(parent_aclp->acl_cnt > 0, ("parent_aclp->acl_cnt > 0"));
860 KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
861 ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
862
863 /*
864 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
865 *
866 * 3.16.6.2. Applying the mode given to CREATE or OPEN
867 * to an inherited ACL
868 */
869
870 /*
871 * 1. Form an ACL that is the concatenation of all inheritable ACEs.
872 */
873 for (i = 0; i < parent_aclp->acl_cnt; i++) {
874 parent_entry = &(parent_aclp->acl_entry[i]);
875 flags = parent_entry->ae_flags;
876
877 /*
878 * Entry is not inheritable at all.
879 */
880 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
881 ACL_ENTRY_FILE_INHERIT)) == 0)
882 continue;
883
884 /*
885 * We're creating a file, but entry is not inheritable
886 * by files.
887 */
888 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
889 continue;
890
891 /*
892 * Entry is inheritable only by files, but has NO_PROPAGATE
893 * flag set, and we're creating a directory, so it wouldn't
894 * propagate to any file in that directory anyway.
895 */
896 if (is_directory &&
897 (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
898 (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
899 continue;
900
901 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
902 ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
903 child_aclp->acl_entry[child_aclp->acl_cnt] = *parent_entry;
904 child_aclp->acl_cnt++;
905 }
906
907 /*
908 * 2. For each entry in the new ACL, adjust its flags, possibly
909 * creating two entries in place of one.
910 */
911 for (i = 0; i < child_aclp->acl_cnt; i++) {
912 entry = &(child_aclp->acl_entry[i]);
913
914 /*
915 * This is not in the specification, but SunOS
916 * apparently does that.
917 */
918 if (((entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT) ||
919 !is_directory) &&
920 entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
921 entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
922
923 /*
924 * 2.A. If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if the object
925 * being created is not a directory, then clear the
926 * following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
927 * ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
928 * ACL_ENTRY_INHERIT_ONLY.
929 */
930 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
931 !is_directory) {
932 entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
933 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
934 ACL_ENTRY_INHERIT_ONLY);
935
936 /*
937 * Continue on to the next ACE.
938 */
939 continue;
940 }
941
942 /*
943 * 2.B. If the object is a directory and ACL_ENTRY_FILE_INHERIT
944 * is set, but ACL_ENTRY_NO_PROPAGATE_INHERIT is not set, ensure
945 * that ACL_ENTRY_INHERIT_ONLY is set. Continue to the
946 * next ACE. Otherwise...
947 */
948 /*
949 * XXX: Read it again and make sure what does the "otherwise"
950 * apply to.
951 */
952 if (is_directory &&
953 (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
954 ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
955 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
956 continue;
957 }
958
959 /*
960 * 2.C. If the type of the ACE is neither ALLOW nor deny,
961 * then continue.
962 */
963 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
964 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
965 continue;
966
967 /*
968 * 2.D. Copy the original ACE into a second, adjacent ACE.
969 */
970 copy = _acl_duplicate_entry(child_aclp, i);
971
972 /*
973 * 2.E. On the first ACE, ensure that ACL_ENTRY_INHERIT_ONLY
974 * is set.
975 */
976 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
977
978 /*
979 * 2.F. On the second ACE, clear the following flags:
980 * ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_FILE_INHERIT,
981 * ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_INHERIT_ONLY.
982 */
983 copy->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
984 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
985 ACL_ENTRY_INHERIT_ONLY);
986
987 /*
988 * 2.G. On the second ACE, if the type is ALLOW,
989 * an implementation MAY clear the following
990 * mask bits: ACL_WRITE_ACL, ACL_WRITE_OWNER.
991 */
992 if (copy->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
993 copy->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
994
995 /*
996 * Increment the counter to skip the copied entry.
997 */
998 i++;
999 }
1000
1001 /*
1002 * 3. To ensure that the mode is honored, apply the algorithm describe
1003 * in Section 2.16.6.3, using the mode that is to be used for file
1004 * creation.
1005 */
1006 acl_nfs4_sync_acl_from_mode(child_aclp, mode, file_owner_id);
1007 }
1008
1009 #ifdef _KERNEL
1010 static int
1011 _acls_are_equal(const struct acl *a, const struct acl *b)
1012 {
1013 int i;
1014 const struct acl_entry *entrya, *entryb;
1015
1016 if (a->acl_cnt != b->acl_cnt)
1017 return (0);
1018
1019 for (i = 0; i < b->acl_cnt; i++) {
1020 entrya = &(a->acl_entry[i]);
1021 entryb = &(b->acl_entry[i]);
1022
1023 if (entrya->ae_tag != entryb->ae_tag ||
1024 entrya->ae_id != entryb->ae_id ||
1025 entrya->ae_perm != entryb->ae_perm ||
1026 entrya->ae_entry_type != entryb->ae_entry_type ||
1027 entrya->ae_flags != entryb->ae_flags)
1028 return (0);
1029 }
1030
1031 return (1);
1032 }
1033
1034 /*
1035 * This routine is used to determine whether to remove entry_type attribute
1036 * that stores ACL contents.
1037 */
1038 int
1039 acl_nfs4_is_trivial(const struct acl *aclp, int file_owner_id)
1040 {
1041 int trivial;
1042 mode_t tmpmode = 0;
1043 struct acl *tmpaclp;
1044
1045 if (aclp->acl_cnt != 6)
1046 return (0);
1047
1048 /*
1049 * Compute the mode from the ACL, then compute new ACL from that mode.
1050 * If the ACLs are identical, then the ACL is trivial.
1051 *
1052 * XXX: I guess there is a faster way to do this. However, even
1053 * this slow implementation significantly speeds things up
1054 * for files that don't have any entry_type ACL entries - it's
1055 * critical for performance to not use EA when they are not
1056 * needed.
1057 */
1058 tmpaclp = acl_alloc(M_WAITOK | M_ZERO);
1059 acl_nfs4_sync_mode_from_acl(&tmpmode, aclp);
1060 acl_nfs4_sync_acl_from_mode(tmpaclp, tmpmode, file_owner_id);
1061 trivial = _acls_are_equal(aclp, tmpaclp);
1062 acl_free(tmpaclp);
1063
1064 return (trivial);
1065 }
1066 #endif /* _KERNEL */
1067
1068 int
1069 acl_nfs4_check(const struct acl *aclp, int is_directory)
1070 {
1071 int i;
1072 const struct acl_entry *entry;
1073
1074 /*
1075 * The spec doesn't seem to say anything about ACL validity.
1076 * It seems there is not much to do here. There is even no need
1077 * to count "owner@" or "everyone@" (ACL_USER_OBJ and ACL_EVERYONE)
1078 * entries, as there can be several of them and that's perfectly
1079 * valid. There can be none of them too. Really.
1080 */
1081
1082 if (aclp->acl_cnt > ACL_MAX_ENTRIES || aclp->acl_cnt <= 0)
1083 return (EINVAL);
1084
1085 for (i = 0; i < aclp->acl_cnt; i++) {
1086 entry = &(aclp->acl_entry[i]);
1087
1088 switch (entry->ae_tag) {
1089 case ACL_USER_OBJ:
1090 case ACL_GROUP_OBJ:
1091 case ACL_EVERYONE:
1092 if (entry->ae_id != ACL_UNDEFINED_ID)
1093 return (EINVAL);
1094 break;
1095
1096 case ACL_USER:
1097 case ACL_GROUP:
1098 if (entry->ae_id == ACL_UNDEFINED_ID)
1099 return (EINVAL);
1100 break;
1101
1102 default:
1103 return (EINVAL);
1104 }
1105
1106 if ((entry->ae_perm | ACL_NFS4_PERM_BITS) != ACL_NFS4_PERM_BITS)
1107 return (EINVAL);
1108
1109 /*
1110 * Disallow ACL_ENTRY_TYPE_AUDIT and ACL_ENTRY_TYPE_ALARM for now.
1111 */
1112 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
1113 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
1114 return (EINVAL);
1115
1116 if ((entry->ae_flags | ACL_FLAGS_BITS) != ACL_FLAGS_BITS)
1117 return (EINVAL);
1118
1119 /* Disallow unimplemented flags. */
1120 if (entry->ae_flags & (ACL_ENTRY_SUCCESSFUL_ACCESS |
1121 ACL_ENTRY_FAILED_ACCESS))
1122 return (EINVAL);
1123
1124 /* Disallow flags not allowed for ordinary files. */
1125 if (!is_directory) {
1126 if (entry->ae_flags & (ACL_ENTRY_FILE_INHERIT |
1127 ACL_ENTRY_DIRECTORY_INHERIT |
1128 ACL_ENTRY_NO_PROPAGATE_INHERIT | ACL_ENTRY_INHERIT_ONLY))
1129 return (EINVAL);
1130 }
1131 }
1132
1133 return (0);
1134 }
Cache object: e3deed3b9d2d1bbedf5a050194548961
|