blob: 021e7c069b8677bc5bea7da08fe99e9ace41fdc6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/posix_acl.c
3 *
4 * Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org>
5 *
6 * Fixes from William Schumacher incorporated on 15 March 2001.
7 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
8 */
9
10/*
11 * This file contains generic functions for manipulating
12 * POSIX 1003.1e draft standard 17 ACLs.
13 */
14
15#include <linux/kernel.h>
16#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070017#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/fs.h>
19#include <linux/sched.h>
20#include <linux/posix_acl.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050021#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <linux/errno.h>
24
Andrew Morton0afaa122014-01-21 15:48:42 -080025struct posix_acl **acl_by_type(struct inode *inode, int type)
26{
27 switch (type) {
28 case ACL_TYPE_ACCESS:
29 return &inode->i_acl;
30 case ACL_TYPE_DEFAULT:
31 return &inode->i_default_acl;
32 default:
33 BUG();
34 }
35}
36EXPORT_SYMBOL(acl_by_type);
37
38struct posix_acl *get_cached_acl(struct inode *inode, int type)
39{
40 struct posix_acl **p = acl_by_type(inode, type);
41 struct posix_acl *acl = ACCESS_ONCE(*p);
42 if (acl) {
43 spin_lock(&inode->i_lock);
44 acl = *p;
45 if (acl != ACL_NOT_CACHED)
46 acl = posix_acl_dup(acl);
47 spin_unlock(&inode->i_lock);
48 }
49 return acl;
50}
51EXPORT_SYMBOL(get_cached_acl);
52
53struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
54{
55 return rcu_dereference(*acl_by_type(inode, type));
56}
57EXPORT_SYMBOL(get_cached_acl_rcu);
58
59void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
60{
61 struct posix_acl **p = acl_by_type(inode, type);
62 struct posix_acl *old;
63 spin_lock(&inode->i_lock);
64 old = *p;
65 rcu_assign_pointer(*p, posix_acl_dup(acl));
66 spin_unlock(&inode->i_lock);
67 if (old != ACL_NOT_CACHED)
68 posix_acl_release(old);
69}
70EXPORT_SYMBOL(set_cached_acl);
71
72void forget_cached_acl(struct inode *inode, int type)
73{
74 struct posix_acl **p = acl_by_type(inode, type);
75 struct posix_acl *old;
76 spin_lock(&inode->i_lock);
77 old = *p;
78 *p = ACL_NOT_CACHED;
79 spin_unlock(&inode->i_lock);
80 if (old != ACL_NOT_CACHED)
81 posix_acl_release(old);
82}
83EXPORT_SYMBOL(forget_cached_acl);
84
85void forget_all_cached_acls(struct inode *inode)
86{
87 struct posix_acl *old_access, *old_default;
88 spin_lock(&inode->i_lock);
89 old_access = inode->i_acl;
90 old_default = inode->i_default_acl;
91 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
92 spin_unlock(&inode->i_lock);
93 if (old_access != ACL_NOT_CACHED)
94 posix_acl_release(old_access);
95 if (old_default != ACL_NOT_CACHED)
96 posix_acl_release(old_default);
97}
98EXPORT_SYMBOL(forget_all_cached_acls);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
Chuck Leverf61f6da2011-01-21 03:05:38 +0000101 * Init a fresh posix_acl
102 */
103void
104posix_acl_init(struct posix_acl *acl, int count)
105{
106 atomic_set(&acl->a_refcount, 1);
107 acl->a_count = count;
108}
Andrew Morton0afaa122014-01-21 15:48:42 -0800109EXPORT_SYMBOL(posix_acl_init);
Chuck Leverf61f6da2011-01-21 03:05:38 +0000110
111/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Allocate a new ACL with the specified number of entries.
113 */
114struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +0100115posix_acl_alloc(int count, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 const size_t size = sizeof(struct posix_acl) +
118 count * sizeof(struct posix_acl_entry);
119 struct posix_acl *acl = kmalloc(size, flags);
Chuck Leverf61f6da2011-01-21 03:05:38 +0000120 if (acl)
121 posix_acl_init(acl, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return acl;
123}
Andrew Morton0afaa122014-01-21 15:48:42 -0800124EXPORT_SYMBOL(posix_acl_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/*
127 * Clone an ACL.
128 */
Al Viroedde8542011-07-23 03:27:37 -0400129static struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +0100130posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct posix_acl *clone = NULL;
133
134 if (acl) {
135 int size = sizeof(struct posix_acl) + acl->a_count *
136 sizeof(struct posix_acl_entry);
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700137 clone = kmemdup(acl, size, flags);
138 if (clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 atomic_set(&clone->a_refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 return clone;
142}
143
144/*
145 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
146 */
147int
148posix_acl_valid(const struct posix_acl *acl)
149{
150 const struct posix_acl_entry *pa, *pe;
151 int state = ACL_USER_OBJ;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800152 kuid_t prev_uid = INVALID_UID;
153 kgid_t prev_gid = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int needs_mask = 0;
155
156 FOREACH_ACL_ENTRY(pa, acl, pe) {
157 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
158 return -EINVAL;
159 switch (pa->e_tag) {
160 case ACL_USER_OBJ:
161 if (state == ACL_USER_OBJ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 state = ACL_USER;
163 break;
164 }
165 return -EINVAL;
166
167 case ACL_USER:
168 if (state != ACL_USER)
169 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800170 if (!uid_valid(pa->e_uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800172 if (uid_valid(prev_uid) &&
173 uid_lte(pa->e_uid, prev_uid))
174 return -EINVAL;
175 prev_uid = pa->e_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 needs_mask = 1;
177 break;
178
179 case ACL_GROUP_OBJ:
180 if (state == ACL_USER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 state = ACL_GROUP;
182 break;
183 }
184 return -EINVAL;
185
186 case ACL_GROUP:
187 if (state != ACL_GROUP)
188 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800189 if (!gid_valid(pa->e_gid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800191 if (gid_valid(prev_gid) &&
192 gid_lte(pa->e_gid, prev_gid))
193 return -EINVAL;
194 prev_gid = pa->e_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 needs_mask = 1;
196 break;
197
198 case ACL_MASK:
199 if (state != ACL_GROUP)
200 return -EINVAL;
201 state = ACL_OTHER;
202 break;
203
204 case ACL_OTHER:
205 if (state == ACL_OTHER ||
206 (state == ACL_GROUP && !needs_mask)) {
207 state = 0;
208 break;
209 }
210 return -EINVAL;
211
212 default:
213 return -EINVAL;
214 }
215 }
216 if (state == 0)
217 return 0;
218 return -EINVAL;
219}
Andrew Morton0afaa122014-01-21 15:48:42 -0800220EXPORT_SYMBOL(posix_acl_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/*
223 * Returns 0 if the acl can be exactly represented in the traditional
224 * file mode permission bits, or else 1. Returns -E... on error.
225 */
226int
Al Virod6952122011-07-23 18:56:36 -0400227posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 const struct posix_acl_entry *pa, *pe;
Al Virod6952122011-07-23 18:56:36 -0400230 umode_t mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int not_equiv = 0;
232
233 FOREACH_ACL_ENTRY(pa, acl, pe) {
234 switch (pa->e_tag) {
235 case ACL_USER_OBJ:
236 mode |= (pa->e_perm & S_IRWXO) << 6;
237 break;
238 case ACL_GROUP_OBJ:
239 mode |= (pa->e_perm & S_IRWXO) << 3;
240 break;
241 case ACL_OTHER:
242 mode |= pa->e_perm & S_IRWXO;
243 break;
244 case ACL_MASK:
245 mode = (mode & ~S_IRWXG) |
246 ((pa->e_perm & S_IRWXO) << 3);
247 not_equiv = 1;
248 break;
249 case ACL_USER:
250 case ACL_GROUP:
251 not_equiv = 1;
252 break;
253 default:
254 return -EINVAL;
255 }
256 }
257 if (mode_p)
258 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
259 return not_equiv;
260}
Andrew Morton0afaa122014-01-21 15:48:42 -0800261EXPORT_SYMBOL(posix_acl_equiv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/*
264 * Create an ACL representing the file mode permission bits of an inode.
265 */
266struct posix_acl *
Al Viro3a5fba12011-07-23 19:01:48 -0400267posix_acl_from_mode(umode_t mode, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct posix_acl *acl = posix_acl_alloc(3, flags);
270 if (!acl)
271 return ERR_PTR(-ENOMEM);
272
273 acl->a_entries[0].e_tag = ACL_USER_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
275
276 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
278
279 acl->a_entries[2].e_tag = ACL_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 acl->a_entries[2].e_perm = (mode & S_IRWXO);
281 return acl;
282}
Andrew Morton0afaa122014-01-21 15:48:42 -0800283EXPORT_SYMBOL(posix_acl_from_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285/*
286 * Return 0 if current is granted want access to the inode
287 * by the acl. Returns -E... otherwise.
288 */
289int
290posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
291{
292 const struct posix_acl_entry *pa, *pe, *mask_obj;
293 int found = 0;
294
Andreas Gruenbacherd124b602011-10-23 23:13:32 +0530295 want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 FOREACH_ACL_ENTRY(pa, acl, pe) {
298 switch(pa->e_tag) {
299 case ACL_USER_OBJ:
300 /* (May have been checked already) */
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800301 if (uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 goto check_perm;
303 break;
304 case ACL_USER:
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800305 if (uid_eq(pa->e_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goto mask;
307 break;
308 case ACL_GROUP_OBJ:
309 if (in_group_p(inode->i_gid)) {
310 found = 1;
311 if ((pa->e_perm & want) == want)
312 goto mask;
313 }
314 break;
315 case ACL_GROUP:
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800316 if (in_group_p(pa->e_gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 found = 1;
318 if ((pa->e_perm & want) == want)
319 goto mask;
320 }
321 break;
322 case ACL_MASK:
323 break;
324 case ACL_OTHER:
325 if (found)
326 return -EACCES;
327 else
328 goto check_perm;
329 default:
330 return -EIO;
331 }
332 }
333 return -EIO;
334
335mask:
336 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
337 if (mask_obj->e_tag == ACL_MASK) {
338 if ((pa->e_perm & mask_obj->e_perm & want) == want)
339 return 0;
340 return -EACCES;
341 }
342 }
343
344check_perm:
345 if ((pa->e_perm & want) == want)
346 return 0;
347 return -EACCES;
348}
349
350/*
351 * Modify acl when creating a new inode. The caller must ensure the acl is
352 * only referenced once.
353 *
354 * mode_p initially must contain the mode parameter to the open() / creat()
355 * system calls. All permissions that are not granted by the acl are removed.
356 * The permissions in the acl are changed to reflect the mode_p parameter.
357 */
Al Virod3fb6122011-07-23 18:37:50 -0400358static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct posix_acl_entry *pa, *pe;
361 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
Al Virod3fb6122011-07-23 18:37:50 -0400362 umode_t mode = *mode_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int not_equiv = 0;
364
365 /* assert(atomic_read(acl->a_refcount) == 1); */
366
367 FOREACH_ACL_ENTRY(pa, acl, pe) {
368 switch(pa->e_tag) {
369 case ACL_USER_OBJ:
370 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
371 mode &= (pa->e_perm << 6) | ~S_IRWXU;
372 break;
373
374 case ACL_USER:
375 case ACL_GROUP:
376 not_equiv = 1;
377 break;
378
379 case ACL_GROUP_OBJ:
380 group_obj = pa;
381 break;
382
383 case ACL_OTHER:
384 pa->e_perm &= mode | ~S_IRWXO;
385 mode &= pa->e_perm | ~S_IRWXO;
386 break;
387
388 case ACL_MASK:
389 mask_obj = pa;
390 not_equiv = 1;
391 break;
392
393 default:
394 return -EIO;
395 }
396 }
397
398 if (mask_obj) {
399 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
400 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
401 } else {
402 if (!group_obj)
403 return -EIO;
404 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
405 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
406 }
407
408 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
409 return not_equiv;
410}
411
412/*
413 * Modify the ACL for the chmod syscall.
414 */
Al Viro86bc7042011-07-23 19:03:11 -0400415static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
418 struct posix_acl_entry *pa, *pe;
419
420 /* assert(atomic_read(acl->a_refcount) == 1); */
421
422 FOREACH_ACL_ENTRY(pa, acl, pe) {
423 switch(pa->e_tag) {
424 case ACL_USER_OBJ:
425 pa->e_perm = (mode & S_IRWXU) >> 6;
426 break;
427
428 case ACL_USER:
429 case ACL_GROUP:
430 break;
431
432 case ACL_GROUP_OBJ:
433 group_obj = pa;
434 break;
435
436 case ACL_MASK:
437 mask_obj = pa;
438 break;
439
440 case ACL_OTHER:
441 pa->e_perm = (mode & S_IRWXO);
442 break;
443
444 default:
445 return -EIO;
446 }
447 }
448
449 if (mask_obj) {
450 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
451 } else {
452 if (!group_obj)
453 return -EIO;
454 group_obj->e_perm = (mode & S_IRWXG) >> 3;
455 }
456
457 return 0;
458}
Al Virobc26ab52011-07-23 00:18:02 -0400459
460int
Al Virod3fb6122011-07-23 18:37:50 -0400461posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
Al Viro826cae22011-07-23 03:10:32 -0400462{
463 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
464 int err = -ENOMEM;
465 if (clone) {
466 err = posix_acl_create_masq(clone, mode_p);
467 if (err < 0) {
468 posix_acl_release(clone);
469 clone = NULL;
470 }
471 }
472 posix_acl_release(*acl);
473 *acl = clone;
474 return err;
475}
476EXPORT_SYMBOL(posix_acl_create);
477
478int
Al Viro86bc7042011-07-23 19:03:11 -0400479posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
Al Virobc26ab52011-07-23 00:18:02 -0400480{
481 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
482 int err = -ENOMEM;
483 if (clone) {
484 err = posix_acl_chmod_masq(clone, mode);
485 if (err) {
486 posix_acl_release(clone);
487 clone = NULL;
488 }
489 }
490 posix_acl_release(*acl);
491 *acl = clone;
492 return err;
493}
494EXPORT_SYMBOL(posix_acl_chmod);