xref: /freebsd/sys/contrib/openzfs/include/sys/zfs_acl.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2026, TrueNAS.
15  */
16 
17 #ifndef	_SYS_FS_ZFS_ACL_H
18 #define	_SYS_FS_ZFS_ACL_H
19 
20 #ifdef _KERNEL
21 #include <sys/isa_defs.h>
22 #include <sys/types32.h>
23 #endif
24 #include <sys/xvattr.h>
25 #include <sys/acl.h>
26 #include <sys/dmu.h>
27 #include <sys/zfs_fuid.h>
28 #include <sys/sa.h>
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 struct znode_phys;
35 
36 #define	ACE_SLOT_CNT	6
37 #define	ZFS_ACL_VERSION_INITIAL 0ULL
38 #define	ZFS_ACL_VERSION_FUID	1ULL
39 #define	ZFS_ACL_VERSION		ZFS_ACL_VERSION_FUID
40 
41 /*
42  * ZFS ACLs (Access Control Lists) are stored in various forms.
43  *
44  * Files created with ACL version ZFS_ACL_VERSION_INITIAL
45  * will all be created with fixed length ACEs of type
46  * zfs_oldace_t.
47  *
48  * Files with ACL version ZFS_ACL_VERSION_FUID will be created
49  * with various sized ACEs.  The abstraction entries will utilize
50  * zfs_ace_hdr_t, normal user/group entries will use zfs_ace_t
51  * and some specialized CIFS ACEs will use zfs_object_ace_t.
52  */
53 
54 /*
55  * All ACEs have a common hdr.  For
56  * owner@, group@, and everyone@ this is all
57  * that's needed.
58  */
59 typedef struct zfs_ace_hdr {
60 	uint16_t z_type;
61 	uint16_t z_flags;
62 	uint32_t z_access_mask;
63 } zfs_ace_hdr_t;
64 
65 typedef zfs_ace_hdr_t zfs_ace_abstract_t;
66 
67 /*
68  * Standard ACE
69  */
70 typedef struct zfs_ace {
71 	zfs_ace_hdr_t	z_hdr;
72 	uint64_t	z_fuid;
73 } zfs_ace_t;
74 
75 /*
76  * The following type only applies to ACE_ACCESS_ALLOWED|DENIED_OBJECT_ACE_TYPE
77  * and will only be set/retrieved in a CIFS context.
78  */
79 
80 typedef struct zfs_object_ace {
81 	zfs_ace_t	z_ace;
82 	uint8_t		z_object_type[16]; /* object type */
83 	uint8_t		z_inherit_type[16]; /* inherited object type */
84 } zfs_object_ace_t;
85 
86 typedef struct zfs_oldace {
87 	uint32_t	z_fuid;		/* "who" */
88 	uint32_t	z_access_mask;  /* access mask */
89 	uint16_t	z_flags;	/* flags, i.e inheritance */
90 	uint16_t	z_type;		/* type of entry allow/deny */
91 } zfs_oldace_t;
92 
93 typedef struct zfs_acl_phys_v0 {
94 	uint64_t	z_acl_extern_obj;	/* ext acl pieces */
95 	uint32_t	z_acl_count;		/* Number of ACEs */
96 	uint16_t	z_acl_version;		/* acl version */
97 	uint16_t	z_acl_pad;		/* pad */
98 	zfs_oldace_t	z_ace_data[ACE_SLOT_CNT]; /* 6 standard ACEs */
99 } zfs_acl_phys_v0_t;
100 
101 #define	ZFS_ACE_SPACE	(sizeof (zfs_oldace_t) * ACE_SLOT_CNT)
102 
103 /*
104  * Size of ACL count is always 2 bytes.
105  * Necessary to for dealing with both V0 ACL and V1 ACL layout
106  */
107 #define	ZFS_ACL_COUNT_SIZE	(sizeof (uint16_t))
108 
109 typedef struct zfs_acl_phys {
110 	uint64_t	z_acl_extern_obj;	  /* ext acl pieces */
111 	uint32_t	z_acl_size;		  /* Number of bytes in ACL */
112 	uint16_t	z_acl_version;		  /* acl version */
113 	uint16_t	z_acl_count;		  /* ace count */
114 	uint8_t	z_ace_data[ZFS_ACE_SPACE]; /* space for embedded ACEs */
115 } zfs_acl_phys_t;
116 
117 typedef struct acl_ops {
118 	uint32_t	(*ace_mask_get) (void *acep); /* get  access mask */
119 	void 		(*ace_mask_set) (void *acep,
120 			    uint32_t mask); /* set access mask */
121 	uint16_t	(*ace_flags_get) (void *acep);	/* get flags */
122 	void		(*ace_flags_set) (void *acep,
123 			    uint16_t flags); /* set flags */
124 	uint16_t	(*ace_type_get)(void *acep); /* get type */
125 	void		(*ace_type_set)(void *acep,
126 			    uint16_t type); /* set type */
127 	uint64_t	(*ace_who_get)(void *acep); /* get who/fuid */
128 	void		(*ace_who_set)(void *acep,
129 			    uint64_t who); /* set who/fuid */
130 	size_t		(*ace_size)(void *acep); /* how big is this ace */
131 	size_t		(*ace_abstract_size)(void); /* sizeof abstract entry */
132 	int		(*ace_mask_off)(void); /* off of access mask in ace */
133 	/* ptr to data if any */
134 	int		(*ace_data)(void *acep, void **datap);
135 } acl_ops_t;
136 
137 /*
138  * A zfs_acl_t structure is composed of a list of zfs_acl_node_t's.
139  * Each node will have one or more ACEs associated with it.  You will
140  * only have multiple nodes during a chmod operation.   Normally only
141  * one node is required.
142  */
143 typedef struct zfs_acl_node {
144 	list_node_t	z_next;		/* Next chunk of ACEs */
145 	void		*z_acldata;	/* pointer into actual ACE(s) */
146 	void		*z_allocdata;	/* pointer to kmem allocated memory */
147 	size_t		z_allocsize;	/* Size of blob in bytes */
148 	size_t		z_size;		/* length of ACL data */
149 	uint64_t	z_ace_count;	/* number of ACEs in this acl node */
150 	int		z_ace_idx;	/* ace iterator positioned on */
151 } zfs_acl_node_t;
152 
153 typedef struct zfs_acl {
154 	uint64_t	z_acl_count;	/* Number of ACEs */
155 	size_t		z_acl_bytes;	/* Number of bytes in ACL */
156 	uint_t		z_version;	/* version of ACL */
157 	void		*z_next_ace;	/* pointer to next ACE */
158 	uint64_t	z_hints;	/* ACL hints (ZFS_INHERIT_ACE ...) */
159 	zfs_acl_node_t	*z_curr_node;	/* current node iterator is handling */
160 	list_t		z_acl;		/* chunks of ACE data */
161 	const acl_ops_t	*z_ops;		/* ACL operations */
162 } zfs_acl_t;
163 
164 typedef struct acl_locator_cb {
165 	zfs_acl_t *cb_aclp;
166 	zfs_acl_node_t *cb_acl_node;
167 } zfs_acl_locator_cb_t;
168 
169 #define	ACL_DATA_ALLOCED	0x1
170 #define	ZFS_ACL_SIZE(aclcnt)	(sizeof (ace_t) * (aclcnt))
171 
172 struct zfs_fuid_info;
173 
174 typedef struct zfs_acl_ids {
175 	uint64_t		z_fuid;		/* file owner fuid */
176 	uint64_t		z_fgid;		/* file group owner fuid */
177 	uint64_t		z_mode;		/* mode to set on create */
178 	zfs_acl_t		*z_aclp;	/* ACL to create with file */
179 	struct zfs_fuid_info 	*z_fuidp;	/* for tracking fuids for log */
180 } zfs_acl_ids_t;
181 
182 /*
183  * Property values for acl_mode and acl_inherit.
184  *
185  * acl_mode can take discard, noallow, groupmask and passthrough.
186  * whereas acl_inherit has secure instead of groupmask.
187  */
188 
189 #define	ZFS_ACL_DISCARD		0
190 #define	ZFS_ACL_NOALLOW		1
191 #define	ZFS_ACL_GROUPMASK	2
192 #define	ZFS_ACL_PASSTHROUGH	3
193 #define	ZFS_ACL_RESTRICTED	4
194 #define	ZFS_ACL_PASSTHROUGH_X	5
195 
196 struct znode;
197 struct zfsvfs;
198 
199 #ifdef _KERNEL
200 
201 /* Platform-specific implementations called by core. */
202 int zfs_getacl(struct znode *, vsecattr_t *, boolean_t, cred_t *);
203 int zfs_setacl(struct znode *, vsecattr_t *, boolean_t, cred_t *);
204 int zfs_zaccess(struct znode *, int, int, boolean_t, cred_t *);
205 int zfs_zaccess_rwx(struct znode *, mode_t, int, cred_t *);
206 uint64_t zfs_external_acl(struct znode *);
207 void zfs_acl_xform(struct znode *, zfs_acl_t *, cred_t *);
208 void zfs_acl_data_locator(void **, uint32_t *, uint32_t, boolean_t, void *);
209 uint64_t zfs_mode_compute(uint64_t, zfs_acl_t *,
210     uint64_t *, uint64_t, uint64_t);
211 int zfs_acl_node_read(struct znode *, boolean_t, zfs_acl_t **, boolean_t);
212 int zfs_acl_chown_setattr(struct znode *);
213 
214 /* Shared core implementations. */
215 void zfs_oldace_byteswap(ace_t *, int);
216 void zfs_ace_byteswap(void *, size_t, boolean_t);
217 
218 #endif
219 
220 #ifdef	__cplusplus
221 }
222 #endif
223 #endif	/* _SYS_FS_ZFS_ACL_H */
224