2 * Copyright (c) 1999-2006, 2016-2017 Robert N. M. Watson
5 * This software was developed by Robert Watson for the TrustedBSD Project.
7 * Portions of this software were developed by BAE Systems, the University of
8 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
9 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
10 * Computing (TC) research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Developed by the TrustedBSD Project.
36 * ACL system calls and other functions common across different ACL types.
37 * Type-specific routines go into subr_acl_<type>.c.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/capsicum.h>
47 #include <sys/fcntl.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
53 #include <sys/mutex.h>
54 #include <sys/namei.h>
56 #include <sys/filedesc.h>
58 #include <sys/sysent.h>
61 #include <security/audit/audit.h>
62 #include <security/mac/mac_framework.h>
64 CTASSERT(ACL_MAX_ENTRIES >= OLDACL_MAX_ENTRIES);
66 MALLOC_DEFINE(M_ACL, "acl", "Access Control Lists");
68 static int vacl_set_acl(struct thread *td, struct vnode *vp,
69 acl_type_t type, struct acl *aclp);
70 static int vacl_get_acl(struct thread *td, struct vnode *vp,
71 acl_type_t type, struct acl *aclp);
72 static int vacl_aclcheck(struct thread *td, struct vnode *vp,
73 acl_type_t type, struct acl *aclp);
76 acl_copy_oldacl_into_acl(const struct oldacl *source, struct acl *dest)
80 if (source->acl_cnt < 0 || source->acl_cnt > OLDACL_MAX_ENTRIES)
83 bzero(dest, sizeof(*dest));
85 dest->acl_cnt = source->acl_cnt;
86 dest->acl_maxcnt = ACL_MAX_ENTRIES;
88 for (i = 0; i < dest->acl_cnt; i++) {
89 dest->acl_entry[i].ae_tag = source->acl_entry[i].ae_tag;
90 dest->acl_entry[i].ae_id = source->acl_entry[i].ae_id;
91 dest->acl_entry[i].ae_perm = source->acl_entry[i].ae_perm;
98 acl_copy_acl_into_oldacl(const struct acl *source, struct oldacl *dest)
102 if (source->acl_cnt > OLDACL_MAX_ENTRIES)
105 bzero(dest, sizeof(*dest));
107 dest->acl_cnt = source->acl_cnt;
109 for (i = 0; i < dest->acl_cnt; i++) {
110 dest->acl_entry[i].ae_tag = source->acl_entry[i].ae_tag;
111 dest->acl_entry[i].ae_id = source->acl_entry[i].ae_id;
112 dest->acl_entry[i].ae_perm = source->acl_entry[i].ae_perm;
119 * At one time, "struct ACL" was extended in order to add support for NFSv4
120 * ACLs. Instead of creating compatibility versions of all the ACL-related
121 * syscalls, they were left intact. It's possible to find out what the code
122 * calling these syscalls (libc) expects basing on "type" argument - if it's
123 * either ACL_TYPE_ACCESS_OLD or ACL_TYPE_DEFAULT_OLD (which previously were
124 * known as ACL_TYPE_ACCESS and ACL_TYPE_DEFAULT), then it's the "struct
125 * oldacl". If it's something else, then it's the new "struct acl". In the
126 * latter case, the routines below just copyin/copyout the contents. In the
127 * former case, they copyin the "struct oldacl" and convert it to the new
131 acl_copyin(void *user_acl, struct acl *kernel_acl, acl_type_t type)
137 case ACL_TYPE_ACCESS_OLD:
138 case ACL_TYPE_DEFAULT_OLD:
139 error = copyin(user_acl, &old, sizeof(old));
142 acl_copy_oldacl_into_acl(&old, kernel_acl);
146 error = copyin(user_acl, kernel_acl, sizeof(*kernel_acl));
147 if (kernel_acl->acl_maxcnt != ACL_MAX_ENTRIES)
155 acl_copyout(struct acl *kernel_acl, void *user_acl, acl_type_t type)
162 case ACL_TYPE_ACCESS_OLD:
163 case ACL_TYPE_DEFAULT_OLD:
164 error = acl_copy_acl_into_oldacl(kernel_acl, &old);
168 error = copyout(&old, user_acl, sizeof(old));
172 error = fueword32((char *)user_acl +
173 offsetof(struct acl, acl_maxcnt), &am);
176 if (am != ACL_MAX_ENTRIES)
179 error = copyout(kernel_acl, user_acl, sizeof(*kernel_acl));
186 * Convert "old" type - ACL_TYPE_{ACCESS,DEFAULT}_OLD - into its "new"
187 * counterpart. It's required for old (pre-NFSv4 ACLs) libc to work
188 * with new kernel. Fixing 'type' for old binaries with new libc
189 * is being done in lib/libc/posix1e/acl_support.c:_acl_type_unold().
192 acl_type_unold(int type)
195 case ACL_TYPE_ACCESS_OLD:
196 return (ACL_TYPE_ACCESS);
198 case ACL_TYPE_DEFAULT_OLD:
199 return (ACL_TYPE_DEFAULT);
207 * These calls wrap the real vnode operations, and are called by the syscall
208 * code once the syscall has converted the path or file descriptor to a vnode
209 * (unlocked). The aclp pointer is assumed still to point to userland, so
210 * this should not be consumed within the kernel except by syscall code.
211 * Other code should directly invoke VOP_{SET,GET}ACL.
215 * Given a vnode, set its ACL.
218 vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
221 struct acl *inkernelacl;
225 AUDIT_ARG_VALUE(type);
226 inkernelacl = acl_alloc(M_WAITOK);
227 error = acl_copyin(aclp, inkernelacl, type);
230 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
233 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
234 AUDIT_ARG_VNODE1(vp);
236 error = mac_vnode_check_setacl(td->td_ucred, vp, type, inkernelacl);
240 error = VOP_SETACL(vp, acl_type_unold(type), inkernelacl,
246 vn_finished_write(mp);
248 acl_free(inkernelacl);
253 * Given a vnode, get its ACL.
256 vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
259 struct acl *inkernelacl;
262 AUDIT_ARG_VALUE(type);
263 inkernelacl = acl_alloc(M_WAITOK | M_ZERO);
264 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
265 AUDIT_ARG_VNODE1(vp);
267 error = mac_vnode_check_getacl(td->td_ucred, vp, type);
271 error = VOP_GETACL(vp, acl_type_unold(type), inkernelacl,
279 error = acl_copyout(inkernelacl, aclp, type);
280 acl_free(inkernelacl);
285 * Given a vnode, delete its ACL.
288 vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
293 AUDIT_ARG_VALUE(type);
294 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
297 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
298 AUDIT_ARG_VNODE1(vp);
300 error = mac_vnode_check_deleteacl(td->td_ucred, vp, type);
304 error = VOP_SETACL(vp, acl_type_unold(type), 0, td->td_ucred, td);
309 vn_finished_write(mp);
314 * Given a vnode, check whether an ACL is appropriate for it
316 * XXXRW: No vnode lock held so can't audit vnode state...?
319 vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
322 struct acl *inkernelacl;
325 inkernelacl = acl_alloc(M_WAITOK);
326 error = acl_copyin(aclp, inkernelacl, type);
329 error = VOP_ACLCHECK(vp, acl_type_unold(type), inkernelacl,
332 acl_free(inkernelacl);
337 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever. Don't
338 * need to lock, as the vacl_ code will get/release any locks required.
342 * Given a file path, get an ACL for it
345 sys___acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
350 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->path,
354 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
361 * Given a file path, get an ACL for it; don't follow links.
364 sys___acl_get_link(struct thread *td, struct __acl_get_link_args *uap)
369 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->path,
373 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
380 * Given a file path, set an ACL for it.
383 sys___acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
388 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->path,
392 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
399 * Given a file path, set an ACL for it; don't follow links.
402 sys___acl_set_link(struct thread *td, struct __acl_set_link_args *uap)
407 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->path,
411 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
418 * Given a file descriptor, get an ACL for it.
421 sys___acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
427 AUDIT_ARG_FD(uap->filedes);
428 error = getvnode(td, uap->filedes,
429 cap_rights_init(&rights, CAP_ACL_GET), &fp);
431 error = vacl_get_acl(td, fp->f_vnode, uap->type, uap->aclp);
438 * Given a file descriptor, set an ACL for it.
441 sys___acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
447 AUDIT_ARG_FD(uap->filedes);
448 error = getvnode(td, uap->filedes,
449 cap_rights_init(&rights, CAP_ACL_SET), &fp);
451 error = vacl_set_acl(td, fp->f_vnode, uap->type, uap->aclp);
458 * Given a file path, delete an ACL from it.
461 sys___acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
466 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
469 error = vacl_delete(td, nd.ni_vp, uap->type);
476 * Given a file path, delete an ACL from it; don't follow links.
479 sys___acl_delete_link(struct thread *td, struct __acl_delete_link_args *uap)
484 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
487 error = vacl_delete(td, nd.ni_vp, uap->type);
494 * Given a file path, delete an ACL from it.
497 sys___acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
503 AUDIT_ARG_FD(uap->filedes);
504 error = getvnode(td, uap->filedes,
505 cap_rights_init(&rights, CAP_ACL_DELETE), &fp);
507 error = vacl_delete(td, fp->f_vnode, uap->type);
514 * Given a file path, check an ACL for it.
517 sys___acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
522 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
525 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
532 * Given a file path, check an ACL for it; don't follow links.
535 sys___acl_aclcheck_link(struct thread *td, struct __acl_aclcheck_link_args *uap)
540 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
543 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
550 * Given a file descriptor, check an ACL for it.
553 sys___acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
559 AUDIT_ARG_FD(uap->filedes);
560 error = getvnode(td, uap->filedes,
561 cap_rights_init(&rights, CAP_ACL_CHECK), &fp);
563 error = vacl_aclcheck(td, fp->f_vnode, uap->type, uap->aclp);
574 aclp = malloc(sizeof(*aclp), M_ACL, flags);
578 aclp->acl_maxcnt = ACL_MAX_ENTRIES;
584 acl_free(struct acl *aclp)