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/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>
61 static int vacl_set_acl(struct thread *td, struct vnode *vp,
62 acl_type_t type, struct acl *aclp);
63 static int vacl_get_acl(struct thread *td, struct vnode *vp,
64 acl_type_t type, struct acl *aclp);
65 static int vacl_aclcheck(struct thread *td, struct vnode *vp,
66 acl_type_t type, struct acl *aclp);
69 * These calls wrap the real vnode operations, and are called by the
70 * syscall code once the syscall has converted the path or file
71 * descriptor to a vnode (unlocked). The aclp pointer is assumed
72 * still to point to userland, so this should not be consumed within
73 * the kernel except by syscall code. Other code should directly
74 * 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,
88 error = copyin(aclp, &inkernacl, sizeof(struct acl));
91 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
94 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
95 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
97 error = mac_check_vnode_setacl(td->td_ucred, vp, type, &inkernacl);
101 error = VOP_SETACL(vp, type, &inkernacl, td->td_ucred, td);
105 VOP_UNLOCK(vp, 0, td);
106 vn_finished_write(mp);
111 * Given a vnode, get its ACL.
114 vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
117 struct acl inkernelacl;
120 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
121 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
123 error = mac_check_vnode_getacl(td->td_ucred, vp, type);
127 error = VOP_GETACL(vp, type, &inkernelacl, td->td_ucred, td);
131 VOP_UNLOCK(vp, 0, td);
133 error = copyout(&inkernelacl, aclp, sizeof(struct acl));
138 * Given a vnode, delete its ACL.
141 vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
146 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
149 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
150 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
152 error = mac_check_vnode_deleteacl(td->td_ucred, vp, type);
156 error = VOP_SETACL(vp, type, 0, td->td_ucred, td);
160 VOP_UNLOCK(vp, 0, td);
161 vn_finished_write(mp);
166 * Given a vnode, check whether an ACL is appropriate for it
169 vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
172 struct acl inkernelacl;
175 error = copyin(aclp, &inkernelacl, sizeof(struct acl));
178 error = VOP_ACLCHECK(vp, type, &inkernelacl, td->td_ucred, td);
183 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
184 * Don't need to lock, as the vacl_ code will get/release any locks
189 * Given a file path, get an ACL for it
194 __acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
197 int vfslocked, error;
199 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
201 vfslocked = NDHASGIANT(&nd);
203 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
206 VFS_UNLOCK_GIANT(vfslocked);
211 * 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
238 __acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
241 int vfslocked, error;
243 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
245 vfslocked = NDHASGIANT(&nd);
247 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
250 VFS_UNLOCK_GIANT(vfslocked);
255 * Given a file path, set an ACL for it; don't follow links.
260 __acl_set_link(struct thread *td, struct __acl_set_link_args *uap)
263 int vfslocked, error;
265 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
267 vfslocked = NDHASGIANT(&nd);
269 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
272 VFS_UNLOCK_GIANT(vfslocked);
277 * Given a file descriptor, get an ACL for it
282 __acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
285 int vfslocked, error;
287 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
289 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
290 error = vacl_get_acl(td, fp->f_vnode, uap->type, uap->aclp);
292 VFS_UNLOCK_GIANT(vfslocked);
298 * Given a file descriptor, set an ACL for it
303 __acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
306 int vfslocked, error;
308 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
310 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
311 error = vacl_set_acl(td, fp->f_vnode, uap->type, uap->aclp);
313 VFS_UNLOCK_GIANT(vfslocked);
319 * Given a file path, delete an ACL from it.
324 __acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
327 int vfslocked, error;
329 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
331 vfslocked = NDHASGIANT(&nd);
333 error = vacl_delete(td, nd.ni_vp, uap->type);
336 VFS_UNLOCK_GIANT(vfslocked);
341 * Given a file path, delete an ACL from it; don't follow links.
346 __acl_delete_link(struct thread *td, struct __acl_delete_link_args *uap)
349 int vfslocked, error;
351 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
353 vfslocked = NDHASGIANT(&nd);
355 error = vacl_delete(td, nd.ni_vp, uap->type);
358 VFS_UNLOCK_GIANT(vfslocked);
363 * Given a file path, delete an ACL from it.
368 __acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
371 int vfslocked, error;
373 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
375 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
376 error = vacl_delete(td, fp->f_vnode, uap->type);
378 VFS_UNLOCK_GIANT(vfslocked);
384 * Given a file path, check an ACL for it
389 __acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
392 int vfslocked, error;
394 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
396 vfslocked = NDHASGIANT(&nd);
398 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
401 VFS_UNLOCK_GIANT(vfslocked);
406 * Given a file path, check an ACL for it; don't follow links.
411 __acl_aclcheck_link(struct thread *td, struct __acl_aclcheck_link_args *uap)
414 int vfslocked, error;
416 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
418 vfslocked = NDHASGIANT(&nd);
420 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
423 VFS_UNLOCK_GIANT(vfslocked);
428 * Given a file descriptor, check an ACL for it
433 __acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
436 int vfslocked, error;
438 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
440 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
441 error = vacl_aclcheck(td, fp->f_vnode, uap->type, uap->aclp);
443 VFS_UNLOCK_GIANT(vfslocked);
451 aclinit(void *dummy __unused)
454 acl_zone = uma_zcreate("ACL UMA zone", sizeof(struct acl),
455 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
457 SYSINIT(acls, SI_SUB_ACL, SI_ORDER_FIRST, aclinit, NULL)