]> git.ipfire.org Git - people/ms/linux.git/blame - fs/posix_acl.c
Merge branch 'for-6.0/dax' into libnvdimm-fixes
[people/ms/linux.git] / fs / posix_acl.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
5c8ebd57 3 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
1da177e4 4 *
5c8ebd57
CH
5 * Fixes from William Schumacher incorporated on 15 March 2001.
6 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
1da177e4
LT
7 */
8
9/*
10 * This file contains generic functions for manipulating
11 * POSIX 1003.1e draft standard 17 ACLs.
12 */
13
14#include <linux/kernel.h>
15#include <linux/slab.h>
60063497 16#include <linux/atomic.h>
1da177e4
LT
17#include <linux/fs.h>
18#include <linux/sched.h>
5b825c3a 19#include <linux/cred.h>
1da177e4 20#include <linux/posix_acl.h>
5c8ebd57 21#include <linux/posix_acl_xattr.h>
2aeccbe9 22#include <linux/xattr.h>
630d9c47 23#include <linux/export.h>
5c8ebd57 24#include <linux/user_namespace.h>
332f606b 25#include <linux/namei.h>
a793d79e 26#include <linux/mnt_idmapping.h>
1da177e4 27
04c57f45 28static struct posix_acl **acl_by_type(struct inode *inode, int type)
0afaa120
AM
29{
30 switch (type) {
31 case ACL_TYPE_ACCESS:
32 return &inode->i_acl;
33 case ACL_TYPE_DEFAULT:
34 return &inode->i_default_acl;
35 default:
36 BUG();
37 }
38}
0afaa120
AM
39
40struct posix_acl *get_cached_acl(struct inode *inode, int type)
41{
42 struct posix_acl **p = acl_by_type(inode, type);
b8a7a3a6
AG
43 struct posix_acl *acl;
44
45 for (;;) {
46 rcu_read_lock();
47 acl = rcu_dereference(*p);
48 if (!acl || is_uncached_acl(acl) ||
66717260 49 refcount_inc_not_zero(&acl->a_refcount))
b8a7a3a6
AG
50 break;
51 rcu_read_unlock();
52 cpu_relax();
0afaa120 53 }
b8a7a3a6 54 rcu_read_unlock();
0afaa120
AM
55 return acl;
56}
57EXPORT_SYMBOL(get_cached_acl);
58
59struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
60{
332f606b
MS
61 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
62
63 if (acl == ACL_DONT_CACHE) {
64 struct posix_acl *ret;
65
66 ret = inode->i_op->get_acl(inode, type, LOOKUP_RCU);
67 if (!IS_ERR(ret))
68 acl = ret;
69 }
70
71 return acl;
0afaa120
AM
72}
73EXPORT_SYMBOL(get_cached_acl_rcu);
74
75void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
76{
77 struct posix_acl **p = acl_by_type(inode, type);
78 struct posix_acl *old;
b8a7a3a6
AG
79
80 old = xchg(p, posix_acl_dup(acl));
81 if (!is_uncached_acl(old))
0afaa120
AM
82 posix_acl_release(old);
83}
84EXPORT_SYMBOL(set_cached_acl);
85
b8a7a3a6 86static void __forget_cached_acl(struct posix_acl **p)
0afaa120 87{
0afaa120 88 struct posix_acl *old;
b8a7a3a6
AG
89
90 old = xchg(p, ACL_NOT_CACHED);
91 if (!is_uncached_acl(old))
0afaa120
AM
92 posix_acl_release(old);
93}
b8a7a3a6
AG
94
95void forget_cached_acl(struct inode *inode, int type)
96{
97 __forget_cached_acl(acl_by_type(inode, type));
98}
0afaa120
AM
99EXPORT_SYMBOL(forget_cached_acl);
100
101void forget_all_cached_acls(struct inode *inode)
102{
b8a7a3a6
AG
103 __forget_cached_acl(&inode->i_acl);
104 __forget_cached_acl(&inode->i_default_acl);
0afaa120
AM
105}
106EXPORT_SYMBOL(forget_all_cached_acls);
1da177e4 107
2982baa2
CH
108struct posix_acl *get_acl(struct inode *inode, int type)
109{
b8a7a3a6
AG
110 void *sentinel;
111 struct posix_acl **p;
2982baa2
CH
112 struct posix_acl *acl;
113
b8a7a3a6
AG
114 /*
115 * The sentinel is used to detect when another operation like
116 * set_cached_acl() or forget_cached_acl() races with get_acl().
117 * It is guaranteed that is_uncached_acl(sentinel) is true.
118 */
119
2982baa2 120 acl = get_cached_acl(inode, type);
b8a7a3a6 121 if (!is_uncached_acl(acl))
2982baa2
CH
122 return acl;
123
124 if (!IS_POSIXACL(inode))
125 return NULL;
126
b8a7a3a6
AG
127 sentinel = uncached_acl_sentinel(current);
128 p = acl_by_type(inode, type);
129
130 /*
131 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
132 * current value of the ACL will not be ACL_NOT_CACHED and so our own
133 * sentinel will not be set; another task will update the cache. We
134 * could wait for that other task to complete its job, but it's easier
135 * to just call ->get_acl to fetch the ACL ourself. (This is going to
136 * be an unlikely race.)
137 */
d1cef29a 138 cmpxchg(p, ACL_NOT_CACHED, sentinel);
b8a7a3a6 139
2982baa2 140 /*
b8a7a3a6
AG
141 * Normally, the ACL returned by ->get_acl will be cached.
142 * A filesystem can prevent that by calling
143 * forget_cached_acl(inode, type) in ->get_acl.
2982baa2
CH
144 *
145 * If the filesystem doesn't have a get_acl() function at all, we'll
146 * just create the negative cache entry.
147 */
148 if (!inode->i_op->get_acl) {
149 set_cached_acl(inode, type, NULL);
150 return NULL;
151 }
0cad6246 152 acl = inode->i_op->get_acl(inode, type, false);
b8a7a3a6
AG
153
154 if (IS_ERR(acl)) {
155 /*
156 * Remove our sentinel so that we don't block future attempts
157 * to cache the ACL.
158 */
159 cmpxchg(p, sentinel, ACL_NOT_CACHED);
160 return acl;
161 }
162
163 /*
164 * Cache the result, but only if our sentinel is still in place.
165 */
166 posix_acl_dup(acl);
167 if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
168 posix_acl_release(acl);
169 return acl;
2982baa2
CH
170}
171EXPORT_SYMBOL(get_acl);
172
f61f6da0
CL
173/*
174 * Init a fresh posix_acl
175 */
176void
177posix_acl_init(struct posix_acl *acl, int count)
178{
66717260 179 refcount_set(&acl->a_refcount, 1);
f61f6da0
CL
180 acl->a_count = count;
181}
0afaa120 182EXPORT_SYMBOL(posix_acl_init);
f61f6da0 183
1da177e4
LT
184/*
185 * Allocate a new ACL with the specified number of entries.
186 */
187struct posix_acl *
dd0fc66f 188posix_acl_alloc(int count, gfp_t flags)
1da177e4
LT
189{
190 const size_t size = sizeof(struct posix_acl) +
191 count * sizeof(struct posix_acl_entry);
192 struct posix_acl *acl = kmalloc(size, flags);
f61f6da0
CL
193 if (acl)
194 posix_acl_init(acl, count);
1da177e4
LT
195 return acl;
196}
0afaa120 197EXPORT_SYMBOL(posix_acl_alloc);
1da177e4
LT
198
199/*
200 * Clone an ACL.
201 */
8043bffd 202struct posix_acl *
dd0fc66f 203posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
1da177e4
LT
204{
205 struct posix_acl *clone = NULL;
206
207 if (acl) {
208 int size = sizeof(struct posix_acl) + acl->a_count *
209 sizeof(struct posix_acl_entry);
52978be6
AD
210 clone = kmemdup(acl, size, flags);
211 if (clone)
66717260 212 refcount_set(&clone->a_refcount, 1);
1da177e4
LT
213 }
214 return clone;
215}
8043bffd 216EXPORT_SYMBOL_GPL(posix_acl_clone);
1da177e4
LT
217
218/*
219 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
220 */
221int
0d4d717f 222posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
1da177e4
LT
223{
224 const struct posix_acl_entry *pa, *pe;
225 int state = ACL_USER_OBJ;
1da177e4
LT
226 int needs_mask = 0;
227
228 FOREACH_ACL_ENTRY(pa, acl, pe) {
229 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
230 return -EINVAL;
231 switch (pa->e_tag) {
232 case ACL_USER_OBJ:
233 if (state == ACL_USER_OBJ) {
1da177e4
LT
234 state = ACL_USER;
235 break;
236 }
237 return -EINVAL;
238
239 case ACL_USER:
240 if (state != ACL_USER)
241 return -EINVAL;
0d4d717f 242 if (!kuid_has_mapping(user_ns, pa->e_uid))
1da177e4 243 return -EINVAL;
1da177e4
LT
244 needs_mask = 1;
245 break;
246
247 case ACL_GROUP_OBJ:
248 if (state == ACL_USER) {
1da177e4
LT
249 state = ACL_GROUP;
250 break;
251 }
252 return -EINVAL;
253
254 case ACL_GROUP:
255 if (state != ACL_GROUP)
256 return -EINVAL;
0d4d717f 257 if (!kgid_has_mapping(user_ns, pa->e_gid))
2f6f0654 258 return -EINVAL;
1da177e4
LT
259 needs_mask = 1;
260 break;
261
262 case ACL_MASK:
263 if (state != ACL_GROUP)
264 return -EINVAL;
265 state = ACL_OTHER;
266 break;
267
268 case ACL_OTHER:
269 if (state == ACL_OTHER ||
270 (state == ACL_GROUP && !needs_mask)) {
271 state = 0;
272 break;
273 }
274 return -EINVAL;
275
276 default:
277 return -EINVAL;
278 }
279 }
280 if (state == 0)
281 return 0;
282 return -EINVAL;
283}
0afaa120 284EXPORT_SYMBOL(posix_acl_valid);
1da177e4
LT
285
286/*
287 * Returns 0 if the acl can be exactly represented in the traditional
288 * file mode permission bits, or else 1. Returns -E... on error.
289 */
290int
d6952123 291posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
1da177e4
LT
292{
293 const struct posix_acl_entry *pa, *pe;
d6952123 294 umode_t mode = 0;
1da177e4
LT
295 int not_equiv = 0;
296
50c6e282
CH
297 /*
298 * A null ACL can always be presented as mode bits.
299 */
300 if (!acl)
301 return 0;
302
1da177e4
LT
303 FOREACH_ACL_ENTRY(pa, acl, pe) {
304 switch (pa->e_tag) {
305 case ACL_USER_OBJ:
306 mode |= (pa->e_perm & S_IRWXO) << 6;
307 break;
308 case ACL_GROUP_OBJ:
309 mode |= (pa->e_perm & S_IRWXO) << 3;
310 break;
311 case ACL_OTHER:
312 mode |= pa->e_perm & S_IRWXO;
313 break;
314 case ACL_MASK:
315 mode = (mode & ~S_IRWXG) |
316 ((pa->e_perm & S_IRWXO) << 3);
317 not_equiv = 1;
318 break;
319 case ACL_USER:
320 case ACL_GROUP:
321 not_equiv = 1;
322 break;
323 default:
324 return -EINVAL;
325 }
326 }
327 if (mode_p)
328 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
329 return not_equiv;
330}
0afaa120 331EXPORT_SYMBOL(posix_acl_equiv_mode);
1da177e4
LT
332
333/*
334 * Create an ACL representing the file mode permission bits of an inode.
335 */
336struct posix_acl *
3a5fba19 337posix_acl_from_mode(umode_t mode, gfp_t flags)
1da177e4
LT
338{
339 struct posix_acl *acl = posix_acl_alloc(3, flags);
340 if (!acl)
341 return ERR_PTR(-ENOMEM);
342
343 acl->a_entries[0].e_tag = ACL_USER_OBJ;
1da177e4
LT
344 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
345
346 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
1da177e4
LT
347 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
348
349 acl->a_entries[2].e_tag = ACL_OTHER;
1da177e4
LT
350 acl->a_entries[2].e_perm = (mode & S_IRWXO);
351 return acl;
352}
0afaa120 353EXPORT_SYMBOL(posix_acl_from_mode);
1da177e4
LT
354
355/*
356 * Return 0 if current is granted want access to the inode
357 * by the acl. Returns -E... otherwise.
358 */
359int
47291baa
CB
360posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode,
361 const struct posix_acl *acl, int want)
1da177e4
LT
362{
363 const struct posix_acl_entry *pa, *pe, *mask_obj;
abfcf55d 364 struct user_namespace *fs_userns = i_user_ns(inode);
1da177e4 365 int found = 0;
e933c15f
CB
366 vfsuid_t vfsuid;
367 vfsgid_t vfsgid;
1da177e4 368
63d72b93 369 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
d124b60a 370
1da177e4
LT
371 FOREACH_ACL_ENTRY(pa, acl, pe) {
372 switch(pa->e_tag) {
373 case ACL_USER_OBJ:
374 /* (May have been checked already) */
e933c15f
CB
375 vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
376 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
1da177e4
LT
377 goto check_perm;
378 break;
379 case ACL_USER:
abfcf55d 380 vfsuid = make_vfsuid(mnt_userns, fs_userns,
bd303368 381 pa->e_uid);
e933c15f 382 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
1da177e4
LT
383 goto mask;
384 break;
385 case ACL_GROUP_OBJ:
e933c15f
CB
386 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
387 if (vfsgid_in_group_p(vfsgid)) {
1da177e4
LT
388 found = 1;
389 if ((pa->e_perm & want) == want)
390 goto mask;
391 }
392 break;
393 case ACL_GROUP:
abfcf55d 394 vfsgid = make_vfsgid(mnt_userns, fs_userns,
bd303368 395 pa->e_gid);
e933c15f 396 if (vfsgid_in_group_p(vfsgid)) {
1da177e4
LT
397 found = 1;
398 if ((pa->e_perm & want) == want)
399 goto mask;
400 }
401 break;
402 case ACL_MASK:
403 break;
404 case ACL_OTHER:
405 if (found)
406 return -EACCES;
407 else
408 goto check_perm;
409 default:
410 return -EIO;
411 }
412 }
413 return -EIO;
414
415mask:
416 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
417 if (mask_obj->e_tag == ACL_MASK) {
418 if ((pa->e_perm & mask_obj->e_perm & want) == want)
419 return 0;
420 return -EACCES;
421 }
422 }
423
424check_perm:
425 if ((pa->e_perm & want) == want)
426 return 0;
427 return -EACCES;
428}
429
430/*
431 * Modify acl when creating a new inode. The caller must ensure the acl is
432 * only referenced once.
433 *
434 * mode_p initially must contain the mode parameter to the open() / creat()
435 * system calls. All permissions that are not granted by the acl are removed.
436 * The permissions in the acl are changed to reflect the mode_p parameter.
437 */
d3fb6120 438static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
1da177e4
LT
439{
440 struct posix_acl_entry *pa, *pe;
441 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
d3fb6120 442 umode_t mode = *mode_p;
1da177e4
LT
443 int not_equiv = 0;
444
445 /* assert(atomic_read(acl->a_refcount) == 1); */
446
447 FOREACH_ACL_ENTRY(pa, acl, pe) {
448 switch(pa->e_tag) {
449 case ACL_USER_OBJ:
450 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
451 mode &= (pa->e_perm << 6) | ~S_IRWXU;
452 break;
453
454 case ACL_USER:
455 case ACL_GROUP:
456 not_equiv = 1;
457 break;
458
459 case ACL_GROUP_OBJ:
460 group_obj = pa;
461 break;
462
463 case ACL_OTHER:
464 pa->e_perm &= mode | ~S_IRWXO;
465 mode &= pa->e_perm | ~S_IRWXO;
466 break;
467
468 case ACL_MASK:
469 mask_obj = pa;
470 not_equiv = 1;
471 break;
472
473 default:
474 return -EIO;
475 }
476 }
477
478 if (mask_obj) {
479 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
480 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
481 } else {
482 if (!group_obj)
483 return -EIO;
484 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
485 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
486 }
487
488 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
489 return not_equiv;
490}
491
492/*
493 * Modify the ACL for the chmod syscall.
494 */
5bf3258f 495static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
1da177e4
LT
496{
497 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
498 struct posix_acl_entry *pa, *pe;
499
500 /* assert(atomic_read(acl->a_refcount) == 1); */
501
502 FOREACH_ACL_ENTRY(pa, acl, pe) {
503 switch(pa->e_tag) {
504 case ACL_USER_OBJ:
505 pa->e_perm = (mode & S_IRWXU) >> 6;
506 break;
507
508 case ACL_USER:
509 case ACL_GROUP:
510 break;
511
512 case ACL_GROUP_OBJ:
513 group_obj = pa;
514 break;
515
516 case ACL_MASK:
517 mask_obj = pa;
518 break;
519
520 case ACL_OTHER:
521 pa->e_perm = (mode & S_IRWXO);
522 break;
523
524 default:
525 return -EIO;
526 }
527 }
528
529 if (mask_obj) {
530 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
531 } else {
532 if (!group_obj)
533 return -EIO;
534 group_obj->e_perm = (mode & S_IRWXG) >> 3;
535 }
536
537 return 0;
538}
bc26ab5f 539
826cae2f 540int
37bc1539 541__posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
826cae2f
AV
542{
543 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
544 int err = -ENOMEM;
545 if (clone) {
546 err = posix_acl_create_masq(clone, mode_p);
547 if (err < 0) {
548 posix_acl_release(clone);
549 clone = NULL;
550 }
551 }
552 posix_acl_release(*acl);
553 *acl = clone;
554 return err;
555}
37bc1539 556EXPORT_SYMBOL(__posix_acl_create);
826cae2f 557
bc26ab5f 558int
5bf3258f 559__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
bc26ab5f
AV
560{
561 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
562 int err = -ENOMEM;
563 if (clone) {
5bf3258f 564 err = __posix_acl_chmod_masq(clone, mode);
bc26ab5f
AV
565 if (err) {
566 posix_acl_release(clone);
567 clone = NULL;
568 }
569 }
570 posix_acl_release(*acl);
571 *acl = clone;
572 return err;
573}
5bf3258f
CH
574EXPORT_SYMBOL(__posix_acl_chmod);
575
e65ce2a5
CB
576/**
577 * posix_acl_chmod - chmod a posix acl
578 *
579 * @mnt_userns: user namespace of the mount @inode was found from
580 * @inode: inode to check permissions on
581 * @mode: the new mode of @inode
582 *
583 * If the inode has been found through an idmapped mount the user namespace of
584 * the vfsmount must be passed through @mnt_userns. This function will then
585 * take care to map the inode according to @mnt_userns before checking
586 * permissions. On non-idmapped mounts or if permission checking is to be
587 * performed on the raw inode simply passs init_user_ns.
588 */
5bf3258f 589int
e65ce2a5
CB
590 posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode,
591 umode_t mode)
5bf3258f
CH
592{
593 struct posix_acl *acl;
594 int ret = 0;
595
596 if (!IS_POSIXACL(inode))
597 return 0;
598 if (!inode->i_op->set_acl)
599 return -EOPNOTSUPP;
600
601 acl = get_acl(inode, ACL_TYPE_ACCESS);
789b663a
TM
602 if (IS_ERR_OR_NULL(acl)) {
603 if (acl == ERR_PTR(-EOPNOTSUPP))
604 return 0;
5bf3258f 605 return PTR_ERR(acl);
789b663a 606 }
5bf3258f 607
37bc1539 608 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
5bf3258f
CH
609 if (ret)
610 return ret;
549c7297 611 ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS);
5bf3258f
CH
612 posix_acl_release(acl);
613 return ret;
614}
bc26ab5f 615EXPORT_SYMBOL(posix_acl_chmod);
5c8ebd57 616
37bc1539
CH
617int
618posix_acl_create(struct inode *dir, umode_t *mode,
619 struct posix_acl **default_acl, struct posix_acl **acl)
620{
621 struct posix_acl *p;
c0c3a718 622 struct posix_acl *clone;
37bc1539
CH
623 int ret;
624
c0c3a718
DC
625 *acl = NULL;
626 *default_acl = NULL;
627
37bc1539 628 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
c0c3a718 629 return 0;
37bc1539
CH
630
631 p = get_acl(dir, ACL_TYPE_DEFAULT);
c0c3a718
DC
632 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
633 *mode &= ~current_umask();
634 return 0;
37bc1539 635 }
c0c3a718
DC
636 if (IS_ERR(p))
637 return PTR_ERR(p);
37bc1539 638
beaf226b 639 ret = -ENOMEM;
c0c3a718
DC
640 clone = posix_acl_clone(p, GFP_NOFS);
641 if (!clone)
beaf226b 642 goto err_release;
37bc1539 643
c0c3a718 644 ret = posix_acl_create_masq(clone, mode);
fed0b588 645 if (ret < 0)
beaf226b 646 goto err_release_clone;
37bc1539 647
c0c3a718
DC
648 if (ret == 0)
649 posix_acl_release(clone);
650 else
651 *acl = clone;
37bc1539 652
c0c3a718 653 if (!S_ISDIR(*mode))
37bc1539 654 posix_acl_release(p);
c0c3a718 655 else
37bc1539 656 *default_acl = p;
37bc1539 657
37bc1539 658 return 0;
fed0b588 659
beaf226b 660err_release_clone:
c0c3a718 661 posix_acl_release(clone);
beaf226b 662err_release:
fed0b588 663 posix_acl_release(p);
beaf226b 664 return ret;
37bc1539
CH
665}
666EXPORT_SYMBOL_GPL(posix_acl_create);
667
07393101
JK
668/**
669 * posix_acl_update_mode - update mode in set_acl
e65ce2a5
CB
670 * @mnt_userns: user namespace of the mount @inode was found from
671 * @inode: target inode
672 * @mode_p: mode (pointer) for update
673 * @acl: acl pointer
07393101
JK
674 *
675 * Update the file mode when setting an ACL: compute the new file permission
676 * bits based on the ACL. In addition, if the ACL is equivalent to the new
e39e773a 677 * file mode, set *@acl to NULL to indicate that no ACL should be set.
07393101 678 *
e39e773a 679 * As with chmod, clear the setgid bit if the caller is not in the owning group
07393101
JK
680 * or capable of CAP_FSETID (see inode_change_ok).
681 *
e65ce2a5
CB
682 * If the inode has been found through an idmapped mount the user namespace of
683 * the vfsmount must be passed through @mnt_userns. This function will then
684 * take care to map the inode according to @mnt_userns before checking
685 * permissions. On non-idmapped mounts or if permission checking is to be
686 * performed on the raw inode simply passs init_user_ns.
687 *
07393101
JK
688 * Called from set_acl inode operations.
689 */
e65ce2a5
CB
690int posix_acl_update_mode(struct user_namespace *mnt_userns,
691 struct inode *inode, umode_t *mode_p,
07393101
JK
692 struct posix_acl **acl)
693{
694 umode_t mode = inode->i_mode;
695 int error;
696
697 error = posix_acl_equiv_mode(*acl, &mode);
698 if (error < 0)
699 return error;
700 if (error == 0)
701 *acl = NULL;
e933c15f 702 if (!vfsgid_in_group_p(i_gid_into_vfsgid(mnt_userns, inode)) &&
e65ce2a5 703 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
07393101
JK
704 mode &= ~S_ISGID;
705 *mode_p = mode;
706 return 0;
707}
708EXPORT_SYMBOL(posix_acl_update_mode);
709
5c8ebd57
CH
710/*
711 * Fix up the uids and gids in posix acl extended attributes in place.
712 */
0c5fd887
CB
713static int posix_acl_fix_xattr_common(void *value, size_t size)
714{
715 struct posix_acl_xattr_header *header = value;
716 int count;
717
718 if (!header)
719 return -EINVAL;
720 if (size < sizeof(struct posix_acl_xattr_header))
721 return -EINVAL;
722 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
723 return -EINVAL;
724
725 count = posix_acl_xattr_count(size);
726 if (count < 0)
727 return -EINVAL;
728 if (count == 0)
729 return -EINVAL;
730
731 return count;
732}
733
734void posix_acl_getxattr_idmapped_mnt(struct user_namespace *mnt_userns,
735 const struct inode *inode,
736 void *value, size_t size)
5c8ebd57 737{
2211d5ba
AG
738 struct posix_acl_xattr_header *header = value;
739 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
abfcf55d 740 struct user_namespace *fs_userns = i_user_ns(inode);
5c8ebd57 741 int count;
0c5fd887
CB
742 vfsuid_t vfsuid;
743 vfsgid_t vfsgid;
5c8ebd57
CH
744 kuid_t uid;
745 kgid_t gid;
746
0c5fd887 747 if (no_idmapping(mnt_userns, i_user_ns(inode)))
5c8ebd57 748 return;
0c5fd887
CB
749
750 count = posix_acl_fix_xattr_common(value, size);
751 if (count < 0)
5c8ebd57 752 return;
0c5fd887
CB
753
754 for (end = entry + count; entry != end; entry++) {
755 switch (le16_to_cpu(entry->e_tag)) {
756 case ACL_USER:
757 uid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id));
abfcf55d 758 vfsuid = make_vfsuid(mnt_userns, fs_userns, uid);
0c5fd887
CB
759 entry->e_id = cpu_to_le32(from_kuid(&init_user_ns,
760 vfsuid_into_kuid(vfsuid)));
761 break;
762 case ACL_GROUP:
763 gid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id));
abfcf55d 764 vfsgid = make_vfsgid(mnt_userns, fs_userns, gid);
0c5fd887
CB
765 entry->e_id = cpu_to_le32(from_kgid(&init_user_ns,
766 vfsgid_into_kgid(vfsgid)));
767 break;
768 default:
769 break;
770 }
771 }
772}
773
774void posix_acl_setxattr_idmapped_mnt(struct user_namespace *mnt_userns,
775 const struct inode *inode,
776 void *value, size_t size)
777{
778 struct posix_acl_xattr_header *header = value;
779 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
abfcf55d 780 struct user_namespace *fs_userns = i_user_ns(inode);
0c5fd887
CB
781 int count;
782 vfsuid_t vfsuid;
783 vfsgid_t vfsgid;
784 kuid_t uid;
785 kgid_t gid;
786
787 if (no_idmapping(mnt_userns, i_user_ns(inode)))
5c8ebd57
CH
788 return;
789
0c5fd887 790 count = posix_acl_fix_xattr_common(value, size);
5c8ebd57
CH
791 if (count < 0)
792 return;
0c5fd887
CB
793
794 for (end = entry + count; entry != end; entry++) {
795 switch (le16_to_cpu(entry->e_tag)) {
796 case ACL_USER:
797 uid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id));
798 vfsuid = VFSUIDT_INIT(uid);
abfcf55d 799 uid = from_vfsuid(mnt_userns, fs_userns, vfsuid);
0c5fd887
CB
800 entry->e_id = cpu_to_le32(from_kuid(&init_user_ns, uid));
801 break;
802 case ACL_GROUP:
803 gid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id));
804 vfsgid = VFSGIDT_INIT(gid);
abfcf55d 805 gid = from_vfsgid(mnt_userns, fs_userns, vfsgid);
0c5fd887
CB
806 entry->e_id = cpu_to_le32(from_kgid(&init_user_ns, gid));
807 break;
808 default:
809 break;
810 }
811 }
812}
813
814static void posix_acl_fix_xattr_userns(
815 struct user_namespace *to, struct user_namespace *from,
816 void *value, size_t size)
817{
818 struct posix_acl_xattr_header *header = value;
819 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
820 int count;
821 kuid_t uid;
822 kgid_t gid;
823
824 count = posix_acl_fix_xattr_common(value, size);
825 if (count < 0)
5c8ebd57
CH
826 return;
827
828 for (end = entry + count; entry != end; entry++) {
829 switch(le16_to_cpu(entry->e_tag)) {
830 case ACL_USER:
831 uid = make_kuid(from, le32_to_cpu(entry->e_id));
832 entry->e_id = cpu_to_le32(from_kuid(to, uid));
833 break;
834 case ACL_GROUP:
835 gid = make_kgid(from, le32_to_cpu(entry->e_id));
836 entry->e_id = cpu_to_le32(from_kgid(to, gid));
837 break;
838 default:
839 break;
840 }
841 }
842}
843
0c5fd887 844void posix_acl_fix_xattr_from_user(void *value, size_t size)
5c8ebd57
CH
845{
846 struct user_namespace *user_ns = current_user_ns();
0c5fd887 847 if (user_ns == &init_user_ns)
5c8ebd57 848 return;
0c5fd887 849 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
5c8ebd57
CH
850}
851
0c5fd887 852void posix_acl_fix_xattr_to_user(void *value, size_t size)
5c8ebd57
CH
853{
854 struct user_namespace *user_ns = current_user_ns();
0c5fd887 855 if (user_ns == &init_user_ns)
5c8ebd57 856 return;
0c5fd887 857 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
5c8ebd57
CH
858}
859
860/*
861 * Convert from extended attribute to in-memory representation.
862 */
863struct posix_acl *
864posix_acl_from_xattr(struct user_namespace *user_ns,
865 const void *value, size_t size)
866{
2211d5ba
AG
867 const struct posix_acl_xattr_header *header = value;
868 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
5c8ebd57
CH
869 int count;
870 struct posix_acl *acl;
871 struct posix_acl_entry *acl_e;
872
873 if (!value)
874 return NULL;
2211d5ba 875 if (size < sizeof(struct posix_acl_xattr_header))
5c8ebd57
CH
876 return ERR_PTR(-EINVAL);
877 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
878 return ERR_PTR(-EOPNOTSUPP);
879
880 count = posix_acl_xattr_count(size);
881 if (count < 0)
882 return ERR_PTR(-EINVAL);
883 if (count == 0)
884 return NULL;
885
886 acl = posix_acl_alloc(count, GFP_NOFS);
887 if (!acl)
888 return ERR_PTR(-ENOMEM);
889 acl_e = acl->a_entries;
890
891 for (end = entry + count; entry != end; acl_e++, entry++) {
892 acl_e->e_tag = le16_to_cpu(entry->e_tag);
893 acl_e->e_perm = le16_to_cpu(entry->e_perm);
894
895 switch(acl_e->e_tag) {
896 case ACL_USER_OBJ:
897 case ACL_GROUP_OBJ:
898 case ACL_MASK:
899 case ACL_OTHER:
900 break;
901
902 case ACL_USER:
903 acl_e->e_uid =
904 make_kuid(user_ns,
905 le32_to_cpu(entry->e_id));
906 if (!uid_valid(acl_e->e_uid))
907 goto fail;
908 break;
909 case ACL_GROUP:
910 acl_e->e_gid =
911 make_kgid(user_ns,
912 le32_to_cpu(entry->e_id));
913 if (!gid_valid(acl_e->e_gid))
914 goto fail;
915 break;
916
917 default:
918 goto fail;
919 }
920 }
921 return acl;
922
923fail:
924 posix_acl_release(acl);
925 return ERR_PTR(-EINVAL);
926}
927EXPORT_SYMBOL (posix_acl_from_xattr);
928
929/*
930 * Convert from in-memory to extended attribute representation.
931 */
932int
933posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
934 void *buffer, size_t size)
935{
2211d5ba
AG
936 struct posix_acl_xattr_header *ext_acl = buffer;
937 struct posix_acl_xattr_entry *ext_entry;
5c8ebd57
CH
938 int real_size, n;
939
940 real_size = posix_acl_xattr_size(acl->a_count);
941 if (!buffer)
942 return real_size;
943 if (real_size > size)
944 return -ERANGE;
47ba9734 945
2211d5ba 946 ext_entry = (void *)(ext_acl + 1);
5c8ebd57
CH
947 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
948
949 for (n=0; n < acl->a_count; n++, ext_entry++) {
950 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
951 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
952 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
953 switch(acl_e->e_tag) {
954 case ACL_USER:
955 ext_entry->e_id =
956 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
957 break;
958 case ACL_GROUP:
959 ext_entry->e_id =
960 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
961 break;
962 default:
963 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
964 break;
965 }
966 }
967 return real_size;
968}
969EXPORT_SYMBOL (posix_acl_to_xattr);
2aeccbe9
CH
970
971static int
d9a82a04 972posix_acl_xattr_get(const struct xattr_handler *handler,
b296821a
AV
973 struct dentry *unused, struct inode *inode,
974 const char *name, void *value, size_t size)
2aeccbe9
CH
975{
976 struct posix_acl *acl;
977 int error;
978
b296821a 979 if (!IS_POSIXACL(inode))
2aeccbe9 980 return -EOPNOTSUPP;
b296821a 981 if (S_ISLNK(inode->i_mode))
2aeccbe9
CH
982 return -EOPNOTSUPP;
983
b296821a 984 acl = get_acl(inode, handler->flags);
2aeccbe9
CH
985 if (IS_ERR(acl))
986 return PTR_ERR(acl);
987 if (acl == NULL)
988 return -ENODATA;
989
990 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
991 posix_acl_release(acl);
992
993 return error;
994}
995
485e71e8 996int
e65ce2a5
CB
997set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode,
998 int type, struct posix_acl *acl)
2aeccbe9 999{
2aeccbe9
CH
1000 if (!IS_POSIXACL(inode))
1001 return -EOPNOTSUPP;
1002 if (!inode->i_op->set_acl)
1003 return -EOPNOTSUPP;
1004
485e71e8
AG
1005 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
1006 return acl ? -EACCES : 0;
e65ce2a5 1007 if (!inode_owner_or_capable(mnt_userns, inode))
2aeccbe9
CH
1008 return -EPERM;
1009
485e71e8 1010 if (acl) {
a867d734 1011 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
485e71e8
AG
1012 if (ret)
1013 return ret;
1014 }
549c7297 1015 return inode->i_op->set_acl(mnt_userns, inode, acl, type);
485e71e8
AG
1016}
1017EXPORT_SYMBOL(set_posix_acl);
1018
1019static int
1020posix_acl_xattr_set(const struct xattr_handler *handler,
e65ce2a5
CB
1021 struct user_namespace *mnt_userns,
1022 struct dentry *unused, struct inode *inode,
1023 const char *name, const void *value, size_t size,
1024 int flags)
485e71e8
AG
1025{
1026 struct posix_acl *acl = NULL;
1027 int ret;
1028
2aeccbe9
CH
1029 if (value) {
1030 acl = posix_acl_from_xattr(&init_user_ns, value, size);
1031 if (IS_ERR(acl))
1032 return PTR_ERR(acl);
2aeccbe9 1033 }
e65ce2a5 1034 ret = set_posix_acl(mnt_userns, inode, handler->flags, acl);
2aeccbe9
CH
1035 posix_acl_release(acl);
1036 return ret;
1037}
1038
764a5c6b
AG
1039static bool
1040posix_acl_xattr_list(struct dentry *dentry)
2aeccbe9 1041{
764a5c6b 1042 return IS_POSIXACL(d_backing_inode(dentry));
2aeccbe9
CH
1043}
1044
1045const struct xattr_handler posix_acl_access_xattr_handler = {
98e9cb57 1046 .name = XATTR_NAME_POSIX_ACL_ACCESS,
2aeccbe9
CH
1047 .flags = ACL_TYPE_ACCESS,
1048 .list = posix_acl_xattr_list,
1049 .get = posix_acl_xattr_get,
1050 .set = posix_acl_xattr_set,
1051};
1052EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
1053
1054const struct xattr_handler posix_acl_default_xattr_handler = {
98e9cb57 1055 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
2aeccbe9
CH
1056 .flags = ACL_TYPE_DEFAULT,
1057 .list = posix_acl_xattr_list,
1058 .get = posix_acl_xattr_get,
1059 .set = posix_acl_xattr_set,
1060};
1061EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
feda821e 1062
549c7297
CB
1063int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
1064 struct posix_acl *acl, int type)
feda821e
CH
1065{
1066 int error;
1067
1068 if (type == ACL_TYPE_ACCESS) {
549c7297 1069 error = posix_acl_update_mode(mnt_userns, inode,
497de07d
GZ
1070 &inode->i_mode, &acl);
1071 if (error)
1072 return error;
feda821e
CH
1073 }
1074
078cd827 1075 inode->i_ctime = current_time(inode);
feda821e
CH
1076 set_cached_acl(inode, type, acl);
1077 return 0;
1078}
1079
1080int simple_acl_create(struct inode *dir, struct inode *inode)
1081{
1082 struct posix_acl *default_acl, *acl;
1083 int error;
1084
1085 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1086 if (error)
1087 return error;
1088
1089 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1090 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1091
1092 if (default_acl)
1093 posix_acl_release(default_acl);
1094 if (acl)
1095 posix_acl_release(acl);
1096 return 0;
1097}