blob: 832673b14587da06496a34b35e8a4046439bc502 [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);
140void nfs4_acl_add_ace(struct nfs4_acl *, u32, u32, u32, int, uid_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142struct nfs4_acl *
143nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
144 unsigned int flags)
145{
146 struct nfs4_acl *acl;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800147 int size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800149 if (pacl) {
150 if (posix_acl_valid(pacl) < 0)
151 return ERR_PTR(-EINVAL);
152 size += 2*pacl->a_count;
153 }
154 if (dpacl) {
155 if (posix_acl_valid(dpacl) < 0)
156 return ERR_PTR(-EINVAL);
157 size += 2*dpacl->a_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800160 /* Allocate for worst case: one (deny, allow) pair each: */
161 acl = nfs4_acl_new(size);
162 if (acl == NULL)
163 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800165 if (pacl)
166 _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800168 if (dpacl)
169 _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return acl;
172}
173
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800174struct posix_acl_summary {
175 unsigned short owner;
176 unsigned short users;
177 unsigned short group;
178 unsigned short groups;
179 unsigned short other;
180 unsigned short mask;
181};
182
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800183static void
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800184summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800186 struct posix_acl_entry *pa, *pe;
187 pas->users = 0;
188 pas->groups = 0;
189 pas->mask = 07;
190
191 pe = acl->a_entries + acl->a_count;
192
193 FOREACH_ACL_ENTRY(pa, acl, pe) {
194 switch (pa->e_tag) {
195 case ACL_USER_OBJ:
196 pas->owner = pa->e_perm;
197 break;
198 case ACL_GROUP_OBJ:
199 pas->group = pa->e_perm;
200 break;
201 case ACL_USER:
202 pas->users |= pa->e_perm;
203 break;
204 case ACL_GROUP:
205 pas->groups |= pa->e_perm;
206 break;
207 case ACL_OTHER:
208 pas->other = pa->e_perm;
209 break;
210 case ACL_MASK:
211 pas->mask = pa->e_perm;
212 break;
213 }
214 }
215 /* We'll only care about effective permissions: */
216 pas->users &= pas->mask;
217 pas->group &= pas->mask;
218 pas->groups &= pas->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/* We assume the acl has been verified with posix_acl_valid. */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800222static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
224 unsigned int flags)
225{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800226 struct posix_acl_entry *pa, *group_owner_entry;
227 struct nfs4_ace *ace;
228 struct posix_acl_summary pas;
229 unsigned short deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
231 NFS4_INHERITANCE_FLAGS : 0);
232
233 BUG_ON(pacl->a_count < 3);
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800234 summarize_posix_acl(pacl, &pas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 pa = pacl->a_entries;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800237 ace = acl->aces + acl->naces;
238
239 /* We could deny everything not granted by the owner: */
240 deny = ~pas.owner;
241 /*
242 * but it is equivalent (and simpler) to deny only what is not
243 * granted by later entries:
244 */
245 deny &= pas.users | pas.group | pas.groups | pas.other;
246 if (deny) {
247 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
248 ace->flag = eflag;
249 ace->access_mask = deny_mask_from_posix(deny, flags);
250 ace->whotype = NFS4_ACL_WHO_OWNER;
251 ace++;
252 acl->naces++;
253 }
254
255 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
256 ace->flag = eflag;
257 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
258 ace->whotype = NFS4_ACL_WHO_OWNER;
259 ace++;
260 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 pa++;
262
263 while (pa->e_tag == ACL_USER) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800264 deny = ~(pa->e_perm & pas.mask);
265 deny &= pas.groups | pas.group | pas.other;
266 if (deny) {
267 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
268 ace->flag = eflag;
269 ace->access_mask = deny_mask_from_posix(deny, flags);
270 ace->whotype = NFS4_ACL_WHO_NAMED;
271 ace->who = pa->e_id;
272 ace++;
273 acl->naces++;
274 }
275 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
276 ace->flag = eflag;
277 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
278 flags);
279 ace->whotype = NFS4_ACL_WHO_NAMED;
280 ace->who = pa->e_id;
281 ace++;
282 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 pa++;
284 }
285
286 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
287 * since a user can be in more than one group. */
288
289 /* allow ACEs */
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 group_owner_entry = pa;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800292
293 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
294 ace->flag = eflag;
295 ace->access_mask = mask_from_posix(pas.group, flags);
296 ace->whotype = NFS4_ACL_WHO_GROUP;
297 ace++;
298 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 pa++;
300
301 while (pa->e_tag == ACL_GROUP) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800302 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
303 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
304 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
305 flags);
306 ace->whotype = NFS4_ACL_WHO_NAMED;
307 ace->who = pa->e_id;
308 ace++;
309 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 pa++;
311 }
312
313 /* deny ACEs */
314
315 pa = group_owner_entry;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800316
317 deny = ~pas.group & pas.other;
318 if (deny) {
319 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
320 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
321 ace->access_mask = deny_mask_from_posix(deny, flags);
322 ace->whotype = NFS4_ACL_WHO_GROUP;
323 ace++;
324 acl->naces++;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 pa++;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 while (pa->e_tag == ACL_GROUP) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800329 deny = ~(pa->e_perm & pas.mask);
330 deny &= pas.other;
331 if (deny) {
332 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
333 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
334 ace->access_mask = mask_from_posix(deny, flags);
335 ace->whotype = NFS4_ACL_WHO_NAMED;
336 ace->who = pa->e_id;
337 ace++;
338 acl->naces++;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 pa++;
341 }
342
343 if (pa->e_tag == ACL_MASK)
344 pa++;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800345 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
346 ace->flag = eflag;
347 ace->access_mask = mask_from_posix(pa->e_perm, flags);
348 ace->whotype = NFS4_ACL_WHO_EVERYONE;
349 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352static void
353sort_pacl_range(struct posix_acl *pacl, int start, int end) {
354 int sorted = 0, i;
355 struct posix_acl_entry tmp;
356
357 /* We just do a bubble sort; easy to do in place, and we're not
358 * expecting acl's to be long enough to justify anything more. */
359 while (!sorted) {
360 sorted = 1;
361 for (i = start; i < end; i++) {
362 if (pacl->a_entries[i].e_id
363 > pacl->a_entries[i+1].e_id) {
364 sorted = 0;
365 tmp = pacl->a_entries[i];
366 pacl->a_entries[i] = pacl->a_entries[i+1];
367 pacl->a_entries[i+1] = tmp;
368 }
369 }
370 }
371}
372
373static void
374sort_pacl(struct posix_acl *pacl)
375{
376 /* posix_acl_valid requires that users and groups be in order
377 * by uid/gid. */
378 int i, j;
379
380 if (pacl->a_count <= 4)
381 return; /* no users or groups */
382 i = 1;
383 while (pacl->a_entries[i].e_tag == ACL_USER)
384 i++;
385 sort_pacl_range(pacl, 1, i-1);
386
387 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
388 j = i++;
389 while (pacl->a_entries[j].e_tag == ACL_GROUP)
390 j++;
391 sort_pacl_range(pacl, i, j-1);
392 return;
393}
394
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700395/*
396 * While processing the NFSv4 ACE, this maintains bitmasks representing
397 * which permission bits have been allowed and which denied to a given
398 * entity: */
399struct posix_ace_state {
400 u32 allow;
401 u32 deny;
402};
403
404struct posix_user_ace_state {
405 uid_t uid;
406 struct posix_ace_state perms;
407};
408
409struct posix_ace_state_array {
410 int n;
411 struct posix_user_ace_state aces[];
412};
413
414/*
415 * While processing the NFSv4 ACE, this maintains the partial permissions
416 * calculated so far: */
417
418struct posix_acl_state {
J. Bruce Fields3160a712007-02-16 01:28:37 -0800419 int empty;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700420 struct posix_ace_state owner;
421 struct posix_ace_state group;
422 struct posix_ace_state other;
423 struct posix_ace_state everyone;
424 struct posix_ace_state mask; /* Deny unused in this case */
425 struct posix_ace_state_array *users;
426 struct posix_ace_state_array *groups;
427};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429static int
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700430init_state(struct posix_acl_state *state, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700432 int alloc;
433
434 memset(state, 0, sizeof(struct posix_acl_state));
J. Bruce Fields3160a712007-02-16 01:28:37 -0800435 state->empty = 1;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700436 /*
437 * In the worst case, each individual acl could be for a distinct
438 * named user or group, but we don't no which, so we allocate
439 * enough space for either:
440 */
441 alloc = sizeof(struct posix_ace_state_array)
442 + cnt*sizeof(struct posix_ace_state);
443 state->users = kzalloc(alloc, GFP_KERNEL);
444 if (!state->users)
445 return -ENOMEM;
446 state->groups = kzalloc(alloc, GFP_KERNEL);
447 if (!state->groups) {
448 kfree(state->users);
449 return -ENOMEM;
450 }
451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700454static void
455free_state(struct posix_acl_state *state) {
456 kfree(state->users);
457 kfree(state->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700460static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700462 state->mask.allow |= astate->allow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700465/*
466 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
467 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
468 * to traditional read/write/execute permissions.
469 *
470 * It's problematic to reject acls that use certain mode bits, because it
471 * places the burden on users to learn the rules about which bits one
472 * particular server sets, without giving the user a lot of help--we return an
473 * error that could mean any number of different things. To make matters
474 * worse, the problematic bits might be introduced by some application that's
475 * automatically mapping from some other acl model.
476 *
477 * So wherever possible we accept anything, possibly erring on the side of
478 * denying more permissions than necessary.
479 *
480 * However we do reject *explicit* DENY's of a few bits representing
481 * permissions we could never deny:
482 */
483
484static inline int check_deny(u32 mask, int isowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700486 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
487 return -EINVAL;
488 if (!isowner)
489 return 0;
490 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
491 return -EINVAL;
492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700495static struct posix_acl *
496posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700498 struct posix_acl_entry *pace;
499 struct posix_acl *pacl;
500 int nace;
501 int i, error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
J. Bruce Fields3160a712007-02-16 01:28:37 -0800503 /*
504 * ACLs with no ACEs are treated differently in the inheritable
505 * and effective cases: when there are no inheritable ACEs, we
506 * set a zero-length default posix acl:
507 */
508 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
509 pacl = posix_acl_alloc(0, GFP_KERNEL);
510 return pacl ? pacl : ERR_PTR(-ENOMEM);
511 }
512 /*
513 * When there are no effective ACEs, the following will end
514 * up setting a 3-element effective posix ACL with all
515 * permissions zero.
516 */
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700517 nace = 4 + state->users->n + state->groups->n;
518 pacl = posix_acl_alloc(nace, GFP_KERNEL);
519 if (!pacl)
520 return ERR_PTR(-ENOMEM);
521
522 pace = pacl->a_entries;
523 pace->e_tag = ACL_USER_OBJ;
524 error = check_deny(state->owner.deny, 1);
525 if (error)
526 goto out_err;
527 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
528 pace->e_id = ACL_UNDEFINED_ID;
529
530 for (i=0; i < state->users->n; i++) {
531 pace++;
532 pace->e_tag = ACL_USER;
533 error = check_deny(state->users->aces[i].perms.deny, 0);
534 if (error)
535 goto out_err;
536 low_mode_from_nfs4(state->users->aces[i].perms.allow,
537 &pace->e_perm, flags);
538 pace->e_id = state->users->aces[i].uid;
539 add_to_mask(state, &state->users->aces[i].perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700541
542 pace++;
543 pace->e_tag = ACL_GROUP_OBJ;
544 error = check_deny(state->group.deny, 0);
545 if (error)
546 goto out_err;
547 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
548 pace->e_id = ACL_UNDEFINED_ID;
549 add_to_mask(state, &state->group);
550
551 for (i=0; i < state->groups->n; i++) {
552 pace++;
553 pace->e_tag = ACL_GROUP;
554 error = check_deny(state->groups->aces[i].perms.deny, 0);
555 if (error)
556 goto out_err;
557 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
558 &pace->e_perm, flags);
559 pace->e_id = state->groups->aces[i].uid;
560 add_to_mask(state, &state->groups->aces[i].perms);
561 }
562
563 pace++;
564 pace->e_tag = ACL_MASK;
565 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
566 pace->e_id = ACL_UNDEFINED_ID;
567
568 pace++;
569 pace->e_tag = ACL_OTHER;
570 error = check_deny(state->other.deny, 0);
571 if (error)
572 goto out_err;
573 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
574 pace->e_id = ACL_UNDEFINED_ID;
575
576 return pacl;
577out_err:
578 posix_acl_release(pacl);
579 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700582static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700584 /* Allow all bits in the mask not already denied: */
585 astate->allow |= mask & ~astate->deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700588static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700590 /* Deny all bits in the mask not already allowed: */
591 astate->deny |= mask & ~astate->allow;
592}
593
594static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
595{
596 int i;
597
598 for (i = 0; i < a->n; i++)
599 if (a->aces[i].uid == uid)
600 return i;
601 /* Not found: */
602 a->n++;
603 a->aces[i].uid = uid;
604 a->aces[i].perms.allow = state->everyone.allow;
605 a->aces[i].perms.deny = state->everyone.deny;
606
607 return i;
608}
609
610static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
611{
612 int i;
613
614 for (i=0; i < a->n; i++)
615 deny_bits(&a->aces[i].perms, mask);
616}
617
618static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
619{
620 int i;
621
622 for (i=0; i < a->n; i++)
623 allow_bits(&a->aces[i].perms, mask);
624}
625
626static void process_one_v4_ace(struct posix_acl_state *state,
627 struct nfs4_ace *ace)
628{
629 u32 mask = ace->access_mask;
630 int i;
631
J. Bruce Fields3160a712007-02-16 01:28:37 -0800632 state->empty = 0;
633
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700634 switch (ace2type(ace)) {
635 case ACL_USER_OBJ:
636 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
637 allow_bits(&state->owner, mask);
638 } else {
639 deny_bits(&state->owner, mask);
640 }
641 break;
642 case ACL_USER:
643 i = find_uid(state, state->users, ace->who);
644 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
645 allow_bits(&state->users->aces[i].perms, mask);
646 } else {
647 deny_bits(&state->users->aces[i].perms, mask);
648 mask = state->users->aces[i].perms.deny;
649 deny_bits(&state->owner, mask);
650 }
651 break;
652 case ACL_GROUP_OBJ:
653 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
654 allow_bits(&state->group, mask);
655 } else {
656 deny_bits(&state->group, mask);
657 mask = state->group.deny;
658 deny_bits(&state->owner, mask);
659 deny_bits(&state->everyone, mask);
660 deny_bits_array(state->users, mask);
661 deny_bits_array(state->groups, mask);
662 }
663 break;
664 case ACL_GROUP:
665 i = find_uid(state, state->groups, ace->who);
666 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
667 allow_bits(&state->groups->aces[i].perms, mask);
668 } else {
669 deny_bits(&state->groups->aces[i].perms, mask);
670 mask = state->groups->aces[i].perms.deny;
671 deny_bits(&state->owner, mask);
672 deny_bits(&state->group, mask);
673 deny_bits(&state->everyone, mask);
674 deny_bits_array(state->users, mask);
675 deny_bits_array(state->groups, mask);
676 }
677 break;
678 case ACL_OTHER:
679 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
680 allow_bits(&state->owner, mask);
681 allow_bits(&state->group, mask);
682 allow_bits(&state->other, mask);
683 allow_bits(&state->everyone, mask);
684 allow_bits_array(state->users, mask);
685 allow_bits_array(state->groups, mask);
686 } else {
687 deny_bits(&state->owner, mask);
688 deny_bits(&state->group, mask);
689 deny_bits(&state->other, mask);
690 deny_bits(&state->everyone, mask);
691 deny_bits_array(state->users, mask);
692 deny_bits_array(state->groups, mask);
693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695}
696
J. Bruce Fields575a6292007-02-16 01:28:29 -0800697int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
698 struct posix_acl **dpacl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
J. Bruce Fields575a6292007-02-16 01:28:29 -0800700 struct posix_acl_state effective_acl_state, default_acl_state;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700701 struct nfs4_ace *ace;
702 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
J. Bruce Fields575a6292007-02-16 01:28:29 -0800704 ret = init_state(&effective_acl_state, acl->naces);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700705 if (ret)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800706 return ret;
707 ret = init_state(&default_acl_state, acl->naces);
708 if (ret)
709 goto out_estate;
710 ret = -EINVAL;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800711 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700712 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
713 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800714 goto out_dstate;
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700715 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800716 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800717 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
J. Bruce Fields575a6292007-02-16 01:28:29 -0800718 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 continue;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800720 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800721 if (!(flags & NFS4_ACL_DIR))
722 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800723 /*
724 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
725 * is set, we're effectively turning on the other. That's OK,
726 * according to rfc 3530.
727 */
J. Bruce Fields575a6292007-02-16 01:28:29 -0800728 process_one_v4_ace(&default_acl_state, ace);
729
730 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
731 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800733 *pacl = posix_state_to_acl(&effective_acl_state, flags);
734 if (IS_ERR(*pacl)) {
735 ret = PTR_ERR(*pacl);
736 goto out_dstate;
737 }
J. Bruce Fields3160a712007-02-16 01:28:37 -0800738 *dpacl = posix_state_to_acl(&default_acl_state,
739 flags | NFS4_ACL_TYPE_DEFAULT);
J. Bruce Fields575a6292007-02-16 01:28:29 -0800740 if (IS_ERR(*dpacl)) {
741 ret = PTR_ERR(*dpacl);
742 posix_acl_release(*pacl);
743 goto out_dstate;
744 }
745 sort_pacl(*pacl);
746 sort_pacl(*dpacl);
747 ret = 0;
748out_dstate:
749 free_state(&default_acl_state);
750out_estate:
751 free_state(&effective_acl_state);
752 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static short
756ace2type(struct nfs4_ace *ace)
757{
758 switch (ace->whotype) {
759 case NFS4_ACL_WHO_NAMED:
760 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
761 ACL_GROUP : ACL_USER);
762 case NFS4_ACL_WHO_OWNER:
763 return ACL_USER_OBJ;
764 case NFS4_ACL_WHO_GROUP:
765 return ACL_GROUP_OBJ;
766 case NFS4_ACL_WHO_EVERYONE:
767 return ACL_OTHER;
768 }
769 BUG();
770 return -1;
771}
772
773EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
774EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
775
776struct nfs4_acl *
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800777nfs4_acl_new(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct nfs4_acl *acl;
780
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800781 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
782 if (acl == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 acl->naces = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return acl;
786}
787
788void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask,
790 int whotype, uid_t who)
791{
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800792 struct nfs4_ace *ace = acl->aces + acl->naces;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 ace->type = type;
795 ace->flag = flag;
796 ace->access_mask = access_mask;
797 ace->whotype = whotype;
798 ace->who = who;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803static struct {
804 char *string;
805 int stringlen;
806 int type;
807} s2t_map[] = {
808 {
809 .string = "OWNER@",
810 .stringlen = sizeof("OWNER@") - 1,
811 .type = NFS4_ACL_WHO_OWNER,
812 },
813 {
814 .string = "GROUP@",
815 .stringlen = sizeof("GROUP@") - 1,
816 .type = NFS4_ACL_WHO_GROUP,
817 },
818 {
819 .string = "EVERYONE@",
820 .stringlen = sizeof("EVERYONE@") - 1,
821 .type = NFS4_ACL_WHO_EVERYONE,
822 },
823};
824
825int
826nfs4_acl_get_whotype(char *p, u32 len)
827{
828 int i;
829
Tobias Klausere8c96f82006-03-24 03:15:34 -0800830 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (s2t_map[i].stringlen == len &&
832 0 == memcmp(s2t_map[i].string, p, len))
833 return s2t_map[i].type;
834 }
835 return NFS4_ACL_WHO_NAMED;
836}
837
838int
839nfs4_acl_write_who(int who, char *p)
840{
841 int i;
842
Tobias Klausere8c96f82006-03-24 03:15:34 -0800843 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (s2t_map[i].type == who) {
845 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
846 return s2t_map[i].stringlen;
847 }
848 }
849 BUG();
850 return -1;
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853EXPORT_SYMBOL(nfs4_acl_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854EXPORT_SYMBOL(nfs4_acl_add_ace);
855EXPORT_SYMBOL(nfs4_acl_get_whotype);
856EXPORT_SYMBOL(nfs4_acl_write_who);