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/cdefs.h>
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>
51 * Implement a version of vaccess() that understands POSIX.1e ACL semantics;
52 * the access ACL has already been prepared for evaluation by the file system
53 * and is passed via 'uid', 'gid', and 'acl'. Return 0 on success, else an
57 vaccess_acl_posix1e(enum vtype type, uid_t file_uid, gid_t file_gid,
58 struct acl *acl, accmode_t accmode, struct ucred *cred)
60 struct acl_entry *acl_other, *acl_mask;
61 accmode_t dac_granted;
62 accmode_t priv_granted;
63 accmode_t acl_mask_granted;
66 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
67 ("invalid bit in accmode"));
68 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
69 ("VAPPEND without VWRITE"));
72 * Look for a normal, non-privileged way to access the file/directory
73 * as requested. If it exists, go with that. Otherwise, attempt to
74 * use privileges granted via priv_granted. In some cases, which
75 * privileges to use may be ambiguous due to "best match", in which
76 * case fall back on first match for the time being.
80 * Determine privileges now, but don't apply until we've found a DAC
81 * entry that matches but has failed to allow access.
83 * XXXRW: Ideally, we'd determine the privileges required before
89 if ((accmode & VEXEC) && !priv_check_cred(cred, PRIV_VFS_LOOKUP))
90 priv_granted |= VEXEC;
93 * Ensure that at least one execute bit is on. Otherwise,
94 * a privileged user will always succeed, and we don't want
95 * this to happen unless the file really is executable.
97 if ((accmode & VEXEC) && (acl_posix1e_acl_to_mode(acl) &
98 (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
99 !priv_check_cred(cred, PRIV_VFS_EXEC))
100 priv_granted |= VEXEC;
103 if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ))
104 priv_granted |= VREAD;
106 if (((accmode & VWRITE) || (accmode & VAPPEND)) &&
107 !priv_check_cred(cred, PRIV_VFS_WRITE))
108 priv_granted |= (VWRITE | VAPPEND);
110 if ((accmode & VADMIN) && !priv_check_cred(cred, PRIV_VFS_ADMIN))
111 priv_granted |= VADMIN;
114 * The owner matches if the effective uid associated with the
115 * credential matches that of the ACL_USER_OBJ entry. While we're
116 * doing the first scan, also cache the location of the ACL_MASK and
117 * ACL_OTHER entries, preventing some future iterations.
119 acl_mask = acl_other = NULL;
120 for (i = 0; i < acl->acl_cnt; i++) {
121 switch (acl->acl_entry[i].ae_tag) {
123 if (file_uid != cred->cr_uid)
126 dac_granted |= VADMIN;
127 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
128 dac_granted |= VEXEC;
129 if (acl->acl_entry[i].ae_perm & ACL_READ)
130 dac_granted |= VREAD;
131 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
132 dac_granted |= (VWRITE | VAPPEND);
133 if ((accmode & dac_granted) == accmode)
137 * XXXRW: Do privilege lookup here.
139 if ((accmode & (dac_granted | priv_granted)) ==
146 acl_mask = &acl->acl_entry[i];
150 acl_other = &acl->acl_entry[i];
159 * An ACL_OTHER entry should always exist in a valid access ACL. If
160 * it doesn't, then generate a serious failure. For now, this means
161 * a debugging message and EPERM, but in the future should probably
164 if (acl_other == NULL) {
166 * XXX This should never happen
168 printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
173 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields are
174 * masked by an ACL_MASK entry, if any. As such, first identify the
175 * ACL_MASK field, then iterate through identifying potential user
176 * matches, then group matches. If there is no ACL_MASK, assume that
177 * the mask allows all requests to succeed.
179 if (acl_mask != NULL) {
180 acl_mask_granted = 0;
181 if (acl_mask->ae_perm & ACL_EXECUTE)
182 acl_mask_granted |= VEXEC;
183 if (acl_mask->ae_perm & ACL_READ)
184 acl_mask_granted |= VREAD;
185 if (acl_mask->ae_perm & ACL_WRITE)
186 acl_mask_granted |= (VWRITE | VAPPEND);
188 acl_mask_granted = VEXEC | VREAD | VWRITE | VAPPEND;
191 * Check ACL_USER ACL entries. There will either be one or no
192 * matches; if there is one, we accept or rejected based on the
193 * match; otherwise, we continue on to groups.
195 for (i = 0; i < acl->acl_cnt; i++) {
196 switch (acl->acl_entry[i].ae_tag) {
198 if (acl->acl_entry[i].ae_id != cred->cr_uid)
201 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
202 dac_granted |= VEXEC;
203 if (acl->acl_entry[i].ae_perm & ACL_READ)
204 dac_granted |= VREAD;
205 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
206 dac_granted |= (VWRITE | VAPPEND);
207 dac_granted &= acl_mask_granted;
208 if ((accmode & dac_granted) == accmode)
211 * XXXRW: Do privilege lookup here.
213 if ((accmode & (dac_granted | priv_granted)) !=
222 * Group match is best-match, not first-match, so find a "best"
223 * match. Iterate across, testing each potential group match. Make
224 * sure we keep track of whether we found a match or not, so that we
225 * know if we should try again with any available privilege, or if we
226 * should move on to ACL_OTHER.
229 for (i = 0; i < acl->acl_cnt; i++) {
230 switch (acl->acl_entry[i].ae_tag) {
232 if (!groupmember(file_gid, cred))
235 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
236 dac_granted |= VEXEC;
237 if (acl->acl_entry[i].ae_perm & ACL_READ)
238 dac_granted |= VREAD;
239 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
240 dac_granted |= (VWRITE | VAPPEND);
241 dac_granted &= acl_mask_granted;
243 if ((accmode & dac_granted) == accmode)
250 if (!groupmember(acl->acl_entry[i].ae_id, cred))
253 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
254 dac_granted |= VEXEC;
255 if (acl->acl_entry[i].ae_perm & ACL_READ)
256 dac_granted |= VREAD;
257 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
258 dac_granted |= (VWRITE | VAPPEND);
259 dac_granted &= acl_mask_granted;
261 if ((accmode & dac_granted) == accmode)
272 if (group_matched == 1) {
274 * There was a match, but it did not grant rights via pure
275 * DAC. Try again, this time with privilege.
277 for (i = 0; i < acl->acl_cnt; i++) {
278 switch (acl->acl_entry[i].ae_tag) {
280 if (!groupmember(file_gid, cred))
283 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
284 dac_granted |= VEXEC;
285 if (acl->acl_entry[i].ae_perm & ACL_READ)
286 dac_granted |= VREAD;
287 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
288 dac_granted |= (VWRITE | VAPPEND);
289 dac_granted &= acl_mask_granted;
292 * XXXRW: Do privilege lookup here.
294 if ((accmode & (dac_granted | priv_granted))
301 if (!groupmember(acl->acl_entry[i].ae_id,
305 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
306 dac_granted |= VEXEC;
307 if (acl->acl_entry[i].ae_perm & ACL_READ)
308 dac_granted |= VREAD;
309 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
310 dac_granted |= (VWRITE | VAPPEND);
311 dac_granted &= acl_mask_granted;
314 * XXXRW: Do privilege lookup here.
316 if ((accmode & (dac_granted | priv_granted))
327 * Even with privilege, group membership was not sufficient.
334 * Fall back on ACL_OTHER. ACL_MASK is not applied to ACL_OTHER.
337 if (acl_other->ae_perm & ACL_EXECUTE)
338 dac_granted |= VEXEC;
339 if (acl_other->ae_perm & ACL_READ)
340 dac_granted |= VREAD;
341 if (acl_other->ae_perm & ACL_WRITE)
342 dac_granted |= (VWRITE | VAPPEND);
344 if ((accmode & dac_granted) == accmode)
347 * XXXRW: Do privilege lookup here.
349 if ((accmode & (dac_granted | priv_granted)) == accmode) {
354 return ((accmode & VADMIN) ? EPERM : EACCES);
358 * For the purposes of filesystems maintaining the _OBJ entries in an inode
359 * with a mode_t field, this routine converts a mode_t entry to an
363 acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
396 printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag);
402 * Given inode information (uid, gid, mode), return an acl entry of the
406 acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
408 struct acl_entry acl_entry;
410 acl_entry.ae_tag = tag;
411 acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
412 acl_entry.ae_entry_type = 0;
413 acl_entry.ae_flags = 0;
416 acl_entry.ae_id = uid;
420 acl_entry.ae_id = gid;
424 acl_entry.ae_id = ACL_UNDEFINED_ID;
428 acl_entry.ae_id = ACL_UNDEFINED_ID;
429 printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
436 * Utility function to generate a file mode given appropriate ACL entries.
439 acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
440 struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
445 if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
447 if (acl_user_obj_entry->ae_perm & ACL_READ)
449 if (acl_user_obj_entry->ae_perm & ACL_WRITE)
451 if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
453 if (acl_group_obj_entry->ae_perm & ACL_READ)
455 if (acl_group_obj_entry->ae_perm & ACL_WRITE)
457 if (acl_other_entry->ae_perm & ACL_EXECUTE)
459 if (acl_other_entry->ae_perm & ACL_READ)
461 if (acl_other_entry->ae_perm & ACL_WRITE)
468 * Utility function to generate a file mode given a complete POSIX.1e access
469 * ACL. Note that if the ACL is improperly formed, this may result in a
473 acl_posix1e_acl_to_mode(struct acl *acl)
475 struct acl_entry *acl_mask, *acl_user_obj, *acl_group_obj, *acl_other;
479 * Find the ACL entries relevant to a POSIX permission mode.
481 acl_user_obj = acl_group_obj = acl_other = acl_mask = NULL;
482 for (i = 0; i < acl->acl_cnt; i++) {
483 switch (acl->acl_entry[i].ae_tag) {
485 acl_user_obj = &acl->acl_entry[i];
489 acl_group_obj = &acl->acl_entry[i];
493 acl_other = &acl->acl_entry[i];
497 acl_mask = &acl->acl_entry[i];
505 panic("acl_posix1e_acl_to_mode: bad ae_tag");
509 if (acl_user_obj == NULL || acl_group_obj == NULL || acl_other == NULL)
510 panic("acl_posix1e_acl_to_mode: missing base ae_tags");
513 * POSIX.1e specifies that if there is an ACL_MASK entry, we replace
514 * the mode "group" bits with its permissions. If there isn't, we
515 * use the ACL_GROUP_OBJ permissions.
517 if (acl_mask != NULL)
518 return (acl_posix1e_perms_to_mode(acl_user_obj, acl_mask,
521 return (acl_posix1e_perms_to_mode(acl_user_obj, acl_group_obj,
526 * Perform a syntactic check of the ACL, sufficient to allow an implementing
527 * filesystem to determine if it should accept this and rely on the POSIX.1e
531 acl_posix1e_check(struct acl *acl)
533 int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
534 int num_acl_mask, num_acl_other, i;
537 * Verify that the number of entries does not exceed the maximum
540 * Verify that the correct number of various sorts of ae_tags are
542 * Exactly one ACL_USER_OBJ
543 * Exactly one ACL_GROUP_OBJ
544 * Exactly one ACL_OTHER
545 * If any ACL_USER or ACL_GROUP entries appear, then exactly one
546 * ACL_MASK entry must also appear.
548 * Verify that all ae_perm entries are in ACL_PERM_BITS.
550 * Verify all ae_tag entries are understood by this implementation.
552 * Note: Does not check for uniqueness of qualifier (ae_id) field.
554 num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
555 num_acl_mask = num_acl_other = 0;
556 if (acl->acl_cnt > ACL_MAX_ENTRIES)
558 for (i = 0; i < acl->acl_cnt; i++) {
560 * Check for a valid tag.
562 switch(acl->acl_entry[i].ae_tag) {
564 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
565 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
570 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
571 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
576 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
581 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
586 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
587 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
592 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
593 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
601 * Check for valid perm entries.
603 if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
607 if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
608 (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
610 if (((num_acl_group != 0) || (num_acl_user != 0)) &&
617 * Given a requested mode for a new object, and a default ACL, combine the
618 * two to produce a new mode. Be careful not to clear any bits that aren't
619 * intended to be affected by the POSIX.1e ACL. Eventually, this might also
620 * take the cmask as an argument, if we push that down into
621 * per-filesystem-code.
624 acl_posix1e_newfilemode(mode_t cmode, struct acl *dacl)
630 * The current composition policy is that a permission bit must be
631 * set in *both* the ACL and the requested creation mode for it to
632 * appear in the resulting mode/ACL. First clear any possibly
633 * effected bits, then reconstruct.
635 mode &= ACL_PRESERVE_MASK;
636 mode |= (ACL_OVERRIDE_MASK & cmode & acl_posix1e_acl_to_mode(dacl));
642 acl_posix1e_modload(module_t mod, int what, void *arg)
670 static moduledata_t acl_posix1e_mod = {
676 DECLARE_MODULE(acl_posix1e, acl_posix1e_mod, SI_SUB_VFS, SI_ORDER_FIRST);
677 MODULE_VERSION(acl_posix1e, 1);