xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_acl.c (revision 3c4993fb5a74112f361d71dab20997bdc749a7fb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/vfs.h>
35 #include <sys/vnode.h>
36 #include <sys/sid.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/kmem.h>
40 #include <sys/cmn_err.h>
41 #include <sys/errno.h>
42 #include <sys/unistd.h>
43 #include <sys/sdt.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/mode.h>
46 #include <sys/policy.h>
47 #include <sys/zfs_znode.h>
48 #include <sys/zfs_fuid.h>
49 #include <sys/zfs_acl.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_vfsops.h>
52 #include <sys/dmu.h>
53 #include <sys/dnode.h>
54 #include <sys/zap.h>
55 #include "fs/fs_subr.h"
56 #include <acl/acl_common.h>
57 
58 #define	ALLOW	ACE_ACCESS_ALLOWED_ACE_TYPE
59 #define	DENY	ACE_ACCESS_DENIED_ACE_TYPE
60 #define	MAX_ACE_TYPE	ACE_SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE
61 
62 #define	OWNING_GROUP		(ACE_GROUP|ACE_IDENTIFIER_GROUP)
63 #define	EVERYONE_ALLOW_MASK (ACE_READ_ACL|ACE_READ_ATTRIBUTES | \
64     ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE)
65 #define	EVERYONE_DENY_MASK (ACE_WRITE_ACL|ACE_WRITE_OWNER | \
66     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
67 #define	OWNER_ALLOW_MASK (ACE_WRITE_ACL | ACE_WRITE_OWNER | \
68     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
69 #define	WRITE_MASK_DATA (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS)
70 
71 #define	ZFS_CHECKED_MASKS (ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_DATA| \
72     ACE_READ_NAMED_ATTRS|ACE_WRITE_DATA|ACE_WRITE_ATTRIBUTES| \
73     ACE_WRITE_NAMED_ATTRS|ACE_APPEND_DATA|ACE_EXECUTE|ACE_WRITE_OWNER| \
74     ACE_WRITE_ACL|ACE_DELETE|ACE_DELETE_CHILD|ACE_SYNCHRONIZE)
75 
76 #define	WRITE_MASK (WRITE_MASK_DATA|ACE_WRITE_ATTRIBUTES|ACE_WRITE_ACL|\
77     ACE_WRITE_OWNER)
78 
79 #define	OGE_CLEAR	(ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
80     ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
81 
82 #define	OKAY_MASK_BITS (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
83     ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
84 
85 #define	ALL_INHERIT	(ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE | \
86     ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE|ACE_INHERITED_ACE)
87 
88 #define	SECURE_CLEAR	(ACE_WRITE_ACL|ACE_WRITE_OWNER)
89 
90 #define	V4_ACL_WIDE_FLAGS (ZFS_ACL_AUTO_INHERIT|ZFS_ACL_DEFAULTED|\
91     ZFS_ACL_PROTECTED)
92 
93 #define	ZFS_ACL_WIDE_FLAGS (V4_ACL_WIDE_FLAGS|ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|\
94     ZFS_ACL_OBJ_ACE)
95 
96 static uint16_t
97 zfs_ace_v0_get_type(void *acep)
98 {
99 	return (((zfs_oldace_t *)acep)->z_type);
100 }
101 
102 static uint16_t
103 zfs_ace_v0_get_flags(void *acep)
104 {
105 	return (((zfs_oldace_t *)acep)->z_flags);
106 }
107 
108 static uint32_t
109 zfs_ace_v0_get_mask(void *acep)
110 {
111 	return (((zfs_oldace_t *)acep)->z_access_mask);
112 }
113 
114 static uint64_t
115 zfs_ace_v0_get_who(void *acep)
116 {
117 	return (((zfs_oldace_t *)acep)->z_fuid);
118 }
119 
120 static void
121 zfs_ace_v0_set_type(void *acep, uint16_t type)
122 {
123 	((zfs_oldace_t *)acep)->z_type = type;
124 }
125 
126 static void
127 zfs_ace_v0_set_flags(void *acep, uint16_t flags)
128 {
129 	((zfs_oldace_t *)acep)->z_flags = flags;
130 }
131 
132 static void
133 zfs_ace_v0_set_mask(void *acep, uint32_t mask)
134 {
135 	((zfs_oldace_t *)acep)->z_access_mask = mask;
136 }
137 
138 static void
139 zfs_ace_v0_set_who(void *acep, uint64_t who)
140 {
141 	((zfs_oldace_t *)acep)->z_fuid = who;
142 }
143 
144 /*ARGSUSED*/
145 static size_t
146 zfs_ace_v0_size(void *acep)
147 {
148 	return (sizeof (zfs_oldace_t));
149 }
150 
151 static size_t
152 zfs_ace_v0_abstract_size(void)
153 {
154 	return (sizeof (zfs_oldace_t));
155 }
156 
157 static int
158 zfs_ace_v0_mask_off(void)
159 {
160 	return (offsetof(zfs_oldace_t, z_access_mask));
161 }
162 
163 /*ARGSUSED*/
164 static int
165 zfs_ace_v0_data(void *acep, void **datap)
166 {
167 	*datap = NULL;
168 	return (0);
169 }
170 
171 static acl_ops_t zfs_acl_v0_ops = {
172 	zfs_ace_v0_get_mask,
173 	zfs_ace_v0_set_mask,
174 	zfs_ace_v0_get_flags,
175 	zfs_ace_v0_set_flags,
176 	zfs_ace_v0_get_type,
177 	zfs_ace_v0_set_type,
178 	zfs_ace_v0_get_who,
179 	zfs_ace_v0_set_who,
180 	zfs_ace_v0_size,
181 	zfs_ace_v0_abstract_size,
182 	zfs_ace_v0_mask_off,
183 	zfs_ace_v0_data
184 };
185 
186 static uint16_t
187 zfs_ace_fuid_get_type(void *acep)
188 {
189 	return (((zfs_ace_hdr_t *)acep)->z_type);
190 }
191 
192 static uint16_t
193 zfs_ace_fuid_get_flags(void *acep)
194 {
195 	return (((zfs_ace_hdr_t *)acep)->z_flags);
196 }
197 
198 static uint32_t
199 zfs_ace_fuid_get_mask(void *acep)
200 {
201 	return (((zfs_ace_hdr_t *)acep)->z_access_mask);
202 }
203 
204 static uint64_t
205 zfs_ace_fuid_get_who(void *args)
206 {
207 	uint16_t entry_type;
208 	zfs_ace_t *acep = args;
209 
210 	entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
211 
212 	if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
213 	    entry_type == ACE_EVERYONE)
214 		return (-1);
215 	return (((zfs_ace_t *)acep)->z_fuid);
216 }
217 
218 static void
219 zfs_ace_fuid_set_type(void *acep, uint16_t type)
220 {
221 	((zfs_ace_hdr_t *)acep)->z_type = type;
222 }
223 
224 static void
225 zfs_ace_fuid_set_flags(void *acep, uint16_t flags)
226 {
227 	((zfs_ace_hdr_t *)acep)->z_flags = flags;
228 }
229 
230 static void
231 zfs_ace_fuid_set_mask(void *acep, uint32_t mask)
232 {
233 	((zfs_ace_hdr_t *)acep)->z_access_mask = mask;
234 }
235 
236 static void
237 zfs_ace_fuid_set_who(void *arg, uint64_t who)
238 {
239 	zfs_ace_t *acep = arg;
240 
241 	uint16_t entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
242 
243 	if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
244 	    entry_type == ACE_EVERYONE)
245 		return;
246 	acep->z_fuid = who;
247 }
248 
249 static size_t
250 zfs_ace_fuid_size(void *acep)
251 {
252 	zfs_ace_hdr_t *zacep = acep;
253 	uint16_t entry_type;
254 
255 	switch (zacep->z_type) {
256 	case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
257 	case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
258 	case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
259 	case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
260 		return (sizeof (zfs_object_ace_t));
261 	case ALLOW:
262 	case DENY:
263 		entry_type =
264 		    (((zfs_ace_hdr_t *)acep)->z_flags & ACE_TYPE_FLAGS);
265 		if (entry_type == ACE_OWNER ||
266 		    entry_type == (ACE_GROUP | ACE_IDENTIFIER_GROUP) ||
267 		    entry_type == ACE_EVERYONE)
268 			return (sizeof (zfs_ace_hdr_t));
269 		/*FALLTHROUGH*/
270 	default:
271 		return (sizeof (zfs_ace_t));
272 	}
273 }
274 
275 static size_t
276 zfs_ace_fuid_abstract_size(void)
277 {
278 	return (sizeof (zfs_ace_hdr_t));
279 }
280 
281 static int
282 zfs_ace_fuid_mask_off(void)
283 {
284 	return (offsetof(zfs_ace_hdr_t, z_access_mask));
285 }
286 
287 static int
288 zfs_ace_fuid_data(void *acep, void **datap)
289 {
290 	zfs_ace_t *zacep = acep;
291 	zfs_object_ace_t *zobjp;
292 
293 	switch (zacep->z_hdr.z_type) {
294 	case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
295 	case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
296 	case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
297 	case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
298 		zobjp = acep;
299 		*datap = (caddr_t)zobjp + sizeof (zfs_ace_t);
300 		return (sizeof (zfs_object_ace_t) - sizeof (zfs_ace_t));
301 	default:
302 		*datap = NULL;
303 		return (0);
304 	}
305 }
306 
307 static acl_ops_t zfs_acl_fuid_ops = {
308 	zfs_ace_fuid_get_mask,
309 	zfs_ace_fuid_set_mask,
310 	zfs_ace_fuid_get_flags,
311 	zfs_ace_fuid_set_flags,
312 	zfs_ace_fuid_get_type,
313 	zfs_ace_fuid_set_type,
314 	zfs_ace_fuid_get_who,
315 	zfs_ace_fuid_set_who,
316 	zfs_ace_fuid_size,
317 	zfs_ace_fuid_abstract_size,
318 	zfs_ace_fuid_mask_off,
319 	zfs_ace_fuid_data
320 };
321 
322 static int
323 zfs_acl_version(int version)
324 {
325 	if (version < ZPL_VERSION_FUID)
326 		return (ZFS_ACL_VERSION_INITIAL);
327 	else
328 		return (ZFS_ACL_VERSION_FUID);
329 }
330 
331 static int
332 zfs_acl_version_zp(znode_t *zp)
333 {
334 	return (zfs_acl_version(zp->z_zfsvfs->z_version));
335 }
336 
337 static zfs_acl_t *
338 zfs_acl_alloc(int vers)
339 {
340 	zfs_acl_t *aclp;
341 
342 	aclp = kmem_zalloc(sizeof (zfs_acl_t), KM_SLEEP);
343 	list_create(&aclp->z_acl, sizeof (zfs_acl_node_t),
344 	    offsetof(zfs_acl_node_t, z_next));
345 	aclp->z_version = vers;
346 	if (vers == ZFS_ACL_VERSION_FUID)
347 		aclp->z_ops = zfs_acl_fuid_ops;
348 	else
349 		aclp->z_ops = zfs_acl_v0_ops;
350 	return (aclp);
351 }
352 
353 static zfs_acl_node_t *
354 zfs_acl_node_alloc(size_t bytes)
355 {
356 	zfs_acl_node_t *aclnode;
357 
358 	aclnode = kmem_zalloc(sizeof (zfs_acl_node_t), KM_SLEEP);
359 	if (bytes) {
360 		aclnode->z_acldata = kmem_alloc(bytes, KM_SLEEP);
361 		aclnode->z_allocdata = aclnode->z_acldata;
362 		aclnode->z_allocsize = bytes;
363 		aclnode->z_size = bytes;
364 	}
365 
366 	return (aclnode);
367 }
368 
369 static void
370 zfs_acl_node_free(zfs_acl_node_t *aclnode)
371 {
372 	if (aclnode->z_allocsize)
373 		kmem_free(aclnode->z_allocdata, aclnode->z_allocsize);
374 	kmem_free(aclnode, sizeof (zfs_acl_node_t));
375 }
376 
377 void
378 zfs_acl_free(zfs_acl_t *aclp)
379 {
380 	zfs_acl_node_t *aclnode;
381 
382 	while (aclnode = list_head(&aclp->z_acl)) {
383 		list_remove(&aclp->z_acl, aclnode);
384 		zfs_acl_node_free(aclnode);
385 	}
386 
387 	list_destroy(&aclp->z_acl);
388 	kmem_free(aclp, sizeof (zfs_acl_t));
389 }
390 
391 static boolean_t
392 zfs_ace_valid(vtype_t obj_type, zfs_acl_t *aclp, uint16_t type, uint16_t iflags)
393 {
394 	/*
395 	 * first check type of entry
396 	 */
397 
398 	switch (iflags & ACE_TYPE_FLAGS) {
399 	case ACE_OWNER:
400 	case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
401 	case ACE_IDENTIFIER_GROUP:
402 	case ACE_EVERYONE:
403 	case 0:	/* User entry */
404 		break;
405 	default:
406 		return (B_FALSE);
407 
408 	}
409 
410 	/*
411 	 * next check inheritance level flags
412 	 */
413 
414 	if (type != ALLOW && type > MAX_ACE_TYPE) {
415 		return (B_FALSE);
416 	}
417 
418 	switch (type) {
419 	case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
420 	case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
421 	case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
422 	case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
423 		if (aclp->z_version < ZFS_ACL_VERSION_FUID)
424 			return (B_FALSE);
425 		aclp->z_hints |= ZFS_ACL_OBJ_ACE;
426 	}
427 
428 	/*
429 	 * Only directories should have inheritance flags.
430 	 */
431 	if (obj_type != VDIR && (iflags &
432 	    (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE|
433 	    ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE))) {
434 		return (B_FALSE);
435 	}
436 
437 	if (iflags & (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE))
438 		aclp->z_hints |= ZFS_INHERIT_ACE;
439 
440 	if (iflags & (ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE)) {
441 		if ((iflags & (ACE_FILE_INHERIT_ACE|
442 		    ACE_DIRECTORY_INHERIT_ACE)) == 0) {
443 			return (B_FALSE);
444 		}
445 	}
446 
447 	return (B_TRUE);
448 }
449 
450 static void *
451 zfs_acl_next_ace(zfs_acl_t *aclp, void *start, uint64_t *who,
452     uint32_t *access_mask, uint16_t *iflags, uint16_t *type)
453 {
454 	zfs_acl_node_t *aclnode;
455 
456 	if (start == NULL) {
457 		aclnode = list_head(&aclp->z_acl);
458 		if (aclnode == NULL)
459 			return (NULL);
460 
461 		aclp->z_next_ace = aclnode->z_acldata;
462 		aclp->z_curr_node = aclnode;
463 		aclnode->z_ace_idx = 0;
464 	}
465 
466 	aclnode = aclp->z_curr_node;
467 
468 	if (aclnode == NULL)
469 		return (NULL);
470 
471 	if (aclnode->z_ace_idx >= aclnode->z_ace_count) {
472 		aclnode = list_next(&aclp->z_acl, aclnode);
473 		if (aclnode == NULL)
474 			return (NULL);
475 		else {
476 			aclp->z_curr_node = aclnode;
477 			aclnode->z_ace_idx = 0;
478 			aclp->z_next_ace = aclnode->z_acldata;
479 		}
480 	}
481 
482 	if (aclnode->z_ace_idx < aclnode->z_ace_count) {
483 		void *acep = aclp->z_next_ace;
484 		*iflags = aclp->z_ops.ace_flags_get(acep);
485 		*type = aclp->z_ops.ace_type_get(acep);
486 		*access_mask = aclp->z_ops.ace_mask_get(acep);
487 		*who = aclp->z_ops.ace_who_get(acep);
488 		aclp->z_next_ace = (caddr_t)aclp->z_next_ace +
489 		    aclp->z_ops.ace_size(acep);
490 		aclnode->z_ace_idx++;
491 		return ((void *)acep);
492 	}
493 	return (NULL);
494 }
495 
496 /*ARGSUSED*/
497 static uint64_t
498 zfs_ace_walk(void *datap, uint64_t cookie, int aclcnt,
499     uint16_t *flags, uint16_t *type, uint32_t *mask)
500 {
501 	zfs_acl_t *aclp = datap;
502 	zfs_ace_hdr_t *acep = (zfs_ace_hdr_t *)(uintptr_t)cookie;
503 	uint64_t who;
504 
505 	acep = zfs_acl_next_ace(aclp, acep, &who, mask,
506 	    flags, type);
507 	return ((uint64_t)(uintptr_t)acep);
508 }
509 
510 static zfs_acl_node_t *
511 zfs_acl_curr_node(zfs_acl_t *aclp)
512 {
513 	ASSERT(aclp->z_curr_node);
514 	return (aclp->z_curr_node);
515 }
516 
517 /*
518  * Copy ACE to internal ZFS format.
519  * While processing the ACL each ACE will be validated for correctness.
520  * ACE FUIDs will be created later.
521  */
522 int
523 zfs_copy_ace_2_fuid(vtype_t obj_type, zfs_acl_t *aclp, void *datap,
524     zfs_ace_t *z_acl, int aclcnt, size_t *size)
525 {
526 	int i;
527 	uint16_t entry_type;
528 	zfs_ace_t *aceptr = z_acl;
529 	ace_t *acep = datap;
530 	zfs_object_ace_t *zobjacep;
531 	ace_object_t *aceobjp;
532 
533 	for (i = 0; i != aclcnt; i++) {
534 		aceptr->z_hdr.z_access_mask = acep->a_access_mask;
535 		aceptr->z_hdr.z_flags = acep->a_flags;
536 		aceptr->z_hdr.z_type = acep->a_type;
537 		entry_type = aceptr->z_hdr.z_flags & ACE_TYPE_FLAGS;
538 		if (entry_type != ACE_OWNER && entry_type != OWNING_GROUP &&
539 		    entry_type != ACE_EVERYONE)
540 			aceptr->z_fuid = (uint64_t)acep->a_who;
541 		/*
542 		 * Make sure ACE is valid
543 		 */
544 		if (zfs_ace_valid(obj_type, aclp, aceptr->z_hdr.z_type,
545 		    aceptr->z_hdr.z_flags) != B_TRUE)
546 			return (EINVAL);
547 
548 		switch (acep->a_type) {
549 		case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
550 		case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
551 		case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
552 		case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
553 			zobjacep = (zfs_object_ace_t *)aceptr;
554 			aceobjp = (ace_object_t *)acep;
555 
556 			bcopy(aceobjp->a_obj_type, zobjacep->z_object_type,
557 			    sizeof (aceobjp->a_obj_type));
558 			bcopy(aceobjp->a_inherit_obj_type,
559 			    zobjacep->z_inherit_type,
560 			    sizeof (aceobjp->a_inherit_obj_type));
561 			acep = (ace_t *)((caddr_t)acep + sizeof (ace_object_t));
562 			break;
563 		default:
564 			acep = (ace_t *)((caddr_t)acep + sizeof (ace_t));
565 		}
566 
567 		aceptr = (zfs_ace_t *)((caddr_t)aceptr +
568 		    aclp->z_ops.ace_size(aceptr));
569 	}
570 
571 	*size = (caddr_t)aceptr - (caddr_t)z_acl;
572 
573 	return (0);
574 }
575 
576 /*
577  * Copy ZFS ACEs to fixed size ace_t layout
578  */
579 static void
580 zfs_copy_fuid_2_ace(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, void *datap, int filter)
581 {
582 	uint64_t who;
583 	uint32_t access_mask;
584 	uint16_t iflags, type;
585 	zfs_ace_hdr_t *zacep = NULL;
586 	ace_t *acep = datap;
587 	ace_object_t *objacep;
588 	zfs_object_ace_t *zobjacep;
589 	zfs_fuid_hdl_t hdl = { 0 };
590 	size_t ace_size;
591 	uint16_t entry_type;
592 
593 	while (zacep = zfs_acl_next_ace(aclp, zacep,
594 	    &who, &access_mask, &iflags, &type)) {
595 
596 		switch (type) {
597 		case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
598 		case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
599 		case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
600 		case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
601 			if (filter) {
602 				continue;
603 			}
604 			zobjacep = (zfs_object_ace_t *)zacep;
605 			objacep = (ace_object_t *)acep;
606 			bcopy(zobjacep->z_object_type,
607 			    objacep->a_obj_type,
608 			    sizeof (zobjacep->z_object_type));
609 			bcopy(zobjacep->z_inherit_type,
610 			    objacep->a_inherit_obj_type,
611 			    sizeof (zobjacep->z_inherit_type));
612 			ace_size = sizeof (ace_object_t);
613 			break;
614 		default:
615 			ace_size = sizeof (ace_t);
616 			break;
617 		}
618 
619 		entry_type = (iflags & ACE_TYPE_FLAGS);
620 		if ((entry_type != ACE_OWNER &&
621 		    entry_type != (ACE_GROUP | ACE_IDENTIFIER_GROUP) &&
622 		    entry_type != ACE_EVERYONE))
623 			zfs_fuid_queue_map_id(zfsvfs, &hdl, who,
624 			    (entry_type & ACE_IDENTIFIER_GROUP) ?
625 			    ZFS_ACE_GROUP : ZFS_ACE_USER, &acep->a_who);
626 		else
627 			acep->a_who = (uid_t)(int64_t)who;
628 		acep->a_access_mask = access_mask;
629 		acep->a_flags = iflags;
630 		acep->a_type = type;
631 		acep = (ace_t *)((caddr_t)acep + ace_size);
632 	}
633 	zfs_fuid_get_mappings(&hdl);
634 }
635 
636 static int
637 zfs_copy_ace_2_oldace(vtype_t obj_type, zfs_acl_t *aclp, ace_t *acep,
638     zfs_oldace_t *z_acl, int aclcnt, size_t *size)
639 {
640 	int i;
641 	zfs_oldace_t *aceptr = z_acl;
642 
643 	for (i = 0; i != aclcnt; i++, aceptr++) {
644 		aceptr->z_access_mask = acep[i].a_access_mask;
645 		aceptr->z_type = acep[i].a_type;
646 		aceptr->z_flags = acep[i].a_flags;
647 		aceptr->z_fuid = acep[i].a_who;
648 		/*
649 		 * Make sure ACE is valid
650 		 */
651 		if (zfs_ace_valid(obj_type, aclp, aceptr->z_type,
652 		    aceptr->z_flags) != B_TRUE)
653 			return (EINVAL);
654 	}
655 	*size = (caddr_t)aceptr - (caddr_t)z_acl;
656 	return (0);
657 }
658 
659 /*
660  * convert old ACL format to new
661  */
662 void
663 zfs_acl_xform(znode_t *zp, zfs_acl_t *aclp)
664 {
665 	zfs_oldace_t *oldaclp;
666 	int i;
667 	uint16_t type, iflags;
668 	uint32_t access_mask;
669 	uint64_t who;
670 	void *cookie = NULL;
671 	zfs_acl_node_t *aclnode, *newaclnode;
672 
673 	ASSERT(aclp->z_version == ZFS_ACL_VERSION_INITIAL);
674 	/*
675 	 * First create the ACE in a contiguous piece of memory
676 	 * for zfs_copy_ace_2_fuid().
677 	 *
678 	 * We only convert an ACL once, so this won't happen
679 	 * everytime.
680 	 */
681 	oldaclp = kmem_alloc(sizeof (zfs_oldace_t) * aclp->z_acl_count,
682 	    KM_SLEEP);
683 	i = 0;
684 	while (cookie = zfs_acl_next_ace(aclp, cookie, &who,
685 	    &access_mask, &iflags, &type)) {
686 		oldaclp[i].z_flags = iflags;
687 		oldaclp[i].z_type = type;
688 		oldaclp[i].z_fuid = who;
689 		oldaclp[i++].z_access_mask = access_mask;
690 	}
691 
692 	newaclnode = zfs_acl_node_alloc(aclp->z_acl_count *
693 	    sizeof (zfs_object_ace_t));
694 	aclp->z_ops = zfs_acl_fuid_ops;
695 	VERIFY(zfs_copy_ace_2_fuid(ZTOV(zp)->v_type, aclp, oldaclp,
696 	    newaclnode->z_acldata, aclp->z_acl_count,
697 	    &newaclnode->z_size) == 0);
698 	aclp->z_acl_bytes = newaclnode->z_size;
699 	newaclnode->z_ace_count = aclp->z_acl_count;
700 	aclp->z_version = ZFS_ACL_VERSION;
701 	kmem_free(oldaclp, aclp->z_acl_count * sizeof (zfs_oldace_t));
702 
703 	/*
704 	 * Release all previous ACL nodes
705 	 */
706 
707 	while (aclnode = list_head(&aclp->z_acl)) {
708 		list_remove(&aclp->z_acl, aclnode);
709 		if (aclnode->z_allocsize)
710 			kmem_free(aclnode->z_allocdata, aclnode->z_allocsize);
711 		kmem_free(aclnode, sizeof (zfs_acl_node_t));
712 	}
713 	list_insert_head(&aclp->z_acl, newaclnode);
714 }
715 
716 /*
717  * Convert unix access mask to v4 access mask
718  */
719 static uint32_t
720 zfs_unix_to_v4(uint32_t access_mask)
721 {
722 	uint32_t new_mask = 0;
723 
724 	if (access_mask & S_IXOTH)
725 		new_mask |= ACE_EXECUTE;
726 	if (access_mask & S_IWOTH)
727 		new_mask |= ACE_WRITE_DATA;
728 	if (access_mask & S_IROTH)
729 		new_mask |= ACE_READ_DATA;
730 	return (new_mask);
731 }
732 
733 static void
734 zfs_set_ace(zfs_acl_t *aclp, void *acep, uint32_t access_mask,
735     uint16_t access_type, uint64_t fuid, uint16_t entry_type)
736 {
737 	uint16_t type = entry_type & ACE_TYPE_FLAGS;
738 
739 	aclp->z_ops.ace_mask_set(acep, access_mask);
740 	aclp->z_ops.ace_type_set(acep, access_type);
741 	aclp->z_ops.ace_flags_set(acep, entry_type);
742 	if ((type != ACE_OWNER && type != (ACE_GROUP | ACE_IDENTIFIER_GROUP) &&
743 	    type != ACE_EVERYONE))
744 		aclp->z_ops.ace_who_set(acep, fuid);
745 }
746 
747 /*
748  * Determine mode of file based on ACL.
749  * Also, create FUIDs for any User/Group ACEs
750  */
751 static uint64_t
752 zfs_mode_fuid_compute(znode_t *zp, zfs_acl_t *aclp, zfs_fuid_info_t **fuidp,
753     dmu_tx_t *tx)
754 {
755 	int		entry_type;
756 	mode_t		mode;
757 	mode_t		seen = 0;
758 	zfs_ace_hdr_t 	*acep = NULL;
759 	uint64_t	who;
760 	uint16_t	iflags, type;
761 	uint32_t	access_mask;
762 
763 	mode = (zp->z_phys->zp_mode & (S_IFMT | S_ISUID | S_ISGID | S_ISVTX));
764 
765 	while (acep = zfs_acl_next_ace(aclp, acep, &who,
766 	    &access_mask, &iflags, &type)) {
767 		entry_type = (iflags & ACE_TYPE_FLAGS);
768 
769 		/*
770 		 * Skip over inherit only ACEs
771 		 */
772 		if (entry_type ==  ACE_INHERIT_ONLY_ACE)
773 			continue;
774 
775 		if (entry_type == ACE_OWNER) {
776 			if ((access_mask & ACE_READ_DATA) &&
777 			    (!(seen & S_IRUSR))) {
778 				seen |= S_IRUSR;
779 				if (type == ALLOW) {
780 					mode |= S_IRUSR;
781 				}
782 			}
783 			if ((access_mask & ACE_WRITE_DATA) &&
784 			    (!(seen & S_IWUSR))) {
785 				seen |= S_IWUSR;
786 				if (type == ALLOW) {
787 					mode |= S_IWUSR;
788 				}
789 			}
790 			if ((access_mask & ACE_EXECUTE) &&
791 			    (!(seen & S_IXUSR))) {
792 				seen |= S_IXUSR;
793 				if (type == ALLOW) {
794 					mode |= S_IXUSR;
795 				}
796 			}
797 		} else if (entry_type == OWNING_GROUP) {
798 			if ((access_mask & ACE_READ_DATA) &&
799 			    (!(seen & S_IRGRP))) {
800 				seen |= S_IRGRP;
801 				if (type == ALLOW) {
802 					mode |= S_IRGRP;
803 				}
804 			}
805 			if ((access_mask & ACE_WRITE_DATA) &&
806 			    (!(seen & S_IWGRP))) {
807 				seen |= S_IWGRP;
808 				if (type == ALLOW) {
809 					mode |= S_IWGRP;
810 				}
811 			}
812 			if ((access_mask & ACE_EXECUTE) &&
813 			    (!(seen & S_IXGRP))) {
814 				seen |= S_IXGRP;
815 				if (type == ALLOW) {
816 					mode |= S_IXGRP;
817 				}
818 			}
819 		} else if (entry_type == ACE_EVERYONE) {
820 			if ((access_mask & ACE_READ_DATA)) {
821 				if (!(seen & S_IRUSR)) {
822 					seen |= S_IRUSR;
823 					if (type == ALLOW) {
824 						mode |= S_IRUSR;
825 					}
826 				}
827 				if (!(seen & S_IRGRP)) {
828 					seen |= S_IRGRP;
829 					if (type == ALLOW) {
830 						mode |= S_IRGRP;
831 					}
832 				}
833 				if (!(seen & S_IROTH)) {
834 					seen |= S_IROTH;
835 					if (type == ALLOW) {
836 						mode |= S_IROTH;
837 					}
838 				}
839 			}
840 			if ((access_mask & ACE_WRITE_DATA)) {
841 				if (!(seen & S_IWUSR)) {
842 					seen |= S_IWUSR;
843 					if (type == ALLOW) {
844 						mode |= S_IWUSR;
845 					}
846 				}
847 				if (!(seen & S_IWGRP)) {
848 					seen |= S_IWGRP;
849 					if (type == ALLOW) {
850 						mode |= S_IWGRP;
851 					}
852 				}
853 				if (!(seen & S_IWOTH)) {
854 					seen |= S_IWOTH;
855 					if (type == ALLOW) {
856 						mode |= S_IWOTH;
857 					}
858 				}
859 			}
860 			if ((access_mask & ACE_EXECUTE)) {
861 				if (!(seen & S_IXUSR)) {
862 					seen |= S_IXUSR;
863 					if (type == ALLOW) {
864 						mode |= S_IXUSR;
865 					}
866 				}
867 				if (!(seen & S_IXGRP)) {
868 					seen |= S_IXGRP;
869 					if (type == ALLOW) {
870 						mode |= S_IXGRP;
871 					}
872 				}
873 				if (!(seen & S_IXOTH)) {
874 					seen |= S_IXOTH;
875 					if (type == ALLOW) {
876 						mode |= S_IXOTH;
877 					}
878 				}
879 			}
880 		}
881 		/*
882 		 * Now handle FUID create for user/group ACEs
883 		 */
884 		if (entry_type == 0 || entry_type == ACE_IDENTIFIER_GROUP) {
885 			aclp->z_ops.ace_who_set(acep,
886 			    zfs_fuid_create(zp->z_zfsvfs, who,
887 			    entry_type == 0 ? ZFS_ACE_USER : ZFS_ACE_GROUP, tx,
888 			    fuidp));
889 		}
890 	}
891 	return (mode);
892 }
893 
894 static zfs_acl_t *
895 zfs_acl_node_read_internal(znode_t *zp, boolean_t will_modify)
896 {
897 	zfs_acl_t	*aclp;
898 	zfs_acl_node_t	*aclnode;
899 
900 	aclp = zfs_acl_alloc(zp->z_phys->zp_acl.z_acl_version);
901 
902 	/*
903 	 * Version 0 to 1 znode_acl_phys has the size/count fields swapped.
904 	 * Version 0 didn't have a size field, only a count.
905 	 */
906 	if (zp->z_phys->zp_acl.z_acl_version == ZFS_ACL_VERSION_INITIAL) {
907 		aclp->z_acl_count = zp->z_phys->zp_acl.z_acl_size;
908 		aclp->z_acl_bytes = ZFS_ACL_SIZE(aclp->z_acl_count);
909 	} else {
910 		aclp->z_acl_count = zp->z_phys->zp_acl.z_acl_count;
911 		aclp->z_acl_bytes = zp->z_phys->zp_acl.z_acl_size;
912 	}
913 
914 	aclnode = zfs_acl_node_alloc(will_modify ? aclp->z_acl_bytes : 0);
915 	aclnode->z_ace_count = aclp->z_acl_count;
916 	if (will_modify) {
917 		bcopy(zp->z_phys->zp_acl.z_ace_data, aclnode->z_acldata,
918 		    aclp->z_acl_bytes);
919 	} else {
920 		aclnode->z_size = aclp->z_acl_bytes;
921 		aclnode->z_acldata = &zp->z_phys->zp_acl.z_ace_data[0];
922 	}
923 
924 	list_insert_head(&aclp->z_acl, aclnode);
925 
926 	return (aclp);
927 }
928 
929 /*
930  * Read an external acl object.
931  */
932 static int
933 zfs_acl_node_read(znode_t *zp, zfs_acl_t **aclpp, boolean_t will_modify)
934 {
935 	uint64_t extacl = zp->z_phys->zp_acl.z_acl_extern_obj;
936 	zfs_acl_t	*aclp;
937 	size_t		aclsize;
938 	size_t		acl_count;
939 	zfs_acl_node_t	*aclnode;
940 	int error;
941 
942 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
943 
944 	if (zp->z_phys->zp_acl.z_acl_extern_obj == 0) {
945 		*aclpp = zfs_acl_node_read_internal(zp, will_modify);
946 		return (0);
947 	}
948 
949 	aclp = zfs_acl_alloc(zp->z_phys->zp_acl.z_acl_version);
950 	if (zp->z_phys->zp_acl.z_acl_version == ZFS_ACL_VERSION_INITIAL) {
951 		zfs_acl_phys_v0_t *zacl0 =
952 		    (zfs_acl_phys_v0_t *)&zp->z_phys->zp_acl;
953 
954 		aclsize = ZFS_ACL_SIZE(zacl0->z_acl_count);
955 		acl_count = zacl0->z_acl_count;
956 	} else {
957 		aclsize = zp->z_phys->zp_acl.z_acl_size;
958 		acl_count = zp->z_phys->zp_acl.z_acl_count;
959 		if (aclsize == 0)
960 			aclsize = acl_count * sizeof (zfs_ace_t);
961 	}
962 	aclnode = zfs_acl_node_alloc(aclsize);
963 	list_insert_head(&aclp->z_acl, aclnode);
964 	error = dmu_read(zp->z_zfsvfs->z_os, extacl, 0,
965 	    aclsize, aclnode->z_acldata);
966 	aclnode->z_ace_count = acl_count;
967 	aclp->z_acl_count = acl_count;
968 	aclp->z_acl_bytes = aclsize;
969 
970 	if (error != 0) {
971 		zfs_acl_free(aclp);
972 		return (error);
973 	}
974 
975 	*aclpp = aclp;
976 	return (0);
977 }
978 
979 /*
980  * common code for setting ACLs.
981  *
982  * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl.
983  * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's
984  * already checked the acl and knows whether to inherit.
985  */
986 int
987 zfs_aclset_common(znode_t *zp, zfs_acl_t *aclp, zfs_fuid_info_t **fuidp,
988     dmu_tx_t *tx)
989 {
990 	int		error;
991 	znode_phys_t	*zphys = zp->z_phys;
992 	zfs_acl_phys_t	*zacl = &zphys->zp_acl;
993 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
994 	uint64_t	aoid = zphys->zp_acl.z_acl_extern_obj;
995 	uint64_t	off = 0;
996 	dmu_object_type_t otype;
997 	zfs_acl_node_t	*aclnode;
998 
999 	ASSERT(MUTEX_HELD(&zp->z_lock));
1000 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1001 
1002 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1003 
1004 	zphys->zp_mode = zfs_mode_fuid_compute(zp, aclp, fuidp, tx);
1005 
1006 	/*
1007 	 * Decide which opbject type to use.  If we are forced to
1008 	 * use old ACL format than transform ACL into zfs_oldace_t
1009 	 * layout.
1010 	 */
1011 	if (!zfsvfs->z_use_fuids) {
1012 		otype = DMU_OT_OLDACL;
1013 	} else {
1014 		if ((aclp->z_version == ZFS_ACL_VERSION_INITIAL) &&
1015 		    (zfsvfs->z_version >= ZPL_VERSION_FUID))
1016 			zfs_acl_xform(zp, aclp);
1017 		ASSERT(aclp->z_version >= ZFS_ACL_VERSION_FUID);
1018 		otype = DMU_OT_ACL;
1019 	}
1020 
1021 	if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1022 		/*
1023 		 * If ACL was previously external and we are now
1024 		 * converting to new ACL format then release old
1025 		 * ACL object and create a new one.
1026 		 */
1027 		if (aoid && aclp->z_version != zacl->z_acl_version) {
1028 			error = dmu_object_free(zfsvfs->z_os,
1029 			    zp->z_phys->zp_acl.z_acl_extern_obj, tx);
1030 			if (error)
1031 				return (error);
1032 			aoid = 0;
1033 		}
1034 		if (aoid == 0) {
1035 			aoid = dmu_object_alloc(zfsvfs->z_os,
1036 			    otype, aclp->z_acl_bytes,
1037 			    otype == DMU_OT_ACL ? DMU_OT_SYSACL : DMU_OT_NONE,
1038 			    otype == DMU_OT_ACL ? DN_MAX_BONUSLEN : 0, tx);
1039 		} else {
1040 			(void) dmu_object_set_blocksize(zfsvfs->z_os, aoid,
1041 			    aclp->z_acl_bytes, 0, tx);
1042 		}
1043 		zphys->zp_acl.z_acl_extern_obj = aoid;
1044 		for (aclnode = list_head(&aclp->z_acl); aclnode;
1045 		    aclnode = list_next(&aclp->z_acl, aclnode)) {
1046 			if (aclnode->z_ace_count == 0)
1047 				continue;
1048 			dmu_write(zfsvfs->z_os, aoid, off,
1049 			    aclnode->z_size, aclnode->z_acldata, tx);
1050 			off += aclnode->z_size;
1051 		}
1052 	} else {
1053 		void *start = zacl->z_ace_data;
1054 		/*
1055 		 * Migrating back embedded?
1056 		 */
1057 		if (zphys->zp_acl.z_acl_extern_obj) {
1058 			error = dmu_object_free(zfsvfs->z_os,
1059 			    zp->z_phys->zp_acl.z_acl_extern_obj, tx);
1060 			if (error)
1061 				return (error);
1062 			zphys->zp_acl.z_acl_extern_obj = 0;
1063 		}
1064 
1065 		for (aclnode = list_head(&aclp->z_acl); aclnode;
1066 		    aclnode = list_next(&aclp->z_acl, aclnode)) {
1067 			if (aclnode->z_ace_count == 0)
1068 				continue;
1069 			bcopy(aclnode->z_acldata, start, aclnode->z_size);
1070 			start = (caddr_t)start + aclnode->z_size;
1071 		}
1072 	}
1073 
1074 	/*
1075 	 * If Old version then swap count/bytes to match old
1076 	 * layout of znode_acl_phys_t.
1077 	 */
1078 	if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1079 		zphys->zp_acl.z_acl_size = aclp->z_acl_count;
1080 		zphys->zp_acl.z_acl_count = aclp->z_acl_bytes;
1081 	} else {
1082 		zphys->zp_acl.z_acl_size = aclp->z_acl_bytes;
1083 		zphys->zp_acl.z_acl_count = aclp->z_acl_count;
1084 	}
1085 
1086 	zphys->zp_acl.z_acl_version = aclp->z_version;
1087 
1088 	/*
1089 	 * Replace ACL wide bits, but first clear them.
1090 	 */
1091 	zp->z_phys->zp_flags &= ~ZFS_ACL_WIDE_FLAGS;
1092 
1093 	zp->z_phys->zp_flags |= aclp->z_hints;
1094 
1095 	if (ace_trivial_common(aclp, 0, zfs_ace_walk) == 0)
1096 		zp->z_phys->zp_flags |= ZFS_ACL_TRIVIAL;
1097 
1098 	zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
1099 	return (0);
1100 }
1101 
1102 /*
1103  * Remove ACE from aclp
1104  */
1105 static void
1106 zfs_ace_remove(zfs_acl_t *aclp, void *acep)
1107 {
1108 	zfs_acl_node_t 	*currnode = zfs_acl_curr_node(aclp);
1109 	size_t		length;
1110 
1111 	/*
1112 	 * If first entry then just alter acldata ptr
1113 	 *
1114 	 * Otherwise split node in two
1115 	 */
1116 	if (currnode->z_ace_count > 1) {
1117 		length = currnode->z_size - ((caddr_t)aclp->z_next_ace -
1118 		    (caddr_t)currnode->z_acldata);
1119 		(void) memmove(acep, aclp->z_next_ace, length);
1120 		currnode->z_size = currnode->z_size -
1121 		    aclp->z_ops.ace_size(acep);
1122 		aclp->z_next_ace = acep;
1123 		aclp->z_acl_bytes -= ((caddr_t)aclp->z_next_ace -
1124 		    (caddr_t)currnode->z_acldata);
1125 	} else {
1126 		list_remove(&aclp->z_acl, currnode);
1127 		aclp->z_next_ace = NULL;
1128 		aclp->z_acl_bytes = 0;
1129 	}
1130 	currnode->z_ace_count--;
1131 	currnode->z_ace_idx--;
1132 	aclp->z_acl_count--;
1133 }
1134 
1135 /*
1136  * Update access mask for prepended ACE
1137  *
1138  * This applies the "groupmask" value for aclmode property.
1139  */
1140 static void
1141 zfs_acl_prepend_fixup(zfs_acl_t *aclp, void  *acep, void  *origacep,
1142     mode_t mode, uint64_t owner)
1143 {
1144 	int	rmask, wmask, xmask;
1145 	int	user_ace;
1146 	uint16_t aceflags;
1147 	uint32_t origmask, acepmask;
1148 	uint64_t fuid;
1149 
1150 	aceflags = aclp->z_ops.ace_flags_get(acep);
1151 	fuid = aclp->z_ops.ace_who_get(acep);
1152 	origmask = aclp->z_ops.ace_mask_get(origacep);
1153 	acepmask = aclp->z_ops.ace_mask_get(acep);
1154 
1155 	user_ace = (!(aceflags &
1156 	    (ACE_OWNER|ACE_GROUP|ACE_IDENTIFIER_GROUP)));
1157 
1158 	if (user_ace && (fuid == owner)) {
1159 		rmask = S_IRUSR;
1160 		wmask = S_IWUSR;
1161 		xmask = S_IXUSR;
1162 	} else {
1163 		rmask = S_IRGRP;
1164 		wmask = S_IWGRP;
1165 		xmask = S_IXGRP;
1166 	}
1167 
1168 	if (origmask & ACE_READ_DATA) {
1169 		if (mode & rmask) {
1170 			acepmask &= ~ACE_READ_DATA;
1171 		} else {
1172 			acepmask |= ACE_READ_DATA;
1173 		}
1174 	}
1175 
1176 	if (origmask & ACE_WRITE_DATA) {
1177 		if (mode & wmask) {
1178 			acepmask &= ~ACE_WRITE_DATA;
1179 		} else {
1180 			acepmask |= ACE_WRITE_DATA;
1181 		}
1182 	}
1183 
1184 	if (origmask & ACE_APPEND_DATA) {
1185 		if (mode & wmask) {
1186 			acepmask &= ~ACE_APPEND_DATA;
1187 		} else {
1188 			acepmask |= ACE_APPEND_DATA;
1189 		}
1190 	}
1191 
1192 	if (origmask & ACE_EXECUTE) {
1193 		if (mode & xmask) {
1194 			acepmask &= ~ACE_EXECUTE;
1195 		} else {
1196 			acepmask |= ACE_EXECUTE;
1197 		}
1198 	}
1199 	aclp->z_ops.ace_mask_set(acep, acepmask);
1200 }
1201 
1202 /*
1203  * Apply mode to canonical six ACEs.
1204  */
1205 static void
1206 zfs_acl_fixup_canonical_six(zfs_acl_t *aclp, mode_t mode)
1207 {
1208 	zfs_acl_node_t *aclnode = list_tail(&aclp->z_acl);
1209 	void	*acep;
1210 	int	maskoff = aclp->z_ops.ace_mask_off();
1211 	size_t abstract_size = aclp->z_ops.ace_abstract_size();
1212 
1213 	ASSERT(aclnode != NULL);
1214 
1215 	acep = (void *)((caddr_t)aclnode->z_acldata +
1216 	    aclnode->z_size - (abstract_size * 6));
1217 
1218 	/*
1219 	 * Fixup final ACEs to match the mode
1220 	 */
1221 
1222 	adjust_ace_pair_common(acep, maskoff, abstract_size,
1223 	    (mode & 0700) >> 6);	/* owner@ */
1224 
1225 	acep = (caddr_t)acep + (abstract_size * 2);
1226 
1227 	adjust_ace_pair_common(acep, maskoff, abstract_size,
1228 	    (mode & 0070) >> 3);	/* group@ */
1229 
1230 	acep = (caddr_t)acep + (abstract_size * 2);
1231 	adjust_ace_pair_common(acep, maskoff,
1232 	    abstract_size, mode);	/* everyone@ */
1233 }
1234 
1235 
1236 static int
1237 zfs_acl_ace_match(zfs_acl_t *aclp, void *acep, int allow_deny,
1238     int entry_type, int accessmask)
1239 {
1240 	uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1241 	uint16_t type = aclp->z_ops.ace_type_get(acep);
1242 	uint16_t flags = aclp->z_ops.ace_flags_get(acep);
1243 
1244 	return (mask == accessmask && type == allow_deny &&
1245 	    ((flags & ACE_TYPE_FLAGS) == entry_type));
1246 }
1247 
1248 /*
1249  * Can prepended ACE be reused?
1250  */
1251 static int
1252 zfs_reuse_deny(zfs_acl_t *aclp, void *acep, void *prevacep)
1253 {
1254 	int okay_masks;
1255 	uint16_t prevtype;
1256 	uint16_t prevflags;
1257 	uint16_t flags;
1258 	uint32_t mask, prevmask;
1259 
1260 	if (prevacep == NULL)
1261 		return (B_FALSE);
1262 
1263 	prevtype = aclp->z_ops.ace_type_get(prevacep);
1264 	prevflags = aclp->z_ops.ace_flags_get(prevacep);
1265 	flags = aclp->z_ops.ace_flags_get(acep);
1266 	mask = aclp->z_ops.ace_mask_get(acep);
1267 	prevmask = aclp->z_ops.ace_mask_get(prevacep);
1268 
1269 	if (prevtype != DENY)
1270 		return (B_FALSE);
1271 
1272 	if (prevflags != (flags & ACE_IDENTIFIER_GROUP))
1273 		return (B_FALSE);
1274 
1275 	okay_masks = (mask & OKAY_MASK_BITS);
1276 
1277 	if (prevmask & ~okay_masks)
1278 		return (B_FALSE);
1279 
1280 	return (B_TRUE);
1281 }
1282 
1283 
1284 /*
1285  * Insert new ACL node into chain of zfs_acl_node_t's
1286  *
1287  * This will result in two possible results.
1288  * 1. If the ACL is currently just a single zfs_acl_node and
1289  *    we are prepending the entry then current acl node will have
1290  *    a new node inserted above it.
1291  *
1292  * 2. If we are inserting in the middle of current acl node then
1293  *    the current node will be split in two and new node will be inserted
1294  *    in between the two split nodes.
1295  */
1296 static zfs_acl_node_t *
1297 zfs_acl_ace_insert(zfs_acl_t *aclp, void  *acep)
1298 {
1299 	zfs_acl_node_t 	*newnode;
1300 	zfs_acl_node_t 	*trailernode = NULL;
1301 	zfs_acl_node_t 	*currnode = zfs_acl_curr_node(aclp);
1302 	int		curr_idx = aclp->z_curr_node->z_ace_idx;
1303 	int		trailer_count;
1304 	size_t		oldsize;
1305 
1306 	newnode = zfs_acl_node_alloc(aclp->z_ops.ace_size(acep));
1307 	newnode->z_ace_count = 1;
1308 
1309 	oldsize = currnode->z_size;
1310 
1311 	if (curr_idx != 1) {
1312 		trailernode = zfs_acl_node_alloc(0);
1313 		trailernode->z_acldata = acep;
1314 
1315 		trailer_count = currnode->z_ace_count - curr_idx + 1;
1316 		currnode->z_ace_count = curr_idx - 1;
1317 		currnode->z_size = (caddr_t)acep - (caddr_t)currnode->z_acldata;
1318 		trailernode->z_size = oldsize - currnode->z_size;
1319 		trailernode->z_ace_count = trailer_count;
1320 	}
1321 
1322 	aclp->z_acl_count += 1;
1323 	aclp->z_acl_bytes += aclp->z_ops.ace_size(acep);
1324 
1325 	if (curr_idx == 1)
1326 		list_insert_before(&aclp->z_acl, currnode, newnode);
1327 	else
1328 		list_insert_after(&aclp->z_acl, currnode, newnode);
1329 	if (trailernode) {
1330 		list_insert_after(&aclp->z_acl, newnode, trailernode);
1331 		aclp->z_curr_node = trailernode;
1332 		trailernode->z_ace_idx = 1;
1333 	}
1334 
1335 	return (newnode);
1336 }
1337 
1338 /*
1339  * Prepend deny ACE
1340  */
1341 static void *
1342 zfs_acl_prepend_deny(znode_t *zp, zfs_acl_t *aclp, void *acep,
1343     mode_t mode)
1344 {
1345 	zfs_acl_node_t *aclnode;
1346 	void  *newacep;
1347 	uint64_t fuid;
1348 	uint16_t flags;
1349 
1350 	aclnode = zfs_acl_ace_insert(aclp, acep);
1351 	newacep = aclnode->z_acldata;
1352 	fuid = aclp->z_ops.ace_who_get(acep);
1353 	flags = aclp->z_ops.ace_flags_get(acep);
1354 	zfs_set_ace(aclp, newacep, 0, DENY, fuid, (flags & ACE_TYPE_FLAGS));
1355 	zfs_acl_prepend_fixup(aclp, newacep, acep, mode, zp->z_phys->zp_uid);
1356 
1357 	return (newacep);
1358 }
1359 
1360 /*
1361  * Split an inherited ACE into inherit_only ACE
1362  * and original ACE with inheritance flags stripped off.
1363  */
1364 static void
1365 zfs_acl_split_ace(zfs_acl_t *aclp, zfs_ace_hdr_t *acep)
1366 {
1367 	zfs_acl_node_t *aclnode;
1368 	zfs_acl_node_t *currnode = zfs_acl_curr_node(aclp);
1369 	void  *newacep;
1370 	uint16_t type, flags;
1371 	uint32_t mask;
1372 	uint64_t fuid;
1373 
1374 	type = aclp->z_ops.ace_type_get(acep);
1375 	flags = aclp->z_ops.ace_flags_get(acep);
1376 	mask = aclp->z_ops.ace_mask_get(acep);
1377 	fuid = aclp->z_ops.ace_who_get(acep);
1378 
1379 	aclnode = zfs_acl_ace_insert(aclp, acep);
1380 	newacep = aclnode->z_acldata;
1381 
1382 	aclp->z_ops.ace_type_set(newacep, type);
1383 	aclp->z_ops.ace_flags_set(newacep, flags | ACE_INHERIT_ONLY_ACE);
1384 	aclp->z_ops.ace_mask_set(newacep, mask);
1385 	aclp->z_ops.ace_type_set(newacep, type);
1386 	aclp->z_ops.ace_who_set(newacep, fuid);
1387 	aclp->z_next_ace = acep;
1388 	flags &= ~ALL_INHERIT;
1389 	aclp->z_ops.ace_flags_set(acep, flags);
1390 	currnode->z_ace_idx -= 1;
1391 }
1392 
1393 /*
1394  * Are ACES started at index i, the canonical six ACES?
1395  */
1396 static int
1397 zfs_have_canonical_six(zfs_acl_t *aclp)
1398 {
1399 	void *acep;
1400 	zfs_acl_node_t *aclnode = list_tail(&aclp->z_acl);
1401 	int		i = 0;
1402 	size_t abstract_size = aclp->z_ops.ace_abstract_size();
1403 
1404 	ASSERT(aclnode != NULL);
1405 
1406 	if (aclnode->z_ace_count < 6)
1407 		return (0);
1408 
1409 	acep = (void *)((caddr_t)aclnode->z_acldata +
1410 	    aclnode->z_size - (aclp->z_ops.ace_abstract_size() * 6));
1411 
1412 	if ((zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++),
1413 	    DENY, ACE_OWNER, 0) &&
1414 	    zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++),
1415 	    ALLOW, ACE_OWNER, OWNER_ALLOW_MASK) &&
1416 	    zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++), DENY,
1417 	    OWNING_GROUP, 0) && zfs_acl_ace_match(aclp, (caddr_t)acep +
1418 	    (abstract_size * i++),
1419 	    ALLOW, OWNING_GROUP, 0) &&
1420 	    zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++),
1421 	    DENY, ACE_EVERYONE, EVERYONE_DENY_MASK) &&
1422 	    zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++),
1423 	    ALLOW, ACE_EVERYONE, EVERYONE_ALLOW_MASK))) {
1424 		return (1);
1425 	} else {
1426 		return (0);
1427 	}
1428 }
1429 
1430 
1431 /*
1432  * Apply step 1g, to group entries
1433  *
1434  * Need to deal with corner case where group may have
1435  * greater permissions than owner.  If so then limit
1436  * group permissions, based on what extra permissions
1437  * group has.
1438  */
1439 static void
1440 zfs_fixup_group_entries(zfs_acl_t *aclp, void *acep, void *prevacep,
1441     mode_t mode)
1442 {
1443 	uint32_t prevmask = aclp->z_ops.ace_mask_get(prevacep);
1444 	uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1445 	uint16_t prevflags = aclp->z_ops.ace_flags_get(prevacep);
1446 	mode_t extramode = (mode >> 3) & 07;
1447 	mode_t ownermode = (mode >> 6);
1448 
1449 	if (prevflags & ACE_IDENTIFIER_GROUP) {
1450 
1451 		extramode &= ~ownermode;
1452 
1453 		if (extramode) {
1454 			if (extramode & S_IROTH) {
1455 				prevmask &= ~ACE_READ_DATA;
1456 				mask &= ~ACE_READ_DATA;
1457 			}
1458 			if (extramode & S_IWOTH) {
1459 				prevmask &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
1460 				mask &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
1461 			}
1462 			if (extramode & S_IXOTH) {
1463 				prevmask  &= ~ACE_EXECUTE;
1464 				mask &= ~ACE_EXECUTE;
1465 			}
1466 		}
1467 	}
1468 	aclp->z_ops.ace_mask_set(acep, mask);
1469 	aclp->z_ops.ace_mask_set(prevacep, prevmask);
1470 }
1471 
1472 /*
1473  * Apply the chmod algorithm as described
1474  * in PSARC/2002/240
1475  */
1476 static int
1477 zfs_acl_chmod(znode_t *zp, uint64_t mode, zfs_acl_t *aclp,
1478     dmu_tx_t *tx)
1479 {
1480 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1481 	void		*acep = NULL, *prevacep = NULL;
1482 	uint64_t	who;
1483 	int 		i;
1484 	int		error;
1485 	int 		entry_type;
1486 	int 		reuse_deny;
1487 	int 		need_canonical_six = 1;
1488 	uint16_t	iflags, type;
1489 	uint32_t	access_mask;
1490 
1491 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1492 	ASSERT(MUTEX_HELD(&zp->z_lock));
1493 
1494 	aclp->z_hints = (zp->z_phys->zp_flags & V4_ACL_WIDE_FLAGS);
1495 	while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
1496 	    &iflags, &type)) {
1497 
1498 		entry_type = (iflags & ACE_TYPE_FLAGS);
1499 		iflags = (iflags & ALL_INHERIT);
1500 
1501 		if ((type != ALLOW && type != DENY) ||
1502 		    (iflags & ACE_INHERIT_ONLY_ACE)) {
1503 			if (iflags)
1504 				aclp->z_hints |= ZFS_INHERIT_ACE;
1505 			switch (type) {
1506 			case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1507 			case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1508 			case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1509 			case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1510 				aclp->z_hints |= ZFS_ACL_OBJ_ACE;
1511 				break;
1512 			}
1513 			goto nextace;
1514 		}
1515 
1516 		if (zfsvfs->z_acl_mode == ZFS_ACL_DISCARD) {
1517 			zfs_ace_remove(aclp, acep);
1518 			goto nextace;
1519 		}
1520 		/*
1521 		 * Need to split ace into two?
1522 		 */
1523 		if ((iflags & (ACE_FILE_INHERIT_ACE|
1524 		    ACE_DIRECTORY_INHERIT_ACE)) &&
1525 		    (!(iflags & ACE_INHERIT_ONLY_ACE))) {
1526 			zfs_acl_split_ace(aclp, acep);
1527 			aclp->z_hints |= ZFS_INHERIT_ACE;
1528 			goto nextace;
1529 		}
1530 
1531 		if (entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
1532 		    (entry_type == OWNING_GROUP)) {
1533 			access_mask &= ~OGE_CLEAR;
1534 			aclp->z_ops.ace_mask_set(acep, access_mask);
1535 			goto nextace;
1536 		} else {
1537 			reuse_deny = B_TRUE;
1538 			if (type == ALLOW) {
1539 
1540 				/*
1541 				 * Check preceding ACE if any, to see
1542 				 * if we need to prepend a DENY ACE.
1543 				 * This is only applicable when the acl_mode
1544 				 * property == groupmask.
1545 				 */
1546 				if (zfsvfs->z_acl_mode == ZFS_ACL_GROUPMASK) {
1547 
1548 					reuse_deny = zfs_reuse_deny(aclp, acep,
1549 					    prevacep);
1550 
1551 					if (reuse_deny == B_FALSE) {
1552 						prevacep =
1553 						    zfs_acl_prepend_deny(zp,
1554 						    aclp, acep, mode);
1555 					} else {
1556 						zfs_acl_prepend_fixup(
1557 						    aclp, prevacep,
1558 						    acep, mode,
1559 						    zp->z_phys->zp_uid);
1560 					}
1561 					zfs_fixup_group_entries(aclp, acep,
1562 					    prevacep, mode);
1563 
1564 				}
1565 			}
1566 		}
1567 nextace:
1568 		prevacep = acep;
1569 	}
1570 
1571 	/*
1572 	 * Check out last six aces, if we have six.
1573 	 */
1574 
1575 	if (aclp->z_acl_count >= 6) {
1576 		if (zfs_have_canonical_six(aclp)) {
1577 			need_canonical_six = 0;
1578 		}
1579 	}
1580 
1581 	if (need_canonical_six) {
1582 		size_t abstract_size = aclp->z_ops.ace_abstract_size();
1583 		void *zacep;
1584 		zfs_acl_node_t *aclnode =
1585 		    zfs_acl_node_alloc(abstract_size * 6);
1586 
1587 		aclnode->z_size = abstract_size * 6;
1588 		aclnode->z_ace_count = 6;
1589 		aclp->z_acl_bytes += aclnode->z_size;
1590 		list_insert_tail(&aclp->z_acl, aclnode);
1591 
1592 		zacep = aclnode->z_acldata;
1593 
1594 		i = 0;
1595 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++),
1596 		    0, DENY, -1, ACE_OWNER);
1597 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++),
1598 		    OWNER_ALLOW_MASK, ALLOW, -1, ACE_OWNER);
1599 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 0,
1600 		    DENY, -1, OWNING_GROUP);
1601 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 0,
1602 		    ALLOW, -1, OWNING_GROUP);
1603 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++),
1604 		    EVERYONE_DENY_MASK, DENY, -1, ACE_EVERYONE);
1605 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++),
1606 		    EVERYONE_ALLOW_MASK, ALLOW, -1, ACE_EVERYONE);
1607 		aclp->z_acl_count += 6;
1608 	}
1609 
1610 	zfs_acl_fixup_canonical_six(aclp, mode);
1611 	zp->z_phys->zp_mode = mode;
1612 	error = zfs_aclset_common(zp, aclp, NULL, tx);
1613 	return (error);
1614 }
1615 
1616 int
1617 zfs_acl_chmod_setattr(znode_t *zp, uint64_t mode, dmu_tx_t *tx)
1618 {
1619 	zfs_acl_t *aclp = NULL;
1620 	int error;
1621 
1622 	ASSERT(MUTEX_HELD(&zp->z_lock));
1623 	mutex_enter(&zp->z_acl_lock);
1624 	error = zfs_acl_node_read(zp, &aclp, B_TRUE);
1625 	if (error == 0)
1626 		error = zfs_acl_chmod(zp, mode, aclp, tx);
1627 	mutex_exit(&zp->z_acl_lock);
1628 	if (aclp)
1629 		zfs_acl_free(aclp);
1630 	return (error);
1631 }
1632 
1633 /*
1634  * strip off write_owner and write_acl
1635  */
1636 static void
1637 zfs_securemode_update(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, void *acep)
1638 {
1639 	uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1640 
1641 	if ((zfsvfs->z_acl_inherit == ZFS_ACL_SECURE) &&
1642 	    (aclp->z_ops.ace_type_get(acep) == ALLOW)) {
1643 		mask &= ~SECURE_CLEAR;
1644 		aclp->z_ops.ace_mask_set(acep, mask);
1645 	}
1646 }
1647 
1648 /*
1649  * Should ACE be inherited?
1650  */
1651 static int
1652 zfs_ace_can_use(znode_t *zp, uint16_t acep_flags)
1653 {
1654 	int vtype = ZTOV(zp)->v_type;
1655 	int	iflags = (acep_flags & 0xf);
1656 
1657 	if ((vtype == VDIR) && (iflags & ACE_DIRECTORY_INHERIT_ACE))
1658 		return (1);
1659 	else if (iflags & ACE_FILE_INHERIT_ACE)
1660 		return (!((vtype == VDIR) &&
1661 		    (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)));
1662 	return (0);
1663 }
1664 
1665 /*
1666  * inherit inheritable ACEs from parent
1667  */
1668 static zfs_acl_t *
1669 zfs_acl_inherit(znode_t *zp, zfs_acl_t *paclp)
1670 {
1671 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1672 	void		*pacep;
1673 	void		*acep, *acep2;
1674 	zfs_acl_node_t  *aclnode, *aclnode2;
1675 	zfs_acl_t	*aclp = NULL;
1676 	uint64_t	who;
1677 	uint32_t	access_mask;
1678 	uint16_t	iflags, newflags, type;
1679 	size_t		ace_size;
1680 	void		*data1, *data2;
1681 	size_t		data1sz, data2sz;
1682 
1683 	pacep = NULL;
1684 	aclp = zfs_acl_alloc(zfs_acl_version_zp(zp));
1685 	if (zfsvfs->z_acl_inherit != ZFS_ACL_DISCARD) {
1686 		while (pacep = zfs_acl_next_ace(paclp, pacep, &who,
1687 		    &access_mask, &iflags, &type)) {
1688 
1689 			if (zfsvfs->z_acl_inherit == ZFS_ACL_NOALLOW &&
1690 			    type == ALLOW)
1691 				continue;
1692 
1693 			ace_size = aclp->z_ops.ace_size(pacep);
1694 
1695 			if (zfs_ace_can_use(zp, iflags)) {
1696 				aclnode =
1697 				    zfs_acl_node_alloc(ace_size);
1698 
1699 				list_insert_tail(&aclp->z_acl, aclnode);
1700 				acep = aclnode->z_acldata;
1701 				zfs_set_ace(aclp, acep, access_mask, type,
1702 				    who, iflags|ACE_INHERITED_ACE);
1703 
1704 				/*
1705 				 * Copy special opaque data if any
1706 				 */
1707 				if ((data1sz = paclp->z_ops.ace_data(pacep,
1708 				    &data1)) != 0) {
1709 					VERIFY((data2sz =
1710 					    aclp->z_ops.ace_data(acep,
1711 					    &data2)) == data1sz);
1712 					bcopy(data1, data2, data2sz);
1713 				}
1714 				aclp->z_acl_count++;
1715 				aclnode->z_ace_count++;
1716 				aclp->z_acl_bytes += aclnode->z_size;
1717 				newflags = aclp->z_ops.ace_flags_get(acep);
1718 				if ((iflags &
1719 				    ACE_NO_PROPAGATE_INHERIT_ACE) ||
1720 				    (ZTOV(zp)->v_type != VDIR)) {
1721 					newflags &= ~ALL_INHERIT;
1722 					aclp->z_ops.ace_flags_set(acep,
1723 					    newflags|ACE_INHERITED_ACE);
1724 					zfs_securemode_update(zfsvfs,
1725 					    aclp, acep);
1726 					continue;
1727 				}
1728 
1729 				ASSERT(ZTOV(zp)->v_type == VDIR);
1730 
1731 				newflags = aclp->z_ops.ace_flags_get(acep);
1732 				if ((iflags & (ACE_FILE_INHERIT_ACE |
1733 				    ACE_DIRECTORY_INHERIT_ACE)) !=
1734 				    ACE_FILE_INHERIT_ACE) {
1735 					aclnode2 = zfs_acl_node_alloc(ace_size);
1736 					list_insert_tail(&aclp->z_acl,
1737 					    aclnode2);
1738 					acep2 = aclnode2->z_acldata;
1739 					zfs_set_ace(aclp, acep2,
1740 					    access_mask, type, who,
1741 					    iflags|ACE_INHERITED_ACE);
1742 					newflags |= ACE_INHERIT_ONLY_ACE;
1743 					aclp->z_ops.ace_flags_set(acep,
1744 					    newflags);
1745 					newflags &= ~ALL_INHERIT;
1746 					aclp->z_ops.ace_flags_set(acep2,
1747 					    newflags|ACE_INHERITED_ACE);
1748 
1749 					/*
1750 					 * Copy special opaque data if any
1751 					 */
1752 					if ((data1sz =
1753 					    aclp->z_ops.ace_data(acep,
1754 					    &data1)) != 0) {
1755 						VERIFY((data2sz =
1756 						    aclp->z_ops.ace_data(acep2,
1757 						    &data2)) == data1sz);
1758 						bcopy(data1, data2, data1sz);
1759 					}
1760 					aclp->z_acl_count++;
1761 					aclnode2->z_ace_count++;
1762 					aclp->z_acl_bytes += aclnode->z_size;
1763 					zfs_securemode_update(zfsvfs,
1764 					    aclp, acep2);
1765 				} else {
1766 					newflags |= ACE_INHERIT_ONLY_ACE;
1767 					aclp->z_ops.ace_flags_set(acep,
1768 					    newflags|ACE_INHERITED_ACE);
1769 				}
1770 
1771 			}
1772 		}
1773 	}
1774 	return (aclp);
1775 }
1776 
1777 /*
1778  * Create file system object initial permissions
1779  * including inheritable ACEs.
1780  */
1781 void
1782 zfs_perm_init(znode_t *zp, znode_t *parent, int flag,
1783     vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
1784     zfs_acl_t *setaclp, zfs_fuid_info_t **fuidp)
1785 {
1786 	uint64_t	mode;
1787 	uint64_t	uid;
1788 	uint64_t	gid;
1789 	int		error;
1790 	int		pull_down;
1791 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1792 	zfs_acl_t	*aclp = NULL;
1793 	zfs_acl_t	*paclp;
1794 	xvattr_t	*xvap = (xvattr_t *)vap;
1795 
1796 	if (setaclp)
1797 		aclp = setaclp;
1798 
1799 	mode = MAKEIMODE(vap->va_type, vap->va_mode);
1800 
1801 	/*
1802 	 * Determine uid and gid.
1803 	 */
1804 	if ((flag & (IS_ROOT_NODE | IS_REPLAY)) ||
1805 	    ((flag & IS_XATTR) && (vap->va_type == VDIR))) {
1806 		uid = zfs_fuid_create(zfsvfs, vap->va_uid,
1807 		    ZFS_OWNER, tx, fuidp);
1808 		gid = zfs_fuid_create(zfsvfs, vap->va_gid,
1809 		    ZFS_GROUP, tx, fuidp);
1810 	} else {
1811 		uid = zfs_fuid_create_cred(zfsvfs, crgetuid(cr),
1812 		    ZFS_OWNER, tx, cr, fuidp);
1813 		if ((vap->va_mask & AT_GID) &&
1814 		    ((vap->va_gid == parent->z_phys->zp_gid) ||
1815 		    groupmember(vap->va_gid, cr) ||
1816 		    secpolicy_vnode_create_gid(cr) == 0)) {
1817 			gid = zfs_fuid_create_cred(zfsvfs, vap->va_gid,
1818 			    ZFS_GROUP, tx, cr, fuidp);
1819 		} else {
1820 			gid = (parent->z_phys->zp_mode & S_ISGID) ?
1821 			    parent->z_phys->zp_gid : crgetgid(cr);
1822 			gid = zfs_fuid_create_cred(zfsvfs, gid,
1823 			    ZFS_GROUP, tx, cr, fuidp);
1824 		}
1825 	}
1826 
1827 	/*
1828 	 * If we're creating a directory, and the parent directory has the
1829 	 * set-GID bit set, set in on the new directory.
1830 	 * Otherwise, if the user is neither privileged nor a member of the
1831 	 * file's new group, clear the file's set-GID bit.
1832 	 */
1833 
1834 	if ((parent->z_phys->zp_mode & S_ISGID) && (vap->va_type == VDIR))
1835 		mode |= S_ISGID;
1836 	else {
1837 		if ((mode & S_ISGID) &&
1838 		    secpolicy_vnode_setids_setgids(cr, gid) != 0)
1839 			mode &= ~S_ISGID;
1840 	}
1841 
1842 	zp->z_phys->zp_uid = uid;
1843 	zp->z_phys->zp_gid = gid;
1844 	zp->z_phys->zp_mode = mode;
1845 
1846 	if (aclp == NULL) {
1847 		mutex_enter(&parent->z_lock);
1848 		pull_down = (parent->z_phys->zp_flags & ZFS_INHERIT_ACE);
1849 		if (pull_down) {
1850 			mutex_enter(&parent->z_acl_lock);
1851 			VERIFY(0 == zfs_acl_node_read(parent, &paclp, B_FALSE));
1852 			mutex_exit(&parent->z_acl_lock);
1853 			aclp = zfs_acl_inherit(zp, paclp);
1854 			zfs_acl_free(paclp);
1855 		} else {
1856 			aclp = zfs_acl_alloc(zfs_acl_version_zp(zp));
1857 		}
1858 		mutex_exit(&parent->z_lock);
1859 		mutex_enter(&zp->z_lock);
1860 		mutex_enter(&zp->z_acl_lock);
1861 		error = zfs_acl_chmod(zp, mode, aclp, tx);
1862 	} else {
1863 		mutex_enter(&zp->z_lock);
1864 		mutex_enter(&zp->z_acl_lock);
1865 	}
1866 
1867 	/* Force auto_inherit on all new directory objects */
1868 	if (vap->va_type == VDIR)
1869 		aclp->z_hints |= ZFS_ACL_AUTO_INHERIT;
1870 
1871 	error = zfs_aclset_common(zp, aclp, fuidp, tx);
1872 
1873 	/* Set optional attributes if any */
1874 	if (vap->va_mask & AT_XVATTR)
1875 		zfs_xvattr_set(zp, xvap);
1876 
1877 	mutex_exit(&zp->z_lock);
1878 	mutex_exit(&zp->z_acl_lock);
1879 	ASSERT3U(error, ==, 0);
1880 
1881 	if (aclp != setaclp) {
1882 		zfs_acl_free(aclp);
1883 	}
1884 }
1885 
1886 /*
1887  * Retrieve a files ACL
1888  */
1889 int
1890 zfs_getacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
1891 {
1892 	zfs_acl_t	*aclp;
1893 	ulong_t		mask;
1894 	int		error;
1895 	int 		count = 0;
1896 	int		largeace = 0;
1897 
1898 	mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT |
1899 	    VSA_ACE_ACLFLAGS | VSA_ACE_ALLTYPES);
1900 
1901 	if (error = zfs_zaccess(zp, ACE_READ_ACL, 0, skipaclchk, cr))
1902 		return (error);
1903 
1904 	if (mask == 0)
1905 		return (ENOSYS);
1906 
1907 	mutex_enter(&zp->z_acl_lock);
1908 
1909 	error = zfs_acl_node_read(zp, &aclp, B_FALSE);
1910 	if (error != 0) {
1911 		mutex_exit(&zp->z_acl_lock);
1912 		return (error);
1913 	}
1914 
1915 	/*
1916 	 * Scan ACL to determine number of ACEs
1917 	 */
1918 	if ((zp->z_phys->zp_flags & ZFS_ACL_OBJ_ACE) &&
1919 	    !(mask & VSA_ACE_ALLTYPES)) {
1920 		void *zacep = NULL;
1921 		uint64_t who;
1922 		uint32_t access_mask;
1923 		uint16_t type, iflags;
1924 
1925 		while (zacep = zfs_acl_next_ace(aclp, zacep,
1926 		    &who, &access_mask, &iflags, &type)) {
1927 			switch (type) {
1928 			case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1929 			case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1930 			case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1931 			case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1932 				largeace++;
1933 				continue;
1934 			default:
1935 				count++;
1936 			}
1937 		}
1938 		vsecp->vsa_aclcnt = count;
1939 	} else
1940 		count = aclp->z_acl_count;
1941 
1942 	if (mask & VSA_ACECNT) {
1943 		vsecp->vsa_aclcnt = count;
1944 	}
1945 
1946 	if (mask & VSA_ACE) {
1947 		size_t aclsz;
1948 
1949 		zfs_acl_node_t *aclnode = list_head(&aclp->z_acl);
1950 
1951 		aclsz = count * sizeof (ace_t) +
1952 		    sizeof (ace_object_t) * largeace;
1953 
1954 		vsecp->vsa_aclentp = kmem_alloc(aclsz, KM_SLEEP);
1955 		vsecp->vsa_aclentsz = aclsz;
1956 
1957 		if (aclp->z_version == ZFS_ACL_VERSION_FUID)
1958 			zfs_copy_fuid_2_ace(zp->z_zfsvfs, aclp,
1959 			    vsecp->vsa_aclentp, !(mask & VSA_ACE_ALLTYPES));
1960 		else {
1961 			bcopy(aclnode->z_acldata, vsecp->vsa_aclentp,
1962 			    count * sizeof (ace_t));
1963 		}
1964 	}
1965 	if (mask & VSA_ACE_ACLFLAGS) {
1966 		vsecp->vsa_aclflags = 0;
1967 		if (zp->z_phys->zp_flags & ZFS_ACL_DEFAULTED)
1968 			vsecp->vsa_aclflags |= ACL_DEFAULTED;
1969 		if (zp->z_phys->zp_flags & ZFS_ACL_PROTECTED)
1970 			vsecp->vsa_aclflags |= ACL_PROTECTED;
1971 		if (zp->z_phys->zp_flags & ZFS_ACL_AUTO_INHERIT)
1972 			vsecp->vsa_aclflags |= ACL_AUTO_INHERIT;
1973 	}
1974 
1975 	mutex_exit(&zp->z_acl_lock);
1976 
1977 	zfs_acl_free(aclp);
1978 
1979 	return (0);
1980 }
1981 
1982 int
1983 zfs_vsec_2_aclp(zfsvfs_t *zfsvfs, vtype_t obj_type,
1984     vsecattr_t *vsecp, zfs_acl_t **zaclp)
1985 {
1986 	zfs_acl_t *aclp;
1987 	zfs_acl_node_t *aclnode;
1988 	int aclcnt = vsecp->vsa_aclcnt;
1989 	int error;
1990 
1991 	if (vsecp->vsa_aclcnt > MAX_ACL_ENTRIES || vsecp->vsa_aclcnt <= 0)
1992 		return (EINVAL);
1993 
1994 	aclp = zfs_acl_alloc(zfs_acl_version(zfsvfs->z_version));
1995 
1996 	aclp->z_hints = 0;
1997 	aclnode = zfs_acl_node_alloc(aclcnt * sizeof (zfs_object_ace_t));
1998 	if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1999 		if ((error = zfs_copy_ace_2_oldace(obj_type, aclp,
2000 		    (ace_t *)vsecp->vsa_aclentp, aclnode->z_acldata,
2001 		    aclcnt, &aclnode->z_size)) != 0) {
2002 			zfs_acl_free(aclp);
2003 			zfs_acl_node_free(aclnode);
2004 			return (error);
2005 		}
2006 	} else {
2007 		if ((error = zfs_copy_ace_2_fuid(obj_type, aclp,
2008 		    vsecp->vsa_aclentp, aclnode->z_acldata, aclcnt,
2009 		    &aclnode->z_size)) != 0) {
2010 			zfs_acl_free(aclp);
2011 			zfs_acl_node_free(aclnode);
2012 			return (error);
2013 		}
2014 	}
2015 	aclp->z_acl_bytes = aclnode->z_size;
2016 	aclnode->z_ace_count = aclcnt;
2017 	aclp->z_acl_count = aclcnt;
2018 	list_insert_head(&aclp->z_acl, aclnode);
2019 
2020 	/*
2021 	 * If flags are being set then add them to z_hints
2022 	 */
2023 	if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) {
2024 		if (vsecp->vsa_aclflags & ACL_PROTECTED)
2025 			aclp->z_hints |= ZFS_ACL_PROTECTED;
2026 		if (vsecp->vsa_aclflags & ACL_DEFAULTED)
2027 			aclp->z_hints |= ZFS_ACL_DEFAULTED;
2028 		if (vsecp->vsa_aclflags & ACL_AUTO_INHERIT)
2029 			aclp->z_hints |= ZFS_ACL_AUTO_INHERIT;
2030 	}
2031 
2032 	*zaclp = aclp;
2033 
2034 	return (0);
2035 }
2036 
2037 /*
2038  * Set a files ACL
2039  */
2040 int
2041 zfs_setacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
2042 {
2043 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2044 	zilog_t		*zilog = zfsvfs->z_log;
2045 	ulong_t		mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT);
2046 	dmu_tx_t	*tx;
2047 	int		error;
2048 	zfs_acl_t	*aclp;
2049 	zfs_fuid_info_t	*fuidp = NULL;
2050 
2051 	if (mask == 0)
2052 		return (ENOSYS);
2053 
2054 	if (zp->z_phys->zp_flags & ZFS_IMMUTABLE)
2055 		return (EPERM);
2056 
2057 	if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr))
2058 		return (error);
2059 
2060 	error = zfs_vsec_2_aclp(zfsvfs, ZTOV(zp)->v_type, vsecp, &aclp);
2061 	if (error)
2062 		return (error);
2063 
2064 	/*
2065 	 * If ACL wide flags aren't being set then preserve any
2066 	 * existing flags.
2067 	 */
2068 	if (!(vsecp->vsa_mask & VSA_ACE_ACLFLAGS)) {
2069 		aclp->z_hints |= (zp->z_phys->zp_flags & V4_ACL_WIDE_FLAGS);
2070 	}
2071 top:
2072 	if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr)) {
2073 		zfs_acl_free(aclp);
2074 		return (error);
2075 	}
2076 
2077 	mutex_enter(&zp->z_lock);
2078 	mutex_enter(&zp->z_acl_lock);
2079 
2080 	tx = dmu_tx_create(zfsvfs->z_os);
2081 	dmu_tx_hold_bonus(tx, zp->z_id);
2082 
2083 	if (zp->z_phys->zp_acl.z_acl_extern_obj) {
2084 		/* Are we upgrading ACL? */
2085 		if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
2086 		    zp->z_phys->zp_acl.z_acl_version ==
2087 		    ZFS_ACL_VERSION_INITIAL) {
2088 			dmu_tx_hold_free(tx,
2089 			    zp->z_phys->zp_acl.z_acl_extern_obj,
2090 			    0, DMU_OBJECT_END);
2091 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2092 			    0, sizeof (zfs_object_ace_t) * 2048 + 6);
2093 		} else {
2094 			dmu_tx_hold_write(tx,
2095 			    zp->z_phys->zp_acl.z_acl_extern_obj,
2096 			    0, aclp->z_acl_bytes);
2097 		}
2098 	} else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2099 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, aclp->z_acl_bytes);
2100 	}
2101 	if (zfsvfs->z_fuid_obj == 0) {
2102 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2103 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2104 			    SPA_MAXBLOCKSIZE);
2105 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
2106 	} else {
2107 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
2108 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
2109 		    SPA_MAXBLOCKSIZE);
2110 	}
2111 
2112 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2113 	if (error) {
2114 		mutex_exit(&zp->z_acl_lock);
2115 		mutex_exit(&zp->z_lock);
2116 
2117 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2118 			dmu_tx_wait(tx);
2119 			dmu_tx_abort(tx);
2120 			goto top;
2121 		}
2122 		dmu_tx_abort(tx);
2123 		zfs_acl_free(aclp);
2124 		return (error);
2125 	}
2126 
2127 	error = zfs_aclset_common(zp, aclp, &fuidp, tx);
2128 	ASSERT(error == 0);
2129 
2130 	zfs_log_acl(zilog, tx, zp, vsecp, fuidp);
2131 
2132 	if (fuidp)
2133 		zfs_fuid_info_free(fuidp);
2134 	zfs_acl_free(aclp);
2135 	dmu_tx_commit(tx);
2136 done:
2137 	mutex_exit(&zp->z_acl_lock);
2138 	mutex_exit(&zp->z_lock);
2139 
2140 	return (error);
2141 }
2142 
2143 /*
2144  * working_mode returns the permissions that were not granted
2145  */
2146 static int
2147 zfs_zaccess_common(znode_t *zp, uint32_t v4_mode, uint32_t *working_mode,
2148     boolean_t *check_privs, boolean_t skipaclchk, cred_t *cr)
2149 {
2150 	zfs_acl_t	*aclp;
2151 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2152 	int		error;
2153 	int		access_deny = ACCESS_UNDETERMINED;
2154 	uid_t		uid = crgetuid(cr);
2155 	uint64_t 	who;
2156 	uint16_t	type, iflags;
2157 	uint16_t	entry_type;
2158 	uint32_t	access_mask;
2159 	zfs_ace_hdr_t	*acep = NULL;
2160 	boolean_t	checkit;
2161 	uid_t		fowner;
2162 	uid_t		gowner;
2163 
2164 	/*
2165 	 * Short circuit empty requests
2166 	 */
2167 	if (v4_mode == 0)
2168 		return (0);
2169 
2170 	*check_privs = B_TRUE;
2171 
2172 	if (zfsvfs->z_assign >= TXG_INITIAL) {		/* ZIL replay */
2173 		*working_mode = 0;
2174 		return (0);
2175 	}
2176 
2177 	*working_mode = v4_mode;
2178 
2179 	if ((v4_mode & WRITE_MASK) &&
2180 	    (zp->z_zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) &&
2181 	    (!IS_DEVVP(ZTOV(zp)))) {
2182 		*check_privs = B_FALSE;
2183 		return (EROFS);
2184 	}
2185 
2186 	/*
2187 	 * Only check for READONLY on non-directories.
2188 	 */
2189 	if ((v4_mode & WRITE_MASK_DATA) &&
2190 	    (((ZTOV(zp)->v_type != VDIR) &&
2191 	    (zp->z_phys->zp_flags & (ZFS_READONLY | ZFS_IMMUTABLE))) ||
2192 	    (ZTOV(zp)->v_type == VDIR &&
2193 	    (zp->z_phys->zp_flags & ZFS_IMMUTABLE)))) {
2194 		*check_privs = B_FALSE;
2195 		return (EPERM);
2196 	}
2197 
2198 	if ((v4_mode & (ACE_DELETE | ACE_DELETE_CHILD)) &&
2199 	    (zp->z_phys->zp_flags & ZFS_NOUNLINK)) {
2200 		*check_privs = B_FALSE;
2201 		return (EPERM);
2202 	}
2203 
2204 	if (((v4_mode & (ACE_READ_DATA|ACE_EXECUTE)) &&
2205 	    (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED))) {
2206 		*check_privs = B_FALSE;
2207 		return (EACCES);
2208 	}
2209 
2210 	/*
2211 	 * The caller requested that the ACL check be skipped.  This
2212 	 * would only happen if the caller checked VOP_ACCESS() with a
2213 	 * 32 bit ACE mask and already had the appropriate permissions.
2214 	 */
2215 	if (skipaclchk) {
2216 		*working_mode = 0;
2217 		return (0);
2218 	}
2219 
2220 	zfs_fuid_map_ids(zp, &fowner, &gowner);
2221 
2222 	mutex_enter(&zp->z_acl_lock);
2223 
2224 	error = zfs_acl_node_read(zp, &aclp, B_FALSE);
2225 	if (error != 0) {
2226 		mutex_exit(&zp->z_acl_lock);
2227 		return (error);
2228 	}
2229 
2230 	while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
2231 	    &iflags, &type)) {
2232 
2233 		if (iflags & ACE_INHERIT_ONLY_ACE)
2234 			continue;
2235 
2236 		entry_type = (iflags & ACE_TYPE_FLAGS);
2237 
2238 		checkit = B_FALSE;
2239 
2240 		switch (entry_type) {
2241 		case ACE_OWNER:
2242 			if (uid == fowner)
2243 				checkit = B_TRUE;
2244 			break;
2245 		case OWNING_GROUP:
2246 			who = gowner;
2247 			/*FALLTHROUGH*/
2248 		case ACE_IDENTIFIER_GROUP:
2249 			checkit = zfs_groupmember(zfsvfs, who, cr);
2250 			break;
2251 		case ACE_EVERYONE:
2252 			checkit = B_TRUE;
2253 			break;
2254 
2255 		/* USER Entry */
2256 		default:
2257 			if (entry_type == 0) {
2258 				uid_t newid;
2259 
2260 				zfs_fuid_map_id(zfsvfs, who,
2261 				    ZFS_ACE_USER, &newid);
2262 				if (newid != IDMAP_WK_CREATOR_OWNER_UID &&
2263 				    uid == newid)
2264 					checkit = B_TRUE;
2265 				break;
2266 			} else {
2267 				zfs_acl_free(aclp);
2268 				mutex_exit(&zp->z_acl_lock);
2269 				return (EIO);
2270 			}
2271 		}
2272 
2273 		if (checkit) {
2274 			if (access_mask & *working_mode) {
2275 				if (type == ALLOW) {
2276 					*working_mode &=
2277 					    ~(*working_mode & access_mask);
2278 					if (*working_mode == 0) {
2279 						access_deny = 0;
2280 					}
2281 				} else if (type == DENY) {
2282 					access_deny = EACCES;
2283 				}
2284 			}
2285 		}
2286 
2287 		if (access_deny != ACCESS_UNDETERMINED)
2288 			break;
2289 	}
2290 
2291 	mutex_exit(&zp->z_acl_lock);
2292 	zfs_acl_free(aclp);
2293 out:
2294 	return (access_deny);
2295 }
2296 
2297 static int
2298 zfs_zaccess_append(znode_t *zp, uint32_t *working_mode, boolean_t *check_privs,
2299     cred_t *cr)
2300 {
2301 	if (*working_mode != ACE_WRITE_DATA)
2302 		return (EACCES);
2303 
2304 	return (zfs_zaccess_common(zp, ACE_APPEND_DATA, working_mode,
2305 	    check_privs, B_FALSE, cr));
2306 }
2307 
2308 /*
2309  * Determine whether Access should be granted/denied, invoking least
2310  * priv subsytem when a deny is determined.
2311  */
2312 int
2313 zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr)
2314 {
2315 	uint32_t	working_mode;
2316 	int		error;
2317 	int		is_attr;
2318 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2319 	boolean_t 	check_privs;
2320 	znode_t		*xzp;
2321 	znode_t 	*check_zp = zp;
2322 
2323 	is_attr = ((zp->z_phys->zp_flags & ZFS_XATTR) &&
2324 	    (ZTOV(zp)->v_type == VDIR));
2325 
2326 	/*
2327 	 * If attribute then validate against base file
2328 	 */
2329 	if (is_attr) {
2330 		if ((error = zfs_zget(zp->z_zfsvfs,
2331 		    zp->z_phys->zp_parent, &xzp)) != 0)	{
2332 			return (error);
2333 		}
2334 
2335 		check_zp = xzp;
2336 
2337 		/*
2338 		 * fixup mode to map to xattr perms
2339 		 */
2340 
2341 		if (mode & (ACE_WRITE_DATA|ACE_APPEND_DATA)) {
2342 			mode &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
2343 			mode |= ACE_WRITE_NAMED_ATTRS;
2344 		}
2345 
2346 		if (mode & (ACE_READ_DATA|ACE_EXECUTE)) {
2347 			mode &= ~(ACE_READ_DATA|ACE_EXECUTE);
2348 			mode |= ACE_READ_NAMED_ATTRS;
2349 		}
2350 	}
2351 
2352 	if ((error = zfs_zaccess_common(check_zp, mode, &working_mode,
2353 	    &check_privs, skipaclchk, cr)) == 0) {
2354 		if (is_attr)
2355 			VN_RELE(ZTOV(xzp));
2356 		return (0);
2357 	}
2358 
2359 	if (error && check_privs == B_FALSE) {
2360 		if (is_attr)
2361 			VN_RELE(ZTOV(xzp));
2362 		return (error);
2363 	}
2364 
2365 	if (error && (flags & V_APPEND)) {
2366 		error = zfs_zaccess_append(zp, &working_mode, &check_privs, cr);
2367 	}
2368 
2369 	if (error && check_privs) {
2370 		uid_t		owner;
2371 		mode_t		checkmode = 0;
2372 
2373 		zfs_fuid_map_id(zfsvfs, check_zp->z_phys->zp_uid,
2374 		    ZFS_OWNER, &owner);
2375 
2376 		/*
2377 		 * First check for implicit owner permission on
2378 		 * read_acl/read_attributes
2379 		 */
2380 
2381 		error = 0;
2382 		ASSERT(working_mode != 0);
2383 
2384 		if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) &&
2385 		    owner == crgetuid(cr)))
2386 			working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES);
2387 
2388 		if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS|
2389 		    ACE_READ_ACL|ACE_READ_ATTRIBUTES))
2390 			checkmode |= VREAD;
2391 		if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS|
2392 		    ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES))
2393 			checkmode |= VWRITE;
2394 		if (working_mode & ACE_EXECUTE)
2395 			checkmode |= VEXEC;
2396 
2397 		if (checkmode)
2398 			error = secpolicy_vnode_access(cr, ZTOV(check_zp),
2399 			    owner, checkmode);
2400 
2401 		if (error == 0 && (working_mode & ACE_WRITE_OWNER))
2402 			error = secpolicy_vnode_create_gid(cr);
2403 		if (error == 0 && (working_mode & ACE_WRITE_ACL))
2404 			error = secpolicy_vnode_setdac(cr, owner);
2405 
2406 		if (error == 0 && (working_mode &
2407 		    (ACE_DELETE|ACE_DELETE_CHILD)))
2408 			error = secpolicy_vnode_remove(cr);
2409 
2410 		if (error == 0 && (working_mode & ACE_SYNCHRONIZE))
2411 			error = secpolicy_vnode_owner(cr, owner);
2412 
2413 		if (error == 0) {
2414 			/*
2415 			 * See if any bits other than those already checked
2416 			 * for are still present.  If so then return EACCES
2417 			 */
2418 			if (working_mode & ~(ZFS_CHECKED_MASKS)) {
2419 				error = EACCES;
2420 			}
2421 		}
2422 	}
2423 
2424 	if (is_attr)
2425 		VN_RELE(ZTOV(xzp));
2426 
2427 	return (error);
2428 }
2429 
2430 /*
2431  * Translate traditional unix VREAD/VWRITE/VEXEC mode into
2432  * native ACL format and call zfs_zaccess()
2433  */
2434 int
2435 zfs_zaccess_rwx(znode_t *zp, mode_t mode, int flags, cred_t *cr)
2436 {
2437 	return (zfs_zaccess(zp, zfs_unix_to_v4(mode >> 6), flags, B_FALSE, cr));
2438 }
2439 
2440 /*
2441  * Access function for secpolicy_vnode_setattr
2442  */
2443 int
2444 zfs_zaccess_unix(znode_t *zp, mode_t mode, cred_t *cr)
2445 {
2446 	int v4_mode = zfs_unix_to_v4(mode >> 6);
2447 
2448 	return (zfs_zaccess(zp, v4_mode, 0, B_FALSE, cr));
2449 }
2450 
2451 static int
2452 zfs_delete_final_check(znode_t *zp, znode_t *dzp, cred_t *cr)
2453 {
2454 	int error;
2455 	uid_t downer;
2456 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2457 
2458 	zfs_fuid_map_id(zfsvfs, dzp->z_phys->zp_uid, ZFS_OWNER, &downer);
2459 
2460 	error = secpolicy_vnode_access(cr, ZTOV(zp), downer, S_IWRITE|S_IEXEC);
2461 
2462 	if (error == 0)
2463 		error = zfs_sticky_remove_access(dzp, zp, cr);
2464 
2465 	return (error);
2466 }
2467 
2468 /*
2469  * Determine whether Access should be granted/deny, without
2470  * consulting least priv subsystem.
2471  *
2472  *
2473  * The following chart is the recommended NFSv4 enforcement for
2474  * ability to delete an object.
2475  *
2476  *      -------------------------------------------------------
2477  *      |   Parent Dir  |           Target Object Permissions |
2478  *      |  permissions  |                                     |
2479  *      -------------------------------------------------------
2480  *      |               | ACL Allows | ACL Denies| Delete     |
2481  *      |               |  Delete    |  Delete   | unspecified|
2482  *      -------------------------------------------------------
2483  *      |  ACL Allows   | Permit     | Permit    | Permit     |
2484  *      |  DELETE_CHILD |                                     |
2485  *      -------------------------------------------------------
2486  *      |  ACL Denies   | Permit     | Deny      | Deny       |
2487  *      |  DELETE_CHILD |            |           |            |
2488  *      -------------------------------------------------------
2489  *      | ACL specifies |            |           |            |
2490  *      | only allow    | Permit     | Permit    | Permit     |
2491  *      | write and     |            |           |            |
2492  *      | execute       |            |           |            |
2493  *      -------------------------------------------------------
2494  *      | ACL denies    |            |           |            |
2495  *      | write and     | Permit     | Deny      | Deny       |
2496  *      | execute       |            |           |            |
2497  *      -------------------------------------------------------
2498  *         ^
2499  *         |
2500  *         No search privilege, can't even look up file?
2501  *
2502  */
2503 int
2504 zfs_zaccess_delete(znode_t *dzp, znode_t *zp, cred_t *cr)
2505 {
2506 	uint32_t dzp_working_mode = 0;
2507 	uint32_t zp_working_mode = 0;
2508 	int dzp_error, zp_error;
2509 	boolean_t dzpcheck_privs = B_TRUE;
2510 	boolean_t zpcheck_privs = B_TRUE;
2511 
2512 	/*
2513 	 * Arghh, this check is going to require a couple of questions
2514 	 * to be asked.  We want specific DELETE permissions to
2515 	 * take precedence over WRITE/EXECUTE.  We don't
2516 	 * want an ACL such as this to mess us up.
2517 	 * user:joe:write_data:deny,user:joe:delete:allow
2518 	 *
2519 	 * However, deny permissions may ultimately be overridden
2520 	 * by secpolicy_vnode_access().
2521 	 */
2522 
2523 	if (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_NOUNLINK))
2524 		return (EPERM);
2525 
2526 	dzp_error = zfs_zaccess_common(dzp, ACE_DELETE_CHILD,
2527 	    &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr);
2528 	zp_error = zfs_zaccess_common(zp, ACE_DELETE, &zp_working_mode,
2529 	    &zpcheck_privs, B_FALSE, cr);
2530 
2531 	if ((dzp_error && dzpcheck_privs == B_FALSE) ||
2532 	    (zp_error && zpcheck_privs == B_FALSE))
2533 		return (dzp_error);
2534 
2535 	/*
2536 	 * First check the first row.
2537 	 * We only need to see if parent Allows delete_child
2538 	 */
2539 	if ((dzp_working_mode & ACE_DELETE_CHILD) == 0)
2540 		return (0);
2541 
2542 	/*
2543 	 * Second row
2544 	 * we already have the necessary information in
2545 	 * zp_working_mode, zp_error and dzp_error.
2546 	 */
2547 
2548 	if ((zp_working_mode & ACE_DELETE) == 0)
2549 		return (0);
2550 
2551 	/*
2552 	 * Now zp_error should either be EACCES which indicates
2553 	 * a "deny" delete entry or ACCESS_UNDETERMINED if the "delete"
2554 	 * entry exists on the target.
2555 	 *
2556 	 * dzp_error should be either EACCES which indicates a "deny"
2557 	 * entry for delete_child or ACCESS_UNDETERMINED if no delete_child
2558 	 * entry exists.  If value is EACCES then we are done
2559 	 * and zfs_delete_final_check() will make the final decision
2560 	 * regarding to allow the delete.
2561 	 */
2562 
2563 	ASSERT(zp_error != 0 && dzp_error != 0);
2564 	if (dzp_error == EACCES)
2565 		return (zfs_delete_final_check(zp, dzp, cr));
2566 
2567 	/*
2568 	 * Third Row
2569 	 * Only need to check for write/execute on parent
2570 	 */
2571 
2572 	dzp_error = zfs_zaccess_common(dzp, ACE_WRITE_DATA|ACE_EXECUTE,
2573 	    &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr);
2574 
2575 	if (dzp_error && dzpcheck_privs == B_FALSE)
2576 		return (dzp_error);
2577 
2578 	if ((dzp_working_mode & (ACE_WRITE_DATA|ACE_EXECUTE)) == 0)
2579 		return (zfs_sticky_remove_access(dzp, zp, cr));
2580 
2581 	/*
2582 	 * Fourth Row
2583 	 */
2584 
2585 	if (((dzp_working_mode & (ACE_WRITE_DATA|ACE_EXECUTE)) != 0) &&
2586 	    ((zp_working_mode & ACE_DELETE) == 0))
2587 		return (zfs_sticky_remove_access(dzp, zp, cr));
2588 
2589 	return (zfs_delete_final_check(zp, dzp, cr));
2590 }
2591 
2592 int
2593 zfs_zaccess_rename(znode_t *sdzp, znode_t *szp, znode_t *tdzp,
2594     znode_t *tzp, cred_t *cr)
2595 {
2596 	int add_perm;
2597 	int error;
2598 
2599 	if (szp->z_phys->zp_flags & ZFS_AV_QUARANTINED)
2600 		return (EACCES);
2601 
2602 	add_perm = (ZTOV(szp)->v_type == VDIR) ?
2603 	    ACE_ADD_SUBDIRECTORY : ACE_ADD_FILE;
2604 
2605 	/*
2606 	 * Rename permissions are combination of delete permission +
2607 	 * add file/subdir permission.
2608 	 */
2609 
2610 	/*
2611 	 * first make sure we do the delete portion.
2612 	 *
2613 	 * If that succeeds then check for add_file/add_subdir permissions
2614 	 */
2615 
2616 	if (error = zfs_zaccess_delete(sdzp, szp, cr))
2617 		return (error);
2618 
2619 	/*
2620 	 * If we have a tzp, see if we can delete it?
2621 	 */
2622 	if (tzp) {
2623 		if (error = zfs_zaccess_delete(tdzp, tzp, cr))
2624 			return (error);
2625 	}
2626 
2627 	/*
2628 	 * Now check for add permissions
2629 	 */
2630 	error = zfs_zaccess(tdzp, add_perm, 0, B_FALSE, cr);
2631 
2632 	return (error);
2633 }
2634