2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2010 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * ACL support routines specific to NFSv4 access control lists. These are
31 * utility routines for code common across file systems implementing NFSv4
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/systm.h>
43 #include <sys/mount.h>
45 #include <sys/vnode.h>
46 #include <sys/errno.h>
48 #include <sys/sysctl.h>
55 #define KASSERT(a, b) assert(a)
62 static void acl_nfs4_trivial_from_mode(struct acl *aclp, mode_t mode);
64 static int acl_nfs4_old_semantics = 0;
66 SYSCTL_INT(_vfs, OID_AUTO, acl_nfs4_old_semantics, CTLFLAG_RW,
67 &acl_nfs4_old_semantics, 0, "Use pre-PSARC/2010/029 NFSv4 ACL semantics");
72 } accmode2mask[] = {{VREAD, ACL_READ_DATA},
73 {VWRITE, ACL_WRITE_DATA},
74 {VAPPEND, ACL_APPEND_DATA},
76 {VREAD_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
77 {VWRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
78 {VDELETE_CHILD, ACL_DELETE_CHILD},
79 {VREAD_ATTRIBUTES, ACL_READ_ATTRIBUTES},
80 {VWRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
81 {VDELETE, ACL_DELETE},
82 {VREAD_ACL, ACL_READ_ACL},
83 {VWRITE_ACL, ACL_WRITE_ACL},
84 {VWRITE_OWNER, ACL_WRITE_OWNER},
85 {VSYNCHRONIZE, ACL_SYNCHRONIZE},
89 _access_mask_from_accmode(accmode_t accmode)
91 int access_mask = 0, i;
93 for (i = 0; accmode2mask[i].accmode != 0; i++) {
94 if (accmode & accmode2mask[i].accmode)
95 access_mask |= accmode2mask[i].mask;
99 * VAPPEND is just a modifier for VWRITE; if the caller asked
100 * for 'VAPPEND | VWRITE', we want to check for ACL_APPEND_DATA only.
102 if (access_mask & ACL_APPEND_DATA)
103 access_mask &= ~ACL_WRITE_DATA;
105 return (access_mask);
109 * Return 0, iff access is allowed, 1 otherwise.
112 _acl_denies(const struct acl *aclp, int access_mask, struct ucred *cred,
113 int file_uid, int file_gid, int *denied_explicitly)
116 const struct acl_entry *entry;
118 if (denied_explicitly != NULL)
119 *denied_explicitly = 0;
121 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
122 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
124 for (i = 0; i < aclp->acl_cnt; i++) {
125 entry = &(aclp->acl_entry[i]);
127 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
128 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
130 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
132 switch (entry->ae_tag) {
134 if (file_uid != cred->cr_uid)
138 if (entry->ae_id != cred->cr_uid)
142 if (!groupmember(file_gid, cred))
146 if (!groupmember(entry->ae_id, cred))
150 KASSERT(entry->ae_tag == ACL_EVERYONE,
151 ("entry->ae_tag == ACL_EVERYONE"));
154 if (entry->ae_entry_type == ACL_ENTRY_TYPE_DENY) {
155 if (entry->ae_perm & access_mask) {
156 if (denied_explicitly != NULL)
157 *denied_explicitly = 1;
162 access_mask &= ~(entry->ae_perm);
163 if (access_mask == 0)
167 if (access_mask == 0)
174 vaccess_acl_nfs4(enum vtype type, uid_t file_uid, gid_t file_gid,
175 struct acl *aclp, accmode_t accmode, struct ucred *cred, int *privused)
177 accmode_t priv_granted = 0;
178 int denied, explicitly_denied, access_mask, is_directory,
180 mode_t file_mode = 0;
182 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND |
183 VEXPLICIT_DENY | VREAD_NAMED_ATTRS | VWRITE_NAMED_ATTRS |
184 VDELETE_CHILD | VREAD_ATTRIBUTES | VWRITE_ATTRIBUTES | VDELETE |
185 VREAD_ACL | VWRITE_ACL | VWRITE_OWNER | VSYNCHRONIZE)) == 0,
186 ("invalid bit in accmode"));
187 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
188 ("VAPPEND without VWRITE"));
190 if (privused != NULL)
193 if (accmode & VADMIN)
197 * Ignore VSYNCHRONIZE permission.
199 accmode &= ~VSYNCHRONIZE;
201 access_mask = _access_mask_from_accmode(accmode);
209 * File owner is always allowed to read and write the ACL
210 * and basic attributes. This is to prevent a situation
211 * where user would change ACL in a way that prevents him
212 * from undoing the change.
214 if (file_uid == cred->cr_uid)
215 access_mask &= ~(ACL_READ_ACL | ACL_WRITE_ACL |
216 ACL_READ_ATTRIBUTES | ACL_WRITE_ATTRIBUTES);
219 * Ignore append permission for regular files; use write
220 * permission instead.
222 if (!is_directory && (access_mask & ACL_APPEND_DATA)) {
223 access_mask &= ~ACL_APPEND_DATA;
224 access_mask |= ACL_WRITE_DATA;
227 denied = _acl_denies(aclp, access_mask, cred, file_uid, file_gid,
231 if (file_uid != cred->cr_uid)
236 * For VEXEC, ensure that at least one execute bit is set for
237 * non-directories. We have to check the mode here to stay
238 * consistent with execve(2). See the test in
239 * exec_check_permissions().
241 acl_nfs4_sync_mode_from_acl(&file_mode, aclp);
242 if (!denied && !is_directory && (accmode & VEXEC) &&
243 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
250 * Access failed. Iff it was not denied explicitly and
251 * VEXPLICIT_DENY flag was specified, allow access.
253 if ((accmode & VEXPLICIT_DENY) && explicitly_denied == 0)
256 accmode &= ~VEXPLICIT_DENY;
259 * No match. Try to use privileges, if there are any.
262 if ((accmode & VEXEC) && !priv_check_cred(cred, PRIV_VFS_LOOKUP))
263 priv_granted |= VEXEC;
266 * Ensure that at least one execute bit is on. Otherwise,
267 * a privileged user will always succeed, and we don't want
268 * this to happen unless the file really is executable.
270 if ((accmode & VEXEC) && (file_mode &
271 (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
272 !priv_check_cred(cred, PRIV_VFS_EXEC))
273 priv_granted |= VEXEC;
276 if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ))
277 priv_granted |= VREAD;
279 if ((accmode & (VWRITE | VAPPEND | VDELETE_CHILD)) &&
280 !priv_check_cred(cred, PRIV_VFS_WRITE))
281 priv_granted |= (VWRITE | VAPPEND | VDELETE_CHILD);
283 if ((accmode & VADMIN_PERMS) &&
284 !priv_check_cred(cred, PRIV_VFS_ADMIN))
285 priv_granted |= VADMIN_PERMS;
287 if ((accmode & VSTAT_PERMS) &&
288 !priv_check_cred(cred, PRIV_VFS_STAT))
289 priv_granted |= VSTAT_PERMS;
291 if ((accmode & priv_granted) == accmode) {
292 if (privused != NULL)
298 if (accmode & (VADMIN_PERMS | VDELETE_CHILD | VDELETE))
308 _acl_entry_matches(struct acl_entry *entry, acl_tag_t tag, acl_perm_t perm,
309 acl_entry_type_t entry_type)
311 if (entry->ae_tag != tag)
314 if (entry->ae_id != ACL_UNDEFINED_ID)
317 if (entry->ae_perm != perm)
320 if (entry->ae_entry_type != entry_type)
323 if (entry->ae_flags != 0)
329 static struct acl_entry *
330 _acl_append(struct acl *aclp, acl_tag_t tag, acl_perm_t perm,
331 acl_entry_type_t entry_type)
333 struct acl_entry *entry;
335 KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
336 ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
338 entry = &(aclp->acl_entry[aclp->acl_cnt]);
342 entry->ae_id = ACL_UNDEFINED_ID;
343 entry->ae_perm = perm;
344 entry->ae_entry_type = entry_type;
350 static struct acl_entry *
351 _acl_duplicate_entry(struct acl *aclp, int entry_index)
355 KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
356 ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
358 for (i = aclp->acl_cnt; i > entry_index; i--)
359 aclp->acl_entry[i] = aclp->acl_entry[i - 1];
363 return (&(aclp->acl_entry[entry_index + 1]));
367 acl_nfs4_sync_acl_from_mode_draft(struct acl *aclp, mode_t mode,
370 int i, meets, must_append;
371 struct acl_entry *entry, *copy, *previous,
372 *a1, *a2, *a3, *a4, *a5, *a6;
375 const int WRITE = 02;
378 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
379 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
382 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
384 * 3.16.6.3. Applying a Mode to an Existing ACL
390 for (i = 0; i < aclp->acl_cnt; i++) {
391 entry = &(aclp->acl_entry[i]);
394 * 1.1. If the type is neither ALLOW or DENY - skip.
396 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
397 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
401 * 1.2. If ACL_ENTRY_INHERIT_ONLY is set - skip.
403 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
407 * 1.3. If ACL_ENTRY_FILE_INHERIT or ACL_ENTRY_DIRECTORY_INHERIT
410 if (entry->ae_flags &
411 (ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT)) {
413 * 1.3.1. A copy of the current ACE is made, and placed
414 * in the ACL immediately following the current
417 copy = _acl_duplicate_entry(aclp, i);
420 * 1.3.2. In the first ACE, the flag
421 * ACL_ENTRY_INHERIT_ONLY is set.
423 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
426 * 1.3.3. In the second ACE, the following flags
428 * ACL_ENTRY_FILE_INHERIT,
429 * ACL_ENTRY_DIRECTORY_INHERIT,
430 * ACL_ENTRY_NO_PROPAGATE_INHERIT.
432 copy->ae_flags &= ~(ACL_ENTRY_FILE_INHERIT |
433 ACL_ENTRY_DIRECTORY_INHERIT |
434 ACL_ENTRY_NO_PROPAGATE_INHERIT);
437 * The algorithm continues on with the second ACE.
444 * 1.4. If it's owner@, group@ or everyone@ entry, clear
445 * ACL_READ_DATA, ACL_WRITE_DATA, ACL_APPEND_DATA
446 * and ACL_EXECUTE. Continue to the next entry.
448 if (entry->ae_tag == ACL_USER_OBJ ||
449 entry->ae_tag == ACL_GROUP_OBJ ||
450 entry->ae_tag == ACL_EVERYONE) {
451 entry->ae_perm &= ~(ACL_READ_DATA | ACL_WRITE_DATA |
452 ACL_APPEND_DATA | ACL_EXECUTE);
457 * 1.5. Otherwise, if the "who" field did not match one
458 * of OWNER@, GROUP@, EVERYONE@:
460 * 1.5.1. If the type is ALLOW, check the preceding ACE.
461 * If it does not meet all of the following criteria:
463 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW)
469 previous = &(aclp->acl_entry[i - 1]);
472 * 1.5.1.1. The type field is DENY,
474 if (previous->ae_entry_type != ACL_ENTRY_TYPE_DENY)
478 * 1.5.1.2. The "who" field is the same as the current
481 * 1.5.1.3. The flag bit ACE4_IDENTIFIER_GROUP
482 * is the same as it is in the current ACE,
483 * and no other flag bits are set,
485 if (previous->ae_id != entry->ae_id ||
486 previous->ae_tag != entry->ae_tag)
489 if (previous->ae_flags)
493 * 1.5.1.4. The mask bits are a subset of the mask bits
494 * of the current ACE, and are also subset of
495 * the following: ACL_READ_DATA,
496 * ACL_WRITE_DATA, ACL_APPEND_DATA, ACL_EXECUTE
498 if (previous->ae_perm & ~(entry->ae_perm))
501 if (previous->ae_perm & ~(ACL_READ_DATA |
502 ACL_WRITE_DATA | ACL_APPEND_DATA | ACL_EXECUTE))
508 * Then the ACE of type DENY, with a who equal
509 * to the current ACE, flag bits equal to
510 * (<current ACE flags> & <ACE_IDENTIFIER_GROUP>)
511 * and no mask bits, is prepended.
514 entry = _acl_duplicate_entry(aclp, i);
516 /* Adjust counter, as we've just added an entry. */
519 previous->ae_tag = entry->ae_tag;
520 previous->ae_id = entry->ae_id;
521 previous->ae_flags = entry->ae_flags;
522 previous->ae_perm = 0;
523 previous->ae_entry_type = ACL_ENTRY_TYPE_DENY;
527 * 1.5.2. The following modifications are made to the prepended
528 * ACE. The intent is to mask the following ACE
529 * to disallow ACL_READ_DATA, ACL_WRITE_DATA,
530 * ACL_APPEND_DATA, or ACL_EXECUTE, based upon the group
531 * permissions of the new mode. As a special case,
532 * if the ACE matches the current owner of the file,
533 * the owner bits are used, rather than the group bits.
534 * This is reflected in the algorithm below.
539 * If ACE4_IDENTIFIER_GROUP is not set, and the "who" field
540 * in ACE matches the owner of the file, we shift amode three
541 * more bits, in order to have the owner permission bits
542 * placed in the three low order bits of amode.
544 if (entry->ae_tag == ACL_USER && entry->ae_id == file_owner_id)
547 if (entry->ae_perm & ACL_READ_DATA) {
549 previous->ae_perm &= ~ACL_READ_DATA;
551 previous->ae_perm |= ACL_READ_DATA;
554 if (entry->ae_perm & ACL_WRITE_DATA) {
556 previous->ae_perm &= ~ACL_WRITE_DATA;
558 previous->ae_perm |= ACL_WRITE_DATA;
561 if (entry->ae_perm & ACL_APPEND_DATA) {
563 previous->ae_perm &= ~ACL_APPEND_DATA;
565 previous->ae_perm |= ACL_APPEND_DATA;
568 if (entry->ae_perm & ACL_EXECUTE) {
570 previous->ae_perm &= ~ACL_EXECUTE;
572 previous->ae_perm |= ACL_EXECUTE;
576 * 1.5.3. If ACE4_IDENTIFIER_GROUP is set in the flags
579 * XXX: This point is not there in the Falkner's draft.
581 if (entry->ae_tag == ACL_GROUP &&
582 entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) {
583 mode_t extramode, ownermode;
584 extramode = (mode >> 3) & 07;
585 ownermode = mode >> 6;
586 extramode &= ~ownermode;
589 if (extramode & READ) {
590 entry->ae_perm &= ~ACL_READ_DATA;
591 previous->ae_perm &= ~ACL_READ_DATA;
594 if (extramode & WRITE) {
596 ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
598 ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
601 if (extramode & EXEC) {
602 entry->ae_perm &= ~ACL_EXECUTE;
603 previous->ae_perm &= ~ACL_EXECUTE;
610 * 2. If there at least six ACEs, the final six ACEs are examined.
611 * If they are not equal to what we want, append six ACEs.
614 if (aclp->acl_cnt < 6) {
617 a6 = &(aclp->acl_entry[aclp->acl_cnt - 1]);
618 a5 = &(aclp->acl_entry[aclp->acl_cnt - 2]);
619 a4 = &(aclp->acl_entry[aclp->acl_cnt - 3]);
620 a3 = &(aclp->acl_entry[aclp->acl_cnt - 4]);
621 a2 = &(aclp->acl_entry[aclp->acl_cnt - 5]);
622 a1 = &(aclp->acl_entry[aclp->acl_cnt - 6]);
624 if (!_acl_entry_matches(a1, ACL_USER_OBJ, 0,
625 ACL_ENTRY_TYPE_DENY))
627 if (!_acl_entry_matches(a2, ACL_USER_OBJ, ACL_WRITE_ACL |
628 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
629 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW))
631 if (!_acl_entry_matches(a3, ACL_GROUP_OBJ, 0,
632 ACL_ENTRY_TYPE_DENY))
634 if (!_acl_entry_matches(a4, ACL_GROUP_OBJ, 0,
635 ACL_ENTRY_TYPE_ALLOW))
637 if (!_acl_entry_matches(a5, ACL_EVERYONE, ACL_WRITE_ACL |
638 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
639 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY))
641 if (!_acl_entry_matches(a6, ACL_EVERYONE, ACL_READ_ACL |
642 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
643 ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW))
648 KASSERT(aclp->acl_cnt + 6 <= ACL_MAX_ENTRIES,
649 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
651 a1 = _acl_append(aclp, ACL_USER_OBJ, 0, ACL_ENTRY_TYPE_DENY);
652 a2 = _acl_append(aclp, ACL_USER_OBJ, ACL_WRITE_ACL |
653 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
654 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW);
655 a3 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_DENY);
656 a4 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_ALLOW);
657 a5 = _acl_append(aclp, ACL_EVERYONE, ACL_WRITE_ACL |
658 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
659 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY);
660 a6 = _acl_append(aclp, ACL_EVERYONE, ACL_READ_ACL |
661 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
662 ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW);
664 KASSERT(a1 != NULL && a2 != NULL && a3 != NULL && a4 != NULL &&
665 a5 != NULL && a6 != NULL, ("couldn't append to ACL."));
669 * 3. The final six ACEs are adjusted according to the incoming mode.
672 a2->ae_perm |= ACL_READ_DATA;
674 a1->ae_perm |= ACL_READ_DATA;
676 a2->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
678 a1->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
680 a2->ae_perm |= ACL_EXECUTE;
682 a1->ae_perm |= ACL_EXECUTE;
685 a4->ae_perm |= ACL_READ_DATA;
687 a3->ae_perm |= ACL_READ_DATA;
689 a4->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
691 a3->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
693 a4->ae_perm |= ACL_EXECUTE;
695 a3->ae_perm |= ACL_EXECUTE;
698 a6->ae_perm |= ACL_READ_DATA;
700 a5->ae_perm |= ACL_READ_DATA;
702 a6->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
704 a5->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
706 a6->ae_perm |= ACL_EXECUTE;
708 a5->ae_perm |= ACL_EXECUTE;
713 acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode,
717 if (acl_nfs4_old_semantics)
718 acl_nfs4_sync_acl_from_mode_draft(aclp, mode, file_owner_id);
720 acl_nfs4_trivial_from_mode(aclp, mode);
725 acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp)
728 mode_t old_mode = *_mode, mode = 0, seen = 0;
729 const struct acl_entry *entry;
731 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
732 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
735 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
737 * 3.16.6.1. Recomputing mode upon SETATTR of ACL
740 for (i = 0; i < aclp->acl_cnt; i++) {
741 entry = &(aclp->acl_entry[i]);
743 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
744 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
747 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
750 if (entry->ae_tag == ACL_USER_OBJ) {
751 if ((entry->ae_perm & ACL_READ_DATA) &&
752 ((seen & S_IRUSR) == 0)) {
754 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
757 if ((entry->ae_perm & ACL_WRITE_DATA) &&
758 ((seen & S_IWUSR) == 0)) {
760 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
763 if ((entry->ae_perm & ACL_EXECUTE) &&
764 ((seen & S_IXUSR) == 0)) {
766 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
769 } else if (entry->ae_tag == ACL_GROUP_OBJ) {
770 if ((entry->ae_perm & ACL_READ_DATA) &&
771 ((seen & S_IRGRP) == 0)) {
773 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
776 if ((entry->ae_perm & ACL_WRITE_DATA) &&
777 ((seen & S_IWGRP) == 0)) {
779 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
782 if ((entry->ae_perm & ACL_EXECUTE) &&
783 ((seen & S_IXGRP) == 0)) {
785 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
788 } else if (entry->ae_tag == ACL_EVERYONE) {
789 if (entry->ae_perm & ACL_READ_DATA) {
790 if ((seen & S_IRUSR) == 0) {
792 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
795 if ((seen & S_IRGRP) == 0) {
797 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
800 if ((seen & S_IROTH) == 0) {
802 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
806 if (entry->ae_perm & ACL_WRITE_DATA) {
807 if ((seen & S_IWUSR) == 0) {
809 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
812 if ((seen & S_IWGRP) == 0) {
814 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
817 if ((seen & S_IWOTH) == 0) {
819 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
823 if (entry->ae_perm & ACL_EXECUTE) {
824 if ((seen & S_IXUSR) == 0) {
826 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
829 if ((seen & S_IXGRP) == 0) {
831 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
834 if ((seen & S_IXOTH) == 0) {
836 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
843 *_mode = mode | (old_mode & ACL_PRESERVE_MASK);
848 * Calculate inherited ACL in a manner compatible with NFSv4 Minor Version 1,
849 * draft-ietf-nfsv4-minorversion1-03.txt.
852 acl_nfs4_compute_inherited_acl_draft(const struct acl *parent_aclp,
853 struct acl *child_aclp, mode_t mode, int file_owner_id,
857 const struct acl_entry *parent_entry;
858 struct acl_entry *entry, *copy;
860 KASSERT(child_aclp->acl_cnt == 0, ("child_aclp->acl_cnt == 0"));
861 KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
862 ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
865 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
867 * 3.16.6.2. Applying the mode given to CREATE or OPEN
868 * to an inherited ACL
872 * 1. Form an ACL that is the concatenation of all inheritable ACEs.
874 for (i = 0; i < parent_aclp->acl_cnt; i++) {
875 parent_entry = &(parent_aclp->acl_entry[i]);
876 flags = parent_entry->ae_flags;
879 * Entry is not inheritable at all.
881 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
882 ACL_ENTRY_FILE_INHERIT)) == 0)
886 * We're creating a file, but entry is not inheritable
889 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
893 * Entry is inheritable only by files, but has NO_PROPAGATE
894 * flag set, and we're creating a directory, so it wouldn't
895 * propagate to any file in that directory anyway.
898 (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
899 (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
902 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
903 ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
904 child_aclp->acl_entry[child_aclp->acl_cnt] = *parent_entry;
905 child_aclp->acl_cnt++;
909 * 2. For each entry in the new ACL, adjust its flags, possibly
910 * creating two entries in place of one.
912 for (i = 0; i < child_aclp->acl_cnt; i++) {
913 entry = &(child_aclp->acl_entry[i]);
916 * This is not in the specification, but SunOS
917 * apparently does that.
919 if (((entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT) ||
921 entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
922 entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
925 * 2.A. If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if the object
926 * being created is not a directory, then clear the
927 * following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
928 * ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
929 * ACL_ENTRY_INHERIT_ONLY.
931 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
933 entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
934 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
935 ACL_ENTRY_INHERIT_ONLY);
938 * Continue on to the next ACE.
944 * 2.B. If the object is a directory and ACL_ENTRY_FILE_INHERIT
945 * is set, but ACL_ENTRY_NO_PROPAGATE_INHERIT is not set, ensure
946 * that ACL_ENTRY_INHERIT_ONLY is set. Continue to the
947 * next ACE. Otherwise...
950 * XXX: Read it again and make sure what does the "otherwise"
954 (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
955 ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
956 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
961 * 2.C. If the type of the ACE is neither ALLOW nor deny,
964 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
965 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
969 * 2.D. Copy the original ACE into a second, adjacent ACE.
971 copy = _acl_duplicate_entry(child_aclp, i);
974 * 2.E. On the first ACE, ensure that ACL_ENTRY_INHERIT_ONLY
977 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
980 * 2.F. On the second ACE, clear the following flags:
981 * ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_FILE_INHERIT,
982 * ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_INHERIT_ONLY.
984 copy->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
985 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
986 ACL_ENTRY_INHERIT_ONLY);
989 * 2.G. On the second ACE, if the type is ALLOW,
990 * an implementation MAY clear the following
991 * mask bits: ACL_WRITE_ACL, ACL_WRITE_OWNER.
993 if (copy->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
994 copy->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
997 * Increment the counter to skip the copied entry.
1003 * 3. To ensure that the mode is honored, apply the algorithm describe
1004 * in Section 2.16.6.3, using the mode that is to be used for file
1007 acl_nfs4_sync_acl_from_mode(child_aclp, mode, file_owner_id);
1009 #endif /* _KERNEL */
1012 * Populate the ACL with entries inherited from parent_aclp.
1015 acl_nfs4_inherit_entries(const struct acl *parent_aclp,
1016 struct acl *child_aclp, mode_t mode, int file_owner_id,
1020 const struct acl_entry *parent_entry;
1021 struct acl_entry *entry;
1023 KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
1024 ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
1026 for (i = 0; i < parent_aclp->acl_cnt; i++) {
1027 parent_entry = &(parent_aclp->acl_entry[i]);
1028 flags = parent_entry->ae_flags;
1029 tag = parent_entry->ae_tag;
1032 * Don't inherit owner@, group@, or everyone@ entries.
1034 if (tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ ||
1035 tag == ACL_EVERYONE)
1039 * Entry is not inheritable at all.
1041 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
1042 ACL_ENTRY_FILE_INHERIT)) == 0)
1046 * We're creating a file, but entry is not inheritable
1049 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
1053 * Entry is inheritable only by files, but has NO_PROPAGATE
1054 * flag set, and we're creating a directory, so it wouldn't
1055 * propagate to any file in that directory anyway.
1058 (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
1059 (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
1063 * Entry qualifies for being inherited.
1065 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
1066 ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
1067 entry = &(child_aclp->acl_entry[child_aclp->acl_cnt]);
1068 *entry = *parent_entry;
1069 child_aclp->acl_cnt++;
1071 entry->ae_flags &= ~ACL_ENTRY_INHERIT_ONLY;
1072 entry->ae_flags |= ACL_ENTRY_INHERITED;
1075 * If the type of the ACE is neither ALLOW nor DENY,
1076 * then leave it as it is and proceed to the next one.
1078 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
1079 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
1083 * If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if
1084 * the object being created is not a directory, then clear
1085 * the following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
1086 * ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
1087 * ACL_ENTRY_INHERIT_ONLY.
1089 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
1091 entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
1092 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
1093 ACL_ENTRY_INHERIT_ONLY);
1097 * If the object is a directory and ACL_ENTRY_FILE_INHERIT
1098 * is set, but ACL_ENTRY_DIRECTORY_INHERIT is not set, ensure
1099 * that ACL_ENTRY_INHERIT_ONLY is set.
1102 (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
1103 ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
1104 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
1107 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW &&
1108 (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY) == 0) {
1110 * Some permissions must never be inherited.
1112 entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER |
1113 ACL_WRITE_NAMED_ATTRS | ACL_WRITE_ATTRIBUTES);
1116 * Others must be masked according to the file mode.
1118 if ((mode & S_IRGRP) == 0)
1119 entry->ae_perm &= ~ACL_READ_DATA;
1120 if ((mode & S_IWGRP) == 0)
1122 ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
1123 if ((mode & S_IXGRP) == 0)
1124 entry->ae_perm &= ~ACL_EXECUTE;
1130 * Calculate inherited ACL in a manner compatible with PSARC/2010/029.
1131 * It's also being used to calculate a trivial ACL, by inheriting from
1135 acl_nfs4_compute_inherited_acl_psarc(const struct acl *parent_aclp,
1136 struct acl *aclp, mode_t mode, int file_owner_id, int is_directory)
1138 acl_perm_t user_allow_first = 0, user_deny = 0, group_deny = 0;
1139 acl_perm_t user_allow, group_allow, everyone_allow;
1141 KASSERT(aclp->acl_cnt == 0, ("aclp->acl_cnt == 0"));
1143 user_allow = group_allow = everyone_allow = ACL_READ_ACL |
1144 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS | ACL_SYNCHRONIZE;
1145 user_allow |= ACL_WRITE_ACL | ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
1146 ACL_WRITE_NAMED_ATTRS;
1149 user_allow |= ACL_READ_DATA;
1151 user_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1153 user_allow |= ACL_EXECUTE;
1156 group_allow |= ACL_READ_DATA;
1158 group_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1160 group_allow |= ACL_EXECUTE;
1163 everyone_allow |= ACL_READ_DATA;
1165 everyone_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1167 everyone_allow |= ACL_EXECUTE;
1169 user_deny = ((group_allow | everyone_allow) & ~user_allow);
1170 group_deny = everyone_allow & ~group_allow;
1171 user_allow_first = group_deny & ~user_deny;
1173 if (user_allow_first != 0)
1174 _acl_append(aclp, ACL_USER_OBJ, user_allow_first,
1175 ACL_ENTRY_TYPE_ALLOW);
1177 _acl_append(aclp, ACL_USER_OBJ, user_deny,
1178 ACL_ENTRY_TYPE_DENY);
1179 if (group_deny != 0)
1180 _acl_append(aclp, ACL_GROUP_OBJ, group_deny,
1181 ACL_ENTRY_TYPE_DENY);
1183 if (parent_aclp != NULL)
1184 acl_nfs4_inherit_entries(parent_aclp, aclp, mode,
1185 file_owner_id, is_directory);
1187 _acl_append(aclp, ACL_USER_OBJ, user_allow, ACL_ENTRY_TYPE_ALLOW);
1188 _acl_append(aclp, ACL_GROUP_OBJ, group_allow, ACL_ENTRY_TYPE_ALLOW);
1189 _acl_append(aclp, ACL_EVERYONE, everyone_allow, ACL_ENTRY_TYPE_ALLOW);
1194 acl_nfs4_compute_inherited_acl(const struct acl *parent_aclp,
1195 struct acl *child_aclp, mode_t mode, int file_owner_id,
1199 if (acl_nfs4_old_semantics)
1200 acl_nfs4_compute_inherited_acl_draft(parent_aclp, child_aclp,
1201 mode, file_owner_id, is_directory);
1203 acl_nfs4_compute_inherited_acl_psarc(parent_aclp, child_aclp,
1204 mode, file_owner_id, is_directory);
1206 #endif /* _KERNEL */
1209 * Calculate trivial ACL in a manner compatible with PSARC/2010/029.
1210 * Note that this results in an ACL different from (but semantically
1211 * equal to) the "canonical six" trivial ACL computed using algorithm
1212 * described in draft-ietf-nfsv4-minorversion1-03.txt, 3.16.6.2.
1215 acl_nfs4_trivial_from_mode(struct acl *aclp, mode_t mode)
1219 acl_nfs4_compute_inherited_acl_psarc(NULL, aclp, mode, -1, -1);
1224 * This routine is used by libc to implement acl_strip_np(3)
1225 * and acl_is_trivial_np(3).
1228 acl_nfs4_trivial_from_mode_libc(struct acl *aclp, int mode, int canonical_six)
1233 acl_nfs4_sync_acl_from_mode_draft(aclp, mode, -1);
1235 acl_nfs4_trivial_from_mode(aclp, mode);
1237 #endif /* !_KERNEL */
1241 _acls_are_equal(const struct acl *a, const struct acl *b)
1244 const struct acl_entry *entrya, *entryb;
1246 if (a->acl_cnt != b->acl_cnt)
1249 for (i = 0; i < b->acl_cnt; i++) {
1250 entrya = &(a->acl_entry[i]);
1251 entryb = &(b->acl_entry[i]);
1253 if (entrya->ae_tag != entryb->ae_tag ||
1254 entrya->ae_id != entryb->ae_id ||
1255 entrya->ae_perm != entryb->ae_perm ||
1256 entrya->ae_entry_type != entryb->ae_entry_type ||
1257 entrya->ae_flags != entryb->ae_flags)
1265 * This routine is used to determine whether to remove extended attribute
1266 * that stores ACL contents.
1269 acl_nfs4_is_trivial(const struct acl *aclp, int file_owner_id)
1273 struct acl *tmpaclp;
1275 if (aclp->acl_cnt > 6)
1279 * Compute the mode from the ACL, then compute new ACL from that mode.
1280 * If the ACLs are identical, then the ACL is trivial.
1282 * XXX: I guess there is a faster way to do this. However, even
1283 * this slow implementation significantly speeds things up
1284 * for files that don't have non-trivial ACLs - it's critical
1285 * for performance to not use EA when they are not needed.
1287 * First try the PSARC/2010/029 semantics.
1289 tmpaclp = acl_alloc(M_WAITOK | M_ZERO);
1290 acl_nfs4_sync_mode_from_acl(&tmpmode, aclp);
1291 acl_nfs4_trivial_from_mode(tmpaclp, tmpmode);
1292 trivial = _acls_are_equal(aclp, tmpaclp);
1299 * Check if it's a draft-ietf-nfsv4-minorversion1-03.txt trivial ACL.
1301 tmpaclp->acl_cnt = 0;
1302 acl_nfs4_sync_acl_from_mode_draft(tmpaclp, tmpmode, file_owner_id);
1303 trivial = _acls_are_equal(aclp, tmpaclp);
1308 #endif /* _KERNEL */
1311 acl_nfs4_check(const struct acl *aclp, int is_directory)
1314 const struct acl_entry *entry;
1317 * The spec doesn't seem to say anything about ACL validity.
1318 * It seems there is not much to do here. There is even no need
1319 * to count "owner@" or "everyone@" (ACL_USER_OBJ and ACL_EVERYONE)
1320 * entries, as there can be several of them and that's perfectly
1321 * valid. There can be none of them too. Really.
1324 if (aclp->acl_cnt > ACL_MAX_ENTRIES || aclp->acl_cnt <= 0)
1327 for (i = 0; i < aclp->acl_cnt; i++) {
1328 entry = &(aclp->acl_entry[i]);
1330 switch (entry->ae_tag) {
1334 if (entry->ae_id != ACL_UNDEFINED_ID)
1340 if (entry->ae_id == ACL_UNDEFINED_ID)
1348 if ((entry->ae_perm | ACL_NFS4_PERM_BITS) != ACL_NFS4_PERM_BITS)
1352 * Disallow ACL_ENTRY_TYPE_AUDIT and ACL_ENTRY_TYPE_ALARM for now.
1354 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
1355 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
1358 if ((entry->ae_flags | ACL_FLAGS_BITS) != ACL_FLAGS_BITS)
1361 /* Disallow unimplemented flags. */
1362 if (entry->ae_flags & (ACL_ENTRY_SUCCESSFUL_ACCESS |
1363 ACL_ENTRY_FAILED_ACCESS))
1366 /* Disallow flags not allowed for ordinary files. */
1367 if (!is_directory) {
1368 if (entry->ae_flags & (ACL_ENTRY_FILE_INHERIT |
1369 ACL_ENTRY_DIRECTORY_INHERIT |
1370 ACL_ENTRY_NO_PROPAGATE_INHERIT | ACL_ENTRY_INHERIT_ONLY))
1380 acl_nfs4_modload(module_t module, int what, void *arg)
1408 static moduledata_t acl_nfs4_mod = {
1415 * XXX TODO: which subsystem, order?
1417 DECLARE_MODULE(acl_nfs4, acl_nfs4_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1418 MODULE_VERSION(acl_nfs4, 1);
1419 #endif /* _KERNEL */