blob: b6ed38380ab805179f8354ab9454fc533e50da53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs4acl/acl.c
3 *
4 * Common NFSv4 ACL handling code.
5 *
6 * Copyright (c) 2002, 2003 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Marius Aamodt Eriksen <marius@umich.edu>
10 * Jeff Sedlak <jsedlak@umich.edu>
11 * J. Bruce Fields <bfields@umich.edu>
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/string.h>
40#include <linux/slab.h>
41#include <linux/list.h>
42#include <linux/types.h>
43#include <linux/fs.h>
44#include <linux/module.h>
45#include <linux/nfs_fs.h>
46#include <linux/posix_acl.h>
47#include <linux/nfs4.h>
48#include <linux/nfs4_acl.h>
49
50
51/* mode bit translations: */
52#define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
53#define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
54#define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
55#define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
56#define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
57
58/* We don't support these bits; insist they be neither allowed nor denied */
59#define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
60 | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
61
62/* flags used to simulate posix default ACLs */
63#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
J. Bruce Fields7bdfa682007-02-16 01:28:28 -080064 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
J. Bruce Fields7bdfa682007-02-16 01:28:28 -080066#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
67 | NFS4_ACE_INHERIT_ONLY_ACE \
68 | NFS4_ACE_IDENTIFIER_GROUP)
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define MASK_EQUAL(mask1, mask2) \
71 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
72
73static u32
74mask_from_posix(unsigned short perm, unsigned int flags)
75{
76 int mask = NFS4_ANYONE_MODE;
77
78 if (flags & NFS4_ACL_OWNER)
79 mask |= NFS4_OWNER_MODE;
80 if (perm & ACL_READ)
81 mask |= NFS4_READ_MODE;
82 if (perm & ACL_WRITE)
83 mask |= NFS4_WRITE_MODE;
84 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
85 mask |= NFS4_ACE_DELETE_CHILD;
86 if (perm & ACL_EXECUTE)
87 mask |= NFS4_EXECUTE_MODE;
88 return mask;
89}
90
91static u32
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -080092deny_mask_from_posix(unsigned short perm, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -080094 u32 mask = 0;
95
96 if (perm & ACL_READ)
97 mask |= NFS4_READ_MODE;
98 if (perm & ACL_WRITE)
99 mask |= NFS4_WRITE_MODE;
100 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
101 mask |= NFS4_ACE_DELETE_CHILD;
102 if (perm & ACL_EXECUTE)
103 mask |= NFS4_EXECUTE_MODE;
104 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/* XXX: modify functions to return NFS errors; they're only ever
108 * used by nfs code, after all.... */
109
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700110/* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
111 * side of being more restrictive, so the mode bit mapping below is
112 * pessimistic. An optimistic version would be needed to handle DENY's,
113 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
114 * bits. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700116static void
117low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
118{
119 u32 write_mode = NFS4_WRITE_MODE;
120
121 if (flags & NFS4_ACL_DIR)
122 write_mode |= NFS4_ACE_DELETE_CHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 *mode = 0;
124 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
125 *mode |= ACL_READ;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700126 if ((perm & write_mode) == write_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *mode |= ACL_WRITE;
128 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
129 *mode |= ACL_EXECUTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132struct ace_container {
133 struct nfs4_ace *ace;
134 struct list_head ace_l;
135};
136
137static short ace2type(struct nfs4_ace *);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800138static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
139 unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141struct nfs4_acl *
142nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
143 unsigned int flags)
144{
145 struct nfs4_acl *acl;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800146 int size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800148 if (pacl) {
149 if (posix_acl_valid(pacl) < 0)
150 return ERR_PTR(-EINVAL);
151 size += 2*pacl->a_count;
152 }
153 if (dpacl) {
154 if (posix_acl_valid(dpacl) < 0)
155 return ERR_PTR(-EINVAL);
156 size += 2*dpacl->a_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800159 /* Allocate for worst case: one (deny, allow) pair each: */
160 acl = nfs4_acl_new(size);
161 if (acl == NULL)
162 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800164 if (pacl)
165 _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800167 if (dpacl)
168 _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 return acl;
171}
172
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800173struct posix_acl_summary {
174 unsigned short owner;
175 unsigned short users;
176 unsigned short group;
177 unsigned short groups;
178 unsigned short other;
179 unsigned short mask;
180};
181
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800182static void
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800183summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800185 struct posix_acl_entry *pa, *pe;
J. Bruce Fieldsf7fede42007-07-17 04:04:36 -0700186
187 /*
188 * Only pas.users and pas.groups need initialization; previous
189 * posix_acl_valid() calls ensure that the other fields will be
190 * initialized in the following loop. But, just to placate gcc:
191 */
192 memset(pas, 0, sizeof(*pas));
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800193 pas->mask = 07;
194
195 pe = acl->a_entries + acl->a_count;
196
197 FOREACH_ACL_ENTRY(pa, acl, pe) {
198 switch (pa->e_tag) {
199 case ACL_USER_OBJ:
200 pas->owner = pa->e_perm;
201 break;
202 case ACL_GROUP_OBJ:
203 pas->group = pa->e_perm;
204 break;
205 case ACL_USER:
206 pas->users |= pa->e_perm;
207 break;
208 case ACL_GROUP:
209 pas->groups |= pa->e_perm;
210 break;
211 case ACL_OTHER:
212 pas->other = pa->e_perm;
213 break;
214 case ACL_MASK:
215 pas->mask = pa->e_perm;
216 break;
217 }
218 }
219 /* We'll only care about effective permissions: */
220 pas->users &= pas->mask;
221 pas->group &= pas->mask;
222 pas->groups &= pas->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/* We assume the acl has been verified with posix_acl_valid. */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800226static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
228 unsigned int flags)
229{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800230 struct posix_acl_entry *pa, *group_owner_entry;
231 struct nfs4_ace *ace;
232 struct posix_acl_summary pas;
233 unsigned short deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
Bruce Fields54c04402007-03-26 21:32:09 -0800235 NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 BUG_ON(pacl->a_count < 3);
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800238 summarize_posix_acl(pacl, &pas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 pa = pacl->a_entries;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800241 ace = acl->aces + acl->naces;
242
243 /* We could deny everything not granted by the owner: */
244 deny = ~pas.owner;
245 /*
246 * but it is equivalent (and simpler) to deny only what is not
247 * granted by later entries:
248 */
249 deny &= pas.users | pas.group | pas.groups | pas.other;
250 if (deny) {
251 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
252 ace->flag = eflag;
253 ace->access_mask = deny_mask_from_posix(deny, flags);
254 ace->whotype = NFS4_ACL_WHO_OWNER;
255 ace++;
256 acl->naces++;
257 }
258
259 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
260 ace->flag = eflag;
261 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
262 ace->whotype = NFS4_ACL_WHO_OWNER;
263 ace++;
264 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 pa++;
266
267 while (pa->e_tag == ACL_USER) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800268 deny = ~(pa->e_perm & pas.mask);
269 deny &= pas.groups | pas.group | pas.other;
270 if (deny) {
271 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
272 ace->flag = eflag;
273 ace->access_mask = deny_mask_from_posix(deny, flags);
274 ace->whotype = NFS4_ACL_WHO_NAMED;
275 ace->who = pa->e_id;
276 ace++;
277 acl->naces++;
278 }
279 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
280 ace->flag = eflag;
281 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
282 flags);
283 ace->whotype = NFS4_ACL_WHO_NAMED;
284 ace->who = pa->e_id;
285 ace++;
286 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 pa++;
288 }
289
290 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
291 * since a user can be in more than one group. */
292
293 /* allow ACEs */
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 group_owner_entry = pa;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800296
297 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
298 ace->flag = eflag;
299 ace->access_mask = mask_from_posix(pas.group, flags);
300 ace->whotype = NFS4_ACL_WHO_GROUP;
301 ace++;
302 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 pa++;
304
305 while (pa->e_tag == ACL_GROUP) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800306 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
307 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
308 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
309 flags);
310 ace->whotype = NFS4_ACL_WHO_NAMED;
311 ace->who = pa->e_id;
312 ace++;
313 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 pa++;
315 }
316
317 /* deny ACEs */
318
319 pa = group_owner_entry;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800320
321 deny = ~pas.group & pas.other;
322 if (deny) {
323 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
324 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
325 ace->access_mask = deny_mask_from_posix(deny, flags);
326 ace->whotype = NFS4_ACL_WHO_GROUP;
327 ace++;
328 acl->naces++;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 pa++;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 while (pa->e_tag == ACL_GROUP) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800333 deny = ~(pa->e_perm & pas.mask);
334 deny &= pas.other;
335 if (deny) {
336 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
337 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
338 ace->access_mask = mask_from_posix(deny, flags);
339 ace->whotype = NFS4_ACL_WHO_NAMED;
340 ace->who = pa->e_id;
341 ace++;
342 acl->naces++;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 pa++;
345 }
346
347 if (pa->e_tag == ACL_MASK)
348 pa++;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800349 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
350 ace->flag = eflag;
351 ace->access_mask = mask_from_posix(pa->e_perm, flags);
352 ace->whotype = NFS4_ACL_WHO_EVERYONE;
353 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356static void
357sort_pacl_range(struct posix_acl *pacl, int start, int end) {
358 int sorted = 0, i;
359 struct posix_acl_entry tmp;
360
361 /* We just do a bubble sort; easy to do in place, and we're not
362 * expecting acl's to be long enough to justify anything more. */
363 while (!sorted) {
364 sorted = 1;
365 for (i = start; i < end; i++) {
366 if (pacl->a_entries[i].e_id
367 > pacl->a_entries[i+1].e_id) {
368 sorted = 0;
369 tmp = pacl->a_entries[i];
370 pacl->a_entries[i] = pacl->a_entries[i+1];
371 pacl->a_entries[i+1] = tmp;
372 }
373 }
374 }
375}
376
377static void
378sort_pacl(struct posix_acl *pacl)
379{
380 /* posix_acl_valid requires that users and groups be in order
381 * by uid/gid. */
382 int i, j;
383
384 if (pacl->a_count <= 4)
385 return; /* no users or groups */
386 i = 1;
387 while (pacl->a_entries[i].e_tag == ACL_USER)
388 i++;
389 sort_pacl_range(pacl, 1, i-1);
390
391 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
392 j = i++;
393 while (pacl->a_entries[j].e_tag == ACL_GROUP)
394 j++;
395 sort_pacl_range(pacl, i, j-1);
396 return;
397}
398
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700399/*
400 * While processing the NFSv4 ACE, this maintains bitmasks representing
401 * which permission bits have been allowed and which denied to a given
402 * entity: */
403struct posix_ace_state {
404 u32 allow;
405 u32 deny;
406};
407
408struct posix_user_ace_state {
409 uid_t uid;
410 struct posix_ace_state perms;
411};
412
413struct posix_ace_state_array {
414 int n;
415 struct posix_user_ace_state aces[];
416};
417
418/*
419 * While processing the NFSv4 ACE, this maintains the partial permissions
420 * calculated so far: */
421
422struct posix_acl_state {
J. Bruce Fields3160a712007-02-16 01:28:37 -0800423 int empty;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700424 struct posix_ace_state owner;
425 struct posix_ace_state group;
426 struct posix_ace_state other;
427 struct posix_ace_state everyone;
428 struct posix_ace_state mask; /* Deny unused in this case */
429 struct posix_ace_state_array *users;
430 struct posix_ace_state_array *groups;
431};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433static int
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700434init_state(struct posix_acl_state *state, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700436 int alloc;
437
438 memset(state, 0, sizeof(struct posix_acl_state));
J. Bruce Fields3160a712007-02-16 01:28:37 -0800439 state->empty = 1;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700440 /*
441 * In the worst case, each individual acl could be for a distinct
442 * named user or group, but we don't no which, so we allocate
443 * enough space for either:
444 */
445 alloc = sizeof(struct posix_ace_state_array)
446 + cnt*sizeof(struct posix_ace_state);
447 state->users = kzalloc(alloc, GFP_KERNEL);
448 if (!state->users)
449 return -ENOMEM;
450 state->groups = kzalloc(alloc, GFP_KERNEL);
451 if (!state->groups) {
452 kfree(state->users);
453 return -ENOMEM;
454 }
455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700458static void
459free_state(struct posix_acl_state *state) {
460 kfree(state->users);
461 kfree(state->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700464static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700466 state->mask.allow |= astate->allow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700469/*
470 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
471 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
472 * to traditional read/write/execute permissions.
473 *
474 * It's problematic to reject acls that use certain mode bits, because it
475 * places the burden on users to learn the rules about which bits one
476 * particular server sets, without giving the user a lot of help--we return an
477 * error that could mean any number of different things. To make matters
478 * worse, the problematic bits might be introduced by some application that's
479 * automatically mapping from some other acl model.
480 *
481 * So wherever possible we accept anything, possibly erring on the side of
482 * denying more permissions than necessary.
483 *
484 * However we do reject *explicit* DENY's of a few bits representing
485 * permissions we could never deny:
486 */
487
488static inline int check_deny(u32 mask, int isowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700490 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
491 return -EINVAL;
492 if (!isowner)
493 return 0;
494 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
495 return -EINVAL;
496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700499static struct posix_acl *
500posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700502 struct posix_acl_entry *pace;
503 struct posix_acl *pacl;
504 int nace;
505 int i, error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
J. Bruce Fields3160a712007-02-16 01:28:37 -0800507 /*
508 * ACLs with no ACEs are treated differently in the inheritable
509 * and effective cases: when there are no inheritable ACEs, we
510 * set a zero-length default posix acl:
511 */
512 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
513 pacl = posix_acl_alloc(0, GFP_KERNEL);
514 return pacl ? pacl : ERR_PTR(-ENOMEM);
515 }
516 /*
517 * When there are no effective ACEs, the following will end
518 * up setting a 3-element effective posix ACL with all
519 * permissions zero.
520 */
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700521 nace = 4 + state->users->n + state->groups->n;
522 pacl = posix_acl_alloc(nace, GFP_KERNEL);
523 if (!pacl)
524 return ERR_PTR(-ENOMEM);
525
526 pace = pacl->a_entries;
527 pace->e_tag = ACL_USER_OBJ;
528 error = check_deny(state->owner.deny, 1);
529 if (error)
530 goto out_err;
531 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
532 pace->e_id = ACL_UNDEFINED_ID;
533
534 for (i=0; i < state->users->n; i++) {
535 pace++;
536 pace->e_tag = ACL_USER;
537 error = check_deny(state->users->aces[i].perms.deny, 0);
538 if (error)
539 goto out_err;
540 low_mode_from_nfs4(state->users->aces[i].perms.allow,
541 &pace->e_perm, flags);
542 pace->e_id = state->users->aces[i].uid;
543 add_to_mask(state, &state->users->aces[i].perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700545
546 pace++;
547 pace->e_tag = ACL_GROUP_OBJ;
548 error = check_deny(state->group.deny, 0);
549 if (error)
550 goto out_err;
551 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
552 pace->e_id = ACL_UNDEFINED_ID;
553 add_to_mask(state, &state->group);
554
555 for (i=0; i < state->groups->n; i++) {
556 pace++;
557 pace->e_tag = ACL_GROUP;
558 error = check_deny(state->groups->aces[i].perms.deny, 0);
559 if (error)
560 goto out_err;
561 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
562 &pace->e_perm, flags);
563 pace->e_id = state->groups->aces[i].uid;
564 add_to_mask(state, &state->groups->aces[i].perms);
565 }
566
567 pace++;
568 pace->e_tag = ACL_MASK;
569 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
570 pace->e_id = ACL_UNDEFINED_ID;
571
572 pace++;
573 pace->e_tag = ACL_OTHER;
574 error = check_deny(state->other.deny, 0);
575 if (error)
576 goto out_err;
577 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
578 pace->e_id = ACL_UNDEFINED_ID;
579
580 return pacl;
581out_err:
582 posix_acl_release(pacl);
583 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700586static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700588 /* Allow all bits in the mask not already denied: */
589 astate->allow |= mask & ~astate->deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700592static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700594 /* Deny all bits in the mask not already allowed: */
595 astate->deny |= mask & ~astate->allow;
596}
597
598static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
599{
600 int i;
601
602 for (i = 0; i < a->n; i++)
603 if (a->aces[i].uid == uid)
604 return i;
605 /* Not found: */
606 a->n++;
607 a->aces[i].uid = uid;
608 a->aces[i].perms.allow = state->everyone.allow;
609 a->aces[i].perms.deny = state->everyone.deny;
610
611 return i;
612}
613
614static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
615{
616 int i;
617
618 for (i=0; i < a->n; i++)
619 deny_bits(&a->aces[i].perms, mask);
620}
621
622static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
623{
624 int i;
625
626 for (i=0; i < a->n; i++)
627 allow_bits(&a->aces[i].perms, mask);
628}
629
630static void process_one_v4_ace(struct posix_acl_state *state,
631 struct nfs4_ace *ace)
632{
633 u32 mask = ace->access_mask;
634 int i;
635
J. Bruce Fields3160a712007-02-16 01:28:37 -0800636 state->empty = 0;
637
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700638 switch (ace2type(ace)) {
639 case ACL_USER_OBJ:
640 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
641 allow_bits(&state->owner, mask);
642 } else {
643 deny_bits(&state->owner, mask);
644 }
645 break;
646 case ACL_USER:
647 i = find_uid(state, state->users, ace->who);
648 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
649 allow_bits(&state->users->aces[i].perms, mask);
650 } else {
651 deny_bits(&state->users->aces[i].perms, mask);
652 mask = state->users->aces[i].perms.deny;
653 deny_bits(&state->owner, mask);
654 }
655 break;
656 case ACL_GROUP_OBJ:
657 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
658 allow_bits(&state->group, mask);
659 } else {
660 deny_bits(&state->group, mask);
661 mask = state->group.deny;
662 deny_bits(&state->owner, mask);
663 deny_bits(&state->everyone, mask);
664 deny_bits_array(state->users, mask);
665 deny_bits_array(state->groups, mask);
666 }
667 break;
668 case ACL_GROUP:
669 i = find_uid(state, state->groups, ace->who);
670 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
671 allow_bits(&state->groups->aces[i].perms, mask);
672 } else {
673 deny_bits(&state->groups->aces[i].perms, mask);
674 mask = state->groups->aces[i].perms.deny;
675 deny_bits(&state->owner, mask);
676 deny_bits(&state->group, mask);
677 deny_bits(&state->everyone, mask);
678 deny_bits_array(state->users, mask);
679 deny_bits_array(state->groups, mask);
680 }
681 break;
682 case ACL_OTHER:
683 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
684 allow_bits(&state->owner, mask);
685 allow_bits(&state->group, mask);
686 allow_bits(&state->other, mask);
687 allow_bits(&state->everyone, mask);
688 allow_bits_array(state->users, mask);
689 allow_bits_array(state->groups, mask);
690 } else {
691 deny_bits(&state->owner, mask);
692 deny_bits(&state->group, mask);
693 deny_bits(&state->other, mask);
694 deny_bits(&state->everyone, mask);
695 deny_bits_array(state->users, mask);
696 deny_bits_array(state->groups, mask);
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699}
700
J. Bruce Fields575a6292007-02-16 01:28:29 -0800701int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
702 struct posix_acl **dpacl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
J. Bruce Fields575a6292007-02-16 01:28:29 -0800704 struct posix_acl_state effective_acl_state, default_acl_state;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700705 struct nfs4_ace *ace;
706 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
J. Bruce Fields575a6292007-02-16 01:28:29 -0800708 ret = init_state(&effective_acl_state, acl->naces);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700709 if (ret)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800710 return ret;
711 ret = init_state(&default_acl_state, acl->naces);
712 if (ret)
713 goto out_estate;
714 ret = -EINVAL;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800715 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700716 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
717 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800718 goto out_dstate;
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700719 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800720 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800721 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
J. Bruce Fields575a6292007-02-16 01:28:29 -0800722 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 continue;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800724 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800725 if (!(flags & NFS4_ACL_DIR))
726 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800727 /*
728 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
729 * is set, we're effectively turning on the other. That's OK,
730 * according to rfc 3530.
731 */
J. Bruce Fields575a6292007-02-16 01:28:29 -0800732 process_one_v4_ace(&default_acl_state, ace);
733
734 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
735 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800737 *pacl = posix_state_to_acl(&effective_acl_state, flags);
738 if (IS_ERR(*pacl)) {
739 ret = PTR_ERR(*pacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700740 *pacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800741 goto out_dstate;
742 }
J. Bruce Fields3160a712007-02-16 01:28:37 -0800743 *dpacl = posix_state_to_acl(&default_acl_state,
744 flags | NFS4_ACL_TYPE_DEFAULT);
J. Bruce Fields575a6292007-02-16 01:28:29 -0800745 if (IS_ERR(*dpacl)) {
746 ret = PTR_ERR(*dpacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700747 *dpacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800748 posix_acl_release(*pacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700749 *pacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800750 goto out_dstate;
751 }
752 sort_pacl(*pacl);
753 sort_pacl(*dpacl);
754 ret = 0;
755out_dstate:
756 free_state(&default_acl_state);
757out_estate:
758 free_state(&effective_acl_state);
759 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762static short
763ace2type(struct nfs4_ace *ace)
764{
765 switch (ace->whotype) {
766 case NFS4_ACL_WHO_NAMED:
767 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
768 ACL_GROUP : ACL_USER);
769 case NFS4_ACL_WHO_OWNER:
770 return ACL_USER_OBJ;
771 case NFS4_ACL_WHO_GROUP:
772 return ACL_GROUP_OBJ;
773 case NFS4_ACL_WHO_EVERYONE:
774 return ACL_OTHER;
775 }
776 BUG();
777 return -1;
778}
779
780EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
781EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
782
783struct nfs4_acl *
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800784nfs4_acl_new(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct nfs4_acl *acl;
787
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800788 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
789 if (acl == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 acl->naces = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return acl;
793}
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795static struct {
796 char *string;
797 int stringlen;
798 int type;
799} s2t_map[] = {
800 {
801 .string = "OWNER@",
802 .stringlen = sizeof("OWNER@") - 1,
803 .type = NFS4_ACL_WHO_OWNER,
804 },
805 {
806 .string = "GROUP@",
807 .stringlen = sizeof("GROUP@") - 1,
808 .type = NFS4_ACL_WHO_GROUP,
809 },
810 {
811 .string = "EVERYONE@",
812 .stringlen = sizeof("EVERYONE@") - 1,
813 .type = NFS4_ACL_WHO_EVERYONE,
814 },
815};
816
817int
818nfs4_acl_get_whotype(char *p, u32 len)
819{
820 int i;
821
Tobias Klausere8c96f82006-03-24 03:15:34 -0800822 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (s2t_map[i].stringlen == len &&
824 0 == memcmp(s2t_map[i].string, p, len))
825 return s2t_map[i].type;
826 }
827 return NFS4_ACL_WHO_NAMED;
828}
829
830int
831nfs4_acl_write_who(int who, char *p)
832{
833 int i;
834
Tobias Klausere8c96f82006-03-24 03:15:34 -0800835 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (s2t_map[i].type == who) {
837 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
838 return s2t_map[i].stringlen;
839 }
840 }
841 BUG();
842 return -1;
843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845EXPORT_SYMBOL(nfs4_acl_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846EXPORT_SYMBOL(nfs4_acl_get_whotype);
847EXPORT_SYMBOL(nfs4_acl_write_who);