2 * Copyright (c) 1999-2001 Robert N. M. Watson
5 * This software was developed by Robert Watson for the TrustedBSD Project.
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
31 * Developed by the TrustedBSD Project.
32 * Support for POSIX.1e access control lists.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/vnode.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
48 #include <sys/sysent.h>
49 #include <sys/errno.h>
53 MALLOC_DEFINE(M_ACL, "acl", "access control list");
55 static int vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
57 static int vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
59 static int vacl_aclcheck(struct thread *td, struct vnode *vp,
60 acl_type_t type, struct acl *aclp);
63 * Implement a version of vaccess() that understands POSIX.1e ACL semantics.
64 * Return 0 on success, else an errno value. Should be merged into
65 * vaccess() eventually.
68 vaccess_acl_posix1e(enum vtype type, uid_t file_uid, gid_t file_gid,
69 struct acl *acl, mode_t acc_mode, struct ucred *cred, int *privused)
71 struct acl_entry *acl_other, *acl_mask;
74 mode_t acl_mask_granted;
78 * Look for a normal, non-privileged way to access the file/directory
79 * as requested. If it exists, go with that. Otherwise, attempt
80 * to use privileges granted via cap_granted. In some cases,
81 * which privileges to use may be ambiguous due to "best match",
82 * in which case fall back on first match for the time being.
88 * Determine privileges now, but don't apply until we've found
89 * a DAC entry that matches but has failed to allow access.
92 if (suser_xxx(cred, NULL, PRISON_ROOT) == 0)
93 cap_granted = (VEXEC | VREAD | VWRITE | VADMIN);
100 if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
101 CAP_DAC_READ_SEARCH, PRISON_ROOT))
102 cap_granted |= VEXEC;
104 if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
105 CAP_DAC_EXECUTE, PRISON_ROOT))
106 cap_granted |= VEXEC;
109 if ((acc_mode & VREAD) && !cap_check(cred, NULL, CAP_DAC_READ_SEARCH,
111 cap_granted |= VREAD;
113 if ((acc_mode & VWRITE) && !cap_check(cred, NULL, CAP_DAC_WRITE,
115 cap_granted |= VWRITE;
117 if ((acc_mode & VADMIN) && !cap_check(cred, NULL, CAP_FOWNER,
119 cap_granted |= VADMIN;
120 #endif /* CAPABILITIES */
123 * The owner matches if the effective uid associated with the
124 * credential matches that of the ACL_USER_OBJ entry. While we're
125 * doing the first scan, also cache the location of the ACL_MASK
126 * and ACL_OTHER entries, preventing some future iterations.
128 acl_mask = acl_other = NULL;
129 for (i = 0; i < acl->acl_cnt; i++) {
130 switch (acl->acl_entry[i].ae_tag) {
132 if (file_uid != cred->cr_uid)
135 dac_granted |= VADMIN;
136 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
137 dac_granted |= VEXEC;
138 if (acl->acl_entry[i].ae_perm & ACL_READ)
139 dac_granted |= VREAD;
140 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
141 dac_granted |= VWRITE;
142 if ((acc_mode & dac_granted) == acc_mode)
144 if ((acc_mode & (dac_granted | cap_granted)) ==
146 if (privused != NULL)
153 acl_mask = &acl->acl_entry[i];
157 acl_other = &acl->acl_entry[i];
166 * An ACL_OTHER entry should always exist in a valid access
167 * ACL. If it doesn't, then generate a serious failure. For now,
168 * this means a debugging message and EPERM, but in the future
169 * should probably be a panic.
171 if (acl_other == NULL) {
173 * XXX This should never happen
175 printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
180 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields
181 * are masked by an ACL_MASK entry, if any. As such, first identify
182 * the ACL_MASK field, then iterate through identifying potential
183 * user matches, then group matches. If there is no ACL_MASK,
184 * assume that the mask allows all requests to succeed.
186 if (acl_mask != NULL) {
187 acl_mask_granted = 0;
188 if (acl_mask->ae_perm & ACL_EXECUTE)
189 acl_mask_granted |= VEXEC;
190 if (acl_mask->ae_perm & ACL_READ)
191 acl_mask_granted |= VREAD;
192 if (acl_mask->ae_perm & ACL_WRITE)
193 acl_mask_granted |= VWRITE;
195 acl_mask_granted = VEXEC | VREAD | VWRITE;
198 * Iterate through user ACL entries. Do checks twice, first
199 * without privilege, and then if a match is found but failed,
200 * a second time with privilege.
204 * Check ACL_USER ACL entries.
206 for (i = 0; i < acl->acl_cnt; i++) {
207 switch (acl->acl_entry[i].ae_tag) {
209 if (acl->acl_entry[i].ae_id != cred->cr_uid)
212 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
213 dac_granted |= VEXEC;
214 if (acl->acl_entry[i].ae_perm & ACL_READ)
215 dac_granted |= VREAD;
216 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
217 dac_granted |= VWRITE;
218 dac_granted &= acl_mask_granted;
219 if ((acc_mode & dac_granted) == acc_mode)
221 if ((acc_mode & (dac_granted | cap_granted)) !=
225 if (privused != NULL)
232 * Group match is best-match, not first-match, so find a
233 * "best" match. Iterate across, testing each potential group
234 * match. Make sure we keep track of whether we found a match
235 * or not, so that we know if we should try again with any
236 * available privilege, or if we should move on to ACL_OTHER.
239 for (i = 0; i < acl->acl_cnt; i++) {
240 switch (acl->acl_entry[i].ae_tag) {
242 if (!groupmember(file_gid, cred))
245 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
246 dac_granted |= VEXEC;
247 if (acl->acl_entry[i].ae_perm & ACL_READ)
248 dac_granted |= VREAD;
249 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
250 dac_granted |= VWRITE;
251 dac_granted &= acl_mask_granted;
253 if ((acc_mode & dac_granted) == acc_mode)
260 if (!groupmember(acl->acl_entry[i].ae_id, cred))
263 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
264 dac_granted |= VEXEC;
265 if (acl->acl_entry[i].ae_perm & ACL_READ)
266 dac_granted |= VREAD;
267 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
268 dac_granted |= VWRITE;
269 dac_granted &= acl_mask_granted;
271 if ((acc_mode & dac_granted) == acc_mode)
282 if (group_matched == 1) {
284 * There was a match, but it did not grant rights via
285 * pure DAC. Try again, this time with privilege.
287 for (i = 0; i < acl->acl_cnt; i++) {
288 switch (acl->acl_entry[i].ae_tag) {
290 if (!groupmember(file_gid, cred))
293 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
294 dac_granted |= VEXEC;
295 if (acl->acl_entry[i].ae_perm & ACL_READ)
296 dac_granted |= VREAD;
297 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
298 dac_granted |= VWRITE;
299 dac_granted &= acl_mask_granted;
301 if ((acc_mode & (dac_granted | cap_granted)) !=
305 if (privused != NULL)
310 if (!groupmember(acl->acl_entry[i].ae_id,
314 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
315 dac_granted |= VEXEC;
316 if (acl->acl_entry[i].ae_perm & ACL_READ)
317 dac_granted |= VREAD;
318 if (acl->acl_entry[i].ae_perm & ACL_WRITE)
319 dac_granted |= VWRITE;
320 dac_granted &= acl_mask_granted;
322 if ((acc_mode & (dac_granted | cap_granted)) !=
326 if (privused != NULL)
335 * Even with privilege, group membership was not sufficient.
342 * Fall back on ACL_OTHER. ACL_MASK is not applied to ACL_OTHER.
345 if (acl_other->ae_perm & ACL_EXECUTE)
346 dac_granted |= VEXEC;
347 if (acl_other->ae_perm & ACL_READ)
348 dac_granted |= VREAD;
349 if (acl_other->ae_perm & ACL_WRITE)
350 dac_granted |= VWRITE;
352 if ((acc_mode & dac_granted) == acc_mode)
354 if ((acc_mode & (dac_granted | cap_granted)) == acc_mode) {
355 if (privused != NULL)
361 return ((acc_mode & VADMIN) ? EPERM : EACCES);
365 * For the purposes of file systems maintaining the _OBJ entries in an
366 * inode with a mode_t field, this routine converts a mode_t entry
370 acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
403 printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag);
409 * Given inode information (uid, gid, mode), return an acl entry of the
413 acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
415 struct acl_entry acl_entry;
417 acl_entry.ae_tag = tag;
418 acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
421 acl_entry.ae_id = uid;
425 acl_entry.ae_id = gid;
429 acl_entry.ae_id = ACL_UNDEFINED_ID;
433 acl_entry.ae_id = ACL_UNDEFINED_ID;
434 printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
441 * Utility function to generate a file mode given appropriate ACL entries.
444 acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
445 struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
450 if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
452 if (acl_user_obj_entry->ae_perm & ACL_READ)
454 if (acl_user_obj_entry->ae_perm & ACL_WRITE)
456 if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
458 if (acl_group_obj_entry->ae_perm & ACL_READ)
460 if (acl_group_obj_entry->ae_perm & ACL_WRITE)
462 if (acl_other_entry->ae_perm & ACL_EXECUTE)
464 if (acl_other_entry->ae_perm & ACL_READ)
466 if (acl_other_entry->ae_perm & ACL_WRITE)
473 * Perform a syntactic check of the ACL, sufficient to allow an
474 * implementing file system to determine if it should accept this and
475 * rely on the POSIX.1e ACL properties.
478 acl_posix1e_check(struct acl *acl)
480 int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
481 int num_acl_mask, num_acl_other, i;
484 * Verify that the number of entries does not exceed the maximum
486 * Verify that the correct number of various sorts of ae_tags are
488 * Exactly one ACL_USER_OBJ
489 * Exactly one ACL_GROUP_OBJ
490 * Exactly one ACL_OTHER
491 * If any ACL_USER or ACL_GROUP entries appear, then exactly one
492 * ACL_MASK entry must also appear.
493 * Verify that all ae_perm entries are in ACL_PERM_BITS.
494 * Verify all ae_tag entries are understood by this implementation.
495 * Note: Does not check for uniqueness of qualifier (ae_id) field.
497 num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
498 num_acl_mask = num_acl_other = 0;
499 if (acl->acl_cnt > ACL_MAX_ENTRIES || acl->acl_cnt < 0)
501 for (i = 0; i < acl->acl_cnt; i++) {
503 * Check for a valid tag.
505 switch(acl->acl_entry[i].ae_tag) {
507 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
508 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
513 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
514 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
519 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
524 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
529 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
530 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
535 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
536 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
544 * Check for valid perm entries.
546 if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
550 if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
551 (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
553 if (((num_acl_group != 0) || (num_acl_user != 0)) &&
560 * These calls wrap the real vnode operations, and are called by the
561 * syscall code once the syscall has converted the path or file
562 * descriptor to a vnode (unlocked). The aclp pointer is assumed
563 * still to point to userland, so this should not be consumed within
564 * the kernel except by syscall code. Other code should directly
565 * invoke VOP_{SET,GET}ACL.
569 * Given a vnode, set its ACL.
572 vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
575 struct acl inkernacl;
579 error = copyin(aclp, &inkernacl, sizeof(struct acl));
582 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
585 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
586 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
587 error = VOP_SETACL(vp, type, &inkernacl, td->td_ucred, td);
588 VOP_UNLOCK(vp, 0, td);
589 vn_finished_write(mp);
594 * Given a vnode, get its ACL.
597 vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
600 struct acl inkernelacl;
603 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
604 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
605 error = VOP_GETACL(vp, type, &inkernelacl, td->td_ucred, td);
606 VOP_UNLOCK(vp, 0, td);
608 error = copyout(&inkernelacl, aclp, sizeof(struct acl));
613 * Given a vnode, delete its ACL.
616 vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
621 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
624 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
625 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
626 error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, td->td_ucred, td);
627 VOP_UNLOCK(vp, 0, td);
628 vn_finished_write(mp);
633 * Given a vnode, check whether an ACL is appropriate for it
636 vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
639 struct acl inkernelacl;
642 error = copyin(aclp, &inkernelacl, sizeof(struct acl));
645 error = VOP_ACLCHECK(vp, type, &inkernelacl, td->td_ucred, td);
650 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
651 * Don't need to lock, as the vacl_ code will get/release any locks
656 * Given a file path, get an ACL for it
661 __acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
667 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
670 error = vacl_get_acl(td, nd.ni_vp, SCARG(uap, type),
679 * Given a file path, set an ACL for it
684 __acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
690 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
693 error = vacl_set_acl(td, nd.ni_vp, SCARG(uap, type),
702 * Given a file descriptor, get an ACL for it
707 __acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
713 error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
715 error = vacl_get_acl(td, (struct vnode *)fp->f_data,
716 SCARG(uap, type), SCARG(uap, aclp));
724 * Given a file descriptor, set an ACL for it
729 __acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
735 error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
737 error = vacl_set_acl(td, (struct vnode *)fp->f_data,
738 SCARG(uap, type), SCARG(uap, aclp));
746 * Given a file path, delete an ACL from it.
751 __acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
757 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
760 error = vacl_delete(td, nd.ni_vp, SCARG(uap, type));
768 * Given a file path, delete an ACL from it.
773 __acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
779 error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
781 error = vacl_delete(td, (struct vnode *)fp->f_data,
790 * Given a file path, check an ACL for it
795 __acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
801 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
804 error = vacl_aclcheck(td, nd.ni_vp, SCARG(uap, type),
813 * Given a file descriptor, check an ACL for it
818 __acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
824 error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
826 error = vacl_aclcheck(td, (struct vnode *)fp->f_data,
827 SCARG(uap, type), SCARG(uap, aclp));