xref: /freebsd/sys/kern/vfs_acl.c (revision d1dfd921774f005fd7f28558bacd6836ea5297f5)
191f37dcbSRobert Watson /*-
2f6a41092SRobert Watson  * Copyright (c) 1999-2003 Robert N. M. Watson
391f37dcbSRobert Watson  * All rights reserved.
491f37dcbSRobert Watson  *
56d878543SRobert Watson  * This software was developed by Robert Watson for the TrustedBSD Project.
66d878543SRobert Watson  *
791f37dcbSRobert Watson  * Redistribution and use in source and binary forms, with or without
891f37dcbSRobert Watson  * modification, are permitted provided that the following conditions
991f37dcbSRobert Watson  * are met:
1091f37dcbSRobert Watson  * 1. Redistributions of source code must retain the above copyright
1191f37dcbSRobert Watson  *    notice, this list of conditions and the following disclaimer.
1291f37dcbSRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1391f37dcbSRobert Watson  *    notice, this list of conditions and the following disclaimer in the
1491f37dcbSRobert Watson  *    documentation and/or other materials provided with the distribution.
1591f37dcbSRobert Watson  *
1691f37dcbSRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1791f37dcbSRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1891f37dcbSRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1991f37dcbSRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2091f37dcbSRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2191f37dcbSRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2291f37dcbSRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2391f37dcbSRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2491f37dcbSRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2591f37dcbSRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2691f37dcbSRobert Watson  * SUCH DAMAGE.
2791f37dcbSRobert Watson  */
2891f37dcbSRobert Watson /*
295293465fSRobert Watson  * Developed by the TrustedBSD Project.
305293465fSRobert Watson  * Support for POSIX.1e access control lists.
3191f37dcbSRobert Watson  */
3291f37dcbSRobert Watson 
33677b542eSDavid E. O'Brien #include <sys/cdefs.h>
34677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
35677b542eSDavid E. O'Brien 
36c86ca022SRobert Watson #include "opt_mac.h"
37c86ca022SRobert Watson 
3891f37dcbSRobert Watson #include <sys/param.h>
3991f37dcbSRobert Watson #include <sys/systm.h>
4091f37dcbSRobert Watson #include <sys/sysproto.h>
4191f37dcbSRobert Watson #include <sys/kernel.h>
42c86ca022SRobert Watson #include <sys/mac.h>
4391f37dcbSRobert Watson #include <sys/malloc.h>
4491f37dcbSRobert Watson #include <sys/vnode.h>
4591f37dcbSRobert Watson #include <sys/lock.h>
46f708f4d1SMatthew Dillon #include <sys/mutex.h>
4791f37dcbSRobert Watson #include <sys/namei.h>
4891f37dcbSRobert Watson #include <sys/file.h>
4913438f68SAlfred Perlstein #include <sys/filedesc.h>
5091f37dcbSRobert Watson #include <sys/proc.h>
5191f37dcbSRobert Watson #include <sys/sysent.h>
5291f37dcbSRobert Watson #include <sys/errno.h>
5391f37dcbSRobert Watson #include <sys/stat.h>
5491f37dcbSRobert Watson #include <sys/acl.h>
5591f37dcbSRobert Watson 
56d1dfd921SChristian S.J. Peron #include <vm/uma.h>
5791f37dcbSRobert Watson 
58d1dfd921SChristian S.J. Peron uma_zone_t	acl_zone;
59cbeb8402SRobert Watson static int	vacl_set_acl(struct thread *td, struct vnode *vp,
60cbeb8402SRobert Watson 		    acl_type_t type, struct acl *aclp);
61cbeb8402SRobert Watson static int	vacl_get_acl(struct thread *td, struct vnode *vp,
62cbeb8402SRobert Watson 		    acl_type_t type, struct acl *aclp);
63b40ce416SJulian Elischer static int	vacl_aclcheck(struct thread *td, struct vnode *vp,
64b114e127SRobert Watson 		    acl_type_t type, struct acl *aclp);
6591f37dcbSRobert Watson 
6691f37dcbSRobert Watson /*
675293465fSRobert Watson  * Implement a version of vaccess() that understands POSIX.1e ACL semantics.
685293465fSRobert Watson  * Return 0 on success, else an errno value.  Should be merged into
695293465fSRobert Watson  * vaccess() eventually.
705293465fSRobert Watson  */
715293465fSRobert Watson int
72b114e127SRobert Watson vaccess_acl_posix1e(enum vtype type, uid_t file_uid, gid_t file_gid,
73b114e127SRobert Watson     struct acl *acl, mode_t acc_mode, struct ucred *cred, int *privused)
745293465fSRobert Watson {
755293465fSRobert Watson 	struct acl_entry *acl_other, *acl_mask;
765293465fSRobert Watson 	mode_t dac_granted;
775293465fSRobert Watson 	mode_t cap_granted;
785293465fSRobert Watson 	mode_t acl_mask_granted;
795293465fSRobert Watson 	int group_matched, i;
805293465fSRobert Watson 
815293465fSRobert Watson 	/*
825293465fSRobert Watson 	 * Look for a normal, non-privileged way to access the file/directory
835293465fSRobert Watson 	 * as requested.  If it exists, go with that.  Otherwise, attempt
845293465fSRobert Watson 	 * to use privileges granted via cap_granted.  In some cases,
855293465fSRobert Watson 	 * which privileges to use may be ambiguous due to "best match",
865293465fSRobert Watson 	 * in which case fall back on first match for the time being.
875293465fSRobert Watson 	 */
885293465fSRobert Watson 	if (privused != NULL)
895293465fSRobert Watson 		*privused = 0;
905293465fSRobert Watson 
915293465fSRobert Watson 	/*
925293465fSRobert Watson 	 * Determine privileges now, but don't apply until we've found
93670f6b2fSRobert Watson 	 * a DAC entry that matches but has failed to allow access.
945293465fSRobert Watson 	 */
955293465fSRobert Watson #ifndef CAPABILITIES
9656f21b9dSColin Percival 	if (suser_cred(cred, SUSER_ALLOWJAIL) == 0)
97b02aac46SRobert Watson 		cap_granted = VALLPERM;
985293465fSRobert Watson 	else
995293465fSRobert Watson 		cap_granted = 0;
1005293465fSRobert Watson #else
1015293465fSRobert Watson 	cap_granted = 0;
1025293465fSRobert Watson 
1035293465fSRobert Watson 	if (type == VDIR) {
1045293465fSRobert Watson 		if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
10556f21b9dSColin Percival 		     CAP_DAC_READ_SEARCH, SUSER_ALLOWJAIL))
1065293465fSRobert Watson 			cap_granted |= VEXEC;
1075293465fSRobert Watson 	} else {
1085293465fSRobert Watson 		if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
10956f21b9dSColin Percival 		    CAP_DAC_EXECUTE, SUSER_ALLOWJAIL))
1105293465fSRobert Watson 			cap_granted |= VEXEC;
1115293465fSRobert Watson 	}
1125293465fSRobert Watson 
1135293465fSRobert Watson 	if ((acc_mode & VREAD) && !cap_check(cred, NULL, CAP_DAC_READ_SEARCH,
11456f21b9dSColin Percival 	    SUSER_ALLOWJAIL))
1155293465fSRobert Watson 		cap_granted |= VREAD;
1165293465fSRobert Watson 
117b02aac46SRobert Watson 	if (((acc_mode & VWRITE) || (acc_mode & VAPPEND)) &&
11856f21b9dSColin Percival 	    !cap_check(cred, NULL, CAP_DAC_WRITE, SUSER_ALLOWJAIL))
119b02aac46SRobert Watson 		cap_granted |= (VWRITE | VAPPEND);
1205293465fSRobert Watson 
1215293465fSRobert Watson 	if ((acc_mode & VADMIN) && !cap_check(cred, NULL, CAP_FOWNER,
12256f21b9dSColin Percival 	    SUSER_ALLOWJAIL))
1235293465fSRobert Watson 		cap_granted |= VADMIN;
1245293465fSRobert Watson #endif /* CAPABILITIES */
1255293465fSRobert Watson 
1265293465fSRobert Watson 	/*
127670f6b2fSRobert Watson 	 * The owner matches if the effective uid associated with the
128670f6b2fSRobert Watson 	 * credential matches that of the ACL_USER_OBJ entry.  While we're
129670f6b2fSRobert Watson 	 * doing the first scan, also cache the location of the ACL_MASK
130670f6b2fSRobert Watson 	 * and ACL_OTHER entries, preventing some future iterations.
1315293465fSRobert Watson 	 */
1325293465fSRobert Watson 	acl_mask = acl_other = NULL;
1335293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
1345293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
1355293465fSRobert Watson 		case ACL_USER_OBJ:
136b114e127SRobert Watson 			if (file_uid != cred->cr_uid)
1375293465fSRobert Watson 				break;
1385293465fSRobert Watson 			dac_granted = 0;
1395293465fSRobert Watson 			dac_granted |= VADMIN;
140fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
1415293465fSRobert Watson 				dac_granted |= VEXEC;
142fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
1435293465fSRobert Watson 				dac_granted |= VREAD;
144fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
145b02aac46SRobert Watson 				dac_granted |= (VWRITE | VAPPEND);
1465293465fSRobert Watson 			if ((acc_mode & dac_granted) == acc_mode)
1475293465fSRobert Watson 				return (0);
1485293465fSRobert Watson 			if ((acc_mode & (dac_granted | cap_granted)) ==
1495293465fSRobert Watson 			    acc_mode) {
1505293465fSRobert Watson 				if (privused != NULL)
1515293465fSRobert Watson 					*privused = 1;
1525293465fSRobert Watson 				return (0);
1535293465fSRobert Watson 			}
1545293465fSRobert Watson 			goto error;
1555293465fSRobert Watson 
1565293465fSRobert Watson 		case ACL_MASK:
1575293465fSRobert Watson 			acl_mask = &acl->acl_entry[i];
1585293465fSRobert Watson 			break;
1595293465fSRobert Watson 
1605293465fSRobert Watson 		case ACL_OTHER:
1615293465fSRobert Watson 			acl_other = &acl->acl_entry[i];
1625293465fSRobert Watson 			break;
1635293465fSRobert Watson 
1645293465fSRobert Watson 		default:
16530171114SPeter Wemm 			break;
1665293465fSRobert Watson 		}
1675293465fSRobert Watson 	}
1685293465fSRobert Watson 
1695293465fSRobert Watson 	/*
170670f6b2fSRobert Watson 	 * An ACL_OTHER entry should always exist in a valid access
171670f6b2fSRobert Watson 	 * ACL.  If it doesn't, then generate a serious failure.  For now,
172670f6b2fSRobert Watson 	 * this means a debugging message and EPERM, but in the future
173670f6b2fSRobert Watson 	 * should probably be a panic.
174670f6b2fSRobert Watson 	 */
175670f6b2fSRobert Watson 	if (acl_other == NULL) {
176670f6b2fSRobert Watson 		/*
177670f6b2fSRobert Watson 		 * XXX This should never happen
178670f6b2fSRobert Watson 		 */
179670f6b2fSRobert Watson 		printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
180670f6b2fSRobert Watson 		return (EPERM);
181670f6b2fSRobert Watson 	}
182670f6b2fSRobert Watson 
183670f6b2fSRobert Watson 	/*
1845293465fSRobert Watson 	 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields
1855293465fSRobert Watson 	 * are masked by an ACL_MASK entry, if any.  As such, first identify
1865293465fSRobert Watson 	 * the ACL_MASK field, then iterate through identifying potential
1875293465fSRobert Watson 	 * user matches, then group matches.  If there is no ACL_MASK,
1885293465fSRobert Watson 	 * assume that the mask allows all requests to succeed.
1895293465fSRobert Watson 	 */
1905293465fSRobert Watson 	if (acl_mask != NULL) {
1915293465fSRobert Watson 		acl_mask_granted = 0;
192fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_EXECUTE)
1935293465fSRobert Watson 			acl_mask_granted |= VEXEC;
194fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_READ)
1955293465fSRobert Watson 			acl_mask_granted |= VREAD;
196fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_WRITE)
197b02aac46SRobert Watson 			acl_mask_granted |= (VWRITE | VAPPEND);
1985293465fSRobert Watson 	} else
199b02aac46SRobert Watson 		acl_mask_granted = VEXEC | VREAD | VWRITE | VAPPEND;
2005293465fSRobert Watson 
2015293465fSRobert Watson 	/*
202670f6b2fSRobert Watson 	 * Iterate through user ACL entries.  Do checks twice, first
203670f6b2fSRobert Watson 	 * without privilege, and then if a match is found but failed,
204670f6b2fSRobert Watson 	 * a second time with privilege.
2055293465fSRobert Watson 	 */
2065293465fSRobert Watson 
2075293465fSRobert Watson 	/*
2085293465fSRobert Watson 	 * Check ACL_USER ACL entries.
2095293465fSRobert Watson 	 */
2105293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
2115293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
2125293465fSRobert Watson 		case ACL_USER:
2135293465fSRobert Watson 			if (acl->acl_entry[i].ae_id != cred->cr_uid)
2145293465fSRobert Watson 				break;
2155293465fSRobert Watson 			dac_granted = 0;
216fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2175293465fSRobert Watson 				dac_granted |= VEXEC;
218fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
2195293465fSRobert Watson 				dac_granted |= VREAD;
220fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
221b02aac46SRobert Watson 				dac_granted |= (VWRITE | VAPPEND);
2225293465fSRobert Watson 			dac_granted &= acl_mask_granted;
2235293465fSRobert Watson 			if ((acc_mode & dac_granted) == acc_mode)
2245293465fSRobert Watson 				return (0);
225b114e127SRobert Watson 			if ((acc_mode & (dac_granted | cap_granted)) !=
226b114e127SRobert Watson 			    acc_mode)
227b114e127SRobert Watson 				goto error;
228b114e127SRobert Watson 
2295293465fSRobert Watson 			if (privused != NULL)
2305293465fSRobert Watson 				*privused = 1;
2315293465fSRobert Watson 			return (0);
2325293465fSRobert Watson 		}
2335293465fSRobert Watson 	}
2345293465fSRobert Watson 
2355293465fSRobert Watson 	/*
2365293465fSRobert Watson 	 * Group match is best-match, not first-match, so find a
2375293465fSRobert Watson 	 * "best" match.  Iterate across, testing each potential group
2385293465fSRobert Watson 	 * match.  Make sure we keep track of whether we found a match
239670f6b2fSRobert Watson 	 * or not, so that we know if we should try again with any
240670f6b2fSRobert Watson 	 * available privilege, or if we should move on to ACL_OTHER.
2415293465fSRobert Watson 	 */
2425293465fSRobert Watson 	group_matched = 0;
2435293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
2445293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
2455293465fSRobert Watson 		case ACL_GROUP_OBJ:
246e15480f8SThomas Moestl 			if (!groupmember(file_gid, cred))
247b114e127SRobert Watson 				break;
2485293465fSRobert Watson 			dac_granted = 0;
249fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2505293465fSRobert Watson 				dac_granted |= VEXEC;
251fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
2525293465fSRobert Watson 				dac_granted |= VREAD;
253fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
254b02aac46SRobert Watson 				dac_granted |= (VWRITE | VAPPEND);
2555293465fSRobert Watson 			dac_granted  &= acl_mask_granted;
2565293465fSRobert Watson 
2575293465fSRobert Watson 			if ((acc_mode & dac_granted) == acc_mode)
2585293465fSRobert Watson 				return (0);
2595293465fSRobert Watson 
2605293465fSRobert Watson 			group_matched = 1;
261b114e127SRobert Watson 			break;
262b114e127SRobert Watson 
263b114e127SRobert Watson 		case ACL_GROUP:
264b114e127SRobert Watson 			if (!groupmember(acl->acl_entry[i].ae_id, cred))
265b114e127SRobert Watson 				break;
266b114e127SRobert Watson 			dac_granted = 0;
267b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
268b114e127SRobert Watson 				dac_granted |= VEXEC;
269b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_READ)
270b114e127SRobert Watson 				dac_granted |= VREAD;
271b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
272b02aac46SRobert Watson 				dac_granted |= (VWRITE | VAPPEND);
273b114e127SRobert Watson 			dac_granted  &= acl_mask_granted;
274b114e127SRobert Watson 
275b114e127SRobert Watson 			if ((acc_mode & dac_granted) == acc_mode)
276b114e127SRobert Watson 				return (0);
277b114e127SRobert Watson 
278b114e127SRobert Watson 			group_matched = 1;
279b114e127SRobert Watson 			break;
280b114e127SRobert Watson 
2815293465fSRobert Watson 		default:
28230171114SPeter Wemm 			break;
2835293465fSRobert Watson 		}
2845293465fSRobert Watson 	}
2855293465fSRobert Watson 
2865293465fSRobert Watson 	if (group_matched == 1) {
2875293465fSRobert Watson 		/*
2885293465fSRobert Watson 		 * There was a match, but it did not grant rights via
2895293465fSRobert Watson 		 * pure DAC.  Try again, this time with privilege.
2905293465fSRobert Watson 		 */
2915293465fSRobert Watson 		for (i = 0; i < acl->acl_cnt; i++) {
2925293465fSRobert Watson 			switch (acl->acl_entry[i].ae_tag) {
2935293465fSRobert Watson 			case ACL_GROUP_OBJ:
29446157a65SRobert Watson 				if (!groupmember(file_gid, cred))
295b114e127SRobert Watson 					break;
2965293465fSRobert Watson 				dac_granted = 0;
297b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2985293465fSRobert Watson 					dac_granted |= VEXEC;
299b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_READ)
3005293465fSRobert Watson 					dac_granted |= VREAD;
301b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_WRITE)
302b02aac46SRobert Watson 					dac_granted |= (VWRITE | VAPPEND);
3035293465fSRobert Watson 				dac_granted &= acl_mask_granted;
304b114e127SRobert Watson 
305b114e127SRobert Watson 				if ((acc_mode & (dac_granted | cap_granted)) !=
306b114e127SRobert Watson 				    acc_mode)
307b114e127SRobert Watson 					break;
308b114e127SRobert Watson 
3095293465fSRobert Watson 				if (privused != NULL)
3105293465fSRobert Watson 					*privused = 1;
3115293465fSRobert Watson 				return (0);
312b114e127SRobert Watson 
313b114e127SRobert Watson 			case ACL_GROUP:
314b114e127SRobert Watson 				if (!groupmember(acl->acl_entry[i].ae_id,
315b114e127SRobert Watson 				    cred))
316b114e127SRobert Watson 					break;
317b114e127SRobert Watson 				dac_granted = 0;
318b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
319b114e127SRobert Watson 				dac_granted |= VEXEC;
320b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_READ)
321b114e127SRobert Watson 					dac_granted |= VREAD;
322b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_WRITE)
323b02aac46SRobert Watson 					dac_granted |= (VWRITE | VAPPEND);
324b114e127SRobert Watson 				dac_granted &= acl_mask_granted;
325b114e127SRobert Watson 
326b114e127SRobert Watson 				if ((acc_mode & (dac_granted | cap_granted)) !=
327b114e127SRobert Watson 				    acc_mode)
328b114e127SRobert Watson 					break;
329b114e127SRobert Watson 
330b114e127SRobert Watson 				if (privused != NULL)
331b114e127SRobert Watson 					*privused = 1;
332b114e127SRobert Watson 				return (0);
333b114e127SRobert Watson 
3345293465fSRobert Watson 			default:
33530171114SPeter Wemm 				break;
3365293465fSRobert Watson 			}
3375293465fSRobert Watson 		}
3385293465fSRobert Watson 		/*
3395293465fSRobert Watson 		 * Even with privilege, group membership was not sufficient.
3405293465fSRobert Watson 		 * Return failure.
3415293465fSRobert Watson 		 */
3425293465fSRobert Watson 		goto error;
3435293465fSRobert Watson 	}
3445293465fSRobert Watson 
3455293465fSRobert Watson 	/*
3465293465fSRobert Watson 	 * Fall back on ACL_OTHER.  ACL_MASK is not applied to ACL_OTHER.
3475293465fSRobert Watson 	 */
3485293465fSRobert Watson 	dac_granted = 0;
349fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_EXECUTE)
3505293465fSRobert Watson 		dac_granted |= VEXEC;
351fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_READ)
3525293465fSRobert Watson 		dac_granted |= VREAD;
353fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_WRITE)
354b02aac46SRobert Watson 		dac_granted |= (VWRITE | VAPPEND);
3555293465fSRobert Watson 
3565293465fSRobert Watson 	if ((acc_mode & dac_granted) == acc_mode)
3575293465fSRobert Watson 		return (0);
3585293465fSRobert Watson 	if ((acc_mode & (dac_granted | cap_granted)) == acc_mode) {
3595293465fSRobert Watson 		if (privused != NULL)
3605293465fSRobert Watson 			*privused = 1;
3615293465fSRobert Watson 		return (0);
3625293465fSRobert Watson 	}
3635293465fSRobert Watson 
3645293465fSRobert Watson error:
3655293465fSRobert Watson 	return ((acc_mode & VADMIN) ? EPERM : EACCES);
3665293465fSRobert Watson }
3675293465fSRobert Watson 
3685293465fSRobert Watson /*
3695293465fSRobert Watson  * For the purposes of filesystems maintaining the _OBJ entries in an
3705293465fSRobert Watson  * inode with a mode_t field, this routine converts a mode_t entry
3715293465fSRobert Watson  * to an acl_perm_t.
3725293465fSRobert Watson  */
3735293465fSRobert Watson acl_perm_t
3745293465fSRobert Watson acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
3755293465fSRobert Watson {
3765293465fSRobert Watson 	acl_perm_t	perm = 0;
3775293465fSRobert Watson 
3785293465fSRobert Watson 	switch(tag) {
3795293465fSRobert Watson 	case ACL_USER_OBJ:
3805293465fSRobert Watson 		if (mode & S_IXUSR)
381fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
3825293465fSRobert Watson 		if (mode & S_IRUSR)
383fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
3845293465fSRobert Watson 		if (mode & S_IWUSR)
385fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
3865293465fSRobert Watson 		return (perm);
3875293465fSRobert Watson 
3885293465fSRobert Watson 	case ACL_GROUP_OBJ:
3895293465fSRobert Watson 		if (mode & S_IXGRP)
390fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
3915293465fSRobert Watson 		if (mode & S_IRGRP)
392fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
3935293465fSRobert Watson 		if (mode & S_IWGRP)
394fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
3955293465fSRobert Watson 		return (perm);
3965293465fSRobert Watson 
3975293465fSRobert Watson 	case ACL_OTHER:
3985293465fSRobert Watson 		if (mode & S_IXOTH)
399fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
4005293465fSRobert Watson 		if (mode & S_IROTH)
401fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
4025293465fSRobert Watson 		if (mode & S_IWOTH)
403fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
4045293465fSRobert Watson 		return (perm);
4055293465fSRobert Watson 
4065293465fSRobert Watson 	default:
4075293465fSRobert Watson 		printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag);
4085293465fSRobert Watson 		return (0);
4095293465fSRobert Watson 	}
4105293465fSRobert Watson }
4115293465fSRobert Watson 
4125293465fSRobert Watson /*
4135293465fSRobert Watson  * Given inode information (uid, gid, mode), return an acl entry of the
4145293465fSRobert Watson  * appropriate type.
4155293465fSRobert Watson  */
4165293465fSRobert Watson struct acl_entry
4175293465fSRobert Watson acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
4185293465fSRobert Watson {
4195293465fSRobert Watson 	struct acl_entry	acl_entry;
4205293465fSRobert Watson 
4215293465fSRobert Watson 	acl_entry.ae_tag = tag;
4225293465fSRobert Watson 	acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
4235293465fSRobert Watson 	switch(tag) {
4245293465fSRobert Watson 	case ACL_USER_OBJ:
4255293465fSRobert Watson 		acl_entry.ae_id = uid;
4265293465fSRobert Watson 		break;
4275293465fSRobert Watson 
4285293465fSRobert Watson 	case ACL_GROUP_OBJ:
4295293465fSRobert Watson 		acl_entry.ae_id = gid;
4305293465fSRobert Watson 		break;
4315293465fSRobert Watson 
4325293465fSRobert Watson 	case ACL_OTHER:
433dbb14f98SChris D. Faulhaber 		acl_entry.ae_id = ACL_UNDEFINED_ID;
4345293465fSRobert Watson 		break;
4355293465fSRobert Watson 
4365293465fSRobert Watson 	default:
437dbb14f98SChris D. Faulhaber 		acl_entry.ae_id = ACL_UNDEFINED_ID;
4385293465fSRobert Watson 		printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
4395293465fSRobert Watson 	}
4405293465fSRobert Watson 
4415293465fSRobert Watson 	return (acl_entry);
4425293465fSRobert Watson }
4435293465fSRobert Watson 
4445293465fSRobert Watson /*
4455293465fSRobert Watson  * Utility function to generate a file mode given appropriate ACL entries.
4465293465fSRobert Watson  */
4475293465fSRobert Watson mode_t
4485293465fSRobert Watson acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
4495293465fSRobert Watson     struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
4505293465fSRobert Watson {
4515293465fSRobert Watson 	mode_t	mode;
4525293465fSRobert Watson 
4535293465fSRobert Watson 	mode = 0;
454fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
4555293465fSRobert Watson 		mode |= S_IXUSR;
456fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_READ)
4575293465fSRobert Watson 		mode |= S_IRUSR;
458fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_WRITE)
4595293465fSRobert Watson 		mode |= S_IWUSR;
460fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
4615293465fSRobert Watson 		mode |= S_IXGRP;
462fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_READ)
4635293465fSRobert Watson 		mode |= S_IRGRP;
464fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_WRITE)
4655293465fSRobert Watson 		mode |= S_IWGRP;
466fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_EXECUTE)
4675293465fSRobert Watson 		mode |= S_IXOTH;
468fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_READ)
4695293465fSRobert Watson 		mode |= S_IROTH;
470fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_WRITE)
4715293465fSRobert Watson 		mode |= S_IWOTH;
4725293465fSRobert Watson 
4735293465fSRobert Watson 	return (mode);
4745293465fSRobert Watson }
4755293465fSRobert Watson 
4765293465fSRobert Watson /*
47760bdc14eSRobert Watson  * Utility function to generate a file mode given a complete POSIX.1e
47860bdc14eSRobert Watson  * access ACL.  Note that if the ACL is improperly formed, this may
47960bdc14eSRobert Watson  * result in a panic.
48060bdc14eSRobert Watson  */
48160bdc14eSRobert Watson mode_t
48260bdc14eSRobert Watson acl_posix1e_acl_to_mode(struct acl *acl)
48360bdc14eSRobert Watson {
48460bdc14eSRobert Watson 	struct acl_entry *acl_mask, *acl_user_obj, *acl_group_obj, *acl_other;
48560bdc14eSRobert Watson 	int i;
48660bdc14eSRobert Watson 
48760bdc14eSRobert Watson 	/*
48860bdc14eSRobert Watson 	 * Find the ACL entries relevant to a POSIX permission mode.
48960bdc14eSRobert Watson 	 */
49060bdc14eSRobert Watson 	acl_user_obj = acl_group_obj = acl_other = acl_mask = NULL;
49160bdc14eSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
49260bdc14eSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
49360bdc14eSRobert Watson 		case ACL_USER_OBJ:
49460bdc14eSRobert Watson 			acl_user_obj = &acl->acl_entry[i];
49560bdc14eSRobert Watson 			break;
49660bdc14eSRobert Watson 
49760bdc14eSRobert Watson 		case ACL_GROUP_OBJ:
49860bdc14eSRobert Watson 			acl_group_obj = &acl->acl_entry[i];
49960bdc14eSRobert Watson 			break;
50060bdc14eSRobert Watson 
50160bdc14eSRobert Watson 		case ACL_OTHER:
50260bdc14eSRobert Watson 			acl_other = &acl->acl_entry[i];
50360bdc14eSRobert Watson 			break;
50460bdc14eSRobert Watson 
50560bdc14eSRobert Watson 		case ACL_MASK:
50660bdc14eSRobert Watson 			acl_mask = &acl->acl_entry[i];
50760bdc14eSRobert Watson 			break;
50860bdc14eSRobert Watson 
50960bdc14eSRobert Watson 		case ACL_USER:
51060bdc14eSRobert Watson 		case ACL_GROUP:
51160bdc14eSRobert Watson 			break;
51260bdc14eSRobert Watson 
51360bdc14eSRobert Watson 		default:
51460bdc14eSRobert Watson 			panic("acl_posix1e_acl_to_mode: bad ae_tag");
51560bdc14eSRobert Watson 		}
51660bdc14eSRobert Watson 	}
51760bdc14eSRobert Watson 
51860bdc14eSRobert Watson 	if (acl_user_obj == NULL || acl_group_obj == NULL || acl_other == NULL)
51960bdc14eSRobert Watson 		panic("acl_posix1e_acl_to_mode: missing base ae_tags");
52060bdc14eSRobert Watson 
52160bdc14eSRobert Watson 	/*
52260bdc14eSRobert Watson 	 * POSIX.1e specifies that if there is an ACL_MASK entry, we replace
52360bdc14eSRobert Watson 	 * the mode "group" bits with its permissions.  If there isn't, we
52460bdc14eSRobert Watson 	 * use the ACL_GROUP_OBJ permissions.
52560bdc14eSRobert Watson 	 */
52660bdc14eSRobert Watson 	if (acl_mask != NULL)
52760bdc14eSRobert Watson 		return (acl_posix1e_perms_to_mode(acl_user_obj, acl_mask,
52860bdc14eSRobert Watson 		    acl_other));
52960bdc14eSRobert Watson 	else
53060bdc14eSRobert Watson 		return (acl_posix1e_perms_to_mode(acl_user_obj, acl_group_obj,
53160bdc14eSRobert Watson 		    acl_other));
53260bdc14eSRobert Watson }
53360bdc14eSRobert Watson 
53460bdc14eSRobert Watson /*
5355293465fSRobert Watson  * Perform a syntactic check of the ACL, sufficient to allow an
5365293465fSRobert Watson  * implementing filesystem to determine if it should accept this and
5375293465fSRobert Watson  * rely on the POSIX.1e ACL properties.
5385293465fSRobert Watson  */
5395293465fSRobert Watson int
5405293465fSRobert Watson acl_posix1e_check(struct acl *acl)
5415293465fSRobert Watson {
5425293465fSRobert Watson 	int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
5435293465fSRobert Watson 	int num_acl_mask, num_acl_other, i;
5445293465fSRobert Watson 
5455293465fSRobert Watson 	/*
5465293465fSRobert Watson 	 * Verify that the number of entries does not exceed the maximum
5475293465fSRobert Watson 	 * defined for acl_t.
5485293465fSRobert Watson 	 * Verify that the correct number of various sorts of ae_tags are
5495293465fSRobert Watson 	 * present:
5505293465fSRobert Watson 	 *   Exactly one ACL_USER_OBJ
5515293465fSRobert Watson 	 *   Exactly one ACL_GROUP_OBJ
5525293465fSRobert Watson 	 *   Exactly one ACL_OTHER
5535293465fSRobert Watson 	 *   If any ACL_USER or ACL_GROUP entries appear, then exactly one
5545293465fSRobert Watson 	 *   ACL_MASK entry must also appear.
5555293465fSRobert Watson 	 * Verify that all ae_perm entries are in ACL_PERM_BITS.
5565293465fSRobert Watson 	 * Verify all ae_tag entries are understood by this implementation.
5575293465fSRobert Watson 	 * Note: Does not check for uniqueness of qualifier (ae_id) field.
5585293465fSRobert Watson 	 */
5595293465fSRobert Watson 	num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
5605293465fSRobert Watson 	    num_acl_mask = num_acl_other = 0;
5615293465fSRobert Watson 	if (acl->acl_cnt > ACL_MAX_ENTRIES || acl->acl_cnt < 0)
5625293465fSRobert Watson 		return (EINVAL);
5635293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
5645293465fSRobert Watson 		/*
5655293465fSRobert Watson 		 * Check for a valid tag.
5665293465fSRobert Watson 		 */
5675293465fSRobert Watson 		switch(acl->acl_entry[i].ae_tag) {
5685293465fSRobert Watson 		case ACL_USER_OBJ:
569b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
570b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
571b114e127SRobert Watson 				return (EINVAL);
5725293465fSRobert Watson 			num_acl_user_obj++;
5735293465fSRobert Watson 			break;
5745293465fSRobert Watson 		case ACL_GROUP_OBJ:
575b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
576b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
577b114e127SRobert Watson 				return (EINVAL);
5785293465fSRobert Watson 			num_acl_group_obj++;
5795293465fSRobert Watson 			break;
5805293465fSRobert Watson 		case ACL_USER:
581b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
582b114e127SRobert Watson 				return (EINVAL);
5835293465fSRobert Watson 			num_acl_user++;
5845293465fSRobert Watson 			break;
5855293465fSRobert Watson 		case ACL_GROUP:
586b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
587b114e127SRobert Watson 				return (EINVAL);
5885293465fSRobert Watson 			num_acl_group++;
5895293465fSRobert Watson 			break;
5905293465fSRobert Watson 		case ACL_OTHER:
591b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
592b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
593b114e127SRobert Watson 				return (EINVAL);
5945293465fSRobert Watson 			num_acl_other++;
5955293465fSRobert Watson 			break;
5965293465fSRobert Watson 		case ACL_MASK:
597b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
598b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
599b114e127SRobert Watson 				return (EINVAL);
6005293465fSRobert Watson 			num_acl_mask++;
6015293465fSRobert Watson 			break;
6025293465fSRobert Watson 		default:
6035293465fSRobert Watson 			return (EINVAL);
6045293465fSRobert Watson 		}
6055293465fSRobert Watson 		/*
6065293465fSRobert Watson 		 * Check for valid perm entries.
6075293465fSRobert Watson 		 */
6085293465fSRobert Watson 		if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
6095293465fSRobert Watson 		    ACL_PERM_BITS)
6105293465fSRobert Watson 			return (EINVAL);
6115293465fSRobert Watson 	}
6125293465fSRobert Watson 	if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
6135293465fSRobert Watson 	    (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
6145293465fSRobert Watson 		return (EINVAL);
6155293465fSRobert Watson 	if (((num_acl_group != 0) || (num_acl_user != 0)) &&
6165293465fSRobert Watson 	    (num_acl_mask != 1))
6175293465fSRobert Watson 		return (EINVAL);
6185293465fSRobert Watson 	return (0);
6195293465fSRobert Watson }
6205293465fSRobert Watson 
6215293465fSRobert Watson /*
62260bdc14eSRobert Watson  * Given a requested mode for a new object, and a default ACL, combine
62360bdc14eSRobert Watson  * the two to produce a new mode.  Be careful not to clear any bits that
62460bdc14eSRobert Watson  * aren't intended to be affected by the POSIX.1e ACL.  Eventually,
62560bdc14eSRobert Watson  * this might also take the cmask as an argument, if we push that down
62660bdc14eSRobert Watson  * into per-filesystem-code.
62760bdc14eSRobert Watson  */
62860bdc14eSRobert Watson mode_t
62960bdc14eSRobert Watson acl_posix1e_newfilemode(mode_t cmode, struct acl *dacl)
63060bdc14eSRobert Watson {
63160bdc14eSRobert Watson 	mode_t mode;
63260bdc14eSRobert Watson 
63360bdc14eSRobert Watson 	mode = cmode;
63460bdc14eSRobert Watson 	/*
63560bdc14eSRobert Watson 	 * The current composition policy is that a permission bit must
63660bdc14eSRobert Watson 	 * be set in *both* the ACL and the requested creation mode for
63760bdc14eSRobert Watson 	 * it to appear in the resulting mode/ACL.  First clear any
63860bdc14eSRobert Watson 	 * possibly effected bits, then reconstruct.
63960bdc14eSRobert Watson 	 */
64060bdc14eSRobert Watson 	mode &= ACL_PRESERVE_MASK;
64160bdc14eSRobert Watson 	mode |= (ACL_OVERRIDE_MASK & cmode & acl_posix1e_acl_to_mode(dacl));
64260bdc14eSRobert Watson 
64360bdc14eSRobert Watson 	return (mode);
64460bdc14eSRobert Watson }
64560bdc14eSRobert Watson 
64660bdc14eSRobert Watson /*
64791f37dcbSRobert Watson  * These calls wrap the real vnode operations, and are called by the
64891f37dcbSRobert Watson  * syscall code once the syscall has converted the path or file
64991f37dcbSRobert Watson  * descriptor to a vnode (unlocked).  The aclp pointer is assumed
65091f37dcbSRobert Watson  * still to point to userland, so this should not be consumed within
65191f37dcbSRobert Watson  * the kernel except by syscall code.  Other code should directly
65291f37dcbSRobert Watson  * invoke VOP_{SET,GET}ACL.
65391f37dcbSRobert Watson  */
65491f37dcbSRobert Watson 
65591f37dcbSRobert Watson /*
65691f37dcbSRobert Watson  * Given a vnode, set its ACL.
65791f37dcbSRobert Watson  */
65891f37dcbSRobert Watson static int
659b40ce416SJulian Elischer vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
66091f37dcbSRobert Watson     struct acl *aclp)
66191f37dcbSRobert Watson {
66291f37dcbSRobert Watson 	struct acl inkernacl;
6634e1123c7SRobert Watson 	struct mount *mp;
66491f37dcbSRobert Watson 	int error;
66591f37dcbSRobert Watson 
66691f37dcbSRobert Watson 	error = copyin(aclp, &inkernacl, sizeof(struct acl));
66791f37dcbSRobert Watson 	if (error)
66891f37dcbSRobert Watson 		return(error);
6694e1123c7SRobert Watson 	error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
6704e1123c7SRobert Watson 	if (error != 0)
6714e1123c7SRobert Watson 		return (error);
672a854ed98SJohn Baldwin 	VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
673b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
674c86ca022SRobert Watson #ifdef MAC
675c86ca022SRobert Watson 	error = mac_check_vnode_setacl(td->td_ucred, vp, type, &inkernacl);
676c86ca022SRobert Watson 	if (error != 0)
677c86ca022SRobert Watson 		goto out;
678c86ca022SRobert Watson #endif
679a854ed98SJohn Baldwin 	error = VOP_SETACL(vp, type, &inkernacl, td->td_ucred, td);
680c86ca022SRobert Watson #ifdef MAC
681c86ca022SRobert Watson out:
682c86ca022SRobert Watson #endif
683b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
6844e1123c7SRobert Watson 	vn_finished_write(mp);
68591f37dcbSRobert Watson 	return(error);
68691f37dcbSRobert Watson }
68791f37dcbSRobert Watson 
68891f37dcbSRobert Watson /*
68991f37dcbSRobert Watson  * Given a vnode, get its ACL.
69091f37dcbSRobert Watson  */
69191f37dcbSRobert Watson static int
692b40ce416SJulian Elischer vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
69391f37dcbSRobert Watson     struct acl *aclp)
69491f37dcbSRobert Watson {
69591f37dcbSRobert Watson 	struct acl inkernelacl;
69691f37dcbSRobert Watson 	int error;
69791f37dcbSRobert Watson 
698a854ed98SJohn Baldwin 	VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
699b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
700c86ca022SRobert Watson #ifdef MAC
701c86ca022SRobert Watson 	error = mac_check_vnode_getacl(td->td_ucred, vp, type);
702c86ca022SRobert Watson 	if (error != 0)
703c86ca022SRobert Watson 		goto out;
704c86ca022SRobert Watson #endif
705a854ed98SJohn Baldwin 	error = VOP_GETACL(vp, type, &inkernelacl, td->td_ucred, td);
706c86ca022SRobert Watson #ifdef MAC
707c86ca022SRobert Watson out:
708c86ca022SRobert Watson #endif
709b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
71091f37dcbSRobert Watson 	if (error == 0)
71191f37dcbSRobert Watson 		error = copyout(&inkernelacl, aclp, sizeof(struct acl));
71291f37dcbSRobert Watson 	return (error);
71391f37dcbSRobert Watson }
71491f37dcbSRobert Watson 
71591f37dcbSRobert Watson /*
71691f37dcbSRobert Watson  * Given a vnode, delete its ACL.
71791f37dcbSRobert Watson  */
71891f37dcbSRobert Watson static int
719b40ce416SJulian Elischer vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
72091f37dcbSRobert Watson {
7214e1123c7SRobert Watson 	struct mount *mp;
72291f37dcbSRobert Watson 	int error;
72391f37dcbSRobert Watson 
7244e1123c7SRobert Watson 	error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
7254e1123c7SRobert Watson 	if (error)
7264e1123c7SRobert Watson 		return (error);
727a854ed98SJohn Baldwin 	VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
728b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
729c86ca022SRobert Watson #ifdef MAC
730c86ca022SRobert Watson 	error = mac_check_vnode_deleteacl(td->td_ucred, vp, type);
731c86ca022SRobert Watson 	if (error)
732c86ca022SRobert Watson 		goto out;
733c86ca022SRobert Watson #endif
734c86ca022SRobert Watson 	error = VOP_SETACL(vp, type, 0, td->td_ucred, td);
735c86ca022SRobert Watson #ifdef MAC
736c86ca022SRobert Watson out:
737c86ca022SRobert Watson #endif
738b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
7394e1123c7SRobert Watson 	vn_finished_write(mp);
74091f37dcbSRobert Watson 	return (error);
74191f37dcbSRobert Watson }
74291f37dcbSRobert Watson 
74391f37dcbSRobert Watson /*
74491f37dcbSRobert Watson  * Given a vnode, check whether an ACL is appropriate for it
74591f37dcbSRobert Watson  */
74691f37dcbSRobert Watson static int
747b40ce416SJulian Elischer vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
74891f37dcbSRobert Watson     struct acl *aclp)
74991f37dcbSRobert Watson {
75091f37dcbSRobert Watson 	struct acl inkernelacl;
75191f37dcbSRobert Watson 	int error;
75291f37dcbSRobert Watson 
75391f37dcbSRobert Watson 	error = copyin(aclp, &inkernelacl, sizeof(struct acl));
75491f37dcbSRobert Watson 	if (error)
75591f37dcbSRobert Watson 		return(error);
756a854ed98SJohn Baldwin 	error = VOP_ACLCHECK(vp, type, &inkernelacl, td->td_ucred, td);
75791f37dcbSRobert Watson 	return (error);
75891f37dcbSRobert Watson }
75991f37dcbSRobert Watson 
76091f37dcbSRobert Watson /*
76191f37dcbSRobert Watson  * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
76291f37dcbSRobert Watson  * Don't need to lock, as the vacl_ code will get/release any locks
76391f37dcbSRobert Watson  * required.
76491f37dcbSRobert Watson  */
76591f37dcbSRobert Watson 
76691f37dcbSRobert Watson /*
76791f37dcbSRobert Watson  * Given a file path, get an ACL for it
768f708f4d1SMatthew Dillon  *
769f708f4d1SMatthew Dillon  * MPSAFE
77091f37dcbSRobert Watson  */
77191f37dcbSRobert Watson int
772b40ce416SJulian Elischer __acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
77391f37dcbSRobert Watson {
77491f37dcbSRobert Watson 	struct nameidata nd;
77591f37dcbSRobert Watson 	int error;
77691f37dcbSRobert Watson 
777f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
778d1e405c5SAlfred Perlstein 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
77991f37dcbSRobert Watson 	error = namei(&nd);
780f708f4d1SMatthew Dillon 	if (error == 0) {
781f97182acSAlfred Perlstein 		error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
78291f37dcbSRobert Watson 		NDFREE(&nd, 0);
783f708f4d1SMatthew Dillon 	}
784f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
78591f37dcbSRobert Watson 	return (error);
78691f37dcbSRobert Watson }
78791f37dcbSRobert Watson 
78891f37dcbSRobert Watson /*
7893c67c23bSRobert Watson  * Given a file path, get an ACL for it; don't follow links.
7903c67c23bSRobert Watson  *
7913c67c23bSRobert Watson  * MPSAFE
7923c67c23bSRobert Watson  */
7933c67c23bSRobert Watson int
7943c67c23bSRobert Watson __acl_get_link(struct thread *td, struct __acl_get_link_args *uap)
7953c67c23bSRobert Watson {
7963c67c23bSRobert Watson 	struct nameidata nd;
7973c67c23bSRobert Watson 	int error;
7983c67c23bSRobert Watson 
7993c67c23bSRobert Watson 	mtx_lock(&Giant);
8003c67c23bSRobert Watson 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
8013c67c23bSRobert Watson 	error = namei(&nd);
8023c67c23bSRobert Watson 	if (error == 0) {
8033c67c23bSRobert Watson 		error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
8043c67c23bSRobert Watson 		NDFREE(&nd, 0);
8053c67c23bSRobert Watson 	}
8063c67c23bSRobert Watson 	mtx_unlock(&Giant);
8073c67c23bSRobert Watson 	return (error);
8083c67c23bSRobert Watson }
8093c67c23bSRobert Watson 
8103c67c23bSRobert Watson /*
81191f37dcbSRobert Watson  * Given a file path, set an ACL for it
812f708f4d1SMatthew Dillon  *
813f708f4d1SMatthew Dillon  * MPSAFE
81491f37dcbSRobert Watson  */
81591f37dcbSRobert Watson int
816b40ce416SJulian Elischer __acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
81791f37dcbSRobert Watson {
81891f37dcbSRobert Watson 	struct nameidata nd;
81991f37dcbSRobert Watson 	int error;
82091f37dcbSRobert Watson 
821f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
822d1e405c5SAlfred Perlstein 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
82391f37dcbSRobert Watson 	error = namei(&nd);
824f708f4d1SMatthew Dillon 	if (error == 0) {
825f97182acSAlfred Perlstein 		error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
82691f37dcbSRobert Watson 		NDFREE(&nd, 0);
827f708f4d1SMatthew Dillon 	}
828f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
82991f37dcbSRobert Watson 	return (error);
83091f37dcbSRobert Watson }
83191f37dcbSRobert Watson 
83291f37dcbSRobert Watson /*
8333c67c23bSRobert Watson  * Given a file path, set an ACL for it; don't follow links.
8343c67c23bSRobert Watson  *
8353c67c23bSRobert Watson  * MPSAFE
8363c67c23bSRobert Watson  */
8373c67c23bSRobert Watson int
8383c67c23bSRobert Watson __acl_set_link(struct thread *td, struct __acl_set_link_args *uap)
8393c67c23bSRobert Watson {
8403c67c23bSRobert Watson 	struct nameidata nd;
8413c67c23bSRobert Watson 	int error;
8423c67c23bSRobert Watson 
8433c67c23bSRobert Watson 	mtx_lock(&Giant);
8443c67c23bSRobert Watson 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
8453c67c23bSRobert Watson 	error = namei(&nd);
8463c67c23bSRobert Watson 	if (error == 0) {
8473c67c23bSRobert Watson 		error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
8483c67c23bSRobert Watson 		NDFREE(&nd, 0);
8493c67c23bSRobert Watson 	}
8503c67c23bSRobert Watson 	mtx_unlock(&Giant);
8513c67c23bSRobert Watson 	return (error);
8523c67c23bSRobert Watson }
8533c67c23bSRobert Watson 
8543c67c23bSRobert Watson /*
85591f37dcbSRobert Watson  * Given a file descriptor, get an ACL for it
856f708f4d1SMatthew Dillon  *
857f708f4d1SMatthew Dillon  * MPSAFE
85891f37dcbSRobert Watson  */
85991f37dcbSRobert Watson int
860b40ce416SJulian Elischer __acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
86191f37dcbSRobert Watson {
86291f37dcbSRobert Watson 	struct file *fp;
86391f37dcbSRobert Watson 	int error;
86491f37dcbSRobert Watson 
865f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
866d1e405c5SAlfred Perlstein 	error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
867f708f4d1SMatthew Dillon 	if (error == 0) {
8683b6d9652SPoul-Henning Kamp 		error = vacl_get_acl(td, fp->f_vnode, uap->type, uap->aclp);
869426da3bcSAlfred Perlstein 		fdrop(fp, td);
870f708f4d1SMatthew Dillon 	}
871f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
87291f37dcbSRobert Watson 	return (error);
87391f37dcbSRobert Watson }
87491f37dcbSRobert Watson 
87591f37dcbSRobert Watson /*
87691f37dcbSRobert Watson  * Given a file descriptor, set an ACL for it
877f708f4d1SMatthew Dillon  *
878f708f4d1SMatthew Dillon  * MPSAFE
87991f37dcbSRobert Watson  */
88091f37dcbSRobert Watson int
881b40ce416SJulian Elischer __acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
88291f37dcbSRobert Watson {
88391f37dcbSRobert Watson 	struct file *fp;
88491f37dcbSRobert Watson 	int error;
88591f37dcbSRobert Watson 
886f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
887d1e405c5SAlfred Perlstein 	error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
888f708f4d1SMatthew Dillon 	if (error == 0) {
8893b6d9652SPoul-Henning Kamp 		error = vacl_set_acl(td, fp->f_vnode, uap->type, uap->aclp);
890426da3bcSAlfred Perlstein 		fdrop(fp, td);
891f708f4d1SMatthew Dillon 	}
892f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
89391f37dcbSRobert Watson 	return (error);
89491f37dcbSRobert Watson }
89591f37dcbSRobert Watson 
89691f37dcbSRobert Watson /*
89791f37dcbSRobert Watson  * Given a file path, delete an ACL from it.
898f708f4d1SMatthew Dillon  *
899f708f4d1SMatthew Dillon  * MPSAFE
90091f37dcbSRobert Watson  */
90191f37dcbSRobert Watson int
902b40ce416SJulian Elischer __acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
90391f37dcbSRobert Watson {
90491f37dcbSRobert Watson 	struct nameidata nd;
90591f37dcbSRobert Watson 	int error;
90691f37dcbSRobert Watson 
907f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
908d1e405c5SAlfred Perlstein 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
90991f37dcbSRobert Watson 	error = namei(&nd);
910f708f4d1SMatthew Dillon 	if (error == 0) {
911d1e405c5SAlfred Perlstein 		error = vacl_delete(td, nd.ni_vp, uap->type);
91291f37dcbSRobert Watson 		NDFREE(&nd, 0);
913f708f4d1SMatthew Dillon 	}
914f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
91591f37dcbSRobert Watson 	return (error);
91691f37dcbSRobert Watson }
91791f37dcbSRobert Watson 
91891f37dcbSRobert Watson /*
9193c67c23bSRobert Watson  * Given a file path, delete an ACL from it; don't follow links.
9203c67c23bSRobert Watson  *
9213c67c23bSRobert Watson  * MPSAFE
9223c67c23bSRobert Watson  */
9233c67c23bSRobert Watson int
9243c67c23bSRobert Watson __acl_delete_link(struct thread *td, struct __acl_delete_link_args *uap)
9253c67c23bSRobert Watson {
9263c67c23bSRobert Watson 	struct nameidata nd;
9273c67c23bSRobert Watson 	int error;
9283c67c23bSRobert Watson 
9293c67c23bSRobert Watson 	mtx_lock(&Giant);
9303c67c23bSRobert Watson 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
9313c67c23bSRobert Watson 	error = namei(&nd);
9323c67c23bSRobert Watson 	if (error == 0) {
9333c67c23bSRobert Watson 		error = vacl_delete(td, nd.ni_vp, uap->type);
9343c67c23bSRobert Watson 		NDFREE(&nd, 0);
9353c67c23bSRobert Watson 	}
9363c67c23bSRobert Watson 	mtx_unlock(&Giant);
9373c67c23bSRobert Watson 	return (error);
9383c67c23bSRobert Watson }
9393c67c23bSRobert Watson 
9403c67c23bSRobert Watson /*
94191f37dcbSRobert Watson  * Given a file path, delete an ACL from it.
942f708f4d1SMatthew Dillon  *
943f708f4d1SMatthew Dillon  * MPSAFE
94491f37dcbSRobert Watson  */
94591f37dcbSRobert Watson int
946b40ce416SJulian Elischer __acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
94791f37dcbSRobert Watson {
94891f37dcbSRobert Watson 	struct file *fp;
94991f37dcbSRobert Watson 	int error;
95091f37dcbSRobert Watson 
951f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
952d1e405c5SAlfred Perlstein 	error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
953f708f4d1SMatthew Dillon 	if (error == 0) {
9543b6d9652SPoul-Henning Kamp 		error = vacl_delete(td, fp->f_vnode, uap->type);
955426da3bcSAlfred Perlstein 		fdrop(fp, td);
956f708f4d1SMatthew Dillon 	}
957f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
95891f37dcbSRobert Watson 	return (error);
95991f37dcbSRobert Watson }
96091f37dcbSRobert Watson 
96191f37dcbSRobert Watson /*
96291f37dcbSRobert Watson  * Given a file path, check an ACL for it
963f708f4d1SMatthew Dillon  *
964f708f4d1SMatthew Dillon  * MPSAFE
96591f37dcbSRobert Watson  */
96691f37dcbSRobert Watson int
967b40ce416SJulian Elischer __acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
96891f37dcbSRobert Watson {
96991f37dcbSRobert Watson 	struct nameidata	nd;
97091f37dcbSRobert Watson 	int	error;
97191f37dcbSRobert Watson 
972f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
973d1e405c5SAlfred Perlstein 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
97491f37dcbSRobert Watson 	error = namei(&nd);
975f708f4d1SMatthew Dillon 	if (error == 0) {
976f97182acSAlfred Perlstein 		error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
97791f37dcbSRobert Watson 		NDFREE(&nd, 0);
978f708f4d1SMatthew Dillon 	}
979f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
98091f37dcbSRobert Watson 	return (error);
98191f37dcbSRobert Watson }
98291f37dcbSRobert Watson 
98391f37dcbSRobert Watson /*
9843c67c23bSRobert Watson  * Given a file path, check an ACL for it; don't follow links.
9853c67c23bSRobert Watson  *
9863c67c23bSRobert Watson  * MPSAFE
9873c67c23bSRobert Watson  */
9883c67c23bSRobert Watson int
9893c67c23bSRobert Watson __acl_aclcheck_link(struct thread *td, struct __acl_aclcheck_link_args *uap)
9903c67c23bSRobert Watson {
9913c67c23bSRobert Watson 	struct nameidata	nd;
9923c67c23bSRobert Watson 	int	error;
9933c67c23bSRobert Watson 
9943c67c23bSRobert Watson 	mtx_lock(&Giant);
9953c67c23bSRobert Watson 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
9963c67c23bSRobert Watson 	error = namei(&nd);
9973c67c23bSRobert Watson 	if (error == 0) {
9983c67c23bSRobert Watson 		error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
9993c67c23bSRobert Watson 		NDFREE(&nd, 0);
10003c67c23bSRobert Watson 	}
10013c67c23bSRobert Watson 	mtx_unlock(&Giant);
10023c67c23bSRobert Watson 	return (error);
10033c67c23bSRobert Watson }
10043c67c23bSRobert Watson 
10053c67c23bSRobert Watson /*
100691f37dcbSRobert Watson  * Given a file descriptor, check an ACL for it
1007f708f4d1SMatthew Dillon  *
1008f708f4d1SMatthew Dillon  * MPSAFE
100991f37dcbSRobert Watson  */
101091f37dcbSRobert Watson int
1011b40ce416SJulian Elischer __acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
101291f37dcbSRobert Watson {
101391f37dcbSRobert Watson 	struct file *fp;
101491f37dcbSRobert Watson 	int error;
101591f37dcbSRobert Watson 
1016f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
1017d1e405c5SAlfred Perlstein 	error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
1018f708f4d1SMatthew Dillon 	if (error == 0) {
10193b6d9652SPoul-Henning Kamp 		error = vacl_aclcheck(td, fp->f_vnode, uap->type, uap->aclp);
1020426da3bcSAlfred Perlstein 		fdrop(fp, td);
1021f708f4d1SMatthew Dillon 	}
1022f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
102391f37dcbSRobert Watson 	return (error);
102491f37dcbSRobert Watson }
1025d1dfd921SChristian S.J. Peron 
1026d1dfd921SChristian S.J. Peron /* ARGUSED */
1027d1dfd921SChristian S.J. Peron 
1028d1dfd921SChristian S.J. Peron static void
1029d1dfd921SChristian S.J. Peron aclinit(void *dummy __unused)
1030d1dfd921SChristian S.J. Peron {
1031d1dfd921SChristian S.J. Peron 
1032d1dfd921SChristian S.J. Peron 	acl_zone = uma_zcreate("ACL UMA zone", sizeof(struct acl),
1033d1dfd921SChristian S.J. Peron 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1034d1dfd921SChristian S.J. Peron }
1035d1dfd921SChristian S.J. Peron SYSINIT(acls, SI_SUB_ACL, SI_ORDER_FIRST, aclinit, NULL)
1036