blob: 0a69cce33efed3e86886df144dee28cc200f524a [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 {
419 struct posix_ace_state owner;
420 struct posix_ace_state group;
421 struct posix_ace_state other;
422 struct posix_ace_state everyone;
423 struct posix_ace_state mask; /* Deny unused in this case */
424 struct posix_ace_state_array *users;
425 struct posix_ace_state_array *groups;
426};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428static int
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700429init_state(struct posix_acl_state *state, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700431 int alloc;
432
433 memset(state, 0, sizeof(struct posix_acl_state));
434 /*
435 * In the worst case, each individual acl could be for a distinct
436 * named user or group, but we don't no which, so we allocate
437 * enough space for either:
438 */
439 alloc = sizeof(struct posix_ace_state_array)
440 + cnt*sizeof(struct posix_ace_state);
441 state->users = kzalloc(alloc, GFP_KERNEL);
442 if (!state->users)
443 return -ENOMEM;
444 state->groups = kzalloc(alloc, GFP_KERNEL);
445 if (!state->groups) {
446 kfree(state->users);
447 return -ENOMEM;
448 }
449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700452static void
453free_state(struct posix_acl_state *state) {
454 kfree(state->users);
455 kfree(state->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700458static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700460 state->mask.allow |= astate->allow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700463/*
464 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
465 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
466 * to traditional read/write/execute permissions.
467 *
468 * It's problematic to reject acls that use certain mode bits, because it
469 * places the burden on users to learn the rules about which bits one
470 * particular server sets, without giving the user a lot of help--we return an
471 * error that could mean any number of different things. To make matters
472 * worse, the problematic bits might be introduced by some application that's
473 * automatically mapping from some other acl model.
474 *
475 * So wherever possible we accept anything, possibly erring on the side of
476 * denying more permissions than necessary.
477 *
478 * However we do reject *explicit* DENY's of a few bits representing
479 * permissions we could never deny:
480 */
481
482static inline int check_deny(u32 mask, int isowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700484 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
485 return -EINVAL;
486 if (!isowner)
487 return 0;
488 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
489 return -EINVAL;
490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700493static struct posix_acl *
494posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700496 struct posix_acl_entry *pace;
497 struct posix_acl *pacl;
498 int nace;
499 int i, error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700501 nace = 4 + state->users->n + state->groups->n;
502 pacl = posix_acl_alloc(nace, GFP_KERNEL);
503 if (!pacl)
504 return ERR_PTR(-ENOMEM);
505
506 pace = pacl->a_entries;
507 pace->e_tag = ACL_USER_OBJ;
508 error = check_deny(state->owner.deny, 1);
509 if (error)
510 goto out_err;
511 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
512 pace->e_id = ACL_UNDEFINED_ID;
513
514 for (i=0; i < state->users->n; i++) {
515 pace++;
516 pace->e_tag = ACL_USER;
517 error = check_deny(state->users->aces[i].perms.deny, 0);
518 if (error)
519 goto out_err;
520 low_mode_from_nfs4(state->users->aces[i].perms.allow,
521 &pace->e_perm, flags);
522 pace->e_id = state->users->aces[i].uid;
523 add_to_mask(state, &state->users->aces[i].perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700525
526 pace++;
527 pace->e_tag = ACL_GROUP_OBJ;
528 error = check_deny(state->group.deny, 0);
529 if (error)
530 goto out_err;
531 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
532 pace->e_id = ACL_UNDEFINED_ID;
533 add_to_mask(state, &state->group);
534
535 for (i=0; i < state->groups->n; i++) {
536 pace++;
537 pace->e_tag = ACL_GROUP;
538 error = check_deny(state->groups->aces[i].perms.deny, 0);
539 if (error)
540 goto out_err;
541 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
542 &pace->e_perm, flags);
543 pace->e_id = state->groups->aces[i].uid;
544 add_to_mask(state, &state->groups->aces[i].perms);
545 }
546
547 pace++;
548 pace->e_tag = ACL_MASK;
549 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
550 pace->e_id = ACL_UNDEFINED_ID;
551
552 pace++;
553 pace->e_tag = ACL_OTHER;
554 error = check_deny(state->other.deny, 0);
555 if (error)
556 goto out_err;
557 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
558 pace->e_id = ACL_UNDEFINED_ID;
559
560 return pacl;
561out_err:
562 posix_acl_release(pacl);
563 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700566static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700568 /* Allow all bits in the mask not already denied: */
569 astate->allow |= mask & ~astate->deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700572static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700574 /* Deny all bits in the mask not already allowed: */
575 astate->deny |= mask & ~astate->allow;
576}
577
578static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
579{
580 int i;
581
582 for (i = 0; i < a->n; i++)
583 if (a->aces[i].uid == uid)
584 return i;
585 /* Not found: */
586 a->n++;
587 a->aces[i].uid = uid;
588 a->aces[i].perms.allow = state->everyone.allow;
589 a->aces[i].perms.deny = state->everyone.deny;
590
591 return i;
592}
593
594static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
595{
596 int i;
597
598 for (i=0; i < a->n; i++)
599 deny_bits(&a->aces[i].perms, mask);
600}
601
602static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
603{
604 int i;
605
606 for (i=0; i < a->n; i++)
607 allow_bits(&a->aces[i].perms, mask);
608}
609
610static void process_one_v4_ace(struct posix_acl_state *state,
611 struct nfs4_ace *ace)
612{
613 u32 mask = ace->access_mask;
614 int i;
615
616 switch (ace2type(ace)) {
617 case ACL_USER_OBJ:
618 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
619 allow_bits(&state->owner, mask);
620 } else {
621 deny_bits(&state->owner, mask);
622 }
623 break;
624 case ACL_USER:
625 i = find_uid(state, state->users, ace->who);
626 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
627 allow_bits(&state->users->aces[i].perms, mask);
628 } else {
629 deny_bits(&state->users->aces[i].perms, mask);
630 mask = state->users->aces[i].perms.deny;
631 deny_bits(&state->owner, mask);
632 }
633 break;
634 case ACL_GROUP_OBJ:
635 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
636 allow_bits(&state->group, mask);
637 } else {
638 deny_bits(&state->group, mask);
639 mask = state->group.deny;
640 deny_bits(&state->owner, mask);
641 deny_bits(&state->everyone, mask);
642 deny_bits_array(state->users, mask);
643 deny_bits_array(state->groups, mask);
644 }
645 break;
646 case ACL_GROUP:
647 i = find_uid(state, state->groups, ace->who);
648 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
649 allow_bits(&state->groups->aces[i].perms, mask);
650 } else {
651 deny_bits(&state->groups->aces[i].perms, mask);
652 mask = state->groups->aces[i].perms.deny;
653 deny_bits(&state->owner, mask);
654 deny_bits(&state->group, mask);
655 deny_bits(&state->everyone, mask);
656 deny_bits_array(state->users, mask);
657 deny_bits_array(state->groups, mask);
658 }
659 break;
660 case ACL_OTHER:
661 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
662 allow_bits(&state->owner, mask);
663 allow_bits(&state->group, mask);
664 allow_bits(&state->other, mask);
665 allow_bits(&state->everyone, mask);
666 allow_bits_array(state->users, mask);
667 allow_bits_array(state->groups, mask);
668 } else {
669 deny_bits(&state->owner, mask);
670 deny_bits(&state->group, mask);
671 deny_bits(&state->other, mask);
672 deny_bits(&state->everyone, mask);
673 deny_bits_array(state->users, mask);
674 deny_bits_array(state->groups, mask);
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677}
678
J. Bruce Fields575a6292007-02-16 01:28:29 -0800679int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
680 struct posix_acl **dpacl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
J. Bruce Fields575a6292007-02-16 01:28:29 -0800682 struct posix_acl_state effective_acl_state, default_acl_state;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700683 struct nfs4_ace *ace;
684 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
J. Bruce Fields575a6292007-02-16 01:28:29 -0800686 ret = init_state(&effective_acl_state, acl->naces);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700687 if (ret)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800688 return ret;
689 ret = init_state(&default_acl_state, acl->naces);
690 if (ret)
691 goto out_estate;
692 ret = -EINVAL;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800693 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700694 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
695 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800696 goto out_dstate;
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700697 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800698 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800699 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
J. Bruce Fields575a6292007-02-16 01:28:29 -0800700 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 continue;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800702 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800703 if (!(flags & NFS4_ACL_DIR))
704 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800705 /*
706 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
707 * is set, we're effectively turning on the other. That's OK,
708 * according to rfc 3530.
709 */
J. Bruce Fields575a6292007-02-16 01:28:29 -0800710 process_one_v4_ace(&default_acl_state, ace);
711
712 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
713 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800715 *pacl = posix_state_to_acl(&effective_acl_state, flags);
716 if (IS_ERR(*pacl)) {
717 ret = PTR_ERR(*pacl);
718 goto out_dstate;
719 }
720 *dpacl = posix_state_to_acl(&default_acl_state, flags);
721 if (IS_ERR(*dpacl)) {
722 ret = PTR_ERR(*dpacl);
723 posix_acl_release(*pacl);
724 goto out_dstate;
725 }
726 sort_pacl(*pacl);
727 sort_pacl(*dpacl);
728 ret = 0;
729out_dstate:
730 free_state(&default_acl_state);
731out_estate:
732 free_state(&effective_acl_state);
733 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736static short
737ace2type(struct nfs4_ace *ace)
738{
739 switch (ace->whotype) {
740 case NFS4_ACL_WHO_NAMED:
741 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
742 ACL_GROUP : ACL_USER);
743 case NFS4_ACL_WHO_OWNER:
744 return ACL_USER_OBJ;
745 case NFS4_ACL_WHO_GROUP:
746 return ACL_GROUP_OBJ;
747 case NFS4_ACL_WHO_EVERYONE:
748 return ACL_OTHER;
749 }
750 BUG();
751 return -1;
752}
753
754EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
755EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
756
757struct nfs4_acl *
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800758nfs4_acl_new(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct nfs4_acl *acl;
761
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800762 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
763 if (acl == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 acl->naces = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return acl;
767}
768
769void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask,
771 int whotype, uid_t who)
772{
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800773 struct nfs4_ace *ace = acl->aces + acl->naces;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 ace->type = type;
776 ace->flag = flag;
777 ace->access_mask = access_mask;
778 ace->whotype = whotype;
779 ace->who = who;
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784static struct {
785 char *string;
786 int stringlen;
787 int type;
788} s2t_map[] = {
789 {
790 .string = "OWNER@",
791 .stringlen = sizeof("OWNER@") - 1,
792 .type = NFS4_ACL_WHO_OWNER,
793 },
794 {
795 .string = "GROUP@",
796 .stringlen = sizeof("GROUP@") - 1,
797 .type = NFS4_ACL_WHO_GROUP,
798 },
799 {
800 .string = "EVERYONE@",
801 .stringlen = sizeof("EVERYONE@") - 1,
802 .type = NFS4_ACL_WHO_EVERYONE,
803 },
804};
805
806int
807nfs4_acl_get_whotype(char *p, u32 len)
808{
809 int i;
810
Tobias Klausere8c96f82006-03-24 03:15:34 -0800811 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (s2t_map[i].stringlen == len &&
813 0 == memcmp(s2t_map[i].string, p, len))
814 return s2t_map[i].type;
815 }
816 return NFS4_ACL_WHO_NAMED;
817}
818
819int
820nfs4_acl_write_who(int who, char *p)
821{
822 int i;
823
Tobias Klausere8c96f82006-03-24 03:15:34 -0800824 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (s2t_map[i].type == who) {
826 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
827 return s2t_map[i].stringlen;
828 }
829 }
830 BUG();
831 return -1;
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834EXPORT_SYMBOL(nfs4_acl_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835EXPORT_SYMBOL(nfs4_acl_add_ace);
836EXPORT_SYMBOL(nfs4_acl_get_whotype);
837EXPORT_SYMBOL(nfs4_acl_write_who);