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$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/vnode.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
52 #include <sys/filedesc.h>
54 #include <sys/sysent.h>
57 #include <security/mac/mac_framework.h>
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 * These calls wrap the real vnode operations, and are called by the syscall
71 * code once the syscall has converted the path or file descriptor to a vnode
72 * (unlocked). The aclp pointer is assumed still to point to userland, so
73 * this should not be consumed within the kernel except by syscall code.
74 * Other code should directly invoke VOP_{SET,GET}ACL.
78 * Given a vnode, set its ACL.
81 vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
84 struct acl *inkernelacl;
88 inkernelacl = acl_alloc(M_WAITOK);
89 error = copyin(aclp, inkernelacl, sizeof(struct acl));
92 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
95 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
97 error = mac_vnode_check_setacl(td->td_ucred, vp, type, inkernelacl);
101 error = VOP_SETACL(vp, type, inkernelacl, td->td_ucred, td);
106 vn_finished_write(mp);
108 acl_free(inkernelacl);
113 * Given a vnode, get its ACL.
116 vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
119 struct acl *inkernelacl;
122 inkernelacl = acl_alloc(M_WAITOK);
123 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
125 error = mac_vnode_check_getacl(td->td_ucred, vp, type);
129 error = VOP_GETACL(vp, type, inkernelacl, td->td_ucred, td);
135 error = copyout(inkernelacl, aclp, sizeof(struct acl));
136 acl_free(inkernelacl);
141 * Given a vnode, delete its ACL.
144 vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
149 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
152 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
154 error = mac_vnode_check_deleteacl(td->td_ucred, vp, type);
158 error = VOP_SETACL(vp, type, 0, td->td_ucred, td);
163 vn_finished_write(mp);
168 * Given a vnode, check whether an ACL is appropriate for it
171 vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
174 struct acl *inkernelacl;
177 inkernelacl = acl_alloc(M_WAITOK);
178 error = copyin(aclp, inkernelacl, sizeof(struct acl));
181 error = VOP_ACLCHECK(vp, type, inkernelacl, td->td_ucred, td);
183 acl_free(inkernelacl);
188 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever. Don't
189 * need to lock, as the vacl_ code will get/release any locks required.
193 * Given a file path, get an ACL for it
196 __acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
199 int vfslocked, error;
201 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
203 vfslocked = NDHASGIANT(&nd);
205 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
208 VFS_UNLOCK_GIANT(vfslocked);
213 * Given a file path, get an ACL for it; don't follow links.
216 __acl_get_link(struct thread *td, struct __acl_get_link_args *uap)
219 int vfslocked, error;
221 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
223 vfslocked = NDHASGIANT(&nd);
225 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
228 VFS_UNLOCK_GIANT(vfslocked);
233 * Given a file path, set an ACL for it.
236 __acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
239 int vfslocked, error;
241 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
243 vfslocked = NDHASGIANT(&nd);
245 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
248 VFS_UNLOCK_GIANT(vfslocked);
253 * Given a file path, set an ACL for it; don't follow links.
256 __acl_set_link(struct thread *td, struct __acl_set_link_args *uap)
259 int vfslocked, error;
261 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
263 vfslocked = NDHASGIANT(&nd);
265 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
268 VFS_UNLOCK_GIANT(vfslocked);
273 * Given a file descriptor, get an ACL for it.
276 __acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
279 int vfslocked, error;
281 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
283 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
284 error = vacl_get_acl(td, fp->f_vnode, uap->type, uap->aclp);
286 VFS_UNLOCK_GIANT(vfslocked);
292 * Given a file descriptor, set an ACL for it.
295 __acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
298 int vfslocked, error;
300 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
302 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
303 error = vacl_set_acl(td, fp->f_vnode, uap->type, uap->aclp);
305 VFS_UNLOCK_GIANT(vfslocked);
311 * Given a file path, delete an ACL from it.
314 __acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
317 int vfslocked, error;
319 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
321 vfslocked = NDHASGIANT(&nd);
323 error = vacl_delete(td, nd.ni_vp, uap->type);
326 VFS_UNLOCK_GIANT(vfslocked);
331 * Given a file path, delete an ACL from it; don't follow links.
334 __acl_delete_link(struct thread *td, struct __acl_delete_link_args *uap)
337 int vfslocked, error;
339 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
341 vfslocked = NDHASGIANT(&nd);
343 error = vacl_delete(td, nd.ni_vp, uap->type);
346 VFS_UNLOCK_GIANT(vfslocked);
351 * Given a file path, delete an ACL from it.
354 __acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
357 int vfslocked, error;
359 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
361 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
362 error = vacl_delete(td, fp->f_vnode, uap->type);
364 VFS_UNLOCK_GIANT(vfslocked);
370 * Given a file path, check an ACL for it.
373 __acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
376 int vfslocked, error;
378 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
380 vfslocked = NDHASGIANT(&nd);
382 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
385 VFS_UNLOCK_GIANT(vfslocked);
390 * Given a file path, check an ACL for it; don't follow links.
393 __acl_aclcheck_link(struct thread *td, struct __acl_aclcheck_link_args *uap)
396 int vfslocked, error;
398 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
400 vfslocked = NDHASGIANT(&nd);
402 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
405 VFS_UNLOCK_GIANT(vfslocked);
410 * Given a file descriptor, check an ACL for it.
413 __acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
416 int vfslocked, error;
418 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
420 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
421 error = vacl_aclcheck(td, fp->f_vnode, uap->type, uap->aclp);
423 VFS_UNLOCK_GIANT(vfslocked);
433 aclp = uma_zalloc(acl_zone, flags);
439 acl_free(struct acl *aclp)
442 uma_zfree(acl_zone, aclp);
448 aclinit(void *dummy __unused)
451 acl_zone = uma_zcreate("ACL UMA zone", sizeof(struct acl),
452 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
454 SYSINIT(acls, SI_SUB_ACL, SI_ORDER_FIRST, aclinit, NULL);