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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
27 #include <sys/types.h>
28 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/resource.h>
34 #include <sys/vnode.h>
38 #include <sys/cmn_err.h>
39 #include <sys/errno.h>
40 #include <sys/unistd.h>
42 #include <sys/fs/zfs.h>
43 #include <sys/policy.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/zfs_fuid.h>
46 #include <sys/zfs_acl.h>
47 #include <sys/zfs_dir.h>
48 #include <sys/zfs_vfsops.h>
50 #include <sys/dnode.h>
53 #include <acl/acl_common.h>
55 #define ALLOW ACE_ACCESS_ALLOWED_ACE_TYPE
56 #define DENY ACE_ACCESS_DENIED_ACE_TYPE
57 #define MAX_ACE_TYPE ACE_SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE
58 #define MIN_ACE_TYPE ALLOW
60 #define OWNING_GROUP (ACE_GROUP|ACE_IDENTIFIER_GROUP)
61 #define EVERYONE_ALLOW_MASK (ACE_READ_ACL|ACE_READ_ATTRIBUTES | \
62 ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE)
63 #define EVERYONE_DENY_MASK (ACE_WRITE_ACL|ACE_WRITE_OWNER | \
64 ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
65 #define OWNER_ALLOW_MASK (ACE_WRITE_ACL | ACE_WRITE_OWNER | \
66 ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
68 #define ZFS_CHECKED_MASKS (ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_DATA| \
69 ACE_READ_NAMED_ATTRS|ACE_WRITE_DATA|ACE_WRITE_ATTRIBUTES| \
70 ACE_WRITE_NAMED_ATTRS|ACE_APPEND_DATA|ACE_EXECUTE|ACE_WRITE_OWNER| \
71 ACE_WRITE_ACL|ACE_DELETE|ACE_DELETE_CHILD|ACE_SYNCHRONIZE)
73 #define WRITE_MASK_DATA (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS)
74 #define WRITE_MASK_ATTRS (ACE_WRITE_ACL|ACE_WRITE_OWNER|ACE_WRITE_ATTRIBUTES| \
75 ACE_DELETE|ACE_DELETE_CHILD)
76 #define WRITE_MASK (WRITE_MASK_DATA|WRITE_MASK_ATTRS)
78 #define OGE_CLEAR (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
79 ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
81 #define OKAY_MASK_BITS (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
82 ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
84 #define ALL_INHERIT (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE | \
85 ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE|ACE_INHERITED_ACE)
87 #define RESTRICTED_CLEAR (ACE_WRITE_ACL|ACE_WRITE_OWNER)
89 #define V4_ACL_WIDE_FLAGS (ZFS_ACL_AUTO_INHERIT|ZFS_ACL_DEFAULTED|\
92 #define ZFS_ACL_WIDE_FLAGS (V4_ACL_WIDE_FLAGS|ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|\
95 #define ALL_MODE_EXECS (S_IXUSR | S_IXGRP | S_IXOTH)
98 zfs_ace_v0_get_type(void *acep)
100 return (((zfs_oldace_t *)acep)->z_type);
104 zfs_ace_v0_get_flags(void *acep)
106 return (((zfs_oldace_t *)acep)->z_flags);
110 zfs_ace_v0_get_mask(void *acep)
112 return (((zfs_oldace_t *)acep)->z_access_mask);
116 zfs_ace_v0_get_who(void *acep)
118 return (((zfs_oldace_t *)acep)->z_fuid);
122 zfs_ace_v0_set_type(void *acep, uint16_t type)
124 ((zfs_oldace_t *)acep)->z_type = type;
128 zfs_ace_v0_set_flags(void *acep, uint16_t flags)
130 ((zfs_oldace_t *)acep)->z_flags = flags;
134 zfs_ace_v0_set_mask(void *acep, uint32_t mask)
136 ((zfs_oldace_t *)acep)->z_access_mask = mask;
140 zfs_ace_v0_set_who(void *acep, uint64_t who)
142 ((zfs_oldace_t *)acep)->z_fuid = who;
147 zfs_ace_v0_size(void *acep)
149 return (sizeof (zfs_oldace_t));
153 zfs_ace_v0_abstract_size(void)
155 return (sizeof (zfs_oldace_t));
159 zfs_ace_v0_mask_off(void)
161 return (offsetof(zfs_oldace_t, z_access_mask));
166 zfs_ace_v0_data(void *acep, void **datap)
172 static acl_ops_t zfs_acl_v0_ops = {
175 zfs_ace_v0_get_flags,
176 zfs_ace_v0_set_flags,
182 zfs_ace_v0_abstract_size,
188 zfs_ace_fuid_get_type(void *acep)
190 return (((zfs_ace_hdr_t *)acep)->z_type);
194 zfs_ace_fuid_get_flags(void *acep)
196 return (((zfs_ace_hdr_t *)acep)->z_flags);
200 zfs_ace_fuid_get_mask(void *acep)
202 return (((zfs_ace_hdr_t *)acep)->z_access_mask);
206 zfs_ace_fuid_get_who(void *args)
209 zfs_ace_t *acep = args;
211 entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
213 if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
214 entry_type == ACE_EVERYONE)
216 return (((zfs_ace_t *)acep)->z_fuid);
220 zfs_ace_fuid_set_type(void *acep, uint16_t type)
222 ((zfs_ace_hdr_t *)acep)->z_type = type;
226 zfs_ace_fuid_set_flags(void *acep, uint16_t flags)
228 ((zfs_ace_hdr_t *)acep)->z_flags = flags;
232 zfs_ace_fuid_set_mask(void *acep, uint32_t mask)
234 ((zfs_ace_hdr_t *)acep)->z_access_mask = mask;
238 zfs_ace_fuid_set_who(void *arg, uint64_t who)
240 zfs_ace_t *acep = arg;
242 uint16_t entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
244 if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
245 entry_type == ACE_EVERYONE)
251 zfs_ace_fuid_size(void *acep)
253 zfs_ace_hdr_t *zacep = acep;
256 switch (zacep->z_type) {
257 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
258 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
259 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
260 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
261 return (sizeof (zfs_object_ace_t));
265 (((zfs_ace_hdr_t *)acep)->z_flags & ACE_TYPE_FLAGS);
266 if (entry_type == ACE_OWNER ||
267 entry_type == OWNING_GROUP ||
268 entry_type == ACE_EVERYONE)
269 return (sizeof (zfs_ace_hdr_t));
272 return (sizeof (zfs_ace_t));
277 zfs_ace_fuid_abstract_size(void)
279 return (sizeof (zfs_ace_hdr_t));
283 zfs_ace_fuid_mask_off(void)
285 return (offsetof(zfs_ace_hdr_t, z_access_mask));
289 zfs_ace_fuid_data(void *acep, void **datap)
291 zfs_ace_t *zacep = acep;
292 zfs_object_ace_t *zobjp;
294 switch (zacep->z_hdr.z_type) {
295 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
296 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
297 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
298 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
300 *datap = (caddr_t)zobjp + sizeof (zfs_ace_t);
301 return (sizeof (zfs_object_ace_t) - sizeof (zfs_ace_t));
308 static acl_ops_t zfs_acl_fuid_ops = {
309 zfs_ace_fuid_get_mask,
310 zfs_ace_fuid_set_mask,
311 zfs_ace_fuid_get_flags,
312 zfs_ace_fuid_set_flags,
313 zfs_ace_fuid_get_type,
314 zfs_ace_fuid_set_type,
315 zfs_ace_fuid_get_who,
316 zfs_ace_fuid_set_who,
318 zfs_ace_fuid_abstract_size,
319 zfs_ace_fuid_mask_off,
324 * The following three functions are provided for compatibility with
325 * older ZPL version in order to determine if the file use to have
326 * an external ACL and what version of ACL previously existed on the
327 * file. Would really be nice to not need this, sigh.
330 zfs_external_acl(znode_t *zp)
332 zfs_acl_phys_t acl_phys;
339 * Need to deal with a potential
340 * race where zfs_sa_upgrade could cause
341 * z_isa_sa to change.
343 * If the lookup fails then the state of z_is_sa should have
347 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zp->z_zfsvfs),
348 &acl_phys, sizeof (acl_phys))) == 0)
349 return (acl_phys.z_acl_extern_obj);
352 * after upgrade the SA_ZPL_ZNODE_ACL should have been
355 VERIFY(zp->z_is_sa && error == ENOENT);
361 * Determine size of ACL in bytes
363 * This is more complicated than it should be since we have to deal
364 * with old external ACLs.
367 zfs_acl_znode_info(znode_t *zp, int *aclsize, int *aclcount,
368 zfs_acl_phys_t *aclphys)
370 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
375 ASSERT(MUTEX_HELD(&zp->z_acl_lock));
377 if ((error = sa_size(zp->z_sa_hdl, SA_ZPL_DACL_ACES(zfsvfs),
381 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DACL_COUNT(zfsvfs),
382 &acl_count, sizeof (acl_count))) != 0)
384 *aclcount = acl_count;
386 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zfsvfs),
387 aclphys, sizeof (*aclphys))) != 0)
390 if (aclphys->z_acl_version == ZFS_ACL_VERSION_INITIAL) {
391 *aclsize = ZFS_ACL_SIZE(aclphys->z_acl_size);
392 *aclcount = aclphys->z_acl_size;
394 *aclsize = aclphys->z_acl_size;
395 *aclcount = aclphys->z_acl_count;
402 zfs_znode_acl_version(znode_t *zp)
404 zfs_acl_phys_t acl_phys;
407 return (ZFS_ACL_VERSION_FUID);
412 * Need to deal with a potential
413 * race where zfs_sa_upgrade could cause
414 * z_isa_sa to change.
416 * If the lookup fails then the state of z_is_sa should have
419 if ((error = sa_lookup(zp->z_sa_hdl,
420 SA_ZPL_ZNODE_ACL(zp->z_zfsvfs),
421 &acl_phys, sizeof (acl_phys))) == 0)
422 return (acl_phys.z_acl_version);
425 * After upgrade SA_ZPL_ZNODE_ACL should have
428 VERIFY(zp->z_is_sa && error == ENOENT);
429 return (ZFS_ACL_VERSION_FUID);
435 zfs_acl_version(int version)
437 if (version < ZPL_VERSION_FUID)
438 return (ZFS_ACL_VERSION_INITIAL);
440 return (ZFS_ACL_VERSION_FUID);
444 zfs_acl_version_zp(znode_t *zp)
446 return (zfs_acl_version(zp->z_zfsvfs->z_version));
450 zfs_acl_alloc(int vers)
454 aclp = kmem_zalloc(sizeof (zfs_acl_t), KM_SLEEP);
455 list_create(&aclp->z_acl, sizeof (zfs_acl_node_t),
456 offsetof(zfs_acl_node_t, z_next));
457 aclp->z_version = vers;
458 if (vers == ZFS_ACL_VERSION_FUID)
459 aclp->z_ops = zfs_acl_fuid_ops;
461 aclp->z_ops = zfs_acl_v0_ops;
466 zfs_acl_node_alloc(size_t bytes)
468 zfs_acl_node_t *aclnode;
470 aclnode = kmem_zalloc(sizeof (zfs_acl_node_t), KM_SLEEP);
472 aclnode->z_acldata = kmem_alloc(bytes, KM_SLEEP);
473 aclnode->z_allocdata = aclnode->z_acldata;
474 aclnode->z_allocsize = bytes;
475 aclnode->z_size = bytes;
482 zfs_acl_node_free(zfs_acl_node_t *aclnode)
484 if (aclnode->z_allocsize)
485 kmem_free(aclnode->z_allocdata, aclnode->z_allocsize);
486 kmem_free(aclnode, sizeof (zfs_acl_node_t));
490 zfs_acl_release_nodes(zfs_acl_t *aclp)
492 zfs_acl_node_t *aclnode;
494 while (aclnode = list_head(&aclp->z_acl)) {
495 list_remove(&aclp->z_acl, aclnode);
496 zfs_acl_node_free(aclnode);
498 aclp->z_acl_count = 0;
499 aclp->z_acl_bytes = 0;
503 zfs_acl_free(zfs_acl_t *aclp)
505 zfs_acl_release_nodes(aclp);
506 list_destroy(&aclp->z_acl);
507 kmem_free(aclp, sizeof (zfs_acl_t));
511 zfs_acl_valid_ace_type(uint_t type, uint_t flags)
518 case ACE_SYSTEM_AUDIT_ACE_TYPE:
519 case ACE_SYSTEM_ALARM_ACE_TYPE:
520 entry_type = flags & ACE_TYPE_FLAGS;
521 return (entry_type == ACE_OWNER ||
522 entry_type == OWNING_GROUP ||
523 entry_type == ACE_EVERYONE || entry_type == 0 ||
524 entry_type == ACE_IDENTIFIER_GROUP);
526 if (type >= MIN_ACE_TYPE && type <= MAX_ACE_TYPE)
533 zfs_ace_valid(vtype_t obj_type, zfs_acl_t *aclp, uint16_t type, uint16_t iflags)
536 * first check type of entry
539 if (!zfs_acl_valid_ace_type(type, iflags))
543 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
544 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
545 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
546 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
547 if (aclp->z_version < ZFS_ACL_VERSION_FUID)
549 aclp->z_hints |= ZFS_ACL_OBJ_ACE;
553 * next check inheritance level flags
556 if (obj_type == VDIR &&
557 (iflags & (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE)))
558 aclp->z_hints |= ZFS_INHERIT_ACE;
560 if (iflags & (ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE)) {
561 if ((iflags & (ACE_FILE_INHERIT_ACE|
562 ACE_DIRECTORY_INHERIT_ACE)) == 0) {
571 zfs_acl_next_ace(zfs_acl_t *aclp, void *start, uint64_t *who,
572 uint32_t *access_mask, uint16_t *iflags, uint16_t *type)
574 zfs_acl_node_t *aclnode;
579 aclnode = list_head(&aclp->z_acl);
583 aclp->z_next_ace = aclnode->z_acldata;
584 aclp->z_curr_node = aclnode;
585 aclnode->z_ace_idx = 0;
588 aclnode = aclp->z_curr_node;
593 if (aclnode->z_ace_idx >= aclnode->z_ace_count) {
594 aclnode = list_next(&aclp->z_acl, aclnode);
598 aclp->z_curr_node = aclnode;
599 aclnode->z_ace_idx = 0;
600 aclp->z_next_ace = aclnode->z_acldata;
604 if (aclnode->z_ace_idx < aclnode->z_ace_count) {
605 void *acep = aclp->z_next_ace;
609 * Make sure we don't overstep our bounds
611 ace_size = aclp->z_ops.ace_size(acep);
613 if (((caddr_t)acep + ace_size) >
614 ((caddr_t)aclnode->z_acldata + aclnode->z_size)) {
618 *iflags = aclp->z_ops.ace_flags_get(acep);
619 *type = aclp->z_ops.ace_type_get(acep);
620 *access_mask = aclp->z_ops.ace_mask_get(acep);
621 *who = aclp->z_ops.ace_who_get(acep);
622 aclp->z_next_ace = (caddr_t)aclp->z_next_ace + ace_size;
623 aclnode->z_ace_idx++;
625 return ((void *)acep);
632 zfs_ace_walk(void *datap, uint64_t cookie, int aclcnt,
633 uint16_t *flags, uint16_t *type, uint32_t *mask)
635 zfs_acl_t *aclp = datap;
636 zfs_ace_hdr_t *acep = (zfs_ace_hdr_t *)(uintptr_t)cookie;
639 acep = zfs_acl_next_ace(aclp, acep, &who, mask,
641 return ((uint64_t)(uintptr_t)acep);
644 static zfs_acl_node_t *
645 zfs_acl_curr_node(zfs_acl_t *aclp)
647 ASSERT(aclp->z_curr_node);
648 return (aclp->z_curr_node);
652 * Copy ACE to internal ZFS format.
653 * While processing the ACL each ACE will be validated for correctness.
654 * ACE FUIDs will be created later.
657 zfs_copy_ace_2_fuid(zfsvfs_t *zfsvfs, vtype_t obj_type, zfs_acl_t *aclp,
658 void *datap, zfs_ace_t *z_acl, uint64_t aclcnt, size_t *size,
659 zfs_fuid_info_t **fuidp, cred_t *cr)
663 zfs_ace_t *aceptr = z_acl;
665 zfs_object_ace_t *zobjacep;
666 ace_object_t *aceobjp;
668 for (i = 0; i != aclcnt; i++) {
669 aceptr->z_hdr.z_access_mask = acep->a_access_mask;
670 aceptr->z_hdr.z_flags = acep->a_flags;
671 aceptr->z_hdr.z_type = acep->a_type;
672 entry_type = aceptr->z_hdr.z_flags & ACE_TYPE_FLAGS;
673 if (entry_type != ACE_OWNER && entry_type != OWNING_GROUP &&
674 entry_type != ACE_EVERYONE) {
675 aceptr->z_fuid = zfs_fuid_create(zfsvfs, acep->a_who,
676 cr, (entry_type == 0) ?
677 ZFS_ACE_USER : ZFS_ACE_GROUP, fuidp);
681 * Make sure ACE is valid
683 if (zfs_ace_valid(obj_type, aclp, aceptr->z_hdr.z_type,
684 aceptr->z_hdr.z_flags) != B_TRUE)
685 return (SET_ERROR(EINVAL));
687 switch (acep->a_type) {
688 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
689 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
690 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
691 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
692 zobjacep = (zfs_object_ace_t *)aceptr;
693 aceobjp = (ace_object_t *)acep;
695 bcopy(aceobjp->a_obj_type, zobjacep->z_object_type,
696 sizeof (aceobjp->a_obj_type));
697 bcopy(aceobjp->a_inherit_obj_type,
698 zobjacep->z_inherit_type,
699 sizeof (aceobjp->a_inherit_obj_type));
700 acep = (ace_t *)((caddr_t)acep + sizeof (ace_object_t));
703 acep = (ace_t *)((caddr_t)acep + sizeof (ace_t));
706 aceptr = (zfs_ace_t *)((caddr_t)aceptr +
707 aclp->z_ops.ace_size(aceptr));
710 *size = (caddr_t)aceptr - (caddr_t)z_acl;
716 * Copy ZFS ACEs to fixed size ace_t layout
719 zfs_copy_fuid_2_ace(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, cred_t *cr,
720 void *datap, int filter)
723 uint32_t access_mask;
724 uint16_t iflags, type;
725 zfs_ace_hdr_t *zacep = NULL;
727 ace_object_t *objacep;
728 zfs_object_ace_t *zobjacep;
732 while (zacep = zfs_acl_next_ace(aclp, zacep,
733 &who, &access_mask, &iflags, &type)) {
736 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
737 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
738 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
739 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
743 zobjacep = (zfs_object_ace_t *)zacep;
744 objacep = (ace_object_t *)acep;
745 bcopy(zobjacep->z_object_type,
747 sizeof (zobjacep->z_object_type));
748 bcopy(zobjacep->z_inherit_type,
749 objacep->a_inherit_obj_type,
750 sizeof (zobjacep->z_inherit_type));
751 ace_size = sizeof (ace_object_t);
754 ace_size = sizeof (ace_t);
758 entry_type = (iflags & ACE_TYPE_FLAGS);
759 if ((entry_type != ACE_OWNER &&
760 entry_type != OWNING_GROUP &&
761 entry_type != ACE_EVERYONE)) {
762 acep->a_who = zfs_fuid_map_id(zfsvfs, who,
763 cr, (entry_type & ACE_IDENTIFIER_GROUP) ?
764 ZFS_ACE_GROUP : ZFS_ACE_USER);
766 acep->a_who = (uid_t)(int64_t)who;
768 acep->a_access_mask = access_mask;
769 acep->a_flags = iflags;
771 acep = (ace_t *)((caddr_t)acep + ace_size);
776 zfs_copy_ace_2_oldace(vtype_t obj_type, zfs_acl_t *aclp, ace_t *acep,
777 zfs_oldace_t *z_acl, int aclcnt, size_t *size)
780 zfs_oldace_t *aceptr = z_acl;
782 for (i = 0; i != aclcnt; i++, aceptr++) {
783 aceptr->z_access_mask = acep[i].a_access_mask;
784 aceptr->z_type = acep[i].a_type;
785 aceptr->z_flags = acep[i].a_flags;
786 aceptr->z_fuid = acep[i].a_who;
788 * Make sure ACE is valid
790 if (zfs_ace_valid(obj_type, aclp, aceptr->z_type,
791 aceptr->z_flags) != B_TRUE)
792 return (SET_ERROR(EINVAL));
794 *size = (caddr_t)aceptr - (caddr_t)z_acl;
799 * convert old ACL format to new
802 zfs_acl_xform(znode_t *zp, zfs_acl_t *aclp, cred_t *cr)
804 zfs_oldace_t *oldaclp;
806 uint16_t type, iflags;
807 uint32_t access_mask;
810 zfs_acl_node_t *newaclnode;
812 ASSERT(aclp->z_version == ZFS_ACL_VERSION_INITIAL);
814 * First create the ACE in a contiguous piece of memory
815 * for zfs_copy_ace_2_fuid().
817 * We only convert an ACL once, so this won't happen
820 oldaclp = kmem_alloc(sizeof (zfs_oldace_t) * aclp->z_acl_count,
823 while (cookie = zfs_acl_next_ace(aclp, cookie, &who,
824 &access_mask, &iflags, &type)) {
825 oldaclp[i].z_flags = iflags;
826 oldaclp[i].z_type = type;
827 oldaclp[i].z_fuid = who;
828 oldaclp[i++].z_access_mask = access_mask;
831 newaclnode = zfs_acl_node_alloc(aclp->z_acl_count *
832 sizeof (zfs_object_ace_t));
833 aclp->z_ops = zfs_acl_fuid_ops;
834 VERIFY(zfs_copy_ace_2_fuid(zp->z_zfsvfs, ZTOV(zp)->v_type, aclp,
835 oldaclp, newaclnode->z_acldata, aclp->z_acl_count,
836 &newaclnode->z_size, NULL, cr) == 0);
837 newaclnode->z_ace_count = aclp->z_acl_count;
838 aclp->z_version = ZFS_ACL_VERSION;
839 kmem_free(oldaclp, aclp->z_acl_count * sizeof (zfs_oldace_t));
842 * Release all previous ACL nodes
845 zfs_acl_release_nodes(aclp);
847 list_insert_head(&aclp->z_acl, newaclnode);
849 aclp->z_acl_bytes = newaclnode->z_size;
850 aclp->z_acl_count = newaclnode->z_ace_count;
855 * Convert unix access mask to v4 access mask
858 zfs_unix_to_v4(uint32_t access_mask)
860 uint32_t new_mask = 0;
862 if (access_mask & S_IXOTH)
863 new_mask |= ACE_EXECUTE;
864 if (access_mask & S_IWOTH)
865 new_mask |= ACE_WRITE_DATA;
866 if (access_mask & S_IROTH)
867 new_mask |= ACE_READ_DATA;
872 zfs_set_ace(zfs_acl_t *aclp, void *acep, uint32_t access_mask,
873 uint16_t access_type, uint64_t fuid, uint16_t entry_type)
875 uint16_t type = entry_type & ACE_TYPE_FLAGS;
877 aclp->z_ops.ace_mask_set(acep, access_mask);
878 aclp->z_ops.ace_type_set(acep, access_type);
879 aclp->z_ops.ace_flags_set(acep, entry_type);
880 if ((type != ACE_OWNER && type != OWNING_GROUP &&
881 type != ACE_EVERYONE))
882 aclp->z_ops.ace_who_set(acep, fuid);
886 * Determine mode of file based on ACL.
887 * Also, create FUIDs for any User/Group ACEs
890 zfs_mode_compute(uint64_t fmode, zfs_acl_t *aclp,
891 uint64_t *pflags, uint64_t fuid, uint64_t fgid)
896 zfs_ace_hdr_t *acep = NULL;
898 uint16_t iflags, type;
899 uint32_t access_mask;
900 boolean_t an_exec_denied = B_FALSE;
902 mode = (fmode & (S_IFMT | S_ISUID | S_ISGID | S_ISVTX));
904 while (acep = zfs_acl_next_ace(aclp, acep, &who,
905 &access_mask, &iflags, &type)) {
907 if (!zfs_acl_valid_ace_type(type, iflags))
910 entry_type = (iflags & ACE_TYPE_FLAGS);
913 * Skip over owner@, group@ or everyone@ inherit only ACEs
915 if ((iflags & ACE_INHERIT_ONLY_ACE) &&
916 (entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
917 entry_type == OWNING_GROUP))
920 if (entry_type == ACE_OWNER || (entry_type == 0 &&
922 if ((access_mask & ACE_READ_DATA) &&
923 (!(seen & S_IRUSR))) {
929 if ((access_mask & ACE_WRITE_DATA) &&
930 (!(seen & S_IWUSR))) {
936 if ((access_mask & ACE_EXECUTE) &&
937 (!(seen & S_IXUSR))) {
943 } else if (entry_type == OWNING_GROUP ||
944 (entry_type == ACE_IDENTIFIER_GROUP && who == fgid)) {
945 if ((access_mask & ACE_READ_DATA) &&
946 (!(seen & S_IRGRP))) {
952 if ((access_mask & ACE_WRITE_DATA) &&
953 (!(seen & S_IWGRP))) {
959 if ((access_mask & ACE_EXECUTE) &&
960 (!(seen & S_IXGRP))) {
966 } else if (entry_type == ACE_EVERYONE) {
967 if ((access_mask & ACE_READ_DATA)) {
968 if (!(seen & S_IRUSR)) {
974 if (!(seen & S_IRGRP)) {
980 if (!(seen & S_IROTH)) {
987 if ((access_mask & ACE_WRITE_DATA)) {
988 if (!(seen & S_IWUSR)) {
994 if (!(seen & S_IWGRP)) {
1000 if (!(seen & S_IWOTH)) {
1002 if (type == ALLOW) {
1007 if ((access_mask & ACE_EXECUTE)) {
1008 if (!(seen & S_IXUSR)) {
1010 if (type == ALLOW) {
1014 if (!(seen & S_IXGRP)) {
1016 if (type == ALLOW) {
1020 if (!(seen & S_IXOTH)) {
1022 if (type == ALLOW) {
1029 * Only care if this IDENTIFIER_GROUP or
1030 * USER ACE denies execute access to someone,
1031 * mode is not affected
1033 if ((access_mask & ACE_EXECUTE) && type == DENY)
1034 an_exec_denied = B_TRUE;
1039 * Failure to allow is effectively a deny, so execute permission
1040 * is denied if it was never mentioned or if we explicitly
1041 * weren't allowed it.
1043 if (!an_exec_denied &&
1044 ((seen & ALL_MODE_EXECS) != ALL_MODE_EXECS ||
1045 (mode & ALL_MODE_EXECS) != ALL_MODE_EXECS))
1046 an_exec_denied = B_TRUE;
1049 *pflags &= ~ZFS_NO_EXECS_DENIED;
1051 *pflags |= ZFS_NO_EXECS_DENIED;
1057 * Read an external acl object. If the intent is to modify, always
1058 * create a new acl and leave any cached acl in place.
1061 zfs_acl_node_read(znode_t *zp, zfs_acl_t **aclpp, boolean_t will_modify)
1066 zfs_acl_node_t *aclnode;
1067 zfs_acl_phys_t znode_acl;
1071 ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1072 ASSERT_VOP_LOCKED(ZTOV(zp), __func__);
1074 if (zp->z_acl_cached && !will_modify) {
1075 *aclpp = zp->z_acl_cached;
1079 version = zfs_znode_acl_version(zp);
1081 if ((error = zfs_acl_znode_info(zp, &aclsize,
1082 &acl_count, &znode_acl)) != 0) {
1086 aclp = zfs_acl_alloc(version);
1088 aclp->z_acl_count = acl_count;
1089 aclp->z_acl_bytes = aclsize;
1091 aclnode = zfs_acl_node_alloc(aclsize);
1092 aclnode->z_ace_count = aclp->z_acl_count;
1093 aclnode->z_size = aclsize;
1096 if (znode_acl.z_acl_extern_obj) {
1097 error = dmu_read(zp->z_zfsvfs->z_os,
1098 znode_acl.z_acl_extern_obj, 0, aclnode->z_size,
1099 aclnode->z_acldata, DMU_READ_PREFETCH);
1101 bcopy(znode_acl.z_ace_data, aclnode->z_acldata,
1105 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DACL_ACES(zp->z_zfsvfs),
1106 aclnode->z_acldata, aclnode->z_size);
1111 zfs_acl_node_free(aclnode);
1112 /* convert checksum errors into IO errors */
1113 if (error == ECKSUM)
1114 error = SET_ERROR(EIO);
1118 list_insert_head(&aclp->z_acl, aclnode);
1122 zp->z_acl_cached = aclp;
1129 zfs_acl_data_locator(void **dataptr, uint32_t *length, uint32_t buflen,
1130 boolean_t start, void *userdata)
1132 zfs_acl_locator_cb_t *cb = (zfs_acl_locator_cb_t *)userdata;
1135 cb->cb_acl_node = list_head(&cb->cb_aclp->z_acl);
1137 cb->cb_acl_node = list_next(&cb->cb_aclp->z_acl,
1140 *dataptr = cb->cb_acl_node->z_acldata;
1141 *length = cb->cb_acl_node->z_size;
1145 zfs_acl_chown_setattr(znode_t *zp)
1150 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
1151 ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1153 if ((error = zfs_acl_node_read(zp, &aclp, B_FALSE)) == 0)
1154 zp->z_mode = zfs_mode_compute(zp->z_mode, aclp,
1155 &zp->z_pflags, zp->z_uid, zp->z_gid);
1160 * common code for setting ACLs.
1162 * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl.
1163 * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's
1164 * already checked the acl and knows whether to inherit.
1167 zfs_aclset_common(znode_t *zp, zfs_acl_t *aclp, cred_t *cr, dmu_tx_t *tx)
1170 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1171 dmu_object_type_t otype;
1172 zfs_acl_locator_cb_t locate = { 0 };
1174 sa_bulk_attr_t bulk[5];
1180 mode = zfs_mode_compute(mode, aclp, &zp->z_pflags,
1181 zp->z_uid, zp->z_gid);
1184 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1185 &mode, sizeof (mode));
1186 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1187 &zp->z_pflags, sizeof (zp->z_pflags));
1188 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1189 &ctime, sizeof (ctime));
1191 if (zp->z_acl_cached) {
1192 zfs_acl_free(zp->z_acl_cached);
1193 zp->z_acl_cached = NULL;
1199 if (!zfsvfs->z_use_fuids) {
1200 otype = DMU_OT_OLDACL;
1202 if ((aclp->z_version == ZFS_ACL_VERSION_INITIAL) &&
1203 (zfsvfs->z_version >= ZPL_VERSION_FUID))
1204 zfs_acl_xform(zp, aclp, cr);
1205 ASSERT(aclp->z_version >= ZFS_ACL_VERSION_FUID);
1210 * Arrgh, we have to handle old on disk format
1211 * as well as newer (preferred) SA format.
1214 if (zp->z_is_sa) { /* the easy case, just update the ACL attribute */
1215 locate.cb_aclp = aclp;
1216 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DACL_ACES(zfsvfs),
1217 zfs_acl_data_locator, &locate, aclp->z_acl_bytes);
1218 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DACL_COUNT(zfsvfs),
1219 NULL, &aclp->z_acl_count, sizeof (uint64_t));
1220 } else { /* Painful legacy way */
1221 zfs_acl_node_t *aclnode;
1223 zfs_acl_phys_t acl_phys;
1226 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zfsvfs),
1227 &acl_phys, sizeof (acl_phys))) != 0)
1230 aoid = acl_phys.z_acl_extern_obj;
1232 if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1234 * If ACL was previously external and we are now
1235 * converting to new ACL format then release old
1236 * ACL object and create a new one.
1239 aclp->z_version != acl_phys.z_acl_version) {
1240 error = dmu_object_free(zfsvfs->z_os, aoid, tx);
1246 aoid = dmu_object_alloc(zfsvfs->z_os,
1247 otype, aclp->z_acl_bytes,
1248 otype == DMU_OT_ACL ?
1249 DMU_OT_SYSACL : DMU_OT_NONE,
1250 otype == DMU_OT_ACL ?
1251 DN_MAX_BONUSLEN : 0, tx);
1253 (void) dmu_object_set_blocksize(zfsvfs->z_os,
1254 aoid, aclp->z_acl_bytes, 0, tx);
1256 acl_phys.z_acl_extern_obj = aoid;
1257 for (aclnode = list_head(&aclp->z_acl); aclnode;
1258 aclnode = list_next(&aclp->z_acl, aclnode)) {
1259 if (aclnode->z_ace_count == 0)
1261 dmu_write(zfsvfs->z_os, aoid, off,
1262 aclnode->z_size, aclnode->z_acldata, tx);
1263 off += aclnode->z_size;
1266 void *start = acl_phys.z_ace_data;
1268 * Migrating back embedded?
1270 if (acl_phys.z_acl_extern_obj) {
1271 error = dmu_object_free(zfsvfs->z_os,
1272 acl_phys.z_acl_extern_obj, tx);
1275 acl_phys.z_acl_extern_obj = 0;
1278 for (aclnode = list_head(&aclp->z_acl); aclnode;
1279 aclnode = list_next(&aclp->z_acl, aclnode)) {
1280 if (aclnode->z_ace_count == 0)
1282 bcopy(aclnode->z_acldata, start,
1284 start = (caddr_t)start + aclnode->z_size;
1288 * If Old version then swap count/bytes to match old
1289 * layout of znode_acl_phys_t.
1291 if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1292 acl_phys.z_acl_size = aclp->z_acl_count;
1293 acl_phys.z_acl_count = aclp->z_acl_bytes;
1295 acl_phys.z_acl_size = aclp->z_acl_bytes;
1296 acl_phys.z_acl_count = aclp->z_acl_count;
1298 acl_phys.z_acl_version = aclp->z_version;
1300 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
1301 &acl_phys, sizeof (acl_phys));
1305 * Replace ACL wide bits, but first clear them.
1307 zp->z_pflags &= ~ZFS_ACL_WIDE_FLAGS;
1309 zp->z_pflags |= aclp->z_hints;
1311 if (ace_trivial_common(aclp, 0, zfs_ace_walk) == 0)
1312 zp->z_pflags |= ZFS_ACL_TRIVIAL;
1314 zfs_tstamp_update_setup(zp, STATE_CHANGED, NULL, ctime, B_TRUE);
1315 return (sa_bulk_update(zp->z_sa_hdl, bulk, count, tx));
1319 zfs_acl_chmod(vtype_t vtype, uint64_t mode, boolean_t trim, zfs_acl_t *aclp)
1323 int new_count, new_bytes;
1326 uint16_t iflags, type;
1327 uint32_t access_mask;
1328 zfs_acl_node_t *newnode;
1329 size_t abstract_size = aclp->z_ops.ace_abstract_size();
1332 trivial_acl_t masks;
1334 new_count = new_bytes = 0;
1336 isdir = (vtype == VDIR);
1338 acl_trivial_access_masks((mode_t)mode, isdir, &masks);
1340 newnode = zfs_acl_node_alloc((abstract_size * 6) + aclp->z_acl_bytes);
1342 zacep = newnode->z_acldata;
1344 zfs_set_ace(aclp, zacep, masks.allow0, ALLOW, -1, ACE_OWNER);
1345 zacep = (void *)((uintptr_t)zacep + abstract_size);
1347 new_bytes += abstract_size;
1350 zfs_set_ace(aclp, zacep, masks.deny1, DENY, -1, ACE_OWNER);
1351 zacep = (void *)((uintptr_t)zacep + abstract_size);
1353 new_bytes += abstract_size;
1356 zfs_set_ace(aclp, zacep, masks.deny2, DENY, -1, OWNING_GROUP);
1357 zacep = (void *)((uintptr_t)zacep + abstract_size);
1359 new_bytes += abstract_size;
1362 while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
1364 uint16_t inherit_flags;
1366 entry_type = (iflags & ACE_TYPE_FLAGS);
1367 inherit_flags = (iflags & ALL_INHERIT);
1369 if ((entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
1370 (entry_type == OWNING_GROUP)) &&
1371 ((inherit_flags & ACE_INHERIT_ONLY_ACE) == 0)) {
1376 * If this ACL has any inheritable ACEs, mark that in
1377 * the hints (which are later masked into the pflags)
1378 * so create knows to do inheritance.
1380 if (isdir && (inherit_flags &
1381 (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE)))
1382 aclp->z_hints |= ZFS_INHERIT_ACE;
1384 if ((type != ALLOW && type != DENY) ||
1385 (inherit_flags & ACE_INHERIT_ONLY_ACE)) {
1387 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1388 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1389 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1390 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1391 aclp->z_hints |= ZFS_ACL_OBJ_ACE;
1397 * Limit permissions to be no greater than
1398 * group permissions.
1399 * The "aclinherit" and "aclmode" properties
1400 * affect policy for create and chmod(2),
1403 if ((type == ALLOW) && trim)
1404 access_mask &= masks.group;
1406 zfs_set_ace(aclp, zacep, access_mask, type, who, iflags);
1407 ace_size = aclp->z_ops.ace_size(acep);
1408 zacep = (void *)((uintptr_t)zacep + ace_size);
1410 new_bytes += ace_size;
1412 zfs_set_ace(aclp, zacep, masks.owner, 0, -1, ACE_OWNER);
1413 zacep = (void *)((uintptr_t)zacep + abstract_size);
1414 zfs_set_ace(aclp, zacep, masks.group, 0, -1, OWNING_GROUP);
1415 zacep = (void *)((uintptr_t)zacep + abstract_size);
1416 zfs_set_ace(aclp, zacep, masks.everyone, 0, -1, ACE_EVERYONE);
1419 new_bytes += abstract_size * 3;
1420 zfs_acl_release_nodes(aclp);
1421 aclp->z_acl_count = new_count;
1422 aclp->z_acl_bytes = new_bytes;
1423 newnode->z_ace_count = new_count;
1424 newnode->z_size = new_bytes;
1425 list_insert_tail(&aclp->z_acl, newnode);
1429 zfs_acl_chmod_setattr(znode_t *zp, zfs_acl_t **aclp, uint64_t mode)
1433 mutex_enter(&zp->z_acl_lock);
1434 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
1435 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_DISCARD)
1436 *aclp = zfs_acl_alloc(zfs_acl_version_zp(zp));
1438 error = zfs_acl_node_read(zp, aclp, B_TRUE);
1441 (*aclp)->z_hints = zp->z_pflags & V4_ACL_WIDE_FLAGS;
1442 zfs_acl_chmod(ZTOV(zp)->v_type, mode,
1443 (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_GROUPMASK), *aclp);
1445 mutex_exit(&zp->z_acl_lock);
1451 * strip off write_owner and write_acl
1454 zfs_restricted_update(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, void *acep)
1456 uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1458 if ((zfsvfs->z_acl_inherit == ZFS_ACL_RESTRICTED) &&
1459 (aclp->z_ops.ace_type_get(acep) == ALLOW)) {
1460 mask &= ~RESTRICTED_CLEAR;
1461 aclp->z_ops.ace_mask_set(acep, mask);
1466 * Should ACE be inherited?
1469 zfs_ace_can_use(vtype_t vtype, uint16_t acep_flags)
1471 int iflags = (acep_flags & 0xf);
1473 if ((vtype == VDIR) && (iflags & ACE_DIRECTORY_INHERIT_ACE))
1475 else if (iflags & ACE_FILE_INHERIT_ACE)
1476 return (!((vtype == VDIR) &&
1477 (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)));
1482 * inherit inheritable ACEs from parent
1485 zfs_acl_inherit(zfsvfs_t *zfsvfs, vtype_t vtype, zfs_acl_t *paclp,
1486 uint64_t mode, boolean_t *need_chmod)
1490 zfs_acl_node_t *aclnode;
1491 zfs_acl_t *aclp = NULL;
1493 uint32_t access_mask;
1494 uint16_t iflags, newflags, type;
1496 void *data1, *data2;
1497 size_t data1sz, data2sz;
1498 boolean_t vdir = vtype == VDIR;
1499 boolean_t vreg = vtype == VREG;
1500 boolean_t passthrough, passthrough_x, noallow;
1503 zfsvfs->z_acl_inherit == ZFS_ACL_PASSTHROUGH_X;
1504 passthrough = passthrough_x ||
1505 zfsvfs->z_acl_inherit == ZFS_ACL_PASSTHROUGH;
1507 zfsvfs->z_acl_inherit == ZFS_ACL_NOALLOW;
1509 *need_chmod = B_TRUE;
1511 aclp = zfs_acl_alloc(paclp->z_version);
1512 if (zfsvfs->z_acl_inherit == ZFS_ACL_DISCARD || vtype == VLNK)
1514 while (pacep = zfs_acl_next_ace(paclp, pacep, &who,
1515 &access_mask, &iflags, &type)) {
1518 * don't inherit bogus ACEs
1520 if (!zfs_acl_valid_ace_type(type, iflags))
1523 if (noallow && type == ALLOW)
1526 ace_size = aclp->z_ops.ace_size(pacep);
1528 if (!zfs_ace_can_use(vtype, iflags))
1532 * If owner@, group@, or everyone@ inheritable
1533 * then zfs_acl_chmod() isn't needed.
1536 ((iflags & (ACE_OWNER|ACE_EVERYONE)) ||
1537 ((iflags & OWNING_GROUP) ==
1538 OWNING_GROUP)) && (vreg || (vdir && (iflags &
1539 ACE_DIRECTORY_INHERIT_ACE)))) {
1540 *need_chmod = B_FALSE;
1543 if (!vdir && passthrough_x &&
1544 ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)) {
1545 access_mask &= ~ACE_EXECUTE;
1548 aclnode = zfs_acl_node_alloc(ace_size);
1549 list_insert_tail(&aclp->z_acl, aclnode);
1550 acep = aclnode->z_acldata;
1552 zfs_set_ace(aclp, acep, access_mask, type,
1553 who, iflags|ACE_INHERITED_ACE);
1556 * Copy special opaque data if any
1558 if ((data1sz = paclp->z_ops.ace_data(pacep, &data1)) != 0) {
1559 VERIFY((data2sz = aclp->z_ops.ace_data(acep,
1560 &data2)) == data1sz);
1561 bcopy(data1, data2, data2sz);
1564 aclp->z_acl_count++;
1565 aclnode->z_ace_count++;
1566 aclp->z_acl_bytes += aclnode->z_size;
1567 newflags = aclp->z_ops.ace_flags_get(acep);
1570 aclp->z_hints |= ZFS_INHERIT_ACE;
1572 if ((iflags & ACE_NO_PROPAGATE_INHERIT_ACE) || !vdir) {
1573 newflags &= ~ALL_INHERIT;
1574 aclp->z_ops.ace_flags_set(acep,
1575 newflags|ACE_INHERITED_ACE);
1576 zfs_restricted_update(zfsvfs, aclp, acep);
1583 * If only FILE_INHERIT is set then turn on
1586 if ((iflags & (ACE_FILE_INHERIT_ACE |
1587 ACE_DIRECTORY_INHERIT_ACE)) == ACE_FILE_INHERIT_ACE) {
1588 newflags |= ACE_INHERIT_ONLY_ACE;
1589 aclp->z_ops.ace_flags_set(acep,
1590 newflags|ACE_INHERITED_ACE);
1592 newflags &= ~ACE_INHERIT_ONLY_ACE;
1593 aclp->z_ops.ace_flags_set(acep,
1594 newflags|ACE_INHERITED_ACE);
1601 * Create file system object initial permissions
1602 * including inheritable ACEs.
1605 zfs_acl_ids_create(znode_t *dzp, int flag, vattr_t *vap, cred_t *cr,
1606 vsecattr_t *vsecp, zfs_acl_ids_t *acl_ids)
1609 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1612 boolean_t need_chmod = B_TRUE;
1613 boolean_t inherited = B_FALSE;
1615 ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
1616 bzero(acl_ids, sizeof (zfs_acl_ids_t));
1617 acl_ids->z_mode = MAKEIMODE(vap->va_type, vap->va_mode);
1620 if ((error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, cr,
1621 &acl_ids->z_fuidp, &acl_ids->z_aclp)) != 0)
1624 * Determine uid and gid.
1626 if ((flag & IS_ROOT_NODE) || zfsvfs->z_replay ||
1627 ((flag & IS_XATTR) && (vap->va_type == VDIR))) {
1628 acl_ids->z_fuid = zfs_fuid_create(zfsvfs,
1629 (uint64_t)vap->va_uid, cr,
1630 ZFS_OWNER, &acl_ids->z_fuidp);
1631 acl_ids->z_fgid = zfs_fuid_create(zfsvfs,
1632 (uint64_t)vap->va_gid, cr,
1633 ZFS_GROUP, &acl_ids->z_fuidp);
1636 acl_ids->z_fuid = zfs_fuid_create_cred(zfsvfs, ZFS_OWNER,
1637 cr, &acl_ids->z_fuidp);
1638 acl_ids->z_fgid = 0;
1639 if (vap->va_mask & AT_GID) {
1640 acl_ids->z_fgid = zfs_fuid_create(zfsvfs,
1641 (uint64_t)vap->va_gid,
1642 cr, ZFS_GROUP, &acl_ids->z_fuidp);
1644 if (acl_ids->z_fgid != dzp->z_gid &&
1645 !groupmember(vap->va_gid, cr) &&
1646 secpolicy_vnode_create_gid(cr) != 0)
1647 acl_ids->z_fgid = 0;
1649 if (acl_ids->z_fgid == 0) {
1650 if (dzp->z_mode & S_ISGID) {
1654 acl_ids->z_fgid = dzp->z_gid;
1655 gid = zfs_fuid_map_id(zfsvfs, acl_ids->z_fgid,
1658 if (zfsvfs->z_use_fuids &&
1659 IS_EPHEMERAL(acl_ids->z_fgid)) {
1660 domain = zfs_fuid_idx_domain(
1661 &zfsvfs->z_fuid_idx,
1662 FUID_INDEX(acl_ids->z_fgid));
1663 rid = FUID_RID(acl_ids->z_fgid);
1664 zfs_fuid_node_add(&acl_ids->z_fuidp,
1666 FUID_INDEX(acl_ids->z_fgid),
1667 acl_ids->z_fgid, ZFS_GROUP);
1670 acl_ids->z_fgid = zfs_fuid_create_cred(zfsvfs,
1671 ZFS_GROUP, cr, &acl_ids->z_fuidp);
1672 #ifdef __FreeBSD_kernel__
1673 gid = acl_ids->z_fgid = dzp->z_gid;
1682 * If we're creating a directory, and the parent directory has the
1683 * set-GID bit set, set in on the new directory.
1684 * Otherwise, if the user is neither privileged nor a member of the
1685 * file's new group, clear the file's set-GID bit.
1688 if (!(flag & IS_ROOT_NODE) && (dzp->z_mode & S_ISGID) &&
1689 (vap->va_type == VDIR)) {
1690 acl_ids->z_mode |= S_ISGID;
1692 if ((acl_ids->z_mode & S_ISGID) &&
1693 secpolicy_vnode_setids_setgids(ZTOV(dzp), cr, gid) != 0)
1694 acl_ids->z_mode &= ~S_ISGID;
1697 if (acl_ids->z_aclp == NULL) {
1698 mutex_enter(&dzp->z_acl_lock);
1699 if (!(flag & IS_ROOT_NODE) &&
1700 (dzp->z_pflags & ZFS_INHERIT_ACE) &&
1701 !(dzp->z_pflags & ZFS_XATTR)) {
1702 VERIFY(0 == zfs_acl_node_read(dzp, &paclp, B_FALSE));
1703 acl_ids->z_aclp = zfs_acl_inherit(zfsvfs,
1704 vap->va_type, paclp, acl_ids->z_mode, &need_chmod);
1708 zfs_acl_alloc(zfs_acl_version_zp(dzp));
1709 acl_ids->z_aclp->z_hints |= ZFS_ACL_TRIVIAL;
1711 mutex_exit(&dzp->z_acl_lock);
1713 acl_ids->z_aclp->z_hints |= (vap->va_type == VDIR) ?
1714 ZFS_ACL_AUTO_INHERIT : 0;
1715 zfs_acl_chmod(vap->va_type, acl_ids->z_mode,
1716 (zfsvfs->z_acl_inherit == ZFS_ACL_RESTRICTED),
1721 if (inherited || vsecp) {
1722 acl_ids->z_mode = zfs_mode_compute(acl_ids->z_mode,
1723 acl_ids->z_aclp, &acl_ids->z_aclp->z_hints,
1724 acl_ids->z_fuid, acl_ids->z_fgid);
1725 if (ace_trivial_common(acl_ids->z_aclp, 0, zfs_ace_walk) == 0)
1726 acl_ids->z_aclp->z_hints |= ZFS_ACL_TRIVIAL;
1733 * Free ACL and fuid_infop, but not the acl_ids structure
1736 zfs_acl_ids_free(zfs_acl_ids_t *acl_ids)
1738 if (acl_ids->z_aclp)
1739 zfs_acl_free(acl_ids->z_aclp);
1740 if (acl_ids->z_fuidp)
1741 zfs_fuid_info_free(acl_ids->z_fuidp);
1742 acl_ids->z_aclp = NULL;
1743 acl_ids->z_fuidp = NULL;
1747 zfs_acl_ids_overquota(zfsvfs_t *zfsvfs, zfs_acl_ids_t *acl_ids)
1749 return (zfs_fuid_overquota(zfsvfs, B_FALSE, acl_ids->z_fuid) ||
1750 zfs_fuid_overquota(zfsvfs, B_TRUE, acl_ids->z_fgid));
1754 * Retrieve a file's ACL
1757 zfs_getacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
1765 mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT |
1766 VSA_ACE_ACLFLAGS | VSA_ACE_ALLTYPES);
1769 return (SET_ERROR(ENOSYS));
1771 if (error = zfs_zaccess(zp, ACE_READ_ACL, 0, skipaclchk, cr))
1774 mutex_enter(&zp->z_acl_lock);
1776 ASSERT_VOP_LOCKED(ZTOV(zp), __func__);
1777 error = zfs_acl_node_read(zp, &aclp, B_FALSE);
1779 mutex_exit(&zp->z_acl_lock);
1784 * Scan ACL to determine number of ACEs
1786 if ((zp->z_pflags & ZFS_ACL_OBJ_ACE) && !(mask & VSA_ACE_ALLTYPES)) {
1789 uint32_t access_mask;
1790 uint16_t type, iflags;
1792 while (zacep = zfs_acl_next_ace(aclp, zacep,
1793 &who, &access_mask, &iflags, &type)) {
1795 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1796 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1797 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1798 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1805 vsecp->vsa_aclcnt = count;
1807 count = (int)aclp->z_acl_count;
1809 if (mask & VSA_ACECNT) {
1810 vsecp->vsa_aclcnt = count;
1813 if (mask & VSA_ACE) {
1816 aclsz = count * sizeof (ace_t) +
1817 sizeof (ace_object_t) * largeace;
1819 vsecp->vsa_aclentp = kmem_alloc(aclsz, KM_SLEEP);
1820 vsecp->vsa_aclentsz = aclsz;
1822 if (aclp->z_version == ZFS_ACL_VERSION_FUID)
1823 zfs_copy_fuid_2_ace(zp->z_zfsvfs, aclp, cr,
1824 vsecp->vsa_aclentp, !(mask & VSA_ACE_ALLTYPES));
1826 zfs_acl_node_t *aclnode;
1827 void *start = vsecp->vsa_aclentp;
1829 for (aclnode = list_head(&aclp->z_acl); aclnode;
1830 aclnode = list_next(&aclp->z_acl, aclnode)) {
1831 bcopy(aclnode->z_acldata, start,
1833 start = (caddr_t)start + aclnode->z_size;
1835 ASSERT((caddr_t)start - (caddr_t)vsecp->vsa_aclentp ==
1839 if (mask & VSA_ACE_ACLFLAGS) {
1840 vsecp->vsa_aclflags = 0;
1841 if (zp->z_pflags & ZFS_ACL_DEFAULTED)
1842 vsecp->vsa_aclflags |= ACL_DEFAULTED;
1843 if (zp->z_pflags & ZFS_ACL_PROTECTED)
1844 vsecp->vsa_aclflags |= ACL_PROTECTED;
1845 if (zp->z_pflags & ZFS_ACL_AUTO_INHERIT)
1846 vsecp->vsa_aclflags |= ACL_AUTO_INHERIT;
1849 mutex_exit(&zp->z_acl_lock);
1855 zfs_vsec_2_aclp(zfsvfs_t *zfsvfs, vtype_t obj_type,
1856 vsecattr_t *vsecp, cred_t *cr, zfs_fuid_info_t **fuidp, zfs_acl_t **zaclp)
1859 zfs_acl_node_t *aclnode;
1860 int aclcnt = vsecp->vsa_aclcnt;
1863 if (vsecp->vsa_aclcnt > MAX_ACL_ENTRIES || vsecp->vsa_aclcnt <= 0)
1864 return (SET_ERROR(EINVAL));
1866 aclp = zfs_acl_alloc(zfs_acl_version(zfsvfs->z_version));
1869 aclnode = zfs_acl_node_alloc(aclcnt * sizeof (zfs_object_ace_t));
1870 if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1871 if ((error = zfs_copy_ace_2_oldace(obj_type, aclp,
1872 (ace_t *)vsecp->vsa_aclentp, aclnode->z_acldata,
1873 aclcnt, &aclnode->z_size)) != 0) {
1875 zfs_acl_node_free(aclnode);
1879 if ((error = zfs_copy_ace_2_fuid(zfsvfs, obj_type, aclp,
1880 vsecp->vsa_aclentp, aclnode->z_acldata, aclcnt,
1881 &aclnode->z_size, fuidp, cr)) != 0) {
1883 zfs_acl_node_free(aclnode);
1887 aclp->z_acl_bytes = aclnode->z_size;
1888 aclnode->z_ace_count = aclcnt;
1889 aclp->z_acl_count = aclcnt;
1890 list_insert_head(&aclp->z_acl, aclnode);
1893 * If flags are being set then add them to z_hints
1895 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) {
1896 if (vsecp->vsa_aclflags & ACL_PROTECTED)
1897 aclp->z_hints |= ZFS_ACL_PROTECTED;
1898 if (vsecp->vsa_aclflags & ACL_DEFAULTED)
1899 aclp->z_hints |= ZFS_ACL_DEFAULTED;
1900 if (vsecp->vsa_aclflags & ACL_AUTO_INHERIT)
1901 aclp->z_hints |= ZFS_ACL_AUTO_INHERIT;
1913 zfs_setacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
1915 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1916 zilog_t *zilog = zfsvfs->z_log;
1917 ulong_t mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT);
1921 zfs_fuid_info_t *fuidp = NULL;
1922 boolean_t fuid_dirtied;
1925 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
1927 return (SET_ERROR(ENOSYS));
1929 if (zp->z_pflags & ZFS_IMMUTABLE)
1930 return (SET_ERROR(EPERM));
1932 if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr))
1935 error = zfs_vsec_2_aclp(zfsvfs, ZTOV(zp)->v_type, vsecp, cr, &fuidp,
1941 * If ACL wide flags aren't being set then preserve any
1944 if (!(vsecp->vsa_mask & VSA_ACE_ACLFLAGS)) {
1946 (zp->z_pflags & V4_ACL_WIDE_FLAGS);
1949 mutex_enter(&zp->z_acl_lock);
1951 tx = dmu_tx_create(zfsvfs->z_os);
1953 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1955 fuid_dirtied = zfsvfs->z_fuid_dirty;
1957 zfs_fuid_txhold(zfsvfs, tx);
1960 * If old version and ACL won't fit in bonus and we aren't
1961 * upgrading then take out necessary DMU holds
1964 if ((acl_obj = zfs_external_acl(zp)) != 0) {
1965 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
1966 zfs_znode_acl_version(zp) <= ZFS_ACL_VERSION_INITIAL) {
1967 dmu_tx_hold_free(tx, acl_obj, 0,
1969 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1972 dmu_tx_hold_write(tx, acl_obj, 0, aclp->z_acl_bytes);
1974 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1975 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, aclp->z_acl_bytes);
1978 zfs_sa_upgrade_txholds(tx, zp);
1979 error = dmu_tx_assign(tx, TXG_NOWAIT);
1981 mutex_exit(&zp->z_acl_lock);
1983 if (error == ERESTART) {
1993 error = zfs_aclset_common(zp, aclp, cr, tx);
1995 ASSERT(zp->z_acl_cached == NULL);
1996 zp->z_acl_cached = aclp;
1999 zfs_fuid_sync(zfsvfs, tx);
2001 zfs_log_acl(zilog, tx, zp, vsecp, fuidp);
2004 zfs_fuid_info_free(fuidp);
2006 mutex_exit(&zp->z_acl_lock);
2012 * Check accesses of interest (AoI) against attributes of the dataset
2013 * such as read-only. Returns zero if no AoI conflict with dataset
2014 * attributes, otherwise an appropriate errno is returned.
2017 zfs_zaccess_dataset_check(znode_t *zp, uint32_t v4_mode)
2019 if ((v4_mode & WRITE_MASK) &&
2020 (zp->z_zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) &&
2021 (!IS_DEVVP(ZTOV(zp)) ||
2022 (IS_DEVVP(ZTOV(zp)) && (v4_mode & WRITE_MASK_ATTRS)))) {
2023 return (SET_ERROR(EROFS));
2027 * Only check for READONLY on non-directories.
2029 if ((v4_mode & WRITE_MASK_DATA) &&
2030 (((ZTOV(zp)->v_type != VDIR) &&
2031 (zp->z_pflags & (ZFS_READONLY | ZFS_IMMUTABLE))) ||
2032 (ZTOV(zp)->v_type == VDIR &&
2033 (zp->z_pflags & ZFS_IMMUTABLE)))) {
2034 return (SET_ERROR(EPERM));
2038 if ((v4_mode & (ACE_DELETE | ACE_DELETE_CHILD)) &&
2039 (zp->z_pflags & ZFS_NOUNLINK)) {
2040 return (SET_ERROR(EPERM));
2044 * In FreeBSD we allow to modify directory's content is ZFS_NOUNLINK
2045 * (sunlnk) is set. We just don't allow directory removal, which is
2046 * handled in zfs_zaccess_delete().
2048 if ((v4_mode & ACE_DELETE) &&
2049 (zp->z_pflags & ZFS_NOUNLINK)) {
2054 if (((v4_mode & (ACE_READ_DATA|ACE_EXECUTE)) &&
2055 (zp->z_pflags & ZFS_AV_QUARANTINED))) {
2056 return (SET_ERROR(EACCES));
2063 * The primary usage of this function is to loop through all of the
2064 * ACEs in the znode, determining what accesses of interest (AoI) to
2065 * the caller are allowed or denied. The AoI are expressed as bits in
2066 * the working_mode parameter. As each ACE is processed, bits covered
2067 * by that ACE are removed from the working_mode. This removal
2068 * facilitates two things. The first is that when the working mode is
2069 * empty (= 0), we know we've looked at all the AoI. The second is
2070 * that the ACE interpretation rules don't allow a later ACE to undo
2071 * something granted or denied by an earlier ACE. Removing the
2072 * discovered access or denial enforces this rule. At the end of
2073 * processing the ACEs, all AoI that were found to be denied are
2074 * placed into the working_mode, giving the caller a mask of denied
2075 * accesses. Returns:
2076 * 0 if all AoI granted
2077 * EACCESS if the denied mask is non-zero
2078 * other error if abnormal failure (e.g., IO error)
2080 * A secondary usage of the function is to determine if any of the
2081 * AoI are granted. If an ACE grants any access in
2082 * the working_mode, we immediately short circuit out of the function.
2083 * This mode is chosen by setting anyaccess to B_TRUE. The
2084 * working_mode is not a denied access mask upon exit if the function
2085 * is used in this manner.
2088 zfs_zaccess_aces_check(znode_t *zp, uint32_t *working_mode,
2089 boolean_t anyaccess, cred_t *cr)
2091 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2094 uid_t uid = crgetuid(cr);
2096 uint16_t type, iflags;
2097 uint16_t entry_type;
2098 uint32_t access_mask;
2099 uint32_t deny_mask = 0;
2100 zfs_ace_hdr_t *acep = NULL;
2105 zfs_fuid_map_ids(zp, cr, &fowner, &gowner);
2107 mutex_enter(&zp->z_acl_lock);
2109 ASSERT_VOP_LOCKED(ZTOV(zp), __func__);
2110 error = zfs_acl_node_read(zp, &aclp, B_FALSE);
2112 mutex_exit(&zp->z_acl_lock);
2116 ASSERT(zp->z_acl_cached);
2118 while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
2120 uint32_t mask_matched;
2122 if (!zfs_acl_valid_ace_type(type, iflags))
2125 if (ZTOV(zp)->v_type == VDIR && (iflags & ACE_INHERIT_ONLY_ACE))
2128 /* Skip ACE if it does not affect any AoI */
2129 mask_matched = (access_mask & *working_mode);
2133 entry_type = (iflags & ACE_TYPE_FLAGS);
2137 switch (entry_type) {
2145 case ACE_IDENTIFIER_GROUP:
2146 checkit = zfs_groupmember(zfsvfs, who, cr);
2154 if (entry_type == 0) {
2157 newid = zfs_fuid_map_id(zfsvfs, who, cr,
2159 if (newid != IDMAP_WK_CREATOR_OWNER_UID &&
2164 mutex_exit(&zp->z_acl_lock);
2165 return (SET_ERROR(EIO));
2171 DTRACE_PROBE3(zfs__ace__denies,
2173 zfs_ace_hdr_t *, acep,
2174 uint32_t, mask_matched);
2175 deny_mask |= mask_matched;
2177 DTRACE_PROBE3(zfs__ace__allows,
2179 zfs_ace_hdr_t *, acep,
2180 uint32_t, mask_matched);
2182 mutex_exit(&zp->z_acl_lock);
2186 *working_mode &= ~mask_matched;
2190 if (*working_mode == 0)
2194 mutex_exit(&zp->z_acl_lock);
2196 /* Put the found 'denies' back on the working mode */
2198 *working_mode |= deny_mask;
2199 return (SET_ERROR(EACCES));
2200 } else if (*working_mode) {
2208 * Return true if any access whatsoever granted, we don't actually
2209 * care what access is granted.
2212 zfs_has_access(znode_t *zp, cred_t *cr)
2214 uint32_t have = ACE_ALL_PERMS;
2216 if (zfs_zaccess_aces_check(zp, &have, B_TRUE, cr) != 0) {
2219 owner = zfs_fuid_map_id(zp->z_zfsvfs, zp->z_uid, cr, ZFS_OWNER);
2220 return (secpolicy_vnode_any_access(cr, ZTOV(zp), owner) == 0);
2226 zfs_zaccess_common(znode_t *zp, uint32_t v4_mode, uint32_t *working_mode,
2227 boolean_t *check_privs, boolean_t skipaclchk, cred_t *cr)
2229 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2232 *working_mode = v4_mode;
2233 *check_privs = B_TRUE;
2236 * Short circuit empty requests
2238 if (v4_mode == 0 || zfsvfs->z_replay) {
2243 if ((err = zfs_zaccess_dataset_check(zp, v4_mode)) != 0) {
2244 *check_privs = B_FALSE;
2249 * The caller requested that the ACL check be skipped. This
2250 * would only happen if the caller checked VOP_ACCESS() with a
2251 * 32 bit ACE mask and already had the appropriate permissions.
2258 return (zfs_zaccess_aces_check(zp, working_mode, B_FALSE, cr));
2262 zfs_zaccess_append(znode_t *zp, uint32_t *working_mode, boolean_t *check_privs,
2265 if (*working_mode != ACE_WRITE_DATA)
2266 return (SET_ERROR(EACCES));
2268 return (zfs_zaccess_common(zp, ACE_APPEND_DATA, working_mode,
2269 check_privs, B_FALSE, cr));
2273 zfs_fastaccesschk_execute(znode_t *zdp, cred_t *cr)
2275 boolean_t owner = B_FALSE;
2276 boolean_t groupmbr = B_FALSE;
2278 uid_t uid = crgetuid(cr);
2281 if (zdp->z_pflags & ZFS_AV_QUARANTINED)
2282 return (SET_ERROR(EACCES));
2284 is_attr = ((zdp->z_pflags & ZFS_XATTR) &&
2285 (ZTOV(zdp)->v_type == VDIR));
2290 mutex_enter(&zdp->z_acl_lock);
2292 if (zdp->z_pflags & ZFS_NO_EXECS_DENIED) {
2293 mutex_exit(&zdp->z_acl_lock);
2297 if (FUID_INDEX(zdp->z_uid) != 0 || FUID_INDEX(zdp->z_gid) != 0) {
2298 mutex_exit(&zdp->z_acl_lock);
2302 if (uid == zdp->z_uid) {
2304 if (zdp->z_mode & S_IXUSR) {
2305 mutex_exit(&zdp->z_acl_lock);
2308 mutex_exit(&zdp->z_acl_lock);
2312 if (groupmember(zdp->z_gid, cr)) {
2314 if (zdp->z_mode & S_IXGRP) {
2315 mutex_exit(&zdp->z_acl_lock);
2318 mutex_exit(&zdp->z_acl_lock);
2322 if (!owner && !groupmbr) {
2323 if (zdp->z_mode & S_IXOTH) {
2324 mutex_exit(&zdp->z_acl_lock);
2329 mutex_exit(&zdp->z_acl_lock);
2332 DTRACE_PROBE(zfs__fastpath__execute__access__miss);
2333 ZFS_ENTER(zdp->z_zfsvfs);
2334 error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr);
2335 ZFS_EXIT(zdp->z_zfsvfs);
2340 * Determine whether Access should be granted/denied.
2342 * The least priv subsytem is always consulted as a basic privilege
2343 * can define any form of access.
2346 zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr)
2348 uint32_t working_mode;
2351 boolean_t check_privs;
2353 znode_t *check_zp = zp;
2357 is_attr = ((zp->z_pflags & ZFS_XATTR) && (ZTOV(zp)->v_type == VDIR));
2359 #ifdef __FreeBSD_kernel__
2361 * In FreeBSD, we don't care about permissions of individual ADS.
2362 * Note that not checking them is not just an optimization - without
2363 * this shortcut, EA operations may bogusly fail with EACCES.
2365 if (zp->z_pflags & ZFS_XATTR)
2369 * If attribute then validate against base file
2374 if ((error = sa_lookup(zp->z_sa_hdl,
2375 SA_ZPL_PARENT(zp->z_zfsvfs), &parent,
2376 sizeof (parent))) != 0)
2379 if ((error = zfs_zget(zp->z_zfsvfs,
2380 parent, &xzp)) != 0) {
2387 * fixup mode to map to xattr perms
2390 if (mode & (ACE_WRITE_DATA|ACE_APPEND_DATA)) {
2391 mode &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
2392 mode |= ACE_WRITE_NAMED_ATTRS;
2395 if (mode & (ACE_READ_DATA|ACE_EXECUTE)) {
2396 mode &= ~(ACE_READ_DATA|ACE_EXECUTE);
2397 mode |= ACE_READ_NAMED_ATTRS;
2402 owner = zfs_fuid_map_id(zp->z_zfsvfs, zp->z_uid, cr, ZFS_OWNER);
2404 * Map the bits required to the standard vnode flags VREAD|VWRITE|VEXEC
2405 * in needed_bits. Map the bits mapped by working_mode (currently
2406 * missing) in missing_bits.
2407 * Call secpolicy_vnode_access2() with (needed_bits & ~checkmode),
2412 working_mode = mode;
2413 if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES)) &&
2414 owner == crgetuid(cr))
2415 working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES);
2417 if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS|
2418 ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_SYNCHRONIZE))
2419 needed_bits |= VREAD;
2420 if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS|
2421 ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES|ACE_SYNCHRONIZE))
2422 needed_bits |= VWRITE;
2423 if (working_mode & ACE_EXECUTE)
2424 needed_bits |= VEXEC;
2426 if ((error = zfs_zaccess_common(check_zp, mode, &working_mode,
2427 &check_privs, skipaclchk, cr)) == 0) {
2430 return (secpolicy_vnode_access2(cr, ZTOV(zp), owner,
2431 needed_bits, needed_bits));
2434 if (error && !check_privs) {
2440 if (error && (flags & V_APPEND)) {
2441 error = zfs_zaccess_append(zp, &working_mode, &check_privs, cr);
2444 if (error && check_privs) {
2445 mode_t checkmode = 0;
2448 * First check for implicit owner permission on
2449 * read_acl/read_attributes
2453 ASSERT(working_mode != 0);
2455 if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) &&
2456 owner == crgetuid(cr)))
2457 working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES);
2459 if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS|
2460 ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_SYNCHRONIZE))
2462 if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS|
2463 ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES|ACE_SYNCHRONIZE))
2464 checkmode |= VWRITE;
2465 if (working_mode & ACE_EXECUTE)
2468 error = secpolicy_vnode_access2(cr, ZTOV(check_zp), owner,
2469 needed_bits & ~checkmode, needed_bits);
2471 if (error == 0 && (working_mode & ACE_WRITE_OWNER))
2472 error = secpolicy_vnode_chown(ZTOV(check_zp), cr, owner);
2473 if (error == 0 && (working_mode & ACE_WRITE_ACL))
2474 error = secpolicy_vnode_setdac(ZTOV(check_zp), cr, owner);
2476 if (error == 0 && (working_mode &
2477 (ACE_DELETE|ACE_DELETE_CHILD)))
2478 error = secpolicy_vnode_remove(ZTOV(check_zp), cr);
2480 if (error == 0 && (working_mode & ACE_SYNCHRONIZE)) {
2481 error = secpolicy_vnode_chown(ZTOV(check_zp), cr, owner);
2485 * See if any bits other than those already checked
2486 * for are still present. If so then return EACCES
2488 if (working_mode & ~(ZFS_CHECKED_MASKS)) {
2489 error = SET_ERROR(EACCES);
2492 } else if (error == 0) {
2493 error = secpolicy_vnode_access2(cr, ZTOV(zp), owner,
2494 needed_bits, needed_bits);
2505 * Translate traditional unix VREAD/VWRITE/VEXEC mode into
2506 * native ACL format and call zfs_zaccess()
2509 zfs_zaccess_rwx(znode_t *zp, mode_t mode, int flags, cred_t *cr)
2511 return (zfs_zaccess(zp, zfs_unix_to_v4(mode >> 6), flags, B_FALSE, cr));
2515 * Access function for secpolicy_vnode_setattr
2518 zfs_zaccess_unix(znode_t *zp, mode_t mode, cred_t *cr)
2520 int v4_mode = zfs_unix_to_v4(mode >> 6);
2522 return (zfs_zaccess(zp, v4_mode, 0, B_FALSE, cr));
2526 zfs_delete_final_check(znode_t *zp, znode_t *dzp,
2527 mode_t available_perms, cred_t *cr)
2532 downer = zfs_fuid_map_id(dzp->z_zfsvfs, dzp->z_uid, cr, ZFS_OWNER);
2534 error = secpolicy_vnode_access2(cr, ZTOV(dzp),
2535 downer, available_perms, VWRITE|VEXEC);
2538 error = zfs_sticky_remove_access(dzp, zp, cr);
2544 * Determine whether Access should be granted/deny, without
2545 * consulting least priv subsystem.
2547 * The following chart is the recommended NFSv4 enforcement for
2548 * ability to delete an object.
2550 * -------------------------------------------------------
2551 * | Parent Dir | Target Object Permissions |
2553 * -------------------------------------------------------
2554 * | | ACL Allows | ACL Denies| Delete |
2555 * | | Delete | Delete | unspecified|
2556 * -------------------------------------------------------
2557 * | ACL Allows | Permit | Permit | Permit |
2558 * | DELETE_CHILD | |
2559 * -------------------------------------------------------
2560 * | ACL Denies | Permit | Deny | Deny |
2561 * | DELETE_CHILD | | | |
2562 * -------------------------------------------------------
2563 * | ACL specifies | | | |
2564 * | only allow | Permit | Permit | Permit |
2565 * | write and | | | |
2567 * -------------------------------------------------------
2568 * | ACL denies | | | |
2569 * | write and | Permit | Deny | Deny |
2571 * -------------------------------------------------------
2574 * No search privilege, can't even look up file?
2578 zfs_zaccess_delete(znode_t *dzp, znode_t *zp, cred_t *cr)
2580 uint32_t dzp_working_mode = 0;
2581 uint32_t zp_working_mode = 0;
2582 int dzp_error, zp_error;
2583 mode_t available_perms;
2584 boolean_t dzpcheck_privs = B_TRUE;
2585 boolean_t zpcheck_privs = B_TRUE;
2588 * We want specific DELETE permissions to
2589 * take precedence over WRITE/EXECUTE. We don't
2590 * want an ACL such as this to mess us up.
2591 * user:joe:write_data:deny,user:joe:delete:allow
2593 * However, deny permissions may ultimately be overridden
2594 * by secpolicy_vnode_access().
2596 * We will ask for all of the necessary permissions and then
2597 * look at the working modes from the directory and target object
2598 * to determine what was found.
2601 if (zp->z_pflags & (ZFS_IMMUTABLE | ZFS_NOUNLINK))
2602 return (SET_ERROR(EPERM));
2606 * If the directory permissions allow the delete, we are done.
2608 if ((dzp_error = zfs_zaccess_common(dzp, ACE_DELETE_CHILD,
2609 &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr)) == 0)
2613 * If target object has delete permission then we are done
2615 if ((zp_error = zfs_zaccess_common(zp, ACE_DELETE, &zp_working_mode,
2616 &zpcheck_privs, B_FALSE, cr)) == 0)
2619 ASSERT(dzp_error && zp_error);
2621 if (!dzpcheck_privs)
2629 * If directory returns EACCES then delete_child was denied
2630 * due to deny delete_child. In this case send the request through
2631 * secpolicy_vnode_remove(). We don't use zfs_delete_final_check()
2632 * since that *could* allow the delete based on write/execute permission
2633 * and we want delete permissions to override write/execute.
2636 if (dzp_error == EACCES)
2637 return (secpolicy_vnode_remove(ZTOV(dzp), cr)); /* XXXPJD: s/dzp/zp/ ? */
2641 * only need to see if we have write/execute on directory.
2644 dzp_error = zfs_zaccess_common(dzp, ACE_EXECUTE|ACE_WRITE_DATA,
2645 &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr);
2647 if (dzp_error != 0 && !dzpcheck_privs)
2654 available_perms = (dzp_working_mode & ACE_WRITE_DATA) ? 0 : VWRITE;
2655 available_perms |= (dzp_working_mode & ACE_EXECUTE) ? 0 : VEXEC;
2657 return (zfs_delete_final_check(zp, dzp, available_perms, cr));
2662 zfs_zaccess_rename(znode_t *sdzp, znode_t *szp, znode_t *tdzp,
2663 znode_t *tzp, cred_t *cr)
2668 if (szp->z_pflags & ZFS_AV_QUARANTINED)
2669 return (SET_ERROR(EACCES));
2671 add_perm = (ZTOV(szp)->v_type == VDIR) ?
2672 ACE_ADD_SUBDIRECTORY : ACE_ADD_FILE;
2675 * Rename permissions are combination of delete permission +
2676 * add file/subdir permission.
2678 * BSD operating systems also require write permission
2679 * on the directory being moved from one parent directory
2682 if (ZTOV(szp)->v_type == VDIR && ZTOV(sdzp) != ZTOV(tdzp)) {
2683 if (error = zfs_zaccess(szp, ACE_WRITE_DATA, 0, B_FALSE, cr))
2688 * first make sure we do the delete portion.
2690 * If that succeeds then check for add_file/add_subdir permissions
2693 if (error = zfs_zaccess_delete(sdzp, szp, cr))
2697 * If we have a tzp, see if we can delete it?
2700 if (error = zfs_zaccess_delete(tdzp, tzp, cr))
2705 * Now check for add permissions
2707 error = zfs_zaccess(tdzp, add_perm, 0, B_FALSE, cr);