2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1999-2006 Robert N. M. Watson
7 * This software was developed by Robert Watson for the TrustedBSD Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Developed by the TrustedBSD Project.
33 * ACL support routines specific to POSIX.1e access control lists. These are
34 * utility routines for code common across file systems implementing POSIX.1e
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/systm.h>
42 #include <sys/mount.h>
44 #include <sys/vnode.h>
45 #include <sys/errno.h>
50 * Implement a version of vaccess() that understands POSIX.1e ACL semantics;
51 * the access ACL has already been prepared for evaluation by the file system
52 * and is passed via 'uid', 'gid', and 'acl'. Return 0 on success, else an
56 vaccess_acl_posix1e(__enum_uint8(vtype) type, uid_t file_uid, gid_t file_gid,
57 struct acl *acl, accmode_t accmode, struct ucred *cred)
59 struct acl_entry *acl_other, *acl_mask;
60 accmode_t dac_granted;
61 accmode_t priv_granted;
62 accmode_t acl_mask_granted;
65 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
66 ("invalid bit in accmode"));
67 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
68 ("VAPPEND without VWRITE"));
71 * Look for a normal, non-privileged way to access the file/directory
72 * as requested. If it exists, go with that. Otherwise, attempt to
73 * use privileges granted via priv_granted. In some cases, which
74 * privileges to use may be ambiguous due to "best match", in which
75 * case fall back on first match for the time being.
79 * Determine privileges now, but don't apply until we've found a DAC
80 * entry that matches but has failed to allow access.
82 * XXXRW: Ideally, we'd determine the privileges required before
88 if ((accmode & VEXEC) && !priv_check_cred(cred, PRIV_VFS_LOOKUP))
89 priv_granted |= VEXEC;
92 * Ensure that at least one execute bit is on. Otherwise,
93 * a privileged user will always succeed, and we don't want
94 * this to happen unless the file really is executable.
96 if ((accmode & VEXEC) && (acl_posix1e_acl_to_mode(acl) &
97 (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
98 !priv_check_cred(cred, PRIV_VFS_EXEC))
99 priv_granted |= VEXEC;
102 if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ))
103 priv_granted |= VREAD;
105 if (((accmode & VWRITE) || (accmode & VAPPEND)) &&
106 !priv_check_cred(cred, PRIV_VFS_WRITE))
107 priv_granted |= (VWRITE | VAPPEND);
109 if ((accmode & VADMIN) && !priv_check_cred(cred, PRIV_VFS_ADMIN))
110 priv_granted |= VADMIN;
113 * The owner matches if the effective uid associated with the
114 * credential matches that of the ACL_USER_OBJ entry. While we're
115 * doing the first scan, also cache the location of the ACL_MASK and
116 * ACL_OTHER entries, preventing some future iterations.
118 acl_mask = acl_other = NULL;
119 for (i = 0; i < acl->acl_cnt; i++) {
120 switch (acl->acl_entry[i].ae_tag) {
122 if (file_uid != cred->cr_uid)
125 dac_granted |= VADMIN;
126 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
127 dac_granted |= VEXEC;
128 if (acl->acl_entry[i].ae_perm & ACL_READ)
129 dac_granted |= VREAD;
130 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
131 dac_granted |= (VWRITE | VAPPEND);
132 if ((accmode & dac_granted) == accmode)
136 * XXXRW: Do privilege lookup here.
138 if ((accmode & (dac_granted | priv_granted)) ==
145 acl_mask = &acl->acl_entry[i];
149 acl_other = &acl->acl_entry[i];
158 * An ACL_OTHER entry should always exist in a valid access ACL. If
159 * it doesn't, then generate a serious failure. For now, this means
160 * a debugging message and EPERM, but in the future should probably
163 if (acl_other == NULL) {
165 * XXX This should never happen
167 printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
172 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields are
173 * masked by an ACL_MASK entry, if any. As such, first identify the
174 * ACL_MASK field, then iterate through identifying potential user
175 * matches, then group matches. If there is no ACL_MASK, assume that
176 * the mask allows all requests to succeed.
178 if (acl_mask != NULL) {
179 acl_mask_granted = 0;
180 if (acl_mask->ae_perm & ACL_EXECUTE)
181 acl_mask_granted |= VEXEC;
182 if (acl_mask->ae_perm & ACL_READ)
183 acl_mask_granted |= VREAD;
184 if (acl_mask->ae_perm & ACL_WRITE)
185 acl_mask_granted |= (VWRITE | VAPPEND);
187 acl_mask_granted = VEXEC | VREAD | VWRITE | VAPPEND;
190 * Check ACL_USER ACL entries. There will either be one or no
191 * matches; if there is one, we accept or rejected based on the
192 * match; otherwise, we continue on to groups.
194 for (i = 0; i < acl->acl_cnt; i++) {
195 switch (acl->acl_entry[i].ae_tag) {
197 if (acl->acl_entry[i].ae_id != cred->cr_uid)
200 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
201 dac_granted |= VEXEC;
202 if (acl->acl_entry[i].ae_perm & ACL_READ)
203 dac_granted |= VREAD;
204 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
205 dac_granted |= (VWRITE | VAPPEND);
206 dac_granted &= acl_mask_granted;
207 if ((accmode & dac_granted) == accmode)
210 * XXXRW: Do privilege lookup here.
212 if ((accmode & (dac_granted | priv_granted)) !=
221 * Group match is best-match, not first-match, so find a "best"
222 * match. Iterate across, testing each potential group match. Make
223 * sure we keep track of whether we found a match or not, so that we
224 * know if we should try again with any available privilege, or if we
225 * should move on to ACL_OTHER.
228 for (i = 0; i < acl->acl_cnt; i++) {
229 switch (acl->acl_entry[i].ae_tag) {
231 if (!groupmember(file_gid, cred))
234 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
235 dac_granted |= VEXEC;
236 if (acl->acl_entry[i].ae_perm & ACL_READ)
237 dac_granted |= VREAD;
238 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
239 dac_granted |= (VWRITE | VAPPEND);
240 dac_granted &= acl_mask_granted;
242 if ((accmode & dac_granted) == accmode)
249 if (!groupmember(acl->acl_entry[i].ae_id, cred))
252 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
253 dac_granted |= VEXEC;
254 if (acl->acl_entry[i].ae_perm & ACL_READ)
255 dac_granted |= VREAD;
256 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
257 dac_granted |= (VWRITE | VAPPEND);
258 dac_granted &= acl_mask_granted;
260 if ((accmode & dac_granted) == accmode)
271 if (group_matched == 1) {
273 * There was a match, but it did not grant rights via pure
274 * DAC. Try again, this time with privilege.
276 for (i = 0; i < acl->acl_cnt; i++) {
277 switch (acl->acl_entry[i].ae_tag) {
279 if (!groupmember(file_gid, cred))
282 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
283 dac_granted |= VEXEC;
284 if (acl->acl_entry[i].ae_perm & ACL_READ)
285 dac_granted |= VREAD;
286 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
287 dac_granted |= (VWRITE | VAPPEND);
288 dac_granted &= acl_mask_granted;
291 * XXXRW: Do privilege lookup here.
293 if ((accmode & (dac_granted | priv_granted))
300 if (!groupmember(acl->acl_entry[i].ae_id,
304 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
305 dac_granted |= VEXEC;
306 if (acl->acl_entry[i].ae_perm & ACL_READ)
307 dac_granted |= VREAD;
308 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
309 dac_granted |= (VWRITE | VAPPEND);
310 dac_granted &= acl_mask_granted;
313 * XXXRW: Do privilege lookup here.
315 if ((accmode & (dac_granted | priv_granted))
326 * Even with privilege, group membership was not sufficient.
333 * Fall back on ACL_OTHER. ACL_MASK is not applied to ACL_OTHER.
336 if (acl_other->ae_perm & ACL_EXECUTE)
337 dac_granted |= VEXEC;
338 if (acl_other->ae_perm & ACL_READ)
339 dac_granted |= VREAD;
340 if (acl_other->ae_perm & ACL_WRITE)
341 dac_granted |= (VWRITE | VAPPEND);
343 if ((accmode & dac_granted) == accmode)
346 * XXXRW: Do privilege lookup here.
348 if ((accmode & (dac_granted | priv_granted)) == accmode) {
353 return ((accmode & VADMIN) ? EPERM : EACCES);
357 * For the purposes of filesystems maintaining the _OBJ entries in an inode
358 * with a mode_t field, this routine converts a mode_t entry to an
362 acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
395 printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag);
401 * Given inode information (uid, gid, mode), return an acl entry of the
405 acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
407 struct acl_entry acl_entry;
409 acl_entry.ae_tag = tag;
410 acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
411 acl_entry.ae_entry_type = 0;
412 acl_entry.ae_flags = 0;
415 acl_entry.ae_id = uid;
419 acl_entry.ae_id = gid;
423 acl_entry.ae_id = ACL_UNDEFINED_ID;
427 acl_entry.ae_id = ACL_UNDEFINED_ID;
428 printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
435 * Utility function to generate a file mode given appropriate ACL entries.
438 acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
439 struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
444 if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
446 if (acl_user_obj_entry->ae_perm & ACL_READ)
448 if (acl_user_obj_entry->ae_perm & ACL_WRITE)
450 if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
452 if (acl_group_obj_entry->ae_perm & ACL_READ)
454 if (acl_group_obj_entry->ae_perm & ACL_WRITE)
456 if (acl_other_entry->ae_perm & ACL_EXECUTE)
458 if (acl_other_entry->ae_perm & ACL_READ)
460 if (acl_other_entry->ae_perm & ACL_WRITE)
467 * Utility function to generate a file mode given a complete POSIX.1e access
468 * ACL. Note that if the ACL is improperly formed, this may result in a
472 acl_posix1e_acl_to_mode(struct acl *acl)
474 struct acl_entry *acl_mask, *acl_user_obj, *acl_group_obj, *acl_other;
478 * Find the ACL entries relevant to a POSIX permission mode.
480 acl_user_obj = acl_group_obj = acl_other = acl_mask = NULL;
481 for (i = 0; i < acl->acl_cnt; i++) {
482 switch (acl->acl_entry[i].ae_tag) {
484 acl_user_obj = &acl->acl_entry[i];
488 acl_group_obj = &acl->acl_entry[i];
492 acl_other = &acl->acl_entry[i];
496 acl_mask = &acl->acl_entry[i];
504 panic("acl_posix1e_acl_to_mode: bad ae_tag");
508 if (acl_user_obj == NULL || acl_group_obj == NULL || acl_other == NULL)
509 panic("acl_posix1e_acl_to_mode: missing base ae_tags");
512 * POSIX.1e specifies that if there is an ACL_MASK entry, we replace
513 * the mode "group" bits with its permissions. If there isn't, we
514 * use the ACL_GROUP_OBJ permissions.
516 if (acl_mask != NULL)
517 return (acl_posix1e_perms_to_mode(acl_user_obj, acl_mask,
520 return (acl_posix1e_perms_to_mode(acl_user_obj, acl_group_obj,
525 * Perform a syntactic check of the ACL, sufficient to allow an implementing
526 * filesystem to determine if it should accept this and rely on the POSIX.1e
530 acl_posix1e_check(struct acl *acl)
532 int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
533 int num_acl_mask, num_acl_other, i;
536 * Verify that the number of entries does not exceed the maximum
539 * Verify that the correct number of various sorts of ae_tags are
541 * Exactly one ACL_USER_OBJ
542 * Exactly one ACL_GROUP_OBJ
543 * Exactly one ACL_OTHER
544 * If any ACL_USER or ACL_GROUP entries appear, then exactly one
545 * ACL_MASK entry must also appear.
547 * Verify that all ae_perm entries are in ACL_PERM_BITS.
549 * Verify all ae_tag entries are understood by this implementation.
551 * Note: Does not check for uniqueness of qualifier (ae_id) field.
553 num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
554 num_acl_mask = num_acl_other = 0;
555 if (acl->acl_cnt > ACL_MAX_ENTRIES)
557 for (i = 0; i < acl->acl_cnt; i++) {
559 * Check for a valid tag.
561 switch(acl->acl_entry[i].ae_tag) {
563 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
564 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
569 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
570 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
575 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
580 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
585 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
586 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
591 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
592 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
600 * Check for valid perm entries.
602 if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
606 if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
607 (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
609 if (((num_acl_group != 0) || (num_acl_user != 0)) &&
616 * Given a requested mode for a new object, and a default ACL, combine the
617 * two to produce a new mode. Be careful not to clear any bits that aren't
618 * intended to be affected by the POSIX.1e ACL. Eventually, this might also
619 * take the cmask as an argument, if we push that down into
620 * per-filesystem-code.
623 acl_posix1e_newfilemode(mode_t cmode, struct acl *dacl)
629 * The current composition policy is that a permission bit must be
630 * set in *both* the ACL and the requested creation mode for it to
631 * appear in the resulting mode/ACL. First clear any possibly
632 * effected bits, then reconstruct.
634 mode &= ACL_PRESERVE_MASK;
635 mode |= (ACL_OVERRIDE_MASK & cmode & acl_posix1e_acl_to_mode(dacl));
641 acl_posix1e_modload(module_t mod, int what, void *arg)
669 static moduledata_t acl_posix1e_mod = {
675 DECLARE_MODULE(acl_posix1e, acl_posix1e_mod, SI_SUB_VFS, SI_ORDER_FIRST);
676 MODULE_VERSION(acl_posix1e, 1);