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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/resource.h>
33 #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>
44 #include <sys/policy.h>
45 #include <sys/zfs_znode.h>
46 #include <sys/zfs_fuid.h>
47 #include <sys/zfs_acl.h>
48 #include <sys/zfs_dir.h>
49 #include <sys/zfs_vfsops.h>
51 #include <sys/dnode.h>
53 #include <sys/fs/fs_subr.h>
54 #include <sys/acl/acl_common.h>
56 #define ALLOW ACE_ACCESS_ALLOWED_ACE_TYPE
57 #define DENY ACE_ACCESS_DENIED_ACE_TYPE
58 #define MAX_ACE_TYPE ACE_SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE
59 #define MIN_ACE_TYPE ALLOW
61 #define OWNING_GROUP (ACE_GROUP|ACE_IDENTIFIER_GROUP)
62 #define EVERYONE_ALLOW_MASK (ACE_READ_ACL|ACE_READ_ATTRIBUTES | \
63 ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE)
64 #define EVERYONE_DENY_MASK (ACE_WRITE_ACL|ACE_WRITE_OWNER | \
65 ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
66 #define OWNER_ALLOW_MASK (ACE_WRITE_ACL | ACE_WRITE_OWNER | \
67 ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
68 #define WRITE_MASK_DATA (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS)
70 #define ZFS_CHECKED_MASKS (ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_DATA| \
71 ACE_READ_NAMED_ATTRS|ACE_WRITE_DATA|ACE_WRITE_ATTRIBUTES| \
72 ACE_WRITE_NAMED_ATTRS|ACE_APPEND_DATA|ACE_EXECUTE|ACE_WRITE_OWNER| \
73 ACE_WRITE_ACL|ACE_DELETE|ACE_DELETE_CHILD|ACE_SYNCHRONIZE)
75 #define WRITE_MASK (WRITE_MASK_DATA|ACE_WRITE_ATTRIBUTES|ACE_WRITE_ACL|\
76 ACE_WRITE_OWNER|ACE_DELETE|ACE_DELETE_CHILD)
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|\
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 zfs_acl_version(int version
)
324 if (version
< ZPL_VERSION_FUID
)
325 return (ZFS_ACL_VERSION_INITIAL
);
327 return (ZFS_ACL_VERSION_FUID
);
331 zfs_acl_version_zp(znode_t
*zp
)
333 return (zfs_acl_version(zp
->z_zfsvfs
->z_version
));
337 zfs_acl_alloc(int vers
)
341 aclp
= kmem_zalloc(sizeof (zfs_acl_t
), KM_SLEEP
);
342 list_create(&aclp
->z_acl
, sizeof (zfs_acl_node_t
),
343 offsetof(zfs_acl_node_t
, z_next
));
344 aclp
->z_version
= vers
;
345 if (vers
== ZFS_ACL_VERSION_FUID
)
346 aclp
->z_ops
= zfs_acl_fuid_ops
;
348 aclp
->z_ops
= zfs_acl_v0_ops
;
352 static zfs_acl_node_t
*
353 zfs_acl_node_alloc(size_t bytes
)
355 zfs_acl_node_t
*aclnode
;
357 aclnode
= kmem_zalloc(sizeof (zfs_acl_node_t
), KM_SLEEP
);
359 aclnode
->z_acldata
= kmem_alloc(bytes
, KM_SLEEP
);
360 aclnode
->z_allocdata
= aclnode
->z_acldata
;
361 aclnode
->z_allocsize
= bytes
;
362 aclnode
->z_size
= bytes
;
369 zfs_acl_node_free(zfs_acl_node_t
*aclnode
)
371 if (aclnode
->z_allocsize
)
372 kmem_free(aclnode
->z_allocdata
, aclnode
->z_allocsize
);
373 kmem_free(aclnode
, sizeof (zfs_acl_node_t
));
377 zfs_acl_release_nodes(zfs_acl_t
*aclp
)
379 zfs_acl_node_t
*aclnode
;
381 while (aclnode
= list_head(&aclp
->z_acl
)) {
382 list_remove(&aclp
->z_acl
, aclnode
);
383 zfs_acl_node_free(aclnode
);
385 aclp
->z_acl_count
= 0;
386 aclp
->z_acl_bytes
= 0;
390 zfs_acl_free(zfs_acl_t
*aclp
)
392 zfs_acl_release_nodes(aclp
);
393 list_destroy(&aclp
->z_acl
);
394 kmem_free(aclp
, sizeof (zfs_acl_t
));
398 zfs_acl_valid_ace_type(uint_t type
, uint_t flags
)
405 case ACE_SYSTEM_AUDIT_ACE_TYPE
:
406 case ACE_SYSTEM_ALARM_ACE_TYPE
:
407 entry_type
= flags
& ACE_TYPE_FLAGS
;
408 return (entry_type
== ACE_OWNER
||
409 entry_type
== OWNING_GROUP
||
410 entry_type
== ACE_EVERYONE
|| entry_type
== 0 ||
411 entry_type
== ACE_IDENTIFIER_GROUP
);
413 if (type
>= MIN_ACE_TYPE
&& type
<= MAX_ACE_TYPE
)
420 zfs_ace_valid(vtype_t obj_type
, zfs_acl_t
*aclp
, uint16_t type
, uint16_t iflags
)
423 * first check type of entry
426 if (!zfs_acl_valid_ace_type(type
, iflags
))
430 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
431 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
432 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
433 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
434 if (aclp
->z_version
< ZFS_ACL_VERSION_FUID
)
436 aclp
->z_hints
|= ZFS_ACL_OBJ_ACE
;
440 * next check inheritance level flags
443 if (obj_type
== VDIR
&&
444 (iflags
& (ACE_FILE_INHERIT_ACE
|ACE_DIRECTORY_INHERIT_ACE
)))
445 aclp
->z_hints
|= ZFS_INHERIT_ACE
;
447 if (iflags
& (ACE_INHERIT_ONLY_ACE
|ACE_NO_PROPAGATE_INHERIT_ACE
)) {
448 if ((iflags
& (ACE_FILE_INHERIT_ACE
|
449 ACE_DIRECTORY_INHERIT_ACE
)) == 0) {
458 zfs_acl_next_ace(zfs_acl_t
*aclp
, void *start
, uint64_t *who
,
459 uint32_t *access_mask
, uint16_t *iflags
, uint16_t *type
)
461 zfs_acl_node_t
*aclnode
;
464 aclnode
= list_head(&aclp
->z_acl
);
468 aclp
->z_next_ace
= aclnode
->z_acldata
;
469 aclp
->z_curr_node
= aclnode
;
470 aclnode
->z_ace_idx
= 0;
473 aclnode
= aclp
->z_curr_node
;
478 if (aclnode
->z_ace_idx
>= aclnode
->z_ace_count
) {
479 aclnode
= list_next(&aclp
->z_acl
, aclnode
);
483 aclp
->z_curr_node
= aclnode
;
484 aclnode
->z_ace_idx
= 0;
485 aclp
->z_next_ace
= aclnode
->z_acldata
;
489 if (aclnode
->z_ace_idx
< aclnode
->z_ace_count
) {
490 void *acep
= aclp
->z_next_ace
;
494 * Make sure we don't overstep our bounds
496 ace_size
= aclp
->z_ops
.ace_size(acep
);
498 if (((caddr_t
)acep
+ ace_size
) >
499 ((caddr_t
)aclnode
->z_acldata
+ aclnode
->z_size
)) {
503 *iflags
= aclp
->z_ops
.ace_flags_get(acep
);
504 *type
= aclp
->z_ops
.ace_type_get(acep
);
505 *access_mask
= aclp
->z_ops
.ace_mask_get(acep
);
506 *who
= aclp
->z_ops
.ace_who_get(acep
);
507 aclp
->z_next_ace
= (caddr_t
)aclp
->z_next_ace
+ ace_size
;
508 aclnode
->z_ace_idx
++;
509 return ((void *)acep
);
516 zfs_ace_walk(void *datap
, uint64_t cookie
, int aclcnt
,
517 uint16_t *flags
, uint16_t *type
, uint32_t *mask
)
519 zfs_acl_t
*aclp
= datap
;
520 zfs_ace_hdr_t
*acep
= (zfs_ace_hdr_t
*)(uintptr_t)cookie
;
523 acep
= zfs_acl_next_ace(aclp
, acep
, &who
, mask
,
525 return ((uint64_t)(uintptr_t)acep
);
528 static zfs_acl_node_t
*
529 zfs_acl_curr_node(zfs_acl_t
*aclp
)
531 ASSERT(aclp
->z_curr_node
);
532 return (aclp
->z_curr_node
);
536 * Copy ACE to internal ZFS format.
537 * While processing the ACL each ACE will be validated for correctness.
538 * ACE FUIDs will be created later.
541 zfs_copy_ace_2_fuid(vtype_t obj_type
, zfs_acl_t
*aclp
, void *datap
,
542 zfs_ace_t
*z_acl
, int aclcnt
, size_t *size
)
546 zfs_ace_t
*aceptr
= z_acl
;
548 zfs_object_ace_t
*zobjacep
;
549 ace_object_t
*aceobjp
;
551 for (i
= 0; i
!= aclcnt
; i
++) {
552 aceptr
->z_hdr
.z_access_mask
= acep
->a_access_mask
;
553 aceptr
->z_hdr
.z_flags
= acep
->a_flags
;
554 aceptr
->z_hdr
.z_type
= acep
->a_type
;
555 entry_type
= aceptr
->z_hdr
.z_flags
& ACE_TYPE_FLAGS
;
556 if (entry_type
!= ACE_OWNER
&& entry_type
!= OWNING_GROUP
&&
557 entry_type
!= ACE_EVERYONE
) {
558 if (!aclp
->z_has_fuids
)
559 aclp
->z_has_fuids
= IS_EPHEMERAL(acep
->a_who
);
560 aceptr
->z_fuid
= (uint64_t)acep
->a_who
;
564 * Make sure ACE is valid
566 if (zfs_ace_valid(obj_type
, aclp
, aceptr
->z_hdr
.z_type
,
567 aceptr
->z_hdr
.z_flags
) != B_TRUE
)
570 switch (acep
->a_type
) {
571 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
572 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
573 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
574 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
575 zobjacep
= (zfs_object_ace_t
*)aceptr
;
576 aceobjp
= (ace_object_t
*)acep
;
578 bcopy(aceobjp
->a_obj_type
, zobjacep
->z_object_type
,
579 sizeof (aceobjp
->a_obj_type
));
580 bcopy(aceobjp
->a_inherit_obj_type
,
581 zobjacep
->z_inherit_type
,
582 sizeof (aceobjp
->a_inherit_obj_type
));
583 acep
= (ace_t
*)((caddr_t
)acep
+ sizeof (ace_object_t
));
586 acep
= (ace_t
*)((caddr_t
)acep
+ sizeof (ace_t
));
589 aceptr
= (zfs_ace_t
*)((caddr_t
)aceptr
+
590 aclp
->z_ops
.ace_size(aceptr
));
593 *size
= (caddr_t
)aceptr
- (caddr_t
)z_acl
;
599 * Copy ZFS ACEs to fixed size ace_t layout
602 zfs_copy_fuid_2_ace(zfsvfs_t
*zfsvfs
, zfs_acl_t
*aclp
, cred_t
*cr
,
603 void *datap
, int filter
)
606 uint32_t access_mask
;
607 uint16_t iflags
, type
;
608 zfs_ace_hdr_t
*zacep
= NULL
;
610 ace_object_t
*objacep
;
611 zfs_object_ace_t
*zobjacep
;
615 while (zacep
= zfs_acl_next_ace(aclp
, zacep
,
616 &who
, &access_mask
, &iflags
, &type
)) {
619 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
620 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
621 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
622 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
626 zobjacep
= (zfs_object_ace_t
*)zacep
;
627 objacep
= (ace_object_t
*)acep
;
628 bcopy(zobjacep
->z_object_type
,
630 sizeof (zobjacep
->z_object_type
));
631 bcopy(zobjacep
->z_inherit_type
,
632 objacep
->a_inherit_obj_type
,
633 sizeof (zobjacep
->z_inherit_type
));
634 ace_size
= sizeof (ace_object_t
);
637 ace_size
= sizeof (ace_t
);
641 entry_type
= (iflags
& ACE_TYPE_FLAGS
);
642 if ((entry_type
!= ACE_OWNER
&&
643 entry_type
!= OWNING_GROUP
&&
644 entry_type
!= ACE_EVERYONE
)) {
645 acep
->a_who
= zfs_fuid_map_id(zfsvfs
, who
,
646 cr
, (entry_type
& ACE_IDENTIFIER_GROUP
) ?
647 ZFS_ACE_GROUP
: ZFS_ACE_USER
);
649 acep
->a_who
= (uid_t
)(int64_t)who
;
651 acep
->a_access_mask
= access_mask
;
652 acep
->a_flags
= iflags
;
654 acep
= (ace_t
*)((caddr_t
)acep
+ ace_size
);
659 zfs_copy_ace_2_oldace(vtype_t obj_type
, zfs_acl_t
*aclp
, ace_t
*acep
,
660 zfs_oldace_t
*z_acl
, int aclcnt
, size_t *size
)
663 zfs_oldace_t
*aceptr
= z_acl
;
665 for (i
= 0; i
!= aclcnt
; i
++, aceptr
++) {
666 aceptr
->z_access_mask
= acep
[i
].a_access_mask
;
667 aceptr
->z_type
= acep
[i
].a_type
;
668 aceptr
->z_flags
= acep
[i
].a_flags
;
669 aceptr
->z_fuid
= acep
[i
].a_who
;
671 * Make sure ACE is valid
673 if (zfs_ace_valid(obj_type
, aclp
, aceptr
->z_type
,
674 aceptr
->z_flags
) != B_TRUE
)
677 *size
= (caddr_t
)aceptr
- (caddr_t
)z_acl
;
682 * convert old ACL format to new
685 zfs_acl_xform(znode_t
*zp
, zfs_acl_t
*aclp
)
687 zfs_oldace_t
*oldaclp
;
689 uint16_t type
, iflags
;
690 uint32_t access_mask
;
693 zfs_acl_node_t
*newaclnode
;
695 ASSERT(aclp
->z_version
== ZFS_ACL_VERSION_INITIAL
);
697 * First create the ACE in a contiguous piece of memory
698 * for zfs_copy_ace_2_fuid().
700 * We only convert an ACL once, so this won't happen
703 oldaclp
= kmem_alloc(sizeof (zfs_oldace_t
) * aclp
->z_acl_count
,
706 while (cookie
= zfs_acl_next_ace(aclp
, cookie
, &who
,
707 &access_mask
, &iflags
, &type
)) {
708 oldaclp
[i
].z_flags
= iflags
;
709 oldaclp
[i
].z_type
= type
;
710 oldaclp
[i
].z_fuid
= who
;
711 oldaclp
[i
++].z_access_mask
= access_mask
;
714 newaclnode
= zfs_acl_node_alloc(aclp
->z_acl_count
*
715 sizeof (zfs_object_ace_t
));
716 aclp
->z_ops
= zfs_acl_fuid_ops
;
717 VERIFY(zfs_copy_ace_2_fuid(ZTOV(zp
)->v_type
, aclp
, oldaclp
,
718 newaclnode
->z_acldata
, aclp
->z_acl_count
,
719 &newaclnode
->z_size
) == 0);
720 newaclnode
->z_ace_count
= aclp
->z_acl_count
;
721 aclp
->z_version
= ZFS_ACL_VERSION
;
722 kmem_free(oldaclp
, aclp
->z_acl_count
* sizeof (zfs_oldace_t
));
725 * Release all previous ACL nodes
728 zfs_acl_release_nodes(aclp
);
730 list_insert_head(&aclp
->z_acl
, newaclnode
);
732 aclp
->z_acl_bytes
= newaclnode
->z_size
;
733 aclp
->z_acl_count
= newaclnode
->z_ace_count
;
738 * Convert unix access mask to v4 access mask
741 zfs_unix_to_v4(uint32_t access_mask
)
743 uint32_t new_mask
= 0;
745 if (access_mask
& S_IXOTH
)
746 new_mask
|= ACE_EXECUTE
;
747 if (access_mask
& S_IWOTH
)
748 new_mask
|= ACE_WRITE_DATA
;
749 if (access_mask
& S_IROTH
)
750 new_mask
|= ACE_READ_DATA
;
755 zfs_set_ace(zfs_acl_t
*aclp
, void *acep
, uint32_t access_mask
,
756 uint16_t access_type
, uint64_t fuid
, uint16_t entry_type
)
758 uint16_t type
= entry_type
& ACE_TYPE_FLAGS
;
760 aclp
->z_ops
.ace_mask_set(acep
, access_mask
);
761 aclp
->z_ops
.ace_type_set(acep
, access_type
);
762 aclp
->z_ops
.ace_flags_set(acep
, entry_type
);
763 if ((type
!= ACE_OWNER
&& type
!= OWNING_GROUP
&&
764 type
!= ACE_EVERYONE
))
765 aclp
->z_ops
.ace_who_set(acep
, fuid
);
769 * Determine mode of file based on ACL.
770 * Also, create FUIDs for any User/Group ACEs
773 zfs_mode_fuid_compute(znode_t
*zp
, zfs_acl_t
*aclp
, cred_t
*cr
,
774 zfs_fuid_info_t
**fuidp
, dmu_tx_t
*tx
)
779 zfs_ace_hdr_t
*acep
= NULL
;
781 uint16_t iflags
, type
;
782 uint32_t access_mask
;
784 mode
= (zp
->z_phys
->zp_mode
& (S_IFMT
| S_ISUID
| S_ISGID
| S_ISVTX
));
786 while (acep
= zfs_acl_next_ace(aclp
, acep
, &who
,
787 &access_mask
, &iflags
, &type
)) {
789 if (!zfs_acl_valid_ace_type(type
, iflags
))
792 entry_type
= (iflags
& ACE_TYPE_FLAGS
);
795 * Skip over owner@, group@ or everyone@ inherit only ACEs
797 if ((iflags
& ACE_INHERIT_ONLY_ACE
) &&
798 (entry_type
== ACE_OWNER
|| entry_type
== ACE_EVERYONE
||
799 entry_type
== OWNING_GROUP
))
802 if (entry_type
== ACE_OWNER
) {
803 if ((access_mask
& ACE_READ_DATA
) &&
804 (!(seen
& S_IRUSR
))) {
810 if ((access_mask
& ACE_WRITE_DATA
) &&
811 (!(seen
& S_IWUSR
))) {
817 if ((access_mask
& ACE_EXECUTE
) &&
818 (!(seen
& S_IXUSR
))) {
824 } else if (entry_type
== OWNING_GROUP
) {
825 if ((access_mask
& ACE_READ_DATA
) &&
826 (!(seen
& S_IRGRP
))) {
832 if ((access_mask
& ACE_WRITE_DATA
) &&
833 (!(seen
& S_IWGRP
))) {
839 if ((access_mask
& ACE_EXECUTE
) &&
840 (!(seen
& S_IXGRP
))) {
846 } else if (entry_type
== ACE_EVERYONE
) {
847 if ((access_mask
& ACE_READ_DATA
)) {
848 if (!(seen
& S_IRUSR
)) {
854 if (!(seen
& S_IRGRP
)) {
860 if (!(seen
& S_IROTH
)) {
867 if ((access_mask
& ACE_WRITE_DATA
)) {
868 if (!(seen
& S_IWUSR
)) {
874 if (!(seen
& S_IWGRP
)) {
880 if (!(seen
& S_IWOTH
)) {
887 if ((access_mask
& ACE_EXECUTE
)) {
888 if (!(seen
& S_IXUSR
)) {
894 if (!(seen
& S_IXGRP
)) {
900 if (!(seen
& S_IXOTH
)) {
909 * Now handle FUID create for user/group ACEs
911 if (entry_type
== 0 || entry_type
== ACE_IDENTIFIER_GROUP
) {
912 aclp
->z_ops
.ace_who_set(acep
,
913 zfs_fuid_create(zp
->z_zfsvfs
, who
, cr
,
914 (entry_type
== 0) ? ZFS_ACE_USER
: ZFS_ACE_GROUP
,
922 zfs_acl_node_read_internal(znode_t
*zp
, boolean_t will_modify
)
925 zfs_acl_node_t
*aclnode
;
927 aclp
= zfs_acl_alloc(zp
->z_phys
->zp_acl
.z_acl_version
);
930 * Version 0 to 1 znode_acl_phys has the size/count fields swapped.
931 * Version 0 didn't have a size field, only a count.
933 if (zp
->z_phys
->zp_acl
.z_acl_version
== ZFS_ACL_VERSION_INITIAL
) {
934 aclp
->z_acl_count
= zp
->z_phys
->zp_acl
.z_acl_size
;
935 aclp
->z_acl_bytes
= ZFS_ACL_SIZE(aclp
->z_acl_count
);
937 aclp
->z_acl_count
= zp
->z_phys
->zp_acl
.z_acl_count
;
938 aclp
->z_acl_bytes
= zp
->z_phys
->zp_acl
.z_acl_size
;
941 aclnode
= zfs_acl_node_alloc(will_modify
? aclp
->z_acl_bytes
: 0);
942 aclnode
->z_ace_count
= aclp
->z_acl_count
;
944 bcopy(zp
->z_phys
->zp_acl
.z_ace_data
, aclnode
->z_acldata
,
947 aclnode
->z_size
= aclp
->z_acl_bytes
;
948 aclnode
->z_acldata
= &zp
->z_phys
->zp_acl
.z_ace_data
[0];
951 list_insert_head(&aclp
->z_acl
, aclnode
);
957 * Read an external acl object.
960 zfs_acl_node_read(znode_t
*zp
, zfs_acl_t
**aclpp
, boolean_t will_modify
)
962 uint64_t extacl
= zp
->z_phys
->zp_acl
.z_acl_extern_obj
;
966 zfs_acl_node_t
*aclnode
;
969 ASSERT(MUTEX_HELD(&zp
->z_acl_lock
));
971 if (zp
->z_phys
->zp_acl
.z_acl_extern_obj
== 0) {
972 *aclpp
= zfs_acl_node_read_internal(zp
, will_modify
);
976 aclp
= zfs_acl_alloc(zp
->z_phys
->zp_acl
.z_acl_version
);
977 if (zp
->z_phys
->zp_acl
.z_acl_version
== ZFS_ACL_VERSION_INITIAL
) {
978 zfs_acl_phys_v0_t
*zacl0
=
979 (zfs_acl_phys_v0_t
*)&zp
->z_phys
->zp_acl
;
981 aclsize
= ZFS_ACL_SIZE(zacl0
->z_acl_count
);
982 acl_count
= zacl0
->z_acl_count
;
984 aclsize
= zp
->z_phys
->zp_acl
.z_acl_size
;
985 acl_count
= zp
->z_phys
->zp_acl
.z_acl_count
;
987 aclsize
= acl_count
* sizeof (zfs_ace_t
);
989 aclnode
= zfs_acl_node_alloc(aclsize
);
990 list_insert_head(&aclp
->z_acl
, aclnode
);
991 error
= dmu_read(zp
->z_zfsvfs
->z_os
, extacl
, 0,
992 aclsize
, aclnode
->z_acldata
);
993 aclnode
->z_ace_count
= acl_count
;
994 aclp
->z_acl_count
= acl_count
;
995 aclp
->z_acl_bytes
= aclsize
;
999 /* convert checksum errors into IO errors */
1000 if (error
== ECKSUM
)
1010 * common code for setting ACLs.
1012 * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl.
1013 * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's
1014 * already checked the acl and knows whether to inherit.
1017 zfs_aclset_common(znode_t
*zp
, zfs_acl_t
*aclp
, cred_t
*cr
,
1018 zfs_fuid_info_t
**fuidp
, dmu_tx_t
*tx
)
1021 znode_phys_t
*zphys
= zp
->z_phys
;
1022 zfs_acl_phys_t
*zacl
= &zphys
->zp_acl
;
1023 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1024 uint64_t aoid
= zphys
->zp_acl
.z_acl_extern_obj
;
1026 dmu_object_type_t otype
;
1027 zfs_acl_node_t
*aclnode
;
1029 ASSERT(MUTEX_HELD(&zp
->z_lock
));
1030 ASSERT(MUTEX_HELD(&zp
->z_acl_lock
));
1032 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
1034 zphys
->zp_mode
= zfs_mode_fuid_compute(zp
, aclp
, cr
, fuidp
, tx
);
1037 * Decide which opbject type to use. If we are forced to
1038 * use old ACL format than transform ACL into zfs_oldace_t
1041 if (!zfsvfs
->z_use_fuids
) {
1042 otype
= DMU_OT_OLDACL
;
1044 if ((aclp
->z_version
== ZFS_ACL_VERSION_INITIAL
) &&
1045 (zfsvfs
->z_version
>= ZPL_VERSION_FUID
))
1046 zfs_acl_xform(zp
, aclp
);
1047 ASSERT(aclp
->z_version
>= ZFS_ACL_VERSION_FUID
);
1051 if (aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1053 * If ACL was previously external and we are now
1054 * converting to new ACL format then release old
1055 * ACL object and create a new one.
1057 if (aoid
&& aclp
->z_version
!= zacl
->z_acl_version
) {
1058 error
= dmu_object_free(zfsvfs
->z_os
,
1059 zp
->z_phys
->zp_acl
.z_acl_extern_obj
, tx
);
1065 aoid
= dmu_object_alloc(zfsvfs
->z_os
,
1066 otype
, aclp
->z_acl_bytes
,
1067 otype
== DMU_OT_ACL
? DMU_OT_SYSACL
: DMU_OT_NONE
,
1068 otype
== DMU_OT_ACL
? DN_MAX_BONUSLEN
: 0, tx
);
1070 (void) dmu_object_set_blocksize(zfsvfs
->z_os
, aoid
,
1071 aclp
->z_acl_bytes
, 0, tx
);
1073 zphys
->zp_acl
.z_acl_extern_obj
= aoid
;
1074 for (aclnode
= list_head(&aclp
->z_acl
); aclnode
;
1075 aclnode
= list_next(&aclp
->z_acl
, aclnode
)) {
1076 if (aclnode
->z_ace_count
== 0)
1078 dmu_write(zfsvfs
->z_os
, aoid
, off
,
1079 aclnode
->z_size
, aclnode
->z_acldata
, tx
);
1080 off
+= aclnode
->z_size
;
1083 void *start
= zacl
->z_ace_data
;
1085 * Migrating back embedded?
1087 if (zphys
->zp_acl
.z_acl_extern_obj
) {
1088 error
= dmu_object_free(zfsvfs
->z_os
,
1089 zp
->z_phys
->zp_acl
.z_acl_extern_obj
, tx
);
1092 zphys
->zp_acl
.z_acl_extern_obj
= 0;
1095 for (aclnode
= list_head(&aclp
->z_acl
); aclnode
;
1096 aclnode
= list_next(&aclp
->z_acl
, aclnode
)) {
1097 if (aclnode
->z_ace_count
== 0)
1099 bcopy(aclnode
->z_acldata
, start
, aclnode
->z_size
);
1100 start
= (caddr_t
)start
+ aclnode
->z_size
;
1105 * If Old version then swap count/bytes to match old
1106 * layout of znode_acl_phys_t.
1108 if (aclp
->z_version
== ZFS_ACL_VERSION_INITIAL
) {
1109 zphys
->zp_acl
.z_acl_size
= aclp
->z_acl_count
;
1110 zphys
->zp_acl
.z_acl_count
= aclp
->z_acl_bytes
;
1112 zphys
->zp_acl
.z_acl_size
= aclp
->z_acl_bytes
;
1113 zphys
->zp_acl
.z_acl_count
= aclp
->z_acl_count
;
1116 zphys
->zp_acl
.z_acl_version
= aclp
->z_version
;
1119 * Replace ACL wide bits, but first clear them.
1121 zp
->z_phys
->zp_flags
&= ~ZFS_ACL_WIDE_FLAGS
;
1123 zp
->z_phys
->zp_flags
|= aclp
->z_hints
;
1125 if (ace_trivial_common(aclp
, 0, zfs_ace_walk
) == 0)
1126 zp
->z_phys
->zp_flags
|= ZFS_ACL_TRIVIAL
;
1128 zfs_time_stamper_locked(zp
, STATE_CHANGED
, tx
);
1133 * Update access mask for prepended ACE
1135 * This applies the "groupmask" value for aclmode property.
1138 zfs_acl_prepend_fixup(zfs_acl_t
*aclp
, void *acep
, void *origacep
,
1139 mode_t mode
, uint64_t owner
)
1141 int rmask
, wmask
, xmask
;
1144 uint32_t origmask
, acepmask
;
1147 aceflags
= aclp
->z_ops
.ace_flags_get(acep
);
1148 fuid
= aclp
->z_ops
.ace_who_get(acep
);
1149 origmask
= aclp
->z_ops
.ace_mask_get(origacep
);
1150 acepmask
= aclp
->z_ops
.ace_mask_get(acep
);
1152 user_ace
= (!(aceflags
&
1153 (ACE_OWNER
|ACE_GROUP
|ACE_IDENTIFIER_GROUP
)));
1155 if (user_ace
&& (fuid
== owner
)) {
1165 if (origmask
& ACE_READ_DATA
) {
1167 acepmask
&= ~ACE_READ_DATA
;
1169 acepmask
|= ACE_READ_DATA
;
1173 if (origmask
& ACE_WRITE_DATA
) {
1175 acepmask
&= ~ACE_WRITE_DATA
;
1177 acepmask
|= ACE_WRITE_DATA
;
1181 if (origmask
& ACE_APPEND_DATA
) {
1183 acepmask
&= ~ACE_APPEND_DATA
;
1185 acepmask
|= ACE_APPEND_DATA
;
1189 if (origmask
& ACE_EXECUTE
) {
1191 acepmask
&= ~ACE_EXECUTE
;
1193 acepmask
|= ACE_EXECUTE
;
1196 aclp
->z_ops
.ace_mask_set(acep
, acepmask
);
1200 * Apply mode to canonical six ACEs.
1203 zfs_acl_fixup_canonical_six(zfs_acl_t
*aclp
, mode_t mode
)
1205 zfs_acl_node_t
*aclnode
= list_tail(&aclp
->z_acl
);
1207 int maskoff
= aclp
->z_ops
.ace_mask_off();
1208 size_t abstract_size
= aclp
->z_ops
.ace_abstract_size();
1210 ASSERT(aclnode
!= NULL
);
1212 acep
= (void *)((caddr_t
)aclnode
->z_acldata
+
1213 aclnode
->z_size
- (abstract_size
* 6));
1216 * Fixup final ACEs to match the mode
1219 adjust_ace_pair_common(acep
, maskoff
, abstract_size
,
1220 (mode
& 0700) >> 6); /* owner@ */
1222 acep
= (caddr_t
)acep
+ (abstract_size
* 2);
1224 adjust_ace_pair_common(acep
, maskoff
, abstract_size
,
1225 (mode
& 0070) >> 3); /* group@ */
1227 acep
= (caddr_t
)acep
+ (abstract_size
* 2);
1228 adjust_ace_pair_common(acep
, maskoff
,
1229 abstract_size
, mode
); /* everyone@ */
1234 zfs_acl_ace_match(zfs_acl_t
*aclp
, void *acep
, int allow_deny
,
1235 int entry_type
, int accessmask
)
1237 uint32_t mask
= aclp
->z_ops
.ace_mask_get(acep
);
1238 uint16_t type
= aclp
->z_ops
.ace_type_get(acep
);
1239 uint16_t flags
= aclp
->z_ops
.ace_flags_get(acep
);
1241 return (mask
== accessmask
&& type
== allow_deny
&&
1242 ((flags
& ACE_TYPE_FLAGS
) == entry_type
));
1246 * Can prepended ACE be reused?
1249 zfs_reuse_deny(zfs_acl_t
*aclp
, void *acep
, void *prevacep
)
1255 uint32_t mask
, prevmask
;
1257 if (prevacep
== NULL
)
1260 prevtype
= aclp
->z_ops
.ace_type_get(prevacep
);
1261 prevflags
= aclp
->z_ops
.ace_flags_get(prevacep
);
1262 flags
= aclp
->z_ops
.ace_flags_get(acep
);
1263 mask
= aclp
->z_ops
.ace_mask_get(acep
);
1264 prevmask
= aclp
->z_ops
.ace_mask_get(prevacep
);
1266 if (prevtype
!= DENY
)
1269 if (prevflags
!= (flags
& ACE_IDENTIFIER_GROUP
))
1272 okay_masks
= (mask
& OKAY_MASK_BITS
);
1274 if (prevmask
& ~okay_masks
)
1282 * Insert new ACL node into chain of zfs_acl_node_t's
1284 * This will result in two possible results.
1285 * 1. If the ACL is currently just a single zfs_acl_node and
1286 * we are prepending the entry then current acl node will have
1287 * a new node inserted above it.
1289 * 2. If we are inserting in the middle of current acl node then
1290 * the current node will be split in two and new node will be inserted
1291 * in between the two split nodes.
1293 static zfs_acl_node_t
*
1294 zfs_acl_ace_insert(zfs_acl_t
*aclp
, void *acep
)
1296 zfs_acl_node_t
*newnode
;
1297 zfs_acl_node_t
*trailernode
= NULL
;
1298 zfs_acl_node_t
*currnode
= zfs_acl_curr_node(aclp
);
1299 int curr_idx
= aclp
->z_curr_node
->z_ace_idx
;
1303 newnode
= zfs_acl_node_alloc(aclp
->z_ops
.ace_size(acep
));
1304 newnode
->z_ace_count
= 1;
1306 oldsize
= currnode
->z_size
;
1308 if (curr_idx
!= 1) {
1309 trailernode
= zfs_acl_node_alloc(0);
1310 trailernode
->z_acldata
= acep
;
1312 trailer_count
= currnode
->z_ace_count
- curr_idx
+ 1;
1313 currnode
->z_ace_count
= curr_idx
- 1;
1314 currnode
->z_size
= (caddr_t
)acep
- (caddr_t
)currnode
->z_acldata
;
1315 trailernode
->z_size
= oldsize
- currnode
->z_size
;
1316 trailernode
->z_ace_count
= trailer_count
;
1319 aclp
->z_acl_count
+= 1;
1320 aclp
->z_acl_bytes
+= aclp
->z_ops
.ace_size(acep
);
1323 list_insert_before(&aclp
->z_acl
, currnode
, newnode
);
1325 list_insert_after(&aclp
->z_acl
, currnode
, newnode
);
1327 list_insert_after(&aclp
->z_acl
, newnode
, trailernode
);
1328 aclp
->z_curr_node
= trailernode
;
1329 trailernode
->z_ace_idx
= 1;
1339 zfs_acl_prepend_deny(znode_t
*zp
, zfs_acl_t
*aclp
, void *acep
,
1342 zfs_acl_node_t
*aclnode
;
1347 aclnode
= zfs_acl_ace_insert(aclp
, acep
);
1348 newacep
= aclnode
->z_acldata
;
1349 fuid
= aclp
->z_ops
.ace_who_get(acep
);
1350 flags
= aclp
->z_ops
.ace_flags_get(acep
);
1351 zfs_set_ace(aclp
, newacep
, 0, DENY
, fuid
, (flags
& ACE_TYPE_FLAGS
));
1352 zfs_acl_prepend_fixup(aclp
, newacep
, acep
, mode
, zp
->z_phys
->zp_uid
);
1358 * Split an inherited ACE into inherit_only ACE
1359 * and original ACE with inheritance flags stripped off.
1362 zfs_acl_split_ace(zfs_acl_t
*aclp
, zfs_ace_hdr_t
*acep
)
1364 zfs_acl_node_t
*aclnode
;
1365 zfs_acl_node_t
*currnode
;
1367 uint16_t type
, flags
;
1371 type
= aclp
->z_ops
.ace_type_get(acep
);
1372 flags
= aclp
->z_ops
.ace_flags_get(acep
);
1373 mask
= aclp
->z_ops
.ace_mask_get(acep
);
1374 fuid
= aclp
->z_ops
.ace_who_get(acep
);
1376 aclnode
= zfs_acl_ace_insert(aclp
, acep
);
1377 newacep
= aclnode
->z_acldata
;
1379 aclp
->z_ops
.ace_type_set(newacep
, type
);
1380 aclp
->z_ops
.ace_flags_set(newacep
, flags
| ACE_INHERIT_ONLY_ACE
);
1381 aclp
->z_ops
.ace_mask_set(newacep
, mask
);
1382 aclp
->z_ops
.ace_type_set(newacep
, type
);
1383 aclp
->z_ops
.ace_who_set(newacep
, fuid
);
1384 aclp
->z_next_ace
= acep
;
1385 flags
&= ~ALL_INHERIT
;
1386 aclp
->z_ops
.ace_flags_set(acep
, flags
);
1387 currnode
= zfs_acl_curr_node(aclp
);
1388 ASSERT(currnode
->z_ace_idx
>= 1);
1389 currnode
->z_ace_idx
-= 1;
1393 * Are ACES started at index i, the canonical six ACES?
1396 zfs_have_canonical_six(zfs_acl_t
*aclp
)
1399 zfs_acl_node_t
*aclnode
= list_tail(&aclp
->z_acl
);
1401 size_t abstract_size
= aclp
->z_ops
.ace_abstract_size();
1403 ASSERT(aclnode
!= NULL
);
1405 if (aclnode
->z_ace_count
< 6)
1408 acep
= (void *)((caddr_t
)aclnode
->z_acldata
+
1409 aclnode
->z_size
- (aclp
->z_ops
.ace_abstract_size() * 6));
1411 if ((zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++),
1412 DENY
, ACE_OWNER
, 0) &&
1413 zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++),
1414 ALLOW
, ACE_OWNER
, OWNER_ALLOW_MASK
) &&
1415 zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++), DENY
,
1416 OWNING_GROUP
, 0) && zfs_acl_ace_match(aclp
, (caddr_t
)acep
+
1417 (abstract_size
* i
++),
1418 ALLOW
, OWNING_GROUP
, 0) &&
1419 zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++),
1420 DENY
, ACE_EVERYONE
, EVERYONE_DENY_MASK
) &&
1421 zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++),
1422 ALLOW
, ACE_EVERYONE
, EVERYONE_ALLOW_MASK
))) {
1431 * Apply step 1g, to group entries
1433 * Need to deal with corner case where group may have
1434 * greater permissions than owner. If so then limit
1435 * group permissions, based on what extra permissions
1439 zfs_fixup_group_entries(zfs_acl_t
*aclp
, void *acep
, void *prevacep
,
1442 uint32_t prevmask
= aclp
->z_ops
.ace_mask_get(prevacep
);
1443 uint32_t mask
= aclp
->z_ops
.ace_mask_get(acep
);
1444 uint16_t prevflags
= aclp
->z_ops
.ace_flags_get(prevacep
);
1445 mode_t extramode
= (mode
>> 3) & 07;
1446 mode_t ownermode
= (mode
>> 6);
1448 if (prevflags
& ACE_IDENTIFIER_GROUP
) {
1450 extramode
&= ~ownermode
;
1453 if (extramode
& S_IROTH
) {
1454 prevmask
&= ~ACE_READ_DATA
;
1455 mask
&= ~ACE_READ_DATA
;
1457 if (extramode
& S_IWOTH
) {
1458 prevmask
&= ~(ACE_WRITE_DATA
|ACE_APPEND_DATA
);
1459 mask
&= ~(ACE_WRITE_DATA
|ACE_APPEND_DATA
);
1461 if (extramode
& S_IXOTH
) {
1462 prevmask
&= ~ACE_EXECUTE
;
1463 mask
&= ~ACE_EXECUTE
;
1467 aclp
->z_ops
.ace_mask_set(acep
, mask
);
1468 aclp
->z_ops
.ace_mask_set(prevacep
, prevmask
);
1472 * Apply the chmod algorithm as described
1476 zfs_acl_chmod(znode_t
*zp
, uint64_t mode
, zfs_acl_t
*aclp
)
1478 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1479 void *acep
= NULL
, *prevacep
= NULL
;
1484 int need_canonical_six
= 1;
1485 uint16_t iflags
, type
;
1486 uint32_t access_mask
;
1488 ASSERT(MUTEX_HELD(&zp
->z_acl_lock
));
1489 ASSERT(MUTEX_HELD(&zp
->z_lock
));
1491 aclp
->z_hints
= (zp
->z_phys
->zp_flags
& V4_ACL_WIDE_FLAGS
);
1494 * If discard then just discard all ACL nodes which
1495 * represent the ACEs.
1497 * New owner@/group@/everone@ ACEs will be added
1500 if (zfsvfs
->z_acl_mode
== ZFS_ACL_DISCARD
)
1501 zfs_acl_release_nodes(aclp
);
1503 while (acep
= zfs_acl_next_ace(aclp
, acep
, &who
, &access_mask
,
1506 entry_type
= (iflags
& ACE_TYPE_FLAGS
);
1507 iflags
= (iflags
& ALL_INHERIT
);
1509 if ((type
!= ALLOW
&& type
!= DENY
) ||
1510 (iflags
& ACE_INHERIT_ONLY_ACE
)) {
1512 aclp
->z_hints
|= ZFS_INHERIT_ACE
;
1514 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
1515 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
1516 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
1517 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
1518 aclp
->z_hints
|= ZFS_ACL_OBJ_ACE
;
1525 * Need to split ace into two?
1527 if ((iflags
& (ACE_FILE_INHERIT_ACE
|
1528 ACE_DIRECTORY_INHERIT_ACE
)) &&
1529 (!(iflags
& ACE_INHERIT_ONLY_ACE
))) {
1530 zfs_acl_split_ace(aclp
, acep
);
1531 aclp
->z_hints
|= ZFS_INHERIT_ACE
;
1535 if (entry_type
== ACE_OWNER
|| entry_type
== ACE_EVERYONE
||
1536 (entry_type
== OWNING_GROUP
)) {
1537 access_mask
&= ~OGE_CLEAR
;
1538 aclp
->z_ops
.ace_mask_set(acep
, access_mask
);
1541 reuse_deny
= B_TRUE
;
1542 if (type
== ALLOW
) {
1545 * Check preceding ACE if any, to see
1546 * if we need to prepend a DENY ACE.
1547 * This is only applicable when the acl_mode
1548 * property == groupmask.
1550 if (zfsvfs
->z_acl_mode
== ZFS_ACL_GROUPMASK
) {
1552 reuse_deny
= zfs_reuse_deny(aclp
, acep
,
1557 zfs_acl_prepend_deny(zp
,
1560 zfs_acl_prepend_fixup(
1563 zp
->z_phys
->zp_uid
);
1565 zfs_fixup_group_entries(aclp
, acep
,
1576 * Check out last six aces, if we have six.
1579 if (aclp
->z_acl_count
>= 6) {
1580 if (zfs_have_canonical_six(aclp
)) {
1581 need_canonical_six
= 0;
1585 if (need_canonical_six
) {
1586 size_t abstract_size
= aclp
->z_ops
.ace_abstract_size();
1588 zfs_acl_node_t
*aclnode
=
1589 zfs_acl_node_alloc(abstract_size
* 6);
1591 aclnode
->z_size
= abstract_size
* 6;
1592 aclnode
->z_ace_count
= 6;
1593 aclp
->z_acl_bytes
+= aclnode
->z_size
;
1594 list_insert_tail(&aclp
->z_acl
, aclnode
);
1596 zacep
= aclnode
->z_acldata
;
1599 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++),
1600 0, DENY
, -1, ACE_OWNER
);
1601 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++),
1602 OWNER_ALLOW_MASK
, ALLOW
, -1, ACE_OWNER
);
1603 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++), 0,
1604 DENY
, -1, OWNING_GROUP
);
1605 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++), 0,
1606 ALLOW
, -1, OWNING_GROUP
);
1607 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++),
1608 EVERYONE_DENY_MASK
, DENY
, -1, ACE_EVERYONE
);
1609 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++),
1610 EVERYONE_ALLOW_MASK
, ALLOW
, -1, ACE_EVERYONE
);
1611 aclp
->z_acl_count
+= 6;
1614 zfs_acl_fixup_canonical_six(aclp
, mode
);
1618 zfs_acl_chmod_setattr(znode_t
*zp
, zfs_acl_t
**aclp
, uint64_t mode
)
1622 mutex_enter(&zp
->z_lock
);
1623 mutex_enter(&zp
->z_acl_lock
);
1625 error
= zfs_acl_node_read(zp
, aclp
, B_TRUE
);
1627 zfs_acl_chmod(zp
, mode
, *aclp
);
1628 mutex_exit(&zp
->z_acl_lock
);
1629 mutex_exit(&zp
->z_lock
);
1634 * strip off write_owner and write_acl
1637 zfs_restricted_update(zfsvfs_t
*zfsvfs
, zfs_acl_t
*aclp
, void *acep
)
1639 uint32_t mask
= aclp
->z_ops
.ace_mask_get(acep
);
1641 if ((zfsvfs
->z_acl_inherit
== ZFS_ACL_RESTRICTED
) &&
1642 (aclp
->z_ops
.ace_type_get(acep
) == ALLOW
)) {
1643 mask
&= ~RESTRICTED_CLEAR
;
1644 aclp
->z_ops
.ace_mask_set(acep
, mask
);
1649 * Should ACE be inherited?
1652 zfs_ace_can_use(znode_t
*zp
, uint16_t acep_flags
)
1654 int vtype
= ZTOV(zp
)->v_type
;
1655 int iflags
= (acep_flags
& 0xf);
1657 if ((vtype
== VDIR
) && (iflags
& ACE_DIRECTORY_INHERIT_ACE
))
1659 else if (iflags
& ACE_FILE_INHERIT_ACE
)
1660 return (!((vtype
== VDIR
) &&
1661 (iflags
& ACE_NO_PROPAGATE_INHERIT_ACE
)));
1666 * inherit inheritable ACEs from parent
1669 zfs_acl_inherit(znode_t
*zp
, zfs_acl_t
*paclp
, uint64_t mode
,
1670 boolean_t
*need_chmod
)
1672 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1675 zfs_acl_node_t
*aclnode
, *aclnode2
;
1676 zfs_acl_t
*aclp
= NULL
;
1678 uint32_t access_mask
;
1679 uint16_t iflags
, newflags
, type
;
1681 void *data1
, *data2
;
1682 size_t data1sz
, data2sz
;
1683 boolean_t vdir
= ZTOV(zp
)->v_type
== VDIR
;
1684 boolean_t vreg
= ZTOV(zp
)->v_type
== VREG
;
1685 boolean_t passthrough
, passthrough_x
, noallow
;
1688 zfsvfs
->z_acl_inherit
== ZFS_ACL_PASSTHROUGH_X
;
1689 passthrough
= passthrough_x
||
1690 zfsvfs
->z_acl_inherit
== ZFS_ACL_PASSTHROUGH
;
1692 zfsvfs
->z_acl_inherit
== ZFS_ACL_NOALLOW
;
1694 *need_chmod
= B_TRUE
;
1696 aclp
= zfs_acl_alloc(paclp
->z_version
);
1697 if (zfsvfs
->z_acl_inherit
== ZFS_ACL_DISCARD
)
1699 while (pacep
= zfs_acl_next_ace(paclp
, pacep
, &who
,
1700 &access_mask
, &iflags
, &type
)) {
1703 * don't inherit bogus ACEs
1705 if (!zfs_acl_valid_ace_type(type
, iflags
))
1708 if (noallow
&& type
== ALLOW
)
1711 ace_size
= aclp
->z_ops
.ace_size(pacep
);
1713 if (!zfs_ace_can_use(zp
, iflags
))
1717 * If owner@, group@, or everyone@ inheritable
1718 * then zfs_acl_chmod() isn't needed.
1721 ((iflags
& (ACE_OWNER
|ACE_EVERYONE
)) ||
1722 ((iflags
& OWNING_GROUP
) ==
1723 OWNING_GROUP
)) && (vreg
|| (vdir
&& (iflags
&
1724 ACE_DIRECTORY_INHERIT_ACE
)))) {
1725 *need_chmod
= B_FALSE
;
1727 if (!vdir
&& passthrough_x
&&
1728 ((mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)) {
1729 access_mask
&= ~ACE_EXECUTE
;
1733 aclnode
= zfs_acl_node_alloc(ace_size
);
1734 list_insert_tail(&aclp
->z_acl
, aclnode
);
1735 acep
= aclnode
->z_acldata
;
1737 zfs_set_ace(aclp
, acep
, access_mask
, type
,
1738 who
, iflags
|ACE_INHERITED_ACE
);
1741 * Copy special opaque data if any
1743 if ((data1sz
= paclp
->z_ops
.ace_data(pacep
, &data1
)) != 0) {
1744 VERIFY((data2sz
= aclp
->z_ops
.ace_data(acep
,
1745 &data2
)) == data1sz
);
1746 bcopy(data1
, data2
, data2sz
);
1748 aclp
->z_acl_count
++;
1749 aclnode
->z_ace_count
++;
1750 aclp
->z_acl_bytes
+= aclnode
->z_size
;
1751 newflags
= aclp
->z_ops
.ace_flags_get(acep
);
1754 aclp
->z_hints
|= ZFS_INHERIT_ACE
;
1756 if ((iflags
& ACE_NO_PROPAGATE_INHERIT_ACE
) || !vdir
) {
1757 newflags
&= ~ALL_INHERIT
;
1758 aclp
->z_ops
.ace_flags_set(acep
,
1759 newflags
|ACE_INHERITED_ACE
);
1760 zfs_restricted_update(zfsvfs
, aclp
, acep
);
1766 newflags
= aclp
->z_ops
.ace_flags_get(acep
);
1767 if ((iflags
& (ACE_FILE_INHERIT_ACE
|
1768 ACE_DIRECTORY_INHERIT_ACE
)) !=
1769 ACE_FILE_INHERIT_ACE
) {
1770 aclnode2
= zfs_acl_node_alloc(ace_size
);
1771 list_insert_tail(&aclp
->z_acl
, aclnode2
);
1772 acep2
= aclnode2
->z_acldata
;
1773 zfs_set_ace(aclp
, acep2
,
1774 access_mask
, type
, who
,
1775 iflags
|ACE_INHERITED_ACE
);
1776 newflags
|= ACE_INHERIT_ONLY_ACE
;
1777 aclp
->z_ops
.ace_flags_set(acep
, newflags
);
1778 newflags
&= ~ALL_INHERIT
;
1779 aclp
->z_ops
.ace_flags_set(acep2
,
1780 newflags
|ACE_INHERITED_ACE
);
1783 * Copy special opaque data if any
1785 if ((data1sz
= aclp
->z_ops
.ace_data(acep
,
1788 aclp
->z_ops
.ace_data(acep2
,
1789 &data2
)) == data1sz
);
1790 bcopy(data1
, data2
, data1sz
);
1792 aclp
->z_acl_count
++;
1793 aclnode2
->z_ace_count
++;
1794 aclp
->z_acl_bytes
+= aclnode
->z_size
;
1795 zfs_restricted_update(zfsvfs
, aclp
, acep2
);
1797 newflags
|= ACE_INHERIT_ONLY_ACE
;
1798 aclp
->z_ops
.ace_flags_set(acep
,
1799 newflags
|ACE_INHERITED_ACE
);
1806 * Create file system object initial permissions
1807 * including inheritable ACEs.
1810 zfs_perm_init(znode_t
*zp
, znode_t
*parent
, int flag
,
1811 vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
1812 zfs_acl_t
*setaclp
, zfs_fuid_info_t
**fuidp
)
1814 uint64_t mode
, fuid
, fgid
;
1816 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1817 zfs_acl_t
*aclp
= NULL
;
1819 xvattr_t
*xvap
= (xvattr_t
*)vap
;
1821 boolean_t need_chmod
= B_TRUE
;
1826 mode
= MAKEIMODE(vap
->va_type
, vap
->va_mode
);
1829 * Determine uid and gid.
1831 if ((flag
& (IS_ROOT_NODE
| IS_REPLAY
)) ||
1832 ((flag
& IS_XATTR
) && (vap
->va_type
== VDIR
))) {
1833 fuid
= zfs_fuid_create(zfsvfs
, vap
->va_uid
, cr
,
1834 ZFS_OWNER
, tx
, fuidp
);
1835 fgid
= zfs_fuid_create(zfsvfs
, vap
->va_gid
, cr
,
1836 ZFS_GROUP
, tx
, fuidp
);
1839 fuid
= zfs_fuid_create_cred(zfsvfs
, ZFS_OWNER
, tx
, cr
, fuidp
);
1841 if (vap
->va_mask
& AT_GID
) {
1842 fgid
= zfs_fuid_create(zfsvfs
, vap
->va_gid
, cr
,
1843 ZFS_GROUP
, tx
, fuidp
);
1845 if (fgid
!= parent
->z_phys
->zp_gid
&&
1846 !groupmember(vap
->va_gid
, cr
) &&
1847 secpolicy_vnode_create_gid(cr
) != 0)
1851 if (parent
->z_phys
->zp_mode
& S_ISGID
) {
1852 fgid
= parent
->z_phys
->zp_gid
;
1853 gid
= zfs_fuid_map_id(zfsvfs
, fgid
,
1856 fgid
= zfs_fuid_create_cred(zfsvfs
,
1857 ZFS_GROUP
, tx
, cr
, fuidp
);
1864 * If we're creating a directory, and the parent directory has the
1865 * set-GID bit set, set in on the new directory.
1866 * Otherwise, if the user is neither privileged nor a member of the
1867 * file's new group, clear the file's set-GID bit.
1870 if ((parent
->z_phys
->zp_mode
& S_ISGID
) && (vap
->va_type
== VDIR
)) {
1873 if ((mode
& S_ISGID
) &&
1874 secpolicy_vnode_setids_setgids(cr
, gid
) != 0)
1878 zp
->z_phys
->zp_uid
= fuid
;
1879 zp
->z_phys
->zp_gid
= fgid
;
1880 zp
->z_phys
->zp_mode
= mode
;
1883 mutex_enter(&parent
->z_lock
);
1884 if ((ZTOV(parent
)->v_type
== VDIR
&&
1885 (parent
->z_phys
->zp_flags
& ZFS_INHERIT_ACE
)) &&
1886 !(zp
->z_phys
->zp_flags
& ZFS_XATTR
)) {
1887 mutex_enter(&parent
->z_acl_lock
);
1888 VERIFY(0 == zfs_acl_node_read(parent
, &paclp
, B_FALSE
));
1889 mutex_exit(&parent
->z_acl_lock
);
1890 aclp
= zfs_acl_inherit(zp
, paclp
, mode
, &need_chmod
);
1891 zfs_acl_free(paclp
);
1893 aclp
= zfs_acl_alloc(zfs_acl_version_zp(zp
));
1895 mutex_exit(&parent
->z_lock
);
1896 mutex_enter(&zp
->z_lock
);
1897 mutex_enter(&zp
->z_acl_lock
);
1899 zfs_acl_chmod(zp
, mode
, aclp
);
1901 mutex_enter(&zp
->z_lock
);
1902 mutex_enter(&zp
->z_acl_lock
);
1905 /* Force auto_inherit on all new directory objects */
1906 if (vap
->va_type
== VDIR
)
1907 aclp
->z_hints
|= ZFS_ACL_AUTO_INHERIT
;
1909 error
= zfs_aclset_common(zp
, aclp
, cr
, fuidp
, tx
);
1911 /* Set optional attributes if any */
1912 if (vap
->va_mask
& AT_XVATTR
)
1913 zfs_xvattr_set(zp
, xvap
);
1915 mutex_exit(&zp
->z_lock
);
1916 mutex_exit(&zp
->z_acl_lock
);
1917 ASSERT3U(error
, ==, 0);
1919 if (aclp
!= setaclp
)
1924 * Retrieve a files ACL
1927 zfs_getacl(znode_t
*zp
, vsecattr_t
*vsecp
, boolean_t skipaclchk
, cred_t
*cr
)
1935 mask
= vsecp
->vsa_mask
& (VSA_ACE
| VSA_ACECNT
|
1936 VSA_ACE_ACLFLAGS
| VSA_ACE_ALLTYPES
);
1938 if (error
= zfs_zaccess(zp
, ACE_READ_ACL
, 0, skipaclchk
, cr
))
1944 mutex_enter(&zp
->z_acl_lock
);
1946 error
= zfs_acl_node_read(zp
, &aclp
, B_FALSE
);
1948 mutex_exit(&zp
->z_acl_lock
);
1953 * Scan ACL to determine number of ACEs
1955 if ((zp
->z_phys
->zp_flags
& ZFS_ACL_OBJ_ACE
) &&
1956 !(mask
& VSA_ACE_ALLTYPES
)) {
1959 uint32_t access_mask
;
1960 uint16_t type
, iflags
;
1962 while (zacep
= zfs_acl_next_ace(aclp
, zacep
,
1963 &who
, &access_mask
, &iflags
, &type
)) {
1965 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
1966 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
1967 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
1968 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
1975 vsecp
->vsa_aclcnt
= count
;
1977 count
= aclp
->z_acl_count
;
1979 if (mask
& VSA_ACECNT
) {
1980 vsecp
->vsa_aclcnt
= count
;
1983 if (mask
& VSA_ACE
) {
1986 zfs_acl_node_t
*aclnode
= list_head(&aclp
->z_acl
);
1988 aclsz
= count
* sizeof (ace_t
) +
1989 sizeof (ace_object_t
) * largeace
;
1991 vsecp
->vsa_aclentp
= kmem_alloc(aclsz
, KM_SLEEP
);
1992 vsecp
->vsa_aclentsz
= aclsz
;
1994 if (aclp
->z_version
== ZFS_ACL_VERSION_FUID
)
1995 zfs_copy_fuid_2_ace(zp
->z_zfsvfs
, aclp
, cr
,
1996 vsecp
->vsa_aclentp
, !(mask
& VSA_ACE_ALLTYPES
));
1998 bcopy(aclnode
->z_acldata
, vsecp
->vsa_aclentp
,
1999 count
* sizeof (ace_t
));
2002 if (mask
& VSA_ACE_ACLFLAGS
) {
2003 vsecp
->vsa_aclflags
= 0;
2004 if (zp
->z_phys
->zp_flags
& ZFS_ACL_DEFAULTED
)
2005 vsecp
->vsa_aclflags
|= ACL_DEFAULTED
;
2006 if (zp
->z_phys
->zp_flags
& ZFS_ACL_PROTECTED
)
2007 vsecp
->vsa_aclflags
|= ACL_PROTECTED
;
2008 if (zp
->z_phys
->zp_flags
& ZFS_ACL_AUTO_INHERIT
)
2009 vsecp
->vsa_aclflags
|= ACL_AUTO_INHERIT
;
2012 mutex_exit(&zp
->z_acl_lock
);
2020 zfs_vsec_2_aclp(zfsvfs_t
*zfsvfs
, vtype_t obj_type
,
2021 vsecattr_t
*vsecp
, zfs_acl_t
**zaclp
)
2024 zfs_acl_node_t
*aclnode
;
2025 int aclcnt
= vsecp
->vsa_aclcnt
;
2028 if (vsecp
->vsa_aclcnt
> MAX_ACL_ENTRIES
|| vsecp
->vsa_aclcnt
<= 0)
2031 aclp
= zfs_acl_alloc(zfs_acl_version(zfsvfs
->z_version
));
2034 aclnode
= zfs_acl_node_alloc(aclcnt
* sizeof (zfs_object_ace_t
));
2035 if (aclp
->z_version
== ZFS_ACL_VERSION_INITIAL
) {
2036 if ((error
= zfs_copy_ace_2_oldace(obj_type
, aclp
,
2037 (ace_t
*)vsecp
->vsa_aclentp
, aclnode
->z_acldata
,
2038 aclcnt
, &aclnode
->z_size
)) != 0) {
2040 zfs_acl_node_free(aclnode
);
2044 if ((error
= zfs_copy_ace_2_fuid(obj_type
, aclp
,
2045 vsecp
->vsa_aclentp
, aclnode
->z_acldata
, aclcnt
,
2046 &aclnode
->z_size
)) != 0) {
2048 zfs_acl_node_free(aclnode
);
2052 aclp
->z_acl_bytes
= aclnode
->z_size
;
2053 aclnode
->z_ace_count
= aclcnt
;
2054 aclp
->z_acl_count
= aclcnt
;
2055 list_insert_head(&aclp
->z_acl
, aclnode
);
2058 * If flags are being set then add them to z_hints
2060 if (vsecp
->vsa_mask
& VSA_ACE_ACLFLAGS
) {
2061 if (vsecp
->vsa_aclflags
& ACL_PROTECTED
)
2062 aclp
->z_hints
|= ZFS_ACL_PROTECTED
;
2063 if (vsecp
->vsa_aclflags
& ACL_DEFAULTED
)
2064 aclp
->z_hints
|= ZFS_ACL_DEFAULTED
;
2065 if (vsecp
->vsa_aclflags
& ACL_AUTO_INHERIT
)
2066 aclp
->z_hints
|= ZFS_ACL_AUTO_INHERIT
;
2078 zfs_setacl(znode_t
*zp
, vsecattr_t
*vsecp
, boolean_t skipaclchk
, cred_t
*cr
)
2080 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2081 zilog_t
*zilog
= zfsvfs
->z_log
;
2082 ulong_t mask
= vsecp
->vsa_mask
& (VSA_ACE
| VSA_ACECNT
);
2086 zfs_fuid_info_t
*fuidp
= NULL
;
2091 if (zp
->z_phys
->zp_flags
& ZFS_IMMUTABLE
)
2094 if (error
= zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
))
2097 error
= zfs_vsec_2_aclp(zfsvfs
, ZTOV(zp
)->v_type
, vsecp
, &aclp
);
2102 * If ACL wide flags aren't being set then preserve any
2105 if (!(vsecp
->vsa_mask
& VSA_ACE_ACLFLAGS
)) {
2106 aclp
->z_hints
|= (zp
->z_phys
->zp_flags
& V4_ACL_WIDE_FLAGS
);
2109 if (error
= zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
)) {
2114 mutex_enter(&zp
->z_lock
);
2115 mutex_enter(&zp
->z_acl_lock
);
2117 tx
= dmu_tx_create(zfsvfs
->z_os
);
2118 dmu_tx_hold_bonus(tx
, zp
->z_id
);
2120 if (zp
->z_phys
->zp_acl
.z_acl_extern_obj
) {
2121 /* Are we upgrading ACL? */
2122 if (zfsvfs
->z_version
<= ZPL_VERSION_FUID
&&
2123 zp
->z_phys
->zp_acl
.z_acl_version
==
2124 ZFS_ACL_VERSION_INITIAL
) {
2125 dmu_tx_hold_free(tx
,
2126 zp
->z_phys
->zp_acl
.z_acl_extern_obj
,
2128 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2129 0, aclp
->z_acl_bytes
);
2131 dmu_tx_hold_write(tx
,
2132 zp
->z_phys
->zp_acl
.z_acl_extern_obj
,
2133 0, aclp
->z_acl_bytes
);
2135 } else if (aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2136 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, aclp
->z_acl_bytes
);
2138 if (aclp
->z_has_fuids
) {
2139 if (zfsvfs
->z_fuid_obj
== 0) {
2140 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
2141 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
2142 FUID_SIZE_ESTIMATE(zfsvfs
));
2143 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, FALSE
, NULL
);
2145 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
2146 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
2147 FUID_SIZE_ESTIMATE(zfsvfs
));
2151 error
= dmu_tx_assign(tx
, zfsvfs
->z_assign
);
2153 mutex_exit(&zp
->z_acl_lock
);
2154 mutex_exit(&zp
->z_lock
);
2156 if (error
== ERESTART
&& zfsvfs
->z_assign
== TXG_NOWAIT
) {
2166 error
= zfs_aclset_common(zp
, aclp
, cr
, &fuidp
, tx
);
2169 zfs_log_acl(zilog
, tx
, zp
, vsecp
, fuidp
);
2172 zfs_fuid_info_free(fuidp
);
2176 mutex_exit(&zp
->z_acl_lock
);
2177 mutex_exit(&zp
->z_lock
);
2183 * working_mode returns the permissions that were not granted
2186 zfs_zaccess_common(znode_t
*zp
, uint32_t v4_mode
, uint32_t *working_mode
,
2187 boolean_t
*check_privs
, boolean_t skipaclchk
, cred_t
*cr
)
2190 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2192 uid_t uid
= crgetuid(cr
);
2194 uint16_t type
, iflags
;
2195 uint16_t entry_type
;
2196 uint32_t access_mask
;
2197 uint32_t deny_mask
= 0;
2198 zfs_ace_hdr_t
*acep
= NULL
;
2204 * Short circuit empty requests
2209 *check_privs
= B_TRUE
;
2211 if (zfsvfs
->z_assign
>= TXG_INITIAL
) { /* ZIL replay */
2216 *working_mode
= v4_mode
;
2218 if ((v4_mode
& WRITE_MASK
) &&
2219 (zp
->z_zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) &&
2220 (!IS_DEVVP(ZTOV(zp
)))) {
2221 *check_privs
= B_FALSE
;
2226 * Only check for READONLY on non-directories.
2228 if ((v4_mode
& WRITE_MASK_DATA
) &&
2229 (((ZTOV(zp
)->v_type
!= VDIR
) &&
2230 (zp
->z_phys
->zp_flags
& (ZFS_READONLY
| ZFS_IMMUTABLE
))) ||
2231 (ZTOV(zp
)->v_type
== VDIR
&&
2232 (zp
->z_phys
->zp_flags
& ZFS_IMMUTABLE
)))) {
2233 *check_privs
= B_FALSE
;
2237 if ((v4_mode
& (ACE_DELETE
| ACE_DELETE_CHILD
)) &&
2238 (zp
->z_phys
->zp_flags
& ZFS_NOUNLINK
)) {
2239 *check_privs
= B_FALSE
;
2243 if (((v4_mode
& (ACE_READ_DATA
|ACE_EXECUTE
)) &&
2244 (zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
))) {
2245 *check_privs
= B_FALSE
;
2250 * The caller requested that the ACL check be skipped. This
2251 * would only happen if the caller checked VOP_ACCESS() with a
2252 * 32 bit ACE mask and already had the appropriate permissions.
2259 zfs_fuid_map_ids(zp
, cr
, &fowner
, &gowner
);
2261 mutex_enter(&zp
->z_acl_lock
);
2263 error
= zfs_acl_node_read(zp
, &aclp
, B_FALSE
);
2265 mutex_exit(&zp
->z_acl_lock
);
2269 while (acep
= zfs_acl_next_ace(aclp
, acep
, &who
, &access_mask
,
2272 if (!zfs_acl_valid_ace_type(type
, iflags
))
2275 if (ZTOV(zp
)->v_type
== VDIR
&& (iflags
& ACE_INHERIT_ONLY_ACE
))
2278 entry_type
= (iflags
& ACE_TYPE_FLAGS
);
2282 switch (entry_type
) {
2290 case ACE_IDENTIFIER_GROUP
:
2291 checkit
= zfs_groupmember(zfsvfs
, who
, cr
);
2299 if (entry_type
== 0) {
2302 newid
= zfs_fuid_map_id(zfsvfs
, who
, cr
,
2304 if (newid
!= IDMAP_WK_CREATOR_OWNER_UID
&&
2310 mutex_exit(&zp
->z_acl_lock
);
2316 uint32_t mask_matched
= (access_mask
& *working_mode
);
2320 deny_mask
|= mask_matched
;
2322 *working_mode
&= ~mask_matched
;
2327 if (*working_mode
== 0)
2331 mutex_exit(&zp
->z_acl_lock
);
2334 /* Put the found 'denies' back on the working mode */
2336 *working_mode
|= deny_mask
;
2338 } else if (*working_mode
) {
2346 zfs_zaccess_append(znode_t
*zp
, uint32_t *working_mode
, boolean_t
*check_privs
,
2349 if (*working_mode
!= ACE_WRITE_DATA
)
2352 return (zfs_zaccess_common(zp
, ACE_APPEND_DATA
, working_mode
,
2353 check_privs
, B_FALSE
, cr
));
2357 * Determine whether Access should be granted/denied, invoking least
2358 * priv subsytem when a deny is determined.
2361 zfs_zaccess(znode_t
*zp
, int mode
, int flags
, boolean_t skipaclchk
, cred_t
*cr
)
2363 uint32_t working_mode
;
2366 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2367 boolean_t check_privs
;
2369 znode_t
*check_zp
= zp
;
2371 is_attr
= ((zp
->z_phys
->zp_flags
& ZFS_XATTR
) &&
2372 (ZTOV(zp
)->v_type
== VDIR
));
2375 * If attribute then validate against base file
2378 if ((error
= zfs_zget(zp
->z_zfsvfs
,
2379 zp
->z_phys
->zp_parent
, &xzp
)) != 0) {
2386 * fixup mode to map to xattr perms
2389 if (mode
& (ACE_WRITE_DATA
|ACE_APPEND_DATA
)) {
2390 mode
&= ~(ACE_WRITE_DATA
|ACE_APPEND_DATA
);
2391 mode
|= ACE_WRITE_NAMED_ATTRS
;
2394 if (mode
& (ACE_READ_DATA
|ACE_EXECUTE
)) {
2395 mode
&= ~(ACE_READ_DATA
|ACE_EXECUTE
);
2396 mode
|= ACE_READ_NAMED_ATTRS
;
2400 if ((error
= zfs_zaccess_common(check_zp
, mode
, &working_mode
,
2401 &check_privs
, skipaclchk
, cr
)) == 0) {
2407 if (error
&& !check_privs
) {
2413 if (error
&& (flags
& V_APPEND
)) {
2414 error
= zfs_zaccess_append(zp
, &working_mode
, &check_privs
, cr
);
2417 if (error
&& check_privs
) {
2419 mode_t checkmode
= 0;
2421 owner
= zfs_fuid_map_id(zfsvfs
, check_zp
->z_phys
->zp_uid
, cr
,
2425 * First check for implicit owner permission on
2426 * read_acl/read_attributes
2430 ASSERT(working_mode
!= 0);
2432 if ((working_mode
& (ACE_READ_ACL
|ACE_READ_ATTRIBUTES
) &&
2433 owner
== crgetuid(cr
)))
2434 working_mode
&= ~(ACE_READ_ACL
|ACE_READ_ATTRIBUTES
);
2436 if (working_mode
& (ACE_READ_DATA
|ACE_READ_NAMED_ATTRS
|
2437 ACE_READ_ACL
|ACE_READ_ATTRIBUTES
|ACE_SYNCHRONIZE
))
2439 if (working_mode
& (ACE_WRITE_DATA
|ACE_WRITE_NAMED_ATTRS
|
2440 ACE_APPEND_DATA
|ACE_WRITE_ATTRIBUTES
|ACE_SYNCHRONIZE
))
2441 checkmode
|= VWRITE
;
2442 if (working_mode
& ACE_EXECUTE
)
2446 error
= secpolicy_vnode_access(cr
, ZTOV(check_zp
),
2449 if (error
== 0 && (working_mode
& ACE_WRITE_OWNER
))
2450 error
= secpolicy_vnode_chown(cr
, B_TRUE
);
2451 if (error
== 0 && (working_mode
& ACE_WRITE_ACL
))
2452 error
= secpolicy_vnode_setdac(cr
, owner
);
2454 if (error
== 0 && (working_mode
&
2455 (ACE_DELETE
|ACE_DELETE_CHILD
)))
2456 error
= secpolicy_vnode_remove(cr
);
2458 if (error
== 0 && (working_mode
& ACE_SYNCHRONIZE
)) {
2459 error
= secpolicy_vnode_chown(cr
, B_FALSE
);
2463 * See if any bits other than those already checked
2464 * for are still present. If so then return EACCES
2466 if (working_mode
& ~(ZFS_CHECKED_MASKS
)) {
2479 * Translate traditional unix VREAD/VWRITE/VEXEC mode into
2480 * native ACL format and call zfs_zaccess()
2483 zfs_zaccess_rwx(znode_t
*zp
, mode_t mode
, int flags
, cred_t
*cr
)
2485 return (zfs_zaccess(zp
, zfs_unix_to_v4(mode
>> 6), flags
, B_FALSE
, cr
));
2489 * Access function for secpolicy_vnode_setattr
2492 zfs_zaccess_unix(znode_t
*zp
, mode_t mode
, cred_t
*cr
)
2494 int v4_mode
= zfs_unix_to_v4(mode
>> 6);
2496 return (zfs_zaccess(zp
, v4_mode
, 0, B_FALSE
, cr
));
2500 zfs_delete_final_check(znode_t
*zp
, znode_t
*dzp
,
2501 mode_t missing_perms
, cred_t
*cr
)
2505 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2507 downer
= zfs_fuid_map_id(zfsvfs
, dzp
->z_phys
->zp_uid
, cr
, ZFS_OWNER
);
2509 error
= secpolicy_vnode_access(cr
, ZTOV(dzp
), downer
, missing_perms
);
2512 error
= zfs_sticky_remove_access(dzp
, zp
, cr
);
2518 * Determine whether Access should be granted/deny, without
2519 * consulting least priv subsystem.
2522 * The following chart is the recommended NFSv4 enforcement for
2523 * ability to delete an object.
2525 * -------------------------------------------------------
2526 * | Parent Dir | Target Object Permissions |
2528 * -------------------------------------------------------
2529 * | | ACL Allows | ACL Denies| Delete |
2530 * | | Delete | Delete | unspecified|
2531 * -------------------------------------------------------
2532 * | ACL Allows | Permit | Permit | Permit |
2533 * | DELETE_CHILD | |
2534 * -------------------------------------------------------
2535 * | ACL Denies | Permit | Deny | Deny |
2536 * | DELETE_CHILD | | | |
2537 * -------------------------------------------------------
2538 * | ACL specifies | | | |
2539 * | only allow | Permit | Permit | Permit |
2540 * | write and | | | |
2542 * -------------------------------------------------------
2543 * | ACL denies | | | |
2544 * | write and | Permit | Deny | Deny |
2546 * -------------------------------------------------------
2549 * No search privilege, can't even look up file?
2553 zfs_zaccess_delete(znode_t
*dzp
, znode_t
*zp
, cred_t
*cr
)
2555 uint32_t dzp_working_mode
= 0;
2556 uint32_t zp_working_mode
= 0;
2557 int dzp_error
, zp_error
;
2558 mode_t missing_perms
;
2559 boolean_t dzpcheck_privs
= B_TRUE
;
2560 boolean_t zpcheck_privs
= B_TRUE
;
2563 * We want specific DELETE permissions to
2564 * take precedence over WRITE/EXECUTE. We don't
2565 * want an ACL such as this to mess us up.
2566 * user:joe:write_data:deny,user:joe:delete:allow
2568 * However, deny permissions may ultimately be overridden
2569 * by secpolicy_vnode_access().
2571 * We will ask for all of the necessary permissions and then
2572 * look at the working modes from the directory and target object
2573 * to determine what was found.
2576 if (zp
->z_phys
->zp_flags
& (ZFS_IMMUTABLE
| ZFS_NOUNLINK
))
2581 * If the directory permissions allow the delete, we are done.
2583 if ((dzp_error
= zfs_zaccess_common(dzp
, ACE_DELETE_CHILD
,
2584 &dzp_working_mode
, &dzpcheck_privs
, B_FALSE
, cr
)) == 0)
2588 * If target object has delete permission then we are done
2590 if ((zp_error
= zfs_zaccess_common(zp
, ACE_DELETE
, &zp_working_mode
,
2591 &zpcheck_privs
, B_FALSE
, cr
)) == 0)
2594 ASSERT(dzp_error
&& zp_error
);
2596 if (!dzpcheck_privs
)
2604 * If directory returns EACCES then delete_child was denied
2605 * due to deny delete_child. In this case send the request through
2606 * secpolicy_vnode_remove(). We don't use zfs_delete_final_check()
2607 * since that *could* allow the delete based on write/execute permission
2608 * and we want delete permissions to override write/execute.
2611 if (dzp_error
== EACCES
)
2612 return (secpolicy_vnode_remove(cr
));
2616 * only need to see if we have write/execute on directory.
2619 if ((dzp_error
= zfs_zaccess_common(dzp
, ACE_EXECUTE
|ACE_WRITE_DATA
,
2620 &dzp_working_mode
, &dzpcheck_privs
, B_FALSE
, cr
)) == 0)
2621 return (zfs_sticky_remove_access(dzp
, zp
, cr
));
2623 if (!dzpcheck_privs
)
2630 missing_perms
= (dzp_working_mode
& ACE_WRITE_DATA
) ? VWRITE
: 0;
2631 missing_perms
|= (dzp_working_mode
& ACE_EXECUTE
) ? VEXEC
: 0;
2633 ASSERT(missing_perms
);
2635 return (zfs_delete_final_check(zp
, dzp
, missing_perms
, cr
));
2640 zfs_zaccess_rename(znode_t
*sdzp
, znode_t
*szp
, znode_t
*tdzp
,
2641 znode_t
*tzp
, cred_t
*cr
)
2646 if (szp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
)
2649 add_perm
= (ZTOV(szp
)->v_type
== VDIR
) ?
2650 ACE_ADD_SUBDIRECTORY
: ACE_ADD_FILE
;
2653 * Rename permissions are combination of delete permission +
2654 * add file/subdir permission.
2658 * first make sure we do the delete portion.
2660 * If that succeeds then check for add_file/add_subdir permissions
2663 if (error
= zfs_zaccess_delete(sdzp
, szp
, cr
))
2667 * If we have a tzp, see if we can delete it?
2670 if (error
= zfs_zaccess_delete(tdzp
, tzp
, cr
))
2675 * Now check for add permissions
2677 error
= zfs_zaccess(tdzp
, add_perm
, 0, B_FALSE
, cr
);