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.
25 #include <sys/types.h>
26 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/sysmacros.h>
30 #include <sys/resource.h>
32 #include <sys/vnode.h>
36 #include <sys/cmn_err.h>
37 #include <sys/errno.h>
38 #include <sys/unistd.h>
40 #include <sys/fs/zfs.h>
41 #include <sys/policy.h>
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_fuid.h>
44 #include <sys/zfs_acl.h>
45 #include <sys/zfs_dir.h>
46 #include <sys/zfs_vfsops.h>
48 #include <sys/dnode.h>
51 #include <acl/acl_common.h>
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
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)
66 #define ZFS_CHECKED_MASKS (ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_DATA| \
67 ACE_READ_NAMED_ATTRS|ACE_WRITE_DATA|ACE_WRITE_ATTRIBUTES| \
68 ACE_WRITE_NAMED_ATTRS|ACE_APPEND_DATA|ACE_EXECUTE|ACE_WRITE_OWNER| \
69 ACE_WRITE_ACL|ACE_DELETE|ACE_DELETE_CHILD|ACE_SYNCHRONIZE)
71 #define WRITE_MASK_DATA (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS)
72 #define WRITE_MASK_ATTRS (ACE_WRITE_ACL|ACE_WRITE_OWNER|ACE_WRITE_ATTRIBUTES| \
73 ACE_DELETE|ACE_DELETE_CHILD)
74 #define WRITE_MASK (WRITE_MASK_DATA|WRITE_MASK_ATTRS)
76 #define OGE_CLEAR (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
77 ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
79 #define OKAY_MASK_BITS (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
80 ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
82 #define ALL_INHERIT (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE | \
83 ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE|ACE_INHERITED_ACE)
85 #define RESTRICTED_CLEAR (ACE_WRITE_ACL|ACE_WRITE_OWNER)
87 #define V4_ACL_WIDE_FLAGS (ZFS_ACL_AUTO_INHERIT|ZFS_ACL_DEFAULTED|\
90 #define ZFS_ACL_WIDE_FLAGS (V4_ACL_WIDE_FLAGS|ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|\
93 #define ALL_MODE_EXECS (S_IXUSR | S_IXGRP | S_IXOTH)
96 zfs_ace_v0_get_type(void *acep)
98 return (((zfs_oldace_t *)acep)->z_type);
102 zfs_ace_v0_get_flags(void *acep)
104 return (((zfs_oldace_t *)acep)->z_flags);
108 zfs_ace_v0_get_mask(void *acep)
110 return (((zfs_oldace_t *)acep)->z_access_mask);
114 zfs_ace_v0_get_who(void *acep)
116 return (((zfs_oldace_t *)acep)->z_fuid);
120 zfs_ace_v0_set_type(void *acep, uint16_t type)
122 ((zfs_oldace_t *)acep)->z_type = type;
126 zfs_ace_v0_set_flags(void *acep, uint16_t flags)
128 ((zfs_oldace_t *)acep)->z_flags = flags;
132 zfs_ace_v0_set_mask(void *acep, uint32_t mask)
134 ((zfs_oldace_t *)acep)->z_access_mask = mask;
138 zfs_ace_v0_set_who(void *acep, uint64_t who)
140 ((zfs_oldace_t *)acep)->z_fuid = who;
145 zfs_ace_v0_size(void *acep)
147 return (sizeof (zfs_oldace_t));
151 zfs_ace_v0_abstract_size(void)
153 return (sizeof (zfs_oldace_t));
157 zfs_ace_v0_mask_off(void)
159 return (offsetof(zfs_oldace_t, z_access_mask));
164 zfs_ace_v0_data(void *acep, void **datap)
170 static acl_ops_t zfs_acl_v0_ops = {
173 zfs_ace_v0_get_flags,
174 zfs_ace_v0_set_flags,
180 zfs_ace_v0_abstract_size,
186 zfs_ace_fuid_get_type(void *acep)
188 return (((zfs_ace_hdr_t *)acep)->z_type);
192 zfs_ace_fuid_get_flags(void *acep)
194 return (((zfs_ace_hdr_t *)acep)->z_flags);
198 zfs_ace_fuid_get_mask(void *acep)
200 return (((zfs_ace_hdr_t *)acep)->z_access_mask);
204 zfs_ace_fuid_get_who(void *args)
207 zfs_ace_t *acep = args;
209 entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
211 if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
212 entry_type == ACE_EVERYONE)
214 return (((zfs_ace_t *)acep)->z_fuid);
218 zfs_ace_fuid_set_type(void *acep, uint16_t type)
220 ((zfs_ace_hdr_t *)acep)->z_type = type;
224 zfs_ace_fuid_set_flags(void *acep, uint16_t flags)
226 ((zfs_ace_hdr_t *)acep)->z_flags = flags;
230 zfs_ace_fuid_set_mask(void *acep, uint32_t mask)
232 ((zfs_ace_hdr_t *)acep)->z_access_mask = mask;
236 zfs_ace_fuid_set_who(void *arg, uint64_t who)
238 zfs_ace_t *acep = arg;
240 uint16_t entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
242 if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
243 entry_type == ACE_EVERYONE)
249 zfs_ace_fuid_size(void *acep)
251 zfs_ace_hdr_t *zacep = acep;
254 switch (zacep->z_type) {
255 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
256 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
257 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
258 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
259 return (sizeof (zfs_object_ace_t));
263 (((zfs_ace_hdr_t *)acep)->z_flags & ACE_TYPE_FLAGS);
264 if (entry_type == ACE_OWNER ||
265 entry_type == OWNING_GROUP ||
266 entry_type == ACE_EVERYONE)
267 return (sizeof (zfs_ace_hdr_t));
270 return (sizeof (zfs_ace_t));
275 zfs_ace_fuid_abstract_size(void)
277 return (sizeof (zfs_ace_hdr_t));
281 zfs_ace_fuid_mask_off(void)
283 return (offsetof(zfs_ace_hdr_t, z_access_mask));
287 zfs_ace_fuid_data(void *acep, void **datap)
289 zfs_ace_t *zacep = acep;
290 zfs_object_ace_t *zobjp;
292 switch (zacep->z_hdr.z_type) {
293 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
294 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
295 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
296 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
298 *datap = (caddr_t)zobjp + sizeof (zfs_ace_t);
299 return (sizeof (zfs_object_ace_t) - sizeof (zfs_ace_t));
306 static acl_ops_t zfs_acl_fuid_ops = {
307 zfs_ace_fuid_get_mask,
308 zfs_ace_fuid_set_mask,
309 zfs_ace_fuid_get_flags,
310 zfs_ace_fuid_set_flags,
311 zfs_ace_fuid_get_type,
312 zfs_ace_fuid_set_type,
313 zfs_ace_fuid_get_who,
314 zfs_ace_fuid_set_who,
316 zfs_ace_fuid_abstract_size,
317 zfs_ace_fuid_mask_off,
322 * The following three functions are provided for compatibility with
323 * older ZPL version in order to determine if the file use to have
324 * an external ACL and what version of ACL previously existed on the
325 * file. Would really be nice to not need this, sigh.
328 zfs_external_acl(znode_t *zp)
330 zfs_acl_phys_t acl_phys;
337 * Need to deal with a potential
338 * race where zfs_sa_upgrade could cause
339 * z_isa_sa to change.
341 * If the lookup fails then the state of z_is_sa should have
345 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zp->z_zfsvfs),
346 &acl_phys, sizeof (acl_phys))) == 0)
347 return (acl_phys.z_acl_extern_obj);
350 * after upgrade the SA_ZPL_ZNODE_ACL should have been
353 VERIFY(zp->z_is_sa && error == ENOENT);
359 * Determine size of ACL in bytes
361 * This is more complicated than it should be since we have to deal
362 * with old external ACLs.
365 zfs_acl_znode_info(znode_t *zp, int *aclsize, int *aclcount,
366 zfs_acl_phys_t *aclphys)
368 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
373 ASSERT(MUTEX_HELD(&zp->z_acl_lock));
375 if ((error = sa_size(zp->z_sa_hdl, SA_ZPL_DACL_ACES(zfsvfs),
379 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DACL_COUNT(zfsvfs),
380 &acl_count, sizeof (acl_count))) != 0)
382 *aclcount = acl_count;
384 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zfsvfs),
385 aclphys, sizeof (*aclphys))) != 0)
388 if (aclphys->z_acl_version == ZFS_ACL_VERSION_INITIAL) {
389 *aclsize = ZFS_ACL_SIZE(aclphys->z_acl_size);
390 *aclcount = aclphys->z_acl_size;
392 *aclsize = aclphys->z_acl_size;
393 *aclcount = aclphys->z_acl_count;
400 zfs_znode_acl_version(znode_t *zp)
402 zfs_acl_phys_t acl_phys;
405 return (ZFS_ACL_VERSION_FUID);
410 * Need to deal with a potential
411 * race where zfs_sa_upgrade could cause
412 * z_isa_sa to change.
414 * If the lookup fails then the state of z_is_sa should have
417 if ((error = sa_lookup(zp->z_sa_hdl,
418 SA_ZPL_ZNODE_ACL(zp->z_zfsvfs),
419 &acl_phys, sizeof (acl_phys))) == 0)
420 return (acl_phys.z_acl_version);
423 * After upgrade SA_ZPL_ZNODE_ACL should have
426 VERIFY(zp->z_is_sa && error == ENOENT);
427 return (ZFS_ACL_VERSION_FUID);
433 zfs_acl_version(int version)
435 if (version < ZPL_VERSION_FUID)
436 return (ZFS_ACL_VERSION_INITIAL);
438 return (ZFS_ACL_VERSION_FUID);
442 zfs_acl_version_zp(znode_t *zp)
444 return (zfs_acl_version(zp->z_zfsvfs->z_version));
448 zfs_acl_alloc(int vers)
452 aclp = kmem_zalloc(sizeof (zfs_acl_t), KM_SLEEP);
453 list_create(&aclp->z_acl, sizeof (zfs_acl_node_t),
454 offsetof(zfs_acl_node_t, z_next));
455 aclp->z_version = vers;
456 if (vers == ZFS_ACL_VERSION_FUID)
457 aclp->z_ops = zfs_acl_fuid_ops;
459 aclp->z_ops = zfs_acl_v0_ops;
464 zfs_acl_node_alloc(size_t bytes)
466 zfs_acl_node_t *aclnode;
468 aclnode = kmem_zalloc(sizeof (zfs_acl_node_t), KM_SLEEP);
470 aclnode->z_acldata = kmem_alloc(bytes, KM_SLEEP);
471 aclnode->z_allocdata = aclnode->z_acldata;
472 aclnode->z_allocsize = bytes;
473 aclnode->z_size = bytes;
480 zfs_acl_node_free(zfs_acl_node_t *aclnode)
482 if (aclnode->z_allocsize)
483 kmem_free(aclnode->z_allocdata, aclnode->z_allocsize);
484 kmem_free(aclnode, sizeof (zfs_acl_node_t));
488 zfs_acl_release_nodes(zfs_acl_t *aclp)
490 zfs_acl_node_t *aclnode;
492 while (aclnode = list_head(&aclp->z_acl)) {
493 list_remove(&aclp->z_acl, aclnode);
494 zfs_acl_node_free(aclnode);
496 aclp->z_acl_count = 0;
497 aclp->z_acl_bytes = 0;
501 zfs_acl_free(zfs_acl_t *aclp)
503 zfs_acl_release_nodes(aclp);
504 list_destroy(&aclp->z_acl);
505 kmem_free(aclp, sizeof (zfs_acl_t));
509 zfs_acl_valid_ace_type(uint_t type, uint_t flags)
516 case ACE_SYSTEM_AUDIT_ACE_TYPE:
517 case ACE_SYSTEM_ALARM_ACE_TYPE:
518 entry_type = flags & ACE_TYPE_FLAGS;
519 return (entry_type == ACE_OWNER ||
520 entry_type == OWNING_GROUP ||
521 entry_type == ACE_EVERYONE || entry_type == 0 ||
522 entry_type == ACE_IDENTIFIER_GROUP);
524 if (type >= MIN_ACE_TYPE && type <= MAX_ACE_TYPE)
531 zfs_ace_valid(vtype_t obj_type, zfs_acl_t *aclp, uint16_t type, uint16_t iflags)
534 * first check type of entry
537 if (!zfs_acl_valid_ace_type(type, iflags))
541 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
542 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
543 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
544 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
545 if (aclp->z_version < ZFS_ACL_VERSION_FUID)
547 aclp->z_hints |= ZFS_ACL_OBJ_ACE;
551 * next check inheritance level flags
554 if (obj_type == VDIR &&
555 (iflags & (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE)))
556 aclp->z_hints |= ZFS_INHERIT_ACE;
558 if (iflags & (ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE)) {
559 if ((iflags & (ACE_FILE_INHERIT_ACE|
560 ACE_DIRECTORY_INHERIT_ACE)) == 0) {
569 zfs_acl_next_ace(zfs_acl_t *aclp, void *start, uint64_t *who,
570 uint32_t *access_mask, uint16_t *iflags, uint16_t *type)
572 zfs_acl_node_t *aclnode;
577 aclnode = list_head(&aclp->z_acl);
581 aclp->z_next_ace = aclnode->z_acldata;
582 aclp->z_curr_node = aclnode;
583 aclnode->z_ace_idx = 0;
586 aclnode = aclp->z_curr_node;
591 if (aclnode->z_ace_idx >= aclnode->z_ace_count) {
592 aclnode = list_next(&aclp->z_acl, aclnode);
596 aclp->z_curr_node = aclnode;
597 aclnode->z_ace_idx = 0;
598 aclp->z_next_ace = aclnode->z_acldata;
602 if (aclnode->z_ace_idx < aclnode->z_ace_count) {
603 void *acep = aclp->z_next_ace;
607 * Make sure we don't overstep our bounds
609 ace_size = aclp->z_ops.ace_size(acep);
611 if (((caddr_t)acep + ace_size) >
612 ((caddr_t)aclnode->z_acldata + aclnode->z_size)) {
616 *iflags = aclp->z_ops.ace_flags_get(acep);
617 *type = aclp->z_ops.ace_type_get(acep);
618 *access_mask = aclp->z_ops.ace_mask_get(acep);
619 *who = aclp->z_ops.ace_who_get(acep);
620 aclp->z_next_ace = (caddr_t)aclp->z_next_ace + ace_size;
621 aclnode->z_ace_idx++;
623 return ((void *)acep);
630 zfs_ace_walk(void *datap, uint64_t cookie, int aclcnt,
631 uint16_t *flags, uint16_t *type, uint32_t *mask)
633 zfs_acl_t *aclp = datap;
634 zfs_ace_hdr_t *acep = (zfs_ace_hdr_t *)(uintptr_t)cookie;
637 acep = zfs_acl_next_ace(aclp, acep, &who, mask,
639 return ((uint64_t)(uintptr_t)acep);
642 static zfs_acl_node_t *
643 zfs_acl_curr_node(zfs_acl_t *aclp)
645 ASSERT(aclp->z_curr_node);
646 return (aclp->z_curr_node);
650 * Copy ACE to internal ZFS format.
651 * While processing the ACL each ACE will be validated for correctness.
652 * ACE FUIDs will be created later.
655 zfs_copy_ace_2_fuid(zfsvfs_t *zfsvfs, vtype_t obj_type, zfs_acl_t *aclp,
656 void *datap, zfs_ace_t *z_acl, uint64_t aclcnt, size_t *size,
657 zfs_fuid_info_t **fuidp, cred_t *cr)
661 zfs_ace_t *aceptr = z_acl;
663 zfs_object_ace_t *zobjacep;
664 ace_object_t *aceobjp;
666 for (i = 0; i != aclcnt; i++) {
667 aceptr->z_hdr.z_access_mask = acep->a_access_mask;
668 aceptr->z_hdr.z_flags = acep->a_flags;
669 aceptr->z_hdr.z_type = acep->a_type;
670 entry_type = aceptr->z_hdr.z_flags & ACE_TYPE_FLAGS;
671 if (entry_type != ACE_OWNER && entry_type != OWNING_GROUP &&
672 entry_type != ACE_EVERYONE) {
673 aceptr->z_fuid = zfs_fuid_create(zfsvfs, acep->a_who,
674 cr, (entry_type == 0) ?
675 ZFS_ACE_USER : ZFS_ACE_GROUP, fuidp);
679 * Make sure ACE is valid
681 if (zfs_ace_valid(obj_type, aclp, aceptr->z_hdr.z_type,
682 aceptr->z_hdr.z_flags) != B_TRUE)
685 switch (acep->a_type) {
686 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
687 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
688 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
689 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
690 zobjacep = (zfs_object_ace_t *)aceptr;
691 aceobjp = (ace_object_t *)acep;
693 bcopy(aceobjp->a_obj_type, zobjacep->z_object_type,
694 sizeof (aceobjp->a_obj_type));
695 bcopy(aceobjp->a_inherit_obj_type,
696 zobjacep->z_inherit_type,
697 sizeof (aceobjp->a_inherit_obj_type));
698 acep = (ace_t *)((caddr_t)acep + sizeof (ace_object_t));
701 acep = (ace_t *)((caddr_t)acep + sizeof (ace_t));
704 aceptr = (zfs_ace_t *)((caddr_t)aceptr +
705 aclp->z_ops.ace_size(aceptr));
708 *size = (caddr_t)aceptr - (caddr_t)z_acl;
714 * Copy ZFS ACEs to fixed size ace_t layout
717 zfs_copy_fuid_2_ace(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, cred_t *cr,
718 void *datap, int filter)
721 uint32_t access_mask;
722 uint16_t iflags, type;
723 zfs_ace_hdr_t *zacep = NULL;
725 ace_object_t *objacep;
726 zfs_object_ace_t *zobjacep;
730 while (zacep = zfs_acl_next_ace(aclp, zacep,
731 &who, &access_mask, &iflags, &type)) {
734 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
735 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
736 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
737 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
741 zobjacep = (zfs_object_ace_t *)zacep;
742 objacep = (ace_object_t *)acep;
743 bcopy(zobjacep->z_object_type,
745 sizeof (zobjacep->z_object_type));
746 bcopy(zobjacep->z_inherit_type,
747 objacep->a_inherit_obj_type,
748 sizeof (zobjacep->z_inherit_type));
749 ace_size = sizeof (ace_object_t);
752 ace_size = sizeof (ace_t);
756 entry_type = (iflags & ACE_TYPE_FLAGS);
757 if ((entry_type != ACE_OWNER &&
758 entry_type != OWNING_GROUP &&
759 entry_type != ACE_EVERYONE)) {
760 acep->a_who = zfs_fuid_map_id(zfsvfs, who,
761 cr, (entry_type & ACE_IDENTIFIER_GROUP) ?
762 ZFS_ACE_GROUP : ZFS_ACE_USER);
764 acep->a_who = (uid_t)(int64_t)who;
766 acep->a_access_mask = access_mask;
767 acep->a_flags = iflags;
769 acep = (ace_t *)((caddr_t)acep + ace_size);
774 zfs_copy_ace_2_oldace(vtype_t obj_type, zfs_acl_t *aclp, ace_t *acep,
775 zfs_oldace_t *z_acl, int aclcnt, size_t *size)
778 zfs_oldace_t *aceptr = z_acl;
780 for (i = 0; i != aclcnt; i++, aceptr++) {
781 aceptr->z_access_mask = acep[i].a_access_mask;
782 aceptr->z_type = acep[i].a_type;
783 aceptr->z_flags = acep[i].a_flags;
784 aceptr->z_fuid = acep[i].a_who;
786 * Make sure ACE is valid
788 if (zfs_ace_valid(obj_type, aclp, aceptr->z_type,
789 aceptr->z_flags) != B_TRUE)
792 *size = (caddr_t)aceptr - (caddr_t)z_acl;
797 * convert old ACL format to new
800 zfs_acl_xform(znode_t *zp, zfs_acl_t *aclp, cred_t *cr)
802 zfs_oldace_t *oldaclp;
804 uint16_t type, iflags;
805 uint32_t access_mask;
808 zfs_acl_node_t *newaclnode;
810 ASSERT(aclp->z_version == ZFS_ACL_VERSION_INITIAL);
812 * First create the ACE in a contiguous piece of memory
813 * for zfs_copy_ace_2_fuid().
815 * We only convert an ACL once, so this won't happen
818 oldaclp = kmem_alloc(sizeof (zfs_oldace_t) * aclp->z_acl_count,
821 while (cookie = zfs_acl_next_ace(aclp, cookie, &who,
822 &access_mask, &iflags, &type)) {
823 oldaclp[i].z_flags = iflags;
824 oldaclp[i].z_type = type;
825 oldaclp[i].z_fuid = who;
826 oldaclp[i++].z_access_mask = access_mask;
829 newaclnode = zfs_acl_node_alloc(aclp->z_acl_count *
830 sizeof (zfs_object_ace_t));
831 aclp->z_ops = zfs_acl_fuid_ops;
832 VERIFY(zfs_copy_ace_2_fuid(zp->z_zfsvfs, ZTOV(zp)->v_type, aclp,
833 oldaclp, newaclnode->z_acldata, aclp->z_acl_count,
834 &newaclnode->z_size, NULL, cr) == 0);
835 newaclnode->z_ace_count = aclp->z_acl_count;
836 aclp->z_version = ZFS_ACL_VERSION;
837 kmem_free(oldaclp, aclp->z_acl_count * sizeof (zfs_oldace_t));
840 * Release all previous ACL nodes
843 zfs_acl_release_nodes(aclp);
845 list_insert_head(&aclp->z_acl, newaclnode);
847 aclp->z_acl_bytes = newaclnode->z_size;
848 aclp->z_acl_count = newaclnode->z_ace_count;
853 * Convert unix access mask to v4 access mask
856 zfs_unix_to_v4(uint32_t access_mask)
858 uint32_t new_mask = 0;
860 if (access_mask & S_IXOTH)
861 new_mask |= ACE_EXECUTE;
862 if (access_mask & S_IWOTH)
863 new_mask |= ACE_WRITE_DATA;
864 if (access_mask & S_IROTH)
865 new_mask |= ACE_READ_DATA;
870 zfs_set_ace(zfs_acl_t *aclp, void *acep, uint32_t access_mask,
871 uint16_t access_type, uint64_t fuid, uint16_t entry_type)
873 uint16_t type = entry_type & ACE_TYPE_FLAGS;
875 aclp->z_ops.ace_mask_set(acep, access_mask);
876 aclp->z_ops.ace_type_set(acep, access_type);
877 aclp->z_ops.ace_flags_set(acep, entry_type);
878 if ((type != ACE_OWNER && type != OWNING_GROUP &&
879 type != ACE_EVERYONE))
880 aclp->z_ops.ace_who_set(acep, fuid);
884 * Determine mode of file based on ACL.
885 * Also, create FUIDs for any User/Group ACEs
888 zfs_mode_compute(uint64_t fmode, zfs_acl_t *aclp,
889 uint64_t *pflags, uint64_t fuid, uint64_t fgid)
894 zfs_ace_hdr_t *acep = NULL;
896 uint16_t iflags, type;
897 uint32_t access_mask;
898 boolean_t an_exec_denied = B_FALSE;
900 mode = (fmode & (S_IFMT | S_ISUID | S_ISGID | S_ISVTX));
902 while (acep = zfs_acl_next_ace(aclp, acep, &who,
903 &access_mask, &iflags, &type)) {
905 if (!zfs_acl_valid_ace_type(type, iflags))
908 entry_type = (iflags & ACE_TYPE_FLAGS);
911 * Skip over owner@, group@ or everyone@ inherit only ACEs
913 if ((iflags & ACE_INHERIT_ONLY_ACE) &&
914 (entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
915 entry_type == OWNING_GROUP))
918 if (entry_type == ACE_OWNER || (entry_type == 0 &&
920 if ((access_mask & ACE_READ_DATA) &&
921 (!(seen & S_IRUSR))) {
927 if ((access_mask & ACE_WRITE_DATA) &&
928 (!(seen & S_IWUSR))) {
934 if ((access_mask & ACE_EXECUTE) &&
935 (!(seen & S_IXUSR))) {
941 } else if (entry_type == OWNING_GROUP ||
942 (entry_type == ACE_IDENTIFIER_GROUP && who == fgid)) {
943 if ((access_mask & ACE_READ_DATA) &&
944 (!(seen & S_IRGRP))) {
950 if ((access_mask & ACE_WRITE_DATA) &&
951 (!(seen & S_IWGRP))) {
957 if ((access_mask & ACE_EXECUTE) &&
958 (!(seen & S_IXGRP))) {
964 } else if (entry_type == ACE_EVERYONE) {
965 if ((access_mask & ACE_READ_DATA)) {
966 if (!(seen & S_IRUSR)) {
972 if (!(seen & S_IRGRP)) {
978 if (!(seen & S_IROTH)) {
985 if ((access_mask & ACE_WRITE_DATA)) {
986 if (!(seen & S_IWUSR)) {
992 if (!(seen & S_IWGRP)) {
998 if (!(seen & S_IWOTH)) {
1000 if (type == ALLOW) {
1005 if ((access_mask & ACE_EXECUTE)) {
1006 if (!(seen & S_IXUSR)) {
1008 if (type == ALLOW) {
1012 if (!(seen & S_IXGRP)) {
1014 if (type == ALLOW) {
1018 if (!(seen & S_IXOTH)) {
1020 if (type == ALLOW) {
1027 * Only care if this IDENTIFIER_GROUP or
1028 * USER ACE denies execute access to someone,
1029 * mode is not affected
1031 if ((access_mask & ACE_EXECUTE) && type == DENY)
1032 an_exec_denied = B_TRUE;
1037 * Failure to allow is effectively a deny, so execute permission
1038 * is denied if it was never mentioned or if we explicitly
1039 * weren't allowed it.
1041 if (!an_exec_denied &&
1042 ((seen & ALL_MODE_EXECS) != ALL_MODE_EXECS ||
1043 (mode & ALL_MODE_EXECS) != ALL_MODE_EXECS))
1044 an_exec_denied = B_TRUE;
1047 *pflags &= ~ZFS_NO_EXECS_DENIED;
1049 *pflags |= ZFS_NO_EXECS_DENIED;
1055 * Read an external acl object. If the intent is to modify, always
1056 * create a new acl and leave any cached acl in place.
1059 zfs_acl_node_read(znode_t *zp, boolean_t have_lock, zfs_acl_t **aclpp,
1060 boolean_t will_modify)
1065 zfs_acl_node_t *aclnode;
1066 zfs_acl_phys_t znode_acl;
1069 boolean_t drop_lock = B_FALSE;
1071 ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1073 if (zp->z_acl_cached && !will_modify) {
1074 *aclpp = zp->z_acl_cached;
1079 * close race where znode could be upgrade while trying to
1080 * read the znode attributes.
1082 * But this could only happen if the file isn't already an SA
1085 if (!zp->z_is_sa && !have_lock) {
1086 mutex_enter(&zp->z_lock);
1089 version = zfs_znode_acl_version(zp);
1091 if ((error = zfs_acl_znode_info(zp, &aclsize,
1092 &acl_count, &znode_acl)) != 0) {
1096 aclp = zfs_acl_alloc(version);
1098 aclp->z_acl_count = acl_count;
1099 aclp->z_acl_bytes = aclsize;
1101 aclnode = zfs_acl_node_alloc(aclsize);
1102 aclnode->z_ace_count = aclp->z_acl_count;
1103 aclnode->z_size = aclsize;
1106 if (znode_acl.z_acl_extern_obj) {
1107 error = dmu_read(zp->z_zfsvfs->z_os,
1108 znode_acl.z_acl_extern_obj, 0, aclnode->z_size,
1109 aclnode->z_acldata, DMU_READ_PREFETCH);
1111 bcopy(znode_acl.z_ace_data, aclnode->z_acldata,
1115 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DACL_ACES(zp->z_zfsvfs),
1116 aclnode->z_acldata, aclnode->z_size);
1121 zfs_acl_node_free(aclnode);
1122 /* convert checksum errors into IO errors */
1123 if (error == ECKSUM)
1128 list_insert_head(&aclp->z_acl, aclnode);
1132 zp->z_acl_cached = aclp;
1135 mutex_exit(&zp->z_lock);
1141 zfs_acl_data_locator(void **dataptr, uint32_t *length, uint32_t buflen,
1142 boolean_t start, void *userdata)
1144 zfs_acl_locator_cb_t *cb = (zfs_acl_locator_cb_t *)userdata;
1147 cb->cb_acl_node = list_head(&cb->cb_aclp->z_acl);
1149 cb->cb_acl_node = list_next(&cb->cb_aclp->z_acl,
1152 *dataptr = cb->cb_acl_node->z_acldata;
1153 *length = cb->cb_acl_node->z_size;
1157 zfs_acl_chown_setattr(znode_t *zp)
1162 ASSERT(MUTEX_HELD(&zp->z_lock));
1163 ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1165 if ((error = zfs_acl_node_read(zp, B_TRUE, &aclp, B_FALSE)) == 0)
1166 zp->z_mode = zfs_mode_compute(zp->z_mode, aclp,
1167 &zp->z_pflags, zp->z_uid, zp->z_gid);
1172 * common code for setting ACLs.
1174 * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl.
1175 * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's
1176 * already checked the acl and knows whether to inherit.
1179 zfs_aclset_common(znode_t *zp, zfs_acl_t *aclp, cred_t *cr, dmu_tx_t *tx)
1182 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1183 dmu_object_type_t otype;
1184 zfs_acl_locator_cb_t locate = { 0 };
1186 sa_bulk_attr_t bulk[5];
1192 mode = zfs_mode_compute(mode, aclp, &zp->z_pflags,
1193 zp->z_uid, zp->z_gid);
1196 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1197 &mode, sizeof (mode));
1198 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1199 &zp->z_pflags, sizeof (zp->z_pflags));
1200 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1201 &ctime, sizeof (ctime));
1203 if (zp->z_acl_cached) {
1204 zfs_acl_free(zp->z_acl_cached);
1205 zp->z_acl_cached = NULL;
1211 if (!zfsvfs->z_use_fuids) {
1212 otype = DMU_OT_OLDACL;
1214 if ((aclp->z_version == ZFS_ACL_VERSION_INITIAL) &&
1215 (zfsvfs->z_version >= ZPL_VERSION_FUID))
1216 zfs_acl_xform(zp, aclp, cr);
1217 ASSERT(aclp->z_version >= ZFS_ACL_VERSION_FUID);
1222 * Arrgh, we have to handle old on disk format
1223 * as well as newer (preferred) SA format.
1226 if (zp->z_is_sa) { /* the easy case, just update the ACL attribute */
1227 locate.cb_aclp = aclp;
1228 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DACL_ACES(zfsvfs),
1229 zfs_acl_data_locator, &locate, aclp->z_acl_bytes);
1230 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DACL_COUNT(zfsvfs),
1231 NULL, &aclp->z_acl_count, sizeof (uint64_t));
1232 } else { /* Painful legacy way */
1233 zfs_acl_node_t *aclnode;
1235 zfs_acl_phys_t acl_phys;
1238 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zfsvfs),
1239 &acl_phys, sizeof (acl_phys))) != 0)
1242 aoid = acl_phys.z_acl_extern_obj;
1244 if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1246 * If ACL was previously external and we are now
1247 * converting to new ACL format then release old
1248 * ACL object and create a new one.
1251 aclp->z_version != acl_phys.z_acl_version) {
1252 error = dmu_object_free(zfsvfs->z_os, aoid, tx);
1258 aoid = dmu_object_alloc(zfsvfs->z_os,
1259 otype, aclp->z_acl_bytes,
1260 otype == DMU_OT_ACL ?
1261 DMU_OT_SYSACL : DMU_OT_NONE,
1262 otype == DMU_OT_ACL ?
1263 DN_MAX_BONUSLEN : 0, tx);
1265 (void) dmu_object_set_blocksize(zfsvfs->z_os,
1266 aoid, aclp->z_acl_bytes, 0, tx);
1268 acl_phys.z_acl_extern_obj = aoid;
1269 for (aclnode = list_head(&aclp->z_acl); aclnode;
1270 aclnode = list_next(&aclp->z_acl, aclnode)) {
1271 if (aclnode->z_ace_count == 0)
1273 dmu_write(zfsvfs->z_os, aoid, off,
1274 aclnode->z_size, aclnode->z_acldata, tx);
1275 off += aclnode->z_size;
1278 void *start = acl_phys.z_ace_data;
1280 * Migrating back embedded?
1282 if (acl_phys.z_acl_extern_obj) {
1283 error = dmu_object_free(zfsvfs->z_os,
1284 acl_phys.z_acl_extern_obj, tx);
1287 acl_phys.z_acl_extern_obj = 0;
1290 for (aclnode = list_head(&aclp->z_acl); aclnode;
1291 aclnode = list_next(&aclp->z_acl, aclnode)) {
1292 if (aclnode->z_ace_count == 0)
1294 bcopy(aclnode->z_acldata, start,
1296 start = (caddr_t)start + aclnode->z_size;
1300 * If Old version then swap count/bytes to match old
1301 * layout of znode_acl_phys_t.
1303 if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1304 acl_phys.z_acl_size = aclp->z_acl_count;
1305 acl_phys.z_acl_count = aclp->z_acl_bytes;
1307 acl_phys.z_acl_size = aclp->z_acl_bytes;
1308 acl_phys.z_acl_count = aclp->z_acl_count;
1310 acl_phys.z_acl_version = aclp->z_version;
1312 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
1313 &acl_phys, sizeof (acl_phys));
1317 * Replace ACL wide bits, but first clear them.
1319 zp->z_pflags &= ~ZFS_ACL_WIDE_FLAGS;
1321 zp->z_pflags |= aclp->z_hints;
1323 if (ace_trivial_common(aclp, 0, zfs_ace_walk) == 0)
1324 zp->z_pflags |= ZFS_ACL_TRIVIAL;
1326 zfs_tstamp_update_setup(zp, STATE_CHANGED, NULL, ctime, B_TRUE);
1327 return (sa_bulk_update(zp->z_sa_hdl, bulk, count, tx));
1331 * Update access mask for prepended ACE
1333 * This applies the "groupmask" value for aclmode property.
1336 zfs_acl_prepend_fixup(zfs_acl_t *aclp, void *acep, void *origacep,
1337 mode_t mode, uint64_t owner)
1339 int rmask, wmask, xmask;
1342 uint32_t origmask, acepmask;
1345 aceflags = aclp->z_ops.ace_flags_get(acep);
1346 fuid = aclp->z_ops.ace_who_get(acep);
1347 origmask = aclp->z_ops.ace_mask_get(origacep);
1348 acepmask = aclp->z_ops.ace_mask_get(acep);
1350 user_ace = (!(aceflags &
1351 (ACE_OWNER|ACE_GROUP|ACE_IDENTIFIER_GROUP)));
1353 if (user_ace && (fuid == owner)) {
1363 if (origmask & ACE_READ_DATA) {
1365 acepmask &= ~ACE_READ_DATA;
1367 acepmask |= ACE_READ_DATA;
1371 if (origmask & ACE_WRITE_DATA) {
1373 acepmask &= ~ACE_WRITE_DATA;
1375 acepmask |= ACE_WRITE_DATA;
1379 if (origmask & ACE_APPEND_DATA) {
1381 acepmask &= ~ACE_APPEND_DATA;
1383 acepmask |= ACE_APPEND_DATA;
1387 if (origmask & ACE_EXECUTE) {
1389 acepmask &= ~ACE_EXECUTE;
1391 acepmask |= ACE_EXECUTE;
1394 aclp->z_ops.ace_mask_set(acep, acepmask);
1398 zfs_acl_chmod(zfsvfs_t *zfsvfs, uint64_t mode, zfs_acl_t *aclp)
1402 int new_count, new_bytes;
1405 uint16_t iflags, type;
1406 uint32_t access_mask;
1407 zfs_acl_node_t *newnode;
1408 size_t abstract_size = aclp->z_ops.ace_abstract_size();
1410 uint32_t owner, group, everyone;
1411 uint32_t deny1, deny2, allow0;
1413 new_count = new_bytes = 0;
1415 acl_trivial_access_masks((mode_t)mode, &allow0, &deny1, &deny2,
1416 &owner, &group, &everyone);
1418 newnode = zfs_acl_node_alloc((abstract_size * 6) + aclp->z_acl_bytes);
1420 zacep = newnode->z_acldata;
1422 zfs_set_ace(aclp, zacep, allow0, ALLOW, -1, ACE_OWNER);
1423 zacep = (void *)((uintptr_t)zacep + abstract_size);
1425 new_bytes += abstract_size;
1427 zfs_set_ace(aclp, zacep, deny1, DENY, -1, ACE_OWNER);
1428 zacep = (void *)((uintptr_t)zacep + abstract_size);
1430 new_bytes += abstract_size;
1433 zfs_set_ace(aclp, zacep, deny2, DENY, -1, OWNING_GROUP);
1434 zacep = (void *)((uintptr_t)zacep + abstract_size);
1436 new_bytes += abstract_size;
1439 while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
1441 uint16_t inherit_flags;
1443 entry_type = (iflags & ACE_TYPE_FLAGS);
1444 inherit_flags = (iflags & ALL_INHERIT);
1446 if ((entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
1447 (entry_type == OWNING_GROUP)) &&
1448 ((inherit_flags & ACE_INHERIT_ONLY_ACE) == 0)) {
1452 if ((type != ALLOW && type != DENY) ||
1453 (inherit_flags & ACE_INHERIT_ONLY_ACE)) {
1455 aclp->z_hints |= ZFS_INHERIT_ACE;
1457 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1458 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1459 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1460 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1461 aclp->z_hints |= ZFS_ACL_OBJ_ACE;
1467 * Limit permissions to be no greater than
1470 if (type == ALLOW && zfsvfs->z_acl_inherit == ZFS_ACL_RESTRICTED) {
1471 if (!(mode & S_IRGRP))
1472 access_mask &= ~ACE_READ_DATA;
1473 if (!(mode & S_IWGRP))
1475 ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
1476 if (!(mode & S_IXGRP))
1477 access_mask &= ~ACE_EXECUTE;
1479 ~(ACE_WRITE_OWNER|ACE_WRITE_ACL|
1480 ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS);
1483 zfs_set_ace(aclp, zacep, access_mask, type, who, iflags);
1484 ace_size = aclp->z_ops.ace_size(acep);
1485 zacep = (void *)((uintptr_t)zacep + ace_size);
1487 new_bytes += ace_size;
1489 zfs_set_ace(aclp, zacep, owner, 0, -1, ACE_OWNER);
1490 zacep = (void *)((uintptr_t)zacep + abstract_size);
1491 zfs_set_ace(aclp, zacep, group, 0, -1, OWNING_GROUP);
1492 zacep = (void *)((uintptr_t)zacep + abstract_size);
1493 zfs_set_ace(aclp, zacep, everyone, 0, -1, ACE_EVERYONE);
1496 new_bytes += abstract_size * 3;
1497 zfs_acl_release_nodes(aclp);
1498 aclp->z_acl_count = new_count;
1499 aclp->z_acl_bytes = new_bytes;
1500 newnode->z_ace_count = new_count;
1501 newnode->z_size = new_bytes;
1502 list_insert_tail(&aclp->z_acl, newnode);
1506 zfs_acl_chmod_setattr(znode_t *zp, zfs_acl_t **aclp, uint64_t mode)
1508 mutex_enter(&zp->z_acl_lock);
1509 mutex_enter(&zp->z_lock);
1510 *aclp = zfs_acl_alloc(zfs_acl_version_zp(zp));
1511 (*aclp)->z_hints = zp->z_pflags & V4_ACL_WIDE_FLAGS;
1512 zfs_acl_chmod(zp->z_zfsvfs, mode, *aclp);
1513 mutex_exit(&zp->z_lock);
1514 mutex_exit(&zp->z_acl_lock);
1519 * strip off write_owner and write_acl
1522 zfs_restricted_update(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, void *acep)
1524 uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1526 if ((zfsvfs->z_acl_inherit == ZFS_ACL_RESTRICTED) &&
1527 (aclp->z_ops.ace_type_get(acep) == ALLOW)) {
1528 mask &= ~RESTRICTED_CLEAR;
1529 aclp->z_ops.ace_mask_set(acep, mask);
1534 * Should ACE be inherited?
1537 zfs_ace_can_use(vtype_t vtype, uint16_t acep_flags)
1539 int iflags = (acep_flags & 0xf);
1541 if ((vtype == VDIR) && (iflags & ACE_DIRECTORY_INHERIT_ACE))
1543 else if (iflags & ACE_FILE_INHERIT_ACE)
1544 return (!((vtype == VDIR) &&
1545 (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)));
1550 * inherit inheritable ACEs from parent
1553 zfs_acl_inherit(zfsvfs_t *zfsvfs, vtype_t vtype, zfs_acl_t *paclp,
1554 uint64_t mode, boolean_t *need_chmod)
1558 zfs_acl_node_t *aclnode;
1559 zfs_acl_t *aclp = NULL;
1561 uint32_t access_mask;
1562 uint16_t iflags, newflags, type;
1564 void *data1, *data2;
1565 size_t data1sz, data2sz;
1566 boolean_t vdir = vtype == VDIR;
1567 boolean_t vreg = vtype == VREG;
1568 boolean_t passthrough, passthrough_x, noallow;
1571 zfsvfs->z_acl_inherit == ZFS_ACL_PASSTHROUGH_X;
1572 passthrough = passthrough_x ||
1573 zfsvfs->z_acl_inherit == ZFS_ACL_PASSTHROUGH;
1575 zfsvfs->z_acl_inherit == ZFS_ACL_NOALLOW;
1577 *need_chmod = B_TRUE;
1579 aclp = zfs_acl_alloc(paclp->z_version);
1580 if (zfsvfs->z_acl_inherit == ZFS_ACL_DISCARD || vtype == VLNK)
1582 while (pacep = zfs_acl_next_ace(paclp, pacep, &who,
1583 &access_mask, &iflags, &type)) {
1586 * don't inherit bogus ACEs
1588 if (!zfs_acl_valid_ace_type(type, iflags))
1591 if (noallow && type == ALLOW)
1594 ace_size = aclp->z_ops.ace_size(pacep);
1596 if (!zfs_ace_can_use(vtype, iflags))
1600 * If owner@, group@, or everyone@ inheritable
1601 * then zfs_acl_chmod() isn't needed.
1604 ((iflags & (ACE_OWNER|ACE_EVERYONE)) ||
1605 ((iflags & OWNING_GROUP) ==
1606 OWNING_GROUP)) && (vreg || (vdir && (iflags &
1607 ACE_DIRECTORY_INHERIT_ACE)))) {
1608 *need_chmod = B_FALSE;
1611 if (!vdir && passthrough_x &&
1612 ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)) {
1613 access_mask &= ~ACE_EXECUTE;
1616 aclnode = zfs_acl_node_alloc(ace_size);
1617 list_insert_tail(&aclp->z_acl, aclnode);
1618 acep = aclnode->z_acldata;
1620 zfs_set_ace(aclp, acep, access_mask, type,
1621 who, iflags|ACE_INHERITED_ACE);
1624 * Copy special opaque data if any
1626 if ((data1sz = paclp->z_ops.ace_data(pacep, &data1)) != 0) {
1627 VERIFY((data2sz = aclp->z_ops.ace_data(acep,
1628 &data2)) == data1sz);
1629 bcopy(data1, data2, data2sz);
1632 aclp->z_acl_count++;
1633 aclnode->z_ace_count++;
1634 aclp->z_acl_bytes += aclnode->z_size;
1635 newflags = aclp->z_ops.ace_flags_get(acep);
1638 aclp->z_hints |= ZFS_INHERIT_ACE;
1640 if ((iflags & ACE_NO_PROPAGATE_INHERIT_ACE) || !vdir) {
1641 newflags &= ~ALL_INHERIT;
1642 aclp->z_ops.ace_flags_set(acep,
1643 newflags|ACE_INHERITED_ACE);
1644 zfs_restricted_update(zfsvfs, aclp, acep);
1651 * If only FILE_INHERIT is set then turn on
1654 if ((iflags & (ACE_FILE_INHERIT_ACE |
1655 ACE_DIRECTORY_INHERIT_ACE)) == ACE_FILE_INHERIT_ACE) {
1656 newflags |= ACE_INHERIT_ONLY_ACE;
1657 aclp->z_ops.ace_flags_set(acep,
1658 newflags|ACE_INHERITED_ACE);
1660 newflags &= ~ACE_INHERIT_ONLY_ACE;
1661 aclp->z_ops.ace_flags_set(acep,
1662 newflags|ACE_INHERITED_ACE);
1669 * Create file system object initial permissions
1670 * including inheritable ACEs.
1673 zfs_acl_ids_create(znode_t *dzp, int flag, vattr_t *vap, cred_t *cr,
1674 vsecattr_t *vsecp, zfs_acl_ids_t *acl_ids)
1677 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1680 boolean_t need_chmod = B_TRUE;
1681 boolean_t inherited = B_FALSE;
1683 bzero(acl_ids, sizeof (zfs_acl_ids_t));
1684 acl_ids->z_mode = MAKEIMODE(vap->va_type, vap->va_mode);
1687 if ((error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, cr,
1688 &acl_ids->z_fuidp, &acl_ids->z_aclp)) != 0)
1691 * Determine uid and gid.
1693 if ((flag & IS_ROOT_NODE) || zfsvfs->z_replay ||
1694 ((flag & IS_XATTR) && (vap->va_type == VDIR))) {
1695 acl_ids->z_fuid = zfs_fuid_create(zfsvfs,
1696 (uint64_t)vap->va_uid, cr,
1697 ZFS_OWNER, &acl_ids->z_fuidp);
1698 acl_ids->z_fgid = zfs_fuid_create(zfsvfs,
1699 (uint64_t)vap->va_gid, cr,
1700 ZFS_GROUP, &acl_ids->z_fuidp);
1703 acl_ids->z_fuid = zfs_fuid_create_cred(zfsvfs, ZFS_OWNER,
1704 cr, &acl_ids->z_fuidp);
1705 acl_ids->z_fgid = 0;
1706 if (vap->va_mask & AT_GID) {
1707 acl_ids->z_fgid = zfs_fuid_create(zfsvfs,
1708 (uint64_t)vap->va_gid,
1709 cr, ZFS_GROUP, &acl_ids->z_fuidp);
1711 if (acl_ids->z_fgid != dzp->z_gid &&
1712 !groupmember(vap->va_gid, cr) &&
1713 secpolicy_vnode_create_gid(cr) != 0)
1714 acl_ids->z_fgid = 0;
1716 if (acl_ids->z_fgid == 0) {
1717 if (dzp->z_mode & S_ISGID) {
1721 acl_ids->z_fgid = dzp->z_gid;
1722 gid = zfs_fuid_map_id(zfsvfs, acl_ids->z_fgid,
1725 if (zfsvfs->z_use_fuids &&
1726 IS_EPHEMERAL(acl_ids->z_fgid)) {
1727 domain = zfs_fuid_idx_domain(
1728 &zfsvfs->z_fuid_idx,
1729 FUID_INDEX(acl_ids->z_fgid));
1730 rid = FUID_RID(acl_ids->z_fgid);
1731 zfs_fuid_node_add(&acl_ids->z_fuidp,
1733 FUID_INDEX(acl_ids->z_fgid),
1734 acl_ids->z_fgid, ZFS_GROUP);
1737 acl_ids->z_fgid = zfs_fuid_create_cred(zfsvfs,
1738 ZFS_GROUP, cr, &acl_ids->z_fuidp);
1740 gid = acl_ids->z_fgid = dzp->z_gid;
1749 * If we're creating a directory, and the parent directory has the
1750 * set-GID bit set, set in on the new directory.
1751 * Otherwise, if the user is neither privileged nor a member of the
1752 * file's new group, clear the file's set-GID bit.
1755 if (!(flag & IS_ROOT_NODE) && (dzp->z_mode & S_ISGID) &&
1756 (vap->va_type == VDIR)) {
1757 acl_ids->z_mode |= S_ISGID;
1759 if ((acl_ids->z_mode & S_ISGID) &&
1760 secpolicy_vnode_setids_setgids(ZTOV(dzp), cr, gid) != 0)
1761 acl_ids->z_mode &= ~S_ISGID;
1764 if (acl_ids->z_aclp == NULL) {
1765 mutex_enter(&dzp->z_acl_lock);
1766 mutex_enter(&dzp->z_lock);
1767 if (!(flag & IS_ROOT_NODE) && (ZTOV(dzp)->v_type == VDIR &&
1768 (dzp->z_pflags & ZFS_INHERIT_ACE)) &&
1769 !(dzp->z_pflags & ZFS_XATTR)) {
1770 VERIFY(0 == zfs_acl_node_read(dzp, B_TRUE,
1772 acl_ids->z_aclp = zfs_acl_inherit(zfsvfs,
1773 vap->va_type, paclp, acl_ids->z_mode, &need_chmod);
1777 zfs_acl_alloc(zfs_acl_version_zp(dzp));
1778 acl_ids->z_aclp->z_hints |= ZFS_ACL_TRIVIAL;
1780 mutex_exit(&dzp->z_lock);
1781 mutex_exit(&dzp->z_acl_lock);
1783 acl_ids->z_aclp->z_hints |= (vap->va_type == VDIR) ?
1784 ZFS_ACL_AUTO_INHERIT : 0;
1785 zfs_acl_chmod(zfsvfs, acl_ids->z_mode, acl_ids->z_aclp);
1789 if (inherited || vsecp) {
1790 acl_ids->z_mode = zfs_mode_compute(acl_ids->z_mode,
1791 acl_ids->z_aclp, &acl_ids->z_aclp->z_hints,
1792 acl_ids->z_fuid, acl_ids->z_fgid);
1793 if (ace_trivial_common(acl_ids->z_aclp, 0, zfs_ace_walk) == 0)
1794 acl_ids->z_aclp->z_hints |= ZFS_ACL_TRIVIAL;
1801 * Free ACL and fuid_infop, but not the acl_ids structure
1804 zfs_acl_ids_free(zfs_acl_ids_t *acl_ids)
1806 if (acl_ids->z_aclp)
1807 zfs_acl_free(acl_ids->z_aclp);
1808 if (acl_ids->z_fuidp)
1809 zfs_fuid_info_free(acl_ids->z_fuidp);
1810 acl_ids->z_aclp = NULL;
1811 acl_ids->z_fuidp = NULL;
1815 zfs_acl_ids_overquota(zfsvfs_t *zfsvfs, zfs_acl_ids_t *acl_ids)
1817 return (zfs_fuid_overquota(zfsvfs, B_FALSE, acl_ids->z_fuid) ||
1818 zfs_fuid_overquota(zfsvfs, B_TRUE, acl_ids->z_fgid));
1822 * Retrieve a files ACL
1825 zfs_getacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
1833 mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT |
1834 VSA_ACE_ACLFLAGS | VSA_ACE_ALLTYPES);
1839 if (error = zfs_zaccess(zp, ACE_READ_ACL, 0, skipaclchk, cr))
1842 mutex_enter(&zp->z_acl_lock);
1844 error = zfs_acl_node_read(zp, B_FALSE, &aclp, B_FALSE);
1846 mutex_exit(&zp->z_acl_lock);
1851 * Scan ACL to determine number of ACEs
1853 if ((zp->z_pflags & ZFS_ACL_OBJ_ACE) && !(mask & VSA_ACE_ALLTYPES)) {
1856 uint32_t access_mask;
1857 uint16_t type, iflags;
1859 while (zacep = zfs_acl_next_ace(aclp, zacep,
1860 &who, &access_mask, &iflags, &type)) {
1862 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1863 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1864 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1865 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1872 vsecp->vsa_aclcnt = count;
1874 count = (int)aclp->z_acl_count;
1876 if (mask & VSA_ACECNT) {
1877 vsecp->vsa_aclcnt = count;
1880 if (mask & VSA_ACE) {
1883 aclsz = count * sizeof (ace_t) +
1884 sizeof (ace_object_t) * largeace;
1886 vsecp->vsa_aclentp = kmem_alloc(aclsz, KM_SLEEP);
1887 vsecp->vsa_aclentsz = aclsz;
1889 if (aclp->z_version == ZFS_ACL_VERSION_FUID)
1890 zfs_copy_fuid_2_ace(zp->z_zfsvfs, aclp, cr,
1891 vsecp->vsa_aclentp, !(mask & VSA_ACE_ALLTYPES));
1893 zfs_acl_node_t *aclnode;
1894 void *start = vsecp->vsa_aclentp;
1896 for (aclnode = list_head(&aclp->z_acl); aclnode;
1897 aclnode = list_next(&aclp->z_acl, aclnode)) {
1898 bcopy(aclnode->z_acldata, start,
1900 start = (caddr_t)start + aclnode->z_size;
1902 ASSERT((caddr_t)start - (caddr_t)vsecp->vsa_aclentp ==
1906 if (mask & VSA_ACE_ACLFLAGS) {
1907 vsecp->vsa_aclflags = 0;
1908 if (zp->z_pflags & ZFS_ACL_DEFAULTED)
1909 vsecp->vsa_aclflags |= ACL_DEFAULTED;
1910 if (zp->z_pflags & ZFS_ACL_PROTECTED)
1911 vsecp->vsa_aclflags |= ACL_PROTECTED;
1912 if (zp->z_pflags & ZFS_ACL_AUTO_INHERIT)
1913 vsecp->vsa_aclflags |= ACL_AUTO_INHERIT;
1916 mutex_exit(&zp->z_acl_lock);
1922 zfs_vsec_2_aclp(zfsvfs_t *zfsvfs, vtype_t obj_type,
1923 vsecattr_t *vsecp, cred_t *cr, zfs_fuid_info_t **fuidp, zfs_acl_t **zaclp)
1926 zfs_acl_node_t *aclnode;
1927 int aclcnt = vsecp->vsa_aclcnt;
1930 if (vsecp->vsa_aclcnt > MAX_ACL_ENTRIES || vsecp->vsa_aclcnt <= 0)
1933 aclp = zfs_acl_alloc(zfs_acl_version(zfsvfs->z_version));
1936 aclnode = zfs_acl_node_alloc(aclcnt * sizeof (zfs_object_ace_t));
1937 if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1938 if ((error = zfs_copy_ace_2_oldace(obj_type, aclp,
1939 (ace_t *)vsecp->vsa_aclentp, aclnode->z_acldata,
1940 aclcnt, &aclnode->z_size)) != 0) {
1942 zfs_acl_node_free(aclnode);
1946 if ((error = zfs_copy_ace_2_fuid(zfsvfs, obj_type, aclp,
1947 vsecp->vsa_aclentp, aclnode->z_acldata, aclcnt,
1948 &aclnode->z_size, fuidp, cr)) != 0) {
1950 zfs_acl_node_free(aclnode);
1954 aclp->z_acl_bytes = aclnode->z_size;
1955 aclnode->z_ace_count = aclcnt;
1956 aclp->z_acl_count = aclcnt;
1957 list_insert_head(&aclp->z_acl, aclnode);
1960 * If flags are being set then add them to z_hints
1962 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) {
1963 if (vsecp->vsa_aclflags & ACL_PROTECTED)
1964 aclp->z_hints |= ZFS_ACL_PROTECTED;
1965 if (vsecp->vsa_aclflags & ACL_DEFAULTED)
1966 aclp->z_hints |= ZFS_ACL_DEFAULTED;
1967 if (vsecp->vsa_aclflags & ACL_AUTO_INHERIT)
1968 aclp->z_hints |= ZFS_ACL_AUTO_INHERIT;
1980 zfs_setacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
1982 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1983 zilog_t *zilog = zfsvfs->z_log;
1984 ulong_t mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT);
1988 zfs_fuid_info_t *fuidp = NULL;
1989 boolean_t fuid_dirtied;
1995 if (zp->z_pflags & ZFS_IMMUTABLE)
1998 if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr))
2001 error = zfs_vsec_2_aclp(zfsvfs, ZTOV(zp)->v_type, vsecp, cr, &fuidp,
2007 * If ACL wide flags aren't being set then preserve any
2010 if (!(vsecp->vsa_mask & VSA_ACE_ACLFLAGS)) {
2012 (zp->z_pflags & V4_ACL_WIDE_FLAGS);
2015 mutex_enter(&zp->z_acl_lock);
2016 mutex_enter(&zp->z_lock);
2018 tx = dmu_tx_create(zfsvfs->z_os);
2020 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2022 fuid_dirtied = zfsvfs->z_fuid_dirty;
2024 zfs_fuid_txhold(zfsvfs, tx);
2027 * If old version and ACL won't fit in bonus and we aren't
2028 * upgrading then take out necessary DMU holds
2031 if ((acl_obj = zfs_external_acl(zp)) != 0) {
2032 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
2033 zfs_znode_acl_version(zp) <= ZFS_ACL_VERSION_INITIAL) {
2034 dmu_tx_hold_free(tx, acl_obj, 0,
2036 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2039 dmu_tx_hold_write(tx, acl_obj, 0, aclp->z_acl_bytes);
2041 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2042 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, aclp->z_acl_bytes);
2045 zfs_sa_upgrade_txholds(tx, zp);
2046 error = dmu_tx_assign(tx, TXG_NOWAIT);
2048 mutex_exit(&zp->z_acl_lock);
2049 mutex_exit(&zp->z_lock);
2051 if (error == ERESTART) {
2061 error = zfs_aclset_common(zp, aclp, cr, tx);
2063 ASSERT(zp->z_acl_cached == NULL);
2064 zp->z_acl_cached = aclp;
2067 zfs_fuid_sync(zfsvfs, tx);
2069 zfs_log_acl(zilog, tx, zp, vsecp, fuidp);
2072 zfs_fuid_info_free(fuidp);
2075 mutex_exit(&zp->z_lock);
2076 mutex_exit(&zp->z_acl_lock);
2082 * Check accesses of interest (AoI) against attributes of the dataset
2083 * such as read-only. Returns zero if no AoI conflict with dataset
2084 * attributes, otherwise an appropriate errno is returned.
2087 zfs_zaccess_dataset_check(znode_t *zp, uint32_t v4_mode)
2089 if ((v4_mode & WRITE_MASK) &&
2090 (zp->z_zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) &&
2091 (!IS_DEVVP(ZTOV(zp)) ||
2092 (IS_DEVVP(ZTOV(zp)) && (v4_mode & WRITE_MASK_ATTRS)))) {
2097 * Only check for READONLY on non-directories.
2099 if ((v4_mode & WRITE_MASK_DATA) &&
2100 (((ZTOV(zp)->v_type != VDIR) &&
2101 (zp->z_pflags & (ZFS_READONLY | ZFS_IMMUTABLE))) ||
2102 (ZTOV(zp)->v_type == VDIR &&
2103 (zp->z_pflags & ZFS_IMMUTABLE)))) {
2108 if ((v4_mode & (ACE_DELETE | ACE_DELETE_CHILD)) &&
2109 (zp->z_pflags & ZFS_NOUNLINK)) {
2114 * In FreeBSD we allow to modify directory's content is ZFS_NOUNLINK
2115 * (sunlnk) is set. We just don't allow directory removal, which is
2116 * handled in zfs_zaccess_delete().
2118 if ((v4_mode & ACE_DELETE) &&
2119 (zp->z_pflags & ZFS_NOUNLINK)) {
2124 if (((v4_mode & (ACE_READ_DATA|ACE_EXECUTE)) &&
2125 (zp->z_pflags & ZFS_AV_QUARANTINED))) {
2133 * The primary usage of this function is to loop through all of the
2134 * ACEs in the znode, determining what accesses of interest (AoI) to
2135 * the caller are allowed or denied. The AoI are expressed as bits in
2136 * the working_mode parameter. As each ACE is processed, bits covered
2137 * by that ACE are removed from the working_mode. This removal
2138 * facilitates two things. The first is that when the working mode is
2139 * empty (= 0), we know we've looked at all the AoI. The second is
2140 * that the ACE interpretation rules don't allow a later ACE to undo
2141 * something granted or denied by an earlier ACE. Removing the
2142 * discovered access or denial enforces this rule. At the end of
2143 * processing the ACEs, all AoI that were found to be denied are
2144 * placed into the working_mode, giving the caller a mask of denied
2145 * accesses. Returns:
2146 * 0 if all AoI granted
2147 * EACCESS if the denied mask is non-zero
2148 * other error if abnormal failure (e.g., IO error)
2150 * A secondary usage of the function is to determine if any of the
2151 * AoI are granted. If an ACE grants any access in
2152 * the working_mode, we immediately short circuit out of the function.
2153 * This mode is chosen by setting anyaccess to B_TRUE. The
2154 * working_mode is not a denied access mask upon exit if the function
2155 * is used in this manner.
2158 zfs_zaccess_aces_check(znode_t *zp, uint32_t *working_mode,
2159 boolean_t anyaccess, cred_t *cr)
2161 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2164 uid_t uid = crgetuid(cr);
2166 uint16_t type, iflags;
2167 uint16_t entry_type;
2168 uint32_t access_mask;
2169 uint32_t deny_mask = 0;
2170 zfs_ace_hdr_t *acep = NULL;
2175 zfs_fuid_map_ids(zp, cr, &fowner, &gowner);
2177 mutex_enter(&zp->z_acl_lock);
2179 error = zfs_acl_node_read(zp, B_FALSE, &aclp, B_FALSE);
2181 mutex_exit(&zp->z_acl_lock);
2185 ASSERT(zp->z_acl_cached);
2187 while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
2189 uint32_t mask_matched;
2191 if (!zfs_acl_valid_ace_type(type, iflags))
2194 if (ZTOV(zp)->v_type == VDIR && (iflags & ACE_INHERIT_ONLY_ACE))
2197 /* Skip ACE if it does not affect any AoI */
2198 mask_matched = (access_mask & *working_mode);
2202 entry_type = (iflags & ACE_TYPE_FLAGS);
2206 switch (entry_type) {
2214 case ACE_IDENTIFIER_GROUP:
2215 checkit = zfs_groupmember(zfsvfs, who, cr);
2223 if (entry_type == 0) {
2226 newid = zfs_fuid_map_id(zfsvfs, who, cr,
2228 if (newid != IDMAP_WK_CREATOR_OWNER_UID &&
2233 mutex_exit(&zp->z_acl_lock);
2240 DTRACE_PROBE3(zfs__ace__denies,
2242 zfs_ace_hdr_t *, acep,
2243 uint32_t, mask_matched);
2244 deny_mask |= mask_matched;
2246 DTRACE_PROBE3(zfs__ace__allows,
2248 zfs_ace_hdr_t *, acep,
2249 uint32_t, mask_matched);
2251 mutex_exit(&zp->z_acl_lock);
2255 *working_mode &= ~mask_matched;
2259 if (*working_mode == 0)
2263 mutex_exit(&zp->z_acl_lock);
2265 /* Put the found 'denies' back on the working mode */
2267 *working_mode |= deny_mask;
2269 } else if (*working_mode) {
2277 * Return true if any access whatsoever granted, we don't actually
2278 * care what access is granted.
2281 zfs_has_access(znode_t *zp, cred_t *cr)
2283 uint32_t have = ACE_ALL_PERMS;
2285 if (zfs_zaccess_aces_check(zp, &have, B_TRUE, cr) != 0) {
2288 owner = zfs_fuid_map_id(zp->z_zfsvfs, zp->z_uid, cr, ZFS_OWNER);
2289 return (secpolicy_vnode_any_access(cr, ZTOV(zp), owner) == 0);
2295 zfs_zaccess_common(znode_t *zp, uint32_t v4_mode, uint32_t *working_mode,
2296 boolean_t *check_privs, boolean_t skipaclchk, cred_t *cr)
2298 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2301 *working_mode = v4_mode;
2302 *check_privs = B_TRUE;
2305 * Short circuit empty requests
2307 if (v4_mode == 0 || zfsvfs->z_replay) {
2312 if ((err = zfs_zaccess_dataset_check(zp, v4_mode)) != 0) {
2313 *check_privs = B_FALSE;
2318 * The caller requested that the ACL check be skipped. This
2319 * would only happen if the caller checked VOP_ACCESS() with a
2320 * 32 bit ACE mask and already had the appropriate permissions.
2327 return (zfs_zaccess_aces_check(zp, working_mode, B_FALSE, cr));
2331 zfs_zaccess_append(znode_t *zp, uint32_t *working_mode, boolean_t *check_privs,
2334 if (*working_mode != ACE_WRITE_DATA)
2337 return (zfs_zaccess_common(zp, ACE_APPEND_DATA, working_mode,
2338 check_privs, B_FALSE, cr));
2342 zfs_fastaccesschk_execute(znode_t *zdp, cred_t *cr)
2344 boolean_t owner = B_FALSE;
2345 boolean_t groupmbr = B_FALSE;
2347 uid_t uid = crgetuid(cr);
2350 if (zdp->z_pflags & ZFS_AV_QUARANTINED)
2353 is_attr = ((zdp->z_pflags & ZFS_XATTR) &&
2354 (ZTOV(zdp)->v_type == VDIR));
2359 mutex_enter(&zdp->z_acl_lock);
2361 if (zdp->z_pflags & ZFS_NO_EXECS_DENIED) {
2362 mutex_exit(&zdp->z_acl_lock);
2366 if (FUID_INDEX(zdp->z_uid) != 0 || FUID_INDEX(zdp->z_gid) != 0) {
2367 mutex_exit(&zdp->z_acl_lock);
2371 if (uid == zdp->z_uid) {
2373 if (zdp->z_mode & S_IXUSR) {
2374 mutex_exit(&zdp->z_acl_lock);
2377 mutex_exit(&zdp->z_acl_lock);
2381 if (groupmember(zdp->z_gid, cr)) {
2383 if (zdp->z_mode & S_IXGRP) {
2384 mutex_exit(&zdp->z_acl_lock);
2387 mutex_exit(&zdp->z_acl_lock);
2391 if (!owner && !groupmbr) {
2392 if (zdp->z_mode & S_IXOTH) {
2393 mutex_exit(&zdp->z_acl_lock);
2398 mutex_exit(&zdp->z_acl_lock);
2401 DTRACE_PROBE(zfs__fastpath__execute__access__miss);
2402 ZFS_ENTER(zdp->z_zfsvfs);
2403 error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr);
2404 ZFS_EXIT(zdp->z_zfsvfs);
2409 * Determine whether Access should be granted/denied.
2410 * The least priv subsytem is always consulted as a basic privilege
2411 * can define any form of access.
2414 zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr)
2416 uint32_t working_mode;
2419 boolean_t check_privs;
2421 znode_t *check_zp = zp;
2425 is_attr = ((zp->z_pflags & ZFS_XATTR) && (ZTOV(zp)->v_type == VDIR));
2429 * In FreeBSD, we don't care about permissions of individual ADS.
2430 * Note that not checking them is not just an optimization - without
2431 * this shortcut, EA operations may bogusly fail with EACCES.
2433 if (zp->z_pflags & ZFS_XATTR)
2437 * If attribute then validate against base file
2442 if ((error = sa_lookup(zp->z_sa_hdl,
2443 SA_ZPL_PARENT(zp->z_zfsvfs), &parent,
2444 sizeof (parent))) != 0)
2447 if ((error = zfs_zget(zp->z_zfsvfs,
2448 parent, &xzp)) != 0) {
2455 * fixup mode to map to xattr perms
2458 if (mode & (ACE_WRITE_DATA|ACE_APPEND_DATA)) {
2459 mode &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
2460 mode |= ACE_WRITE_NAMED_ATTRS;
2463 if (mode & (ACE_READ_DATA|ACE_EXECUTE)) {
2464 mode &= ~(ACE_READ_DATA|ACE_EXECUTE);
2465 mode |= ACE_READ_NAMED_ATTRS;
2470 owner = zfs_fuid_map_id(zp->z_zfsvfs, zp->z_uid, cr, ZFS_OWNER);
2472 * Map the bits required to the standard vnode flags VREAD|VWRITE|VEXEC
2473 * in needed_bits. Map the bits mapped by working_mode (currently
2474 * missing) in missing_bits.
2475 * Call secpolicy_vnode_access2() with (needed_bits & ~checkmode),
2480 working_mode = mode;
2481 if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES)) &&
2482 owner == crgetuid(cr))
2483 working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES);
2485 if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS|
2486 ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_SYNCHRONIZE))
2487 needed_bits |= VREAD;
2488 if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS|
2489 ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES|ACE_SYNCHRONIZE))
2490 needed_bits |= VWRITE;
2491 if (working_mode & ACE_EXECUTE)
2492 needed_bits |= VEXEC;
2494 if ((error = zfs_zaccess_common(check_zp, mode, &working_mode,
2495 &check_privs, skipaclchk, cr)) == 0) {
2498 return (secpolicy_vnode_access2(cr, ZTOV(zp), owner,
2499 needed_bits, needed_bits));
2502 if (error && !check_privs) {
2508 if (error && (flags & V_APPEND)) {
2509 error = zfs_zaccess_append(zp, &working_mode, &check_privs, cr);
2512 if (error && check_privs) {
2513 mode_t checkmode = 0;
2516 * First check for implicit owner permission on
2517 * read_acl/read_attributes
2521 ASSERT(working_mode != 0);
2523 if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) &&
2524 owner == crgetuid(cr)))
2525 working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES);
2527 if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS|
2528 ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_SYNCHRONIZE))
2530 if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS|
2531 ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES|ACE_SYNCHRONIZE))
2532 checkmode |= VWRITE;
2533 if (working_mode & ACE_EXECUTE)
2536 error = secpolicy_vnode_access2(cr, ZTOV(check_zp), owner,
2537 needed_bits & ~checkmode, needed_bits);
2539 if (error == 0 && (working_mode & ACE_WRITE_OWNER))
2540 error = secpolicy_vnode_chown(ZTOV(check_zp), cr, owner);
2541 if (error == 0 && (working_mode & ACE_WRITE_ACL))
2542 error = secpolicy_vnode_setdac(ZTOV(check_zp), cr, owner);
2544 if (error == 0 && (working_mode &
2545 (ACE_DELETE|ACE_DELETE_CHILD)))
2546 error = secpolicy_vnode_remove(ZTOV(check_zp), cr);
2548 if (error == 0 && (working_mode & ACE_SYNCHRONIZE)) {
2549 error = secpolicy_vnode_chown(ZTOV(check_zp), cr, owner);
2553 * See if any bits other than those already checked
2554 * for are still present. If so then return EACCES
2556 if (working_mode & ~(ZFS_CHECKED_MASKS)) {
2560 } else if (error == 0) {
2561 error = secpolicy_vnode_access2(cr, ZTOV(zp), owner,
2562 needed_bits, needed_bits);
2573 * Translate traditional unix VREAD/VWRITE/VEXEC mode into
2574 * native ACL format and call zfs_zaccess()
2577 zfs_zaccess_rwx(znode_t *zp, mode_t mode, int flags, cred_t *cr)
2579 return (zfs_zaccess(zp, zfs_unix_to_v4(mode >> 6), flags, B_FALSE, cr));
2583 * Access function for secpolicy_vnode_setattr
2586 zfs_zaccess_unix(znode_t *zp, mode_t mode, cred_t *cr)
2588 int v4_mode = zfs_unix_to_v4(mode >> 6);
2590 return (zfs_zaccess(zp, v4_mode, 0, B_FALSE, cr));
2594 zfs_delete_final_check(znode_t *zp, znode_t *dzp,
2595 mode_t available_perms, cred_t *cr)
2600 downer = zfs_fuid_map_id(dzp->z_zfsvfs, dzp->z_uid, cr, ZFS_OWNER);
2602 error = secpolicy_vnode_access2(cr, ZTOV(dzp),
2603 downer, available_perms, VWRITE|VEXEC);
2606 error = zfs_sticky_remove_access(dzp, zp, cr);
2612 * Determine whether Access should be granted/deny, without
2613 * consulting least priv subsystem.
2616 * The following chart is the recommended NFSv4 enforcement for
2617 * ability to delete an object.
2619 * -------------------------------------------------------
2620 * | Parent Dir | Target Object Permissions |
2622 * -------------------------------------------------------
2623 * | | ACL Allows | ACL Denies| Delete |
2624 * | | Delete | Delete | unspecified|
2625 * -------------------------------------------------------
2626 * | ACL Allows | Permit | Permit | Permit |
2627 * | DELETE_CHILD | |
2628 * -------------------------------------------------------
2629 * | ACL Denies | Permit | Deny | Deny |
2630 * | DELETE_CHILD | | | |
2631 * -------------------------------------------------------
2632 * | ACL specifies | | | |
2633 * | only allow | Permit | Permit | Permit |
2634 * | write and | | | |
2636 * -------------------------------------------------------
2637 * | ACL denies | | | |
2638 * | write and | Permit | Deny | Deny |
2640 * -------------------------------------------------------
2643 * No search privilege, can't even look up file?
2647 zfs_zaccess_delete(znode_t *dzp, znode_t *zp, cred_t *cr)
2649 uint32_t dzp_working_mode = 0;
2650 uint32_t zp_working_mode = 0;
2651 int dzp_error, zp_error;
2652 mode_t available_perms;
2653 boolean_t dzpcheck_privs = B_TRUE;
2654 boolean_t zpcheck_privs = B_TRUE;
2657 * We want specific DELETE permissions to
2658 * take precedence over WRITE/EXECUTE. We don't
2659 * want an ACL such as this to mess us up.
2660 * user:joe:write_data:deny,user:joe:delete:allow
2662 * However, deny permissions may ultimately be overridden
2663 * by secpolicy_vnode_access().
2665 * We will ask for all of the necessary permissions and then
2666 * look at the working modes from the directory and target object
2667 * to determine what was found.
2670 if (zp->z_pflags & (ZFS_IMMUTABLE | ZFS_NOUNLINK))
2675 * If the directory permissions allow the delete, we are done.
2677 if ((dzp_error = zfs_zaccess_common(dzp, ACE_DELETE_CHILD,
2678 &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr)) == 0)
2682 * If target object has delete permission then we are done
2684 if ((zp_error = zfs_zaccess_common(zp, ACE_DELETE, &zp_working_mode,
2685 &zpcheck_privs, B_FALSE, cr)) == 0)
2688 ASSERT(dzp_error && zp_error);
2690 if (!dzpcheck_privs)
2698 * If directory returns EACCES then delete_child was denied
2699 * due to deny delete_child. In this case send the request through
2700 * secpolicy_vnode_remove(). We don't use zfs_delete_final_check()
2701 * since that *could* allow the delete based on write/execute permission
2702 * and we want delete permissions to override write/execute.
2705 if (dzp_error == EACCES)
2706 return (secpolicy_vnode_remove(ZTOV(dzp), cr)); /* XXXPJD: s/dzp/zp/ ? */
2710 * only need to see if we have write/execute on directory.
2713 dzp_error = zfs_zaccess_common(dzp, ACE_EXECUTE|ACE_WRITE_DATA,
2714 &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr);
2716 if (dzp_error != 0 && !dzpcheck_privs)
2723 available_perms = (dzp_working_mode & ACE_WRITE_DATA) ? 0 : VWRITE;
2724 available_perms |= (dzp_working_mode & ACE_EXECUTE) ? 0 : VEXEC;
2726 return (zfs_delete_final_check(zp, dzp, available_perms, cr));
2731 zfs_zaccess_rename(znode_t *sdzp, znode_t *szp, znode_t *tdzp,
2732 znode_t *tzp, cred_t *cr)
2737 if (szp->z_pflags & ZFS_AV_QUARANTINED)
2740 add_perm = (ZTOV(szp)->v_type == VDIR) ?
2741 ACE_ADD_SUBDIRECTORY : ACE_ADD_FILE;
2744 * Rename permissions are combination of delete permission +
2745 * add file/subdir permission.
2747 * BSD operating systems also require write permission
2748 * on the directory being moved from one parent directory
2751 if (ZTOV(szp)->v_type == VDIR && ZTOV(sdzp) != ZTOV(tdzp)) {
2752 if (error = zfs_zaccess(szp, ACE_WRITE_DATA, 0, B_FALSE, cr))
2757 * first make sure we do the delete portion.
2759 * If that succeeds then check for add_file/add_subdir permissions
2762 if (error = zfs_zaccess_delete(sdzp, szp, cr))
2766 * If we have a tzp, see if we can delete it?
2769 if (error = zfs_zaccess_delete(tdzp, tzp, cr))
2774 * Now check for add permissions
2776 error = zfs_zaccess(tdzp, add_perm, 0, B_FALSE, cr);