blk-cgroup: fix rcu lockdep warning in blkg_lookup()
[linux-block.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>
36f05cab 27#include <linux/iversion.h>
e4cc9163
CB
28#include <linux/security.h>
29#include <linux/evm.h>
30#include <linux/fsnotify.h>
5970e15d 31#include <linux/filelock.h>
e4cc9163
CB
32
33#include "internal.h"
1da177e4 34
04c57f45 35static struct posix_acl **acl_by_type(struct inode *inode, int type)
0afaa120
AM
36{
37 switch (type) {
38 case ACL_TYPE_ACCESS:
39 return &inode->i_acl;
40 case ACL_TYPE_DEFAULT:
41 return &inode->i_default_acl;
42 default:
43 BUG();
44 }
45}
0afaa120
AM
46
47struct posix_acl *get_cached_acl(struct inode *inode, int type)
48{
49 struct posix_acl **p = acl_by_type(inode, type);
b8a7a3a6
AG
50 struct posix_acl *acl;
51
52 for (;;) {
53 rcu_read_lock();
54 acl = rcu_dereference(*p);
55 if (!acl || is_uncached_acl(acl) ||
66717260 56 refcount_inc_not_zero(&acl->a_refcount))
b8a7a3a6
AG
57 break;
58 rcu_read_unlock();
59 cpu_relax();
0afaa120 60 }
b8a7a3a6 61 rcu_read_unlock();
0afaa120
AM
62 return acl;
63}
64EXPORT_SYMBOL(get_cached_acl);
65
66struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
67{
332f606b
MS
68 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
69
70 if (acl == ACL_DONT_CACHE) {
71 struct posix_acl *ret;
72
cac2f8b8 73 ret = inode->i_op->get_inode_acl(inode, type, LOOKUP_RCU);
332f606b
MS
74 if (!IS_ERR(ret))
75 acl = ret;
76 }
77
78 return acl;
0afaa120
AM
79}
80EXPORT_SYMBOL(get_cached_acl_rcu);
81
82void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
83{
84 struct posix_acl **p = acl_by_type(inode, type);
85 struct posix_acl *old;
b8a7a3a6
AG
86
87 old = xchg(p, posix_acl_dup(acl));
88 if (!is_uncached_acl(old))
0afaa120
AM
89 posix_acl_release(old);
90}
91EXPORT_SYMBOL(set_cached_acl);
92
b8a7a3a6 93static void __forget_cached_acl(struct posix_acl **p)
0afaa120 94{
0afaa120 95 struct posix_acl *old;
b8a7a3a6
AG
96
97 old = xchg(p, ACL_NOT_CACHED);
98 if (!is_uncached_acl(old))
0afaa120
AM
99 posix_acl_release(old);
100}
b8a7a3a6
AG
101
102void forget_cached_acl(struct inode *inode, int type)
103{
104 __forget_cached_acl(acl_by_type(inode, type));
105}
0afaa120
AM
106EXPORT_SYMBOL(forget_cached_acl);
107
108void forget_all_cached_acls(struct inode *inode)
109{
b8a7a3a6
AG
110 __forget_cached_acl(&inode->i_acl);
111 __forget_cached_acl(&inode->i_default_acl);
0afaa120
AM
112}
113EXPORT_SYMBOL(forget_all_cached_acls);
1da177e4 114
77435322 115static struct posix_acl *__get_acl(struct mnt_idmap *idmap,
4f353ba4
CB
116 struct dentry *dentry, struct inode *inode,
117 int type)
2982baa2 118{
d6fdf29f 119 struct posix_acl *sentinel;
b8a7a3a6 120 struct posix_acl **p;
2982baa2
CH
121 struct posix_acl *acl;
122
b8a7a3a6
AG
123 /*
124 * The sentinel is used to detect when another operation like
cac2f8b8 125 * set_cached_acl() or forget_cached_acl() races with get_inode_acl().
b8a7a3a6
AG
126 * It is guaranteed that is_uncached_acl(sentinel) is true.
127 */
128
2982baa2 129 acl = get_cached_acl(inode, type);
b8a7a3a6 130 if (!is_uncached_acl(acl))
2982baa2
CH
131 return acl;
132
133 if (!IS_POSIXACL(inode))
134 return NULL;
135
b8a7a3a6
AG
136 sentinel = uncached_acl_sentinel(current);
137 p = acl_by_type(inode, type);
138
139 /*
140 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
141 * current value of the ACL will not be ACL_NOT_CACHED and so our own
142 * sentinel will not be set; another task will update the cache. We
143 * could wait for that other task to complete its job, but it's easier
cac2f8b8
CB
144 * to just call ->get_inode_acl to fetch the ACL ourself. (This is
145 * going to be an unlikely race.)
b8a7a3a6 146 */
d1cef29a 147 cmpxchg(p, ACL_NOT_CACHED, sentinel);
b8a7a3a6 148
2982baa2 149 /*
4f353ba4 150 * Normally, the ACL returned by ->get{_inode}_acl will be cached.
b8a7a3a6 151 * A filesystem can prevent that by calling
4f353ba4 152 * forget_cached_acl(inode, type) in ->get{_inode}_acl.
2982baa2 153 *
4f353ba4 154 * If the filesystem doesn't have a get{_inode}_ acl() function at all,
cac2f8b8 155 * we'll just create the negative cache entry.
2982baa2 156 */
4f353ba4 157 if (dentry && inode->i_op->get_acl) {
77435322 158 acl = inode->i_op->get_acl(idmap, dentry, type);
4f353ba4
CB
159 } else if (inode->i_op->get_inode_acl) {
160 acl = inode->i_op->get_inode_acl(inode, type, false);
161 } else {
2982baa2
CH
162 set_cached_acl(inode, type, NULL);
163 return NULL;
164 }
b8a7a3a6
AG
165 if (IS_ERR(acl)) {
166 /*
167 * Remove our sentinel so that we don't block future attempts
168 * to cache the ACL.
169 */
170 cmpxchg(p, sentinel, ACL_NOT_CACHED);
171 return acl;
172 }
173
174 /*
175 * Cache the result, but only if our sentinel is still in place.
176 */
177 posix_acl_dup(acl);
4e1da8fe 178 if (unlikely(!try_cmpxchg(p, &sentinel, acl)))
b8a7a3a6
AG
179 posix_acl_release(acl);
180 return acl;
2982baa2 181}
4f353ba4
CB
182
183struct posix_acl *get_inode_acl(struct inode *inode, int type)
184{
77435322 185 return __get_acl(&nop_mnt_idmap, NULL, inode, type);
4f353ba4 186}
cac2f8b8 187EXPORT_SYMBOL(get_inode_acl);
2982baa2 188
f61f6da0
CL
189/*
190 * Init a fresh posix_acl
191 */
192void
193posix_acl_init(struct posix_acl *acl, int count)
194{
66717260 195 refcount_set(&acl->a_refcount, 1);
f61f6da0
CL
196 acl->a_count = count;
197}
0afaa120 198EXPORT_SYMBOL(posix_acl_init);
f61f6da0 199
1da177e4
LT
200/*
201 * Allocate a new ACL with the specified number of entries.
202 */
203struct posix_acl *
dd0fc66f 204posix_acl_alloc(int count, gfp_t flags)
1da177e4
LT
205{
206 const size_t size = sizeof(struct posix_acl) +
207 count * sizeof(struct posix_acl_entry);
208 struct posix_acl *acl = kmalloc(size, flags);
f61f6da0
CL
209 if (acl)
210 posix_acl_init(acl, count);
1da177e4
LT
211 return acl;
212}
0afaa120 213EXPORT_SYMBOL(posix_acl_alloc);
1da177e4
LT
214
215/*
216 * Clone an ACL.
217 */
8043bffd 218struct posix_acl *
dd0fc66f 219posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
1da177e4
LT
220{
221 struct posix_acl *clone = NULL;
222
223 if (acl) {
224 int size = sizeof(struct posix_acl) + acl->a_count *
225 sizeof(struct posix_acl_entry);
52978be6
AD
226 clone = kmemdup(acl, size, flags);
227 if (clone)
66717260 228 refcount_set(&clone->a_refcount, 1);
1da177e4
LT
229 }
230 return clone;
231}
8043bffd 232EXPORT_SYMBOL_GPL(posix_acl_clone);
1da177e4
LT
233
234/*
235 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
236 */
237int
0d4d717f 238posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
1da177e4
LT
239{
240 const struct posix_acl_entry *pa, *pe;
241 int state = ACL_USER_OBJ;
1da177e4
LT
242 int needs_mask = 0;
243
244 FOREACH_ACL_ENTRY(pa, acl, pe) {
245 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
246 return -EINVAL;
247 switch (pa->e_tag) {
248 case ACL_USER_OBJ:
249 if (state == ACL_USER_OBJ) {
1da177e4
LT
250 state = ACL_USER;
251 break;
252 }
253 return -EINVAL;
254
255 case ACL_USER:
256 if (state != ACL_USER)
257 return -EINVAL;
0d4d717f 258 if (!kuid_has_mapping(user_ns, pa->e_uid))
1da177e4 259 return -EINVAL;
1da177e4
LT
260 needs_mask = 1;
261 break;
262
263 case ACL_GROUP_OBJ:
264 if (state == ACL_USER) {
1da177e4
LT
265 state = ACL_GROUP;
266 break;
267 }
268 return -EINVAL;
269
270 case ACL_GROUP:
271 if (state != ACL_GROUP)
272 return -EINVAL;
0d4d717f 273 if (!kgid_has_mapping(user_ns, pa->e_gid))
2f6f0654 274 return -EINVAL;
1da177e4
LT
275 needs_mask = 1;
276 break;
277
278 case ACL_MASK:
279 if (state != ACL_GROUP)
280 return -EINVAL;
281 state = ACL_OTHER;
282 break;
283
284 case ACL_OTHER:
285 if (state == ACL_OTHER ||
286 (state == ACL_GROUP && !needs_mask)) {
287 state = 0;
288 break;
289 }
290 return -EINVAL;
291
292 default:
293 return -EINVAL;
294 }
295 }
296 if (state == 0)
297 return 0;
298 return -EINVAL;
299}
0afaa120 300EXPORT_SYMBOL(posix_acl_valid);
1da177e4
LT
301
302/*
303 * Returns 0 if the acl can be exactly represented in the traditional
304 * file mode permission bits, or else 1. Returns -E... on error.
305 */
306int
d6952123 307posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
1da177e4
LT
308{
309 const struct posix_acl_entry *pa, *pe;
d6952123 310 umode_t mode = 0;
1da177e4
LT
311 int not_equiv = 0;
312
50c6e282
CH
313 /*
314 * A null ACL can always be presented as mode bits.
315 */
316 if (!acl)
317 return 0;
318
1da177e4
LT
319 FOREACH_ACL_ENTRY(pa, acl, pe) {
320 switch (pa->e_tag) {
321 case ACL_USER_OBJ:
322 mode |= (pa->e_perm & S_IRWXO) << 6;
323 break;
324 case ACL_GROUP_OBJ:
325 mode |= (pa->e_perm & S_IRWXO) << 3;
326 break;
327 case ACL_OTHER:
328 mode |= pa->e_perm & S_IRWXO;
329 break;
330 case ACL_MASK:
331 mode = (mode & ~S_IRWXG) |
332 ((pa->e_perm & S_IRWXO) << 3);
333 not_equiv = 1;
334 break;
335 case ACL_USER:
336 case ACL_GROUP:
337 not_equiv = 1;
338 break;
339 default:
340 return -EINVAL;
341 }
342 }
343 if (mode_p)
344 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
345 return not_equiv;
346}
0afaa120 347EXPORT_SYMBOL(posix_acl_equiv_mode);
1da177e4
LT
348
349/*
350 * Create an ACL representing the file mode permission bits of an inode.
351 */
352struct posix_acl *
3a5fba19 353posix_acl_from_mode(umode_t mode, gfp_t flags)
1da177e4
LT
354{
355 struct posix_acl *acl = posix_acl_alloc(3, flags);
356 if (!acl)
357 return ERR_PTR(-ENOMEM);
358
359 acl->a_entries[0].e_tag = ACL_USER_OBJ;
1da177e4
LT
360 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
361
362 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
1da177e4
LT
363 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
364
365 acl->a_entries[2].e_tag = ACL_OTHER;
1da177e4
LT
366 acl->a_entries[2].e_perm = (mode & S_IRWXO);
367 return acl;
368}
0afaa120 369EXPORT_SYMBOL(posix_acl_from_mode);
1da177e4
LT
370
371/*
372 * Return 0 if current is granted want access to the inode
373 * by the acl. Returns -E... otherwise.
374 */
375int
700b7940 376posix_acl_permission(struct mnt_idmap *idmap, struct inode *inode,
47291baa 377 const struct posix_acl *acl, int want)
1da177e4
LT
378{
379 const struct posix_acl_entry *pa, *pe, *mask_obj;
abfcf55d 380 struct user_namespace *fs_userns = i_user_ns(inode);
1da177e4 381 int found = 0;
e933c15f
CB
382 vfsuid_t vfsuid;
383 vfsgid_t vfsgid;
1da177e4 384
63d72b93 385 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
d124b60a 386
1da177e4
LT
387 FOREACH_ACL_ENTRY(pa, acl, pe) {
388 switch(pa->e_tag) {
389 case ACL_USER_OBJ:
390 /* (May have been checked already) */
e67fe633 391 vfsuid = i_uid_into_vfsuid(idmap, inode);
e933c15f 392 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
1da177e4
LT
393 goto check_perm;
394 break;
395 case ACL_USER:
4d7ca409 396 vfsuid = make_vfsuid(idmap, fs_userns,
bd303368 397 pa->e_uid);
e933c15f 398 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
1da177e4
LT
399 goto mask;
400 break;
401 case ACL_GROUP_OBJ:
e67fe633 402 vfsgid = i_gid_into_vfsgid(idmap, inode);
e933c15f 403 if (vfsgid_in_group_p(vfsgid)) {
1da177e4
LT
404 found = 1;
405 if ((pa->e_perm & want) == want)
406 goto mask;
407 }
408 break;
409 case ACL_GROUP:
4d7ca409 410 vfsgid = make_vfsgid(idmap, fs_userns,
bd303368 411 pa->e_gid);
e933c15f 412 if (vfsgid_in_group_p(vfsgid)) {
1da177e4
LT
413 found = 1;
414 if ((pa->e_perm & want) == want)
415 goto mask;
416 }
417 break;
418 case ACL_MASK:
419 break;
420 case ACL_OTHER:
421 if (found)
422 return -EACCES;
423 else
424 goto check_perm;
425 default:
426 return -EIO;
427 }
428 }
429 return -EIO;
430
431mask:
432 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
433 if (mask_obj->e_tag == ACL_MASK) {
434 if ((pa->e_perm & mask_obj->e_perm & want) == want)
435 return 0;
436 return -EACCES;
437 }
438 }
439
440check_perm:
441 if ((pa->e_perm & want) == want)
442 return 0;
443 return -EACCES;
444}
445
446/*
447 * Modify acl when creating a new inode. The caller must ensure the acl is
448 * only referenced once.
449 *
450 * mode_p initially must contain the mode parameter to the open() / creat()
451 * system calls. All permissions that are not granted by the acl are removed.
452 * The permissions in the acl are changed to reflect the mode_p parameter.
453 */
d3fb6120 454static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
1da177e4
LT
455{
456 struct posix_acl_entry *pa, *pe;
457 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
d3fb6120 458 umode_t mode = *mode_p;
1da177e4
LT
459 int not_equiv = 0;
460
461 /* assert(atomic_read(acl->a_refcount) == 1); */
462
463 FOREACH_ACL_ENTRY(pa, acl, pe) {
464 switch(pa->e_tag) {
465 case ACL_USER_OBJ:
466 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
467 mode &= (pa->e_perm << 6) | ~S_IRWXU;
468 break;
469
470 case ACL_USER:
471 case ACL_GROUP:
472 not_equiv = 1;
473 break;
474
475 case ACL_GROUP_OBJ:
476 group_obj = pa;
477 break;
478
479 case ACL_OTHER:
480 pa->e_perm &= mode | ~S_IRWXO;
481 mode &= pa->e_perm | ~S_IRWXO;
482 break;
483
484 case ACL_MASK:
485 mask_obj = pa;
486 not_equiv = 1;
487 break;
488
489 default:
490 return -EIO;
491 }
492 }
493
494 if (mask_obj) {
495 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
496 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
497 } else {
498 if (!group_obj)
499 return -EIO;
500 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
501 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
502 }
503
504 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
505 return not_equiv;
506}
507
508/*
509 * Modify the ACL for the chmod syscall.
510 */
5bf3258f 511static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
1da177e4
LT
512{
513 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
514 struct posix_acl_entry *pa, *pe;
515
516 /* assert(atomic_read(acl->a_refcount) == 1); */
517
518 FOREACH_ACL_ENTRY(pa, acl, pe) {
519 switch(pa->e_tag) {
520 case ACL_USER_OBJ:
521 pa->e_perm = (mode & S_IRWXU) >> 6;
522 break;
523
524 case ACL_USER:
525 case ACL_GROUP:
526 break;
527
528 case ACL_GROUP_OBJ:
529 group_obj = pa;
530 break;
531
532 case ACL_MASK:
533 mask_obj = pa;
534 break;
535
536 case ACL_OTHER:
537 pa->e_perm = (mode & S_IRWXO);
538 break;
539
540 default:
541 return -EIO;
542 }
543 }
544
545 if (mask_obj) {
546 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
547 } else {
548 if (!group_obj)
549 return -EIO;
550 group_obj->e_perm = (mode & S_IRWXG) >> 3;
551 }
552
553 return 0;
554}
bc26ab5f 555
826cae2f 556int
37bc1539 557__posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
826cae2f
AV
558{
559 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
560 int err = -ENOMEM;
561 if (clone) {
562 err = posix_acl_create_masq(clone, mode_p);
563 if (err < 0) {
564 posix_acl_release(clone);
565 clone = NULL;
566 }
567 }
568 posix_acl_release(*acl);
569 *acl = clone;
570 return err;
571}
37bc1539 572EXPORT_SYMBOL(__posix_acl_create);
826cae2f 573
bc26ab5f 574int
5bf3258f 575__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
bc26ab5f
AV
576{
577 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
578 int err = -ENOMEM;
579 if (clone) {
5bf3258f 580 err = __posix_acl_chmod_masq(clone, mode);
bc26ab5f
AV
581 if (err) {
582 posix_acl_release(clone);
583 clone = NULL;
584 }
585 }
586 posix_acl_release(*acl);
587 *acl = clone;
588 return err;
589}
5bf3258f
CH
590EXPORT_SYMBOL(__posix_acl_chmod);
591
e65ce2a5
CB
592/**
593 * posix_acl_chmod - chmod a posix acl
594 *
13e83a49 595 * @idmap: idmap of the mount @inode was found from
138060ba 596 * @dentry: dentry to check permissions on
e65ce2a5
CB
597 * @mode: the new mode of @inode
598 *
13e83a49
CB
599 * If the dentry has been found through an idmapped mount the idmap of
600 * the vfsmount must be passed through @idmap. This function will then
601 * take care to map the inode according to @idmap before checking
e65ce2a5 602 * permissions. On non-idmapped mounts or if permission checking is to be
13e83a49 603 * performed on the raw inode simply passs @nop_mnt_idmap.
e65ce2a5 604 */
5bf3258f 605int
13e83a49 606 posix_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry,
e65ce2a5 607 umode_t mode)
5bf3258f 608{
138060ba 609 struct inode *inode = d_inode(dentry);
5bf3258f
CH
610 struct posix_acl *acl;
611 int ret = 0;
612
613 if (!IS_POSIXACL(inode))
614 return 0;
615 if (!inode->i_op->set_acl)
616 return -EOPNOTSUPP;
617
cac2f8b8 618 acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
789b663a
TM
619 if (IS_ERR_OR_NULL(acl)) {
620 if (acl == ERR_PTR(-EOPNOTSUPP))
621 return 0;
5bf3258f 622 return PTR_ERR(acl);
789b663a 623 }
5bf3258f 624
37bc1539 625 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
5bf3258f
CH
626 if (ret)
627 return ret;
13e83a49 628 ret = inode->i_op->set_acl(idmap, dentry, acl, ACL_TYPE_ACCESS);
5bf3258f
CH
629 posix_acl_release(acl);
630 return ret;
631}
bc26ab5f 632EXPORT_SYMBOL(posix_acl_chmod);
5c8ebd57 633
37bc1539
CH
634int
635posix_acl_create(struct inode *dir, umode_t *mode,
636 struct posix_acl **default_acl, struct posix_acl **acl)
637{
638 struct posix_acl *p;
c0c3a718 639 struct posix_acl *clone;
37bc1539
CH
640 int ret;
641
c0c3a718
DC
642 *acl = NULL;
643 *default_acl = NULL;
644
37bc1539 645 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
c0c3a718 646 return 0;
37bc1539 647
cac2f8b8 648 p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
c0c3a718
DC
649 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
650 *mode &= ~current_umask();
651 return 0;
37bc1539 652 }
c0c3a718
DC
653 if (IS_ERR(p))
654 return PTR_ERR(p);
37bc1539 655
beaf226b 656 ret = -ENOMEM;
c0c3a718
DC
657 clone = posix_acl_clone(p, GFP_NOFS);
658 if (!clone)
beaf226b 659 goto err_release;
37bc1539 660
c0c3a718 661 ret = posix_acl_create_masq(clone, mode);
fed0b588 662 if (ret < 0)
beaf226b 663 goto err_release_clone;
37bc1539 664
c0c3a718
DC
665 if (ret == 0)
666 posix_acl_release(clone);
667 else
668 *acl = clone;
37bc1539 669
c0c3a718 670 if (!S_ISDIR(*mode))
37bc1539 671 posix_acl_release(p);
c0c3a718 672 else
37bc1539 673 *default_acl = p;
37bc1539 674
37bc1539 675 return 0;
fed0b588 676
beaf226b 677err_release_clone:
c0c3a718 678 posix_acl_release(clone);
beaf226b 679err_release:
fed0b588 680 posix_acl_release(p);
beaf226b 681 return ret;
37bc1539
CH
682}
683EXPORT_SYMBOL_GPL(posix_acl_create);
684
07393101
JK
685/**
686 * posix_acl_update_mode - update mode in set_acl
700b7940 687 * @idmap: idmap of the mount @inode was found from
e65ce2a5
CB
688 * @inode: target inode
689 * @mode_p: mode (pointer) for update
690 * @acl: acl pointer
07393101
JK
691 *
692 * Update the file mode when setting an ACL: compute the new file permission
693 * bits based on the ACL. In addition, if the ACL is equivalent to the new
e39e773a 694 * file mode, set *@acl to NULL to indicate that no ACL should be set.
07393101 695 *
e39e773a 696 * As with chmod, clear the setgid bit if the caller is not in the owning group
07393101
JK
697 * or capable of CAP_FSETID (see inode_change_ok).
698 *
700b7940
CB
699 * If the inode has been found through an idmapped mount the idmap of
700 * the vfsmount must be passed through @idmap. This function will then
701 * take care to map the inode according to @idmap before checking
e65ce2a5 702 * permissions. On non-idmapped mounts or if permission checking is to be
700b7940 703 * performed on the raw inode simply passs @nop_mnt_idmap.
e65ce2a5 704 *
07393101
JK
705 * Called from set_acl inode operations.
706 */
700b7940 707int posix_acl_update_mode(struct mnt_idmap *idmap,
e65ce2a5 708 struct inode *inode, umode_t *mode_p,
07393101
JK
709 struct posix_acl **acl)
710{
711 umode_t mode = inode->i_mode;
712 int error;
713
714 error = posix_acl_equiv_mode(*acl, &mode);
715 if (error < 0)
716 return error;
717 if (error == 0)
718 *acl = NULL;
e67fe633 719 if (!vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode)) &&
9452e93e 720 !capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
07393101
JK
721 mode &= ~S_ISGID;
722 *mode_p = mode;
723 return 0;
724}
725EXPORT_SYMBOL(posix_acl_update_mode);
726
5c8ebd57
CH
727/*
728 * Fix up the uids and gids in posix acl extended attributes in place.
729 */
985a6d0b 730static int posix_acl_fix_xattr_common(const void *value, size_t size)
0c5fd887 731{
985a6d0b 732 const struct posix_acl_xattr_header *header = value;
0c5fd887
CB
733 int count;
734
735 if (!header)
736 return -EINVAL;
737 if (size < sizeof(struct posix_acl_xattr_header))
738 return -EINVAL;
739 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
985a6d0b 740 return -EOPNOTSUPP;
0c5fd887
CB
741
742 count = posix_acl_xattr_count(size);
743 if (count < 0)
744 return -EINVAL;
745 if (count == 0)
985a6d0b 746 return 0;
0c5fd887
CB
747
748 return count;
749}
750
6b70fe06 751/**
0a26bde2
CB
752 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
753 * @userns: the filesystem's idmapping
6b70fe06
CB
754 * @value: the uapi representation of POSIX ACLs
755 * @size: the size of @void
6b70fe06 756 *
0a26bde2
CB
757 * Filesystems that store POSIX ACLs in the unaltered uapi format should use
758 * posix_acl_from_xattr() when reading them from the backing store and
759 * converting them into the struct posix_acl VFS format. The helper is
760 * specifically intended to be called from the acl inode operation.
761 *
762 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
763 * in ACL_{GROUP,USER} entries into idmapping in @userns.
764 *
765 * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
766 * If it did it calling it from the get acl inode operation would return POSIX
767 * ACLs mapped according to an idmapped mount which would mean that the value
768 * couldn't be cached for the filesystem. Idmapped mounts are taken into
769 * account on the fly during permission checking or right at the VFS -
770 * userspace boundary before reporting them to the user.
6b70fe06
CB
771 *
772 * Return: Allocated struct posix_acl on success, NULL for a valid header but
773 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
5c8ebd57 774 */
0a26bde2
CB
775struct posix_acl *posix_acl_from_xattr(struct user_namespace *userns,
776 const void *value, size_t size)
5c8ebd57 777{
2211d5ba
AG
778 const struct posix_acl_xattr_header *header = value;
779 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
5c8ebd57
CH
780 int count;
781 struct posix_acl *acl;
782 struct posix_acl_entry *acl_e;
783
985a6d0b 784 count = posix_acl_fix_xattr_common(value, size);
5c8ebd57 785 if (count < 0)
985a6d0b 786 return ERR_PTR(count);
5c8ebd57
CH
787 if (count == 0)
788 return NULL;
789
790 acl = posix_acl_alloc(count, GFP_NOFS);
791 if (!acl)
792 return ERR_PTR(-ENOMEM);
793 acl_e = acl->a_entries;
794
795 for (end = entry + count; entry != end; acl_e++, entry++) {
796 acl_e->e_tag = le16_to_cpu(entry->e_tag);
797 acl_e->e_perm = le16_to_cpu(entry->e_perm);
798
799 switch(acl_e->e_tag) {
800 case ACL_USER_OBJ:
801 case ACL_GROUP_OBJ:
802 case ACL_MASK:
803 case ACL_OTHER:
804 break;
805
806 case ACL_USER:
0a26bde2
CB
807 acl_e->e_uid = make_kuid(userns,
808 le32_to_cpu(entry->e_id));
5c8ebd57
CH
809 if (!uid_valid(acl_e->e_uid))
810 goto fail;
811 break;
812 case ACL_GROUP:
0a26bde2
CB
813 acl_e->e_gid = make_kgid(userns,
814 le32_to_cpu(entry->e_id));
5c8ebd57
CH
815 if (!gid_valid(acl_e->e_gid))
816 goto fail;
817 break;
818
819 default:
820 goto fail;
821 }
822 }
823 return acl;
824
825fail:
826 posix_acl_release(acl);
827 return ERR_PTR(-EINVAL);
828}
829EXPORT_SYMBOL (posix_acl_from_xattr);
830
831/*
832 * Convert from in-memory to extended attribute representation.
833 */
834int
835posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
836 void *buffer, size_t size)
837{
2211d5ba
AG
838 struct posix_acl_xattr_header *ext_acl = buffer;
839 struct posix_acl_xattr_entry *ext_entry;
5c8ebd57
CH
840 int real_size, n;
841
842 real_size = posix_acl_xattr_size(acl->a_count);
843 if (!buffer)
844 return real_size;
845 if (real_size > size)
846 return -ERANGE;
47ba9734 847
2211d5ba 848 ext_entry = (void *)(ext_acl + 1);
5c8ebd57
CH
849 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
850
851 for (n=0; n < acl->a_count; n++, ext_entry++) {
852 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
853 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
854 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
855 switch(acl_e->e_tag) {
856 case ACL_USER:
857 ext_entry->e_id =
858 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
859 break;
860 case ACL_GROUP:
861 ext_entry->e_id =
862 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
863 break;
864 default:
865 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
866 break;
867 }
868 }
869 return real_size;
870}
871EXPORT_SYMBOL (posix_acl_to_xattr);
2aeccbe9 872
4f353ba4
CB
873/**
874 * vfs_posix_acl_to_xattr - convert from kernel to userspace representation
5a6f52d2 875 * @idmap: idmap of the mount
4f353ba4
CB
876 * @inode: inode the posix acls are set on
877 * @acl: the posix acls as represented by the vfs
878 * @buffer: the buffer into which to convert @acl
879 * @size: size of @buffer
880 *
881 * This converts @acl from the VFS representation in the filesystem idmapping
882 * to the uapi form reportable to userspace. And mount and caller idmappings
883 * are handled appropriately.
884 *
885 * Return: On success, the size of the stored uapi posix acls, on error a
886 * negative errno.
887 */
5a6f52d2 888static ssize_t vfs_posix_acl_to_xattr(struct mnt_idmap *idmap,
a351b1f4
CB
889 struct inode *inode,
890 const struct posix_acl *acl, void *buffer,
891 size_t size)
4f353ba4
CB
892
893{
894 struct posix_acl_xattr_header *ext_acl = buffer;
895 struct posix_acl_xattr_entry *ext_entry;
896 struct user_namespace *fs_userns, *caller_userns;
897 ssize_t real_size, n;
898 vfsuid_t vfsuid;
899 vfsgid_t vfsgid;
900
901 real_size = posix_acl_xattr_size(acl->a_count);
902 if (!buffer)
903 return real_size;
904 if (real_size > size)
905 return -ERANGE;
906
907 ext_entry = (void *)(ext_acl + 1);
908 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
909
910 fs_userns = i_user_ns(inode);
911 caller_userns = current_user_ns();
912 for (n=0; n < acl->a_count; n++, ext_entry++) {
913 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
914 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
915 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
916 switch(acl_e->e_tag) {
917 case ACL_USER:
4d7ca409 918 vfsuid = make_vfsuid(idmap, fs_userns, acl_e->e_uid);
4f353ba4
CB
919 ext_entry->e_id = cpu_to_le32(from_kuid(
920 caller_userns, vfsuid_into_kuid(vfsuid)));
921 break;
922 case ACL_GROUP:
4d7ca409 923 vfsgid = make_vfsgid(idmap, fs_userns, acl_e->e_gid);
4f353ba4
CB
924 ext_entry->e_id = cpu_to_le32(from_kgid(
925 caller_userns, vfsgid_into_kgid(vfsgid)));
926 break;
927 default:
928 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
929 break;
930 }
931 }
932 return real_size;
933}
934
485e71e8 935int
13e83a49 936set_posix_acl(struct mnt_idmap *idmap, struct dentry *dentry,
e65ce2a5 937 int type, struct posix_acl *acl)
2aeccbe9 938{
138060ba
CB
939 struct inode *inode = d_inode(dentry);
940
2aeccbe9
CH
941 if (!IS_POSIXACL(inode))
942 return -EOPNOTSUPP;
943 if (!inode->i_op->set_acl)
944 return -EOPNOTSUPP;
945
485e71e8
AG
946 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
947 return acl ? -EACCES : 0;
01beba79 948 if (!inode_owner_or_capable(idmap, inode))
2aeccbe9
CH
949 return -EPERM;
950
485e71e8 951 if (acl) {
a867d734 952 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
485e71e8
AG
953 if (ret)
954 return ret;
955 }
13e83a49 956 return inode->i_op->set_acl(idmap, dentry, acl, type);
485e71e8
AG
957}
958EXPORT_SYMBOL(set_posix_acl);
959
f2620f16
CB
960int posix_acl_listxattr(struct inode *inode, char **buffer,
961 ssize_t *remaining_size)
962{
963 int err;
964
965 if (!IS_POSIXACL(inode))
966 return 0;
967
968 if (inode->i_acl) {
969 err = xattr_list_one(buffer, remaining_size,
970 XATTR_NAME_POSIX_ACL_ACCESS);
971 if (err)
972 return err;
973 }
974
975 if (inode->i_default_acl) {
976 err = xattr_list_one(buffer, remaining_size,
977 XATTR_NAME_POSIX_ACL_DEFAULT);
978 if (err)
979 return err;
980 }
981
982 return 0;
983}
984
764a5c6b
AG
985static bool
986posix_acl_xattr_list(struct dentry *dentry)
2aeccbe9 987{
764a5c6b 988 return IS_POSIXACL(d_backing_inode(dentry));
2aeccbe9
CH
989}
990
d549b741
CB
991/*
992 * nop_posix_acl_access - legacy xattr handler for access POSIX ACLs
993 *
994 * This is the legacy POSIX ACL access xattr handler. It is used by some
995 * filesystems to implement their ->listxattr() inode operation. New code
996 * should never use them.
997 */
998const struct xattr_handler nop_posix_acl_access = {
98e9cb57 999 .name = XATTR_NAME_POSIX_ACL_ACCESS,
2aeccbe9 1000 .list = posix_acl_xattr_list,
2aeccbe9 1001};
d549b741 1002EXPORT_SYMBOL_GPL(nop_posix_acl_access);
2aeccbe9 1003
d549b741
CB
1004/*
1005 * nop_posix_acl_default - legacy xattr handler for default POSIX ACLs
1006 *
1007 * This is the legacy POSIX ACL default xattr handler. It is used by some
1008 * filesystems to implement their ->listxattr() inode operation. New code
1009 * should never use them.
1010 */
1011const struct xattr_handler nop_posix_acl_default = {
98e9cb57 1012 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
2aeccbe9 1013 .list = posix_acl_xattr_list,
2aeccbe9 1014};
d549b741 1015EXPORT_SYMBOL_GPL(nop_posix_acl_default);
feda821e 1016
13e83a49 1017int simple_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
549c7297 1018 struct posix_acl *acl, int type)
feda821e
CH
1019{
1020 int error;
138060ba 1021 struct inode *inode = d_inode(dentry);
feda821e
CH
1022
1023 if (type == ACL_TYPE_ACCESS) {
700b7940 1024 error = posix_acl_update_mode(idmap, inode,
497de07d
GZ
1025 &inode->i_mode, &acl);
1026 if (error)
1027 return error;
feda821e
CH
1028 }
1029
2276e5ba 1030 inode_set_ctime_current(inode);
36f05cab
JL
1031 if (IS_I_VERSION(inode))
1032 inode_inc_iversion(inode);
feda821e
CH
1033 set_cached_acl(inode, type, acl);
1034 return 0;
1035}
1036
1037int simple_acl_create(struct inode *dir, struct inode *inode)
1038{
1039 struct posix_acl *default_acl, *acl;
1040 int error;
1041
1042 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1043 if (error)
1044 return error;
1045
1046 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1047 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1048
1049 if (default_acl)
1050 posix_acl_release(default_acl);
1051 if (acl)
1052 posix_acl_release(acl);
1053 return 0;
1054}
e4cc9163 1055
700b7940 1056static int vfs_set_acl_idmapped_mnt(struct mnt_idmap *idmap,
e4cc9163
CB
1057 struct user_namespace *fs_userns,
1058 struct posix_acl *acl)
1059{
1060 for (int n = 0; n < acl->a_count; n++) {
1061 struct posix_acl_entry *acl_e = &acl->a_entries[n];
1062
1063 switch (acl_e->e_tag) {
1064 case ACL_USER:
4d7ca409 1065 acl_e->e_uid = from_vfsuid(idmap, fs_userns,
e4cc9163
CB
1066 VFSUIDT_INIT(acl_e->e_uid));
1067 break;
1068 case ACL_GROUP:
4d7ca409 1069 acl_e->e_gid = from_vfsgid(idmap, fs_userns,
e4cc9163
CB
1070 VFSGIDT_INIT(acl_e->e_gid));
1071 break;
1072 }
1073 }
1074
1075 return 0;
1076}
1077
1078/**
1079 * vfs_set_acl - set posix acls
13e83a49 1080 * @idmap: idmap of the mount
e4cc9163
CB
1081 * @dentry: the dentry based on which to set the posix acls
1082 * @acl_name: the name of the posix acl
1083 * @kacl: the posix acls in the appropriate VFS format
1084 *
1085 * This function sets @kacl. The caller must all posix_acl_release() on @kacl
1086 * afterwards.
1087 *
1088 * Return: On success 0, on error negative errno.
1089 */
13e83a49 1090int vfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
e4cc9163
CB
1091 const char *acl_name, struct posix_acl *kacl)
1092{
1093 int acl_type;
1094 int error;
1095 struct inode *inode = d_inode(dentry);
1096 struct inode *delegated_inode = NULL;
1097
1098 acl_type = posix_acl_type(acl_name);
1099 if (acl_type < 0)
1100 return -EINVAL;
1101
1102 if (kacl) {
1103 /*
1104 * If we're on an idmapped mount translate from mount specific
1105 * vfs{g,u}id_t into global filesystem k{g,u}id_t.
1106 * Afterwards we can cache the POSIX ACLs filesystem wide and -
1107 * if this is a filesystem with a backing store - ultimately
1108 * translate them to backing store values.
1109 */
700b7940 1110 error = vfs_set_acl_idmapped_mnt(idmap, i_user_ns(inode), kacl);
e4cc9163
CB
1111 if (error)
1112 return error;
1113 }
1114
1115retry_deleg:
1116 inode_lock(inode);
1117
1118 /*
1119 * We only care about restrictions the inode struct itself places upon
1120 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1121 */
4609e1f1 1122 error = may_write_xattr(idmap, inode);
e4cc9163
CB
1123 if (error)
1124 goto out_inode_unlock;
1125
700b7940 1126 error = security_inode_set_acl(idmap, dentry, acl_name, kacl);
e4cc9163
CB
1127 if (error)
1128 goto out_inode_unlock;
1129
1130 error = try_break_deleg(inode, &delegated_inode);
1131 if (error)
1132 goto out_inode_unlock;
1133
e499214c 1134 if (likely(!is_bad_inode(inode)))
13e83a49 1135 error = set_posix_acl(idmap, dentry, acl_type, kacl);
e4cc9163 1136 else
e499214c 1137 error = -EIO;
e4cc9163
CB
1138 if (!error) {
1139 fsnotify_xattr(dentry);
1140 evm_inode_post_set_acl(dentry, acl_name, kacl);
1141 }
1142
1143out_inode_unlock:
1144 inode_unlock(inode);
1145
1146 if (delegated_inode) {
1147 error = break_deleg_wait(&delegated_inode);
1148 if (!error)
1149 goto retry_deleg;
1150 }
1151
1152 return error;
1153}
1154EXPORT_SYMBOL_GPL(vfs_set_acl);
4f353ba4
CB
1155
1156/**
1157 * vfs_get_acl - get posix acls
77435322 1158 * @idmap: idmap of the mount
4f353ba4
CB
1159 * @dentry: the dentry based on which to retrieve the posix acls
1160 * @acl_name: the name of the posix acl
1161 *
1162 * This function retrieves @kacl from the filesystem. The caller must all
1163 * posix_acl_release() on @kacl.
1164 *
1165 * Return: On success POSIX ACLs in VFS format, on error negative errno.
1166 */
77435322 1167struct posix_acl *vfs_get_acl(struct mnt_idmap *idmap,
4f353ba4
CB
1168 struct dentry *dentry, const char *acl_name)
1169{
1170 struct inode *inode = d_inode(dentry);
1171 struct posix_acl *acl;
1172 int acl_type, error;
1173
1174 acl_type = posix_acl_type(acl_name);
1175 if (acl_type < 0)
1176 return ERR_PTR(-EINVAL);
1177
1178 /*
1179 * The VFS has no restrictions on reading POSIX ACLs so calling
1180 * something like xattr_permission() isn't needed. Only LSMs get a say.
1181 */
700b7940 1182 error = security_inode_get_acl(idmap, dentry, acl_name);
4f353ba4
CB
1183 if (error)
1184 return ERR_PTR(error);
1185
1186 if (!IS_POSIXACL(inode))
1187 return ERR_PTR(-EOPNOTSUPP);
1188 if (S_ISLNK(inode->i_mode))
1189 return ERR_PTR(-EOPNOTSUPP);
1190
77435322 1191 acl = __get_acl(idmap, dentry, inode, acl_type);
4f353ba4
CB
1192 if (IS_ERR(acl))
1193 return acl;
1194 if (!acl)
1195 return ERR_PTR(-ENODATA);
1196
1197 return acl;
1198}
1199EXPORT_SYMBOL_GPL(vfs_get_acl);
aeb7f005
CB
1200
1201/**
1202 * vfs_remove_acl - remove posix acls
13e83a49 1203 * @idmap: idmap of the mount
aeb7f005
CB
1204 * @dentry: the dentry based on which to retrieve the posix acls
1205 * @acl_name: the name of the posix acl
1206 *
1207 * This function removes posix acls.
1208 *
1209 * Return: On success 0, on error negative errno.
1210 */
13e83a49 1211int vfs_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
aeb7f005
CB
1212 const char *acl_name)
1213{
1214 int acl_type;
1215 int error;
1216 struct inode *inode = d_inode(dentry);
1217 struct inode *delegated_inode = NULL;
1218
1219 acl_type = posix_acl_type(acl_name);
1220 if (acl_type < 0)
1221 return -EINVAL;
1222
1223retry_deleg:
1224 inode_lock(inode);
1225
1226 /*
1227 * We only care about restrictions the inode struct itself places upon
1228 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1229 */
4609e1f1 1230 error = may_write_xattr(idmap, inode);
aeb7f005
CB
1231 if (error)
1232 goto out_inode_unlock;
1233
700b7940 1234 error = security_inode_remove_acl(idmap, dentry, acl_name);
aeb7f005
CB
1235 if (error)
1236 goto out_inode_unlock;
1237
1238 error = try_break_deleg(inode, &delegated_inode);
1239 if (error)
1240 goto out_inode_unlock;
1241
e499214c 1242 if (likely(!is_bad_inode(inode)))
13e83a49 1243 error = set_posix_acl(idmap, dentry, acl_type, NULL);
aeb7f005 1244 else
e499214c 1245 error = -EIO;
aeb7f005
CB
1246 if (!error) {
1247 fsnotify_xattr(dentry);
700b7940 1248 evm_inode_post_remove_acl(idmap, dentry, acl_name);
aeb7f005
CB
1249 }
1250
1251out_inode_unlock:
1252 inode_unlock(inode);
1253
1254 if (delegated_inode) {
1255 error = break_deleg_wait(&delegated_inode);
1256 if (!error)
1257 goto retry_deleg;
1258 }
1259
1260 return error;
1261}
1262EXPORT_SYMBOL_GPL(vfs_remove_acl);
318e6685 1263
5a6f52d2 1264int do_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
318e6685
CB
1265 const char *acl_name, const void *kvalue, size_t size)
1266{
1267 int error;
1268 struct posix_acl *acl = NULL;
1269
1270 if (size) {
1271 /*
1272 * Note that posix_acl_from_xattr() uses GFP_NOFS when it
1273 * probably doesn't need to here.
1274 */
1275 acl = posix_acl_from_xattr(current_user_ns(), kvalue, size);
1276 if (IS_ERR(acl))
1277 return PTR_ERR(acl);
1278 }
1279
13e83a49 1280 error = vfs_set_acl(idmap, dentry, acl_name, acl);
318e6685
CB
1281 posix_acl_release(acl);
1282 return error;
1283}
1284
5a6f52d2 1285ssize_t do_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
318e6685
CB
1286 const char *acl_name, void *kvalue, size_t size)
1287{
1288 ssize_t error;
1289 struct posix_acl *acl;
1290
77435322 1291 acl = vfs_get_acl(idmap, dentry, acl_name);
318e6685
CB
1292 if (IS_ERR(acl))
1293 return PTR_ERR(acl);
1294
5a6f52d2 1295 error = vfs_posix_acl_to_xattr(idmap, d_inode(dentry),
318e6685
CB
1296 acl, kvalue, size);
1297 posix_acl_release(acl);
1298 return error;
1299}