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 2009 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 "fs/fs_subr.h"
54 #include <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)
69 #define ZFS_CHECKED_MASKS (ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_DATA| \
70 ACE_READ_NAMED_ATTRS|ACE_WRITE_DATA|ACE_WRITE_ATTRIBUTES| \
71 ACE_WRITE_NAMED_ATTRS|ACE_APPEND_DATA|ACE_EXECUTE|ACE_WRITE_OWNER| \
72 ACE_WRITE_ACL|ACE_DELETE|ACE_DELETE_CHILD|ACE_SYNCHRONIZE)
74 #define WRITE_MASK_DATA (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS)
75 #define WRITE_MASK_ATTRS (ACE_WRITE_ACL|ACE_WRITE_OWNER|ACE_WRITE_ATTRIBUTES| \
76 ACE_DELETE|ACE_DELETE_CHILD)
77 #define WRITE_MASK (WRITE_MASK_DATA|WRITE_MASK_ATTRS)
79 #define OGE_CLEAR (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
80 ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
82 #define OKAY_MASK_BITS (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
83 ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
85 #define ALL_INHERIT (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE | \
86 ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE|ACE_INHERITED_ACE)
88 #define RESTRICTED_CLEAR (ACE_WRITE_ACL|ACE_WRITE_OWNER)
90 #define V4_ACL_WIDE_FLAGS (ZFS_ACL_AUTO_INHERIT|ZFS_ACL_DEFAULTED|\
93 #define ZFS_ACL_WIDE_FLAGS (V4_ACL_WIDE_FLAGS|ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|\
97 zfs_ace_v0_get_type(void *acep
)
99 return (((zfs_oldace_t
*)acep
)->z_type
);
103 zfs_ace_v0_get_flags(void *acep
)
105 return (((zfs_oldace_t
*)acep
)->z_flags
);
109 zfs_ace_v0_get_mask(void *acep
)
111 return (((zfs_oldace_t
*)acep
)->z_access_mask
);
115 zfs_ace_v0_get_who(void *acep
)
117 return (((zfs_oldace_t
*)acep
)->z_fuid
);
121 zfs_ace_v0_set_type(void *acep
, uint16_t type
)
123 ((zfs_oldace_t
*)acep
)->z_type
= type
;
127 zfs_ace_v0_set_flags(void *acep
, uint16_t flags
)
129 ((zfs_oldace_t
*)acep
)->z_flags
= flags
;
133 zfs_ace_v0_set_mask(void *acep
, uint32_t mask
)
135 ((zfs_oldace_t
*)acep
)->z_access_mask
= mask
;
139 zfs_ace_v0_set_who(void *acep
, uint64_t who
)
141 ((zfs_oldace_t
*)acep
)->z_fuid
= who
;
146 zfs_ace_v0_size(void *acep
)
148 return (sizeof (zfs_oldace_t
));
152 zfs_ace_v0_abstract_size(void)
154 return (sizeof (zfs_oldace_t
));
158 zfs_ace_v0_mask_off(void)
160 return (offsetof(zfs_oldace_t
, z_access_mask
));
165 zfs_ace_v0_data(void *acep
, void **datap
)
171 static acl_ops_t zfs_acl_v0_ops
= {
174 zfs_ace_v0_get_flags
,
175 zfs_ace_v0_set_flags
,
181 zfs_ace_v0_abstract_size
,
187 zfs_ace_fuid_get_type(void *acep
)
189 return (((zfs_ace_hdr_t
*)acep
)->z_type
);
193 zfs_ace_fuid_get_flags(void *acep
)
195 return (((zfs_ace_hdr_t
*)acep
)->z_flags
);
199 zfs_ace_fuid_get_mask(void *acep
)
201 return (((zfs_ace_hdr_t
*)acep
)->z_access_mask
);
205 zfs_ace_fuid_get_who(void *args
)
208 zfs_ace_t
*acep
= args
;
210 entry_type
= acep
->z_hdr
.z_flags
& ACE_TYPE_FLAGS
;
212 if (entry_type
== ACE_OWNER
|| entry_type
== OWNING_GROUP
||
213 entry_type
== ACE_EVERYONE
)
215 return (((zfs_ace_t
*)acep
)->z_fuid
);
219 zfs_ace_fuid_set_type(void *acep
, uint16_t type
)
221 ((zfs_ace_hdr_t
*)acep
)->z_type
= type
;
225 zfs_ace_fuid_set_flags(void *acep
, uint16_t flags
)
227 ((zfs_ace_hdr_t
*)acep
)->z_flags
= flags
;
231 zfs_ace_fuid_set_mask(void *acep
, uint32_t mask
)
233 ((zfs_ace_hdr_t
*)acep
)->z_access_mask
= mask
;
237 zfs_ace_fuid_set_who(void *arg
, uint64_t who
)
239 zfs_ace_t
*acep
= arg
;
241 uint16_t entry_type
= acep
->z_hdr
.z_flags
& ACE_TYPE_FLAGS
;
243 if (entry_type
== ACE_OWNER
|| entry_type
== OWNING_GROUP
||
244 entry_type
== ACE_EVERYONE
)
250 zfs_ace_fuid_size(void *acep
)
252 zfs_ace_hdr_t
*zacep
= acep
;
255 switch (zacep
->z_type
) {
256 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
257 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
258 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
259 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
260 return (sizeof (zfs_object_ace_t
));
264 (((zfs_ace_hdr_t
*)acep
)->z_flags
& ACE_TYPE_FLAGS
);
265 if (entry_type
== ACE_OWNER
||
266 entry_type
== OWNING_GROUP
||
267 entry_type
== ACE_EVERYONE
)
268 return (sizeof (zfs_ace_hdr_t
));
271 return (sizeof (zfs_ace_t
));
276 zfs_ace_fuid_abstract_size(void)
278 return (sizeof (zfs_ace_hdr_t
));
282 zfs_ace_fuid_mask_off(void)
284 return (offsetof(zfs_ace_hdr_t
, z_access_mask
));
288 zfs_ace_fuid_data(void *acep
, void **datap
)
290 zfs_ace_t
*zacep
= acep
;
291 zfs_object_ace_t
*zobjp
;
293 switch (zacep
->z_hdr
.z_type
) {
294 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
295 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
296 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
297 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
299 *datap
= (caddr_t
)zobjp
+ sizeof (zfs_ace_t
);
300 return (sizeof (zfs_object_ace_t
) - sizeof (zfs_ace_t
));
307 static acl_ops_t zfs_acl_fuid_ops
= {
308 zfs_ace_fuid_get_mask
,
309 zfs_ace_fuid_set_mask
,
310 zfs_ace_fuid_get_flags
,
311 zfs_ace_fuid_set_flags
,
312 zfs_ace_fuid_get_type
,
313 zfs_ace_fuid_set_type
,
314 zfs_ace_fuid_get_who
,
315 zfs_ace_fuid_set_who
,
317 zfs_ace_fuid_abstract_size
,
318 zfs_ace_fuid_mask_off
,
323 zfs_acl_version(int version
)
325 if (version
< ZPL_VERSION_FUID
)
326 return (ZFS_ACL_VERSION_INITIAL
);
328 return (ZFS_ACL_VERSION_FUID
);
332 zfs_acl_version_zp(znode_t
*zp
)
334 return (zfs_acl_version(zp
->z_zfsvfs
->z_version
));
338 zfs_acl_alloc(int vers
)
342 aclp
= kmem_zalloc(sizeof (zfs_acl_t
), KM_SLEEP
);
343 list_create(&aclp
->z_acl
, sizeof (zfs_acl_node_t
),
344 offsetof(zfs_acl_node_t
, z_next
));
345 aclp
->z_version
= vers
;
346 if (vers
== ZFS_ACL_VERSION_FUID
)
347 aclp
->z_ops
= zfs_acl_fuid_ops
;
349 aclp
->z_ops
= zfs_acl_v0_ops
;
353 static zfs_acl_node_t
*
354 zfs_acl_node_alloc(size_t bytes
)
356 zfs_acl_node_t
*aclnode
;
358 aclnode
= kmem_zalloc(sizeof (zfs_acl_node_t
), KM_SLEEP
);
360 aclnode
->z_acldata
= kmem_alloc(bytes
, KM_SLEEP
);
361 aclnode
->z_allocdata
= aclnode
->z_acldata
;
362 aclnode
->z_allocsize
= bytes
;
363 aclnode
->z_size
= bytes
;
370 zfs_acl_node_free(zfs_acl_node_t
*aclnode
)
372 if (aclnode
->z_allocsize
)
373 kmem_free(aclnode
->z_allocdata
, aclnode
->z_allocsize
);
374 kmem_free(aclnode
, sizeof (zfs_acl_node_t
));
378 zfs_acl_release_nodes(zfs_acl_t
*aclp
)
380 zfs_acl_node_t
*aclnode
;
382 while (aclnode
= list_head(&aclp
->z_acl
)) {
383 list_remove(&aclp
->z_acl
, aclnode
);
384 zfs_acl_node_free(aclnode
);
386 aclp
->z_acl_count
= 0;
387 aclp
->z_acl_bytes
= 0;
391 zfs_acl_free(zfs_acl_t
*aclp
)
393 zfs_acl_release_nodes(aclp
);
394 list_destroy(&aclp
->z_acl
);
395 kmem_free(aclp
, sizeof (zfs_acl_t
));
399 zfs_acl_valid_ace_type(uint_t type
, uint_t flags
)
406 case ACE_SYSTEM_AUDIT_ACE_TYPE
:
407 case ACE_SYSTEM_ALARM_ACE_TYPE
:
408 entry_type
= flags
& ACE_TYPE_FLAGS
;
409 return (entry_type
== ACE_OWNER
||
410 entry_type
== OWNING_GROUP
||
411 entry_type
== ACE_EVERYONE
|| entry_type
== 0 ||
412 entry_type
== ACE_IDENTIFIER_GROUP
);
414 if (type
>= MIN_ACE_TYPE
&& type
<= MAX_ACE_TYPE
)
421 zfs_ace_valid(vtype_t obj_type
, zfs_acl_t
*aclp
, uint16_t type
, uint16_t iflags
)
424 * first check type of entry
427 if (!zfs_acl_valid_ace_type(type
, iflags
))
431 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
432 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
433 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
434 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
435 if (aclp
->z_version
< ZFS_ACL_VERSION_FUID
)
437 aclp
->z_hints
|= ZFS_ACL_OBJ_ACE
;
441 * next check inheritance level flags
444 if (obj_type
== VDIR
&&
445 (iflags
& (ACE_FILE_INHERIT_ACE
|ACE_DIRECTORY_INHERIT_ACE
)))
446 aclp
->z_hints
|= ZFS_INHERIT_ACE
;
448 if (iflags
& (ACE_INHERIT_ONLY_ACE
|ACE_NO_PROPAGATE_INHERIT_ACE
)) {
449 if ((iflags
& (ACE_FILE_INHERIT_ACE
|
450 ACE_DIRECTORY_INHERIT_ACE
)) == 0) {
459 zfs_acl_next_ace(zfs_acl_t
*aclp
, void *start
, uint64_t *who
,
460 uint32_t *access_mask
, uint16_t *iflags
, uint16_t *type
)
462 zfs_acl_node_t
*aclnode
;
465 aclnode
= list_head(&aclp
->z_acl
);
469 aclp
->z_next_ace
= aclnode
->z_acldata
;
470 aclp
->z_curr_node
= aclnode
;
471 aclnode
->z_ace_idx
= 0;
474 aclnode
= aclp
->z_curr_node
;
479 if (aclnode
->z_ace_idx
>= aclnode
->z_ace_count
) {
480 aclnode
= list_next(&aclp
->z_acl
, aclnode
);
484 aclp
->z_curr_node
= aclnode
;
485 aclnode
->z_ace_idx
= 0;
486 aclp
->z_next_ace
= aclnode
->z_acldata
;
490 if (aclnode
->z_ace_idx
< aclnode
->z_ace_count
) {
491 void *acep
= aclp
->z_next_ace
;
495 * Make sure we don't overstep our bounds
497 ace_size
= aclp
->z_ops
.ace_size(acep
);
499 if (((caddr_t
)acep
+ ace_size
) >
500 ((caddr_t
)aclnode
->z_acldata
+ aclnode
->z_size
)) {
504 *iflags
= aclp
->z_ops
.ace_flags_get(acep
);
505 *type
= aclp
->z_ops
.ace_type_get(acep
);
506 *access_mask
= aclp
->z_ops
.ace_mask_get(acep
);
507 *who
= aclp
->z_ops
.ace_who_get(acep
);
508 aclp
->z_next_ace
= (caddr_t
)aclp
->z_next_ace
+ ace_size
;
509 aclnode
->z_ace_idx
++;
510 return ((void *)acep
);
517 zfs_ace_walk(void *datap
, uint64_t cookie
, int aclcnt
,
518 uint16_t *flags
, uint16_t *type
, uint32_t *mask
)
520 zfs_acl_t
*aclp
= datap
;
521 zfs_ace_hdr_t
*acep
= (zfs_ace_hdr_t
*)(uintptr_t)cookie
;
524 acep
= zfs_acl_next_ace(aclp
, acep
, &who
, mask
,
526 return ((uint64_t)(uintptr_t)acep
);
529 static zfs_acl_node_t
*
530 zfs_acl_curr_node(zfs_acl_t
*aclp
)
532 ASSERT(aclp
->z_curr_node
);
533 return (aclp
->z_curr_node
);
537 * Copy ACE to internal ZFS format.
538 * While processing the ACL each ACE will be validated for correctness.
539 * ACE FUIDs will be created later.
542 zfs_copy_ace_2_fuid(zfsvfs_t
*zfsvfs
, vtype_t obj_type
, zfs_acl_t
*aclp
,
543 void *datap
, zfs_ace_t
*z_acl
, int aclcnt
, size_t *size
,
544 zfs_fuid_info_t
**fuidp
, cred_t
*cr
)
548 zfs_ace_t
*aceptr
= z_acl
;
550 zfs_object_ace_t
*zobjacep
;
551 ace_object_t
*aceobjp
;
553 for (i
= 0; i
!= aclcnt
; i
++) {
554 aceptr
->z_hdr
.z_access_mask
= acep
->a_access_mask
;
555 aceptr
->z_hdr
.z_flags
= acep
->a_flags
;
556 aceptr
->z_hdr
.z_type
= acep
->a_type
;
557 entry_type
= aceptr
->z_hdr
.z_flags
& ACE_TYPE_FLAGS
;
558 if (entry_type
!= ACE_OWNER
&& entry_type
!= OWNING_GROUP
&&
559 entry_type
!= ACE_EVERYONE
) {
560 aceptr
->z_fuid
= zfs_fuid_create(zfsvfs
, acep
->a_who
,
561 cr
, (entry_type
== 0) ?
562 ZFS_ACE_USER
: ZFS_ACE_GROUP
, fuidp
);
566 * Make sure ACE is valid
568 if (zfs_ace_valid(obj_type
, aclp
, aceptr
->z_hdr
.z_type
,
569 aceptr
->z_hdr
.z_flags
) != B_TRUE
)
572 switch (acep
->a_type
) {
573 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
574 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
575 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
576 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
577 zobjacep
= (zfs_object_ace_t
*)aceptr
;
578 aceobjp
= (ace_object_t
*)acep
;
580 bcopy(aceobjp
->a_obj_type
, zobjacep
->z_object_type
,
581 sizeof (aceobjp
->a_obj_type
));
582 bcopy(aceobjp
->a_inherit_obj_type
,
583 zobjacep
->z_inherit_type
,
584 sizeof (aceobjp
->a_inherit_obj_type
));
585 acep
= (ace_t
*)((caddr_t
)acep
+ sizeof (ace_object_t
));
588 acep
= (ace_t
*)((caddr_t
)acep
+ sizeof (ace_t
));
591 aceptr
= (zfs_ace_t
*)((caddr_t
)aceptr
+
592 aclp
->z_ops
.ace_size(aceptr
));
595 *size
= (caddr_t
)aceptr
- (caddr_t
)z_acl
;
601 * Copy ZFS ACEs to fixed size ace_t layout
604 zfs_copy_fuid_2_ace(zfsvfs_t
*zfsvfs
, zfs_acl_t
*aclp
, cred_t
*cr
,
605 void *datap
, int filter
)
608 uint32_t access_mask
;
609 uint16_t iflags
, type
;
610 zfs_ace_hdr_t
*zacep
= NULL
;
612 ace_object_t
*objacep
;
613 zfs_object_ace_t
*zobjacep
;
617 while (zacep
= zfs_acl_next_ace(aclp
, zacep
,
618 &who
, &access_mask
, &iflags
, &type
)) {
621 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
622 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
623 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
624 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
628 zobjacep
= (zfs_object_ace_t
*)zacep
;
629 objacep
= (ace_object_t
*)acep
;
630 bcopy(zobjacep
->z_object_type
,
632 sizeof (zobjacep
->z_object_type
));
633 bcopy(zobjacep
->z_inherit_type
,
634 objacep
->a_inherit_obj_type
,
635 sizeof (zobjacep
->z_inherit_type
));
636 ace_size
= sizeof (ace_object_t
);
639 ace_size
= sizeof (ace_t
);
643 entry_type
= (iflags
& ACE_TYPE_FLAGS
);
644 if ((entry_type
!= ACE_OWNER
&&
645 entry_type
!= OWNING_GROUP
&&
646 entry_type
!= ACE_EVERYONE
)) {
647 acep
->a_who
= zfs_fuid_map_id(zfsvfs
, who
,
648 cr
, (entry_type
& ACE_IDENTIFIER_GROUP
) ?
649 ZFS_ACE_GROUP
: ZFS_ACE_USER
);
651 acep
->a_who
= (uid_t
)(int64_t)who
;
653 acep
->a_access_mask
= access_mask
;
654 acep
->a_flags
= iflags
;
656 acep
= (ace_t
*)((caddr_t
)acep
+ ace_size
);
661 zfs_copy_ace_2_oldace(vtype_t obj_type
, zfs_acl_t
*aclp
, ace_t
*acep
,
662 zfs_oldace_t
*z_acl
, int aclcnt
, size_t *size
)
665 zfs_oldace_t
*aceptr
= z_acl
;
667 for (i
= 0; i
!= aclcnt
; i
++, aceptr
++) {
668 aceptr
->z_access_mask
= acep
[i
].a_access_mask
;
669 aceptr
->z_type
= acep
[i
].a_type
;
670 aceptr
->z_flags
= acep
[i
].a_flags
;
671 aceptr
->z_fuid
= acep
[i
].a_who
;
673 * Make sure ACE is valid
675 if (zfs_ace_valid(obj_type
, aclp
, aceptr
->z_type
,
676 aceptr
->z_flags
) != B_TRUE
)
679 *size
= (caddr_t
)aceptr
- (caddr_t
)z_acl
;
684 * convert old ACL format to new
687 zfs_acl_xform(znode_t
*zp
, zfs_acl_t
*aclp
, cred_t
*cr
)
689 zfs_oldace_t
*oldaclp
;
691 uint16_t type
, iflags
;
692 uint32_t access_mask
;
695 zfs_acl_node_t
*newaclnode
;
697 ASSERT(aclp
->z_version
== ZFS_ACL_VERSION_INITIAL
);
699 * First create the ACE in a contiguous piece of memory
700 * for zfs_copy_ace_2_fuid().
702 * We only convert an ACL once, so this won't happen
705 oldaclp
= kmem_alloc(sizeof (zfs_oldace_t
) * aclp
->z_acl_count
,
708 while (cookie
= zfs_acl_next_ace(aclp
, cookie
, &who
,
709 &access_mask
, &iflags
, &type
)) {
710 oldaclp
[i
].z_flags
= iflags
;
711 oldaclp
[i
].z_type
= type
;
712 oldaclp
[i
].z_fuid
= who
;
713 oldaclp
[i
++].z_access_mask
= access_mask
;
716 newaclnode
= zfs_acl_node_alloc(aclp
->z_acl_count
*
717 sizeof (zfs_object_ace_t
));
718 aclp
->z_ops
= zfs_acl_fuid_ops
;
719 VERIFY(zfs_copy_ace_2_fuid(zp
->z_zfsvfs
, ZTOV(zp
)->v_type
, aclp
,
720 oldaclp
, newaclnode
->z_acldata
, aclp
->z_acl_count
,
721 &newaclnode
->z_size
, NULL
, cr
) == 0);
722 newaclnode
->z_ace_count
= aclp
->z_acl_count
;
723 aclp
->z_version
= ZFS_ACL_VERSION
;
724 kmem_free(oldaclp
, aclp
->z_acl_count
* sizeof (zfs_oldace_t
));
727 * Release all previous ACL nodes
730 zfs_acl_release_nodes(aclp
);
732 list_insert_head(&aclp
->z_acl
, newaclnode
);
734 aclp
->z_acl_bytes
= newaclnode
->z_size
;
735 aclp
->z_acl_count
= newaclnode
->z_ace_count
;
740 * Convert unix access mask to v4 access mask
743 zfs_unix_to_v4(uint32_t access_mask
)
745 uint32_t new_mask
= 0;
747 if (access_mask
& S_IXOTH
)
748 new_mask
|= ACE_EXECUTE
;
749 if (access_mask
& S_IWOTH
)
750 new_mask
|= ACE_WRITE_DATA
;
751 if (access_mask
& S_IROTH
)
752 new_mask
|= ACE_READ_DATA
;
757 zfs_set_ace(zfs_acl_t
*aclp
, void *acep
, uint32_t access_mask
,
758 uint16_t access_type
, uint64_t fuid
, uint16_t entry_type
)
760 uint16_t type
= entry_type
& ACE_TYPE_FLAGS
;
762 aclp
->z_ops
.ace_mask_set(acep
, access_mask
);
763 aclp
->z_ops
.ace_type_set(acep
, access_type
);
764 aclp
->z_ops
.ace_flags_set(acep
, entry_type
);
765 if ((type
!= ACE_OWNER
&& type
!= OWNING_GROUP
&&
766 type
!= ACE_EVERYONE
))
767 aclp
->z_ops
.ace_who_set(acep
, fuid
);
771 * Determine mode of file based on ACL.
772 * Also, create FUIDs for any User/Group ACEs
775 zfs_mode_compute(znode_t
*zp
, zfs_acl_t
*aclp
)
780 zfs_ace_hdr_t
*acep
= NULL
;
782 uint16_t iflags
, type
;
783 uint32_t access_mask
;
785 mode
= (zp
->z_phys
->zp_mode
& (S_IFMT
| S_ISUID
| S_ISGID
| S_ISVTX
));
787 while (acep
= zfs_acl_next_ace(aclp
, acep
, &who
,
788 &access_mask
, &iflags
, &type
)) {
790 if (!zfs_acl_valid_ace_type(type
, iflags
))
793 entry_type
= (iflags
& ACE_TYPE_FLAGS
);
796 * Skip over owner@, group@ or everyone@ inherit only ACEs
798 if ((iflags
& ACE_INHERIT_ONLY_ACE
) &&
799 (entry_type
== ACE_OWNER
|| entry_type
== ACE_EVERYONE
||
800 entry_type
== OWNING_GROUP
))
803 if (entry_type
== ACE_OWNER
) {
804 if ((access_mask
& ACE_READ_DATA
) &&
805 (!(seen
& S_IRUSR
))) {
811 if ((access_mask
& ACE_WRITE_DATA
) &&
812 (!(seen
& S_IWUSR
))) {
818 if ((access_mask
& ACE_EXECUTE
) &&
819 (!(seen
& S_IXUSR
))) {
825 } else if (entry_type
== OWNING_GROUP
) {
826 if ((access_mask
& ACE_READ_DATA
) &&
827 (!(seen
& S_IRGRP
))) {
833 if ((access_mask
& ACE_WRITE_DATA
) &&
834 (!(seen
& S_IWGRP
))) {
840 if ((access_mask
& ACE_EXECUTE
) &&
841 (!(seen
& S_IXGRP
))) {
847 } else if (entry_type
== ACE_EVERYONE
) {
848 if ((access_mask
& ACE_READ_DATA
)) {
849 if (!(seen
& S_IRUSR
)) {
855 if (!(seen
& S_IRGRP
)) {
861 if (!(seen
& S_IROTH
)) {
868 if ((access_mask
& ACE_WRITE_DATA
)) {
869 if (!(seen
& S_IWUSR
)) {
875 if (!(seen
& S_IWGRP
)) {
881 if (!(seen
& S_IWOTH
)) {
888 if ((access_mask
& ACE_EXECUTE
)) {
889 if (!(seen
& S_IXUSR
)) {
895 if (!(seen
& S_IXGRP
)) {
901 if (!(seen
& S_IXOTH
)) {
914 zfs_acl_node_read_internal(znode_t
*zp
, boolean_t will_modify
)
917 zfs_acl_node_t
*aclnode
;
919 aclp
= zfs_acl_alloc(zp
->z_phys
->zp_acl
.z_acl_version
);
922 * Version 0 to 1 znode_acl_phys has the size/count fields swapped.
923 * Version 0 didn't have a size field, only a count.
925 if (zp
->z_phys
->zp_acl
.z_acl_version
== ZFS_ACL_VERSION_INITIAL
) {
926 aclp
->z_acl_count
= zp
->z_phys
->zp_acl
.z_acl_size
;
927 aclp
->z_acl_bytes
= ZFS_ACL_SIZE(aclp
->z_acl_count
);
929 aclp
->z_acl_count
= zp
->z_phys
->zp_acl
.z_acl_count
;
930 aclp
->z_acl_bytes
= zp
->z_phys
->zp_acl
.z_acl_size
;
933 aclnode
= zfs_acl_node_alloc(will_modify
? aclp
->z_acl_bytes
: 0);
934 aclnode
->z_ace_count
= aclp
->z_acl_count
;
936 bcopy(zp
->z_phys
->zp_acl
.z_ace_data
, aclnode
->z_acldata
,
939 aclnode
->z_size
= aclp
->z_acl_bytes
;
940 aclnode
->z_acldata
= &zp
->z_phys
->zp_acl
.z_ace_data
[0];
943 list_insert_head(&aclp
->z_acl
, aclnode
);
949 * Read an external acl object.
952 zfs_acl_node_read(znode_t
*zp
, zfs_acl_t
**aclpp
, boolean_t will_modify
)
954 uint64_t extacl
= zp
->z_phys
->zp_acl
.z_acl_extern_obj
;
958 zfs_acl_node_t
*aclnode
;
961 ASSERT(MUTEX_HELD(&zp
->z_acl_lock
));
963 if (zp
->z_phys
->zp_acl
.z_acl_extern_obj
== 0) {
964 *aclpp
= zfs_acl_node_read_internal(zp
, will_modify
);
968 aclp
= zfs_acl_alloc(zp
->z_phys
->zp_acl
.z_acl_version
);
969 if (zp
->z_phys
->zp_acl
.z_acl_version
== ZFS_ACL_VERSION_INITIAL
) {
970 zfs_acl_phys_v0_t
*zacl0
=
971 (zfs_acl_phys_v0_t
*)&zp
->z_phys
->zp_acl
;
973 aclsize
= ZFS_ACL_SIZE(zacl0
->z_acl_count
);
974 acl_count
= zacl0
->z_acl_count
;
976 aclsize
= zp
->z_phys
->zp_acl
.z_acl_size
;
977 acl_count
= zp
->z_phys
->zp_acl
.z_acl_count
;
979 aclsize
= acl_count
* sizeof (zfs_ace_t
);
981 aclnode
= zfs_acl_node_alloc(aclsize
);
982 list_insert_head(&aclp
->z_acl
, aclnode
);
983 error
= dmu_read(zp
->z_zfsvfs
->z_os
, extacl
, 0,
984 aclsize
, aclnode
->z_acldata
, DMU_READ_PREFETCH
);
985 aclnode
->z_ace_count
= acl_count
;
986 aclp
->z_acl_count
= acl_count
;
987 aclp
->z_acl_bytes
= aclsize
;
991 /* convert checksum errors into IO errors */
1002 * common code for setting ACLs.
1004 * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl.
1005 * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's
1006 * already checked the acl and knows whether to inherit.
1009 zfs_aclset_common(znode_t
*zp
, zfs_acl_t
*aclp
, cred_t
*cr
, dmu_tx_t
*tx
)
1012 znode_phys_t
*zphys
= zp
->z_phys
;
1013 zfs_acl_phys_t
*zacl
= &zphys
->zp_acl
;
1014 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1015 uint64_t aoid
= zphys
->zp_acl
.z_acl_extern_obj
;
1017 dmu_object_type_t otype
;
1018 zfs_acl_node_t
*aclnode
;
1020 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
1022 zphys
->zp_mode
= zfs_mode_compute(zp
, aclp
);
1025 * Decide which opbject type to use. If we are forced to
1026 * use old ACL format than transform ACL into zfs_oldace_t
1029 if (!zfsvfs
->z_use_fuids
) {
1030 otype
= DMU_OT_OLDACL
;
1032 if ((aclp
->z_version
== ZFS_ACL_VERSION_INITIAL
) &&
1033 (zfsvfs
->z_version
>= ZPL_VERSION_FUID
))
1034 zfs_acl_xform(zp
, aclp
, cr
);
1035 ASSERT(aclp
->z_version
>= ZFS_ACL_VERSION_FUID
);
1039 if (aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1041 * If ACL was previously external and we are now
1042 * converting to new ACL format then release old
1043 * ACL object and create a new one.
1045 if (aoid
&& aclp
->z_version
!= zacl
->z_acl_version
) {
1046 error
= dmu_object_free(zfsvfs
->z_os
,
1047 zp
->z_phys
->zp_acl
.z_acl_extern_obj
, tx
);
1053 aoid
= dmu_object_alloc(zfsvfs
->z_os
,
1054 otype
, aclp
->z_acl_bytes
,
1055 otype
== DMU_OT_ACL
? DMU_OT_SYSACL
: DMU_OT_NONE
,
1056 otype
== DMU_OT_ACL
? DN_MAX_BONUSLEN
: 0, tx
);
1058 (void) dmu_object_set_blocksize(zfsvfs
->z_os
, aoid
,
1059 aclp
->z_acl_bytes
, 0, tx
);
1061 zphys
->zp_acl
.z_acl_extern_obj
= aoid
;
1062 for (aclnode
= list_head(&aclp
->z_acl
); aclnode
;
1063 aclnode
= list_next(&aclp
->z_acl
, aclnode
)) {
1064 if (aclnode
->z_ace_count
== 0)
1066 dmu_write(zfsvfs
->z_os
, aoid
, off
,
1067 aclnode
->z_size
, aclnode
->z_acldata
, tx
);
1068 off
+= aclnode
->z_size
;
1071 void *start
= zacl
->z_ace_data
;
1073 * Migrating back embedded?
1075 if (zphys
->zp_acl
.z_acl_extern_obj
) {
1076 error
= dmu_object_free(zfsvfs
->z_os
,
1077 zp
->z_phys
->zp_acl
.z_acl_extern_obj
, tx
);
1080 zphys
->zp_acl
.z_acl_extern_obj
= 0;
1083 for (aclnode
= list_head(&aclp
->z_acl
); aclnode
;
1084 aclnode
= list_next(&aclp
->z_acl
, aclnode
)) {
1085 if (aclnode
->z_ace_count
== 0)
1087 bcopy(aclnode
->z_acldata
, start
, aclnode
->z_size
);
1088 start
= (caddr_t
)start
+ aclnode
->z_size
;
1093 * If Old version then swap count/bytes to match old
1094 * layout of znode_acl_phys_t.
1096 if (aclp
->z_version
== ZFS_ACL_VERSION_INITIAL
) {
1097 zphys
->zp_acl
.z_acl_size
= aclp
->z_acl_count
;
1098 zphys
->zp_acl
.z_acl_count
= aclp
->z_acl_bytes
;
1100 zphys
->zp_acl
.z_acl_size
= aclp
->z_acl_bytes
;
1101 zphys
->zp_acl
.z_acl_count
= aclp
->z_acl_count
;
1104 zphys
->zp_acl
.z_acl_version
= aclp
->z_version
;
1107 * Replace ACL wide bits, but first clear them.
1109 zp
->z_phys
->zp_flags
&= ~ZFS_ACL_WIDE_FLAGS
;
1111 zp
->z_phys
->zp_flags
|= aclp
->z_hints
;
1113 if (ace_trivial_common(aclp
, 0, zfs_ace_walk
) == 0)
1114 zp
->z_phys
->zp_flags
|= ZFS_ACL_TRIVIAL
;
1120 * Update access mask for prepended ACE
1122 * This applies the "groupmask" value for aclmode property.
1125 zfs_acl_prepend_fixup(zfs_acl_t
*aclp
, void *acep
, void *origacep
,
1126 mode_t mode
, uint64_t owner
)
1128 int rmask
, wmask
, xmask
;
1131 uint32_t origmask
, acepmask
;
1134 aceflags
= aclp
->z_ops
.ace_flags_get(acep
);
1135 fuid
= aclp
->z_ops
.ace_who_get(acep
);
1136 origmask
= aclp
->z_ops
.ace_mask_get(origacep
);
1137 acepmask
= aclp
->z_ops
.ace_mask_get(acep
);
1139 user_ace
= (!(aceflags
&
1140 (ACE_OWNER
|ACE_GROUP
|ACE_IDENTIFIER_GROUP
)));
1142 if (user_ace
&& (fuid
== owner
)) {
1152 if (origmask
& ACE_READ_DATA
) {
1154 acepmask
&= ~ACE_READ_DATA
;
1156 acepmask
|= ACE_READ_DATA
;
1160 if (origmask
& ACE_WRITE_DATA
) {
1162 acepmask
&= ~ACE_WRITE_DATA
;
1164 acepmask
|= ACE_WRITE_DATA
;
1168 if (origmask
& ACE_APPEND_DATA
) {
1170 acepmask
&= ~ACE_APPEND_DATA
;
1172 acepmask
|= ACE_APPEND_DATA
;
1176 if (origmask
& ACE_EXECUTE
) {
1178 acepmask
&= ~ACE_EXECUTE
;
1180 acepmask
|= ACE_EXECUTE
;
1183 aclp
->z_ops
.ace_mask_set(acep
, acepmask
);
1187 * Apply mode to canonical six ACEs.
1190 zfs_acl_fixup_canonical_six(zfs_acl_t
*aclp
, mode_t mode
)
1192 zfs_acl_node_t
*aclnode
= list_tail(&aclp
->z_acl
);
1194 int maskoff
= aclp
->z_ops
.ace_mask_off();
1195 size_t abstract_size
= aclp
->z_ops
.ace_abstract_size();
1197 ASSERT(aclnode
!= NULL
);
1199 acep
= (void *)((caddr_t
)aclnode
->z_acldata
+
1200 aclnode
->z_size
- (abstract_size
* 6));
1203 * Fixup final ACEs to match the mode
1206 adjust_ace_pair_common(acep
, maskoff
, abstract_size
,
1207 (mode
& 0700) >> 6); /* owner@ */
1209 acep
= (caddr_t
)acep
+ (abstract_size
* 2);
1211 adjust_ace_pair_common(acep
, maskoff
, abstract_size
,
1212 (mode
& 0070) >> 3); /* group@ */
1214 acep
= (caddr_t
)acep
+ (abstract_size
* 2);
1215 adjust_ace_pair_common(acep
, maskoff
,
1216 abstract_size
, mode
); /* everyone@ */
1221 zfs_acl_ace_match(zfs_acl_t
*aclp
, void *acep
, int allow_deny
,
1222 int entry_type
, int accessmask
)
1224 uint32_t mask
= aclp
->z_ops
.ace_mask_get(acep
);
1225 uint16_t type
= aclp
->z_ops
.ace_type_get(acep
);
1226 uint16_t flags
= aclp
->z_ops
.ace_flags_get(acep
);
1228 return (mask
== accessmask
&& type
== allow_deny
&&
1229 ((flags
& ACE_TYPE_FLAGS
) == entry_type
));
1233 * Can prepended ACE be reused?
1236 zfs_reuse_deny(zfs_acl_t
*aclp
, void *acep
, void *prevacep
)
1242 uint32_t mask
, prevmask
;
1244 if (prevacep
== NULL
)
1247 prevtype
= aclp
->z_ops
.ace_type_get(prevacep
);
1248 prevflags
= aclp
->z_ops
.ace_flags_get(prevacep
);
1249 flags
= aclp
->z_ops
.ace_flags_get(acep
);
1250 mask
= aclp
->z_ops
.ace_mask_get(acep
);
1251 prevmask
= aclp
->z_ops
.ace_mask_get(prevacep
);
1253 if (prevtype
!= DENY
)
1256 if (prevflags
!= (flags
& ACE_IDENTIFIER_GROUP
))
1259 okay_masks
= (mask
& OKAY_MASK_BITS
);
1261 if (prevmask
& ~okay_masks
)
1269 * Insert new ACL node into chain of zfs_acl_node_t's
1271 * This will result in two possible results.
1272 * 1. If the ACL is currently just a single zfs_acl_node and
1273 * we are prepending the entry then current acl node will have
1274 * a new node inserted above it.
1276 * 2. If we are inserting in the middle of current acl node then
1277 * the current node will be split in two and new node will be inserted
1278 * in between the two split nodes.
1280 static zfs_acl_node_t
*
1281 zfs_acl_ace_insert(zfs_acl_t
*aclp
, void *acep
)
1283 zfs_acl_node_t
*newnode
;
1284 zfs_acl_node_t
*trailernode
= NULL
;
1285 zfs_acl_node_t
*currnode
= zfs_acl_curr_node(aclp
);
1286 int curr_idx
= aclp
->z_curr_node
->z_ace_idx
;
1290 newnode
= zfs_acl_node_alloc(aclp
->z_ops
.ace_size(acep
));
1291 newnode
->z_ace_count
= 1;
1293 oldsize
= currnode
->z_size
;
1295 if (curr_idx
!= 1) {
1296 trailernode
= zfs_acl_node_alloc(0);
1297 trailernode
->z_acldata
= acep
;
1299 trailer_count
= currnode
->z_ace_count
- curr_idx
+ 1;
1300 currnode
->z_ace_count
= curr_idx
- 1;
1301 currnode
->z_size
= (caddr_t
)acep
- (caddr_t
)currnode
->z_acldata
;
1302 trailernode
->z_size
= oldsize
- currnode
->z_size
;
1303 trailernode
->z_ace_count
= trailer_count
;
1306 aclp
->z_acl_count
+= 1;
1307 aclp
->z_acl_bytes
+= aclp
->z_ops
.ace_size(acep
);
1310 list_insert_before(&aclp
->z_acl
, currnode
, newnode
);
1312 list_insert_after(&aclp
->z_acl
, currnode
, newnode
);
1314 list_insert_after(&aclp
->z_acl
, newnode
, trailernode
);
1315 aclp
->z_curr_node
= trailernode
;
1316 trailernode
->z_ace_idx
= 1;
1326 zfs_acl_prepend_deny(uint64_t uid
, zfs_acl_t
*aclp
, void *acep
,
1329 zfs_acl_node_t
*aclnode
;
1334 aclnode
= zfs_acl_ace_insert(aclp
, acep
);
1335 newacep
= aclnode
->z_acldata
;
1336 fuid
= aclp
->z_ops
.ace_who_get(acep
);
1337 flags
= aclp
->z_ops
.ace_flags_get(acep
);
1338 zfs_set_ace(aclp
, newacep
, 0, DENY
, fuid
, (flags
& ACE_TYPE_FLAGS
));
1339 zfs_acl_prepend_fixup(aclp
, newacep
, acep
, mode
, uid
);
1345 * Split an inherited ACE into inherit_only ACE
1346 * and original ACE with inheritance flags stripped off.
1349 zfs_acl_split_ace(zfs_acl_t
*aclp
, zfs_ace_hdr_t
*acep
)
1351 zfs_acl_node_t
*aclnode
;
1352 zfs_acl_node_t
*currnode
;
1354 uint16_t type
, flags
;
1358 type
= aclp
->z_ops
.ace_type_get(acep
);
1359 flags
= aclp
->z_ops
.ace_flags_get(acep
);
1360 mask
= aclp
->z_ops
.ace_mask_get(acep
);
1361 fuid
= aclp
->z_ops
.ace_who_get(acep
);
1363 aclnode
= zfs_acl_ace_insert(aclp
, acep
);
1364 newacep
= aclnode
->z_acldata
;
1366 aclp
->z_ops
.ace_type_set(newacep
, type
);
1367 aclp
->z_ops
.ace_flags_set(newacep
, flags
| ACE_INHERIT_ONLY_ACE
);
1368 aclp
->z_ops
.ace_mask_set(newacep
, mask
);
1369 aclp
->z_ops
.ace_type_set(newacep
, type
);
1370 aclp
->z_ops
.ace_who_set(newacep
, fuid
);
1371 aclp
->z_next_ace
= acep
;
1372 flags
&= ~ALL_INHERIT
;
1373 aclp
->z_ops
.ace_flags_set(acep
, flags
);
1374 currnode
= zfs_acl_curr_node(aclp
);
1375 ASSERT(currnode
->z_ace_idx
>= 1);
1376 currnode
->z_ace_idx
-= 1;
1380 * Are ACES started at index i, the canonical six ACES?
1383 zfs_have_canonical_six(zfs_acl_t
*aclp
)
1386 zfs_acl_node_t
*aclnode
= list_tail(&aclp
->z_acl
);
1388 size_t abstract_size
= aclp
->z_ops
.ace_abstract_size();
1390 ASSERT(aclnode
!= NULL
);
1392 if (aclnode
->z_ace_count
< 6)
1395 acep
= (void *)((caddr_t
)aclnode
->z_acldata
+
1396 aclnode
->z_size
- (aclp
->z_ops
.ace_abstract_size() * 6));
1398 if ((zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++),
1399 DENY
, ACE_OWNER
, 0) &&
1400 zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++),
1401 ALLOW
, ACE_OWNER
, OWNER_ALLOW_MASK
) &&
1402 zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++), DENY
,
1403 OWNING_GROUP
, 0) && zfs_acl_ace_match(aclp
, (caddr_t
)acep
+
1404 (abstract_size
* i
++),
1405 ALLOW
, OWNING_GROUP
, 0) &&
1406 zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++),
1407 DENY
, ACE_EVERYONE
, EVERYONE_DENY_MASK
) &&
1408 zfs_acl_ace_match(aclp
, (caddr_t
)acep
+ (abstract_size
* i
++),
1409 ALLOW
, ACE_EVERYONE
, EVERYONE_ALLOW_MASK
))) {
1418 * Apply step 1g, to group entries
1420 * Need to deal with corner case where group may have
1421 * greater permissions than owner. If so then limit
1422 * group permissions, based on what extra permissions
1426 zfs_fixup_group_entries(zfs_acl_t
*aclp
, void *acep
, void *prevacep
,
1429 uint32_t prevmask
= aclp
->z_ops
.ace_mask_get(prevacep
);
1430 uint32_t mask
= aclp
->z_ops
.ace_mask_get(acep
);
1431 uint16_t prevflags
= aclp
->z_ops
.ace_flags_get(prevacep
);
1432 mode_t extramode
= (mode
>> 3) & 07;
1433 mode_t ownermode
= (mode
>> 6);
1435 if (prevflags
& ACE_IDENTIFIER_GROUP
) {
1437 extramode
&= ~ownermode
;
1440 if (extramode
& S_IROTH
) {
1441 prevmask
&= ~ACE_READ_DATA
;
1442 mask
&= ~ACE_READ_DATA
;
1444 if (extramode
& S_IWOTH
) {
1445 prevmask
&= ~(ACE_WRITE_DATA
|ACE_APPEND_DATA
);
1446 mask
&= ~(ACE_WRITE_DATA
|ACE_APPEND_DATA
);
1448 if (extramode
& S_IXOTH
) {
1449 prevmask
&= ~ACE_EXECUTE
;
1450 mask
&= ~ACE_EXECUTE
;
1454 aclp
->z_ops
.ace_mask_set(acep
, mask
);
1455 aclp
->z_ops
.ace_mask_set(prevacep
, prevmask
);
1459 * Apply the chmod algorithm as described
1463 zfs_acl_chmod(zfsvfs_t
*zfsvfs
, uint64_t uid
,
1464 uint64_t mode
, zfs_acl_t
*aclp
)
1466 void *acep
= NULL
, *prevacep
= NULL
;
1471 int need_canonical_six
= 1;
1472 uint16_t iflags
, type
;
1473 uint32_t access_mask
;
1476 * If discard then just discard all ACL nodes which
1477 * represent the ACEs.
1479 * New owner@/group@/everone@ ACEs will be added
1482 if (zfsvfs
->z_acl_mode
== ZFS_ACL_DISCARD
)
1483 zfs_acl_release_nodes(aclp
);
1485 while (acep
= zfs_acl_next_ace(aclp
, acep
, &who
, &access_mask
,
1488 entry_type
= (iflags
& ACE_TYPE_FLAGS
);
1489 iflags
= (iflags
& ALL_INHERIT
);
1491 if ((type
!= ALLOW
&& type
!= DENY
) ||
1492 (iflags
& ACE_INHERIT_ONLY_ACE
)) {
1494 aclp
->z_hints
|= ZFS_INHERIT_ACE
;
1496 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
1497 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
1498 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
1499 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
1500 aclp
->z_hints
|= ZFS_ACL_OBJ_ACE
;
1507 * Need to split ace into two?
1509 if ((iflags
& (ACE_FILE_INHERIT_ACE
|
1510 ACE_DIRECTORY_INHERIT_ACE
)) &&
1511 (!(iflags
& ACE_INHERIT_ONLY_ACE
))) {
1512 zfs_acl_split_ace(aclp
, acep
);
1513 aclp
->z_hints
|= ZFS_INHERIT_ACE
;
1517 if (entry_type
== ACE_OWNER
|| entry_type
== ACE_EVERYONE
||
1518 (entry_type
== OWNING_GROUP
)) {
1519 access_mask
&= ~OGE_CLEAR
;
1520 aclp
->z_ops
.ace_mask_set(acep
, access_mask
);
1523 reuse_deny
= B_TRUE
;
1524 if (type
== ALLOW
) {
1527 * Check preceding ACE if any, to see
1528 * if we need to prepend a DENY ACE.
1529 * This is only applicable when the acl_mode
1530 * property == groupmask.
1532 if (zfsvfs
->z_acl_mode
== ZFS_ACL_GROUPMASK
) {
1534 reuse_deny
= zfs_reuse_deny(aclp
, acep
,
1539 zfs_acl_prepend_deny(uid
,
1542 zfs_acl_prepend_fixup(
1546 zfs_fixup_group_entries(aclp
, acep
,
1556 * Check out last six aces, if we have six.
1559 if (aclp
->z_acl_count
>= 6) {
1560 if (zfs_have_canonical_six(aclp
)) {
1561 need_canonical_six
= 0;
1565 if (need_canonical_six
) {
1566 size_t abstract_size
= aclp
->z_ops
.ace_abstract_size();
1568 zfs_acl_node_t
*aclnode
=
1569 zfs_acl_node_alloc(abstract_size
* 6);
1571 aclnode
->z_size
= abstract_size
* 6;
1572 aclnode
->z_ace_count
= 6;
1573 aclp
->z_acl_bytes
+= aclnode
->z_size
;
1574 list_insert_tail(&aclp
->z_acl
, aclnode
);
1576 zacep
= aclnode
->z_acldata
;
1579 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++),
1580 0, DENY
, -1, ACE_OWNER
);
1581 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++),
1582 OWNER_ALLOW_MASK
, ALLOW
, -1, ACE_OWNER
);
1583 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++), 0,
1584 DENY
, -1, OWNING_GROUP
);
1585 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++), 0,
1586 ALLOW
, -1, OWNING_GROUP
);
1587 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++),
1588 EVERYONE_DENY_MASK
, DENY
, -1, ACE_EVERYONE
);
1589 zfs_set_ace(aclp
, (caddr_t
)zacep
+ (abstract_size
* i
++),
1590 EVERYONE_ALLOW_MASK
, ALLOW
, -1, ACE_EVERYONE
);
1591 aclp
->z_acl_count
+= 6;
1594 zfs_acl_fixup_canonical_six(aclp
, mode
);
1598 zfs_acl_chmod_setattr(znode_t
*zp
, zfs_acl_t
**aclp
, uint64_t mode
)
1602 mutex_enter(&zp
->z_lock
);
1603 mutex_enter(&zp
->z_acl_lock
);
1605 error
= zfs_acl_node_read(zp
, aclp
, B_TRUE
);
1607 (*aclp
)->z_hints
= zp
->z_phys
->zp_flags
& V4_ACL_WIDE_FLAGS
;
1608 zfs_acl_chmod(zp
->z_zfsvfs
, zp
->z_phys
->zp_uid
, mode
, *aclp
);
1610 mutex_exit(&zp
->z_acl_lock
);
1611 mutex_exit(&zp
->z_lock
);
1616 * strip off write_owner and write_acl
1619 zfs_restricted_update(zfsvfs_t
*zfsvfs
, zfs_acl_t
*aclp
, void *acep
)
1621 uint32_t mask
= aclp
->z_ops
.ace_mask_get(acep
);
1623 if ((zfsvfs
->z_acl_inherit
== ZFS_ACL_RESTRICTED
) &&
1624 (aclp
->z_ops
.ace_type_get(acep
) == ALLOW
)) {
1625 mask
&= ~RESTRICTED_CLEAR
;
1626 aclp
->z_ops
.ace_mask_set(acep
, mask
);
1631 * Should ACE be inherited?
1634 zfs_ace_can_use(vtype_t vtype
, uint16_t acep_flags
)
1636 int iflags
= (acep_flags
& 0xf);
1638 if ((vtype
== VDIR
) && (iflags
& ACE_DIRECTORY_INHERIT_ACE
))
1640 else if (iflags
& ACE_FILE_INHERIT_ACE
)
1641 return (!((vtype
== VDIR
) &&
1642 (iflags
& ACE_NO_PROPAGATE_INHERIT_ACE
)));
1647 * inherit inheritable ACEs from parent
1650 zfs_acl_inherit(zfsvfs_t
*zfsvfs
, vtype_t vtype
, zfs_acl_t
*paclp
,
1651 uint64_t mode
, boolean_t
*need_chmod
)
1655 zfs_acl_node_t
*aclnode
, *aclnode2
;
1656 zfs_acl_t
*aclp
= NULL
;
1658 uint32_t access_mask
;
1659 uint16_t iflags
, newflags
, type
;
1661 void *data1
, *data2
;
1662 size_t data1sz
, data2sz
;
1663 boolean_t vdir
= vtype
== VDIR
;
1664 boolean_t vreg
= vtype
== VREG
;
1665 boolean_t passthrough
, passthrough_x
, noallow
;
1668 zfsvfs
->z_acl_inherit
== ZFS_ACL_PASSTHROUGH_X
;
1669 passthrough
= passthrough_x
||
1670 zfsvfs
->z_acl_inherit
== ZFS_ACL_PASSTHROUGH
;
1672 zfsvfs
->z_acl_inherit
== ZFS_ACL_NOALLOW
;
1674 *need_chmod
= B_TRUE
;
1676 aclp
= zfs_acl_alloc(paclp
->z_version
);
1677 if (zfsvfs
->z_acl_inherit
== ZFS_ACL_DISCARD
)
1679 while (pacep
= zfs_acl_next_ace(paclp
, pacep
, &who
,
1680 &access_mask
, &iflags
, &type
)) {
1683 * don't inherit bogus ACEs
1685 if (!zfs_acl_valid_ace_type(type
, iflags
))
1688 if (noallow
&& type
== ALLOW
)
1691 ace_size
= aclp
->z_ops
.ace_size(pacep
);
1693 if (!zfs_ace_can_use(vtype
, iflags
))
1697 * If owner@, group@, or everyone@ inheritable
1698 * then zfs_acl_chmod() isn't needed.
1701 ((iflags
& (ACE_OWNER
|ACE_EVERYONE
)) ||
1702 ((iflags
& OWNING_GROUP
) ==
1703 OWNING_GROUP
)) && (vreg
|| (vdir
&& (iflags
&
1704 ACE_DIRECTORY_INHERIT_ACE
)))) {
1705 *need_chmod
= B_FALSE
;
1707 if (!vdir
&& passthrough_x
&&
1708 ((mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)) {
1709 access_mask
&= ~ACE_EXECUTE
;
1713 aclnode
= zfs_acl_node_alloc(ace_size
);
1714 list_insert_tail(&aclp
->z_acl
, aclnode
);
1715 acep
= aclnode
->z_acldata
;
1717 zfs_set_ace(aclp
, acep
, access_mask
, type
,
1718 who
, iflags
|ACE_INHERITED_ACE
);
1721 * Copy special opaque data if any
1723 if ((data1sz
= paclp
->z_ops
.ace_data(pacep
, &data1
)) != 0) {
1724 VERIFY((data2sz
= aclp
->z_ops
.ace_data(acep
,
1725 &data2
)) == data1sz
);
1726 bcopy(data1
, data2
, data2sz
);
1728 aclp
->z_acl_count
++;
1729 aclnode
->z_ace_count
++;
1730 aclp
->z_acl_bytes
+= aclnode
->z_size
;
1731 newflags
= aclp
->z_ops
.ace_flags_get(acep
);
1734 aclp
->z_hints
|= ZFS_INHERIT_ACE
;
1736 if ((iflags
& ACE_NO_PROPAGATE_INHERIT_ACE
) || !vdir
) {
1737 newflags
&= ~ALL_INHERIT
;
1738 aclp
->z_ops
.ace_flags_set(acep
,
1739 newflags
|ACE_INHERITED_ACE
);
1740 zfs_restricted_update(zfsvfs
, aclp
, acep
);
1746 newflags
= aclp
->z_ops
.ace_flags_get(acep
);
1747 if ((iflags
& (ACE_FILE_INHERIT_ACE
|
1748 ACE_DIRECTORY_INHERIT_ACE
)) !=
1749 ACE_FILE_INHERIT_ACE
) {
1750 aclnode2
= zfs_acl_node_alloc(ace_size
);
1751 list_insert_tail(&aclp
->z_acl
, aclnode2
);
1752 acep2
= aclnode2
->z_acldata
;
1753 zfs_set_ace(aclp
, acep2
,
1754 access_mask
, type
, who
,
1755 iflags
|ACE_INHERITED_ACE
);
1756 newflags
|= ACE_INHERIT_ONLY_ACE
;
1757 aclp
->z_ops
.ace_flags_set(acep
, newflags
);
1758 newflags
&= ~ALL_INHERIT
;
1759 aclp
->z_ops
.ace_flags_set(acep2
,
1760 newflags
|ACE_INHERITED_ACE
);
1763 * Copy special opaque data if any
1765 if ((data1sz
= aclp
->z_ops
.ace_data(acep
,
1768 aclp
->z_ops
.ace_data(acep2
,
1769 &data2
)) == data1sz
);
1770 bcopy(data1
, data2
, data1sz
);
1772 aclp
->z_acl_count
++;
1773 aclnode2
->z_ace_count
++;
1774 aclp
->z_acl_bytes
+= aclnode
->z_size
;
1775 zfs_restricted_update(zfsvfs
, aclp
, acep2
);
1777 newflags
|= ACE_INHERIT_ONLY_ACE
;
1778 aclp
->z_ops
.ace_flags_set(acep
,
1779 newflags
|ACE_INHERITED_ACE
);
1786 * Create file system object initial permissions
1787 * including inheritable ACEs.
1790 zfs_acl_ids_create(znode_t
*dzp
, int flag
, vattr_t
*vap
, cred_t
*cr
,
1791 vsecattr_t
*vsecp
, zfs_acl_ids_t
*acl_ids
)
1794 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1797 boolean_t need_chmod
= B_TRUE
;
1799 bzero(acl_ids
, sizeof (zfs_acl_ids_t
));
1800 acl_ids
->z_mode
= MAKEIMODE(vap
->va_type
, vap
->va_mode
);
1803 if ((error
= zfs_vsec_2_aclp(zfsvfs
, vap
->va_type
, vsecp
, cr
,
1804 &acl_ids
->z_fuidp
, &acl_ids
->z_aclp
)) != 0)
1808 * Determine uid and gid.
1810 if ((flag
& (IS_ROOT_NODE
| IS_REPLAY
)) ||
1811 ((flag
& IS_XATTR
) && (vap
->va_type
== VDIR
))) {
1812 acl_ids
->z_fuid
= zfs_fuid_create(zfsvfs
,
1813 (uint64_t)vap
->va_uid
, cr
,
1814 ZFS_OWNER
, &acl_ids
->z_fuidp
);
1815 acl_ids
->z_fgid
= zfs_fuid_create(zfsvfs
,
1816 (uint64_t)vap
->va_gid
, cr
,
1817 ZFS_GROUP
, &acl_ids
->z_fuidp
);
1820 acl_ids
->z_fuid
= zfs_fuid_create_cred(zfsvfs
, ZFS_OWNER
,
1821 cr
, &acl_ids
->z_fuidp
);
1822 acl_ids
->z_fgid
= 0;
1823 if (vap
->va_mask
& AT_GID
) {
1824 acl_ids
->z_fgid
= zfs_fuid_create(zfsvfs
,
1825 (uint64_t)vap
->va_gid
,
1826 cr
, ZFS_GROUP
, &acl_ids
->z_fuidp
);
1828 if (acl_ids
->z_fgid
!= dzp
->z_phys
->zp_gid
&&
1829 !groupmember(vap
->va_gid
, cr
) &&
1830 secpolicy_vnode_create_gid(cr
) != 0)
1831 acl_ids
->z_fgid
= 0;
1833 if (acl_ids
->z_fgid
== 0) {
1834 if (dzp
->z_phys
->zp_mode
& S_ISGID
) {
1835 acl_ids
->z_fgid
= dzp
->z_phys
->zp_gid
;
1836 gid
= zfs_fuid_map_id(zfsvfs
, acl_ids
->z_fgid
,
1839 acl_ids
->z_fgid
= zfs_fuid_create_cred(zfsvfs
,
1840 ZFS_GROUP
, cr
, &acl_ids
->z_fuidp
);
1847 * If we're creating a directory, and the parent directory has the
1848 * set-GID bit set, set in on the new directory.
1849 * Otherwise, if the user is neither privileged nor a member of the
1850 * file's new group, clear the file's set-GID bit.
1853 if (!(flag
& IS_ROOT_NODE
) && (dzp
->z_phys
->zp_mode
& S_ISGID
) &&
1854 (vap
->va_type
== VDIR
)) {
1855 acl_ids
->z_mode
|= S_ISGID
;
1857 if ((acl_ids
->z_mode
& S_ISGID
) &&
1858 secpolicy_vnode_setids_setgids(cr
, gid
) != 0)
1859 acl_ids
->z_mode
&= ~S_ISGID
;
1862 if (acl_ids
->z_aclp
== NULL
) {
1863 mutex_enter(&dzp
->z_lock
);
1864 if (!(flag
& IS_ROOT_NODE
) && (ZTOV(dzp
)->v_type
== VDIR
&&
1865 (dzp
->z_phys
->zp_flags
& ZFS_INHERIT_ACE
)) &&
1866 !(dzp
->z_phys
->zp_flags
& ZFS_XATTR
)) {
1867 mutex_enter(&dzp
->z_acl_lock
);
1868 VERIFY(0 == zfs_acl_node_read(dzp
, &paclp
, B_FALSE
));
1869 mutex_exit(&dzp
->z_acl_lock
);
1870 acl_ids
->z_aclp
= zfs_acl_inherit(zfsvfs
,
1871 vap
->va_type
, paclp
, acl_ids
->z_mode
, &need_chmod
);
1872 zfs_acl_free(paclp
);
1875 zfs_acl_alloc(zfs_acl_version_zp(dzp
));
1877 mutex_exit(&dzp
->z_lock
);
1879 acl_ids
->z_aclp
->z_hints
= (vap
->va_type
== VDIR
) ?
1880 ZFS_ACL_AUTO_INHERIT
: 0;
1881 zfs_acl_chmod(zfsvfs
, acl_ids
->z_fuid
,
1882 acl_ids
->z_mode
, acl_ids
->z_aclp
);
1890 * Free ACL and fuid_infop, but not the acl_ids structure
1893 zfs_acl_ids_free(zfs_acl_ids_t
*acl_ids
)
1895 if (acl_ids
->z_aclp
)
1896 zfs_acl_free(acl_ids
->z_aclp
);
1897 if (acl_ids
->z_fuidp
)
1898 zfs_fuid_info_free(acl_ids
->z_fuidp
);
1899 acl_ids
->z_aclp
= NULL
;
1900 acl_ids
->z_fuidp
= NULL
;
1904 zfs_acl_ids_overquota(zfsvfs_t
*zfsvfs
, zfs_acl_ids_t
*acl_ids
)
1906 return (zfs_usergroup_overquota(zfsvfs
, B_FALSE
, acl_ids
->z_fuid
) ||
1907 zfs_usergroup_overquota(zfsvfs
, B_TRUE
, acl_ids
->z_fgid
));
1911 * Retrieve a files ACL
1914 zfs_getacl(znode_t
*zp
, vsecattr_t
*vsecp
, boolean_t skipaclchk
, cred_t
*cr
)
1922 mask
= vsecp
->vsa_mask
& (VSA_ACE
| VSA_ACECNT
|
1923 VSA_ACE_ACLFLAGS
| VSA_ACE_ALLTYPES
);
1925 if (error
= zfs_zaccess(zp
, ACE_READ_ACL
, 0, skipaclchk
, cr
))
1931 mutex_enter(&zp
->z_acl_lock
);
1933 error
= zfs_acl_node_read(zp
, &aclp
, B_FALSE
);
1935 mutex_exit(&zp
->z_acl_lock
);
1940 * Scan ACL to determine number of ACEs
1942 if ((zp
->z_phys
->zp_flags
& ZFS_ACL_OBJ_ACE
) &&
1943 !(mask
& VSA_ACE_ALLTYPES
)) {
1946 uint32_t access_mask
;
1947 uint16_t type
, iflags
;
1949 while (zacep
= zfs_acl_next_ace(aclp
, zacep
,
1950 &who
, &access_mask
, &iflags
, &type
)) {
1952 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE
:
1953 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE
:
1954 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE
:
1955 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE
:
1962 vsecp
->vsa_aclcnt
= count
;
1964 count
= aclp
->z_acl_count
;
1966 if (mask
& VSA_ACECNT
) {
1967 vsecp
->vsa_aclcnt
= count
;
1970 if (mask
& VSA_ACE
) {
1973 zfs_acl_node_t
*aclnode
= list_head(&aclp
->z_acl
);
1975 aclsz
= count
* sizeof (ace_t
) +
1976 sizeof (ace_object_t
) * largeace
;
1978 vsecp
->vsa_aclentp
= kmem_alloc(aclsz
, KM_SLEEP
);
1979 vsecp
->vsa_aclentsz
= aclsz
;
1981 if (aclp
->z_version
== ZFS_ACL_VERSION_FUID
)
1982 zfs_copy_fuid_2_ace(zp
->z_zfsvfs
, aclp
, cr
,
1983 vsecp
->vsa_aclentp
, !(mask
& VSA_ACE_ALLTYPES
));
1985 bcopy(aclnode
->z_acldata
, vsecp
->vsa_aclentp
,
1986 count
* sizeof (ace_t
));
1989 if (mask
& VSA_ACE_ACLFLAGS
) {
1990 vsecp
->vsa_aclflags
= 0;
1991 if (zp
->z_phys
->zp_flags
& ZFS_ACL_DEFAULTED
)
1992 vsecp
->vsa_aclflags
|= ACL_DEFAULTED
;
1993 if (zp
->z_phys
->zp_flags
& ZFS_ACL_PROTECTED
)
1994 vsecp
->vsa_aclflags
|= ACL_PROTECTED
;
1995 if (zp
->z_phys
->zp_flags
& ZFS_ACL_AUTO_INHERIT
)
1996 vsecp
->vsa_aclflags
|= ACL_AUTO_INHERIT
;
1999 mutex_exit(&zp
->z_acl_lock
);
2007 zfs_vsec_2_aclp(zfsvfs_t
*zfsvfs
, vtype_t obj_type
,
2008 vsecattr_t
*vsecp
, cred_t
*cr
, zfs_fuid_info_t
**fuidp
, zfs_acl_t
**zaclp
)
2011 zfs_acl_node_t
*aclnode
;
2012 int aclcnt
= vsecp
->vsa_aclcnt
;
2015 if (vsecp
->vsa_aclcnt
> MAX_ACL_ENTRIES
|| vsecp
->vsa_aclcnt
<= 0)
2018 aclp
= zfs_acl_alloc(zfs_acl_version(zfsvfs
->z_version
));
2021 aclnode
= zfs_acl_node_alloc(aclcnt
* sizeof (zfs_object_ace_t
));
2022 if (aclp
->z_version
== ZFS_ACL_VERSION_INITIAL
) {
2023 if ((error
= zfs_copy_ace_2_oldace(obj_type
, aclp
,
2024 (ace_t
*)vsecp
->vsa_aclentp
, aclnode
->z_acldata
,
2025 aclcnt
, &aclnode
->z_size
)) != 0) {
2027 zfs_acl_node_free(aclnode
);
2031 if ((error
= zfs_copy_ace_2_fuid(zfsvfs
, obj_type
, aclp
,
2032 vsecp
->vsa_aclentp
, aclnode
->z_acldata
, aclcnt
,
2033 &aclnode
->z_size
, fuidp
, cr
)) != 0) {
2035 zfs_acl_node_free(aclnode
);
2039 aclp
->z_acl_bytes
= aclnode
->z_size
;
2040 aclnode
->z_ace_count
= aclcnt
;
2041 aclp
->z_acl_count
= aclcnt
;
2042 list_insert_head(&aclp
->z_acl
, aclnode
);
2045 * If flags are being set then add them to z_hints
2047 if (vsecp
->vsa_mask
& VSA_ACE_ACLFLAGS
) {
2048 if (vsecp
->vsa_aclflags
& ACL_PROTECTED
)
2049 aclp
->z_hints
|= ZFS_ACL_PROTECTED
;
2050 if (vsecp
->vsa_aclflags
& ACL_DEFAULTED
)
2051 aclp
->z_hints
|= ZFS_ACL_DEFAULTED
;
2052 if (vsecp
->vsa_aclflags
& ACL_AUTO_INHERIT
)
2053 aclp
->z_hints
|= ZFS_ACL_AUTO_INHERIT
;
2065 zfs_setacl(znode_t
*zp
, vsecattr_t
*vsecp
, boolean_t skipaclchk
, cred_t
*cr
)
2067 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2068 zilog_t
*zilog
= zfsvfs
->z_log
;
2069 ulong_t mask
= vsecp
->vsa_mask
& (VSA_ACE
| VSA_ACECNT
);
2073 zfs_fuid_info_t
*fuidp
= NULL
;
2074 boolean_t fuid_dirtied
;
2079 if (zp
->z_phys
->zp_flags
& ZFS_IMMUTABLE
)
2082 if (error
= zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
))
2085 error
= zfs_vsec_2_aclp(zfsvfs
, ZTOV(zp
)->v_type
, vsecp
, cr
, &fuidp
,
2091 * If ACL wide flags aren't being set then preserve any
2094 if (!(vsecp
->vsa_mask
& VSA_ACE_ACLFLAGS
)) {
2095 aclp
->z_hints
|= (zp
->z_phys
->zp_flags
& V4_ACL_WIDE_FLAGS
);
2098 if (error
= zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
)) {
2103 mutex_enter(&zp
->z_lock
);
2104 mutex_enter(&zp
->z_acl_lock
);
2106 tx
= dmu_tx_create(zfsvfs
->z_os
);
2107 dmu_tx_hold_bonus(tx
, zp
->z_id
);
2109 if (zp
->z_phys
->zp_acl
.z_acl_extern_obj
) {
2110 /* Are we upgrading ACL? */
2111 if (zfsvfs
->z_version
<= ZPL_VERSION_FUID
&&
2112 zp
->z_phys
->zp_acl
.z_acl_version
==
2113 ZFS_ACL_VERSION_INITIAL
) {
2114 dmu_tx_hold_free(tx
,
2115 zp
->z_phys
->zp_acl
.z_acl_extern_obj
,
2117 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2118 0, aclp
->z_acl_bytes
);
2120 dmu_tx_hold_write(tx
,
2121 zp
->z_phys
->zp_acl
.z_acl_extern_obj
,
2122 0, aclp
->z_acl_bytes
);
2124 } else if (aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2125 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, aclp
->z_acl_bytes
);
2127 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2129 zfs_fuid_txhold(zfsvfs
, tx
);
2131 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
2133 mutex_exit(&zp
->z_acl_lock
);
2134 mutex_exit(&zp
->z_lock
);
2136 if (error
== ERESTART
) {
2146 error
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2150 zfs_fuid_sync(zfsvfs
, tx
);
2152 zfs_time_stamper_locked(zp
, STATE_CHANGED
, tx
);
2153 zfs_log_acl(zilog
, tx
, zp
, vsecp
, fuidp
);
2156 zfs_fuid_info_free(fuidp
);
2160 mutex_exit(&zp
->z_acl_lock
);
2161 mutex_exit(&zp
->z_lock
);
2167 * Check accesses of interest (AoI) against attributes of the dataset
2168 * such as read-only. Returns zero if no AoI conflict with dataset
2169 * attributes, otherwise an appropriate errno is returned.
2172 zfs_zaccess_dataset_check(znode_t
*zp
, uint32_t v4_mode
)
2174 if ((v4_mode
& WRITE_MASK
) &&
2175 (zp
->z_zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) &&
2176 (!IS_DEVVP(ZTOV(zp
)) ||
2177 (IS_DEVVP(ZTOV(zp
)) && (v4_mode
& WRITE_MASK_ATTRS
)))) {
2182 * Only check for READONLY on non-directories.
2184 if ((v4_mode
& WRITE_MASK_DATA
) &&
2185 (((ZTOV(zp
)->v_type
!= VDIR
) &&
2186 (zp
->z_phys
->zp_flags
& (ZFS_READONLY
| ZFS_IMMUTABLE
))) ||
2187 (ZTOV(zp
)->v_type
== VDIR
&&
2188 (zp
->z_phys
->zp_flags
& ZFS_IMMUTABLE
)))) {
2192 if ((v4_mode
& (ACE_DELETE
| ACE_DELETE_CHILD
)) &&
2193 (zp
->z_phys
->zp_flags
& ZFS_NOUNLINK
)) {
2197 if (((v4_mode
& (ACE_READ_DATA
|ACE_EXECUTE
)) &&
2198 (zp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
))) {
2206 * The primary usage of this function is to loop through all of the
2207 * ACEs in the znode, determining what accesses of interest (AoI) to
2208 * the caller are allowed or denied. The AoI are expressed as bits in
2209 * the working_mode parameter. As each ACE is processed, bits covered
2210 * by that ACE are removed from the working_mode. This removal
2211 * facilitates two things. The first is that when the working mode is
2212 * empty (= 0), we know we've looked at all the AoI. The second is
2213 * that the ACE interpretation rules don't allow a later ACE to undo
2214 * something granted or denied by an earlier ACE. Removing the
2215 * discovered access or denial enforces this rule. At the end of
2216 * processing the ACEs, all AoI that were found to be denied are
2217 * placed into the working_mode, giving the caller a mask of denied
2218 * accesses. Returns:
2219 * 0 if all AoI granted
2220 * EACCESS if the denied mask is non-zero
2221 * other error if abnormal failure (e.g., IO error)
2223 * A secondary usage of the function is to determine if any of the
2224 * AoI are granted. If an ACE grants any access in
2225 * the working_mode, we immediately short circuit out of the function.
2226 * This mode is chosen by setting anyaccess to B_TRUE. The
2227 * working_mode is not a denied access mask upon exit if the function
2228 * is used in this manner.
2231 zfs_zaccess_aces_check(znode_t
*zp
, uint32_t *working_mode
,
2232 boolean_t anyaccess
, cred_t
*cr
)
2234 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2237 uid_t uid
= crgetuid(cr
);
2239 uint16_t type
, iflags
;
2240 uint16_t entry_type
;
2241 uint32_t access_mask
;
2242 uint32_t deny_mask
= 0;
2243 zfs_ace_hdr_t
*acep
= NULL
;
2248 zfs_fuid_map_ids(zp
, cr
, &fowner
, &gowner
);
2250 mutex_enter(&zp
->z_acl_lock
);
2252 error
= zfs_acl_node_read(zp
, &aclp
, B_FALSE
);
2254 mutex_exit(&zp
->z_acl_lock
);
2258 while (acep
= zfs_acl_next_ace(aclp
, acep
, &who
, &access_mask
,
2260 uint32_t mask_matched
;
2262 if (!zfs_acl_valid_ace_type(type
, iflags
))
2265 if (ZTOV(zp
)->v_type
== VDIR
&& (iflags
& ACE_INHERIT_ONLY_ACE
))
2268 /* Skip ACE if it does not affect any AoI */
2269 mask_matched
= (access_mask
& *working_mode
);
2273 entry_type
= (iflags
& ACE_TYPE_FLAGS
);
2277 switch (entry_type
) {
2285 case ACE_IDENTIFIER_GROUP
:
2286 checkit
= zfs_groupmember(zfsvfs
, who
, cr
);
2294 if (entry_type
== 0) {
2297 newid
= zfs_fuid_map_id(zfsvfs
, who
, cr
,
2299 if (newid
!= IDMAP_WK_CREATOR_OWNER_UID
&&
2305 mutex_exit(&zp
->z_acl_lock
);
2312 DTRACE_PROBE3(zfs__ace__denies
,
2314 zfs_ace_hdr_t
*, acep
,
2315 uint32_t, mask_matched
);
2316 deny_mask
|= mask_matched
;
2318 DTRACE_PROBE3(zfs__ace__allows
,
2320 zfs_ace_hdr_t
*, acep
,
2321 uint32_t, mask_matched
);
2323 mutex_exit(&zp
->z_acl_lock
);
2328 *working_mode
&= ~mask_matched
;
2332 if (*working_mode
== 0)
2336 mutex_exit(&zp
->z_acl_lock
);
2339 /* Put the found 'denies' back on the working mode */
2341 *working_mode
|= deny_mask
;
2343 } else if (*working_mode
) {
2351 * Return true if any access whatsoever granted, we don't actually
2352 * care what access is granted.
2355 zfs_has_access(znode_t
*zp
, cred_t
*cr
)
2357 uint32_t have
= ACE_ALL_PERMS
;
2359 if (zfs_zaccess_aces_check(zp
, &have
, B_TRUE
, cr
) != 0) {
2362 owner
= zfs_fuid_map_id(zp
->z_zfsvfs
,
2363 zp
->z_phys
->zp_uid
, cr
, ZFS_OWNER
);
2366 secpolicy_vnode_access(cr
, ZTOV(zp
), owner
, VREAD
) == 0 ||
2367 secpolicy_vnode_access(cr
, ZTOV(zp
), owner
, VWRITE
) == 0 ||
2368 secpolicy_vnode_access(cr
, ZTOV(zp
), owner
, VEXEC
) == 0 ||
2369 secpolicy_vnode_chown(cr
, B_TRUE
) == 0 ||
2370 secpolicy_vnode_chown(cr
, B_FALSE
) == 0 ||
2371 secpolicy_vnode_setdac(cr
, owner
) == 0 ||
2372 secpolicy_vnode_remove(cr
) == 0);
2378 zfs_zaccess_common(znode_t
*zp
, uint32_t v4_mode
, uint32_t *working_mode
,
2379 boolean_t
*check_privs
, boolean_t skipaclchk
, cred_t
*cr
)
2381 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2384 *working_mode
= v4_mode
;
2385 *check_privs
= B_TRUE
;
2388 * Short circuit empty requests
2390 if (v4_mode
== 0 || zfsvfs
->z_replay
) {
2395 if ((err
= zfs_zaccess_dataset_check(zp
, v4_mode
)) != 0) {
2396 *check_privs
= B_FALSE
;
2401 * The caller requested that the ACL check be skipped. This
2402 * would only happen if the caller checked VOP_ACCESS() with a
2403 * 32 bit ACE mask and already had the appropriate permissions.
2410 return (zfs_zaccess_aces_check(zp
, working_mode
, B_FALSE
, cr
));
2414 zfs_zaccess_append(znode_t
*zp
, uint32_t *working_mode
, boolean_t
*check_privs
,
2417 if (*working_mode
!= ACE_WRITE_DATA
)
2420 return (zfs_zaccess_common(zp
, ACE_APPEND_DATA
, working_mode
,
2421 check_privs
, B_FALSE
, cr
));
2425 * Determine whether Access should be granted/denied, invoking least
2426 * priv subsytem when a deny is determined.
2429 zfs_zaccess(znode_t
*zp
, int mode
, int flags
, boolean_t skipaclchk
, cred_t
*cr
)
2431 uint32_t working_mode
;
2434 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2435 boolean_t check_privs
;
2437 znode_t
*check_zp
= zp
;
2439 is_attr
= ((zp
->z_phys
->zp_flags
& ZFS_XATTR
) &&
2440 (ZTOV(zp
)->v_type
== VDIR
));
2443 * If attribute then validate against base file
2446 if ((error
= zfs_zget(zp
->z_zfsvfs
,
2447 zp
->z_phys
->zp_parent
, &xzp
)) != 0) {
2454 * fixup mode to map to xattr perms
2457 if (mode
& (ACE_WRITE_DATA
|ACE_APPEND_DATA
)) {
2458 mode
&= ~(ACE_WRITE_DATA
|ACE_APPEND_DATA
);
2459 mode
|= ACE_WRITE_NAMED_ATTRS
;
2462 if (mode
& (ACE_READ_DATA
|ACE_EXECUTE
)) {
2463 mode
&= ~(ACE_READ_DATA
|ACE_EXECUTE
);
2464 mode
|= ACE_READ_NAMED_ATTRS
;
2468 if ((error
= zfs_zaccess_common(check_zp
, mode
, &working_mode
,
2469 &check_privs
, skipaclchk
, cr
)) == 0) {
2475 if (error
&& !check_privs
) {
2481 if (error
&& (flags
& V_APPEND
)) {
2482 error
= zfs_zaccess_append(zp
, &working_mode
, &check_privs
, cr
);
2485 if (error
&& check_privs
) {
2487 mode_t checkmode
= 0;
2489 owner
= zfs_fuid_map_id(zfsvfs
, check_zp
->z_phys
->zp_uid
, cr
,
2493 * First check for implicit owner permission on
2494 * read_acl/read_attributes
2498 ASSERT(working_mode
!= 0);
2500 if ((working_mode
& (ACE_READ_ACL
|ACE_READ_ATTRIBUTES
) &&
2501 owner
== crgetuid(cr
)))
2502 working_mode
&= ~(ACE_READ_ACL
|ACE_READ_ATTRIBUTES
);
2504 if (working_mode
& (ACE_READ_DATA
|ACE_READ_NAMED_ATTRS
|
2505 ACE_READ_ACL
|ACE_READ_ATTRIBUTES
|ACE_SYNCHRONIZE
))
2507 if (working_mode
& (ACE_WRITE_DATA
|ACE_WRITE_NAMED_ATTRS
|
2508 ACE_APPEND_DATA
|ACE_WRITE_ATTRIBUTES
|ACE_SYNCHRONIZE
))
2509 checkmode
|= VWRITE
;
2510 if (working_mode
& ACE_EXECUTE
)
2514 error
= secpolicy_vnode_access(cr
, ZTOV(check_zp
),
2517 if (error
== 0 && (working_mode
& ACE_WRITE_OWNER
))
2518 error
= secpolicy_vnode_chown(cr
, B_TRUE
);
2519 if (error
== 0 && (working_mode
& ACE_WRITE_ACL
))
2520 error
= secpolicy_vnode_setdac(cr
, owner
);
2522 if (error
== 0 && (working_mode
&
2523 (ACE_DELETE
|ACE_DELETE_CHILD
)))
2524 error
= secpolicy_vnode_remove(cr
);
2526 if (error
== 0 && (working_mode
& ACE_SYNCHRONIZE
)) {
2527 error
= secpolicy_vnode_chown(cr
, B_FALSE
);
2531 * See if any bits other than those already checked
2532 * for are still present. If so then return EACCES
2534 if (working_mode
& ~(ZFS_CHECKED_MASKS
)) {
2547 * Translate traditional unix VREAD/VWRITE/VEXEC mode into
2548 * native ACL format and call zfs_zaccess()
2551 zfs_zaccess_rwx(znode_t
*zp
, mode_t mode
, int flags
, cred_t
*cr
)
2553 return (zfs_zaccess(zp
, zfs_unix_to_v4(mode
>> 6), flags
, B_FALSE
, cr
));
2557 * Access function for secpolicy_vnode_setattr
2560 zfs_zaccess_unix(znode_t
*zp
, mode_t mode
, cred_t
*cr
)
2562 int v4_mode
= zfs_unix_to_v4(mode
>> 6);
2564 return (zfs_zaccess(zp
, v4_mode
, 0, B_FALSE
, cr
));
2568 zfs_delete_final_check(znode_t
*zp
, znode_t
*dzp
,
2569 mode_t missing_perms
, cred_t
*cr
)
2573 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2575 downer
= zfs_fuid_map_id(zfsvfs
, dzp
->z_phys
->zp_uid
, cr
, ZFS_OWNER
);
2577 error
= secpolicy_vnode_access(cr
, ZTOV(dzp
), downer
, missing_perms
);
2580 error
= zfs_sticky_remove_access(dzp
, zp
, cr
);
2586 * Determine whether Access should be granted/deny, without
2587 * consulting least priv subsystem.
2590 * The following chart is the recommended NFSv4 enforcement for
2591 * ability to delete an object.
2593 * -------------------------------------------------------
2594 * | Parent Dir | Target Object Permissions |
2596 * -------------------------------------------------------
2597 * | | ACL Allows | ACL Denies| Delete |
2598 * | | Delete | Delete | unspecified|
2599 * -------------------------------------------------------
2600 * | ACL Allows | Permit | Permit | Permit |
2601 * | DELETE_CHILD | |
2602 * -------------------------------------------------------
2603 * | ACL Denies | Permit | Deny | Deny |
2604 * | DELETE_CHILD | | | |
2605 * -------------------------------------------------------
2606 * | ACL specifies | | | |
2607 * | only allow | Permit | Permit | Permit |
2608 * | write and | | | |
2610 * -------------------------------------------------------
2611 * | ACL denies | | | |
2612 * | write and | Permit | Deny | Deny |
2614 * -------------------------------------------------------
2617 * No search privilege, can't even look up file?
2621 zfs_zaccess_delete(znode_t
*dzp
, znode_t
*zp
, cred_t
*cr
)
2623 uint32_t dzp_working_mode
= 0;
2624 uint32_t zp_working_mode
= 0;
2625 int dzp_error
, zp_error
;
2626 mode_t missing_perms
;
2627 boolean_t dzpcheck_privs
= B_TRUE
;
2628 boolean_t zpcheck_privs
= B_TRUE
;
2631 * We want specific DELETE permissions to
2632 * take precedence over WRITE/EXECUTE. We don't
2633 * want an ACL such as this to mess us up.
2634 * user:joe:write_data:deny,user:joe:delete:allow
2636 * However, deny permissions may ultimately be overridden
2637 * by secpolicy_vnode_access().
2639 * We will ask for all of the necessary permissions and then
2640 * look at the working modes from the directory and target object
2641 * to determine what was found.
2644 if (zp
->z_phys
->zp_flags
& (ZFS_IMMUTABLE
| ZFS_NOUNLINK
))
2649 * If the directory permissions allow the delete, we are done.
2651 if ((dzp_error
= zfs_zaccess_common(dzp
, ACE_DELETE_CHILD
,
2652 &dzp_working_mode
, &dzpcheck_privs
, B_FALSE
, cr
)) == 0)
2656 * If target object has delete permission then we are done
2658 if ((zp_error
= zfs_zaccess_common(zp
, ACE_DELETE
, &zp_working_mode
,
2659 &zpcheck_privs
, B_FALSE
, cr
)) == 0)
2662 ASSERT(dzp_error
&& zp_error
);
2664 if (!dzpcheck_privs
)
2672 * If directory returns EACCES then delete_child was denied
2673 * due to deny delete_child. In this case send the request through
2674 * secpolicy_vnode_remove(). We don't use zfs_delete_final_check()
2675 * since that *could* allow the delete based on write/execute permission
2676 * and we want delete permissions to override write/execute.
2679 if (dzp_error
== EACCES
)
2680 return (secpolicy_vnode_remove(cr
));
2684 * only need to see if we have write/execute on directory.
2687 if ((dzp_error
= zfs_zaccess_common(dzp
, ACE_EXECUTE
|ACE_WRITE_DATA
,
2688 &dzp_working_mode
, &dzpcheck_privs
, B_FALSE
, cr
)) == 0)
2689 return (zfs_sticky_remove_access(dzp
, zp
, cr
));
2691 if (!dzpcheck_privs
)
2698 missing_perms
= (dzp_working_mode
& ACE_WRITE_DATA
) ? VWRITE
: 0;
2699 missing_perms
|= (dzp_working_mode
& ACE_EXECUTE
) ? VEXEC
: 0;
2701 ASSERT(missing_perms
);
2703 return (zfs_delete_final_check(zp
, dzp
, missing_perms
, cr
));
2708 zfs_zaccess_rename(znode_t
*sdzp
, znode_t
*szp
, znode_t
*tdzp
,
2709 znode_t
*tzp
, cred_t
*cr
)
2714 if (szp
->z_phys
->zp_flags
& ZFS_AV_QUARANTINED
)
2717 add_perm
= (ZTOV(szp
)->v_type
== VDIR
) ?
2718 ACE_ADD_SUBDIRECTORY
: ACE_ADD_FILE
;
2721 * Rename permissions are combination of delete permission +
2722 * add file/subdir permission.
2726 * first make sure we do the delete portion.
2728 * If that succeeds then check for add_file/add_subdir permissions
2731 if (error
= zfs_zaccess_delete(sdzp
, szp
, cr
))
2735 * If we have a tzp, see if we can delete it?
2738 if (error
= zfs_zaccess_delete(tdzp
, tzp
, cr
))
2743 * Now check for add permissions
2745 error
= zfs_zaccess(tdzp
, add_perm
, 0, B_FALSE
, cr
);