blob: 84c27d69d421e494c2c41359f06dd02eb2c32960 [file] [log] [blame]
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00001/*
2 * fs/nfs_common/nfsacl.c
3 *
4 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
5 */
6
7/*
8 * The Solaris nfsacl protocol represents some ACLs slightly differently
9 * than POSIX 1003.1e draft 17 does (and we do):
10 *
11 * - Minimal ACLs always have an ACL_MASK entry, so they have
12 * four instead of three entries.
13 * - The ACL_MASK entry in such minimal ACLs always has the same
14 * permissions as the ACL_GROUP_OBJ entry. (In extended ACLs
15 * the ACL_MASK and ACL_GROUP_OBJ entries may differ.)
16 * - The identifier fields of the ACL_USER_OBJ and ACL_GROUP_OBJ
17 * entries contain the identifiers of the owner and owning group.
18 * (In POSIX ACLs we always set them to ACL_UNDEFINED_ID).
19 * - ACL entries in the kernel are kept sorted in ascending order
20 * of (e_tag, e_id). Solaris ACLs are unsorted.
21 */
22
23#include <linux/module.h>
24#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000026#include <linux/sunrpc/xdr.h>
27#include <linux/nfsacl.h>
28#include <linux/nfs3.h>
29#include <linux/sort.h>
30
31MODULE_LICENSE("GPL");
32
Trond Myklebustd716f0b2008-12-23 15:21:32 -050033EXPORT_SYMBOL_GPL(nfsacl_encode);
34EXPORT_SYMBOL_GPL(nfsacl_decode);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000035
36struct nfsacl_encode_desc {
37 struct xdr_array2_desc desc;
38 unsigned int count;
39 struct posix_acl *acl;
40 int typeflag;
41 uid_t uid;
42 gid_t gid;
43};
44
Chuck Leverf61f6da2011-01-21 03:05:38 +000045struct nfsacl_simple_acl {
46 struct posix_acl acl;
47 struct posix_acl_entry ace[4];
48};
49
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000050static int
51xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem)
52{
53 struct nfsacl_encode_desc *nfsacl_desc =
54 (struct nfsacl_encode_desc *) desc;
Al Viro83bbe2e2006-10-19 23:28:53 -070055 __be32 *p = elem;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000056
Andreas Gruenbacher22c1ea42005-10-11 08:29:05 -070057 struct posix_acl_entry *entry =
58 &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000059
Andreas Gruenbacher22c1ea42005-10-11 08:29:05 -070060 *p++ = htonl(entry->e_tag | nfsacl_desc->typeflag);
61 switch(entry->e_tag) {
62 case ACL_USER_OBJ:
63 *p++ = htonl(nfsacl_desc->uid);
64 break;
65 case ACL_GROUP_OBJ:
66 *p++ = htonl(nfsacl_desc->gid);
67 break;
68 case ACL_USER:
69 case ACL_GROUP:
70 *p++ = htonl(entry->e_id);
71 break;
72 default: /* Solaris depends on that! */
73 *p++ = 0;
74 break;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000075 }
Andreas Gruenbacher22c1ea42005-10-11 08:29:05 -070076 *p++ = htonl(entry->e_perm & S_IRWXO);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000077 return 0;
78}
79
Chuck Lever731f3f42011-01-21 03:05:28 +000080/**
81 * nfsacl_encode - Encode an NFSv3 ACL
82 *
83 * @buf: destination xdr_buf to contain XDR encoded ACL
84 * @base: byte offset in xdr_buf where XDR'd ACL begins
85 * @inode: inode of file whose ACL this is
86 * @acl: posix_acl to encode
87 * @encode_entries: whether to encode ACEs as well
88 * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
89 *
90 * Returns size of encoded ACL in bytes or a negative errno value.
91 */
92int nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
93 struct posix_acl *acl, int encode_entries, int typeflag)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000094{
95 int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
96 struct nfsacl_encode_desc nfsacl_desc = {
97 .desc = {
98 .elem_size = 12,
99 .array_len = encode_entries ? entries : 0,
100 .xcode = xdr_nfsace_encode,
101 },
102 .acl = acl,
103 .typeflag = typeflag,
104 .uid = inode->i_uid,
105 .gid = inode->i_gid,
106 };
Chuck Leverf61f6da2011-01-21 03:05:38 +0000107 struct nfsacl_simple_acl aclbuf;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000108 int err;
109
110 if (entries > NFS_ACL_MAX_ENTRIES ||
111 xdr_encode_word(buf, base, entries))
112 return -EINVAL;
Andreas Gruenbacher22c1ea42005-10-11 08:29:05 -0700113 if (encode_entries && acl && acl->a_count == 3) {
Chuck Leverf61f6da2011-01-21 03:05:38 +0000114 struct posix_acl *acl2 = &aclbuf.acl;
115
116 /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
117 * invoked in contexts where a memory allocation failure is
118 * fatal. Fortunately this fake ACL is small enough to
119 * construct on the stack. */
120 memset(acl2, 0, sizeof(acl2));
121 posix_acl_init(acl2, 4);
122
Andreas Gruenbacher22c1ea42005-10-11 08:29:05 -0700123 /* 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;
131 }
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000132 err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc);
133 if (!err)
134 err = 8 + nfsacl_desc.desc.elem_size *
135 nfsacl_desc.desc.array_len;
136 return err;
137}
138
139struct nfsacl_decode_desc {
140 struct xdr_array2_desc desc;
141 unsigned int count;
142 struct posix_acl *acl;
143};
144
145static int
146xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
147{
148 struct nfsacl_decode_desc *nfsacl_desc =
149 (struct nfsacl_decode_desc *) desc;
Al Viro83bbe2e2006-10-19 23:28:53 -0700150 __be32 *p = elem;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000151 struct posix_acl_entry *entry;
152
153 if (!nfsacl_desc->acl) {
154 if (desc->array_len > NFS_ACL_MAX_ENTRIES)
155 return -EINVAL;
156 nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
157 if (!nfsacl_desc->acl)
158 return -ENOMEM;
159 nfsacl_desc->count = 0;
160 }
161
162 entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
163 entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
164 entry->e_id = ntohl(*p++);
165 entry->e_perm = ntohl(*p++);
166
167 switch(entry->e_tag) {
168 case ACL_USER_OBJ:
169 case ACL_USER:
170 case ACL_GROUP_OBJ:
171 case ACL_GROUP:
172 case ACL_OTHER:
173 if (entry->e_perm & ~S_IRWXO)
174 return -EINVAL;
175 break;
176 case ACL_MASK:
177 /* Solaris sometimes sets additonal bits in the mask */
178 entry->e_perm &= S_IRWXO;
179 break;
180 default:
181 return -EINVAL;
182 }
183
184 return 0;
185}
186
187static int
188cmp_acl_entry(const void *x, const void *y)
189{
190 const struct posix_acl_entry *a = x, *b = y;
191
192 if (a->e_tag != b->e_tag)
193 return a->e_tag - b->e_tag;
194 else if (a->e_id > b->e_id)
195 return 1;
196 else if (a->e_id < b->e_id)
197 return -1;
198 else
199 return 0;
200}
201
202/*
203 * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
204 */
205static int
206posix_acl_from_nfsacl(struct posix_acl *acl)
207{
208 struct posix_acl_entry *pa, *pe,
209 *group_obj = NULL, *mask = NULL;
210
211 if (!acl)
212 return 0;
213
214 sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
215 cmp_acl_entry, NULL);
216
217 /* Clear undefined identifier fields and find the ACL_GROUP_OBJ
218 and ACL_MASK entries. */
219 FOREACH_ACL_ENTRY(pa, acl, pe) {
220 switch(pa->e_tag) {
221 case ACL_USER_OBJ:
222 pa->e_id = ACL_UNDEFINED_ID;
223 break;
224 case ACL_GROUP_OBJ:
225 pa->e_id = ACL_UNDEFINED_ID;
226 group_obj = pa;
227 break;
228 case ACL_MASK:
229 mask = pa;
230 /* fall through */
231 case ACL_OTHER:
232 pa->e_id = ACL_UNDEFINED_ID;
233 break;
234 }
235 }
236 if (acl->a_count == 4 && group_obj && mask &&
237 mask->e_perm == group_obj->e_perm) {
238 /* remove bogus ACL_MASK entry */
239 memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
240 sizeof(struct posix_acl_entry));
241 acl->a_count = 3;
242 }
243 return 0;
244}
245
Chuck Lever731f3f42011-01-21 03:05:28 +0000246/**
247 * nfsacl_decode - Decode an NFSv3 ACL
248 *
249 * @buf: xdr_buf containing XDR'd ACL data to decode
250 * @base: byte offset in xdr_buf where XDR'd ACL begins
251 * @aclcnt: count of ACEs in decoded posix_acl
252 * @pacl: buffer in which to place decoded posix_acl
253 *
254 * Returns the length of the decoded ACL in bytes, or a negative errno value.
255 */
256int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
257 struct posix_acl **pacl)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000258{
259 struct nfsacl_decode_desc nfsacl_desc = {
260 .desc = {
261 .elem_size = 12,
262 .xcode = pacl ? xdr_nfsace_decode : NULL,
263 },
264 };
265 u32 entries;
266 int err;
267
268 if (xdr_decode_word(buf, base, &entries) ||
269 entries > NFS_ACL_MAX_ENTRIES)
270 return -EINVAL;
Trond Myklebust58fcb8d2005-08-10 18:15:12 -0400271 nfsacl_desc.desc.array_maxlen = entries;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000272 err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
273 if (err)
274 return err;
275 if (pacl) {
276 if (entries != nfsacl_desc.desc.array_len ||
277 posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
278 posix_acl_release(nfsacl_desc.acl);
279 return -EINVAL;
280 }
281 *pacl = nfsacl_desc.acl;
282 }
283 if (aclcnt)
284 *aclcnt = entries;
285 return 8 + nfsacl_desc.desc.elem_size *
286 nfsacl_desc.desc.array_len;
287}