xref: /titanic_50/usr/src/uts/common/fs/zfs/zfs_acl.c (revision 36fe4a92b52649b0979d6a13212f4cea730d19c7)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/vfs.h>
36 #include <sys/vnode.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/fs/zfs.h>
44 #include <sys/mode.h>
45 #include <sys/policy.h>
46 #include <sys/zfs_znode.h>
47 #include <sys/zfs_acl.h>
48 #include <sys/zfs_dir.h>
49 #include <sys/zfs_vfsops.h>
50 #include <sys/dmu.h>
51 #include <sys/zap.h>
52 #include <util/qsort.h>
53 #include "fs/fs_subr.h"
54 #include <acl/acl_common.h>
55 
56 #define	ALLOW	ACE_ACCESS_ALLOWED_ACE_TYPE
57 #define	DENY	ACE_ACCESS_DENIED_ACE_TYPE
58 
59 #define	OWNING_GROUP		(ACE_GROUP|ACE_IDENTIFIER_GROUP)
60 #define	EVERYONE_ALLOW_MASK (ACE_READ_ACL|ACE_READ_ATTRIBUTES | \
61     ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE)
62 #define	EVERYONE_DENY_MASK (ACE_WRITE_ACL|ACE_WRITE_OWNER | \
63     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
64 #define	OWNER_ALLOW_MASK (ACE_WRITE_ACL | ACE_WRITE_OWNER | \
65     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
66 #define	WRITE_MASK (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS| \
67     ACE_WRITE_ATTRIBUTES|ACE_WRITE_ACL|ACE_WRITE_OWNER)
68 
69 #define	OGE_CLEAR	(ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
70     ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
71 
72 #define	OKAY_MASK_BITS (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
73     ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
74 
75 #define	ALL_INHERIT	(ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE | \
76     ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE)
77 
78 #define	SECURE_NO_INHERIT	(ACE_WRITE_ACL|ACE_WRITE_OWNER)
79 
80 #define	OGE_PAD	6		/* traditional owner/group/everyone ACES */
81 
82 static int zfs_ace_can_use(znode_t *zp, ace_t *);
83 
84 static zfs_acl_t *
85 zfs_acl_alloc(int slots)
86 {
87 	zfs_acl_t *aclp;
88 
89 	aclp = kmem_zalloc(sizeof (zfs_acl_t), KM_SLEEP);
90 	if (slots != 0) {
91 		aclp->z_acl = kmem_alloc(ZFS_ACL_SIZE(slots), KM_SLEEP);
92 		aclp->z_acl_count = 0;
93 		aclp->z_state = ACL_DATA_ALLOCED;
94 	} else {
95 		aclp->z_state = 0;
96 	}
97 	aclp->z_slots = slots;
98 	return (aclp);
99 }
100 
101 void
102 zfs_acl_free(zfs_acl_t *aclp)
103 {
104 	if (aclp->z_state == ACL_DATA_ALLOCED) {
105 		kmem_free(aclp->z_acl, ZFS_ACL_SIZE(aclp->z_slots));
106 	}
107 	kmem_free(aclp, sizeof (zfs_acl_t));
108 }
109 
110 static uint32_t
111 zfs_v4_to_unix(uint32_t access_mask)
112 {
113 	uint32_t new_mask = 0;
114 
115 	if (access_mask & (ACE_READ_DATA | ACE_LIST_DIRECTORY))
116 		new_mask |= S_IROTH;
117 	if (access_mask & (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_ADD_FILE))
118 		new_mask |= S_IWOTH;
119 	if (access_mask & (ACE_EXECUTE|ACE_READ_NAMED_ATTRS))
120 		new_mask |= S_IXOTH;
121 
122 	return (new_mask);
123 }
124 
125 /*
126  * Convert unix access mask to v4 access mask
127  */
128 static uint32_t
129 zfs_unix_to_v4(uint32_t access_mask)
130 {
131 	uint32_t new_mask = 0;
132 
133 	if (access_mask & 01)
134 		new_mask |= (ACE_EXECUTE);
135 	if (access_mask & 02) {
136 		new_mask |= (ACE_WRITE_DATA);
137 	} if (access_mask & 04) {
138 		new_mask |= ACE_READ_DATA;
139 	}
140 	return (new_mask);
141 }
142 
143 static void
144 zfs_set_ace(ace_t *zacep, uint32_t access_mask, int access_type,
145     uid_t uid, int entry_type)
146 {
147 	zacep->a_access_mask = access_mask;
148 	zacep->a_type = access_type;
149 	zacep->a_who = uid;
150 	zacep->a_flags = entry_type;
151 }
152 
153 static uint64_t
154 zfs_mode_compute(znode_t *zp, zfs_acl_t *aclp)
155 {
156 	int 	i;
157 	int	entry_type;
158 	mode_t	mode = (zp->z_phys->zp_mode &
159 	    (S_IFMT | S_ISUID | S_ISGID | S_ISVTX));
160 	mode_t	 seen = 0;
161 	ace_t 	*acep;
162 
163 	for (i = 0, acep = aclp->z_acl;
164 	    i != aclp->z_acl_count; i++, acep++) {
165 		entry_type = (acep->a_flags & 0xf040);
166 		if (entry_type == ACE_OWNER) {
167 			if ((acep->a_access_mask & ACE_READ_DATA) &&
168 			    (!(seen & S_IRUSR))) {
169 				seen |= S_IRUSR;
170 				if (acep->a_type == ALLOW) {
171 					mode |= S_IRUSR;
172 				}
173 			}
174 			if ((acep->a_access_mask & ACE_WRITE_DATA) &&
175 			    (!(seen & S_IWUSR))) {
176 				seen |= S_IWUSR;
177 				if (acep->a_type == ALLOW) {
178 					mode |= S_IWUSR;
179 				}
180 			}
181 			if ((acep->a_access_mask & ACE_EXECUTE) &&
182 			    (!(seen & S_IXUSR))) {
183 				seen |= S_IXUSR;
184 				if (acep->a_type == ALLOW) {
185 					mode |= S_IXUSR;
186 				}
187 			}
188 		} else if (entry_type == OWNING_GROUP) {
189 			if ((acep->a_access_mask & ACE_READ_DATA) &&
190 			    (!(seen & S_IRGRP))) {
191 				seen |= S_IRGRP;
192 				if (acep->a_type == ALLOW) {
193 					mode |= S_IRGRP;
194 				}
195 			}
196 			if ((acep->a_access_mask & ACE_WRITE_DATA) &&
197 			    (!(seen & S_IWGRP))) {
198 				seen |= S_IWGRP;
199 				if (acep->a_type == ALLOW) {
200 					mode |= S_IWGRP;
201 				}
202 			}
203 			if ((acep->a_access_mask & ACE_EXECUTE) &&
204 			    (!(seen & S_IXGRP))) {
205 				seen |= S_IXGRP;
206 				if (acep->a_type == ALLOW) {
207 					mode |= S_IXGRP;
208 				}
209 			}
210 		} else if (entry_type == ACE_EVERYONE) {
211 			if ((acep->a_access_mask & ACE_READ_DATA)) {
212 				if (!(seen & S_IRUSR)) {
213 					seen |= S_IRUSR;
214 					if (acep->a_type == ALLOW) {
215 						mode |= S_IRUSR;
216 					}
217 				}
218 				if (!(seen & S_IRGRP)) {
219 					seen |= S_IRGRP;
220 					if (acep->a_type == ALLOW) {
221 						mode |= S_IRGRP;
222 					}
223 				}
224 				if (!(seen & S_IROTH)) {
225 					seen |= S_IROTH;
226 					if (acep->a_type == ALLOW) {
227 						mode |= S_IROTH;
228 					}
229 				}
230 			}
231 			if ((acep->a_access_mask & ACE_WRITE_DATA)) {
232 				if (!(seen & S_IWUSR)) {
233 					seen |= S_IWUSR;
234 					if (acep->a_type == ALLOW) {
235 						mode |= S_IWUSR;
236 					}
237 				}
238 				if (!(seen & S_IWGRP)) {
239 					seen |= S_IWGRP;
240 					if (acep->a_type == ALLOW) {
241 						mode |= S_IWGRP;
242 					}
243 				}
244 				if (!(seen & S_IWOTH)) {
245 					seen |= S_IWOTH;
246 					if (acep->a_type == ALLOW) {
247 						mode |= S_IWOTH;
248 					}
249 				}
250 			}
251 			if ((acep->a_access_mask & ACE_EXECUTE)) {
252 				if (!(seen & S_IXUSR)) {
253 					seen |= S_IXUSR;
254 					if (acep->a_type == ALLOW) {
255 						mode |= S_IXUSR;
256 					}
257 				}
258 				if (!(seen & S_IXGRP)) {
259 					seen |= S_IXGRP;
260 					if (acep->a_type == ALLOW) {
261 						mode |= S_IXGRP;
262 					}
263 				}
264 				if (!(seen & S_IXOTH)) {
265 					seen |= S_IXOTH;
266 					if (acep->a_type == ALLOW) {
267 						mode |= S_IXOTH;
268 					}
269 				}
270 			}
271 		}
272 	}
273 	return (mode);
274 }
275 
276 static zfs_acl_t *
277 zfs_acl_node_read_internal(znode_t *zp)
278 {
279 	zfs_acl_t	*aclp;
280 
281 	aclp = zfs_acl_alloc(0);
282 	aclp->z_acl_count = zp->z_phys->zp_acl.z_acl_count;
283 	aclp->z_acl = &zp->z_phys->zp_acl.z_ace_data[0];
284 
285 	return (aclp);
286 }
287 
288 /*
289  * Read an external acl object.
290  */
291 zfs_acl_t *
292 zfs_acl_node_read(znode_t *zp)
293 {
294 	uint64_t extacl = zp->z_phys->zp_acl.z_acl_extern_obj;
295 	zfs_acl_t	*aclp;
296 
297 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
298 
299 	if (zp->z_phys->zp_acl.z_acl_extern_obj == 0)
300 		return (zfs_acl_node_read_internal(zp));
301 
302 	aclp = zfs_acl_alloc(zp->z_phys->zp_acl.z_acl_count);
303 
304 	dmu_read(zp->z_zfsvfs->z_os, extacl, 0,
305 	    ZFS_ACL_SIZE(zp->z_phys->zp_acl.z_acl_count), aclp->z_acl);
306 
307 	aclp->z_acl_count = zp->z_phys->zp_acl.z_acl_count;
308 
309 	return (aclp);
310 }
311 
312 static boolean_t
313 zfs_acl_valid(znode_t *zp, ace_t *uace, int aclcnt, int *inherit)
314 {
315 	ace_t 	*acep;
316 	int i;
317 
318 	*inherit = 0;
319 
320 	if (aclcnt > MAX_ACL_ENTRIES || aclcnt <= 0) {
321 		return (B_FALSE);
322 	}
323 
324 	for (i = 0, acep = uace; i != aclcnt; i++, acep++) {
325 
326 		/*
327 		 * first check type of entry
328 		 */
329 
330 		switch (acep->a_flags & 0xf040) {
331 		case ACE_OWNER:
332 			acep->a_who = -1;
333 			break;
334 		case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
335 		case ACE_IDENTIFIER_GROUP:
336 			if (acep->a_flags & ACE_GROUP) {
337 				acep->a_who = -1;
338 			}
339 			break;
340 		case ACE_EVERYONE:
341 			acep->a_who = -1;
342 			break;
343 		}
344 
345 		/*
346 		 * next check inheritance level flags
347 		 */
348 
349 		if (acep->a_type != ALLOW && acep->a_type != DENY)
350 			return (B_FALSE);
351 
352 		/*
353 		 * Only directories should have inheritance flags.
354 		 */
355 		if (ZTOV(zp)->v_type != VDIR && (acep->a_flags &
356 		    (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE|
357 		    ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE))) {
358 			return (B_FALSE);
359 		}
360 
361 		if (acep->a_flags &
362 		    (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE))
363 			*inherit = 1;
364 
365 		if (acep->a_flags &
366 		    (ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE)) {
367 			if ((acep->a_flags & (ACE_FILE_INHERIT_ACE|
368 			    ACE_DIRECTORY_INHERIT_ACE)) == 0) {
369 				return (B_FALSE);
370 			}
371 		}
372 	}
373 
374 	return (B_TRUE);
375 }
376 /*
377  * common code for setting acl's.
378  *
379  * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl.
380  * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's
381  * already checked the acl and knows whether to inherit.
382  */
383 int
384 zfs_aclset_common(znode_t *zp, zfs_acl_t *aclp, dmu_tx_t *tx, int *ihp)
385 {
386 	int 		inherit = 0;
387 	int		error;
388 	znode_phys_t	*zphys = zp->z_phys;
389 	zfs_znode_acl_t	*zacl = &zphys->zp_acl;
390 	uint32_t	acl_phys_size = ZFS_ACL_SIZE(aclp->z_acl_count);
391 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
392 	uint64_t	aoid = zphys->zp_acl.z_acl_extern_obj;
393 
394 	ASSERT(MUTEX_HELD(&zp->z_lock));
395 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
396 
397 	if (ihp)
398 		inherit = *ihp;		/* already determined by caller */
399 	else if (!zfs_acl_valid(zp, aclp->z_acl,
400 	    aclp->z_acl_count, &inherit)) {
401 		return (EINVAL);
402 	}
403 
404 	dmu_buf_will_dirty(zp->z_dbuf, tx);
405 
406 	/*
407 	 * Will ACL fit internally?
408 	 */
409 	if (aclp->z_acl_count > ACE_SLOT_CNT) {
410 		if (aoid == 0) {
411 			aoid = dmu_object_alloc(zfsvfs->z_os,
412 			    DMU_OT_ACL, acl_phys_size, DMU_OT_NONE, 0, tx);
413 		} else {
414 			(void) dmu_object_set_blocksize(zfsvfs->z_os, aoid,
415 			    acl_phys_size, 0, tx);
416 		}
417 		zphys->zp_acl.z_acl_extern_obj = aoid;
418 		zphys->zp_acl.z_acl_count = aclp->z_acl_count;
419 		dmu_write(zfsvfs->z_os, aoid, 0,
420 		    acl_phys_size, aclp->z_acl, tx);
421 	} else {
422 		/*
423 		 * Migrating back embedded?
424 		 */
425 		if (zphys->zp_acl.z_acl_extern_obj) {
426 			error = dmu_object_free(zfsvfs->z_os,
427 				zp->z_phys->zp_acl.z_acl_extern_obj, tx);
428 			if (error)
429 				return (error);
430 			zphys->zp_acl.z_acl_extern_obj = 0;
431 		}
432 		bcopy(aclp->z_acl, zacl->z_ace_data,
433 		    aclp->z_acl_count * sizeof (ace_t));
434 		zacl->z_acl_count = aclp->z_acl_count;
435 	}
436 
437 	zp->z_phys->zp_flags &= ~(ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE);
438 	if (inherit) {
439 		zp->z_phys->zp_flags |= ZFS_INHERIT_ACE;
440 	} else if (ace_trivial(zacl->z_ace_data, zacl->z_acl_count) == 0) {
441 		zp->z_phys->zp_flags |= ZFS_ACL_TRIVIAL;
442 	}
443 
444 	zphys->zp_mode = zfs_mode_compute(zp, aclp);
445 	zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
446 
447 	return (0);
448 }
449 
450 /*
451  * Create space for slots_needed ACEs to be append
452  * to aclp.
453  */
454 static void
455 zfs_acl_append(zfs_acl_t *aclp, int slots_needed)
456 {
457 	ace_t	*newacep;
458 	ace_t	*oldaclp;
459 	int	slot_cnt;
460 	int 	slots_left = aclp->z_slots - aclp->z_acl_count;
461 
462 	if (aclp->z_state == ACL_DATA_ALLOCED)
463 		ASSERT(aclp->z_slots >= aclp->z_acl_count);
464 	if (slots_left < slots_needed || aclp->z_state != ACL_DATA_ALLOCED) {
465 		slot_cnt = aclp->z_slots +  1 + (slots_needed - slots_left);
466 		newacep = kmem_alloc(ZFS_ACL_SIZE(slot_cnt), KM_SLEEP);
467 		bcopy(aclp->z_acl, newacep,
468 		    ZFS_ACL_SIZE(aclp->z_acl_count));
469 		oldaclp = aclp->z_acl;
470 		if (aclp->z_state == ACL_DATA_ALLOCED)
471 			kmem_free(oldaclp, ZFS_ACL_SIZE(aclp->z_slots));
472 		aclp->z_acl = newacep;
473 		aclp->z_slots = slot_cnt;
474 		aclp->z_state = ACL_DATA_ALLOCED;
475 	}
476 }
477 
478 /*
479  * Remove "slot" ACE from aclp
480  */
481 static void
482 zfs_ace_remove(zfs_acl_t *aclp, int slot)
483 {
484 	if (aclp->z_acl_count > 1) {
485 		(void) memmove(&aclp->z_acl[slot],
486 		    &aclp->z_acl[slot +1], sizeof (ace_t) *
487 		    (--aclp->z_acl_count - slot));
488 	} else
489 		aclp->z_acl_count--;
490 }
491 
492 /*
493  * Update access mask for prepended ACE
494  *
495  * This applies the "groupmask" value for aclmode property.
496  */
497 static void
498 zfs_acl_prepend_fixup(ace_t *acep, ace_t *origacep, mode_t mode, uid_t owner)
499 {
500 
501 	int	rmask, wmask, xmask;
502 	int	user_ace;
503 
504 	user_ace = (!(acep->a_flags &
505 	    (ACE_OWNER|ACE_GROUP|ACE_IDENTIFIER_GROUP)));
506 
507 	if (user_ace && (acep->a_who == owner)) {
508 		rmask = S_IRUSR;
509 		wmask = S_IWUSR;
510 		xmask = S_IXUSR;
511 	} else {
512 		rmask = S_IRGRP;
513 		wmask = S_IWGRP;
514 		xmask = S_IXGRP;
515 	}
516 
517 	if (origacep->a_access_mask & ACE_READ_DATA) {
518 		if (mode & rmask)
519 			acep->a_access_mask &= ~ACE_READ_DATA;
520 		else
521 			acep->a_access_mask |= ACE_READ_DATA;
522 	}
523 
524 	if (origacep->a_access_mask & ACE_WRITE_DATA) {
525 		if (mode & wmask)
526 			acep->a_access_mask &= ~ACE_WRITE_DATA;
527 		else
528 			acep->a_access_mask |= ACE_WRITE_DATA;
529 	}
530 
531 	if (origacep->a_access_mask & ACE_APPEND_DATA) {
532 		if (mode & wmask)
533 			acep->a_access_mask &= ~ACE_APPEND_DATA;
534 		else
535 			acep->a_access_mask |= ACE_APPEND_DATA;
536 	}
537 
538 	if (origacep->a_access_mask & ACE_EXECUTE) {
539 		if (mode & xmask)
540 			acep->a_access_mask &= ~ACE_EXECUTE;
541 		else
542 			acep->a_access_mask |= ACE_EXECUTE;
543 	}
544 }
545 
546 /*
547  * Apply mode to canonical six ACEs.
548  */
549 static void
550 zfs_acl_fixup_canonical_six(zfs_acl_t *aclp, mode_t mode)
551 {
552 	int	cnt;
553 	ace_t	*acep;
554 
555 	cnt = aclp->z_acl_count -1;
556 	acep = aclp->z_acl;
557 
558 	/*
559 	 * Fixup final ACEs to match the mode
560 	 */
561 
562 	ASSERT(cnt >= 5);
563 	adjust_ace_pair(&acep[cnt - 1], mode);	/* everyone@ */
564 	adjust_ace_pair(&acep[cnt - 3], (mode & 0070) >> 3);	/* group@ */
565 	adjust_ace_pair(&acep[cnt - 5], (mode & 0700) >> 6);	/* owner@ */
566 }
567 
568 
569 static int
570 zfs_acl_ace_match(ace_t *acep, int allow_deny, int type, int mask)
571 {
572 	return (acep->a_access_mask == mask && acep->a_type == allow_deny &&
573 	    ((acep->a_flags & 0xf040) == type));
574 }
575 
576 /*
577  * Can prepended ACE be reused?
578  */
579 static int
580 zfs_reuse_deny(ace_t *acep, int i)
581 {
582 	int okay_masks;
583 
584 	if (i < 1)
585 		return (B_FALSE);
586 
587 	if (acep[i-1].a_type != DENY)
588 		return (B_FALSE);
589 
590 	if (acep[i-1].a_flags != (acep[i].a_flags & ACE_IDENTIFIER_GROUP))
591 		return (B_FALSE);
592 
593 	okay_masks = (acep[i].a_access_mask & OKAY_MASK_BITS);
594 
595 	if (acep[i-1].a_access_mask & ~okay_masks)
596 		return (B_FALSE);
597 
598 	return (B_TRUE);
599 }
600 
601 /*
602  * Create space to prepend an ACE
603  */
604 static void
605 zfs_acl_prepend(zfs_acl_t *aclp, int i)
606 {
607 	ace_t	*oldaclp = NULL;
608 	ace_t	*to, *from;
609 	int	slots_left = aclp->z_slots - aclp->z_acl_count;
610 	int	oldslots;
611 	int	need_free = 0;
612 
613 	if (aclp->z_state == ACL_DATA_ALLOCED)
614 		ASSERT(aclp->z_slots >= aclp->z_acl_count);
615 
616 	if (slots_left == 0 || aclp->z_state != ACL_DATA_ALLOCED) {
617 
618 		to = kmem_alloc(ZFS_ACL_SIZE(aclp->z_acl_count +
619 		    OGE_PAD), KM_SLEEP);
620 		if (aclp->z_state == ACL_DATA_ALLOCED)
621 			need_free++;
622 		from = aclp->z_acl;
623 		oldaclp = aclp->z_acl;
624 		(void) memmove(to, from,
625 		    sizeof (ace_t) * aclp->z_acl_count);
626 		aclp->z_state = ACL_DATA_ALLOCED;
627 	} else {
628 		from = aclp->z_acl;
629 		to = aclp->z_acl;
630 	}
631 
632 
633 	(void) memmove(&to[i + 1], &from[i],
634 	    sizeof (ace_t) * (aclp->z_acl_count - i));
635 
636 	if (oldaclp) {
637 		aclp->z_acl = to;
638 		oldslots = aclp->z_slots;
639 		aclp->z_slots = aclp->z_acl_count + OGE_PAD;
640 		if (need_free)
641 			kmem_free(oldaclp, ZFS_ACL_SIZE(oldslots));
642 	}
643 
644 }
645 
646 /*
647  * Prepend deny ACE
648  */
649 static void
650 zfs_acl_prepend_deny(znode_t *zp, zfs_acl_t *aclp, int i,
651     mode_t mode)
652 {
653 	ace_t	*acep;
654 
655 	zfs_acl_prepend(aclp, i);
656 
657 	acep = aclp->z_acl;
658 	zfs_set_ace(&acep[i], 0, DENY, acep[i + 1].a_who,
659 	    (acep[i + 1].a_flags & 0xf040));
660 	zfs_acl_prepend_fixup(&acep[i], &acep[i+1], mode, zp->z_phys->zp_uid);
661 	aclp->z_acl_count++;
662 }
663 
664 /*
665  * Split an inherited ACE into inherit_only ACE
666  * and original ACE with inheritance flags stripped off.
667  */
668 static void
669 zfs_acl_split_ace(zfs_acl_t *aclp, int i)
670 {
671 	ace_t *acep = aclp->z_acl;
672 
673 	zfs_acl_prepend(aclp, i);
674 	acep = aclp->z_acl;
675 	acep[i] = acep[i + 1];
676 	acep[i].a_flags |= ACE_INHERIT_ONLY_ACE;
677 	acep[i + 1].a_flags &= ~ALL_INHERIT;
678 	aclp->z_acl_count++;
679 }
680 
681 /*
682  * Are ACES started at index i, the canonical six ACES?
683  */
684 static int
685 zfs_have_canonical_six(zfs_acl_t *aclp, int i)
686 {
687 	ace_t *acep = aclp->z_acl;
688 
689 	if ((zfs_acl_ace_match(&acep[i],
690 	    DENY, ACE_OWNER, 0) &&
691 	    zfs_acl_ace_match(&acep[i + 1], ALLOW, ACE_OWNER,
692 	    OWNER_ALLOW_MASK) && zfs_acl_ace_match(&acep[i + 2],
693 	    DENY, OWNING_GROUP, 0) && zfs_acl_ace_match(&acep[i + 3],
694 	    ALLOW, OWNING_GROUP, 0) && zfs_acl_ace_match(&acep[i + 4],
695 	    DENY, ACE_EVERYONE, EVERYONE_DENY_MASK) &&
696 	    zfs_acl_ace_match(&acep[i + 5], ALLOW, ACE_EVERYONE,
697 	    EVERYONE_ALLOW_MASK))) {
698 		return (1);
699 	} else {
700 		return (0);
701 	}
702 }
703 
704 /*
705  * Apply step 1g, to group entries
706  *
707  * Need to deal with corner case where group may have
708  * greater permissions than owner.  If so then limit
709  * group permissions, based on what extra permissions
710  * group has.
711  */
712 static void
713 zfs_fixup_group_entries(ace_t *acep, mode_t mode)
714 {
715 	mode_t extramode = (mode >> 3) & 07;
716 	mode_t ownermode = (mode >> 6);
717 
718 	if (acep[0].a_flags & ACE_IDENTIFIER_GROUP) {
719 
720 		extramode &= ~ownermode;
721 
722 		if (extramode) {
723 			if (extramode & 04) {
724 				acep[0].a_access_mask &= ~ACE_READ_DATA;
725 				acep[1].a_access_mask &= ~ACE_READ_DATA;
726 			}
727 			if (extramode & 02) {
728 				acep[0].a_access_mask &=
729 				    ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
730 				acep[1].a_access_mask &=
731 				    ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
732 			}
733 			if (extramode & 01) {
734 				acep[0].a_access_mask &= ~ACE_EXECUTE;
735 				acep[1].a_access_mask &= ~ACE_EXECUTE;
736 			}
737 		}
738 	}
739 }
740 
741 /*
742  * Apply the chmod algorithm as described
743  * in PSARC/2002/240
744  */
745 static int
746 zfs_acl_chmod(znode_t *zp, uint64_t mode, zfs_acl_t *aclp,
747     dmu_tx_t *tx)
748 {
749 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
750 	ace_t 		*acep;
751 	int 		i;
752 	int		error;
753 	int 		entry_type;
754 	int 		reuse_deny;
755 	int 		need_canonical_six = 1;
756 	int		inherit = 0;
757 	int		iflags;
758 
759 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
760 	ASSERT(MUTEX_HELD(&zp->z_lock));
761 
762 	i = 0;
763 	while (i < aclp->z_acl_count) {
764 		acep = aclp->z_acl;
765 		entry_type = (acep[i].a_flags & 0xf040);
766 		iflags = (acep[i].a_flags & ALL_INHERIT);
767 
768 		if ((acep[i].a_type != ALLOW && acep[i].a_type != DENY) ||
769 		    (iflags & ACE_INHERIT_ONLY_ACE)) {
770 			i++;
771 			if (iflags)
772 				inherit = 1;
773 			continue;
774 		}
775 
776 
777 		if (zfsvfs->z_acl_mode == DISCARD) {
778 			zfs_ace_remove(aclp, i);
779 			continue;
780 		}
781 
782 		/*
783 		 * Need to split ace into two?
784 		 */
785 		if ((iflags & (ACE_FILE_INHERIT_ACE|
786 		    ACE_DIRECTORY_INHERIT_ACE)) &&
787 		    (!(iflags & ACE_INHERIT_ONLY_ACE))) {
788 			zfs_acl_split_ace(aclp, i);
789 			i++;
790 			inherit = 1;
791 			continue;
792 		}
793 
794 		if (entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
795 		    (entry_type == OWNING_GROUP)) {
796 			acep[i].a_access_mask &= ~OGE_CLEAR;
797 			i++;
798 			continue;
799 
800 		} else {
801 			if (acep[i].a_type == ALLOW) {
802 
803 				/*
804 				 * Check preceding ACE if any, to see
805 				 * if we need to prepend a DENY ACE.
806 				 * This is only applicable when the acl_mode
807 				 * property == groupmask.
808 				 */
809 				if (zfsvfs->z_acl_mode == GROUPMASK) {
810 
811 					reuse_deny = zfs_reuse_deny(acep, i);
812 
813 					if (reuse_deny == B_FALSE) {
814 						zfs_acl_prepend_deny(zp, aclp,
815 						    i, mode);
816 						i++;
817 						acep = aclp->z_acl;
818 					} else {
819 						zfs_acl_prepend_fixup(
820 						    &acep[i - 1],
821 						    &acep[i], mode,
822 						    zp->z_phys->zp_uid);
823 					}
824 					zfs_fixup_group_entries(&acep[i - 1],
825 					    mode);
826 				}
827 			}
828 			i++;
829 		}
830 	}
831 
832 	/*
833 	 * Check out last six aces, if we have six.
834 	 */
835 
836 	if (aclp->z_acl_count >= 6) {
837 		i = aclp->z_acl_count - 6;
838 
839 		if (zfs_have_canonical_six(aclp, i)) {
840 			need_canonical_six = 0;
841 		}
842 	}
843 
844 	if (need_canonical_six) {
845 
846 		zfs_acl_append(aclp, 6);
847 		i = aclp->z_acl_count;
848 		acep = aclp->z_acl;
849 		zfs_set_ace(&acep[i++], 0, DENY, -1, ACE_OWNER);
850 		zfs_set_ace(&acep[i++], OWNER_ALLOW_MASK, ALLOW, -1, ACE_OWNER);
851 		zfs_set_ace(&acep[i++], 0, DENY, -1, OWNING_GROUP);
852 		zfs_set_ace(&acep[i++], 0, ALLOW, -1, OWNING_GROUP);
853 		zfs_set_ace(&acep[i++], EVERYONE_DENY_MASK,
854 		    DENY, -1, ACE_EVERYONE);
855 		zfs_set_ace(&acep[i++], EVERYONE_ALLOW_MASK,
856 		    ALLOW, -1, ACE_EVERYONE);
857 		aclp->z_acl_count += 6;
858 	}
859 
860 	zfs_acl_fixup_canonical_six(aclp, mode);
861 
862 	zp->z_phys->zp_mode = mode;
863 	error = zfs_aclset_common(zp, aclp, tx, &inherit);
864 	return (error);
865 }
866 
867 
868 int
869 zfs_acl_chmod_setattr(znode_t *zp, uint64_t mode, dmu_tx_t *tx)
870 {
871 	zfs_acl_t *aclp;
872 	int error;
873 
874 	ASSERT(MUTEX_HELD(&zp->z_lock));
875 	mutex_enter(&zp->z_acl_lock);
876 	aclp = zfs_acl_node_read(zp);
877 	error = zfs_acl_chmod(zp, mode, aclp, tx);
878 	mutex_exit(&zp->z_acl_lock);
879 	zfs_acl_free(aclp);
880 	return (error);
881 }
882 
883 /*
884  * strip off write_owner and write_acl
885  */
886 static void
887 zfs_securemode_update(zfsvfs_t *zfsvfs, ace_t *acep)
888 {
889 	if ((zfsvfs->z_acl_inherit == SECURE) &&
890 	    acep->a_type == ALLOW)
891 		acep->a_access_mask &= ~SECURE_NO_INHERIT;
892 }
893 
894 /*
895  * inherit inheritable ACEs from parent
896  */
897 static zfs_acl_t *
898 zfs_acl_inherit(znode_t *zp, zfs_acl_t *paclp)
899 {
900 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
901 	ace_t 		*pacep;
902 	ace_t		*acep;
903 	int 		ace_cnt = 0;
904 	int		pace_cnt;
905 	int 		i, j;
906 	zfs_acl_t	*aclp = NULL;
907 
908 	i = j = 0;
909 	pace_cnt = paclp->z_acl_count;
910 	pacep = paclp->z_acl;
911 	if (zfsvfs->z_acl_inherit != DISCARD) {
912 		for (i = 0; i != pace_cnt; i++) {
913 
914 			if (zfsvfs->z_acl_inherit == NOALLOW &&
915 			    pacep[i].a_type == ALLOW)
916 				continue;
917 
918 			if (zfs_ace_can_use(zp, &pacep[i])) {
919 				ace_cnt++;
920 				if (!(pacep[i].a_flags &
921 				    ACE_NO_PROPAGATE_INHERIT_ACE))
922 					ace_cnt++;
923 			}
924 		}
925 	}
926 
927 	aclp = zfs_acl_alloc(ace_cnt + OGE_PAD);
928 	if (ace_cnt && zfsvfs->z_acl_inherit != DISCARD) {
929 		acep = aclp->z_acl;
930 		pacep = paclp->z_acl;
931 		for (i = 0; i != pace_cnt; i++) {
932 
933 			if (zfsvfs->z_acl_inherit == NOALLOW &&
934 			    pacep[i].a_type == ALLOW)
935 				continue;
936 
937 			if (zfs_ace_can_use(zp, &pacep[i])) {
938 				/*
939 				 * Now create entry for inherited ace
940 				 */
941 				acep[j] = pacep[i];
942 
943 				if (pacep[i].a_flags &
944 				    ACE_NO_PROPAGATE_INHERIT_ACE) {
945 					acep[j].a_flags &= ~ALL_INHERIT;
946 					j++;
947 					continue;
948 				}
949 
950 				if (pacep[i].a_type != ALLOW &&
951 				    pacep[i].a_type != DENY) {
952 					zfs_securemode_update(zfsvfs, &acep[j]);
953 					j++;
954 					continue;
955 				}
956 
957 				if (ZTOV(zp)->v_type != VDIR) {
958 					acep[j].a_flags &= ~ALL_INHERIT;
959 					zfs_securemode_update(zfsvfs, &acep[j]);
960 					j++;
961 					continue;
962 				}
963 
964 				ASSERT(ZTOV(zp)->v_type == VDIR);
965 
966 				/*
967 				 * If we are inheriting an ACE targeted for
968 				 * only files, then make sure inherit_only
969 				 * is on for future propagation.
970 				 */
971 				if ((acep[j].a_flags & (ACE_FILE_INHERIT_ACE |
972 				    ACE_DIRECTORY_INHERIT_ACE)) ==
973 				    ACE_FILE_INHERIT_ACE) {
974 					acep[j].a_flags |= ACE_INHERIT_ONLY_ACE;
975 				} else {
976 					acep[j].a_flags &=
977 					    ~ACE_INHERIT_ONLY_ACE;
978 				}
979 
980 				zfs_securemode_update(zfsvfs, &acep[j]);
981 				j++;
982 			}
983 		}
984 	}
985 	aclp->z_acl_count = j;
986 	ASSERT(aclp->z_slots >= aclp->z_acl_count);
987 
988 	return (aclp);
989 }
990 
991 /*
992  * Create file system object initial permissions
993  * including inheritable ACEs.
994  */
995 void
996 zfs_perm_init(znode_t *zp, znode_t *parent, int flag,
997     vattr_t *vap, dmu_tx_t *tx, cred_t *cr)
998 {
999 	uint64_t	mode;
1000 	uid_t		uid;
1001 	gid_t		gid;
1002 	int		error;
1003 	int		pull_down;
1004 	zfs_acl_t	*aclp, *paclp;
1005 
1006 	mode = MAKEIMODE(vap->va_type, vap->va_mode);
1007 
1008 	/*
1009 	 * Determine uid and gid.
1010 	 */
1011 	if ((flag & (IS_ROOT_NODE | IS_REPLAY)) ||
1012 	    ((flag & IS_XATTR) && (vap->va_type == VDIR))) {
1013 		uid = vap->va_uid;
1014 		gid = vap->va_gid;
1015 	} else {
1016 		uid = crgetuid(cr);
1017 		if ((vap->va_mask & AT_GID) &&
1018 		    ((vap->va_gid == parent->z_phys->zp_gid) ||
1019 		    groupmember(vap->va_gid, cr) ||
1020 		    secpolicy_vnode_create_gid(cr)))
1021 			gid = vap->va_gid;
1022 		else
1023 			gid = (parent->z_phys->zp_mode & S_ISGID) ?
1024 			    parent->z_phys->zp_gid : crgetgid(cr);
1025 	}
1026 
1027 	/*
1028 	 * If we're creating a directory, and the parent directory has the
1029 	 * set-GID bit set, set in on the new directory.
1030 	 * Otherwise, if the user is neither privileged nor a member of the
1031 	 * file's new group, clear the file's set-GID bit.
1032 	 */
1033 
1034 	if ((parent->z_phys->zp_mode & S_ISGID) && (vap->va_type == VDIR))
1035 		mode |= S_ISGID;
1036 	else {
1037 		if ((mode & S_ISGID) &&
1038 		    secpolicy_vnode_setids_setgids(cr, gid) != 0)
1039 			mode &= ~S_ISGID;
1040 	}
1041 
1042 	zp->z_phys->zp_uid = uid;
1043 	zp->z_phys->zp_gid = gid;
1044 	zp->z_phys->zp_mode = mode;
1045 
1046 	mutex_enter(&parent->z_lock);
1047 	pull_down = (parent->z_phys->zp_flags & ZFS_INHERIT_ACE);
1048 	if (pull_down) {
1049 		mutex_enter(&parent->z_acl_lock);
1050 		paclp = zfs_acl_node_read(parent);
1051 		mutex_exit(&parent->z_acl_lock);
1052 		aclp = zfs_acl_inherit(zp, paclp);
1053 		zfs_acl_free(paclp);
1054 	} else {
1055 		aclp = zfs_acl_alloc(6);
1056 	}
1057 	mutex_exit(&parent->z_lock);
1058 	mutex_enter(&zp->z_lock);
1059 	mutex_enter(&zp->z_acl_lock);
1060 	error = zfs_acl_chmod(zp, mode, aclp, tx);
1061 	mutex_exit(&zp->z_lock);
1062 	mutex_exit(&zp->z_acl_lock);
1063 	ASSERT3U(error, ==, 0);
1064 	zfs_acl_free(aclp);
1065 }
1066 
1067 /*
1068  * Can use be used for inheritance
1069  */
1070 static int
1071 zfs_ace_can_use(znode_t *zp, ace_t *acep)
1072 {
1073 	int vtype = ZTOV(zp)->v_type;
1074 
1075 	int	iflags = (acep->a_flags & 0xf);
1076 
1077 	if ((vtype == VDIR) && (iflags & ACE_DIRECTORY_INHERIT_ACE))
1078 		return (1);
1079 
1080 	else if (iflags & ACE_FILE_INHERIT_ACE)
1081 		return (1);
1082 
1083 	return (0);
1084 }
1085 
1086 /*
1087  * Retrieve a files ACL
1088  */
1089 int
1090 zfs_getacl(znode_t *zp, vsecattr_t  *vsecp, cred_t *cr)
1091 {
1092 	zfs_acl_t	*aclp;
1093 	ulong_t		mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT);
1094 	int		error;
1095 
1096 	if (error = zfs_zaccess(zp, ACE_READ_ACL, cr)) {
1097 		/*
1098 		 * If owner of file then allow reading of the
1099 		 * ACL.
1100 		 */
1101 		if (crgetuid(cr) != zp->z_phys->zp_uid)
1102 			return (error);
1103 	}
1104 
1105 	if (mask == 0)
1106 		return (ENOSYS);
1107 
1108 	mutex_enter(&zp->z_acl_lock);
1109 
1110 	aclp = zfs_acl_node_read(zp);
1111 
1112 	if (mask & VSA_ACECNT) {
1113 		vsecp->vsa_aclcnt = aclp->z_acl_count;
1114 	}
1115 
1116 	if (mask & VSA_ACE) {
1117 		vsecp->vsa_aclentp = kmem_alloc(aclp->z_acl_count *
1118 		    sizeof (ace_t), KM_SLEEP);
1119 		bcopy(aclp->z_acl, vsecp->vsa_aclentp,
1120 		    aclp->z_acl_count * sizeof (ace_t));
1121 	}
1122 
1123 	mutex_exit(&zp->z_acl_lock);
1124 
1125 	zfs_acl_free(aclp);
1126 
1127 	return (0);
1128 }
1129 
1130 /*
1131  * Set a files ACL
1132  */
1133 int
1134 zfs_setacl(znode_t *zp, vsecattr_t *vsecp, cred_t *cr)
1135 {
1136 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1137 	zilog_t		*zilog = zfsvfs->z_log;
1138 	ace_t		*acep = vsecp->vsa_aclentp;
1139 	int		aclcnt = vsecp->vsa_aclcnt;
1140 	ulong_t		mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT);
1141 	dmu_tx_t	*tx;
1142 	int		error;
1143 	int		inherit;
1144 	zfs_acl_t	*aclp;
1145 	uint64_t	seq = 0;
1146 
1147 	if (mask == 0)
1148 		return (EINVAL);
1149 
1150 	if (!zfs_acl_valid(zp, acep, aclcnt, &inherit))
1151 		return (EINVAL);
1152 top:
1153 	error = zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr);
1154 	if (error == EACCES || error == ACCESS_UNDETERMINED) {
1155 		if ((error = secpolicy_vnode_setdac(cr,
1156 		    zp->z_phys->zp_uid)) != 0) {
1157 			return (error);
1158 		}
1159 	} else if (error) {
1160 		return (error == EROFS ? error : EPERM);
1161 	}
1162 
1163 	mutex_enter(&zp->z_lock);
1164 	mutex_enter(&zp->z_acl_lock);
1165 
1166 	tx = dmu_tx_create(zfsvfs->z_os);
1167 	dmu_tx_hold_bonus(tx, zp->z_id);
1168 
1169 	if (zp->z_phys->zp_acl.z_acl_extern_obj) {
1170 		dmu_tx_hold_write(tx, zp->z_phys->zp_acl.z_acl_extern_obj,
1171 		    0, ZFS_ACL_SIZE(aclcnt));
1172 	} else if (aclcnt > ACE_SLOT_CNT) {
1173 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, ZFS_ACL_SIZE(aclcnt));
1174 	}
1175 
1176 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1177 	if (error) {
1178 		dmu_tx_abort(tx);
1179 
1180 		mutex_exit(&zp->z_acl_lock);
1181 		mutex_exit(&zp->z_lock);
1182 
1183 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1184 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
1185 			goto top;
1186 		}
1187 		return (error);
1188 	}
1189 
1190 	aclp = zfs_acl_alloc(aclcnt);
1191 	bcopy(acep, aclp->z_acl, sizeof (ace_t) * aclcnt);
1192 	aclp->z_acl_count = aclcnt;
1193 	error = zfs_aclset_common(zp, aclp, tx, &inherit);
1194 	ASSERT(error == 0);
1195 
1196 	zfs_acl_free(aclp);
1197 	seq = zfs_log_acl(zilog, tx, TX_ACL, zp, aclcnt, acep);
1198 	dmu_tx_commit(tx);
1199 done:
1200 	mutex_exit(&zp->z_acl_lock);
1201 	mutex_exit(&zp->z_lock);
1202 
1203 	zil_commit(zilog, seq, 0);
1204 
1205 	return (error);
1206 }
1207 
1208 static int
1209 zfs_ace_access(ace_t *zacep, int mode_wanted, int *working_mode)
1210 {
1211 	if ((*working_mode & mode_wanted) == mode_wanted) {
1212 		return (0);
1213 	}
1214 
1215 	if (zacep->a_access_mask & mode_wanted) {
1216 		if (zacep->a_type == ALLOW) {
1217 			*working_mode |= (mode_wanted & zacep->a_access_mask);
1218 			if ((*working_mode & mode_wanted) == mode_wanted)
1219 				return (0);
1220 		} else if (zacep->a_type == DENY) {
1221 			return (EACCES);
1222 		}
1223 	}
1224 
1225 	/*
1226 	 * haven't been specifcally denied at this point
1227 	 * so return UNDETERMINED.
1228 	 */
1229 
1230 	return (ACCESS_UNDETERMINED);
1231 }
1232 
1233 
1234 static int
1235 zfs_zaccess_common(znode_t *zp, int v4_mode, int *working_mode, cred_t *cr)
1236 {
1237 	zfs_acl_t	*aclp;
1238 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1239 	ace_t		*zacep;
1240 	gid_t		gid;
1241 	int		mode_wanted = v4_mode;
1242 	int		cnt;
1243 	int		i;
1244 	int		access_deny = ACCESS_UNDETERMINED;
1245 	uint_t		entry_type;
1246 	uid_t		uid = crgetuid(cr);
1247 
1248 	*working_mode = 0;
1249 
1250 	if (zfsvfs->z_assign >= TXG_INITIAL)		/* ZIL replay */
1251 		return (0);
1252 
1253 	if ((v4_mode & WRITE_MASK) &&
1254 	    (zp->z_zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) &&
1255 	    (!IS_DEVVP(ZTOV(zp)))) {
1256 		return (EROFS);
1257 	}
1258 
1259 	mutex_enter(&zp->z_acl_lock);
1260 
1261 	aclp = zfs_acl_node_read(zp);
1262 
1263 	zacep = aclp->z_acl;
1264 	cnt = aclp->z_acl_count;
1265 
1266 	for (i = 0; i != cnt; i++) {
1267 
1268 		if (zacep[i].a_flags & ACE_INHERIT_ONLY_ACE)
1269 			continue;
1270 
1271 		entry_type = (zacep[i].a_flags & 0xf040);
1272 		switch (entry_type) {
1273 		case ACE_OWNER:
1274 			if (uid == zp->z_phys->zp_uid) {
1275 				access_deny = zfs_ace_access(&zacep[i],
1276 				    mode_wanted, working_mode);
1277 			}
1278 			break;
1279 		case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
1280 		case ACE_IDENTIFIER_GROUP:
1281 			/*
1282 			 * Owning group gid is in znode not ACL
1283 			 */
1284 			if (entry_type == (ACE_IDENTIFIER_GROUP | ACE_GROUP))
1285 				gid = zp->z_phys->zp_gid;
1286 			else
1287 				gid = zacep[i].a_who;
1288 
1289 			if (groupmember(gid, cr)) {
1290 				access_deny = zfs_ace_access(&zacep[i],
1291 				    mode_wanted, working_mode);
1292 			}
1293 			break;
1294 		case ACE_EVERYONE:
1295 			access_deny = zfs_ace_access(&zacep[i],
1296 			    mode_wanted, working_mode);
1297 			break;
1298 
1299 		/* USER Entry */
1300 		default:
1301 			if (entry_type == 0) {
1302 				if (uid == zacep[i].a_who) {
1303 					access_deny = zfs_ace_access(&zacep[i],
1304 					    mode_wanted, working_mode);
1305 				}
1306 				break;
1307 			}
1308 			zfs_acl_free(aclp);
1309 			mutex_exit(&zp->z_acl_lock);
1310 			return (EIO);
1311 		}
1312 
1313 		if (access_deny != ACCESS_UNDETERMINED)
1314 			break;
1315 
1316 	}
1317 
1318 	mutex_exit(&zp->z_acl_lock);
1319 	zfs_acl_free(aclp);
1320 
1321 	return (access_deny);
1322 }
1323 
1324 
1325 /*
1326  * Determine whether Access should be granted/denied, invoking least
1327  * priv subsytem when a deny is determined.
1328  */
1329 int
1330 zfs_zaccess(znode_t *zp, int mode, cred_t *cr)
1331 {
1332 	int	working_mode = 0;
1333 	int	error;
1334 	int	is_attr;
1335 	znode_t	*xzp;
1336 	znode_t *check_zp = zp;
1337 
1338 	is_attr = ((zp->z_phys->zp_flags & ZFS_XATTR) &&
1339 	    (ZTOV(zp)->v_type == VDIR));
1340 
1341 	/*
1342 	 * If attribute then validate against base file
1343 	 */
1344 	if (is_attr) {
1345 		if ((error = zfs_zget(zp->z_zfsvfs,
1346 		    zp->z_phys->zp_parent, &xzp)) != 0)	{
1347 			return (error);
1348 		}
1349 		check_zp = xzp;
1350 		/*
1351 		 * fixup mode to map to xattr perms
1352 		 */
1353 
1354 		if (mode & (ACE_WRITE_DATA|ACE_APPEND_DATA)) {
1355 			mode &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
1356 			mode |= ACE_WRITE_NAMED_ATTRS;
1357 		}
1358 
1359 		if (mode & (ACE_READ_DATA|ACE_EXECUTE)) {
1360 			mode &= ~(ACE_READ_DATA|ACE_EXECUTE);
1361 			mode |= ACE_READ_NAMED_ATTRS;
1362 		}
1363 	}
1364 
1365 	error = zfs_zaccess_common(check_zp, mode, &working_mode, cr);
1366 
1367 	if (error == EROFS) {
1368 		if (is_attr)
1369 			VN_RELE(ZTOV(xzp));
1370 		return (error);
1371 	}
1372 
1373 	if (error || (working_mode != mode)) {
1374 		error = secpolicy_vnode_access(cr, ZTOV(check_zp),
1375 		    check_zp->z_phys->zp_uid, ~zfs_v4_to_unix(working_mode));
1376 	}
1377 
1378 	if (is_attr)
1379 		VN_RELE(ZTOV(xzp));
1380 
1381 	return (error);
1382 }
1383 
1384 /*
1385  * Special zaccess function to check for special nfsv4 perm.
1386  * doesn't call secpolicy_vnode_access() for failure, since that
1387  * would probably be the wrong policy function to call.
1388  * instead its up to the caller to handle that situation.
1389  */
1390 
1391 int
1392 zfs_zaccess_v4_perm(znode_t *zp, int mode, cred_t *cr)
1393 {
1394 	int working_mode = 0;
1395 	return (zfs_zaccess_common(zp, mode, &working_mode, cr));
1396 }
1397 
1398 /*
1399  * Translate tradition unix VREAD/VWRITE/VEXEC mode into
1400  * native ACL format and call zfs_zaccess()
1401  */
1402 int
1403 zfs_zaccess_rwx(znode_t *zp, mode_t mode, cred_t *cr)
1404 {
1405 	int v4_mode = zfs_unix_to_v4(mode >> 6);
1406 
1407 	return (zfs_zaccess(zp, v4_mode, cr));
1408 }
1409 
1410 /*
1411  * Determine whether Access should be granted/deny, without
1412  * consulting least priv subsystem.
1413  *
1414  *
1415  * The following chart is the recommended NFSv4 enforcement for
1416  * ability to delete an object.
1417  *
1418  *      -------------------------------------------------------
1419  *      |   Parent Dir  |           Target Object Permissions |
1420  *      |  permissions  |                                     |
1421  *      -------------------------------------------------------
1422  *      |               | ACL Allows | ACL Denies| Delete     |
1423  *      |               |  Delete    |  Delete   | unspecified|
1424  *      -------------------------------------------------------
1425  *      |  ACL Allows   | Permit     | Permit    | Permit     |
1426  *      |  DELETE_CHILD |                                     |
1427  *      -------------------------------------------------------
1428  *      |  ACL Denies   | Permit     | Deny      | Deny       |
1429  *      |  DELETE_CHILD |            |           |            |
1430  *      -------------------------------------------------------
1431  *      | ACL specifies |            |           |            |
1432  *      | only allow    | Permit     | Permit    | Permit     |
1433  *      | write and     |            |           |            |
1434  *      | execute       |            |           |            |
1435  *      -------------------------------------------------------
1436  *      | ACL denies    |            |           |            |
1437  *      | write and     | Permit     | Deny      | Deny       |
1438  *      | execute       |            |           |            |
1439  *      -------------------------------------------------------
1440  *         ^
1441  *         |
1442  *         No search privilege, can't even look up file?
1443  *
1444  */
1445 int
1446 zfs_zaccess_delete(znode_t *dzp, znode_t *zp, cred_t *cr)
1447 {
1448 	int dzp_working_mode = 0;
1449 	int zp_working_mode = 0;
1450 	int dzp_error, zp_error;
1451 
1452 	/*
1453 	 * Arghh, this check is going to require a couple of questions
1454 	 * to be asked.  We want specific DELETE permissions to
1455 	 * take precedence over WRITE/EXECUTE.  We don't
1456 	 * want an ACL such as this to mess us up.
1457 	 * user:sloar:write_data:deny,user:sloar:delete:allow
1458 	 *
1459 	 * However, deny permissions may ultimately be overridden
1460 	 * by secpolicy_vnode_access().
1461 	 */
1462 
1463 	dzp_error = zfs_zaccess_common(dzp, ACE_DELETE_CHILD,
1464 	    &dzp_working_mode, cr);
1465 	zp_error = zfs_zaccess_common(zp, ACE_DELETE, &zp_working_mode, cr);
1466 
1467 	if (dzp_error == EROFS || zp_error == EROFS)
1468 		return (dzp_error);
1469 
1470 	/*
1471 	 * First handle the first row
1472 	 */
1473 	if (dzp_working_mode & ACE_DELETE_CHILD)
1474 		return (0);
1475 
1476 	/*
1477 	 * Second row
1478 	 */
1479 
1480 	if (zp_working_mode & ACE_DELETE)
1481 		return (0);
1482 
1483 	/*
1484 	 * Third Row
1485 	 */
1486 
1487 	dzp_error = zfs_zaccess_common(dzp, ACE_WRITE_DATA|ACE_EXECUTE,
1488 	    &dzp_working_mode, cr);
1489 
1490 	if (dzp_error == EROFS)
1491 		return (dzp_error);
1492 
1493 	if (dzp_working_mode & (ACE_WRITE_DATA|ACE_EXECUTE))
1494 		return (0);
1495 
1496 	/*
1497 	 * Fourth Row
1498 	 */
1499 
1500 	if (((dzp_working_mode & (ACE_WRITE_DATA|ACE_EXECUTE)) == 0) &&
1501 	    (zp_working_mode & ACE_DELETE))
1502 		return (0);
1503 
1504 	return (secpolicy_vnode_access(cr, ZTOV(zp), dzp->z_phys->zp_uid,
1505 	    S_IWRITE|S_IEXEC));
1506 }
1507 
1508 int
1509 zfs_zaccess_rename(znode_t *sdzp, znode_t *szp, znode_t *tdzp,
1510     znode_t *tzp, cred_t *cr)
1511 {
1512 	int add_perm;
1513 	int error;
1514 
1515 	add_perm = (ZTOV(szp)->v_type == VDIR) ?
1516 	    ACE_ADD_SUBDIRECTORY : ACE_ADD_FILE;
1517 
1518 	/*
1519 	 * Rename permissions are combination of delete permission +
1520 	 * add file/subdir permission.
1521 	 */
1522 
1523 	/*
1524 	 * first make sure we do the delete portion.
1525 	 *
1526 	 * If that succeeds then check for add_file/add_subdir permissions
1527 	 */
1528 
1529 	if (error = zfs_zaccess_delete(sdzp, szp, cr))
1530 		return (error);
1531 
1532 	/*
1533 	 * If we have a tzp, see if we can delete it?
1534 	 */
1535 	if (tzp) {
1536 		if (error = zfs_zaccess_delete(tdzp, tzp, cr))
1537 			return (error);
1538 	}
1539 
1540 	/*
1541 	 * Now check for add permissions
1542 	 */
1543 	if (error = zfs_zaccess(sdzp, add_perm, cr))
1544 		return (error);
1545 
1546 	error = zfs_sticky_remove_access(sdzp, szp, cr);
1547 
1548 	return (error);
1549 }
1550