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