2 * Copyright (c) 1999, 2000, 2001 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Developed by the TrustedBSD Project.
30 * Support for POSIX.1e access control lists.
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/vnode.h>
40 #include <sys/mutex.h>
41 #include <sys/namei.h>
44 #include <sys/sysent.h>
45 #include <sys/errno.h>
49 MALLOC_DEFINE(M_ACL, "acl", "access control list");
51 static int vacl_set_acl(struct proc *p, struct vnode *vp, acl_type_t type,
53 static int vacl_get_acl(struct proc *p, struct vnode *vp, acl_type_t type,
55 static int vacl_aclcheck(struct proc *p, struct vnode *vp,
56 acl_type_t type, struct acl *aclp);
59 * Implement a version of vaccess() that understands POSIX.1e ACL semantics.
60 * Return 0 on success, else an errno value. Should be merged into
61 * vaccess() eventually.
64 vaccess_acl_posix1e(enum vtype type, uid_t file_uid, gid_t file_gid,
65 struct acl *acl, mode_t acc_mode, struct ucred *cred, int *privused)
67 struct acl_entry *acl_other, *acl_mask;
70 mode_t acl_mask_granted;
74 * Look for a normal, non-privileged way to access the file/directory
75 * as requested. If it exists, go with that. Otherwise, attempt
76 * to use privileges granted via cap_granted. In some cases,
77 * which privileges to use may be ambiguous due to "best match",
78 * in which case fall back on first match for the time being.
84 * Determine privileges now, but don't apply until we've found
85 * a DAC entry that matches but has failed to allow access.
88 if (suser_xxx(cred, NULL, PRISON_ROOT) == 0)
89 cap_granted = (VEXEC | VREAD | VWRITE | VADMIN);
96 if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
97 CAP_DAC_READ_SEARCH, PRISON_ROOT))
100 if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
101 CAP_DAC_EXECUTE, PRISON_ROOT))
102 cap_granted |= VEXEC;
105 if ((acc_mode & VREAD) && !cap_check(cred, NULL, CAP_DAC_READ_SEARCH,
107 cap_granted |= VREAD;
109 if ((acc_mode & VWRITE) && !cap_check(cred, NULL, CAP_DAC_WRITE,
111 cap_granted |= VWRITE;
113 if ((acc_mode & VADMIN) && !cap_check(cred, NULL, CAP_FOWNER,
115 cap_granted |= VADMIN;
116 #endif /* CAPABILITIES */
119 * The owner matches if the effective uid associated with the
120 * credential matches that of the ACL_USER_OBJ entry. While we're
121 * doing the first scan, also cache the location of the ACL_MASK
122 * and ACL_OTHER entries, preventing some future iterations.
124 acl_mask = acl_other = NULL;
125 for (i = 0; i < acl->acl_cnt; i++) {
126 switch (acl->acl_entry[i].ae_tag) {
128 if (file_uid != cred->cr_uid)
131 dac_granted |= VADMIN;
132 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
133 dac_granted |= VEXEC;
134 if (acl->acl_entry[i].ae_perm & ACL_READ)
135 dac_granted |= VREAD;
136 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
137 dac_granted |= VWRITE;
138 if ((acc_mode & dac_granted) == acc_mode)
140 if ((acc_mode & (dac_granted | cap_granted)) ==
142 if (privused != NULL)
149 acl_mask = &acl->acl_entry[i];
153 acl_other = &acl->acl_entry[i];
161 * An ACL_OTHER entry should always exist in a valid access
162 * ACL. If it doesn't, then generate a serious failure. For now,
163 * this means a debugging message and EPERM, but in the future
164 * should probably be a panic.
166 if (acl_other == NULL) {
168 * XXX This should never happen
170 printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
175 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields
176 * are masked by an ACL_MASK entry, if any. As such, first identify
177 * the ACL_MASK field, then iterate through identifying potential
178 * user matches, then group matches. If there is no ACL_MASK,
179 * assume that the mask allows all requests to succeed.
181 if (acl_mask != NULL) {
182 acl_mask_granted = 0;
183 if (acl_mask->ae_perm & ACL_EXECUTE)
184 acl_mask_granted |= VEXEC;
185 if (acl_mask->ae_perm & ACL_READ)
186 acl_mask_granted |= VREAD;
187 if (acl_mask->ae_perm & ACL_WRITE)
188 acl_mask_granted |= VWRITE;
190 acl_mask_granted = VEXEC | VREAD | VWRITE;
193 * Iterate through user ACL entries. Do checks twice, first
194 * without privilege, and then if a match is found but failed,
195 * a second time with privilege.
199 * Check ACL_USER ACL entries.
201 for (i = 0; i < acl->acl_cnt; i++) {
202 switch (acl->acl_entry[i].ae_tag) {
204 if (acl->acl_entry[i].ae_id != cred->cr_uid)
207 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
208 dac_granted |= VEXEC;
209 if (acl->acl_entry[i].ae_perm & ACL_READ)
210 dac_granted |= VREAD;
211 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
212 dac_granted |= VWRITE;
213 dac_granted &= acl_mask_granted;
214 if ((acc_mode & dac_granted) == acc_mode)
216 if ((acc_mode & (dac_granted | cap_granted)) !=
220 if (privused != NULL)
227 * Group match is best-match, not first-match, so find a
228 * "best" match. Iterate across, testing each potential group
229 * match. Make sure we keep track of whether we found a match
230 * or not, so that we know if we should try again with any
231 * available privilege, or if we should move on to ACL_OTHER.
234 for (i = 0; i < acl->acl_cnt; i++) {
235 switch (acl->acl_entry[i].ae_tag) {
237 if (!groupmember(file_gid, cred))
240 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
241 dac_granted |= VEXEC;
242 if (acl->acl_entry[i].ae_perm & ACL_READ)
243 dac_granted |= VREAD;
244 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
245 dac_granted |= VWRITE;
246 dac_granted &= acl_mask_granted;
248 if ((acc_mode & dac_granted) == acc_mode)
255 if (!groupmember(acl->acl_entry[i].ae_id, cred))
258 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
259 dac_granted |= VEXEC;
260 if (acl->acl_entry[i].ae_perm & ACL_READ)
261 dac_granted |= VREAD;
262 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
263 dac_granted |= VWRITE;
264 dac_granted &= acl_mask_granted;
266 if ((acc_mode & dac_granted) == acc_mode)
276 if (group_matched == 1) {
278 * There was a match, but it did not grant rights via
279 * pure DAC. Try again, this time with privilege.
281 for (i = 0; i < acl->acl_cnt; i++) {
282 switch (acl->acl_entry[i].ae_tag) {
284 if (!groupmember(file_gid, cred))
287 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
288 dac_granted |= VEXEC;
289 if (acl->acl_entry[i].ae_perm & ACL_READ)
290 dac_granted |= VREAD;
291 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
292 dac_granted |= VWRITE;
293 dac_granted &= acl_mask_granted;
295 if ((acc_mode & (dac_granted | cap_granted)) !=
299 if (privused != NULL)
304 if (!groupmember(acl->acl_entry[i].ae_id,
308 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
309 dac_granted |= VEXEC;
310 if (acl->acl_entry[i].ae_perm & ACL_READ)
311 dac_granted |= VREAD;
312 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
313 dac_granted |= VWRITE;
314 dac_granted &= acl_mask_granted;
316 if ((acc_mode & (dac_granted | cap_granted)) !=
320 if (privused != NULL)
328 * Even with privilege, group membership was not sufficient.
335 * Fall back on ACL_OTHER. ACL_MASK is not applied to ACL_OTHER.
338 if (acl_other->ae_perm & ACL_EXECUTE)
339 dac_granted |= VEXEC;
340 if (acl_other->ae_perm & ACL_READ)
341 dac_granted |= VREAD;
342 if (acl_other->ae_perm & ACL_WRITE)
343 dac_granted |= VWRITE;
345 if ((acc_mode & dac_granted) == acc_mode)
347 if ((acc_mode & (dac_granted | cap_granted)) == acc_mode) {
348 if (privused != NULL)
354 return ((acc_mode & VADMIN) ? EPERM : EACCES);
358 * For the purposes of file systems maintaining the _OBJ entries in an
359 * inode with a mode_t field, this routine converts a mode_t entry
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);
414 acl_entry.ae_id = uid;
418 acl_entry.ae_id = gid;
427 printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
434 * Utility function to generate a file mode given appropriate ACL entries.
437 acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
438 struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
443 if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
445 if (acl_user_obj_entry->ae_perm & ACL_READ)
447 if (acl_user_obj_entry->ae_perm & ACL_WRITE)
449 if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
451 if (acl_group_obj_entry->ae_perm & ACL_READ)
453 if (acl_group_obj_entry->ae_perm & ACL_WRITE)
455 if (acl_other_entry->ae_perm & ACL_EXECUTE)
457 if (acl_other_entry->ae_perm & ACL_READ)
459 if (acl_other_entry->ae_perm & ACL_WRITE)
466 * Perform a syntactic check of the ACL, sufficient to allow an
467 * implementing file system to determine if it should accept this and
468 * rely on the POSIX.1e ACL properties.
471 acl_posix1e_check(struct acl *acl)
473 int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
474 int num_acl_mask, num_acl_other, i;
477 * Verify that the number of entries does not exceed the maximum
479 * Verify that the correct number of various sorts of ae_tags are
481 * Exactly one ACL_USER_OBJ
482 * Exactly one ACL_GROUP_OBJ
483 * Exactly one ACL_OTHER
484 * If any ACL_USER or ACL_GROUP entries appear, then exactly one
485 * ACL_MASK entry must also appear.
486 * Verify that all ae_perm entries are in ACL_PERM_BITS.
487 * Verify all ae_tag entries are understood by this implementation.
488 * Note: Does not check for uniqueness of qualifier (ae_id) field.
490 num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
491 num_acl_mask = num_acl_other = 0;
492 if (acl->acl_cnt > ACL_MAX_ENTRIES || acl->acl_cnt < 0)
494 for (i = 0; i < acl->acl_cnt; i++) {
496 * Check for a valid tag.
498 switch(acl->acl_entry[i].ae_tag) {
500 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
501 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
506 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
507 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
512 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
517 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
522 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
523 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
528 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
529 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
537 * Check for valid perm entries.
539 if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
543 if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
544 (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
546 if (((num_acl_group != 0) || (num_acl_user != 0)) &&
553 * These calls wrap the real vnode operations, and are called by the
554 * syscall code once the syscall has converted the path or file
555 * descriptor to a vnode (unlocked). The aclp pointer is assumed
556 * still to point to userland, so this should not be consumed within
557 * the kernel except by syscall code. Other code should directly
558 * invoke VOP_{SET,GET}ACL.
562 * Given a vnode, set its ACL.
565 vacl_set_acl(struct proc *p, struct vnode *vp, acl_type_t type,
568 struct acl inkernacl;
571 error = copyin(aclp, &inkernacl, sizeof(struct acl));
574 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
575 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
576 error = VOP_SETACL(vp, type, &inkernacl, p->p_ucred, p);
577 VOP_UNLOCK(vp, 0, p);
582 * Given a vnode, get its ACL.
585 vacl_get_acl(struct proc *p, struct vnode *vp, acl_type_t type,
588 struct acl inkernelacl;
591 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
592 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
593 error = VOP_GETACL(vp, type, &inkernelacl, p->p_ucred, p);
594 VOP_UNLOCK(vp, 0, p);
596 error = copyout(&inkernelacl, aclp, sizeof(struct acl));
601 * Given a vnode, delete its ACL.
604 vacl_delete(struct proc *p, struct vnode *vp, acl_type_t type)
608 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
609 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
610 error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, p->p_ucred, p);
611 VOP_UNLOCK(vp, 0, p);
616 * Given a vnode, check whether an ACL is appropriate for it
619 vacl_aclcheck(struct proc *p, struct vnode *vp, acl_type_t type,
622 struct acl inkernelacl;
625 error = copyin(aclp, &inkernelacl, sizeof(struct acl));
628 error = VOP_ACLCHECK(vp, type, &inkernelacl, p->p_ucred, p);
633 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
634 * Don't need to lock, as the vacl_ code will get/release any locks
639 * Given a file path, get an ACL for it
644 __acl_get_file(struct proc *p, struct __acl_get_file_args *uap)
650 /* what flags are required here -- possible not LOCKLEAF? */
651 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
654 error = vacl_get_acl(p, nd.ni_vp, SCARG(uap, type),
663 * Given a file path, set an ACL for it
668 __acl_set_file(struct proc *p, struct __acl_set_file_args *uap)
674 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
677 error = vacl_set_acl(p, nd.ni_vp, SCARG(uap, type),
686 * Given a file descriptor, get an ACL for it
691 __acl_get_fd(struct proc *p, struct __acl_get_fd_args *uap)
697 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
699 error = vacl_get_acl(p, (struct vnode *)fp->f_data,
700 SCARG(uap, type), SCARG(uap, aclp));
707 * Given a file descriptor, set an ACL for it
712 __acl_set_fd(struct proc *p, struct __acl_set_fd_args *uap)
718 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
720 error = vacl_set_acl(p, (struct vnode *)fp->f_data,
721 SCARG(uap, type), SCARG(uap, aclp));
728 * Given a file path, delete an ACL from it.
733 __acl_delete_file(struct proc *p, struct __acl_delete_file_args *uap)
739 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
742 error = vacl_delete(p, nd.ni_vp, SCARG(uap, type));
750 * Given a file path, delete an ACL from it.
755 __acl_delete_fd(struct proc *p, struct __acl_delete_fd_args *uap)
761 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
763 error = vacl_delete(p, (struct vnode *)fp->f_data,
771 * Given a file path, check an ACL for it
776 __acl_aclcheck_file(struct proc *p, struct __acl_aclcheck_file_args *uap)
782 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
785 error = vacl_aclcheck(p, nd.ni_vp, SCARG(uap, type),
794 * Given a file descriptor, check an ACL for it
799 __acl_aclcheck_fd(struct proc *p, struct __acl_aclcheck_fd_args *uap)
805 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
807 error = vacl_aclcheck(p, (struct vnode *)fp->f_data,
808 SCARG(uap, type), SCARG(uap, aclp));