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