blob: 435a9be1265ee29aebcc5ef064d106d0f3e62f5a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Common NFSv4 ACL handling code.
3 *
4 * Copyright (c) 2002, 2003 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Marius Aamodt Eriksen <marius@umich.edu>
8 * Jeff Sedlak <jsedlak@umich.edu>
9 * J. Bruce Fields <bfields@umich.edu>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/nfs_fs.h>
Paul Gortmakerafeacc82011-05-26 16:00:52 -040039#include <linux/export.h>
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050040#include "acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42
43/* mode bit translations: */
44#define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
45#define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
46#define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
47#define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
48#define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
49
50/* We don't support these bits; insist they be neither allowed nor denied */
51#define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
52 | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
53
54/* flags used to simulate posix default ACLs */
55#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
J. Bruce Fields7bdfa682007-02-16 01:28:28 -080056 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
J. Bruce Fields7bdfa682007-02-16 01:28:28 -080058#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
59 | NFS4_ACE_INHERIT_ONLY_ACE \
60 | NFS4_ACE_IDENTIFIER_GROUP)
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define MASK_EQUAL(mask1, mask2) \
63 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
64
65static u32
66mask_from_posix(unsigned short perm, unsigned int flags)
67{
68 int mask = NFS4_ANYONE_MODE;
69
70 if (flags & NFS4_ACL_OWNER)
71 mask |= NFS4_OWNER_MODE;
72 if (perm & ACL_READ)
73 mask |= NFS4_READ_MODE;
74 if (perm & ACL_WRITE)
75 mask |= NFS4_WRITE_MODE;
76 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
77 mask |= NFS4_ACE_DELETE_CHILD;
78 if (perm & ACL_EXECUTE)
79 mask |= NFS4_EXECUTE_MODE;
80 return mask;
81}
82
83static u32
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -080084deny_mask_from_posix(unsigned short perm, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -080086 u32 mask = 0;
87
88 if (perm & ACL_READ)
89 mask |= NFS4_READ_MODE;
90 if (perm & ACL_WRITE)
91 mask |= NFS4_WRITE_MODE;
92 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
93 mask |= NFS4_ACE_DELETE_CHILD;
94 if (perm & ACL_EXECUTE)
95 mask |= NFS4_EXECUTE_MODE;
96 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99/* XXX: modify functions to return NFS errors; they're only ever
100 * used by nfs code, after all.... */
101
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700102/* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
103 * side of being more restrictive, so the mode bit mapping below is
104 * pessimistic. An optimistic version would be needed to handle DENY's,
105 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
106 * bits. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700108static void
109low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
110{
111 u32 write_mode = NFS4_WRITE_MODE;
112
113 if (flags & NFS4_ACL_DIR)
114 write_mode |= NFS4_ACE_DELETE_CHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *mode = 0;
116 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
117 *mode |= ACL_READ;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700118 if ((perm & write_mode) == write_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *mode |= ACL_WRITE;
120 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
121 *mode |= ACL_EXECUTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124struct ace_container {
125 struct nfs4_ace *ace;
126 struct list_head ace_l;
127};
128
129static short ace2type(struct nfs4_ace *);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800130static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
131 unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133struct nfs4_acl *
134nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
135 unsigned int flags)
136{
137 struct nfs4_acl *acl;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800138 int size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800140 if (pacl) {
141 if (posix_acl_valid(pacl) < 0)
142 return ERR_PTR(-EINVAL);
143 size += 2*pacl->a_count;
144 }
145 if (dpacl) {
146 if (posix_acl_valid(dpacl) < 0)
147 return ERR_PTR(-EINVAL);
148 size += 2*dpacl->a_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800151 /* Allocate for worst case: one (deny, allow) pair each: */
152 acl = nfs4_acl_new(size);
153 if (acl == NULL)
154 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800156 if (pacl)
157 _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800159 if (dpacl)
160 _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 return acl;
163}
164
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800165struct posix_acl_summary {
166 unsigned short owner;
167 unsigned short users;
168 unsigned short group;
169 unsigned short groups;
170 unsigned short other;
171 unsigned short mask;
172};
173
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800174static void
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800175summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800177 struct posix_acl_entry *pa, *pe;
J. Bruce Fieldsf7fede42007-07-17 04:04:36 -0700178
179 /*
180 * Only pas.users and pas.groups need initialization; previous
181 * posix_acl_valid() calls ensure that the other fields will be
182 * initialized in the following loop. But, just to placate gcc:
183 */
184 memset(pas, 0, sizeof(*pas));
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800185 pas->mask = 07;
186
187 pe = acl->a_entries + acl->a_count;
188
189 FOREACH_ACL_ENTRY(pa, acl, pe) {
190 switch (pa->e_tag) {
191 case ACL_USER_OBJ:
192 pas->owner = pa->e_perm;
193 break;
194 case ACL_GROUP_OBJ:
195 pas->group = pa->e_perm;
196 break;
197 case ACL_USER:
198 pas->users |= pa->e_perm;
199 break;
200 case ACL_GROUP:
201 pas->groups |= pa->e_perm;
202 break;
203 case ACL_OTHER:
204 pas->other = pa->e_perm;
205 break;
206 case ACL_MASK:
207 pas->mask = pa->e_perm;
208 break;
209 }
210 }
211 /* We'll only care about effective permissions: */
212 pas->users &= pas->mask;
213 pas->group &= pas->mask;
214 pas->groups &= pas->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/* We assume the acl has been verified with posix_acl_valid. */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800218static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
220 unsigned int flags)
221{
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800222 struct posix_acl_entry *pa, *group_owner_entry;
223 struct nfs4_ace *ace;
224 struct posix_acl_summary pas;
225 unsigned short deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
Bruce Fields54c04402007-03-26 21:32:09 -0800227 NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 BUG_ON(pacl->a_count < 3);
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800230 summarize_posix_acl(pacl, &pas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 pa = pacl->a_entries;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800233 ace = acl->aces + acl->naces;
234
235 /* We could deny everything not granted by the owner: */
236 deny = ~pas.owner;
237 /*
238 * but it is equivalent (and simpler) to deny only what is not
239 * granted by later entries:
240 */
241 deny &= pas.users | pas.group | pas.groups | pas.other;
242 if (deny) {
243 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
244 ace->flag = eflag;
245 ace->access_mask = deny_mask_from_posix(deny, flags);
246 ace->whotype = NFS4_ACL_WHO_OWNER;
247 ace++;
248 acl->naces++;
249 }
250
251 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
252 ace->flag = eflag;
253 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
254 ace->whotype = NFS4_ACL_WHO_OWNER;
255 ace++;
256 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 pa++;
258
259 while (pa->e_tag == ACL_USER) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800260 deny = ~(pa->e_perm & pas.mask);
261 deny &= pas.groups | pas.group | pas.other;
262 if (deny) {
263 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
264 ace->flag = eflag;
265 ace->access_mask = deny_mask_from_posix(deny, flags);
266 ace->whotype = NFS4_ACL_WHO_NAMED;
267 ace->who = pa->e_id;
268 ace++;
269 acl->naces++;
270 }
271 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
272 ace->flag = eflag;
273 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
274 flags);
275 ace->whotype = NFS4_ACL_WHO_NAMED;
276 ace->who = pa->e_id;
277 ace++;
278 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 pa++;
280 }
281
282 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
283 * since a user can be in more than one group. */
284
285 /* allow ACEs */
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 group_owner_entry = pa;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800288
289 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
290 ace->flag = eflag;
291 ace->access_mask = mask_from_posix(pas.group, flags);
292 ace->whotype = NFS4_ACL_WHO_GROUP;
293 ace++;
294 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 pa++;
296
297 while (pa->e_tag == ACL_GROUP) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800298 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
299 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
300 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
301 flags);
302 ace->whotype = NFS4_ACL_WHO_NAMED;
303 ace->who = pa->e_id;
304 ace++;
305 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 pa++;
307 }
308
309 /* deny ACEs */
310
311 pa = group_owner_entry;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800312
313 deny = ~pas.group & pas.other;
314 if (deny) {
315 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
Frank Filzd8d0b852009-08-27 17:35:41 -0400316 ace->flag = eflag;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800317 ace->access_mask = deny_mask_from_posix(deny, flags);
318 ace->whotype = NFS4_ACL_WHO_GROUP;
319 ace++;
320 acl->naces++;
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 pa++;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 while (pa->e_tag == ACL_GROUP) {
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800325 deny = ~(pa->e_perm & pas.mask);
326 deny &= pas.other;
327 if (deny) {
328 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
329 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
Frank Filz55bb55d2009-08-14 15:02:30 -0700330 ace->access_mask = deny_mask_from_posix(deny, flags);
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800331 ace->whotype = NFS4_ACL_WHO_NAMED;
332 ace->who = pa->e_id;
333 ace++;
334 acl->naces++;
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 pa++;
337 }
338
339 if (pa->e_tag == ACL_MASK)
340 pa++;
J. Bruce Fieldsbec50c42007-02-16 01:28:36 -0800341 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
342 ace->flag = eflag;
343 ace->access_mask = mask_from_posix(pa->e_perm, flags);
344 ace->whotype = NFS4_ACL_WHO_EVERYONE;
345 acl->naces++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348static void
349sort_pacl_range(struct posix_acl *pacl, int start, int end) {
350 int sorted = 0, i;
351 struct posix_acl_entry tmp;
352
353 /* We just do a bubble sort; easy to do in place, and we're not
354 * expecting acl's to be long enough to justify anything more. */
355 while (!sorted) {
356 sorted = 1;
357 for (i = start; i < end; i++) {
358 if (pacl->a_entries[i].e_id
359 > pacl->a_entries[i+1].e_id) {
360 sorted = 0;
361 tmp = pacl->a_entries[i];
362 pacl->a_entries[i] = pacl->a_entries[i+1];
363 pacl->a_entries[i+1] = tmp;
364 }
365 }
366 }
367}
368
369static void
370sort_pacl(struct posix_acl *pacl)
371{
372 /* posix_acl_valid requires that users and groups be in order
373 * by uid/gid. */
374 int i, j;
375
Kinglong Mee6ba3ac42014-04-18 20:49:04 +0800376 /* no users or groups */
377 if (!pacl || pacl->a_count <= 4)
378 return;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 i = 1;
381 while (pacl->a_entries[i].e_tag == ACL_USER)
382 i++;
383 sort_pacl_range(pacl, 1, i-1);
384
385 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
Frank Filzaba24d72009-10-21 16:45:02 -0700386 j = ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 while (pacl->a_entries[j].e_tag == ACL_GROUP)
388 j++;
389 sort_pacl_range(pacl, i, j-1);
390 return;
391}
392
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700393/*
394 * While processing the NFSv4 ACE, this maintains bitmasks representing
395 * which permission bits have been allowed and which denied to a given
396 * entity: */
397struct posix_ace_state {
398 u32 allow;
399 u32 deny;
400};
401
402struct posix_user_ace_state {
403 uid_t uid;
404 struct posix_ace_state perms;
405};
406
407struct posix_ace_state_array {
408 int n;
409 struct posix_user_ace_state aces[];
410};
411
412/*
413 * While processing the NFSv4 ACE, this maintains the partial permissions
414 * calculated so far: */
415
416struct posix_acl_state {
J. Bruce Fields3160a712007-02-16 01:28:37 -0800417 int empty;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700418 struct posix_ace_state owner;
419 struct posix_ace_state group;
420 struct posix_ace_state other;
421 struct posix_ace_state everyone;
422 struct posix_ace_state mask; /* Deny unused in this case */
423 struct posix_ace_state_array *users;
424 struct posix_ace_state_array *groups;
425};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427static int
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700428init_state(struct posix_acl_state *state, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700430 int alloc;
431
432 memset(state, 0, sizeof(struct posix_acl_state));
J. Bruce Fields3160a712007-02-16 01:28:37 -0800433 state->empty = 1;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700434 /*
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)
J. Bruce Fields91b80962008-08-29 19:18:45 -0400440 + cnt*sizeof(struct posix_user_ace_state);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700441 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 Fields3160a712007-02-16 01:28:37 -0800501 /*
502 * ACLs with no ACEs are treated differently in the inheritable
Kinglong Mee6ba3ac42014-04-18 20:49:04 +0800503 * and effective cases: when there are no inheritable ACEs,
504 * calls ->set_acl with a NULL ACL structure.
J. Bruce Fields3160a712007-02-16 01:28:37 -0800505 */
Kinglong Mee6ba3ac42014-04-18 20:49:04 +0800506 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT))
507 return NULL;
508
J. Bruce Fields3160a712007-02-16 01:28:37 -0800509 /*
510 * When there are no effective ACEs, the following will end
511 * up setting a 3-element effective posix ACL with all
512 * permissions zero.
513 */
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700514 nace = 4 + state->users->n + state->groups->n;
515 pacl = posix_acl_alloc(nace, GFP_KERNEL);
516 if (!pacl)
517 return ERR_PTR(-ENOMEM);
518
519 pace = pacl->a_entries;
520 pace->e_tag = ACL_USER_OBJ;
521 error = check_deny(state->owner.deny, 1);
522 if (error)
523 goto out_err;
524 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
525 pace->e_id = ACL_UNDEFINED_ID;
526
527 for (i=0; i < state->users->n; i++) {
528 pace++;
529 pace->e_tag = ACL_USER;
530 error = check_deny(state->users->aces[i].perms.deny, 0);
531 if (error)
532 goto out_err;
533 low_mode_from_nfs4(state->users->aces[i].perms.allow,
534 &pace->e_perm, flags);
535 pace->e_id = state->users->aces[i].uid;
536 add_to_mask(state, &state->users->aces[i].perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700538
539 pace++;
540 pace->e_tag = ACL_GROUP_OBJ;
541 error = check_deny(state->group.deny, 0);
542 if (error)
543 goto out_err;
544 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
545 pace->e_id = ACL_UNDEFINED_ID;
546 add_to_mask(state, &state->group);
547
548 for (i=0; i < state->groups->n; i++) {
549 pace++;
550 pace->e_tag = ACL_GROUP;
551 error = check_deny(state->groups->aces[i].perms.deny, 0);
552 if (error)
553 goto out_err;
554 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
555 &pace->e_perm, flags);
556 pace->e_id = state->groups->aces[i].uid;
557 add_to_mask(state, &state->groups->aces[i].perms);
558 }
559
560 pace++;
561 pace->e_tag = ACL_MASK;
562 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
563 pace->e_id = ACL_UNDEFINED_ID;
564
565 pace++;
566 pace->e_tag = ACL_OTHER;
567 error = check_deny(state->other.deny, 0);
568 if (error)
569 goto out_err;
570 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
571 pace->e_id = ACL_UNDEFINED_ID;
572
573 return pacl;
574out_err:
575 posix_acl_release(pacl);
576 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700579static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700581 /* Allow all bits in the mask not already denied: */
582 astate->allow |= mask & ~astate->deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700585static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700587 /* Deny all bits in the mask not already allowed: */
588 astate->deny |= mask & ~astate->allow;
589}
590
591static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
592{
593 int i;
594
595 for (i = 0; i < a->n; i++)
596 if (a->aces[i].uid == uid)
597 return i;
598 /* Not found: */
599 a->n++;
600 a->aces[i].uid = uid;
601 a->aces[i].perms.allow = state->everyone.allow;
602 a->aces[i].perms.deny = state->everyone.deny;
603
604 return i;
605}
606
607static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
608{
609 int i;
610
611 for (i=0; i < a->n; i++)
612 deny_bits(&a->aces[i].perms, mask);
613}
614
615static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
616{
617 int i;
618
619 for (i=0; i < a->n; i++)
620 allow_bits(&a->aces[i].perms, mask);
621}
622
623static void process_one_v4_ace(struct posix_acl_state *state,
624 struct nfs4_ace *ace)
625{
626 u32 mask = ace->access_mask;
627 int i;
628
J. Bruce Fields3160a712007-02-16 01:28:37 -0800629 state->empty = 0;
630
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700631 switch (ace2type(ace)) {
632 case ACL_USER_OBJ:
633 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
634 allow_bits(&state->owner, mask);
635 } else {
636 deny_bits(&state->owner, mask);
637 }
638 break;
639 case ACL_USER:
640 i = find_uid(state, state->users, ace->who);
641 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
642 allow_bits(&state->users->aces[i].perms, mask);
643 } else {
644 deny_bits(&state->users->aces[i].perms, mask);
645 mask = state->users->aces[i].perms.deny;
646 deny_bits(&state->owner, mask);
647 }
648 break;
649 case ACL_GROUP_OBJ:
650 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
651 allow_bits(&state->group, mask);
652 } else {
653 deny_bits(&state->group, mask);
654 mask = state->group.deny;
655 deny_bits(&state->owner, mask);
656 deny_bits(&state->everyone, mask);
657 deny_bits_array(state->users, mask);
658 deny_bits_array(state->groups, mask);
659 }
660 break;
661 case ACL_GROUP:
662 i = find_uid(state, state->groups, ace->who);
663 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
664 allow_bits(&state->groups->aces[i].perms, mask);
665 } else {
666 deny_bits(&state->groups->aces[i].perms, mask);
667 mask = state->groups->aces[i].perms.deny;
668 deny_bits(&state->owner, mask);
669 deny_bits(&state->group, mask);
670 deny_bits(&state->everyone, mask);
671 deny_bits_array(state->users, mask);
672 deny_bits_array(state->groups, mask);
673 }
674 break;
675 case ACL_OTHER:
676 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
677 allow_bits(&state->owner, mask);
678 allow_bits(&state->group, mask);
679 allow_bits(&state->other, mask);
680 allow_bits(&state->everyone, mask);
681 allow_bits_array(state->users, mask);
682 allow_bits_array(state->groups, mask);
683 } else {
684 deny_bits(&state->owner, mask);
685 deny_bits(&state->group, mask);
686 deny_bits(&state->other, mask);
687 deny_bits(&state->everyone, mask);
688 deny_bits_array(state->users, mask);
689 deny_bits_array(state->groups, mask);
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692}
693
J. Bruce Fields575a6292007-02-16 01:28:29 -0800694int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
695 struct posix_acl **dpacl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
J. Bruce Fields575a6292007-02-16 01:28:29 -0800697 struct posix_acl_state effective_acl_state, default_acl_state;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700698 struct nfs4_ace *ace;
699 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
J. Bruce Fields575a6292007-02-16 01:28:29 -0800701 ret = init_state(&effective_acl_state, acl->naces);
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700702 if (ret)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800703 return ret;
704 ret = init_state(&default_acl_state, acl->naces);
705 if (ret)
706 goto out_estate;
707 ret = -EINVAL;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800708 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700709 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
710 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800711 goto out_dstate;
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700712 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
J. Bruce Fields575a6292007-02-16 01:28:29 -0800713 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800714 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
J. Bruce Fields575a6292007-02-16 01:28:29 -0800715 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 continue;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800717 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800718 if (!(flags & NFS4_ACL_DIR))
719 goto out_dstate;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800720 /*
721 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
722 * is set, we're effectively turning on the other. That's OK,
723 * according to rfc 3530.
724 */
J. Bruce Fields575a6292007-02-16 01:28:29 -0800725 process_one_v4_ace(&default_acl_state, ace);
726
727 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
728 process_one_v4_ace(&effective_acl_state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
J. Bruce Fields575a6292007-02-16 01:28:29 -0800730 *pacl = posix_state_to_acl(&effective_acl_state, flags);
731 if (IS_ERR(*pacl)) {
732 ret = PTR_ERR(*pacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700733 *pacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800734 goto out_dstate;
735 }
J. Bruce Fields3160a712007-02-16 01:28:37 -0800736 *dpacl = posix_state_to_acl(&default_acl_state,
737 flags | NFS4_ACL_TYPE_DEFAULT);
J. Bruce Fields575a6292007-02-16 01:28:29 -0800738 if (IS_ERR(*dpacl)) {
739 ret = PTR_ERR(*dpacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700740 *dpacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800741 posix_acl_release(*pacl);
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700742 *pacl = NULL;
J. Bruce Fields575a6292007-02-16 01:28:29 -0800743 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788static struct {
789 char *string;
790 int stringlen;
791 int type;
792} s2t_map[] = {
793 {
794 .string = "OWNER@",
795 .stringlen = sizeof("OWNER@") - 1,
796 .type = NFS4_ACL_WHO_OWNER,
797 },
798 {
799 .string = "GROUP@",
800 .stringlen = sizeof("GROUP@") - 1,
801 .type = NFS4_ACL_WHO_GROUP,
802 },
803 {
804 .string = "EVERYONE@",
805 .stringlen = sizeof("EVERYONE@") - 1,
806 .type = NFS4_ACL_WHO_EVERYONE,
807 },
808};
809
810int
811nfs4_acl_get_whotype(char *p, u32 len)
812{
813 int i;
814
Tobias Klausere8c96f82006-03-24 03:15:34 -0800815 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (s2t_map[i].stringlen == len &&
817 0 == memcmp(s2t_map[i].string, p, len))
818 return s2t_map[i].type;
819 }
820 return NFS4_ACL_WHO_NAMED;
821}
822
823int
824nfs4_acl_write_who(int who, char *p)
825{
826 int i;
827
Tobias Klausere8c96f82006-03-24 03:15:34 -0800828 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (s2t_map[i].type == who) {
830 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
831 return s2t_map[i].stringlen;
832 }
833 }
834 BUG();
835 return -1;
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838EXPORT_SYMBOL(nfs4_acl_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839EXPORT_SYMBOL(nfs4_acl_get_whotype);
840EXPORT_SYMBOL(nfs4_acl_write_who);