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
{
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
= &aclbuf
.acl
;
117 /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
118 * invoked in contexts where a memory allocation failure is
119 * fatal. Fortunately this fake ACL is small enough to
120 * construct on the stack. */
121 posix_acl_init(acl2
, 4);
123 /* Insert entries in canonical order: other orders seem
124 to confuse Solaris VxFS. */
125 acl2
->a_entries
[0] = acl
->a_entries
[0]; /* ACL_USER_OBJ */
126 acl2
->a_entries
[1] = acl
->a_entries
[1]; /* ACL_GROUP_OBJ */
127 acl2
->a_entries
[2] = acl
->a_entries
[1]; /* ACL_MASK */
128 acl2
->a_entries
[2].e_tag
= ACL_MASK
;
129 acl2
->a_entries
[3] = acl
->a_entries
[2]; /* ACL_OTHER */
130 nfsacl_desc
.acl
= acl2
;
132 err
= xdr_encode_array2(buf
, base
+ 4, &nfsacl_desc
.desc
);
134 err
= 8 + nfsacl_desc
.desc
.elem_size
*
135 nfsacl_desc
.desc
.array_len
;
138 EXPORT_SYMBOL_GPL(nfsacl_encode
);
141 * nfs_stream_encode_acl - Encode an NFSv3 ACL
143 * @xdr: an xdr_stream positioned to receive an encoded ACL
144 * @inode: inode of file whose ACL this is
145 * @acl: posix_acl to encode
146 * @encode_entries: whether to encode ACEs as well
147 * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
150 * %false: The ACL could not be encoded
151 * %true: @xdr is advanced to the next available position
153 bool nfs_stream_encode_acl(struct xdr_stream
*xdr
, struct inode
*inode
,
154 struct posix_acl
*acl
, int encode_entries
,
157 const size_t elem_size
= XDR_UNIT
* 3;
158 u32 entries
= (acl
&& acl
->a_count
) ? max_t(int, acl
->a_count
, 4) : 0;
159 struct nfsacl_encode_desc nfsacl_desc
= {
161 .elem_size
= elem_size
,
162 .array_len
= encode_entries
? entries
: 0,
163 .xcode
= xdr_nfsace_encode
,
166 .typeflag
= typeflag
,
170 struct nfsacl_simple_acl aclbuf
;
174 if (entries
> NFS_ACL_MAX_ENTRIES
)
176 if (xdr_stream_encode_u32(xdr
, entries
) < 0)
179 if (encode_entries
&& acl
&& acl
->a_count
== 3) {
180 struct posix_acl
*acl2
= &aclbuf
.acl
;
182 /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
183 * invoked in contexts where a memory allocation failure is
184 * fatal. Fortunately this fake ACL is small enough to
185 * construct on the stack. */
186 posix_acl_init(acl2
, 4);
188 /* Insert entries in canonical order: other orders seem
189 to confuse Solaris VxFS. */
190 acl2
->a_entries
[0] = acl
->a_entries
[0]; /* ACL_USER_OBJ */
191 acl2
->a_entries
[1] = acl
->a_entries
[1]; /* ACL_GROUP_OBJ */
192 acl2
->a_entries
[2] = acl
->a_entries
[1]; /* ACL_MASK */
193 acl2
->a_entries
[2].e_tag
= ACL_MASK
;
194 acl2
->a_entries
[3] = acl
->a_entries
[2]; /* ACL_OTHER */
195 nfsacl_desc
.acl
= acl2
;
198 base
= xdr_stream_pos(xdr
);
199 if (!xdr_reserve_space(xdr
, XDR_UNIT
+
200 elem_size
* nfsacl_desc
.desc
.array_len
))
202 err
= xdr_encode_array2(xdr
->buf
, base
, &nfsacl_desc
.desc
);
208 EXPORT_SYMBOL_GPL(nfs_stream_encode_acl
);
211 struct nfsacl_decode_desc
{
212 struct xdr_array2_desc desc
;
214 struct posix_acl
*acl
;
218 xdr_nfsace_decode(struct xdr_array2_desc
*desc
, void *elem
)
220 struct nfsacl_decode_desc
*nfsacl_desc
=
221 (struct nfsacl_decode_desc
*) desc
;
223 struct posix_acl_entry
*entry
;
226 if (!nfsacl_desc
->acl
) {
227 if (desc
->array_len
> NFS_ACL_MAX_ENTRIES
)
229 nfsacl_desc
->acl
= posix_acl_alloc(desc
->array_len
, GFP_KERNEL
);
230 if (!nfsacl_desc
->acl
)
232 nfsacl_desc
->count
= 0;
235 entry
= &nfsacl_desc
->acl
->a_entries
[nfsacl_desc
->count
++];
236 entry
->e_tag
= ntohl(*p
++) & ~NFS_ACL_DEFAULT
;
238 entry
->e_perm
= ntohl(*p
++);
240 switch(entry
->e_tag
) {
242 entry
->e_uid
= make_kuid(&init_user_ns
, id
);
243 if (!uid_valid(entry
->e_uid
))
247 entry
->e_gid
= make_kgid(&init_user_ns
, id
);
248 if (!gid_valid(entry
->e_gid
))
254 if (entry
->e_perm
& ~S_IRWXO
)
258 /* Solaris sometimes sets additional bits in the mask */
259 entry
->e_perm
&= S_IRWXO
;
269 cmp_acl_entry(const void *x
, const void *y
)
271 const struct posix_acl_entry
*a
= x
, *b
= y
;
273 if (a
->e_tag
!= b
->e_tag
)
274 return a
->e_tag
- b
->e_tag
;
275 else if ((a
->e_tag
== ACL_USER
) && uid_gt(a
->e_uid
, b
->e_uid
))
277 else if ((a
->e_tag
== ACL_USER
) && uid_lt(a
->e_uid
, b
->e_uid
))
279 else if ((a
->e_tag
== ACL_GROUP
) && gid_gt(a
->e_gid
, b
->e_gid
))
281 else if ((a
->e_tag
== ACL_GROUP
) && gid_lt(a
->e_gid
, b
->e_gid
))
288 * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
291 posix_acl_from_nfsacl(struct posix_acl
*acl
)
293 struct posix_acl_entry
*pa
, *pe
,
294 *group_obj
= NULL
, *mask
= NULL
;
299 sort(acl
->a_entries
, acl
->a_count
, sizeof(struct posix_acl_entry
),
300 cmp_acl_entry
, NULL
);
302 /* Find the ACL_GROUP_OBJ and ACL_MASK entries. */
303 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
317 if (acl
->a_count
== 4 && group_obj
&& mask
&&
318 mask
->e_perm
== group_obj
->e_perm
) {
319 /* remove bogus ACL_MASK entry */
320 memmove(mask
, mask
+1, (3 - (mask
- acl
->a_entries
)) *
321 sizeof(struct posix_acl_entry
));
328 * nfsacl_decode - Decode an NFSv3 ACL
330 * @buf: xdr_buf containing XDR'd ACL data to decode
331 * @base: byte offset in xdr_buf where XDR'd ACL begins
332 * @aclcnt: count of ACEs in decoded posix_acl
333 * @pacl: buffer in which to place decoded posix_acl
335 * Returns the length of the decoded ACL in bytes, or a negative errno value.
337 int nfsacl_decode(struct xdr_buf
*buf
, unsigned int base
, unsigned int *aclcnt
,
338 struct posix_acl
**pacl
)
340 struct nfsacl_decode_desc nfsacl_desc
= {
343 .xcode
= pacl
? xdr_nfsace_decode
: NULL
,
349 if (xdr_decode_word(buf
, base
, &entries
) ||
350 entries
> NFS_ACL_MAX_ENTRIES
)
352 nfsacl_desc
.desc
.array_maxlen
= entries
;
353 err
= xdr_decode_array2(buf
, base
+ 4, &nfsacl_desc
.desc
);
357 if (entries
!= nfsacl_desc
.desc
.array_len
||
358 posix_acl_from_nfsacl(nfsacl_desc
.acl
) != 0) {
359 posix_acl_release(nfsacl_desc
.acl
);
362 *pacl
= nfsacl_desc
.acl
;
366 return 8 + nfsacl_desc
.desc
.elem_size
*
367 nfsacl_desc
.desc
.array_len
;
369 EXPORT_SYMBOL_GPL(nfsacl_decode
);
372 * nfs_stream_decode_acl - Decode an NFSv3 ACL
374 * @xdr: an xdr_stream positioned at an encoded ACL
375 * @aclcnt: OUT: count of ACEs in decoded posix_acl
376 * @pacl: OUT: a dynamically-allocated buffer containing the decoded posix_acl
379 * %false: The encoded ACL is not valid
380 * %true: @pacl contains a decoded ACL, and @xdr is advanced
382 * On a successful return, caller must release *pacl using posix_acl_release().
384 bool nfs_stream_decode_acl(struct xdr_stream
*xdr
, unsigned int *aclcnt
,
385 struct posix_acl
**pacl
)
387 const size_t elem_size
= XDR_UNIT
* 3;
388 struct nfsacl_decode_desc nfsacl_desc
= {
390 .elem_size
= elem_size
,
391 .xcode
= pacl
? xdr_nfsace_decode
: NULL
,
397 if (xdr_stream_decode_u32(xdr
, &entries
) < 0)
399 if (entries
> NFS_ACL_MAX_ENTRIES
)
402 base
= xdr_stream_pos(xdr
);
403 if (!xdr_inline_decode(xdr
, XDR_UNIT
+ elem_size
* entries
))
405 nfsacl_desc
.desc
.array_maxlen
= entries
;
406 if (xdr_decode_array2(xdr
->buf
, base
, &nfsacl_desc
.desc
))
410 if (entries
!= nfsacl_desc
.desc
.array_len
||
411 posix_acl_from_nfsacl(nfsacl_desc
.acl
) != 0) {
412 posix_acl_release(nfsacl_desc
.acl
);
415 *pacl
= nfsacl_desc
.acl
;
421 EXPORT_SYMBOL_GPL(nfs_stream_decode_acl
);