2 * Copyright (c) 1999-2006 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
29 * Developed by the TrustedBSD Project.
31 * ACL system calls and other functions common across different ACL types.
32 * Type-specific routines go into subr_acl_<type>.c.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/capsicum.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
51 #include <sys/filedesc.h>
53 #include <sys/sysent.h>
56 #include <security/mac/mac_framework.h>
58 CTASSERT(ACL_MAX_ENTRIES >= OLDACL_MAX_ENTRIES);
60 MALLOC_DEFINE(M_ACL, "acl", "Access Control Lists");
62 static int vacl_set_acl(struct thread *td, struct vnode *vp,
63 acl_type_t type, struct acl *aclp);
64 static int vacl_get_acl(struct thread *td, struct vnode *vp,
65 acl_type_t type, struct acl *aclp);
66 static int vacl_aclcheck(struct thread *td, struct vnode *vp,
67 acl_type_t type, struct acl *aclp);
70 acl_copy_oldacl_into_acl(const struct oldacl *source, struct acl *dest)
74 if (source->acl_cnt < 0 || source->acl_cnt > OLDACL_MAX_ENTRIES)
77 bzero(dest, sizeof(*dest));
79 dest->acl_cnt = source->acl_cnt;
80 dest->acl_maxcnt = ACL_MAX_ENTRIES;
82 for (i = 0; i < dest->acl_cnt; i++) {
83 dest->acl_entry[i].ae_tag = source->acl_entry[i].ae_tag;
84 dest->acl_entry[i].ae_id = source->acl_entry[i].ae_id;
85 dest->acl_entry[i].ae_perm = source->acl_entry[i].ae_perm;
92 acl_copy_acl_into_oldacl(const struct acl *source, struct oldacl *dest)
96 if (source->acl_cnt > OLDACL_MAX_ENTRIES)
99 bzero(dest, sizeof(*dest));
101 dest->acl_cnt = source->acl_cnt;
103 for (i = 0; i < dest->acl_cnt; i++) {
104 dest->acl_entry[i].ae_tag = source->acl_entry[i].ae_tag;
105 dest->acl_entry[i].ae_id = source->acl_entry[i].ae_id;
106 dest->acl_entry[i].ae_perm = source->acl_entry[i].ae_perm;
113 * At one time, "struct ACL" was extended in order to add support for NFSv4
114 * ACLs. Instead of creating compatibility versions of all the ACL-related
115 * syscalls, they were left intact. It's possible to find out what the code
116 * calling these syscalls (libc) expects basing on "type" argument - if it's
117 * either ACL_TYPE_ACCESS_OLD or ACL_TYPE_DEFAULT_OLD (which previously were
118 * known as ACL_TYPE_ACCESS and ACL_TYPE_DEFAULT), then it's the "struct
119 * oldacl". If it's something else, then it's the new "struct acl". In the
120 * latter case, the routines below just copyin/copyout the contents. In the
121 * former case, they copyin the "struct oldacl" and convert it to the new
125 acl_copyin(void *user_acl, struct acl *kernel_acl, acl_type_t type)
131 case ACL_TYPE_ACCESS_OLD:
132 case ACL_TYPE_DEFAULT_OLD:
133 error = copyin(user_acl, &old, sizeof(old));
136 acl_copy_oldacl_into_acl(&old, kernel_acl);
140 error = copyin(user_acl, kernel_acl, sizeof(*kernel_acl));
141 if (kernel_acl->acl_maxcnt != ACL_MAX_ENTRIES)
149 acl_copyout(struct acl *kernel_acl, void *user_acl, acl_type_t type)
156 case ACL_TYPE_ACCESS_OLD:
157 case ACL_TYPE_DEFAULT_OLD:
158 error = acl_copy_acl_into_oldacl(kernel_acl, &old);
162 error = copyout(&old, user_acl, sizeof(old));
166 error = fueword32((char *)user_acl +
167 offsetof(struct acl, acl_maxcnt), &am);
170 if (am != ACL_MAX_ENTRIES)
173 error = copyout(kernel_acl, user_acl, sizeof(*kernel_acl));
180 * Convert "old" type - ACL_TYPE_{ACCESS,DEFAULT}_OLD - into its "new"
181 * counterpart. It's required for old (pre-NFSv4 ACLs) libc to work
182 * with new kernel. Fixing 'type' for old binaries with new libc
183 * is being done in lib/libc/posix1e/acl_support.c:_acl_type_unold().
186 acl_type_unold(int type)
189 case ACL_TYPE_ACCESS_OLD:
190 return (ACL_TYPE_ACCESS);
192 case ACL_TYPE_DEFAULT_OLD:
193 return (ACL_TYPE_DEFAULT);
201 * These calls wrap the real vnode operations, and are called by the syscall
202 * code once the syscall has converted the path or file descriptor to a vnode
203 * (unlocked). The aclp pointer is assumed still to point to userland, so
204 * this should not be consumed within the kernel except by syscall code.
205 * Other code should directly invoke VOP_{SET,GET}ACL.
209 * Given a vnode, set its ACL.
212 vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
215 struct acl *inkernelacl;
219 inkernelacl = acl_alloc(M_WAITOK);
220 error = acl_copyin(aclp, inkernelacl, type);
223 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
226 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
228 error = mac_vnode_check_setacl(td->td_ucred, vp, type, inkernelacl);
232 error = VOP_SETACL(vp, acl_type_unold(type), inkernelacl,
238 vn_finished_write(mp);
240 acl_free(inkernelacl);
245 * Given a vnode, get its ACL.
248 vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
251 struct acl *inkernelacl;
254 inkernelacl = acl_alloc(M_WAITOK | M_ZERO);
255 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
257 error = mac_vnode_check_getacl(td->td_ucred, vp, type);
261 error = VOP_GETACL(vp, acl_type_unold(type), inkernelacl,
269 error = acl_copyout(inkernelacl, aclp, type);
270 acl_free(inkernelacl);
275 * Given a vnode, delete its ACL.
278 vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
283 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
286 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
288 error = mac_vnode_check_deleteacl(td->td_ucred, vp, type);
292 error = VOP_SETACL(vp, acl_type_unold(type), 0, td->td_ucred, td);
297 vn_finished_write(mp);
302 * Given a vnode, check whether an ACL is appropriate for it
305 vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
308 struct acl *inkernelacl;
311 inkernelacl = acl_alloc(M_WAITOK);
312 error = acl_copyin(aclp, inkernelacl, type);
315 error = VOP_ACLCHECK(vp, acl_type_unold(type), inkernelacl,
318 acl_free(inkernelacl);
323 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever. Don't
324 * need to lock, as the vacl_ code will get/release any locks required.
328 * Given a file path, get an ACL for it
331 sys___acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
336 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
339 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
346 * Given a file path, get an ACL for it; don't follow links.
349 sys___acl_get_link(struct thread *td, struct __acl_get_link_args *uap)
354 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
357 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
364 * Given a file path, set an ACL for it.
367 sys___acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
372 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
375 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
382 * Given a file path, set an ACL for it; don't follow links.
385 sys___acl_set_link(struct thread *td, struct __acl_set_link_args *uap)
390 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
393 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
400 * Given a file descriptor, get an ACL for it.
403 sys___acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
409 error = getvnode(td->td_proc->p_fd, uap->filedes,
410 cap_rights_init(&rights, CAP_ACL_GET), &fp);
412 error = vacl_get_acl(td, fp->f_vnode, uap->type, uap->aclp);
419 * Given a file descriptor, set an ACL for it.
422 sys___acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
428 error = getvnode(td->td_proc->p_fd, uap->filedes,
429 cap_rights_init(&rights, CAP_ACL_SET), &fp);
431 error = vacl_set_acl(td, fp->f_vnode, uap->type, uap->aclp);
438 * Given a file path, delete an ACL from it.
441 sys___acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
446 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
449 error = vacl_delete(td, nd.ni_vp, uap->type);
456 * Given a file path, delete an ACL from it; don't follow links.
459 sys___acl_delete_link(struct thread *td, struct __acl_delete_link_args *uap)
464 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
467 error = vacl_delete(td, nd.ni_vp, uap->type);
474 * Given a file path, delete an ACL from it.
477 sys___acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
483 error = getvnode(td->td_proc->p_fd, uap->filedes,
484 cap_rights_init(&rights, CAP_ACL_DELETE), &fp);
486 error = vacl_delete(td, fp->f_vnode, uap->type);
493 * Given a file path, check an ACL for it.
496 sys___acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
501 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
504 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
511 * Given a file path, check an ACL for it; don't follow links.
514 sys___acl_aclcheck_link(struct thread *td, struct __acl_aclcheck_link_args *uap)
519 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
522 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
529 * Given a file descriptor, check an ACL for it.
532 sys___acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
538 error = getvnode(td->td_proc->p_fd, uap->filedes,
539 cap_rights_init(&rights, CAP_ACL_CHECK), &fp);
541 error = vacl_aclcheck(td, fp->f_vnode, uap->type, uap->aclp);
552 aclp = malloc(sizeof(*aclp), M_ACL, flags);
556 aclp->acl_maxcnt = ACL_MAX_ENTRIES;
562 acl_free(struct acl *aclp)