xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/spl/acl_common.c (revision 80aae8a3f8aa70712930664572be9e6885dc0be7)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/avl.h>
30 #include <sys/misc.h>
31 #if defined(_KERNEL)
32 #include <sys/kmem.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <acl/acl_common.h>
36 #include <sys/debug.h>
37 #else
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <stddef.h>
41 #include <unistd.h>
42 #include <assert.h>
43 #include <grp.h>
44 #include <pwd.h>
45 #include <acl_common.h>
46 #endif
47 
48 #define	ACE_POSIX_SUPPORTED_BITS (ACE_READ_DATA | \
49     ACE_WRITE_DATA | ACE_APPEND_DATA | ACE_EXECUTE | \
50     ACE_READ_ATTRIBUTES | ACE_READ_ACL | ACE_WRITE_ACL)
51 
52 
53 #define	ACL_SYNCHRONIZE_SET_DENY		0x0000001
54 #define	ACL_SYNCHRONIZE_SET_ALLOW		0x0000002
55 #define	ACL_SYNCHRONIZE_ERR_DENY		0x0000004
56 #define	ACL_SYNCHRONIZE_ERR_ALLOW		0x0000008
57 
58 #define	ACL_WRITE_OWNER_SET_DENY		0x0000010
59 #define	ACL_WRITE_OWNER_SET_ALLOW		0x0000020
60 #define	ACL_WRITE_OWNER_ERR_DENY		0x0000040
61 #define	ACL_WRITE_OWNER_ERR_ALLOW		0x0000080
62 
63 #define	ACL_DELETE_SET_DENY			0x0000100
64 #define	ACL_DELETE_SET_ALLOW			0x0000200
65 #define	ACL_DELETE_ERR_DENY			0x0000400
66 #define	ACL_DELETE_ERR_ALLOW			0x0000800
67 
68 #define	ACL_WRITE_ATTRS_OWNER_SET_DENY		0x0001000
69 #define	ACL_WRITE_ATTRS_OWNER_SET_ALLOW		0x0002000
70 #define	ACL_WRITE_ATTRS_OWNER_ERR_DENY		0x0004000
71 #define	ACL_WRITE_ATTRS_OWNER_ERR_ALLOW		0x0008000
72 
73 #define	ACL_WRITE_ATTRS_WRITER_SET_DENY		0x0010000
74 #define	ACL_WRITE_ATTRS_WRITER_SET_ALLOW	0x0020000
75 #define	ACL_WRITE_ATTRS_WRITER_ERR_DENY		0x0040000
76 #define	ACL_WRITE_ATTRS_WRITER_ERR_ALLOW	0x0080000
77 
78 #define	ACL_WRITE_NAMED_WRITER_SET_DENY		0x0100000
79 #define	ACL_WRITE_NAMED_WRITER_SET_ALLOW	0x0200000
80 #define	ACL_WRITE_NAMED_WRITER_ERR_DENY		0x0400000
81 #define	ACL_WRITE_NAMED_WRITER_ERR_ALLOW	0x0800000
82 
83 #define	ACL_READ_NAMED_READER_SET_DENY		0x1000000
84 #define	ACL_READ_NAMED_READER_SET_ALLOW		0x2000000
85 #define	ACL_READ_NAMED_READER_ERR_DENY		0x4000000
86 #define	ACL_READ_NAMED_READER_ERR_ALLOW		0x8000000
87 
88 
89 #define	ACE_VALID_MASK_BITS (\
90     ACE_READ_DATA | \
91     ACE_LIST_DIRECTORY | \
92     ACE_WRITE_DATA | \
93     ACE_ADD_FILE | \
94     ACE_APPEND_DATA | \
95     ACE_ADD_SUBDIRECTORY | \
96     ACE_READ_NAMED_ATTRS | \
97     ACE_WRITE_NAMED_ATTRS | \
98     ACE_EXECUTE | \
99     ACE_DELETE_CHILD | \
100     ACE_READ_ATTRIBUTES | \
101     ACE_WRITE_ATTRIBUTES | \
102     ACE_DELETE | \
103     ACE_READ_ACL | \
104     ACE_WRITE_ACL | \
105     ACE_WRITE_OWNER | \
106     ACE_SYNCHRONIZE)
107 
108 #define	ACE_MASK_UNDEFINED			0x80000000
109 
110 #define	ACE_VALID_FLAG_BITS (ACE_FILE_INHERIT_ACE | \
111     ACE_DIRECTORY_INHERIT_ACE | \
112     ACE_NO_PROPAGATE_INHERIT_ACE | ACE_INHERIT_ONLY_ACE | \
113     ACE_SUCCESSFUL_ACCESS_ACE_FLAG | ACE_FAILED_ACCESS_ACE_FLAG | \
114     ACE_IDENTIFIER_GROUP | ACE_OWNER | ACE_GROUP | ACE_EVERYONE)
115 
116 /*
117  * ACL conversion helpers
118  */
119 
120 typedef enum {
121 	ace_unused,
122 	ace_user_obj,
123 	ace_user,
124 	ace_group, /* includes GROUP and GROUP_OBJ */
125 	ace_other_obj
126 } ace_to_aent_state_t;
127 
128 typedef struct acevals {
129 	uid_t key;
130 	avl_node_t avl;
131 	uint32_t mask;
132 	uint32_t allowed;
133 	uint32_t denied;
134 	int aent_type;
135 } acevals_t;
136 
137 typedef struct ace_list {
138 	acevals_t user_obj;
139 	avl_tree_t user;
140 	int numusers;
141 	acevals_t group_obj;
142 	avl_tree_t group;
143 	int numgroups;
144 	acevals_t other_obj;
145 	uint32_t acl_mask;
146 	int hasmask;
147 	int dfacl_flag;
148 	ace_to_aent_state_t state;
149 	int seen; /* bitmask of all aclent_t a_type values seen */
150 } ace_list_t;
151 
152 /*
153  * Generic shellsort, from K&R (1st ed, p 58.), somewhat modified.
154  * v = Ptr to array/vector of objs
155  * n = # objs in the array
156  * s = size of each obj (must be multiples of a word size)
157  * f = ptr to function to compare two objs
158  *	returns (-1 = less than, 0 = equal, 1 = greater than
159  */
160 void
ksort(caddr_t v,int n,int s,int (* f)(void *,void *))161 ksort(caddr_t v, int n, int s, int (*f)(void *, void *))
162 {
163 	int g, i, j, ii;
164 	unsigned int *p1, *p2;
165 	unsigned int tmp;
166 
167 	/* No work to do */
168 	if (v == NULL || n <= 1)
169 		return;
170 
171 	/* Sanity check on arguments */
172 	ASSERT3U(((uintptr_t)v & 0x3), ==, 0);
173 	ASSERT3S((s & 0x3), ==, 0);
174 	ASSERT3S(s, >, 0);
175 	for (g = n / 2; g > 0; g /= 2) {
176 		for (i = g; i < n; i++) {
177 			for (j = i - g; j >= 0 &&
178 			    (*f)(v + j * s, v + (j + g) * s) == 1;
179 			    j -= g) {
180 				p1 = (void *)(v + j * s);
181 				p2 = (void *)(v + (j + g) * s);
182 				for (ii = 0; ii < s / 4; ii++) {
183 					tmp = *p1;
184 					*p1++ = *p2;
185 					*p2++ = tmp;
186 				}
187 			}
188 		}
189 	}
190 }
191 
192 /*
193  * Compare two acls, all fields.  Returns:
194  * -1 (less than)
195  *  0 (equal)
196  * +1 (greater than)
197  */
198 int
cmp2acls(void * a,void * b)199 cmp2acls(void *a, void *b)
200 {
201 	aclent_t *x = (aclent_t *)a;
202 	aclent_t *y = (aclent_t *)b;
203 
204 	/* Compare types */
205 	if (x->a_type < y->a_type)
206 		return (-1);
207 	if (x->a_type > y->a_type)
208 		return (1);
209 	/* Equal types; compare id's */
210 	if (x->a_id < y->a_id)
211 		return (-1);
212 	if (x->a_id > y->a_id)
213 		return (1);
214 	/* Equal ids; compare perms */
215 	if (x->a_perm < y->a_perm)
216 		return (-1);
217 	if (x->a_perm > y->a_perm)
218 		return (1);
219 	/* Totally equal */
220 	return (0);
221 }
222 
223 static int
cacl_malloc(void ** ptr,size_t size)224 cacl_malloc(void **ptr, size_t size)
225 {
226 	*ptr = kmem_zalloc(size, KM_SLEEP);
227 	return (0);
228 }
229 
230 
231 #if !defined(_KERNEL)
232 acl_t *
acl_alloc(enum acl_type type)233 acl_alloc(enum acl_type type)
234 {
235 	acl_t *aclp;
236 
237 	if (cacl_malloc((void **)&aclp, sizeof (acl_t)) != 0)
238 		return (NULL);
239 
240 	aclp->acl_aclp = NULL;
241 	aclp->acl_cnt = 0;
242 
243 	switch (type) {
244 	case ACE_T:
245 		aclp->acl_type = ACE_T;
246 		aclp->acl_entry_size = sizeof (ace_t);
247 		break;
248 	case ACLENT_T:
249 		aclp->acl_type = ACLENT_T;
250 		aclp->acl_entry_size = sizeof (aclent_t);
251 		break;
252 	default:
253 		acl_free(aclp);
254 		aclp = NULL;
255 	}
256 	return (aclp);
257 }
258 
259 /*
260  * Free acl_t structure
261  */
262 void
acl_free(acl_t * aclp)263 acl_free(acl_t *aclp)
264 {
265 	int acl_size;
266 
267 	if (aclp == NULL)
268 		return;
269 
270 	if (aclp->acl_aclp) {
271 		acl_size = aclp->acl_cnt * aclp->acl_entry_size;
272 		cacl_free(aclp->acl_aclp, acl_size);
273 	}
274 
275 	cacl_free(aclp, sizeof (acl_t));
276 }
277 
278 static uint32_t
access_mask_set(int haswriteperm,int hasreadperm,int isowner,int isallow)279 access_mask_set(int haswriteperm, int hasreadperm, int isowner, int isallow)
280 {
281 	uint32_t access_mask = 0;
282 	int acl_produce;
283 	int synchronize_set = 0, write_owner_set = 0;
284 	int delete_set = 0, write_attrs_set = 0;
285 	int read_named_set = 0, write_named_set = 0;
286 
287 	acl_produce = (ACL_SYNCHRONIZE_SET_ALLOW |
288 	    ACL_WRITE_ATTRS_OWNER_SET_ALLOW |
289 	    ACL_WRITE_ATTRS_WRITER_SET_DENY);
290 
291 	if (isallow) {
292 		synchronize_set = ACL_SYNCHRONIZE_SET_ALLOW;
293 		write_owner_set = ACL_WRITE_OWNER_SET_ALLOW;
294 		delete_set = ACL_DELETE_SET_ALLOW;
295 		if (hasreadperm)
296 			read_named_set = ACL_READ_NAMED_READER_SET_ALLOW;
297 		if (haswriteperm)
298 			write_named_set = ACL_WRITE_NAMED_WRITER_SET_ALLOW;
299 		if (isowner)
300 			write_attrs_set = ACL_WRITE_ATTRS_OWNER_SET_ALLOW;
301 		else if (haswriteperm)
302 			write_attrs_set = ACL_WRITE_ATTRS_WRITER_SET_ALLOW;
303 	} else {
304 
305 		synchronize_set = ACL_SYNCHRONIZE_SET_DENY;
306 		write_owner_set = ACL_WRITE_OWNER_SET_DENY;
307 		delete_set = ACL_DELETE_SET_DENY;
308 		if (hasreadperm)
309 			read_named_set = ACL_READ_NAMED_READER_SET_DENY;
310 		if (haswriteperm)
311 			write_named_set = ACL_WRITE_NAMED_WRITER_SET_DENY;
312 		if (isowner)
313 			write_attrs_set = ACL_WRITE_ATTRS_OWNER_SET_DENY;
314 		else if (haswriteperm)
315 			write_attrs_set = ACL_WRITE_ATTRS_WRITER_SET_DENY;
316 		else
317 			/*
318 			 * If the entity is not the owner and does not
319 			 * have write permissions ACE_WRITE_ATTRIBUTES will
320 			 * always go in the DENY ACE.
321 			 */
322 			access_mask |= ACE_WRITE_ATTRIBUTES;
323 	}
324 
325 	if (acl_produce & synchronize_set)
326 		access_mask |= ACE_SYNCHRONIZE;
327 	if (acl_produce & write_owner_set)
328 		access_mask |= ACE_WRITE_OWNER;
329 	if (acl_produce & delete_set)
330 		access_mask |= ACE_DELETE;
331 	if (acl_produce & write_attrs_set)
332 		access_mask |= ACE_WRITE_ATTRIBUTES;
333 	if (acl_produce & read_named_set)
334 		access_mask |= ACE_READ_NAMED_ATTRS;
335 	if (acl_produce & write_named_set)
336 		access_mask |= ACE_WRITE_NAMED_ATTRS;
337 
338 	return (access_mask);
339 }
340 
341 /*
342  * Given an mode_t, convert it into an access_mask as used
343  * by nfsace, assuming aclent_t -> nfsace semantics.
344  */
345 static uint32_t
mode_to_ace_access(mode_t mode,boolean_t isdir,int isowner,int isallow)346 mode_to_ace_access(mode_t mode, boolean_t isdir, int isowner, int isallow)
347 {
348 	uint32_t access = 0;
349 	int haswriteperm = 0;
350 	int hasreadperm = 0;
351 
352 	if (isallow) {
353 		haswriteperm = (mode & S_IWOTH);
354 		hasreadperm = (mode & S_IROTH);
355 	} else {
356 		haswriteperm = !(mode & S_IWOTH);
357 		hasreadperm = !(mode & S_IROTH);
358 	}
359 
360 	/*
361 	 * The following call takes care of correctly setting the following
362 	 * mask bits in the access_mask:
363 	 * ACE_SYNCHRONIZE, ACE_WRITE_OWNER, ACE_DELETE,
364 	 * ACE_WRITE_ATTRIBUTES, ACE_WRITE_NAMED_ATTRS, ACE_READ_NAMED_ATTRS
365 	 */
366 	access = access_mask_set(haswriteperm, hasreadperm, isowner, isallow);
367 
368 	if (isallow) {
369 		access |= ACE_READ_ACL | ACE_READ_ATTRIBUTES;
370 		if (isowner)
371 			access |= ACE_WRITE_ACL;
372 	} else {
373 		if (! isowner)
374 			access |= ACE_WRITE_ACL;
375 	}
376 
377 	/* read */
378 	if (mode & S_IROTH) {
379 		access |= ACE_READ_DATA;
380 	}
381 	/* write */
382 	if (mode & S_IWOTH) {
383 		access |= ACE_WRITE_DATA |
384 		    ACE_APPEND_DATA;
385 		if (isdir)
386 			access |= ACE_DELETE_CHILD;
387 	}
388 	/* exec */
389 	if (mode & S_IXOTH) {
390 		access |= ACE_EXECUTE;
391 	}
392 
393 	return (access);
394 }
395 
396 /*
397  * Given an nfsace (presumably an ALLOW entry), make a
398  * corresponding DENY entry at the address given.
399  */
400 static void
ace_make_deny(ace_t * allow,ace_t * deny,int isdir,int isowner)401 ace_make_deny(ace_t *allow, ace_t *deny, int isdir, int isowner)
402 {
403 	(void) memcpy(deny, allow, sizeof (ace_t));
404 
405 	deny->a_who = allow->a_who;
406 
407 	deny->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
408 	deny->a_access_mask ^= ACE_POSIX_SUPPORTED_BITS;
409 	if (isdir)
410 		deny->a_access_mask ^= ACE_DELETE_CHILD;
411 
412 	deny->a_access_mask &= ~(ACE_SYNCHRONIZE | ACE_WRITE_OWNER |
413 	    ACE_DELETE | ACE_WRITE_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
414 	    ACE_WRITE_NAMED_ATTRS);
415 	deny->a_access_mask |= access_mask_set((allow->a_access_mask &
416 	    ACE_WRITE_DATA), (allow->a_access_mask & ACE_READ_DATA), isowner,
417 	    B_FALSE);
418 }
419 /*
420  * Make an initial pass over an array of aclent_t's.  Gather
421  * information such as an ACL_MASK (if any), number of users,
422  * number of groups, and whether the array needs to be sorted.
423  */
424 static int
ln_aent_preprocess(aclent_t * aclent,int n,int * hasmask,mode_t * mask,int * numuser,int * numgroup,int * needsort)425 ln_aent_preprocess(aclent_t *aclent, int n,
426     int *hasmask, mode_t *mask,
427     int *numuser, int *numgroup, int *needsort)
428 {
429 	int error = 0;
430 	int i;
431 	int curtype = 0;
432 
433 	*hasmask = 0;
434 	*mask = 07;
435 	*needsort = 0;
436 	*numuser = 0;
437 	*numgroup = 0;
438 
439 	for (i = 0; i < n; i++) {
440 		if (aclent[i].a_type < curtype)
441 			*needsort = 1;
442 		else if (aclent[i].a_type > curtype)
443 			curtype = aclent[i].a_type;
444 		if (aclent[i].a_type & USER)
445 			(*numuser)++;
446 		if (aclent[i].a_type & (GROUP | GROUP_OBJ))
447 			(*numgroup)++;
448 		if (aclent[i].a_type & CLASS_OBJ) {
449 			if (*hasmask) {
450 				error = EINVAL;
451 				goto out;
452 			} else {
453 				*hasmask = 1;
454 				*mask = aclent[i].a_perm;
455 			}
456 		}
457 	}
458 
459 	if ((! *hasmask) && (*numuser + *numgroup > 1)) {
460 		error = EINVAL;
461 		goto out;
462 	}
463 
464 out:
465 	return (error);
466 }
467 
468 /*
469  * Convert an array of aclent_t into an array of nfsace entries,
470  * following POSIX draft -> nfsv4 conversion semantics as outlined in
471  * the IETF draft.
472  */
473 static int
ln_aent_to_ace(aclent_t * aclent,int n,ace_t ** acepp,int * rescount,int isdir)474 ln_aent_to_ace(aclent_t *aclent, int n, ace_t **acepp, int *rescount, int isdir)
475 {
476 	int error = 0;
477 	mode_t mask;
478 	int numuser, numgroup, needsort;
479 	int resultsize = 0;
480 	int i, groupi = 0, skip;
481 	ace_t *acep, *result = NULL;
482 	int hasmask;
483 
484 	error = ln_aent_preprocess(aclent, n, &hasmask, &mask,
485 	    &numuser, &numgroup, &needsort);
486 	if (error != 0)
487 		goto out;
488 
489 	/* allow + deny for each aclent */
490 	resultsize = n * 2;
491 	if (hasmask) {
492 		/*
493 		 * stick extra deny on the group_obj and on each
494 		 * user|group for the mask (the group_obj was added
495 		 * into the count for numgroup)
496 		 */
497 		resultsize += numuser + numgroup;
498 		/* ... and don't count the mask itself */
499 		resultsize -= 2;
500 	}
501 
502 	/* sort the source if necessary */
503 	if (needsort)
504 		ksort((caddr_t)aclent, n, sizeof (aclent_t), cmp2acls);
505 
506 	if (cacl_malloc((void **)&result, resultsize * sizeof (ace_t)) != 0)
507 		goto out;
508 
509 	acep = result;
510 
511 	for (i = 0; i < n; i++) {
512 		/*
513 		 * don't process CLASS_OBJ (mask); mask was grabbed in
514 		 * ln_aent_preprocess()
515 		 */
516 		if (aclent[i].a_type & CLASS_OBJ)
517 			continue;
518 
519 		/* If we need an ACL_MASK emulator, prepend it now */
520 		if ((hasmask) &&
521 		    (aclent[i].a_type & (USER | GROUP | GROUP_OBJ))) {
522 			acep->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
523 			acep->a_flags = 0;
524 			if (aclent[i].a_type & GROUP_OBJ) {
525 				acep->a_who = (uid_t)-1;
526 				acep->a_flags |=
527 				    (ACE_IDENTIFIER_GROUP|ACE_GROUP);
528 			} else if (aclent[i].a_type & USER) {
529 				acep->a_who = aclent[i].a_id;
530 			} else {
531 				acep->a_who = aclent[i].a_id;
532 				acep->a_flags |= ACE_IDENTIFIER_GROUP;
533 			}
534 			if (aclent[i].a_type & ACL_DEFAULT) {
535 				acep->a_flags |= ACE_INHERIT_ONLY_ACE |
536 				    ACE_FILE_INHERIT_ACE |
537 				    ACE_DIRECTORY_INHERIT_ACE;
538 			}
539 			/*
540 			 * Set the access mask for the prepended deny
541 			 * ace.  To do this, we invert the mask (found
542 			 * in ln_aent_preprocess()) then convert it to an
543 			 * DENY ace access_mask.
544 			 */
545 			acep->a_access_mask = mode_to_ace_access((mask ^ 07),
546 			    isdir, 0, 0);
547 			acep += 1;
548 		}
549 
550 		/* handle a_perm -> access_mask */
551 		acep->a_access_mask = mode_to_ace_access(aclent[i].a_perm,
552 		    isdir, aclent[i].a_type & USER_OBJ, 1);
553 
554 		/* emulate a default aclent */
555 		if (aclent[i].a_type & ACL_DEFAULT) {
556 			acep->a_flags |= ACE_INHERIT_ONLY_ACE |
557 			    ACE_FILE_INHERIT_ACE |
558 			    ACE_DIRECTORY_INHERIT_ACE;
559 		}
560 
561 		/*
562 		 * handle a_perm and a_id
563 		 *
564 		 * this must be done last, since it involves the
565 		 * corresponding deny aces, which are handled
566 		 * differently for each different a_type.
567 		 */
568 		if (aclent[i].a_type & USER_OBJ) {
569 			acep->a_who = (uid_t)-1;
570 			acep->a_flags |= ACE_OWNER;
571 			ace_make_deny(acep, acep + 1, isdir, B_TRUE);
572 			acep += 2;
573 		} else if (aclent[i].a_type & USER) {
574 			acep->a_who = aclent[i].a_id;
575 			ace_make_deny(acep, acep + 1, isdir, B_FALSE);
576 			acep += 2;
577 		} else if (aclent[i].a_type & (GROUP_OBJ | GROUP)) {
578 			if (aclent[i].a_type & GROUP_OBJ) {
579 				acep->a_who = (uid_t)-1;
580 				acep->a_flags |= ACE_GROUP;
581 			} else {
582 				acep->a_who = aclent[i].a_id;
583 			}
584 			acep->a_flags |= ACE_IDENTIFIER_GROUP;
585 			/*
586 			 * Set the corresponding deny for the group ace.
587 			 *
588 			 * The deny aces go after all of the groups, unlike
589 			 * everything else, where they immediately follow
590 			 * the allow ace.
591 			 *
592 			 * We calculate "skip", the number of slots to
593 			 * skip ahead for the deny ace, here.
594 			 *
595 			 * The pattern is:
596 			 * MD1 A1 MD2 A2 MD3 A3 D1 D2 D3
597 			 * thus, skip is
598 			 * (2 * numgroup) - 1 - groupi
599 			 * (2 * numgroup) to account for MD + A
600 			 * - 1 to account for the fact that we're on the
601 			 * access (A), not the mask (MD)
602 			 * - groupi to account for the fact that we have
603 			 * passed up groupi number of MD's.
604 			 */
605 			skip = (2 * numgroup) - 1 - groupi;
606 			ace_make_deny(acep, acep + skip, isdir, B_FALSE);
607 			/*
608 			 * If we just did the last group, skip acep past
609 			 * all of the denies; else, just move ahead one.
610 			 */
611 			if (++groupi >= numgroup)
612 				acep += numgroup + 1;
613 			else
614 				acep += 1;
615 		} else if (aclent[i].a_type & OTHER_OBJ) {
616 			acep->a_who = (uid_t)-1;
617 			acep->a_flags |= ACE_EVERYONE;
618 			ace_make_deny(acep, acep + 1, isdir, B_FALSE);
619 			acep += 2;
620 		} else {
621 			error = EINVAL;
622 			goto out;
623 		}
624 	}
625 
626 	*acepp = result;
627 	*rescount = resultsize;
628 
629 out:
630 	if (error != 0) {
631 		if ((result != NULL) && (resultsize > 0)) {
632 			cacl_free(result, resultsize * sizeof (ace_t));
633 		}
634 	}
635 
636 	return (error);
637 }
638 
639 static int
convert_aent_to_ace(aclent_t * aclentp,int aclcnt,boolean_t isdir,ace_t ** retacep,int * retacecnt)640 convert_aent_to_ace(aclent_t *aclentp, int aclcnt, boolean_t isdir,
641     ace_t **retacep, int *retacecnt)
642 {
643 	ace_t *acep;
644 	ace_t *dfacep;
645 	int acecnt = 0;
646 	int dfacecnt = 0;
647 	int dfaclstart = 0;
648 	int dfaclcnt = 0;
649 	aclent_t *aclp;
650 	int i;
651 	int error;
652 	int acesz, dfacesz;
653 
654 	ksort((caddr_t)aclentp, aclcnt, sizeof (aclent_t), cmp2acls);
655 
656 	for (i = 0, aclp = aclentp; i < aclcnt; aclp++, i++) {
657 		if (aclp->a_type & ACL_DEFAULT)
658 			break;
659 	}
660 
661 	if (i < aclcnt) {
662 		dfaclstart = i;
663 		dfaclcnt = aclcnt - i;
664 	}
665 
666 	if (dfaclcnt && !isdir) {
667 		return (EINVAL);
668 	}
669 
670 	error = ln_aent_to_ace(aclentp, i,  &acep, &acecnt, isdir);
671 	if (error)
672 		return (error);
673 
674 	if (dfaclcnt) {
675 		error = ln_aent_to_ace(&aclentp[dfaclstart], dfaclcnt,
676 		    &dfacep, &dfacecnt, isdir);
677 		if (error) {
678 			if (acep) {
679 				cacl_free(acep, acecnt * sizeof (ace_t));
680 			}
681 			return (error);
682 		}
683 	}
684 
685 	if (dfacecnt != 0) {
686 		acesz = sizeof (ace_t) * acecnt;
687 		dfacesz = sizeof (ace_t) * dfacecnt;
688 		acep = cacl_realloc(acep, acesz, acesz + dfacesz);
689 		if (acep == NULL)
690 			return (ENOMEM);
691 		if (dfaclcnt) {
692 			(void) memcpy(acep + acecnt, dfacep, dfacesz);
693 		}
694 	}
695 	if (dfaclcnt)
696 		cacl_free(dfacep, dfacecnt * sizeof (ace_t));
697 
698 	*retacecnt = acecnt + dfacecnt;
699 	*retacep = acep;
700 	return (0);
701 }
702 
703 static int
ace_mask_to_mode(uint32_t mask,o_mode_t * modep,boolean_t isdir)704 ace_mask_to_mode(uint32_t  mask, o_mode_t *modep, boolean_t isdir)
705 {
706 	int error = 0;
707 	o_mode_t mode = 0;
708 	uint32_t bits, wantbits;
709 
710 	/* read */
711 	if (mask & ACE_READ_DATA)
712 		mode |= S_IROTH;
713 
714 	/* write */
715 	wantbits = (ACE_WRITE_DATA | ACE_APPEND_DATA);
716 	if (isdir)
717 		wantbits |= ACE_DELETE_CHILD;
718 	bits = mask & wantbits;
719 	if (bits != 0) {
720 		if (bits != wantbits) {
721 			error = ENOTSUP;
722 			goto out;
723 		}
724 		mode |= S_IWOTH;
725 	}
726 
727 	/* exec */
728 	if (mask & ACE_EXECUTE) {
729 		mode |= S_IXOTH;
730 	}
731 
732 	*modep = mode;
733 
734 out:
735 	return (error);
736 }
737 
738 static void
acevals_init(acevals_t * vals,uid_t key)739 acevals_init(acevals_t *vals, uid_t key)
740 {
741 	memset(vals, 0, sizeof (*vals));
742 	vals->allowed = ACE_MASK_UNDEFINED;
743 	vals->denied = ACE_MASK_UNDEFINED;
744 	vals->mask = ACE_MASK_UNDEFINED;
745 	vals->key = key;
746 }
747 
748 static void
ace_list_init(ace_list_t * al,int dfacl_flag)749 ace_list_init(ace_list_t *al, int dfacl_flag)
750 {
751 	acevals_init(&al->user_obj, 0);
752 	acevals_init(&al->group_obj, 0);
753 	acevals_init(&al->other_obj, 0);
754 	al->numusers = 0;
755 	al->numgroups = 0;
756 	al->acl_mask = 0;
757 	al->hasmask = 0;
758 	al->state = ace_unused;
759 	al->seen = 0;
760 	al->dfacl_flag = dfacl_flag;
761 }
762 
763 /*
764  * Find or create an acevals holder for a given id and avl tree.
765  *
766  * Note that only one thread will ever touch these avl trees, so
767  * there is no need for locking.
768  */
769 static acevals_t *
acevals_find(ace_t * ace,avl_tree_t * avl,int * num)770 acevals_find(ace_t *ace, avl_tree_t *avl, int *num)
771 {
772 	acevals_t key, *rc;
773 	avl_index_t where;
774 
775 	key.key = ace->a_who;
776 	rc = avl_find(avl, &key, &where);
777 	if (rc != NULL)
778 		return (rc);
779 
780 	/* this memory is freed by ln_ace_to_aent()->ace_list_free() */
781 	if (cacl_malloc((void **)&rc, sizeof (acevals_t)) != 0)
782 		return (NULL);
783 
784 	acevals_init(rc, ace->a_who);
785 	avl_insert(avl, rc, where);
786 	(*num)++;
787 
788 	return (rc);
789 }
790 
791 static int
access_mask_check(ace_t * acep,int mask_bit,int isowner)792 access_mask_check(ace_t *acep, int mask_bit, int isowner)
793 {
794 	int set_deny, err_deny;
795 	int set_allow, err_allow;
796 	int acl_consume;
797 	int haswriteperm, hasreadperm;
798 
799 	if (acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) {
800 		haswriteperm = (acep->a_access_mask & ACE_WRITE_DATA) ? 0 : 1;
801 		hasreadperm = (acep->a_access_mask & ACE_READ_DATA) ? 0 : 1;
802 	} else {
803 		haswriteperm = (acep->a_access_mask & ACE_WRITE_DATA) ? 1 : 0;
804 		hasreadperm = (acep->a_access_mask & ACE_READ_DATA) ? 1 : 0;
805 	}
806 
807 	acl_consume = (ACL_SYNCHRONIZE_ERR_DENY |
808 	    ACL_DELETE_ERR_DENY |
809 	    ACL_WRITE_OWNER_ERR_DENY |
810 	    ACL_WRITE_OWNER_ERR_ALLOW |
811 	    ACL_WRITE_ATTRS_OWNER_SET_ALLOW |
812 	    ACL_WRITE_ATTRS_OWNER_ERR_DENY |
813 	    ACL_WRITE_ATTRS_WRITER_SET_DENY |
814 	    ACL_WRITE_ATTRS_WRITER_ERR_ALLOW |
815 	    ACL_WRITE_NAMED_WRITER_ERR_DENY |
816 	    ACL_READ_NAMED_READER_ERR_DENY);
817 
818 	if (mask_bit == ACE_SYNCHRONIZE) {
819 		set_deny = ACL_SYNCHRONIZE_SET_DENY;
820 		err_deny =  ACL_SYNCHRONIZE_ERR_DENY;
821 		set_allow = ACL_SYNCHRONIZE_SET_ALLOW;
822 		err_allow = ACL_SYNCHRONIZE_ERR_ALLOW;
823 	} else if (mask_bit == ACE_WRITE_OWNER) {
824 		set_deny = ACL_WRITE_OWNER_SET_DENY;
825 		err_deny =  ACL_WRITE_OWNER_ERR_DENY;
826 		set_allow = ACL_WRITE_OWNER_SET_ALLOW;
827 		err_allow = ACL_WRITE_OWNER_ERR_ALLOW;
828 	} else if (mask_bit == ACE_DELETE) {
829 		set_deny = ACL_DELETE_SET_DENY;
830 		err_deny =  ACL_DELETE_ERR_DENY;
831 		set_allow = ACL_DELETE_SET_ALLOW;
832 		err_allow = ACL_DELETE_ERR_ALLOW;
833 	} else if (mask_bit == ACE_WRITE_ATTRIBUTES) {
834 		if (isowner) {
835 			set_deny = ACL_WRITE_ATTRS_OWNER_SET_DENY;
836 			err_deny =  ACL_WRITE_ATTRS_OWNER_ERR_DENY;
837 			set_allow = ACL_WRITE_ATTRS_OWNER_SET_ALLOW;
838 			err_allow = ACL_WRITE_ATTRS_OWNER_ERR_ALLOW;
839 		} else if (haswriteperm) {
840 			set_deny = ACL_WRITE_ATTRS_WRITER_SET_DENY;
841 			err_deny =  ACL_WRITE_ATTRS_WRITER_ERR_DENY;
842 			set_allow = ACL_WRITE_ATTRS_WRITER_SET_ALLOW;
843 			err_allow = ACL_WRITE_ATTRS_WRITER_ERR_ALLOW;
844 		} else {
845 			if ((acep->a_access_mask & mask_bit) &&
846 			    (acep->a_type & ACE_ACCESS_ALLOWED_ACE_TYPE)) {
847 				return (ENOTSUP);
848 			}
849 			return (0);
850 		}
851 	} else if (mask_bit == ACE_READ_NAMED_ATTRS) {
852 		if (!hasreadperm)
853 			return (0);
854 
855 		set_deny = ACL_READ_NAMED_READER_SET_DENY;
856 		err_deny = ACL_READ_NAMED_READER_ERR_DENY;
857 		set_allow = ACL_READ_NAMED_READER_SET_ALLOW;
858 		err_allow = ACL_READ_NAMED_READER_ERR_ALLOW;
859 	} else if (mask_bit == ACE_WRITE_NAMED_ATTRS) {
860 		if (!haswriteperm)
861 			return (0);
862 
863 		set_deny = ACL_WRITE_NAMED_WRITER_SET_DENY;
864 		err_deny = ACL_WRITE_NAMED_WRITER_ERR_DENY;
865 		set_allow = ACL_WRITE_NAMED_WRITER_SET_ALLOW;
866 		err_allow = ACL_WRITE_NAMED_WRITER_ERR_ALLOW;
867 	} else {
868 		return (EINVAL);
869 	}
870 
871 	if (acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) {
872 		if (acl_consume & set_deny) {
873 			if (!(acep->a_access_mask & mask_bit)) {
874 				return (ENOTSUP);
875 			}
876 		} else if (acl_consume & err_deny) {
877 			if (acep->a_access_mask & mask_bit) {
878 				return (ENOTSUP);
879 			}
880 		}
881 	} else {
882 		/* ACE_ACCESS_ALLOWED_ACE_TYPE */
883 		if (acl_consume & set_allow) {
884 			if (!(acep->a_access_mask & mask_bit)) {
885 				return (ENOTSUP);
886 			}
887 		} else if (acl_consume & err_allow) {
888 			if (acep->a_access_mask & mask_bit) {
889 				return (ENOTSUP);
890 			}
891 		}
892 	}
893 	return (0);
894 }
895 
896 static int
ace_to_aent_legal(ace_t * acep)897 ace_to_aent_legal(ace_t *acep)
898 {
899 	int error = 0;
900 	int isowner;
901 
902 	/* only ALLOW or DENY */
903 	if ((acep->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE) &&
904 	    (acep->a_type != ACE_ACCESS_DENIED_ACE_TYPE)) {
905 		error = ENOTSUP;
906 		goto out;
907 	}
908 
909 	/* check for invalid flags */
910 	if (acep->a_flags & ~(ACE_VALID_FLAG_BITS)) {
911 		error = EINVAL;
912 		goto out;
913 	}
914 
915 	/* some flags are illegal */
916 	if (acep->a_flags & (ACE_SUCCESSFUL_ACCESS_ACE_FLAG |
917 	    ACE_FAILED_ACCESS_ACE_FLAG |
918 	    ACE_NO_PROPAGATE_INHERIT_ACE)) {
919 		error = ENOTSUP;
920 		goto out;
921 	}
922 
923 	/* check for invalid masks */
924 	if (acep->a_access_mask & ~(ACE_VALID_MASK_BITS)) {
925 		error = EINVAL;
926 		goto out;
927 	}
928 
929 	if ((acep->a_flags & ACE_OWNER)) {
930 		isowner = 1;
931 	} else {
932 		isowner = 0;
933 	}
934 
935 	error = access_mask_check(acep, ACE_SYNCHRONIZE, isowner);
936 	if (error)
937 		goto out;
938 
939 	error = access_mask_check(acep, ACE_WRITE_OWNER, isowner);
940 	if (error)
941 		goto out;
942 
943 	error = access_mask_check(acep, ACE_DELETE, isowner);
944 	if (error)
945 		goto out;
946 
947 	error = access_mask_check(acep, ACE_WRITE_ATTRIBUTES, isowner);
948 	if (error)
949 		goto out;
950 
951 	error = access_mask_check(acep, ACE_READ_NAMED_ATTRS, isowner);
952 	if (error)
953 		goto out;
954 
955 	error = access_mask_check(acep, ACE_WRITE_NAMED_ATTRS, isowner);
956 	if (error)
957 		goto out;
958 
959 	/* more detailed checking of masks */
960 	if (acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
961 		if (! (acep->a_access_mask & ACE_READ_ATTRIBUTES)) {
962 			error = ENOTSUP;
963 			goto out;
964 		}
965 		if ((acep->a_access_mask & ACE_WRITE_DATA) &&
966 		    (! (acep->a_access_mask & ACE_APPEND_DATA))) {
967 			error = ENOTSUP;
968 			goto out;
969 		}
970 		if ((! (acep->a_access_mask & ACE_WRITE_DATA)) &&
971 		    (acep->a_access_mask & ACE_APPEND_DATA)) {
972 			error = ENOTSUP;
973 			goto out;
974 		}
975 	}
976 
977 	/* ACL enforcement */
978 	if ((acep->a_access_mask & ACE_READ_ACL) &&
979 	    (acep->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE)) {
980 		error = ENOTSUP;
981 		goto out;
982 	}
983 	if (acep->a_access_mask & ACE_WRITE_ACL) {
984 		if ((acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) &&
985 		    (isowner)) {
986 			error = ENOTSUP;
987 			goto out;
988 		}
989 		if ((acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) &&
990 		    (! isowner)) {
991 			error = ENOTSUP;
992 			goto out;
993 		}
994 	}
995 
996 out:
997 	return (error);
998 }
999 
1000 static int
ace_allow_to_mode(uint32_t mask,o_mode_t * modep,boolean_t isdir)1001 ace_allow_to_mode(uint32_t mask, o_mode_t *modep, boolean_t isdir)
1002 {
1003 	/* ACE_READ_ACL and ACE_READ_ATTRIBUTES must both be set */
1004 	if ((mask & (ACE_READ_ACL | ACE_READ_ATTRIBUTES)) !=
1005 	    (ACE_READ_ACL | ACE_READ_ATTRIBUTES)) {
1006 		return (ENOTSUP);
1007 	}
1008 
1009 	return (ace_mask_to_mode(mask, modep, isdir));
1010 }
1011 
1012 static int
acevals_to_aent(acevals_t * vals,aclent_t * dest,ace_list_t * list,uid_t owner,gid_t group,boolean_t isdir)1013 acevals_to_aent(acevals_t *vals, aclent_t *dest, ace_list_t *list,
1014     uid_t owner, gid_t group, boolean_t isdir)
1015 {
1016 	int error;
1017 	uint32_t  flips = ACE_POSIX_SUPPORTED_BITS;
1018 
1019 	if (isdir)
1020 		flips |= ACE_DELETE_CHILD;
1021 	if (vals->allowed != (vals->denied ^ flips)) {
1022 		error = ENOTSUP;
1023 		goto out;
1024 	}
1025 	if ((list->hasmask) && (list->acl_mask != vals->mask) &&
1026 	    (vals->aent_type & (USER | GROUP | GROUP_OBJ))) {
1027 		error = ENOTSUP;
1028 		goto out;
1029 	}
1030 	error = ace_allow_to_mode(vals->allowed, &dest->a_perm, isdir);
1031 	if (error != 0)
1032 		goto out;
1033 	dest->a_type = vals->aent_type;
1034 	if (dest->a_type & (USER | GROUP)) {
1035 		dest->a_id = vals->key;
1036 	} else if (dest->a_type & USER_OBJ) {
1037 		dest->a_id = owner;
1038 	} else if (dest->a_type & GROUP_OBJ) {
1039 		dest->a_id = group;
1040 	} else if (dest->a_type & OTHER_OBJ) {
1041 		dest->a_id = 0;
1042 	} else {
1043 		error = EINVAL;
1044 		goto out;
1045 	}
1046 
1047 out:
1048 	return (error);
1049 }
1050 
1051 
1052 static int
ace_list_to_aent(ace_list_t * list,aclent_t ** aclentp,int * aclcnt,uid_t owner,gid_t group,boolean_t isdir)1053 ace_list_to_aent(ace_list_t *list, aclent_t **aclentp, int *aclcnt,
1054     uid_t owner, gid_t group, boolean_t isdir)
1055 {
1056 	int error = 0;
1057 	aclent_t *aent, *result = NULL;
1058 	acevals_t *vals;
1059 	int resultcount;
1060 
1061 	if ((list->seen & (USER_OBJ | GROUP_OBJ | OTHER_OBJ)) !=
1062 	    (USER_OBJ | GROUP_OBJ | OTHER_OBJ)) {
1063 		error = ENOTSUP;
1064 		goto out;
1065 	}
1066 	if ((! list->hasmask) && (list->numusers + list->numgroups > 0)) {
1067 		error = ENOTSUP;
1068 		goto out;
1069 	}
1070 
1071 	resultcount = 3 + list->numusers + list->numgroups;
1072 	/*
1073 	 * This must be the same condition as below, when we add the CLASS_OBJ
1074 	 * (aka ACL mask)
1075 	 */
1076 	if ((list->hasmask) || (! list->dfacl_flag))
1077 		resultcount += 1;
1078 
1079 	if (cacl_malloc((void **)&result,
1080 	    resultcount * sizeof (aclent_t)) != 0) {
1081 		error = ENOMEM;
1082 		goto out;
1083 	}
1084 	aent = result;
1085 
1086 	/* USER_OBJ */
1087 	if (!(list->user_obj.aent_type & USER_OBJ)) {
1088 		error = EINVAL;
1089 		goto out;
1090 	}
1091 
1092 	error = acevals_to_aent(&list->user_obj, aent, list, owner, group,
1093 	    isdir);
1094 
1095 	if (error != 0)
1096 		goto out;
1097 	++aent;
1098 	/* USER */
1099 	vals = NULL;
1100 	for (vals = avl_first(&list->user); vals != NULL;
1101 	    vals = AVL_NEXT(&list->user, vals)) {
1102 		if (!(vals->aent_type & USER)) {
1103 			error = EINVAL;
1104 			goto out;
1105 		}
1106 		error = acevals_to_aent(vals, aent, list, owner, group,
1107 		    isdir);
1108 		if (error != 0)
1109 			goto out;
1110 		++aent;
1111 	}
1112 	/* GROUP_OBJ */
1113 	if (!(list->group_obj.aent_type & GROUP_OBJ)) {
1114 		error = EINVAL;
1115 		goto out;
1116 	}
1117 	error = acevals_to_aent(&list->group_obj, aent, list, owner, group,
1118 	    isdir);
1119 	if (error != 0)
1120 		goto out;
1121 	++aent;
1122 	/* GROUP */
1123 	vals = NULL;
1124 	for (vals = avl_first(&list->group); vals != NULL;
1125 	    vals = AVL_NEXT(&list->group, vals)) {
1126 		if (!(vals->aent_type & GROUP)) {
1127 			error = EINVAL;
1128 			goto out;
1129 		}
1130 		error = acevals_to_aent(vals, aent, list, owner, group,
1131 		    isdir);
1132 		if (error != 0)
1133 			goto out;
1134 		++aent;
1135 	}
1136 	/*
1137 	 * CLASS_OBJ (aka ACL_MASK)
1138 	 *
1139 	 * An ACL_MASK is not fabricated if the ACL is a default ACL.
1140 	 * This is to follow UFS's behavior.
1141 	 */
1142 	if ((list->hasmask) || (! list->dfacl_flag)) {
1143 		if (list->hasmask) {
1144 			uint32_t flips = ACE_POSIX_SUPPORTED_BITS;
1145 			if (isdir)
1146 				flips |= ACE_DELETE_CHILD;
1147 			error = ace_mask_to_mode(list->acl_mask ^ flips,
1148 			    &aent->a_perm, isdir);
1149 			if (error != 0)
1150 				goto out;
1151 		} else {
1152 			/* fabricate the ACL_MASK from the group permissions */
1153 			error = ace_mask_to_mode(list->group_obj.allowed,
1154 			    &aent->a_perm, isdir);
1155 			if (error != 0)
1156 				goto out;
1157 		}
1158 		aent->a_id = 0;
1159 		aent->a_type = CLASS_OBJ | list->dfacl_flag;
1160 		++aent;
1161 	}
1162 	/* OTHER_OBJ */
1163 	if (!(list->other_obj.aent_type & OTHER_OBJ)) {
1164 		error = EINVAL;
1165 		goto out;
1166 	}
1167 	error = acevals_to_aent(&list->other_obj, aent, list, owner, group,
1168 	    isdir);
1169 	if (error != 0)
1170 		goto out;
1171 	++aent;
1172 
1173 	*aclentp = result;
1174 	*aclcnt = resultcount;
1175 
1176 out:
1177 	if (error != 0) {
1178 		if (result != NULL)
1179 			cacl_free(result, resultcount * sizeof (aclent_t));
1180 	}
1181 
1182 	return (error);
1183 }
1184 
1185 
1186 /*
1187  * free all data associated with an ace_list
1188  */
1189 static void
ace_list_free(ace_list_t * al)1190 ace_list_free(ace_list_t *al)
1191 {
1192 	acevals_t *node;
1193 	void *cookie;
1194 
1195 	if (al == NULL)
1196 		return;
1197 
1198 	cookie = NULL;
1199 	while ((node = avl_destroy_nodes(&al->user, &cookie)) != NULL)
1200 		cacl_free(node, sizeof (acevals_t));
1201 	cookie = NULL;
1202 	while ((node = avl_destroy_nodes(&al->group, &cookie)) != NULL)
1203 		cacl_free(node, sizeof (acevals_t));
1204 
1205 	avl_destroy(&al->user);
1206 	avl_destroy(&al->group);
1207 
1208 	/* free the container itself */
1209 	cacl_free(al, sizeof (ace_list_t));
1210 }
1211 
1212 static int
acevals_compare(const void * va,const void * vb)1213 acevals_compare(const void *va, const void *vb)
1214 {
1215 	const acevals_t *a = va, *b = vb;
1216 	return (TREE_CMP(a->key, b->key));
1217 }
1218 
1219 /*
1220  * Convert a list of ace_t entries to equivalent regular and default
1221  * aclent_t lists.  Return error (ENOTSUP) when conversion is not possible.
1222  */
1223 static int
ln_ace_to_aent(ace_t * ace,int n,uid_t owner,gid_t group,aclent_t ** aclentp,int * aclcnt,aclent_t ** dfaclentp,int * dfaclcnt,boolean_t isdir)1224 ln_ace_to_aent(ace_t *ace, int n, uid_t owner, gid_t group,
1225     aclent_t **aclentp, int *aclcnt, aclent_t **dfaclentp, int *dfaclcnt,
1226     boolean_t isdir)
1227 {
1228 	int error = 0;
1229 	ace_t *acep;
1230 	uint32_t bits;
1231 	int i;
1232 	ace_list_t *normacl = NULL, *dfacl = NULL, *acl;
1233 	acevals_t *vals;
1234 
1235 	*aclentp = NULL;
1236 	*aclcnt = 0;
1237 	*dfaclentp = NULL;
1238 	*dfaclcnt = 0;
1239 
1240 	/* we need at least user_obj, group_obj, and other_obj */
1241 	if (n < 6) {
1242 		error = ENOTSUP;
1243 		goto out;
1244 	}
1245 	if (ace == NULL) {
1246 		error = EINVAL;
1247 		goto out;
1248 	}
1249 
1250 	error = cacl_malloc((void **)&normacl, sizeof (ace_list_t));
1251 	if (error != 0)
1252 		goto out;
1253 
1254 	avl_create(&normacl->user, acevals_compare, sizeof (acevals_t),
1255 	    offsetof(acevals_t, avl));
1256 	avl_create(&normacl->group, acevals_compare, sizeof (acevals_t),
1257 	    offsetof(acevals_t, avl));
1258 
1259 	ace_list_init(normacl, 0);
1260 
1261 	error = cacl_malloc((void **)&dfacl, sizeof (ace_list_t));
1262 	if (error != 0)
1263 		goto out;
1264 
1265 	avl_create(&dfacl->user, acevals_compare, sizeof (acevals_t),
1266 	    offsetof(acevals_t, avl));
1267 	avl_create(&dfacl->group, acevals_compare, sizeof (acevals_t),
1268 	    offsetof(acevals_t, avl));
1269 	ace_list_init(dfacl, ACL_DEFAULT);
1270 
1271 	/* process every ace_t... */
1272 	for (i = 0; i < n; i++) {
1273 		acep = &ace[i];
1274 
1275 		/* rule out certain cases quickly */
1276 		error = ace_to_aent_legal(acep);
1277 		if (error != 0)
1278 			goto out;
1279 
1280 		/*
1281 		 * Turn off these bits in order to not have to worry about
1282 		 * them when doing the checks for compliments.
1283 		 */
1284 		acep->a_access_mask &= ~(ACE_WRITE_OWNER | ACE_DELETE |
1285 		    ACE_SYNCHRONIZE | ACE_WRITE_ATTRIBUTES |
1286 		    ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS);
1287 
1288 		/* see if this should be a regular or default acl */
1289 		bits = acep->a_flags &
1290 		    (ACE_INHERIT_ONLY_ACE |
1291 		    ACE_FILE_INHERIT_ACE |
1292 		    ACE_DIRECTORY_INHERIT_ACE);
1293 		if (bits != 0) {
1294 			/* all or nothing on these inherit bits */
1295 			if (bits != (ACE_INHERIT_ONLY_ACE |
1296 			    ACE_FILE_INHERIT_ACE |
1297 			    ACE_DIRECTORY_INHERIT_ACE)) {
1298 				error = ENOTSUP;
1299 				goto out;
1300 			}
1301 			acl = dfacl;
1302 		} else {
1303 			acl = normacl;
1304 		}
1305 
1306 		if ((acep->a_flags & ACE_OWNER)) {
1307 			if (acl->state > ace_user_obj) {
1308 				error = ENOTSUP;
1309 				goto out;
1310 			}
1311 			acl->state = ace_user_obj;
1312 			acl->seen |= USER_OBJ;
1313 			vals = &acl->user_obj;
1314 			vals->aent_type = USER_OBJ | acl->dfacl_flag;
1315 		} else if ((acep->a_flags & ACE_EVERYONE)) {
1316 			acl->state = ace_other_obj;
1317 			acl->seen |= OTHER_OBJ;
1318 			vals = &acl->other_obj;
1319 			vals->aent_type = OTHER_OBJ | acl->dfacl_flag;
1320 		} else if (acep->a_flags & ACE_IDENTIFIER_GROUP) {
1321 			if (acl->state > ace_group) {
1322 				error = ENOTSUP;
1323 				goto out;
1324 			}
1325 			if ((acep->a_flags & ACE_GROUP)) {
1326 				acl->seen |= GROUP_OBJ;
1327 				vals = &acl->group_obj;
1328 				vals->aent_type = GROUP_OBJ | acl->dfacl_flag;
1329 			} else {
1330 				acl->seen |= GROUP;
1331 				vals = acevals_find(acep, &acl->group,
1332 				    &acl->numgroups);
1333 				if (vals == NULL) {
1334 					error = ENOMEM;
1335 					goto out;
1336 				}
1337 				vals->aent_type = GROUP | acl->dfacl_flag;
1338 			}
1339 			acl->state = ace_group;
1340 		} else {
1341 			if (acl->state > ace_user) {
1342 				error = ENOTSUP;
1343 				goto out;
1344 			}
1345 			acl->state = ace_user;
1346 			acl->seen |= USER;
1347 			vals = acevals_find(acep, &acl->user,
1348 			    &acl->numusers);
1349 			if (vals == NULL) {
1350 				error = ENOMEM;
1351 				goto out;
1352 			}
1353 			vals->aent_type = USER | acl->dfacl_flag;
1354 		}
1355 
1356 		if (!(acl->state > ace_unused)) {
1357 			error = EINVAL;
1358 			goto out;
1359 		}
1360 
1361 		if (acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
1362 			/* no more than one allowed per aclent_t */
1363 			if (vals->allowed != ACE_MASK_UNDEFINED) {
1364 				error = ENOTSUP;
1365 				goto out;
1366 			}
1367 			vals->allowed = acep->a_access_mask;
1368 		} else {
1369 			/*
1370 			 * it's a DENY; if there was a previous DENY, it
1371 			 * must have been an ACL_MASK.
1372 			 */
1373 			if (vals->denied != ACE_MASK_UNDEFINED) {
1374 				/* ACL_MASK is for USER and GROUP only */
1375 				if ((acl->state != ace_user) &&
1376 				    (acl->state != ace_group)) {
1377 					error = ENOTSUP;
1378 					goto out;
1379 				}
1380 
1381 				if (! acl->hasmask) {
1382 					acl->hasmask = 1;
1383 					acl->acl_mask = vals->denied;
1384 				/* check for mismatched ACL_MASK emulations */
1385 				} else if (acl->acl_mask != vals->denied) {
1386 					error = ENOTSUP;
1387 					goto out;
1388 				}
1389 				vals->mask = vals->denied;
1390 			}
1391 			vals->denied = acep->a_access_mask;
1392 		}
1393 	}
1394 
1395 	/* done collating; produce the aclent_t lists */
1396 	if (normacl->state != ace_unused) {
1397 		error = ace_list_to_aent(normacl, aclentp, aclcnt,
1398 		    owner, group, isdir);
1399 		if (error != 0) {
1400 			goto out;
1401 		}
1402 	}
1403 	if (dfacl->state != ace_unused) {
1404 		error = ace_list_to_aent(dfacl, dfaclentp, dfaclcnt,
1405 		    owner, group, isdir);
1406 		if (error != 0) {
1407 			goto out;
1408 		}
1409 	}
1410 
1411 out:
1412 	if (normacl != NULL)
1413 		ace_list_free(normacl);
1414 	if (dfacl != NULL)
1415 		ace_list_free(dfacl);
1416 
1417 	return (error);
1418 }
1419 
1420 static int
convert_ace_to_aent(ace_t * acebufp,int acecnt,boolean_t isdir,uid_t owner,gid_t group,aclent_t ** retaclentp,int * retaclcnt)1421 convert_ace_to_aent(ace_t *acebufp, int acecnt, boolean_t isdir,
1422     uid_t owner, gid_t group, aclent_t **retaclentp, int *retaclcnt)
1423 {
1424 	int error = 0;
1425 	aclent_t *aclentp, *dfaclentp;
1426 	int aclcnt, dfaclcnt;
1427 	int aclsz, dfaclsz;
1428 
1429 	error = ln_ace_to_aent(acebufp, acecnt, owner, group,
1430 	    &aclentp, &aclcnt, &dfaclentp, &dfaclcnt, isdir);
1431 
1432 	if (error)
1433 		return (error);
1434 
1435 
1436 	if (dfaclcnt != 0) {
1437 		/*
1438 		 * Slap aclentp and dfaclentp into a single array.
1439 		 */
1440 		aclsz = sizeof (aclent_t) * aclcnt;
1441 		dfaclsz = sizeof (aclent_t) * dfaclcnt;
1442 		aclentp = cacl_realloc(aclentp, aclsz, aclsz + dfaclsz);
1443 		if (aclentp != NULL) {
1444 			(void) memcpy(aclentp + aclcnt, dfaclentp, dfaclsz);
1445 		} else {
1446 			error = ENOMEM;
1447 		}
1448 	}
1449 
1450 	if (aclentp) {
1451 		*retaclentp = aclentp;
1452 		*retaclcnt = aclcnt + dfaclcnt;
1453 	}
1454 
1455 	if (dfaclentp)
1456 		cacl_free(dfaclentp, dfaclsz);
1457 
1458 	return (error);
1459 }
1460 
1461 
1462 int
acl_translate(acl_t * aclp,int target_flavor,boolean_t isdir,uid_t owner,gid_t group)1463 acl_translate(acl_t *aclp, int target_flavor, boolean_t isdir, uid_t owner,
1464     gid_t group)
1465 {
1466 	int aclcnt;
1467 	void *acldata;
1468 	int error;
1469 
1470 	/*
1471 	 * See if we need to translate
1472 	 */
1473 	if ((target_flavor == _ACL_ACE_ENABLED && aclp->acl_type == ACE_T) ||
1474 	    (target_flavor == _ACL_ACLENT_ENABLED &&
1475 	    aclp->acl_type == ACLENT_T))
1476 		return (0);
1477 
1478 	if (target_flavor == -1) {
1479 		error = EINVAL;
1480 		goto out;
1481 	}
1482 
1483 	if (target_flavor ==  _ACL_ACE_ENABLED &&
1484 	    aclp->acl_type == ACLENT_T) {
1485 		error = convert_aent_to_ace(aclp->acl_aclp,
1486 		    aclp->acl_cnt, isdir, (ace_t **)&acldata, &aclcnt);
1487 		if (error)
1488 			goto out;
1489 
1490 	} else if (target_flavor == _ACL_ACLENT_ENABLED &&
1491 	    aclp->acl_type == ACE_T) {
1492 		error = convert_ace_to_aent(aclp->acl_aclp, aclp->acl_cnt,
1493 		    isdir, owner, group, (aclent_t **)&acldata, &aclcnt);
1494 		if (error)
1495 			goto out;
1496 	} else {
1497 		error = ENOTSUP;
1498 		goto out;
1499 	}
1500 
1501 	/*
1502 	 * replace old acl with newly translated acl
1503 	 */
1504 	cacl_free(aclp->acl_aclp, aclp->acl_cnt * aclp->acl_entry_size);
1505 	aclp->acl_aclp = acldata;
1506 	aclp->acl_cnt = aclcnt;
1507 	if (target_flavor == _ACL_ACE_ENABLED) {
1508 		aclp->acl_type = ACE_T;
1509 		aclp->acl_entry_size = sizeof (ace_t);
1510 	} else {
1511 		aclp->acl_type = ACLENT_T;
1512 		aclp->acl_entry_size = sizeof (aclent_t);
1513 	}
1514 	return (0);
1515 
1516 out:
1517 
1518 #if !defined(_KERNEL)
1519 	errno = error;
1520 	return (-1);
1521 #else
1522 	return (error);
1523 #endif
1524 }
1525 #endif /* !_KERNEL */
1526 
1527 #define	SET_ACE(acl, index, who, mask, type, flags) { \
1528 	acl[0][index].a_who = (uint32_t)who; \
1529 	acl[0][index].a_type = type; \
1530 	acl[0][index].a_flags = flags; \
1531 	acl[0][index++].a_access_mask = mask; \
1532 }
1533 
1534 void
acl_trivial_access_masks(mode_t mode,boolean_t isdir,trivial_acl_t * masks)1535 acl_trivial_access_masks(mode_t mode, boolean_t isdir, trivial_acl_t *masks)
1536 {
1537 	uint32_t read_mask = ACE_READ_DATA;
1538 	uint32_t write_mask = ACE_WRITE_DATA|ACE_APPEND_DATA;
1539 	uint32_t execute_mask = ACE_EXECUTE;
1540 
1541 	(void) isdir;	/* will need this later */
1542 
1543 	masks->deny1 = 0;
1544 	if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
1545 		masks->deny1 |= read_mask;
1546 	if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH)))
1547 		masks->deny1 |= write_mask;
1548 	if (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))
1549 		masks->deny1 |= execute_mask;
1550 
1551 	masks->deny2 = 0;
1552 	if (!(mode & S_IRGRP) && (mode & S_IROTH))
1553 		masks->deny2 |= read_mask;
1554 	if (!(mode & S_IWGRP) && (mode & S_IWOTH))
1555 		masks->deny2 |= write_mask;
1556 	if (!(mode & S_IXGRP) && (mode & S_IXOTH))
1557 		masks->deny2 |= execute_mask;
1558 
1559 	masks->allow0 = 0;
1560 	if ((mode & S_IRUSR) && (!(mode & S_IRGRP) && (mode & S_IROTH)))
1561 		masks->allow0 |= read_mask;
1562 	if ((mode & S_IWUSR) && (!(mode & S_IWGRP) && (mode & S_IWOTH)))
1563 		masks->allow0 |= write_mask;
1564 	if ((mode & S_IXUSR) && (!(mode & S_IXGRP) && (mode & S_IXOTH)))
1565 		masks->allow0 |= execute_mask;
1566 
1567 	masks->owner = ACE_WRITE_ATTRIBUTES|ACE_WRITE_OWNER|ACE_WRITE_ACL|
1568 	    ACE_WRITE_NAMED_ATTRS|ACE_READ_ACL|ACE_READ_ATTRIBUTES|
1569 	    ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE;
1570 	if (mode & S_IRUSR)
1571 		masks->owner |= read_mask;
1572 	if (mode & S_IWUSR)
1573 		masks->owner |= write_mask;
1574 	if (mode & S_IXUSR)
1575 		masks->owner |= execute_mask;
1576 
1577 	masks->group = ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_NAMED_ATTRS|
1578 	    ACE_SYNCHRONIZE;
1579 	if (mode & S_IRGRP)
1580 		masks->group |= read_mask;
1581 	if (mode & S_IWGRP)
1582 		masks->group |= write_mask;
1583 	if (mode & S_IXGRP)
1584 		masks->group |= execute_mask;
1585 
1586 	masks->everyone = ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_NAMED_ATTRS|
1587 	    ACE_SYNCHRONIZE;
1588 	if (mode & S_IROTH)
1589 		masks->everyone |= read_mask;
1590 	if (mode & S_IWOTH)
1591 		masks->everyone |= write_mask;
1592 	if (mode & S_IXOTH)
1593 		masks->everyone |= execute_mask;
1594 }
1595 
1596 int
acl_trivial_create(mode_t mode,boolean_t isdir,ace_t ** acl,int * count)1597 acl_trivial_create(mode_t mode, boolean_t isdir, ace_t **acl, int *count)
1598 {
1599 	int		index = 0;
1600 	int		error;
1601 	trivial_acl_t	masks;
1602 
1603 	*count = 3;
1604 	acl_trivial_access_masks(mode, isdir, &masks);
1605 
1606 	if (masks.allow0)
1607 		(*count)++;
1608 	if (masks.deny1)
1609 		(*count)++;
1610 	if (masks.deny2)
1611 		(*count)++;
1612 
1613 	if ((error = cacl_malloc((void **)acl, *count * sizeof (ace_t))) != 0)
1614 		return (error);
1615 
1616 	if (masks.allow0) {
1617 		SET_ACE(acl, index, -1, masks.allow0,
1618 		    ACE_ACCESS_ALLOWED_ACE_TYPE, ACE_OWNER);
1619 	}
1620 	if (masks.deny1) {
1621 		SET_ACE(acl, index, -1, masks.deny1,
1622 		    ACE_ACCESS_DENIED_ACE_TYPE, ACE_OWNER);
1623 	}
1624 	if (masks.deny2) {
1625 		SET_ACE(acl, index, -1, masks.deny2,
1626 		    ACE_ACCESS_DENIED_ACE_TYPE, ACE_GROUP|ACE_IDENTIFIER_GROUP);
1627 	}
1628 
1629 	SET_ACE(acl, index, -1, masks.owner, ACE_ACCESS_ALLOWED_ACE_TYPE,
1630 	    ACE_OWNER);
1631 	SET_ACE(acl, index, -1, masks.group, ACE_ACCESS_ALLOWED_ACE_TYPE,
1632 	    ACE_IDENTIFIER_GROUP|ACE_GROUP);
1633 	SET_ACE(acl, index, -1, masks.everyone, ACE_ACCESS_ALLOWED_ACE_TYPE,
1634 	    ACE_EVERYONE);
1635 
1636 	return (0);
1637 }
1638 
1639 /*
1640  * ace_trivial:
1641  * determine whether an ace_t acl is trivial
1642  *
1643  * Trivialness implies that the acl is composed of only
1644  * owner, group, everyone entries.  ACL can't
1645  * have read_acl denied, and write_owner/write_acl/write_attributes
1646  * can only be owner@ entry.
1647  */
1648 int
ace_trivial_common(void * acep,int aclcnt,uintptr_t (* walk)(void *,uintptr_t,int aclcnt,uint16_t *,uint16_t *,uint32_t *))1649 ace_trivial_common(void *acep, int aclcnt,
1650     uintptr_t (*walk)(void *, uintptr_t, int aclcnt,
1651     uint16_t *, uint16_t *, uint32_t *))
1652 {
1653 	uint16_t flags;
1654 	uint32_t mask;
1655 	uint16_t type;
1656 	uintptr_t cookie = 0;
1657 
1658 	while ((cookie = walk(acep, cookie, aclcnt, &flags, &type, &mask))) {
1659 		switch (flags & ACE_TYPE_FLAGS) {
1660 		case ACE_OWNER:
1661 		case ACE_GROUP|ACE_IDENTIFIER_GROUP:
1662 		case ACE_EVERYONE:
1663 			break;
1664 		default:
1665 			return (1);
1666 
1667 		}
1668 
1669 		if (flags & (ACE_FILE_INHERIT_ACE|
1670 		    ACE_DIRECTORY_INHERIT_ACE|ACE_NO_PROPAGATE_INHERIT_ACE|
1671 		    ACE_INHERIT_ONLY_ACE))
1672 			return (1);
1673 
1674 		/*
1675 		 * Special check for some special bits
1676 		 *
1677 		 * Don't allow anybody to deny reading basic
1678 		 * attributes or a files ACL.
1679 		 */
1680 		if ((mask & (ACE_READ_ACL|ACE_READ_ATTRIBUTES)) &&
1681 		    (type == ACE_ACCESS_DENIED_ACE_TYPE))
1682 			return (1);
1683 
1684 		/*
1685 		 * Delete permissions are never set by default
1686 		 */
1687 		if (mask & (ACE_DELETE|ACE_DELETE_CHILD))
1688 			return (1);
1689 		/*
1690 		 * only allow owner@ to have
1691 		 * write_acl/write_owner/write_attributes/write_xattr/
1692 		 */
1693 		if (type == ACE_ACCESS_ALLOWED_ACE_TYPE &&
1694 		    (!(flags & ACE_OWNER) && (mask &
1695 		    (ACE_WRITE_OWNER|ACE_WRITE_ACL| ACE_WRITE_ATTRIBUTES|
1696 		    ACE_WRITE_NAMED_ATTRS))))
1697 			return (1);
1698 
1699 	}
1700 	return (0);
1701 }
1702