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,
264 priv_granted |= VEXEC;
267 * Ensure that at least one execute bit is on. Otherwise,
268 * a privileged user will always succeed, and we don't want
269 * this to happen unless the file really is executable.
271 if ((accmode & VEXEC) && (file_mode &
272 (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
273 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
274 priv_granted |= VEXEC;
277 if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ, 0))
278 priv_granted |= VREAD;
280 if ((accmode & (VWRITE | VAPPEND | VDELETE_CHILD)) &&
281 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
282 priv_granted |= (VWRITE | VAPPEND | VDELETE_CHILD);
284 if ((accmode & VADMIN_PERMS) &&
285 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
286 priv_granted |= VADMIN_PERMS;
288 if ((accmode & VSTAT_PERMS) &&
289 !priv_check_cred(cred, PRIV_VFS_STAT, 0))
290 priv_granted |= VSTAT_PERMS;
292 if ((accmode & priv_granted) == accmode) {
293 if (privused != NULL)
299 if (accmode & (VADMIN_PERMS | VDELETE_CHILD | VDELETE))
309 _acl_entry_matches(struct acl_entry *entry, acl_tag_t tag, acl_perm_t perm,
310 acl_entry_type_t entry_type)
312 if (entry->ae_tag != tag)
315 if (entry->ae_id != ACL_UNDEFINED_ID)
318 if (entry->ae_perm != perm)
321 if (entry->ae_entry_type != entry_type)
324 if (entry->ae_flags != 0)
330 static struct acl_entry *
331 _acl_append(struct acl *aclp, acl_tag_t tag, acl_perm_t perm,
332 acl_entry_type_t entry_type)
334 struct acl_entry *entry;
336 KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
337 ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
339 entry = &(aclp->acl_entry[aclp->acl_cnt]);
343 entry->ae_id = ACL_UNDEFINED_ID;
344 entry->ae_perm = perm;
345 entry->ae_entry_type = entry_type;
351 static struct acl_entry *
352 _acl_duplicate_entry(struct acl *aclp, unsigned entry_index)
356 KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
357 ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
359 for (i = aclp->acl_cnt; i > entry_index; i--)
360 aclp->acl_entry[i] = aclp->acl_entry[i - 1];
364 return (&(aclp->acl_entry[entry_index + 1]));
368 acl_nfs4_sync_acl_from_mode_draft(struct acl *aclp, mode_t mode,
371 int meets, must_append;
373 struct acl_entry *entry, *copy, *previous,
374 *a1, *a2, *a3, *a4, *a5, *a6;
377 const int WRITE = 02;
380 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
381 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
384 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
386 * 3.16.6.3. Applying a Mode to an Existing ACL
392 for (i = 0; i < aclp->acl_cnt; i++) {
393 entry = &(aclp->acl_entry[i]);
396 * 1.1. If the type is neither ALLOW or DENY - skip.
398 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
399 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
403 * 1.2. If ACL_ENTRY_INHERIT_ONLY is set - skip.
405 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
409 * 1.3. If ACL_ENTRY_FILE_INHERIT or ACL_ENTRY_DIRECTORY_INHERIT
412 if (entry->ae_flags &
413 (ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT)) {
415 * 1.3.1. A copy of the current ACE is made, and placed
416 * in the ACL immediately following the current
419 copy = _acl_duplicate_entry(aclp, i);
422 * 1.3.2. In the first ACE, the flag
423 * ACL_ENTRY_INHERIT_ONLY is set.
425 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
428 * 1.3.3. In the second ACE, the following flags
430 * ACL_ENTRY_FILE_INHERIT,
431 * ACL_ENTRY_DIRECTORY_INHERIT,
432 * ACL_ENTRY_NO_PROPAGATE_INHERIT.
434 copy->ae_flags &= ~(ACL_ENTRY_FILE_INHERIT |
435 ACL_ENTRY_DIRECTORY_INHERIT |
436 ACL_ENTRY_NO_PROPAGATE_INHERIT);
439 * The algorithm continues on with the second ACE.
446 * 1.4. If it's owner@, group@ or everyone@ entry, clear
447 * ACL_READ_DATA, ACL_WRITE_DATA, ACL_APPEND_DATA
448 * and ACL_EXECUTE. Continue to the next entry.
450 if (entry->ae_tag == ACL_USER_OBJ ||
451 entry->ae_tag == ACL_GROUP_OBJ ||
452 entry->ae_tag == ACL_EVERYONE) {
453 entry->ae_perm &= ~(ACL_READ_DATA | ACL_WRITE_DATA |
454 ACL_APPEND_DATA | ACL_EXECUTE);
459 * 1.5. Otherwise, if the "who" field did not match one
460 * of OWNER@, GROUP@, EVERYONE@:
462 * 1.5.1. If the type is ALLOW, check the preceding ACE.
463 * If it does not meet all of the following criteria:
465 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW)
471 previous = &(aclp->acl_entry[i - 1]);
474 * 1.5.1.1. The type field is DENY,
476 if (previous->ae_entry_type != ACL_ENTRY_TYPE_DENY)
480 * 1.5.1.2. The "who" field is the same as the current
483 * 1.5.1.3. The flag bit ACE4_IDENTIFIER_GROUP
484 * is the same as it is in the current ACE,
485 * and no other flag bits are set,
487 if (previous->ae_id != entry->ae_id ||
488 previous->ae_tag != entry->ae_tag)
491 if (previous->ae_flags)
495 * 1.5.1.4. The mask bits are a subset of the mask bits
496 * of the current ACE, and are also subset of
497 * the following: ACL_READ_DATA,
498 * ACL_WRITE_DATA, ACL_APPEND_DATA, ACL_EXECUTE
500 if (previous->ae_perm & ~(entry->ae_perm))
503 if (previous->ae_perm & ~(ACL_READ_DATA |
504 ACL_WRITE_DATA | ACL_APPEND_DATA | ACL_EXECUTE))
510 * Then the ACE of type DENY, with a who equal
511 * to the current ACE, flag bits equal to
512 * (<current ACE flags> & <ACE_IDENTIFIER_GROUP>)
513 * and no mask bits, is prepended.
516 entry = _acl_duplicate_entry(aclp, i);
518 /* Adjust counter, as we've just added an entry. */
521 previous->ae_tag = entry->ae_tag;
522 previous->ae_id = entry->ae_id;
523 previous->ae_flags = entry->ae_flags;
524 previous->ae_perm = 0;
525 previous->ae_entry_type = ACL_ENTRY_TYPE_DENY;
529 * 1.5.2. The following modifications are made to the prepended
530 * ACE. The intent is to mask the following ACE
531 * to disallow ACL_READ_DATA, ACL_WRITE_DATA,
532 * ACL_APPEND_DATA, or ACL_EXECUTE, based upon the group
533 * permissions of the new mode. As a special case,
534 * if the ACE matches the current owner of the file,
535 * the owner bits are used, rather than the group bits.
536 * This is reflected in the algorithm below.
541 * If ACE4_IDENTIFIER_GROUP is not set, and the "who" field
542 * in ACE matches the owner of the file, we shift amode three
543 * more bits, in order to have the owner permission bits
544 * placed in the three low order bits of amode.
546 if (entry->ae_tag == ACL_USER && entry->ae_id == file_owner_id)
549 if (entry->ae_perm & ACL_READ_DATA) {
551 previous->ae_perm &= ~ACL_READ_DATA;
553 previous->ae_perm |= ACL_READ_DATA;
556 if (entry->ae_perm & ACL_WRITE_DATA) {
558 previous->ae_perm &= ~ACL_WRITE_DATA;
560 previous->ae_perm |= ACL_WRITE_DATA;
563 if (entry->ae_perm & ACL_APPEND_DATA) {
565 previous->ae_perm &= ~ACL_APPEND_DATA;
567 previous->ae_perm |= ACL_APPEND_DATA;
570 if (entry->ae_perm & ACL_EXECUTE) {
572 previous->ae_perm &= ~ACL_EXECUTE;
574 previous->ae_perm |= ACL_EXECUTE;
578 * 1.5.3. If ACE4_IDENTIFIER_GROUP is set in the flags
581 * XXX: This point is not there in the Falkner's draft.
583 if (entry->ae_tag == ACL_GROUP &&
584 entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) {
585 mode_t extramode, ownermode;
586 extramode = (mode >> 3) & 07;
587 ownermode = mode >> 6;
588 extramode &= ~ownermode;
591 if (extramode & READ) {
592 entry->ae_perm &= ~ACL_READ_DATA;
593 previous->ae_perm &= ~ACL_READ_DATA;
596 if (extramode & WRITE) {
598 ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
600 ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
603 if (extramode & EXEC) {
604 entry->ae_perm &= ~ACL_EXECUTE;
605 previous->ae_perm &= ~ACL_EXECUTE;
612 * 2. If there at least six ACEs, the final six ACEs are examined.
613 * If they are not equal to what we want, append six ACEs.
616 if (aclp->acl_cnt < 6) {
619 a6 = &(aclp->acl_entry[aclp->acl_cnt - 1]);
620 a5 = &(aclp->acl_entry[aclp->acl_cnt - 2]);
621 a4 = &(aclp->acl_entry[aclp->acl_cnt - 3]);
622 a3 = &(aclp->acl_entry[aclp->acl_cnt - 4]);
623 a2 = &(aclp->acl_entry[aclp->acl_cnt - 5]);
624 a1 = &(aclp->acl_entry[aclp->acl_cnt - 6]);
626 if (!_acl_entry_matches(a1, ACL_USER_OBJ, 0,
627 ACL_ENTRY_TYPE_DENY))
629 if (!_acl_entry_matches(a2, ACL_USER_OBJ, ACL_WRITE_ACL |
630 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
631 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW))
633 if (!_acl_entry_matches(a3, ACL_GROUP_OBJ, 0,
634 ACL_ENTRY_TYPE_DENY))
636 if (!_acl_entry_matches(a4, ACL_GROUP_OBJ, 0,
637 ACL_ENTRY_TYPE_ALLOW))
639 if (!_acl_entry_matches(a5, ACL_EVERYONE, ACL_WRITE_ACL |
640 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
641 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY))
643 if (!_acl_entry_matches(a6, ACL_EVERYONE, ACL_READ_ACL |
644 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
645 ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW))
650 KASSERT(aclp->acl_cnt + 6 <= ACL_MAX_ENTRIES,
651 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
653 a1 = _acl_append(aclp, ACL_USER_OBJ, 0, ACL_ENTRY_TYPE_DENY);
654 a2 = _acl_append(aclp, ACL_USER_OBJ, ACL_WRITE_ACL |
655 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
656 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW);
657 a3 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_DENY);
658 a4 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_ALLOW);
659 a5 = _acl_append(aclp, ACL_EVERYONE, ACL_WRITE_ACL |
660 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
661 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY);
662 a6 = _acl_append(aclp, ACL_EVERYONE, ACL_READ_ACL |
663 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
664 ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW);
666 KASSERT(a1 != NULL && a2 != NULL && a3 != NULL && a4 != NULL &&
667 a5 != NULL && a6 != NULL, ("couldn't append to ACL."));
671 * 3. The final six ACEs are adjusted according to the incoming mode.
674 a2->ae_perm |= ACL_READ_DATA;
676 a1->ae_perm |= ACL_READ_DATA;
678 a2->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
680 a1->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
682 a2->ae_perm |= ACL_EXECUTE;
684 a1->ae_perm |= ACL_EXECUTE;
687 a4->ae_perm |= ACL_READ_DATA;
689 a3->ae_perm |= ACL_READ_DATA;
691 a4->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
693 a3->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
695 a4->ae_perm |= ACL_EXECUTE;
697 a3->ae_perm |= ACL_EXECUTE;
700 a6->ae_perm |= ACL_READ_DATA;
702 a5->ae_perm |= ACL_READ_DATA;
704 a6->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
706 a5->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
708 a6->ae_perm |= ACL_EXECUTE;
710 a5->ae_perm |= ACL_EXECUTE;
715 acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode,
719 if (acl_nfs4_old_semantics)
720 acl_nfs4_sync_acl_from_mode_draft(aclp, mode, file_owner_id);
722 acl_nfs4_trivial_from_mode(aclp, mode);
727 acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp)
730 mode_t old_mode = *_mode, mode = 0, seen = 0;
731 const struct acl_entry *entry;
733 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
734 ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
737 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
739 * 3.16.6.1. Recomputing mode upon SETATTR of ACL
742 for (i = 0; i < aclp->acl_cnt; i++) {
743 entry = &(aclp->acl_entry[i]);
745 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
746 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
749 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
752 if (entry->ae_tag == ACL_USER_OBJ) {
753 if ((entry->ae_perm & ACL_READ_DATA) &&
754 ((seen & S_IRUSR) == 0)) {
756 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
759 if ((entry->ae_perm & ACL_WRITE_DATA) &&
760 ((seen & S_IWUSR) == 0)) {
762 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
765 if ((entry->ae_perm & ACL_EXECUTE) &&
766 ((seen & S_IXUSR) == 0)) {
768 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
771 } else if (entry->ae_tag == ACL_GROUP_OBJ) {
772 if ((entry->ae_perm & ACL_READ_DATA) &&
773 ((seen & S_IRGRP) == 0)) {
775 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
778 if ((entry->ae_perm & ACL_WRITE_DATA) &&
779 ((seen & S_IWGRP) == 0)) {
781 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
784 if ((entry->ae_perm & ACL_EXECUTE) &&
785 ((seen & S_IXGRP) == 0)) {
787 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
790 } else if (entry->ae_tag == ACL_EVERYONE) {
791 if (entry->ae_perm & ACL_READ_DATA) {
792 if ((seen & S_IRUSR) == 0) {
794 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
797 if ((seen & S_IRGRP) == 0) {
799 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
802 if ((seen & S_IROTH) == 0) {
804 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
808 if (entry->ae_perm & ACL_WRITE_DATA) {
809 if ((seen & S_IWUSR) == 0) {
811 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
814 if ((seen & S_IWGRP) == 0) {
816 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
819 if ((seen & S_IWOTH) == 0) {
821 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
825 if (entry->ae_perm & ACL_EXECUTE) {
826 if ((seen & S_IXUSR) == 0) {
828 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
831 if ((seen & S_IXGRP) == 0) {
833 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
836 if ((seen & S_IXOTH) == 0) {
838 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
845 *_mode = mode | (old_mode & ACL_PRESERVE_MASK);
850 * Calculate inherited ACL in a manner compatible with NFSv4 Minor Version 1,
851 * draft-ietf-nfsv4-minorversion1-03.txt.
854 acl_nfs4_compute_inherited_acl_draft(const struct acl *parent_aclp,
855 struct acl *child_aclp, mode_t mode, int file_owner_id,
859 const struct acl_entry *parent_entry;
860 struct acl_entry *entry, *copy;
862 KASSERT(child_aclp->acl_cnt == 0, ("child_aclp->acl_cnt == 0"));
863 KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
864 ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
867 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
869 * 3.16.6.2. Applying the mode given to CREATE or OPEN
870 * to an inherited ACL
874 * 1. Form an ACL that is the concatenation of all inheritable ACEs.
876 for (i = 0; i < parent_aclp->acl_cnt; i++) {
877 parent_entry = &(parent_aclp->acl_entry[i]);
878 flags = parent_entry->ae_flags;
881 * Entry is not inheritable at all.
883 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
884 ACL_ENTRY_FILE_INHERIT)) == 0)
888 * We're creating a file, but entry is not inheritable
891 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
895 * Entry is inheritable only by files, but has NO_PROPAGATE
896 * flag set, and we're creating a directory, so it wouldn't
897 * propagate to any file in that directory anyway.
900 (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
901 (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
904 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
905 ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
906 child_aclp->acl_entry[child_aclp->acl_cnt] = *parent_entry;
907 child_aclp->acl_cnt++;
911 * 2. For each entry in the new ACL, adjust its flags, possibly
912 * creating two entries in place of one.
914 for (i = 0; i < child_aclp->acl_cnt; i++) {
915 entry = &(child_aclp->acl_entry[i]);
918 * This is not in the specification, but SunOS
919 * apparently does that.
921 if (((entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT) ||
923 entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
924 entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
927 * 2.A. If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if the object
928 * being created is not a directory, then clear the
929 * following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
930 * ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
931 * ACL_ENTRY_INHERIT_ONLY.
933 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
935 entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
936 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
937 ACL_ENTRY_INHERIT_ONLY);
940 * Continue on to the next ACE.
946 * 2.B. If the object is a directory and ACL_ENTRY_FILE_INHERIT
947 * is set, but ACL_ENTRY_NO_PROPAGATE_INHERIT is not set, ensure
948 * that ACL_ENTRY_INHERIT_ONLY is set. Continue to the
949 * next ACE. Otherwise...
952 * XXX: Read it again and make sure what does the "otherwise"
956 (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
957 ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
958 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
963 * 2.C. If the type of the ACE is neither ALLOW nor deny,
966 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
967 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
971 * 2.D. Copy the original ACE into a second, adjacent ACE.
973 copy = _acl_duplicate_entry(child_aclp, i);
976 * 2.E. On the first ACE, ensure that ACL_ENTRY_INHERIT_ONLY
979 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
982 * 2.F. On the second ACE, clear the following flags:
983 * ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_FILE_INHERIT,
984 * ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_INHERIT_ONLY.
986 copy->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
987 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
988 ACL_ENTRY_INHERIT_ONLY);
991 * 2.G. On the second ACE, if the type is ALLOW,
992 * an implementation MAY clear the following
993 * mask bits: ACL_WRITE_ACL, ACL_WRITE_OWNER.
995 if (copy->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
996 copy->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
999 * Increment the counter to skip the copied entry.
1005 * 3. To ensure that the mode is honored, apply the algorithm describe
1006 * in Section 2.16.6.3, using the mode that is to be used for file
1009 acl_nfs4_sync_acl_from_mode(child_aclp, mode, file_owner_id);
1011 #endif /* _KERNEL */
1014 * Populate the ACL with entries inherited from parent_aclp.
1017 acl_nfs4_inherit_entries(const struct acl *parent_aclp,
1018 struct acl *child_aclp, mode_t mode, int file_owner_id,
1022 const struct acl_entry *parent_entry;
1023 struct acl_entry *entry;
1025 KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
1026 ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
1028 for (i = 0; i < parent_aclp->acl_cnt; i++) {
1029 parent_entry = &(parent_aclp->acl_entry[i]);
1030 flags = parent_entry->ae_flags;
1031 tag = parent_entry->ae_tag;
1034 * Don't inherit owner@, group@, or everyone@ entries.
1036 if (tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ ||
1037 tag == ACL_EVERYONE)
1041 * Entry is not inheritable at all.
1043 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
1044 ACL_ENTRY_FILE_INHERIT)) == 0)
1048 * We're creating a file, but entry is not inheritable
1051 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
1055 * Entry is inheritable only by files, but has NO_PROPAGATE
1056 * flag set, and we're creating a directory, so it wouldn't
1057 * propagate to any file in that directory anyway.
1060 (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
1061 (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
1065 * Entry qualifies for being inherited.
1067 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
1068 ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
1069 entry = &(child_aclp->acl_entry[child_aclp->acl_cnt]);
1070 *entry = *parent_entry;
1071 child_aclp->acl_cnt++;
1073 entry->ae_flags &= ~ACL_ENTRY_INHERIT_ONLY;
1074 entry->ae_flags |= ACL_ENTRY_INHERITED;
1077 * If the type of the ACE is neither ALLOW nor DENY,
1078 * then leave it as it is and proceed to the next one.
1080 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
1081 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
1085 * If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if
1086 * the object being created is not a directory, then clear
1087 * the following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
1088 * ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
1089 * ACL_ENTRY_INHERIT_ONLY.
1091 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
1093 entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
1094 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
1095 ACL_ENTRY_INHERIT_ONLY);
1099 * If the object is a directory and ACL_ENTRY_FILE_INHERIT
1100 * is set, but ACL_ENTRY_DIRECTORY_INHERIT is not set, ensure
1101 * that ACL_ENTRY_INHERIT_ONLY is set.
1104 (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
1105 ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
1106 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
1109 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW &&
1110 (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY) == 0) {
1112 * Some permissions must never be inherited.
1114 entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER |
1115 ACL_WRITE_NAMED_ATTRS | ACL_WRITE_ATTRIBUTES);
1118 * Others must be masked according to the file mode.
1120 if ((mode & S_IRGRP) == 0)
1121 entry->ae_perm &= ~ACL_READ_DATA;
1122 if ((mode & S_IWGRP) == 0)
1124 ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
1125 if ((mode & S_IXGRP) == 0)
1126 entry->ae_perm &= ~ACL_EXECUTE;
1132 * Calculate inherited ACL in a manner compatible with PSARC/2010/029.
1133 * It's also being used to calculate a trivial ACL, by inheriting from
1137 acl_nfs4_compute_inherited_acl_psarc(const struct acl *parent_aclp,
1138 struct acl *aclp, mode_t mode, int file_owner_id, int is_directory)
1140 acl_perm_t user_allow_first = 0, user_deny = 0, group_deny = 0;
1141 acl_perm_t user_allow, group_allow, everyone_allow;
1143 KASSERT(aclp->acl_cnt == 0, ("aclp->acl_cnt == 0"));
1145 user_allow = group_allow = everyone_allow = ACL_READ_ACL |
1146 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS | ACL_SYNCHRONIZE;
1147 user_allow |= ACL_WRITE_ACL | ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
1148 ACL_WRITE_NAMED_ATTRS;
1151 user_allow |= ACL_READ_DATA;
1153 user_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1155 user_allow |= ACL_EXECUTE;
1158 group_allow |= ACL_READ_DATA;
1160 group_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1162 group_allow |= ACL_EXECUTE;
1165 everyone_allow |= ACL_READ_DATA;
1167 everyone_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1169 everyone_allow |= ACL_EXECUTE;
1171 user_deny = ((group_allow | everyone_allow) & ~user_allow);
1172 group_deny = everyone_allow & ~group_allow;
1173 user_allow_first = group_deny & ~user_deny;
1175 if (user_allow_first != 0)
1176 _acl_append(aclp, ACL_USER_OBJ, user_allow_first,
1177 ACL_ENTRY_TYPE_ALLOW);
1179 _acl_append(aclp, ACL_USER_OBJ, user_deny,
1180 ACL_ENTRY_TYPE_DENY);
1181 if (group_deny != 0)
1182 _acl_append(aclp, ACL_GROUP_OBJ, group_deny,
1183 ACL_ENTRY_TYPE_DENY);
1185 if (parent_aclp != NULL)
1186 acl_nfs4_inherit_entries(parent_aclp, aclp, mode,
1187 file_owner_id, is_directory);
1189 _acl_append(aclp, ACL_USER_OBJ, user_allow, ACL_ENTRY_TYPE_ALLOW);
1190 _acl_append(aclp, ACL_GROUP_OBJ, group_allow, ACL_ENTRY_TYPE_ALLOW);
1191 _acl_append(aclp, ACL_EVERYONE, everyone_allow, ACL_ENTRY_TYPE_ALLOW);
1196 acl_nfs4_compute_inherited_acl(const struct acl *parent_aclp,
1197 struct acl *child_aclp, mode_t mode, int file_owner_id,
1201 if (acl_nfs4_old_semantics)
1202 acl_nfs4_compute_inherited_acl_draft(parent_aclp, child_aclp,
1203 mode, file_owner_id, is_directory);
1205 acl_nfs4_compute_inherited_acl_psarc(parent_aclp, child_aclp,
1206 mode, file_owner_id, is_directory);
1208 #endif /* _KERNEL */
1211 * Calculate trivial ACL in a manner compatible with PSARC/2010/029.
1212 * Note that this results in an ACL different from (but semantically
1213 * equal to) the "canonical six" trivial ACL computed using algorithm
1214 * described in draft-ietf-nfsv4-minorversion1-03.txt, 3.16.6.2.
1217 acl_nfs4_trivial_from_mode(struct acl *aclp, mode_t mode)
1221 acl_nfs4_compute_inherited_acl_psarc(NULL, aclp, mode, -1, -1);
1226 * This routine is used by libc to implement acl_strip_np(3)
1227 * and acl_is_trivial_np(3).
1230 acl_nfs4_trivial_from_mode_libc(struct acl *aclp, int mode, int canonical_six)
1235 acl_nfs4_sync_acl_from_mode_draft(aclp, mode, -1);
1237 acl_nfs4_trivial_from_mode(aclp, mode);
1239 #endif /* !_KERNEL */
1243 _acls_are_equal(const struct acl *a, const struct acl *b)
1246 const struct acl_entry *entrya, *entryb;
1248 if (a->acl_cnt != b->acl_cnt)
1251 for (i = 0; i < b->acl_cnt; i++) {
1252 entrya = &(a->acl_entry[i]);
1253 entryb = &(b->acl_entry[i]);
1255 if (entrya->ae_tag != entryb->ae_tag ||
1256 entrya->ae_id != entryb->ae_id ||
1257 entrya->ae_perm != entryb->ae_perm ||
1258 entrya->ae_entry_type != entryb->ae_entry_type ||
1259 entrya->ae_flags != entryb->ae_flags)
1267 * This routine is used to determine whether to remove extended attribute
1268 * that stores ACL contents.
1271 acl_nfs4_is_trivial(const struct acl *aclp, int file_owner_id)
1275 struct acl *tmpaclp;
1277 if (aclp->acl_cnt > 6)
1281 * Compute the mode from the ACL, then compute new ACL from that mode.
1282 * If the ACLs are identical, then the ACL is trivial.
1284 * XXX: I guess there is a faster way to do this. However, even
1285 * this slow implementation significantly speeds things up
1286 * for files that don't have non-trivial ACLs - it's critical
1287 * for performance to not use EA when they are not needed.
1289 * First try the PSARC/2010/029 semantics.
1291 tmpaclp = acl_alloc(M_WAITOK | M_ZERO);
1292 acl_nfs4_sync_mode_from_acl(&tmpmode, aclp);
1293 acl_nfs4_trivial_from_mode(tmpaclp, tmpmode);
1294 trivial = _acls_are_equal(aclp, tmpaclp);
1301 * Check if it's a draft-ietf-nfsv4-minorversion1-03.txt trivial ACL.
1303 tmpaclp->acl_cnt = 0;
1304 acl_nfs4_sync_acl_from_mode_draft(tmpaclp, tmpmode, file_owner_id);
1305 trivial = _acls_are_equal(aclp, tmpaclp);
1310 #endif /* _KERNEL */
1313 acl_nfs4_check(const struct acl *aclp, int is_directory)
1316 const struct acl_entry *entry;
1319 * The spec doesn't seem to say anything about ACL validity.
1320 * It seems there is not much to do here. There is even no need
1321 * to count "owner@" or "everyone@" (ACL_USER_OBJ and ACL_EVERYONE)
1322 * entries, as there can be several of them and that's perfectly
1323 * valid. There can be none of them too. Really.
1326 if (aclp->acl_cnt > ACL_MAX_ENTRIES || aclp->acl_cnt <= 0)
1329 for (i = 0; i < aclp->acl_cnt; i++) {
1330 entry = &(aclp->acl_entry[i]);
1332 switch (entry->ae_tag) {
1336 if (entry->ae_id != ACL_UNDEFINED_ID)
1342 if (entry->ae_id == ACL_UNDEFINED_ID)
1350 if ((entry->ae_perm | ACL_NFS4_PERM_BITS) != ACL_NFS4_PERM_BITS)
1354 * Disallow ACL_ENTRY_TYPE_AUDIT and ACL_ENTRY_TYPE_ALARM for now.
1356 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
1357 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
1360 if ((entry->ae_flags | ACL_FLAGS_BITS) != ACL_FLAGS_BITS)
1363 /* Disallow unimplemented flags. */
1364 if (entry->ae_flags & (ACL_ENTRY_SUCCESSFUL_ACCESS |
1365 ACL_ENTRY_FAILED_ACCESS))
1368 /* Disallow flags not allowed for ordinary files. */
1369 if (!is_directory) {
1370 if (entry->ae_flags & (ACL_ENTRY_FILE_INHERIT |
1371 ACL_ENTRY_DIRECTORY_INHERIT |
1372 ACL_ENTRY_NO_PROPAGATE_INHERIT | ACL_ENTRY_INHERIT_ONLY))
1382 acl_nfs4_modload(module_t module, int what, void *arg)
1410 static moduledata_t acl_nfs4_mod = {
1417 * XXX TODO: which subsystem, order?
1419 DECLARE_MODULE(acl_nfs4, acl_nfs4_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1420 MODULE_VERSION(acl_nfs4, 1);
1421 #endif /* _KERNEL */