1 // SPDX-License-Identifier: GPL-2.0-only
3 * fs/nfs_common/nfsacl.c
5 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
9 * The Solaris nfsacl protocol represents some ACLs slightly differently
10 * than POSIX 1003.1e draft 17 does (and we do):
12 * - Minimal ACLs always have an ACL_MASK entry, so they have
13 * four instead of three entries.
14 * - The ACL_MASK entry in such minimal ACLs always has the same
15 * permissions as the ACL_GROUP_OBJ entry. (In extended ACLs
16 * the ACL_MASK and ACL_GROUP_OBJ entries may differ.)
17 * - The identifier fields of the ACL_USER_OBJ and ACL_GROUP_OBJ
18 * entries contain the identifiers of the owner and owning group.
19 * (In POSIX ACLs we always set them to ACL_UNDEFINED_ID).
20 * - ACL entries in the kernel are kept sorted in ascending order
21 * of (e_tag, e_id). Solaris ACLs are unsorted.
24 #include <linux/module.h>
26 #include <linux/gfp.h>
27 #include <linux/sunrpc/xdr.h>
28 #include <linux/nfsacl.h>
29 #include <linux/nfs3.h>
30 #include <linux/sort.h>
32 MODULE_DESCRIPTION("NFS ACL support");
33 MODULE_LICENSE("GPL");
35 struct nfsacl_encode_desc {
36 struct xdr_array2_desc desc;
38 struct posix_acl *acl;
44 struct nfsacl_simple_acl {
45 struct posix_acl_hdr acl;
46 struct posix_acl_entry ace[4];
50 xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem)
52 struct nfsacl_encode_desc *nfsacl_desc =
53 (struct nfsacl_encode_desc *) desc;
56 struct posix_acl_entry *entry =
57 &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
59 *p++ = htonl(entry->e_tag | nfsacl_desc->typeflag);
60 switch(entry->e_tag) {
62 *p++ = htonl(from_kuid(&init_user_ns, nfsacl_desc->uid));
65 *p++ = htonl(from_kgid(&init_user_ns, nfsacl_desc->gid));
68 *p++ = htonl(from_kuid(&init_user_ns, entry->e_uid));
71 *p++ = htonl(from_kgid(&init_user_ns, entry->e_gid));
73 default: /* Solaris depends on that! */
77 *p++ = htonl(entry->e_perm & S_IRWXO);
82 * nfsacl_encode - Encode an NFSv3 ACL
84 * @buf: destination xdr_buf to contain XDR encoded ACL
85 * @base: byte offset in xdr_buf where XDR'd ACL begins
86 * @inode: inode of file whose ACL this is
87 * @acl: posix_acl to encode
88 * @encode_entries: whether to encode ACEs as well
89 * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
91 * Returns size of encoded ACL in bytes or a negative errno value.
93 int nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
94 struct posix_acl *acl, int encode_entries, int typeflag)
96 int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
97 struct nfsacl_encode_desc nfsacl_desc = {
100 .array_len = encode_entries ? entries : 0,
101 .xcode = xdr_nfsace_encode,
104 .typeflag = typeflag,
108 struct nfsacl_simple_acl aclbuf;
111 if (entries > NFS_ACL_MAX_ENTRIES ||
112 xdr_encode_word(buf, base, entries))
114 if (encode_entries && acl && acl->a_count == 3) {
115 struct posix_acl *acl2 =
116 container_of(&aclbuf.acl, struct posix_acl, hdr);
118 /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
119 * invoked in contexts where a memory allocation failure is
120 * fatal. Fortunately this fake ACL is small enough to
121 * construct on the stack. */
122 posix_acl_init(acl2, 4);
124 /* Insert entries in canonical order: other orders seem
125 to confuse Solaris VxFS. */
126 acl2->a_entries[0] = acl->a_entries[0]; /* ACL_USER_OBJ */
127 acl2->a_entries[1] = acl->a_entries[1]; /* ACL_GROUP_OBJ */
128 acl2->a_entries[2] = acl->a_entries[1]; /* ACL_MASK */
129 acl2->a_entries[2].e_tag = ACL_MASK;
130 acl2->a_entries[3] = acl->a_entries[2]; /* ACL_OTHER */
131 nfsacl_desc.acl = acl2;
133 err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc);
135 err = 8 + nfsacl_desc.desc.elem_size *
136 nfsacl_desc.desc.array_len;
139 EXPORT_SYMBOL_GPL(nfsacl_encode);
142 * nfs_stream_encode_acl - Encode an NFSv3 ACL
144 * @xdr: an xdr_stream positioned to receive an encoded ACL
145 * @inode: inode of file whose ACL this is
146 * @acl: posix_acl to encode
147 * @encode_entries: whether to encode ACEs as well
148 * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
151 * %false: The ACL could not be encoded
152 * %true: @xdr is advanced to the next available position
154 bool nfs_stream_encode_acl(struct xdr_stream *xdr, struct inode *inode,
155 struct posix_acl *acl, int encode_entries,
158 const size_t elem_size = XDR_UNIT * 3;
159 u32 entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
160 struct nfsacl_encode_desc nfsacl_desc = {
162 .elem_size = elem_size,
163 .array_len = encode_entries ? entries : 0,
164 .xcode = xdr_nfsace_encode,
167 .typeflag = typeflag,
171 struct nfsacl_simple_acl aclbuf;
175 if (entries > NFS_ACL_MAX_ENTRIES)
177 if (xdr_stream_encode_u32(xdr, entries) < 0)
180 if (encode_entries && acl && acl->a_count == 3) {
181 struct posix_acl *acl2 =
182 container_of(&aclbuf.acl, struct posix_acl, hdr);
184 /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
185 * invoked in contexts where a memory allocation failure is
186 * fatal. Fortunately this fake ACL is small enough to
187 * construct on the stack. */
188 posix_acl_init(acl2, 4);
190 /* Insert entries in canonical order: other orders seem
191 to confuse Solaris VxFS. */
192 acl2->a_entries[0] = acl->a_entries[0]; /* ACL_USER_OBJ */
193 acl2->a_entries[1] = acl->a_entries[1]; /* ACL_GROUP_OBJ */
194 acl2->a_entries[2] = acl->a_entries[1]; /* ACL_MASK */
195 acl2->a_entries[2].e_tag = ACL_MASK;
196 acl2->a_entries[3] = acl->a_entries[2]; /* ACL_OTHER */
197 nfsacl_desc.acl = acl2;
200 base = xdr_stream_pos(xdr);
201 if (!xdr_reserve_space(xdr, XDR_UNIT +
202 elem_size * nfsacl_desc.desc.array_len))
204 err = xdr_encode_array2(xdr->buf, base, &nfsacl_desc.desc);
210 EXPORT_SYMBOL_GPL(nfs_stream_encode_acl);
213 struct nfsacl_decode_desc {
214 struct xdr_array2_desc desc;
216 struct posix_acl *acl;
220 xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
222 struct nfsacl_decode_desc *nfsacl_desc =
223 (struct nfsacl_decode_desc *) desc;
225 struct posix_acl_entry *entry;
228 if (!nfsacl_desc->acl) {
229 if (desc->array_len > NFS_ACL_MAX_ENTRIES)
231 nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
232 if (!nfsacl_desc->acl)
234 nfsacl_desc->count = 0;
237 entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
238 entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
240 entry->e_perm = ntohl(*p++);
242 switch(entry->e_tag) {
244 entry->e_uid = make_kuid(&init_user_ns, id);
245 if (!uid_valid(entry->e_uid))
249 entry->e_gid = make_kgid(&init_user_ns, id);
250 if (!gid_valid(entry->e_gid))
256 if (entry->e_perm & ~S_IRWXO)
260 /* Solaris sometimes sets additional bits in the mask */
261 entry->e_perm &= S_IRWXO;
271 cmp_acl_entry(const void *x, const void *y)
273 const struct posix_acl_entry *a = x, *b = y;
275 if (a->e_tag != b->e_tag)
276 return a->e_tag - b->e_tag;
277 else if ((a->e_tag == ACL_USER) && uid_gt(a->e_uid, b->e_uid))
279 else if ((a->e_tag == ACL_USER) && uid_lt(a->e_uid, b->e_uid))
281 else if ((a->e_tag == ACL_GROUP) && gid_gt(a->e_gid, b->e_gid))
283 else if ((a->e_tag == ACL_GROUP) && gid_lt(a->e_gid, b->e_gid))
290 * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
293 posix_acl_from_nfsacl(struct posix_acl *acl)
295 struct posix_acl_entry *pa, *pe,
296 *group_obj = NULL, *mask = NULL;
301 sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
302 cmp_acl_entry, NULL);
304 /* Find the ACL_GROUP_OBJ and ACL_MASK entries. */
305 FOREACH_ACL_ENTRY(pa, acl, pe) {
319 if (acl->a_count == 4 && group_obj && mask &&
320 mask->e_perm == group_obj->e_perm) {
321 /* remove bogus ACL_MASK entry */
322 memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
323 sizeof(struct posix_acl_entry));
330 * nfsacl_decode - Decode an NFSv3 ACL
332 * @buf: xdr_buf containing XDR'd ACL data to decode
333 * @base: byte offset in xdr_buf where XDR'd ACL begins
334 * @aclcnt: count of ACEs in decoded posix_acl
335 * @pacl: buffer in which to place decoded posix_acl
337 * Returns the length of the decoded ACL in bytes, or a negative errno value.
339 int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
340 struct posix_acl **pacl)
342 struct nfsacl_decode_desc nfsacl_desc = {
345 .xcode = pacl ? xdr_nfsace_decode : NULL,
351 if (xdr_decode_word(buf, base, &entries) ||
352 entries > NFS_ACL_MAX_ENTRIES)
354 nfsacl_desc.desc.array_maxlen = entries;
355 err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
359 if (entries != nfsacl_desc.desc.array_len ||
360 posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
361 posix_acl_release(nfsacl_desc.acl);
364 *pacl = nfsacl_desc.acl;
368 return 8 + nfsacl_desc.desc.elem_size *
369 nfsacl_desc.desc.array_len;
371 EXPORT_SYMBOL_GPL(nfsacl_decode);
374 * nfs_stream_decode_acl - Decode an NFSv3 ACL
376 * @xdr: an xdr_stream positioned at an encoded ACL
377 * @aclcnt: OUT: count of ACEs in decoded posix_acl
378 * @pacl: OUT: a dynamically-allocated buffer containing the decoded posix_acl
381 * %false: The encoded ACL is not valid
382 * %true: @pacl contains a decoded ACL, and @xdr is advanced
384 * On a successful return, caller must release *pacl using posix_acl_release().
386 bool nfs_stream_decode_acl(struct xdr_stream *xdr, unsigned int *aclcnt,
387 struct posix_acl **pacl)
389 const size_t elem_size = XDR_UNIT * 3;
390 struct nfsacl_decode_desc nfsacl_desc = {
392 .elem_size = elem_size,
393 .xcode = pacl ? xdr_nfsace_decode : NULL,
399 if (xdr_stream_decode_u32(xdr, &entries) < 0)
401 if (entries > NFS_ACL_MAX_ENTRIES)
404 base = xdr_stream_pos(xdr);
405 if (!xdr_inline_decode(xdr, XDR_UNIT + elem_size * entries))
407 nfsacl_desc.desc.array_maxlen = entries;
408 if (xdr_decode_array2(xdr->buf, base, &nfsacl_desc.desc))
412 if (entries != nfsacl_desc.desc.array_len ||
413 posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
414 posix_acl_release(nfsacl_desc.acl);
417 *pacl = nfsacl_desc.acl;
423 EXPORT_SYMBOL_GPL(nfs_stream_decode_acl);