xref: /freebsd/sys/kern/subr_acl_posix1e.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
191f37dcbSRobert Watson /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
4e4256d1eSRobert Watson  * Copyright (c) 1999-2006 Robert N. M. Watson
591f37dcbSRobert Watson  * All rights reserved.
691f37dcbSRobert Watson  *
76d878543SRobert Watson  * This software was developed by Robert Watson for the TrustedBSD Project.
86d878543SRobert Watson  *
991f37dcbSRobert Watson  * Redistribution and use in source and binary forms, with or without
1091f37dcbSRobert Watson  * modification, are permitted provided that the following conditions
1191f37dcbSRobert Watson  * are met:
1291f37dcbSRobert Watson  * 1. Redistributions of source code must retain the above copyright
1391f37dcbSRobert Watson  *    notice, this list of conditions and the following disclaimer.
1491f37dcbSRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1591f37dcbSRobert Watson  *    notice, this list of conditions and the following disclaimer in the
1691f37dcbSRobert Watson  *    documentation and/or other materials provided with the distribution.
1791f37dcbSRobert Watson  *
1891f37dcbSRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1991f37dcbSRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2091f37dcbSRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2191f37dcbSRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2291f37dcbSRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2391f37dcbSRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2491f37dcbSRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2591f37dcbSRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2691f37dcbSRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2791f37dcbSRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2891f37dcbSRobert Watson  * SUCH DAMAGE.
2991f37dcbSRobert Watson  */
3091f37dcbSRobert Watson /*
315293465fSRobert Watson  * Developed by the TrustedBSD Project.
32e4256d1eSRobert Watson  *
33e4256d1eSRobert Watson  * ACL support routines specific to POSIX.1e access control lists.  These are
34e4256d1eSRobert Watson  * utility routines for code common across file systems implementing POSIX.1e
35e4256d1eSRobert Watson  * ACLs.
3691f37dcbSRobert Watson  */
3791f37dcbSRobert Watson 
3891f37dcbSRobert Watson #include <sys/param.h>
39a9a282f6SAdrian Chadd #include <sys/kernel.h>
40a9a282f6SAdrian Chadd #include <sys/module.h>
4191f37dcbSRobert Watson #include <sys/systm.h>
4242e7197fSChristian S.J. Peron #include <sys/mount.h>
43acd3428bSRobert Watson #include <sys/priv.h>
4491f37dcbSRobert Watson #include <sys/vnode.h>
4591f37dcbSRobert Watson #include <sys/errno.h>
4691f37dcbSRobert Watson #include <sys/stat.h>
4791f37dcbSRobert Watson #include <sys/acl.h>
4891f37dcbSRobert Watson 
4991f37dcbSRobert Watson /*
50fb6d736dSRobert Watson  * Implement a version of vaccess() that understands POSIX.1e ACL semantics;
51acd3428bSRobert Watson  * the access ACL has already been prepared for evaluation by the file system
52acd3428bSRobert Watson  * and is passed via 'uid', 'gid', and 'acl'.  Return 0 on success, else an
53acd3428bSRobert Watson  * errno value.
545293465fSRobert Watson  */
555293465fSRobert Watson int
vaccess_acl_posix1e(__enum_uint8 (vtype)type,uid_t file_uid,gid_t file_gid,struct acl * acl,accmode_t accmode,struct ucred * cred)56*ba8cc6d7SMateusz Guzik vaccess_acl_posix1e(__enum_uint8(vtype) type, uid_t file_uid, gid_t file_gid,
57d292b194SMateusz Guzik     struct acl *acl, accmode_t accmode, struct ucred *cred)
585293465fSRobert Watson {
595293465fSRobert Watson 	struct acl_entry *acl_other, *acl_mask;
6015bc6b2bSEdward Tomasz Napierala 	accmode_t dac_granted;
6115bc6b2bSEdward Tomasz Napierala 	accmode_t priv_granted;
6215bc6b2bSEdward Tomasz Napierala 	accmode_t acl_mask_granted;
635293465fSRobert Watson 	int group_matched, i;
645293465fSRobert Watson 
652c29cfa0SEdward Tomasz Napierala 	KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
662c29cfa0SEdward Tomasz Napierala 	    ("invalid bit in accmode"));
67558e9b5cSEdward Tomasz Napierala 	KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
68558e9b5cSEdward Tomasz Napierala 	    	("VAPPEND without VWRITE"));
692c29cfa0SEdward Tomasz Napierala 
705293465fSRobert Watson 	/*
715293465fSRobert Watson 	 * Look for a normal, non-privileged way to access the file/directory
72fb6d736dSRobert Watson 	 * as requested.  If it exists, go with that.  Otherwise, attempt to
73acd3428bSRobert Watson 	 * use privileges granted via priv_granted.  In some cases, which
74fb6d736dSRobert Watson 	 * privileges to use may be ambiguous due to "best match", in which
75fb6d736dSRobert Watson 	 * case fall back on first match for the time being.
765293465fSRobert Watson 	 */
775293465fSRobert Watson 
785293465fSRobert Watson 	/*
79fb6d736dSRobert Watson 	 * Determine privileges now, but don't apply until we've found a DAC
80acd3428bSRobert Watson 	 * entry that matches but has failed to allow access.
81acd3428bSRobert Watson 	 *
82acd3428bSRobert Watson 	 * XXXRW: Ideally, we'd determine the privileges required before
83acd3428bSRobert Watson 	 * asking for them.
845293465fSRobert Watson 	 */
85acd3428bSRobert Watson 	priv_granted = 0;
865293465fSRobert Watson 
875293465fSRobert Watson 	if (type == VDIR) {
88cc426dd3SMateusz Guzik 		if ((accmode & VEXEC) && !priv_check_cred(cred, PRIV_VFS_LOOKUP))
89acd3428bSRobert Watson 			priv_granted |= VEXEC;
905293465fSRobert Watson 	} else {
91de478dd4SJaakko Heinonen 		/*
92de478dd4SJaakko Heinonen 		 * Ensure that at least one execute bit is on. Otherwise,
93de478dd4SJaakko Heinonen 		 * a privileged user will always succeed, and we don't want
94de478dd4SJaakko Heinonen 		 * this to happen unless the file really is executable.
95de478dd4SJaakko Heinonen 		 */
96de478dd4SJaakko Heinonen 		if ((accmode & VEXEC) && (acl_posix1e_acl_to_mode(acl) &
97de478dd4SJaakko Heinonen 		    (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
98cc426dd3SMateusz Guzik 		    !priv_check_cred(cred, PRIV_VFS_EXEC))
99acd3428bSRobert Watson 			priv_granted |= VEXEC;
1005293465fSRobert Watson 	}
1015293465fSRobert Watson 
102cc426dd3SMateusz Guzik 	if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ))
103acd3428bSRobert Watson 		priv_granted |= VREAD;
1045293465fSRobert Watson 
105013098c8SEdward Tomasz Napierala 	if (((accmode & VWRITE) || (accmode & VAPPEND)) &&
106cc426dd3SMateusz Guzik 	    !priv_check_cred(cred, PRIV_VFS_WRITE))
107acd3428bSRobert Watson 		priv_granted |= (VWRITE | VAPPEND);
1085293465fSRobert Watson 
109cc426dd3SMateusz Guzik 	if ((accmode & VADMIN) && !priv_check_cred(cred, PRIV_VFS_ADMIN))
110acd3428bSRobert Watson 		priv_granted |= VADMIN;
1115293465fSRobert Watson 
1125293465fSRobert Watson 	/*
113670f6b2fSRobert Watson 	 * The owner matches if the effective uid associated with the
114670f6b2fSRobert Watson 	 * credential matches that of the ACL_USER_OBJ entry.  While we're
115fb6d736dSRobert Watson 	 * doing the first scan, also cache the location of the ACL_MASK and
116fb6d736dSRobert Watson 	 * ACL_OTHER entries, preventing some future iterations.
1175293465fSRobert Watson 	 */
1185293465fSRobert Watson 	acl_mask = acl_other = NULL;
1195293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
1205293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
1215293465fSRobert Watson 		case ACL_USER_OBJ:
122b114e127SRobert Watson 			if (file_uid != cred->cr_uid)
1235293465fSRobert Watson 				break;
1245293465fSRobert Watson 			dac_granted = 0;
1255293465fSRobert Watson 			dac_granted |= VADMIN;
126fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
1275293465fSRobert Watson 				dac_granted |= VEXEC;
128fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
1295293465fSRobert Watson 				dac_granted |= VREAD;
130fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
131b02aac46SRobert Watson 				dac_granted |= (VWRITE | VAPPEND);
132013098c8SEdward Tomasz Napierala 			if ((accmode & dac_granted) == accmode)
1335293465fSRobert Watson 				return (0);
134acd3428bSRobert Watson 
135acd3428bSRobert Watson 			/*
136acd3428bSRobert Watson 			 * XXXRW: Do privilege lookup here.
137acd3428bSRobert Watson 			 */
138013098c8SEdward Tomasz Napierala 			if ((accmode & (dac_granted | priv_granted)) ==
139013098c8SEdward Tomasz Napierala 			    accmode) {
1405293465fSRobert Watson 				return (0);
1415293465fSRobert Watson 			}
1425293465fSRobert Watson 			goto error;
1435293465fSRobert Watson 
1445293465fSRobert Watson 		case ACL_MASK:
1455293465fSRobert Watson 			acl_mask = &acl->acl_entry[i];
1465293465fSRobert Watson 			break;
1475293465fSRobert Watson 
1485293465fSRobert Watson 		case ACL_OTHER:
1495293465fSRobert Watson 			acl_other = &acl->acl_entry[i];
1505293465fSRobert Watson 			break;
1515293465fSRobert Watson 
1525293465fSRobert Watson 		default:
15330171114SPeter Wemm 			break;
1545293465fSRobert Watson 		}
1555293465fSRobert Watson 	}
1565293465fSRobert Watson 
1575293465fSRobert Watson 	/*
158fb6d736dSRobert Watson 	 * An ACL_OTHER entry should always exist in a valid access ACL.  If
159fb6d736dSRobert Watson 	 * it doesn't, then generate a serious failure.  For now, this means
160fb6d736dSRobert Watson 	 * a debugging message and EPERM, but in the future should probably
161fb6d736dSRobert Watson 	 * be a panic.
162670f6b2fSRobert Watson 	 */
163670f6b2fSRobert Watson 	if (acl_other == NULL) {
164670f6b2fSRobert Watson 		/*
165670f6b2fSRobert Watson 		 * XXX This should never happen
166670f6b2fSRobert Watson 		 */
167670f6b2fSRobert Watson 		printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
168670f6b2fSRobert Watson 		return (EPERM);
169670f6b2fSRobert Watson 	}
170670f6b2fSRobert Watson 
171670f6b2fSRobert Watson 	/*
172fb6d736dSRobert Watson 	 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields are
173fb6d736dSRobert Watson 	 * masked by an ACL_MASK entry, if any.  As such, first identify the
174fb6d736dSRobert Watson 	 * ACL_MASK field, then iterate through identifying potential user
175fb6d736dSRobert Watson 	 * matches, then group matches.  If there is no ACL_MASK, assume that
176fb6d736dSRobert Watson 	 * the mask allows all requests to succeed.
1775293465fSRobert Watson 	 */
1785293465fSRobert Watson 	if (acl_mask != NULL) {
1795293465fSRobert Watson 		acl_mask_granted = 0;
180fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_EXECUTE)
1815293465fSRobert Watson 			acl_mask_granted |= VEXEC;
182fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_READ)
1835293465fSRobert Watson 			acl_mask_granted |= VREAD;
184fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_WRITE)
185b02aac46SRobert Watson 			acl_mask_granted |= (VWRITE | VAPPEND);
1865293465fSRobert Watson 	} else
187b02aac46SRobert Watson 		acl_mask_granted = VEXEC | VREAD | VWRITE | VAPPEND;
1885293465fSRobert Watson 
1895293465fSRobert Watson 	/*
190acd3428bSRobert Watson 	 * Check ACL_USER ACL entries.  There will either be one or no
191acd3428bSRobert Watson 	 * matches; if there is one, we accept or rejected based on the
192acd3428bSRobert Watson 	 * match; otherwise, we continue on to groups.
1935293465fSRobert Watson 	 */
1945293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
1955293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
1965293465fSRobert Watson 		case ACL_USER:
1975293465fSRobert Watson 			if (acl->acl_entry[i].ae_id != cred->cr_uid)
1985293465fSRobert Watson 				break;
1995293465fSRobert Watson 			dac_granted = 0;
200fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2015293465fSRobert Watson 				dac_granted |= VEXEC;
202fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
2035293465fSRobert Watson 				dac_granted |= VREAD;
204fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
205b02aac46SRobert Watson 				dac_granted |= (VWRITE | VAPPEND);
2065293465fSRobert Watson 			dac_granted &= acl_mask_granted;
207013098c8SEdward Tomasz Napierala 			if ((accmode & dac_granted) == accmode)
2085293465fSRobert Watson 				return (0);
209acd3428bSRobert Watson 			/*
210acd3428bSRobert Watson 			 * XXXRW: Do privilege lookup here.
211acd3428bSRobert Watson 			 */
212013098c8SEdward Tomasz Napierala 			if ((accmode & (dac_granted | priv_granted)) !=
213013098c8SEdward Tomasz Napierala 			    accmode)
214b114e127SRobert Watson 				goto error;
215b114e127SRobert Watson 
2165293465fSRobert Watson 			return (0);
2175293465fSRobert Watson 		}
2185293465fSRobert Watson 	}
2195293465fSRobert Watson 
2205293465fSRobert Watson 	/*
221fb6d736dSRobert Watson 	 * Group match is best-match, not first-match, so find a "best"
222fb6d736dSRobert Watson 	 * match.  Iterate across, testing each potential group match.  Make
223fb6d736dSRobert Watson 	 * sure we keep track of whether we found a match or not, so that we
224fb6d736dSRobert Watson 	 * know if we should try again with any available privilege, or if we
225fb6d736dSRobert Watson 	 * should move on to ACL_OTHER.
2265293465fSRobert Watson 	 */
2275293465fSRobert Watson 	group_matched = 0;
2285293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
2295293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
2305293465fSRobert Watson 		case ACL_GROUP_OBJ:
231e15480f8SThomas Moestl 			if (!groupmember(file_gid, cred))
232b114e127SRobert Watson 				break;
2335293465fSRobert Watson 			dac_granted = 0;
234fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2355293465fSRobert Watson 				dac_granted |= VEXEC;
236fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
2375293465fSRobert Watson 				dac_granted |= VREAD;
238fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
239b02aac46SRobert Watson 				dac_granted |= (VWRITE | VAPPEND);
2405293465fSRobert Watson 			dac_granted  &= acl_mask_granted;
2415293465fSRobert Watson 
242013098c8SEdward Tomasz Napierala 			if ((accmode & dac_granted) == accmode)
2435293465fSRobert Watson 				return (0);
2445293465fSRobert Watson 
2455293465fSRobert Watson 			group_matched = 1;
246b114e127SRobert Watson 			break;
247b114e127SRobert Watson 
248b114e127SRobert Watson 		case ACL_GROUP:
249b114e127SRobert Watson 			if (!groupmember(acl->acl_entry[i].ae_id, cred))
250b114e127SRobert Watson 				break;
251b114e127SRobert Watson 			dac_granted = 0;
252b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
253b114e127SRobert Watson 				dac_granted |= VEXEC;
254b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_READ)
255b114e127SRobert Watson 				dac_granted |= VREAD;
256b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
257b02aac46SRobert Watson 				dac_granted |= (VWRITE | VAPPEND);
258b114e127SRobert Watson 			dac_granted  &= acl_mask_granted;
259b114e127SRobert Watson 
260013098c8SEdward Tomasz Napierala 			if ((accmode & dac_granted) == accmode)
261b114e127SRobert Watson 				return (0);
262b114e127SRobert Watson 
263b114e127SRobert Watson 			group_matched = 1;
264b114e127SRobert Watson 			break;
265b114e127SRobert Watson 
2665293465fSRobert Watson 		default:
26730171114SPeter Wemm 			break;
2685293465fSRobert Watson 		}
2695293465fSRobert Watson 	}
2705293465fSRobert Watson 
2715293465fSRobert Watson 	if (group_matched == 1) {
2725293465fSRobert Watson 		/*
273fb6d736dSRobert Watson 		 * There was a match, but it did not grant rights via pure
274fb6d736dSRobert Watson 		 * DAC.  Try again, this time with privilege.
2755293465fSRobert Watson 		 */
2765293465fSRobert Watson 		for (i = 0; i < acl->acl_cnt; i++) {
2775293465fSRobert Watson 			switch (acl->acl_entry[i].ae_tag) {
2785293465fSRobert Watson 			case ACL_GROUP_OBJ:
27946157a65SRobert Watson 				if (!groupmember(file_gid, cred))
280b114e127SRobert Watson 					break;
2815293465fSRobert Watson 				dac_granted = 0;
282b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2835293465fSRobert Watson 					dac_granted |= VEXEC;
284b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_READ)
2855293465fSRobert Watson 					dac_granted |= VREAD;
286b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_WRITE)
287b02aac46SRobert Watson 					dac_granted |= (VWRITE | VAPPEND);
2885293465fSRobert Watson 				dac_granted &= acl_mask_granted;
289b114e127SRobert Watson 
290acd3428bSRobert Watson 				/*
291acd3428bSRobert Watson 				 * XXXRW: Do privilege lookup here.
292acd3428bSRobert Watson 				 */
293013098c8SEdward Tomasz Napierala 				if ((accmode & (dac_granted | priv_granted))
294013098c8SEdward Tomasz Napierala 				    != accmode)
295b114e127SRobert Watson 					break;
296b114e127SRobert Watson 
2975293465fSRobert Watson 				return (0);
298b114e127SRobert Watson 
299b114e127SRobert Watson 			case ACL_GROUP:
300b114e127SRobert Watson 				if (!groupmember(acl->acl_entry[i].ae_id,
301b114e127SRobert Watson 				    cred))
302b114e127SRobert Watson 					break;
303b114e127SRobert Watson 				dac_granted = 0;
304b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
305b114e127SRobert Watson 				dac_granted |= VEXEC;
306b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_READ)
307b114e127SRobert Watson 					dac_granted |= VREAD;
308b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_WRITE)
309b02aac46SRobert Watson 					dac_granted |= (VWRITE | VAPPEND);
310b114e127SRobert Watson 				dac_granted &= acl_mask_granted;
311b114e127SRobert Watson 
312acd3428bSRobert Watson 				/*
313acd3428bSRobert Watson 				 * XXXRW: Do privilege lookup here.
314acd3428bSRobert Watson 				 */
315013098c8SEdward Tomasz Napierala 				if ((accmode & (dac_granted | priv_granted))
316013098c8SEdward Tomasz Napierala 				    != accmode)
317b114e127SRobert Watson 					break;
318b114e127SRobert Watson 
319b114e127SRobert Watson 				return (0);
320b114e127SRobert Watson 
3215293465fSRobert Watson 			default:
32230171114SPeter Wemm 				break;
3235293465fSRobert Watson 			}
3245293465fSRobert Watson 		}
3255293465fSRobert Watson 		/*
3265293465fSRobert Watson 		 * Even with privilege, group membership was not sufficient.
3275293465fSRobert Watson 		 * Return failure.
3285293465fSRobert Watson 		 */
3295293465fSRobert Watson 		goto error;
3305293465fSRobert Watson 	}
3315293465fSRobert Watson 
3325293465fSRobert Watson 	/*
3335293465fSRobert Watson 	 * Fall back on ACL_OTHER.  ACL_MASK is not applied to ACL_OTHER.
3345293465fSRobert Watson 	 */
3355293465fSRobert Watson 	dac_granted = 0;
336fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_EXECUTE)
3375293465fSRobert Watson 		dac_granted |= VEXEC;
338fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_READ)
3395293465fSRobert Watson 		dac_granted |= VREAD;
340fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_WRITE)
341b02aac46SRobert Watson 		dac_granted |= (VWRITE | VAPPEND);
3425293465fSRobert Watson 
343013098c8SEdward Tomasz Napierala 	if ((accmode & dac_granted) == accmode)
3445293465fSRobert Watson 		return (0);
345acd3428bSRobert Watson 	/*
346acd3428bSRobert Watson 	 * XXXRW: Do privilege lookup here.
347acd3428bSRobert Watson 	 */
348013098c8SEdward Tomasz Napierala 	if ((accmode & (dac_granted | priv_granted)) == accmode) {
3495293465fSRobert Watson 		return (0);
3505293465fSRobert Watson 	}
3515293465fSRobert Watson 
3525293465fSRobert Watson error:
353013098c8SEdward Tomasz Napierala 	return ((accmode & VADMIN) ? EPERM : EACCES);
3545293465fSRobert Watson }
3555293465fSRobert Watson 
3565293465fSRobert Watson /*
357fb6d736dSRobert Watson  * For the purposes of filesystems maintaining the _OBJ entries in an inode
358fb6d736dSRobert Watson  * with a mode_t field, this routine converts a mode_t entry to an
359fb6d736dSRobert Watson  * acl_perm_t.
3605293465fSRobert Watson  */
3615293465fSRobert Watson acl_perm_t
acl_posix1e_mode_to_perm(acl_tag_t tag,mode_t mode)3625293465fSRobert Watson acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
3635293465fSRobert Watson {
3645293465fSRobert Watson 	acl_perm_t	perm = 0;
3655293465fSRobert Watson 
3665293465fSRobert Watson 	switch(tag) {
3675293465fSRobert Watson 	case ACL_USER_OBJ:
3685293465fSRobert Watson 		if (mode & S_IXUSR)
369fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
3705293465fSRobert Watson 		if (mode & S_IRUSR)
371fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
3725293465fSRobert Watson 		if (mode & S_IWUSR)
373fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
3745293465fSRobert Watson 		return (perm);
3755293465fSRobert Watson 
3765293465fSRobert Watson 	case ACL_GROUP_OBJ:
3775293465fSRobert Watson 		if (mode & S_IXGRP)
378fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
3795293465fSRobert Watson 		if (mode & S_IRGRP)
380fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
3815293465fSRobert Watson 		if (mode & S_IWGRP)
382fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
3835293465fSRobert Watson 		return (perm);
3845293465fSRobert Watson 
3855293465fSRobert Watson 	case ACL_OTHER:
3865293465fSRobert Watson 		if (mode & S_IXOTH)
387fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
3885293465fSRobert Watson 		if (mode & S_IROTH)
389fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
3905293465fSRobert Watson 		if (mode & S_IWOTH)
391fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
3925293465fSRobert Watson 		return (perm);
3935293465fSRobert Watson 
3945293465fSRobert Watson 	default:
3955293465fSRobert Watson 		printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag);
3965293465fSRobert Watson 		return (0);
3975293465fSRobert Watson 	}
3985293465fSRobert Watson }
3995293465fSRobert Watson 
4005293465fSRobert Watson /*
4015293465fSRobert Watson  * Given inode information (uid, gid, mode), return an acl entry of the
4025293465fSRobert Watson  * appropriate type.
4035293465fSRobert Watson  */
4045293465fSRobert Watson struct acl_entry
acl_posix1e_mode_to_entry(acl_tag_t tag,uid_t uid,gid_t gid,mode_t mode)4055293465fSRobert Watson acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
4065293465fSRobert Watson {
4075293465fSRobert Watson 	struct acl_entry	acl_entry;
4085293465fSRobert Watson 
4095293465fSRobert Watson 	acl_entry.ae_tag = tag;
4105293465fSRobert Watson 	acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
411ae1add4eSEdward Tomasz Napierala 	acl_entry.ae_entry_type = 0;
412ae1add4eSEdward Tomasz Napierala 	acl_entry.ae_flags = 0;
4135293465fSRobert Watson 	switch(tag) {
4145293465fSRobert Watson 	case ACL_USER_OBJ:
4155293465fSRobert Watson 		acl_entry.ae_id = uid;
4165293465fSRobert Watson 		break;
4175293465fSRobert Watson 
4185293465fSRobert Watson 	case ACL_GROUP_OBJ:
4195293465fSRobert Watson 		acl_entry.ae_id = gid;
4205293465fSRobert Watson 		break;
4215293465fSRobert Watson 
4225293465fSRobert Watson 	case ACL_OTHER:
423dbb14f98SChris D. Faulhaber 		acl_entry.ae_id = ACL_UNDEFINED_ID;
4245293465fSRobert Watson 		break;
4255293465fSRobert Watson 
4265293465fSRobert Watson 	default:
427dbb14f98SChris D. Faulhaber 		acl_entry.ae_id = ACL_UNDEFINED_ID;
4285293465fSRobert Watson 		printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
4295293465fSRobert Watson 	}
4305293465fSRobert Watson 
4315293465fSRobert Watson 	return (acl_entry);
4325293465fSRobert Watson }
4335293465fSRobert Watson 
4345293465fSRobert Watson /*
4355293465fSRobert Watson  * Utility function to generate a file mode given appropriate ACL entries.
4365293465fSRobert Watson  */
4375293465fSRobert Watson mode_t
acl_posix1e_perms_to_mode(struct acl_entry * acl_user_obj_entry,struct acl_entry * acl_group_obj_entry,struct acl_entry * acl_other_entry)4385293465fSRobert Watson acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
4395293465fSRobert Watson     struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
4405293465fSRobert Watson {
4415293465fSRobert Watson 	mode_t	mode;
4425293465fSRobert Watson 
4435293465fSRobert Watson 	mode = 0;
444fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
4455293465fSRobert Watson 		mode |= S_IXUSR;
446fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_READ)
4475293465fSRobert Watson 		mode |= S_IRUSR;
448fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_WRITE)
4495293465fSRobert Watson 		mode |= S_IWUSR;
450fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
4515293465fSRobert Watson 		mode |= S_IXGRP;
452fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_READ)
4535293465fSRobert Watson 		mode |= S_IRGRP;
454fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_WRITE)
4555293465fSRobert Watson 		mode |= S_IWGRP;
456fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_EXECUTE)
4575293465fSRobert Watson 		mode |= S_IXOTH;
458fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_READ)
4595293465fSRobert Watson 		mode |= S_IROTH;
460fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_WRITE)
4615293465fSRobert Watson 		mode |= S_IWOTH;
4625293465fSRobert Watson 
4635293465fSRobert Watson 	return (mode);
4645293465fSRobert Watson }
4655293465fSRobert Watson 
4665293465fSRobert Watson /*
467fb6d736dSRobert Watson  * Utility function to generate a file mode given a complete POSIX.1e access
468fb6d736dSRobert Watson  * ACL.  Note that if the ACL is improperly formed, this may result in a
469fb6d736dSRobert Watson  * panic.
47060bdc14eSRobert Watson  */
47160bdc14eSRobert Watson mode_t
acl_posix1e_acl_to_mode(struct acl * acl)47260bdc14eSRobert Watson acl_posix1e_acl_to_mode(struct acl *acl)
47360bdc14eSRobert Watson {
47460bdc14eSRobert Watson 	struct acl_entry *acl_mask, *acl_user_obj, *acl_group_obj, *acl_other;
47560bdc14eSRobert Watson 	int i;
47660bdc14eSRobert Watson 
47760bdc14eSRobert Watson 	/*
47860bdc14eSRobert Watson 	 * Find the ACL entries relevant to a POSIX permission mode.
47960bdc14eSRobert Watson 	 */
48060bdc14eSRobert Watson 	acl_user_obj = acl_group_obj = acl_other = acl_mask = NULL;
48160bdc14eSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
48260bdc14eSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
48360bdc14eSRobert Watson 		case ACL_USER_OBJ:
48460bdc14eSRobert Watson 			acl_user_obj = &acl->acl_entry[i];
48560bdc14eSRobert Watson 			break;
48660bdc14eSRobert Watson 
48760bdc14eSRobert Watson 		case ACL_GROUP_OBJ:
48860bdc14eSRobert Watson 			acl_group_obj = &acl->acl_entry[i];
48960bdc14eSRobert Watson 			break;
49060bdc14eSRobert Watson 
49160bdc14eSRobert Watson 		case ACL_OTHER:
49260bdc14eSRobert Watson 			acl_other = &acl->acl_entry[i];
49360bdc14eSRobert Watson 			break;
49460bdc14eSRobert Watson 
49560bdc14eSRobert Watson 		case ACL_MASK:
49660bdc14eSRobert Watson 			acl_mask = &acl->acl_entry[i];
49760bdc14eSRobert Watson 			break;
49860bdc14eSRobert Watson 
49960bdc14eSRobert Watson 		case ACL_USER:
50060bdc14eSRobert Watson 		case ACL_GROUP:
50160bdc14eSRobert Watson 			break;
50260bdc14eSRobert Watson 
50360bdc14eSRobert Watson 		default:
50460bdc14eSRobert Watson 			panic("acl_posix1e_acl_to_mode: bad ae_tag");
50560bdc14eSRobert Watson 		}
50660bdc14eSRobert Watson 	}
50760bdc14eSRobert Watson 
50860bdc14eSRobert Watson 	if (acl_user_obj == NULL || acl_group_obj == NULL || acl_other == NULL)
50960bdc14eSRobert Watson 		panic("acl_posix1e_acl_to_mode: missing base ae_tags");
51060bdc14eSRobert Watson 
51160bdc14eSRobert Watson 	/*
51260bdc14eSRobert Watson 	 * POSIX.1e specifies that if there is an ACL_MASK entry, we replace
51360bdc14eSRobert Watson 	 * the mode "group" bits with its permissions.  If there isn't, we
51460bdc14eSRobert Watson 	 * use the ACL_GROUP_OBJ permissions.
51560bdc14eSRobert Watson 	 */
51660bdc14eSRobert Watson 	if (acl_mask != NULL)
51760bdc14eSRobert Watson 		return (acl_posix1e_perms_to_mode(acl_user_obj, acl_mask,
51860bdc14eSRobert Watson 		    acl_other));
51960bdc14eSRobert Watson 	else
52060bdc14eSRobert Watson 		return (acl_posix1e_perms_to_mode(acl_user_obj, acl_group_obj,
52160bdc14eSRobert Watson 		    acl_other));
52260bdc14eSRobert Watson }
52360bdc14eSRobert Watson 
52460bdc14eSRobert Watson /*
525fb6d736dSRobert Watson  * Perform a syntactic check of the ACL, sufficient to allow an implementing
526fb6d736dSRobert Watson  * filesystem to determine if it should accept this and rely on the POSIX.1e
527fb6d736dSRobert Watson  * ACL properties.
5285293465fSRobert Watson  */
5295293465fSRobert Watson int
acl_posix1e_check(struct acl * acl)5305293465fSRobert Watson acl_posix1e_check(struct acl *acl)
5315293465fSRobert Watson {
5325293465fSRobert Watson 	int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
5335293465fSRobert Watson 	int num_acl_mask, num_acl_other, i;
5345293465fSRobert Watson 
5355293465fSRobert Watson 	/*
5365293465fSRobert Watson 	 * Verify that the number of entries does not exceed the maximum
5375293465fSRobert Watson 	 * defined for acl_t.
538fb6d736dSRobert Watson 	 *
5395293465fSRobert Watson 	 * Verify that the correct number of various sorts of ae_tags are
5405293465fSRobert Watson 	 * present:
5415293465fSRobert Watson 	 *   Exactly one ACL_USER_OBJ
5425293465fSRobert Watson 	 *   Exactly one ACL_GROUP_OBJ
5435293465fSRobert Watson 	 *   Exactly one ACL_OTHER
5445293465fSRobert Watson 	 *   If any ACL_USER or ACL_GROUP entries appear, then exactly one
5455293465fSRobert Watson 	 *   ACL_MASK entry must also appear.
546fb6d736dSRobert Watson 	 *
5475293465fSRobert Watson 	 * Verify that all ae_perm entries are in ACL_PERM_BITS.
548fb6d736dSRobert Watson 	 *
5495293465fSRobert Watson 	 * Verify all ae_tag entries are understood by this implementation.
550fb6d736dSRobert Watson 	 *
5515293465fSRobert Watson 	 * Note: Does not check for uniqueness of qualifier (ae_id) field.
5525293465fSRobert Watson 	 */
5535293465fSRobert Watson 	num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
5545293465fSRobert Watson 	    num_acl_mask = num_acl_other = 0;
555019b32daSEdward Tomasz Napierala 	if (acl->acl_cnt > ACL_MAX_ENTRIES)
5565293465fSRobert Watson 		return (EINVAL);
5575293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
5585293465fSRobert Watson 		/*
5595293465fSRobert Watson 		 * Check for a valid tag.
5605293465fSRobert Watson 		 */
5615293465fSRobert Watson 		switch(acl->acl_entry[i].ae_tag) {
5625293465fSRobert Watson 		case ACL_USER_OBJ:
563b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
564b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
565b114e127SRobert Watson 				return (EINVAL);
5665293465fSRobert Watson 			num_acl_user_obj++;
5675293465fSRobert Watson 			break;
5685293465fSRobert Watson 		case ACL_GROUP_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_group_obj++;
5735293465fSRobert Watson 			break;
5745293465fSRobert Watson 		case ACL_USER:
575b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
576b114e127SRobert Watson 				return (EINVAL);
5775293465fSRobert Watson 			num_acl_user++;
5785293465fSRobert Watson 			break;
5795293465fSRobert Watson 		case ACL_GROUP:
580b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
581b114e127SRobert Watson 				return (EINVAL);
5825293465fSRobert Watson 			num_acl_group++;
5835293465fSRobert Watson 			break;
5845293465fSRobert Watson 		case ACL_OTHER:
585b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
586b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
587b114e127SRobert Watson 				return (EINVAL);
5885293465fSRobert Watson 			num_acl_other++;
5895293465fSRobert Watson 			break;
5905293465fSRobert Watson 		case ACL_MASK:
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_mask++;
5955293465fSRobert Watson 			break;
5965293465fSRobert Watson 		default:
5975293465fSRobert Watson 			return (EINVAL);
5985293465fSRobert Watson 		}
5995293465fSRobert Watson 		/*
6005293465fSRobert Watson 		 * Check for valid perm entries.
6015293465fSRobert Watson 		 */
6025293465fSRobert Watson 		if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
6035293465fSRobert Watson 		    ACL_PERM_BITS)
6045293465fSRobert Watson 			return (EINVAL);
6055293465fSRobert Watson 	}
6065293465fSRobert Watson 	if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
6075293465fSRobert Watson 	    (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
6085293465fSRobert Watson 		return (EINVAL);
6095293465fSRobert Watson 	if (((num_acl_group != 0) || (num_acl_user != 0)) &&
6105293465fSRobert Watson 	    (num_acl_mask != 1))
6115293465fSRobert Watson 		return (EINVAL);
6125293465fSRobert Watson 	return (0);
6135293465fSRobert Watson }
6145293465fSRobert Watson 
6155293465fSRobert Watson /*
616fb6d736dSRobert Watson  * Given a requested mode for a new object, and a default ACL, combine the
617fb6d736dSRobert Watson  * two to produce a new mode.  Be careful not to clear any bits that aren't
618fb6d736dSRobert Watson  * intended to be affected by the POSIX.1e ACL.  Eventually, this might also
619fb6d736dSRobert Watson  * take the cmask as an argument, if we push that down into
620fb6d736dSRobert Watson  * per-filesystem-code.
62160bdc14eSRobert Watson  */
62260bdc14eSRobert Watson mode_t
acl_posix1e_newfilemode(mode_t cmode,struct acl * dacl)62360bdc14eSRobert Watson acl_posix1e_newfilemode(mode_t cmode, struct acl *dacl)
62460bdc14eSRobert Watson {
62560bdc14eSRobert Watson 	mode_t mode;
62660bdc14eSRobert Watson 
62760bdc14eSRobert Watson 	mode = cmode;
62860bdc14eSRobert Watson 	/*
629fb6d736dSRobert Watson 	 * The current composition policy is that a permission bit must be
630fb6d736dSRobert Watson 	 * set in *both* the ACL and the requested creation mode for it to
631fb6d736dSRobert Watson 	 * appear in the resulting mode/ACL.  First clear any possibly
632fb6d736dSRobert Watson 	 * effected bits, then reconstruct.
63360bdc14eSRobert Watson 	 */
63460bdc14eSRobert Watson 	mode &= ACL_PRESERVE_MASK;
63560bdc14eSRobert Watson 	mode |= (ACL_OVERRIDE_MASK & cmode & acl_posix1e_acl_to_mode(dacl));
63660bdc14eSRobert Watson 
63760bdc14eSRobert Watson 	return (mode);
63860bdc14eSRobert Watson }
639a9a282f6SAdrian Chadd 
640a9a282f6SAdrian Chadd static int
acl_posix1e_modload(module_t mod,int what,void * arg)641a9a282f6SAdrian Chadd acl_posix1e_modload(module_t mod, int what, void *arg)
642a9a282f6SAdrian Chadd {
643a9a282f6SAdrian Chadd 	int ret;
644a9a282f6SAdrian Chadd 
645a9a282f6SAdrian Chadd 	ret = 0;
646a9a282f6SAdrian Chadd 
647a9a282f6SAdrian Chadd 	switch (what) {
648a9a282f6SAdrian Chadd 	case MOD_LOAD:
649a9a282f6SAdrian Chadd 	case MOD_SHUTDOWN:
650a9a282f6SAdrian Chadd 		break;
651a9a282f6SAdrian Chadd 
652a9a282f6SAdrian Chadd 	case MOD_QUIESCE:
653a9a282f6SAdrian Chadd 		/* XXX TODO */
654a9a282f6SAdrian Chadd 		ret = 0;
655a9a282f6SAdrian Chadd 		break;
656a9a282f6SAdrian Chadd 
657a9a282f6SAdrian Chadd 	case MOD_UNLOAD:
658a9a282f6SAdrian Chadd 		/* XXX TODO */
659a9a282f6SAdrian Chadd 		ret = 0;
660a9a282f6SAdrian Chadd 		break;
661a9a282f6SAdrian Chadd 	default:
662a9a282f6SAdrian Chadd 		ret = EINVAL;
663a9a282f6SAdrian Chadd 		break;
664a9a282f6SAdrian Chadd 	}
665a9a282f6SAdrian Chadd 
666a9a282f6SAdrian Chadd 	return (ret);
667a9a282f6SAdrian Chadd }
668a9a282f6SAdrian Chadd 
669a9a282f6SAdrian Chadd static moduledata_t acl_posix1e_mod = {
670a9a282f6SAdrian Chadd 	"acl_posix1e",
671a9a282f6SAdrian Chadd 	acl_posix1e_modload,
672a9a282f6SAdrian Chadd 	NULL
673a9a282f6SAdrian Chadd };
674a9a282f6SAdrian Chadd 
675a9a282f6SAdrian Chadd DECLARE_MODULE(acl_posix1e, acl_posix1e_mod, SI_SUB_VFS, SI_ORDER_FIRST);
676a9a282f6SAdrian Chadd MODULE_VERSION(acl_posix1e, 1);
677