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/fcntl.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
47 #include <sys/mutex.h>
48 #include <sys/namei.h>
50 #include <sys/filedesc.h>
52 #include <sys/sysent.h>
55 #include <security/mac/mac_framework.h>
57 CTASSERT(ACL_MAX_ENTRIES >= OLDACL_MAX_ENTRIES);
59 MALLOC_DEFINE(M_ACL, "acl", "Access Control Lists");
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 acl_copy_oldacl_into_acl(const struct oldacl *source, struct acl *dest)
73 if (source->acl_cnt < 0 || source->acl_cnt > OLDACL_MAX_ENTRIES)
76 bzero(dest, sizeof(*dest));
78 dest->acl_cnt = source->acl_cnt;
79 dest->acl_maxcnt = ACL_MAX_ENTRIES;
81 for (i = 0; i < dest->acl_cnt; i++) {
82 dest->acl_entry[i].ae_tag = source->acl_entry[i].ae_tag;
83 dest->acl_entry[i].ae_id = source->acl_entry[i].ae_id;
84 dest->acl_entry[i].ae_perm = source->acl_entry[i].ae_perm;
91 acl_copy_acl_into_oldacl(const struct acl *source, struct oldacl *dest)
95 if (source->acl_cnt < 0 || source->acl_cnt > OLDACL_MAX_ENTRIES)
98 bzero(dest, sizeof(*dest));
100 dest->acl_cnt = source->acl_cnt;
102 for (i = 0; i < dest->acl_cnt; i++) {
103 dest->acl_entry[i].ae_tag = source->acl_entry[i].ae_tag;
104 dest->acl_entry[i].ae_id = source->acl_entry[i].ae_id;
105 dest->acl_entry[i].ae_perm = source->acl_entry[i].ae_perm;
112 * At one time, "struct ACL" was extended in order to add support for NFSv4
113 * ACLs. Instead of creating compatibility versions of all the ACL-related
114 * syscalls, they were left intact. It's possible to find out what the code
115 * calling these syscalls (libc) expects basing on "type" argument - if it's
116 * either ACL_TYPE_ACCESS_OLD or ACL_TYPE_DEFAULT_OLD (which previously were
117 * known as ACL_TYPE_ACCESS and ACL_TYPE_DEFAULT), then it's the "struct
118 * oldacl". If it's something else, then it's the new "struct acl". In the
119 * latter case, the routines below just copyin/copyout the contents. In the
120 * former case, they copyin the "struct oldacl" and convert it to the new
124 acl_copyin(void *user_acl, struct acl *kernel_acl, acl_type_t type)
130 case ACL_TYPE_ACCESS_OLD:
131 case ACL_TYPE_DEFAULT_OLD:
132 error = copyin(user_acl, &old, sizeof(old));
135 acl_copy_oldacl_into_acl(&old, kernel_acl);
139 error = copyin(user_acl, kernel_acl, sizeof(*kernel_acl));
140 if (kernel_acl->acl_maxcnt != ACL_MAX_ENTRIES)
148 acl_copyout(struct acl *kernel_acl, void *user_acl, acl_type_t type)
154 case ACL_TYPE_ACCESS_OLD:
155 case ACL_TYPE_DEFAULT_OLD:
156 error = acl_copy_acl_into_oldacl(kernel_acl, &old);
160 error = copyout(&old, user_acl, sizeof(old));
164 if (fuword32((char *)user_acl +
165 offsetof(struct acl, acl_maxcnt)) != ACL_MAX_ENTRIES)
168 error = copyout(kernel_acl, user_acl, sizeof(*kernel_acl));
175 * Convert "old" type - ACL_TYPE_{ACCESS,DEFAULT}_OLD - into its "new"
176 * counterpart. It's required for old (pre-NFSv4 ACLs) libc to work
177 * with new kernel. Fixing 'type' for old binaries with new libc
178 * is being done in lib/libc/posix1e/acl_support.c:_acl_type_unold().
181 acl_type_unold(int type)
184 case ACL_TYPE_ACCESS_OLD:
185 return (ACL_TYPE_ACCESS);
187 case ACL_TYPE_DEFAULT_OLD:
188 return (ACL_TYPE_DEFAULT);
196 * These calls wrap the real vnode operations, and are called by the syscall
197 * code once the syscall has converted the path or file descriptor to a vnode
198 * (unlocked). The aclp pointer is assumed still to point to userland, so
199 * this should not be consumed within the kernel except by syscall code.
200 * Other code should directly invoke VOP_{SET,GET}ACL.
204 * Given a vnode, set its ACL.
207 vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
210 struct acl *inkernelacl;
214 inkernelacl = acl_alloc(M_WAITOK);
215 error = acl_copyin(aclp, inkernelacl, type);
218 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
221 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
223 error = mac_vnode_check_setacl(td->td_ucred, vp, type, inkernelacl);
227 error = VOP_SETACL(vp, acl_type_unold(type), inkernelacl,
233 vn_finished_write(mp);
235 acl_free(inkernelacl);
240 * Given a vnode, get its ACL.
243 vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
246 struct acl *inkernelacl;
249 inkernelacl = acl_alloc(M_WAITOK | M_ZERO);
250 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
252 error = mac_vnode_check_getacl(td->td_ucred, vp, type);
256 error = VOP_GETACL(vp, acl_type_unold(type), inkernelacl,
264 error = acl_copyout(inkernelacl, aclp, type);
265 acl_free(inkernelacl);
270 * Given a vnode, delete its ACL.
273 vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
278 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
281 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
283 error = mac_vnode_check_deleteacl(td->td_ucred, vp, type);
287 error = VOP_SETACL(vp, acl_type_unold(type), 0, td->td_ucred, td);
292 vn_finished_write(mp);
297 * Given a vnode, check whether an ACL is appropriate for it
300 vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
303 struct acl *inkernelacl;
306 inkernelacl = acl_alloc(M_WAITOK);
307 error = acl_copyin(aclp, inkernelacl, type);
310 error = VOP_ACLCHECK(vp, acl_type_unold(type), inkernelacl,
313 acl_free(inkernelacl);
318 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever. Don't
319 * need to lock, as the vacl_ code will get/release any locks required.
323 * Given a file path, get an ACL for it
326 __acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
329 int vfslocked, error;
331 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
333 vfslocked = NDHASGIANT(&nd);
335 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
338 VFS_UNLOCK_GIANT(vfslocked);
343 * Given a file path, get an ACL for it; don't follow links.
346 __acl_get_link(struct thread *td, struct __acl_get_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_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
358 VFS_UNLOCK_GIANT(vfslocked);
363 * Given a file path, set an ACL for it.
366 __acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
369 int vfslocked, error;
371 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
373 vfslocked = NDHASGIANT(&nd);
375 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
378 VFS_UNLOCK_GIANT(vfslocked);
383 * Given a file path, set an ACL for it; don't follow links.
386 __acl_set_link(struct thread *td, struct __acl_set_link_args *uap)
389 int vfslocked, error;
391 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
393 vfslocked = NDHASGIANT(&nd);
395 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
398 VFS_UNLOCK_GIANT(vfslocked);
403 * Given a file descriptor, get an ACL for it.
406 __acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
409 int vfslocked, error;
411 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
413 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
414 error = vacl_get_acl(td, fp->f_vnode, uap->type, uap->aclp);
416 VFS_UNLOCK_GIANT(vfslocked);
422 * Given a file descriptor, set an ACL for it.
425 __acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
428 int vfslocked, error;
430 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
432 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
433 error = vacl_set_acl(td, fp->f_vnode, uap->type, uap->aclp);
435 VFS_UNLOCK_GIANT(vfslocked);
441 * Given a file path, delete an ACL from it.
444 __acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
447 int vfslocked, error;
449 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
451 vfslocked = NDHASGIANT(&nd);
453 error = vacl_delete(td, nd.ni_vp, uap->type);
456 VFS_UNLOCK_GIANT(vfslocked);
461 * Given a file path, delete an ACL from it; don't follow links.
464 __acl_delete_link(struct thread *td, struct __acl_delete_link_args *uap)
467 int vfslocked, error;
469 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
471 vfslocked = NDHASGIANT(&nd);
473 error = vacl_delete(td, nd.ni_vp, uap->type);
476 VFS_UNLOCK_GIANT(vfslocked);
481 * Given a file path, delete an ACL from it.
484 __acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
487 int vfslocked, error;
489 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
491 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
492 error = vacl_delete(td, fp->f_vnode, uap->type);
494 VFS_UNLOCK_GIANT(vfslocked);
500 * Given a file path, check an ACL for it.
503 __acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
506 int vfslocked, error;
508 NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
510 vfslocked = NDHASGIANT(&nd);
512 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
515 VFS_UNLOCK_GIANT(vfslocked);
520 * Given a file path, check an ACL for it; don't follow links.
523 __acl_aclcheck_link(struct thread *td, struct __acl_aclcheck_link_args *uap)
526 int vfslocked, error;
528 NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
530 vfslocked = NDHASGIANT(&nd);
532 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
535 VFS_UNLOCK_GIANT(vfslocked);
540 * Given a file descriptor, check an ACL for it.
543 __acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
546 int vfslocked, error;
548 error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
550 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
551 error = vacl_aclcheck(td, fp->f_vnode, uap->type, uap->aclp);
553 VFS_UNLOCK_GIANT(vfslocked);
563 aclp = malloc(sizeof(*aclp), M_ACL, flags);
564 aclp->acl_maxcnt = ACL_MAX_ENTRIES;
570 acl_free(struct acl *aclp)