blob: 9323a854a60aeb27e7e36db9da4164941a8730c0 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -08003 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -08005 * Fixes from William Schumacher incorporated on 15 March 2001.
6 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
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>
Arun Sharma600634972011-07-26 16:09:06 -070016#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/fs.h>
18#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010019#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/posix_acl.h>
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -080021#include <linux/posix_acl_xattr.h>
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -080022#include <linux/xattr.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050023#include <linux/export.h>
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -080024#include <linux/user_namespace.h>
Miklos Szeredi332f6062021-08-18 22:08:24 +020025#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Andreas Gruenbacher04c57f42016-03-24 14:38:38 +010027static struct posix_acl **acl_by_type(struct inode *inode, int type)
Andrew Morton0afaa122014-01-21 15:48:42 -080028{
29 switch (type) {
30 case ACL_TYPE_ACCESS:
31 return &inode->i_acl;
32 case ACL_TYPE_DEFAULT:
33 return &inode->i_default_acl;
34 default:
35 BUG();
36 }
37}
Andrew Morton0afaa122014-01-21 15:48:42 -080038
39struct posix_acl *get_cached_acl(struct inode *inode, int type)
40{
41 struct posix_acl **p = acl_by_type(inode, type);
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010042 struct posix_acl *acl;
43
44 for (;;) {
45 rcu_read_lock();
46 acl = rcu_dereference(*p);
47 if (!acl || is_uncached_acl(acl) ||
Elena Reshetova66717262017-11-29 13:19:31 +020048 refcount_inc_not_zero(&acl->a_refcount))
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010049 break;
50 rcu_read_unlock();
51 cpu_relax();
Andrew Morton0afaa122014-01-21 15:48:42 -080052 }
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010053 rcu_read_unlock();
Andrew Morton0afaa122014-01-21 15:48:42 -080054 return acl;
55}
56EXPORT_SYMBOL(get_cached_acl);
57
58struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
59{
Miklos Szeredi332f6062021-08-18 22:08:24 +020060 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
61
62 if (acl == ACL_DONT_CACHE) {
63 struct posix_acl *ret;
64
65 ret = inode->i_op->get_acl(inode, type, LOOKUP_RCU);
66 if (!IS_ERR(ret))
67 acl = ret;
68 }
69
70 return acl;
Andrew Morton0afaa122014-01-21 15:48:42 -080071}
72EXPORT_SYMBOL(get_cached_acl_rcu);
73
74void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
75{
76 struct posix_acl **p = acl_by_type(inode, type);
77 struct posix_acl *old;
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010078
79 old = xchg(p, posix_acl_dup(acl));
80 if (!is_uncached_acl(old))
Andrew Morton0afaa122014-01-21 15:48:42 -080081 posix_acl_release(old);
82}
83EXPORT_SYMBOL(set_cached_acl);
84
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010085static void __forget_cached_acl(struct posix_acl **p)
86{
87 struct posix_acl *old;
88
89 old = xchg(p, ACL_NOT_CACHED);
90 if (!is_uncached_acl(old))
91 posix_acl_release(old);
92}
93
Andrew Morton0afaa122014-01-21 15:48:42 -080094void forget_cached_acl(struct inode *inode, int type)
95{
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010096 __forget_cached_acl(acl_by_type(inode, type));
Andrew Morton0afaa122014-01-21 15:48:42 -080097}
98EXPORT_SYMBOL(forget_cached_acl);
99
100void forget_all_cached_acls(struct inode *inode)
101{
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100102 __forget_cached_acl(&inode->i_acl);
103 __forget_cached_acl(&inode->i_default_acl);
Andrew Morton0afaa122014-01-21 15:48:42 -0800104}
105EXPORT_SYMBOL(forget_all_cached_acls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800107struct posix_acl *get_acl(struct inode *inode, int type)
108{
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100109 void *sentinel;
110 struct posix_acl **p;
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800111 struct posix_acl *acl;
112
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100113 /*
114 * The sentinel is used to detect when another operation like
115 * set_cached_acl() or forget_cached_acl() races with get_acl().
116 * It is guaranteed that is_uncached_acl(sentinel) is true.
117 */
118
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800119 acl = get_cached_acl(inode, type);
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100120 if (!is_uncached_acl(acl))
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800121 return acl;
122
123 if (!IS_POSIXACL(inode))
124 return NULL;
125
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100126 sentinel = uncached_acl_sentinel(current);
127 p = acl_by_type(inode, type);
128
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800129 /*
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100130 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
131 * current value of the ACL will not be ACL_NOT_CACHED and so our own
132 * sentinel will not be set; another task will update the cache. We
133 * could wait for that other task to complete its job, but it's easier
134 * to just call ->get_acl to fetch the ACL ourself. (This is going to
135 * be an unlikely race.)
136 */
Arnd Bergmannd1cef292021-11-05 13:35:01 -0700137 cmpxchg(p, ACL_NOT_CACHED, sentinel);
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100138
139 /*
140 * Normally, the ACL returned by ->get_acl will be cached.
141 * A filesystem can prevent that by calling
142 * forget_cached_acl(inode, type) in ->get_acl.
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800143 *
144 * If the filesystem doesn't have a get_acl() function at all, we'll
145 * just create the negative cache entry.
146 */
147 if (!inode->i_op->get_acl) {
148 set_cached_acl(inode, type, NULL);
149 return NULL;
150 }
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200151 acl = inode->i_op->get_acl(inode, type, false);
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100152
153 if (IS_ERR(acl)) {
154 /*
155 * Remove our sentinel so that we don't block future attempts
156 * to cache the ACL.
157 */
158 cmpxchg(p, sentinel, ACL_NOT_CACHED);
159 return acl;
160 }
161
162 /*
163 * Cache the result, but only if our sentinel is still in place.
164 */
165 posix_acl_dup(acl);
166 if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
167 posix_acl_release(acl);
168 return acl;
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800169}
170EXPORT_SYMBOL(get_acl);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
Chuck Leverf61f6da2011-01-21 03:05:38 +0000173 * Init a fresh posix_acl
174 */
175void
176posix_acl_init(struct posix_acl *acl, int count)
177{
Elena Reshetova66717262017-11-29 13:19:31 +0200178 refcount_set(&acl->a_refcount, 1);
Chuck Leverf61f6da2011-01-21 03:05:38 +0000179 acl->a_count = count;
180}
Andrew Morton0afaa122014-01-21 15:48:42 -0800181EXPORT_SYMBOL(posix_acl_init);
Chuck Leverf61f6da2011-01-21 03:05:38 +0000182
183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Allocate a new ACL with the specified number of entries.
185 */
186struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +0100187posix_acl_alloc(int count, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 const size_t size = sizeof(struct posix_acl) +
190 count * sizeof(struct posix_acl_entry);
191 struct posix_acl *acl = kmalloc(size, flags);
Chuck Leverf61f6da2011-01-21 03:05:38 +0000192 if (acl)
193 posix_acl_init(acl, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return acl;
195}
Andrew Morton0afaa122014-01-21 15:48:42 -0800196EXPORT_SYMBOL(posix_acl_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/*
199 * Clone an ACL.
200 */
Al Viroedde8542011-07-23 03:27:37 -0400201static struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +0100202posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct posix_acl *clone = NULL;
205
206 if (acl) {
207 int size = sizeof(struct posix_acl) + acl->a_count *
208 sizeof(struct posix_acl_entry);
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700209 clone = kmemdup(acl, size, flags);
210 if (clone)
Elena Reshetova66717262017-11-29 13:19:31 +0200211 refcount_set(&clone->a_refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 return clone;
214}
215
216/*
217 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
218 */
219int
Eric W. Biederman0d4d7172016-06-27 16:04:06 -0500220posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 const struct posix_acl_entry *pa, *pe;
223 int state = ACL_USER_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int needs_mask = 0;
225
226 FOREACH_ACL_ENTRY(pa, acl, pe) {
227 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
228 return -EINVAL;
229 switch (pa->e_tag) {
230 case ACL_USER_OBJ:
231 if (state == ACL_USER_OBJ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 state = ACL_USER;
233 break;
234 }
235 return -EINVAL;
236
237 case ACL_USER:
238 if (state != ACL_USER)
239 return -EINVAL;
Eric W. Biederman0d4d7172016-06-27 16:04:06 -0500240 if (!kuid_has_mapping(user_ns, pa->e_uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 needs_mask = 1;
243 break;
244
245 case ACL_GROUP_OBJ:
246 if (state == ACL_USER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 state = ACL_GROUP;
248 break;
249 }
250 return -EINVAL;
251
252 case ACL_GROUP:
253 if (state != ACL_GROUP)
254 return -EINVAL;
Eric W. Biederman0d4d7172016-06-27 16:04:06 -0500255 if (!kgid_has_mapping(user_ns, pa->e_gid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 needs_mask = 1;
258 break;
259
260 case ACL_MASK:
261 if (state != ACL_GROUP)
262 return -EINVAL;
263 state = ACL_OTHER;
264 break;
265
266 case ACL_OTHER:
267 if (state == ACL_OTHER ||
268 (state == ACL_GROUP && !needs_mask)) {
269 state = 0;
270 break;
271 }
272 return -EINVAL;
273
274 default:
275 return -EINVAL;
276 }
277 }
278 if (state == 0)
279 return 0;
280 return -EINVAL;
281}
Andrew Morton0afaa122014-01-21 15:48:42 -0800282EXPORT_SYMBOL(posix_acl_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/*
285 * Returns 0 if the acl can be exactly represented in the traditional
286 * file mode permission bits, or else 1. Returns -E... on error.
287 */
288int
Al Virod6952122011-07-23 18:56:36 -0400289posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 const struct posix_acl_entry *pa, *pe;
Al Virod6952122011-07-23 18:56:36 -0400292 umode_t mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int not_equiv = 0;
294
Christoph Hellwig50c6e282014-05-04 13:03:32 +0200295 /*
296 * A null ACL can always be presented as mode bits.
297 */
298 if (!acl)
299 return 0;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 FOREACH_ACL_ENTRY(pa, acl, pe) {
302 switch (pa->e_tag) {
303 case ACL_USER_OBJ:
304 mode |= (pa->e_perm & S_IRWXO) << 6;
305 break;
306 case ACL_GROUP_OBJ:
307 mode |= (pa->e_perm & S_IRWXO) << 3;
308 break;
309 case ACL_OTHER:
310 mode |= pa->e_perm & S_IRWXO;
311 break;
312 case ACL_MASK:
313 mode = (mode & ~S_IRWXG) |
314 ((pa->e_perm & S_IRWXO) << 3);
315 not_equiv = 1;
316 break;
317 case ACL_USER:
318 case ACL_GROUP:
319 not_equiv = 1;
320 break;
321 default:
322 return -EINVAL;
323 }
324 }
325 if (mode_p)
326 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
327 return not_equiv;
328}
Andrew Morton0afaa122014-01-21 15:48:42 -0800329EXPORT_SYMBOL(posix_acl_equiv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331/*
332 * Create an ACL representing the file mode permission bits of an inode.
333 */
334struct posix_acl *
Al Viro3a5fba12011-07-23 19:01:48 -0400335posix_acl_from_mode(umode_t mode, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct posix_acl *acl = posix_acl_alloc(3, flags);
338 if (!acl)
339 return ERR_PTR(-ENOMEM);
340
341 acl->a_entries[0].e_tag = ACL_USER_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
343
344 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
346
347 acl->a_entries[2].e_tag = ACL_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 acl->a_entries[2].e_perm = (mode & S_IRWXO);
349 return acl;
350}
Andrew Morton0afaa122014-01-21 15:48:42 -0800351EXPORT_SYMBOL(posix_acl_from_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353/*
354 * Return 0 if current is granted want access to the inode
355 * by the acl. Returns -E... otherwise.
356 */
357int
Christian Brauner47291ba2021-01-21 14:19:24 +0100358posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode,
359 const struct posix_acl *acl, int want)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 const struct posix_acl_entry *pa, *pe, *mask_obj;
362 int found = 0;
Christian Brauner47291ba2021-01-21 14:19:24 +0100363 kuid_t uid;
364 kgid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds63d72b92020-06-07 12:19:06 -0700366 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
Andreas Gruenbacherd124b602011-10-23 23:13:32 +0530367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 FOREACH_ACL_ENTRY(pa, acl, pe) {
369 switch(pa->e_tag) {
370 case ACL_USER_OBJ:
371 /* (May have been checked already) */
Christian Brauner47291ba2021-01-21 14:19:24 +0100372 uid = i_uid_into_mnt(mnt_userns, inode);
373 if (uid_eq(uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto check_perm;
375 break;
376 case ACL_USER:
Christian Brauner47291ba2021-01-21 14:19:24 +0100377 uid = kuid_into_mnt(mnt_userns, pa->e_uid);
378 if (uid_eq(uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 goto mask;
380 break;
381 case ACL_GROUP_OBJ:
Christian Brauner47291ba2021-01-21 14:19:24 +0100382 gid = i_gid_into_mnt(mnt_userns, inode);
383 if (in_group_p(gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 found = 1;
385 if ((pa->e_perm & want) == want)
386 goto mask;
387 }
388 break;
389 case ACL_GROUP:
Christian Brauner47291ba2021-01-21 14:19:24 +0100390 gid = kgid_into_mnt(mnt_userns, pa->e_gid);
391 if (in_group_p(gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 found = 1;
393 if ((pa->e_perm & want) == want)
394 goto mask;
395 }
396 break;
397 case ACL_MASK:
398 break;
399 case ACL_OTHER:
400 if (found)
401 return -EACCES;
402 else
403 goto check_perm;
404 default:
405 return -EIO;
406 }
407 }
408 return -EIO;
409
410mask:
411 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
412 if (mask_obj->e_tag == ACL_MASK) {
413 if ((pa->e_perm & mask_obj->e_perm & want) == want)
414 return 0;
415 return -EACCES;
416 }
417 }
418
419check_perm:
420 if ((pa->e_perm & want) == want)
421 return 0;
422 return -EACCES;
423}
424
425/*
426 * Modify acl when creating a new inode. The caller must ensure the acl is
427 * only referenced once.
428 *
429 * mode_p initially must contain the mode parameter to the open() / creat()
430 * system calls. All permissions that are not granted by the acl are removed.
431 * The permissions in the acl are changed to reflect the mode_p parameter.
432 */
Al Virod3fb6122011-07-23 18:37:50 -0400433static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct posix_acl_entry *pa, *pe;
436 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
Al Virod3fb6122011-07-23 18:37:50 -0400437 umode_t mode = *mode_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int not_equiv = 0;
439
440 /* assert(atomic_read(acl->a_refcount) == 1); */
441
442 FOREACH_ACL_ENTRY(pa, acl, pe) {
443 switch(pa->e_tag) {
444 case ACL_USER_OBJ:
445 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
446 mode &= (pa->e_perm << 6) | ~S_IRWXU;
447 break;
448
449 case ACL_USER:
450 case ACL_GROUP:
451 not_equiv = 1;
452 break;
453
454 case ACL_GROUP_OBJ:
455 group_obj = pa;
456 break;
457
458 case ACL_OTHER:
459 pa->e_perm &= mode | ~S_IRWXO;
460 mode &= pa->e_perm | ~S_IRWXO;
461 break;
462
463 case ACL_MASK:
464 mask_obj = pa;
465 not_equiv = 1;
466 break;
467
468 default:
469 return -EIO;
470 }
471 }
472
473 if (mask_obj) {
474 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
475 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
476 } else {
477 if (!group_obj)
478 return -EIO;
479 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
480 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
481 }
482
483 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
484 return not_equiv;
485}
486
487/*
488 * Modify the ACL for the chmod syscall.
489 */
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800490static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
493 struct posix_acl_entry *pa, *pe;
494
495 /* assert(atomic_read(acl->a_refcount) == 1); */
496
497 FOREACH_ACL_ENTRY(pa, acl, pe) {
498 switch(pa->e_tag) {
499 case ACL_USER_OBJ:
500 pa->e_perm = (mode & S_IRWXU) >> 6;
501 break;
502
503 case ACL_USER:
504 case ACL_GROUP:
505 break;
506
507 case ACL_GROUP_OBJ:
508 group_obj = pa;
509 break;
510
511 case ACL_MASK:
512 mask_obj = pa;
513 break;
514
515 case ACL_OTHER:
516 pa->e_perm = (mode & S_IRWXO);
517 break;
518
519 default:
520 return -EIO;
521 }
522 }
523
524 if (mask_obj) {
525 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
526 } else {
527 if (!group_obj)
528 return -EIO;
529 group_obj->e_perm = (mode & S_IRWXG) >> 3;
530 }
531
532 return 0;
533}
Al Virobc26ab52011-07-23 00:18:02 -0400534
535int
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800536__posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
Al Viro826cae22011-07-23 03:10:32 -0400537{
538 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
539 int err = -ENOMEM;
540 if (clone) {
541 err = posix_acl_create_masq(clone, mode_p);
542 if (err < 0) {
543 posix_acl_release(clone);
544 clone = NULL;
545 }
546 }
547 posix_acl_release(*acl);
548 *acl = clone;
549 return err;
550}
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800551EXPORT_SYMBOL(__posix_acl_create);
Al Viro826cae22011-07-23 03:10:32 -0400552
553int
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800554__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
Al Virobc26ab52011-07-23 00:18:02 -0400555{
556 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
557 int err = -ENOMEM;
558 if (clone) {
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800559 err = __posix_acl_chmod_masq(clone, mode);
Al Virobc26ab52011-07-23 00:18:02 -0400560 if (err) {
561 posix_acl_release(clone);
562 clone = NULL;
563 }
564 }
565 posix_acl_release(*acl);
566 *acl = clone;
567 return err;
568}
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800569EXPORT_SYMBOL(__posix_acl_chmod);
570
Christian Braunere65ce2a2021-01-21 14:19:27 +0100571/**
572 * posix_acl_chmod - chmod a posix acl
573 *
574 * @mnt_userns: user namespace of the mount @inode was found from
575 * @inode: inode to check permissions on
576 * @mode: the new mode of @inode
577 *
578 * If the inode has been found through an idmapped mount the user namespace of
579 * the vfsmount must be passed through @mnt_userns. This function will then
580 * take care to map the inode according to @mnt_userns before checking
581 * permissions. On non-idmapped mounts or if permission checking is to be
582 * performed on the raw inode simply passs init_user_ns.
583 */
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800584int
Christian Braunere65ce2a2021-01-21 14:19:27 +0100585 posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode,
586 umode_t mode)
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800587{
588 struct posix_acl *acl;
589 int ret = 0;
590
591 if (!IS_POSIXACL(inode))
592 return 0;
593 if (!inode->i_op->set_acl)
594 return -EOPNOTSUPP;
595
596 acl = get_acl(inode, ACL_TYPE_ACCESS);
Trond Myklebust789b6632014-01-31 14:25:19 -0500597 if (IS_ERR_OR_NULL(acl)) {
598 if (acl == ERR_PTR(-EOPNOTSUPP))
599 return 0;
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800600 return PTR_ERR(acl);
Trond Myklebust789b6632014-01-31 14:25:19 -0500601 }
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800602
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800603 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800604 if (ret)
605 return ret;
Christian Brauner549c7292021-01-21 14:19:43 +0100606 ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS);
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800607 posix_acl_release(acl);
608 return ret;
609}
Al Virobc26ab52011-07-23 00:18:02 -0400610EXPORT_SYMBOL(posix_acl_chmod);
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800611
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800612int
613posix_acl_create(struct inode *dir, umode_t *mode,
614 struct posix_acl **default_acl, struct posix_acl **acl)
615{
616 struct posix_acl *p;
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000617 struct posix_acl *clone;
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800618 int ret;
619
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000620 *acl = NULL;
621 *default_acl = NULL;
622
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800623 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000624 return 0;
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800625
626 p = get_acl(dir, ACL_TYPE_DEFAULT);
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000627 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
628 *mode &= ~current_umask();
629 return 0;
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800630 }
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000631 if (IS_ERR(p))
632 return PTR_ERR(p);
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800633
Miklos Szeredibeaf2262016-09-16 12:44:21 +0200634 ret = -ENOMEM;
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000635 clone = posix_acl_clone(p, GFP_NOFS);
636 if (!clone)
Miklos Szeredibeaf2262016-09-16 12:44:21 +0200637 goto err_release;
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800638
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000639 ret = posix_acl_create_masq(clone, mode);
Omar Sandovalfed0b582015-02-08 21:45:25 -0800640 if (ret < 0)
Miklos Szeredibeaf2262016-09-16 12:44:21 +0200641 goto err_release_clone;
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800642
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000643 if (ret == 0)
644 posix_acl_release(clone);
645 else
646 *acl = clone;
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800647
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000648 if (!S_ISDIR(*mode))
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800649 posix_acl_release(p);
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000650 else
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800651 *default_acl = p;
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800652
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800653 return 0;
Omar Sandovalfed0b582015-02-08 21:45:25 -0800654
Miklos Szeredibeaf2262016-09-16 12:44:21 +0200655err_release_clone:
Dan Carpenterc0c3a712015-06-19 09:00:55 +1000656 posix_acl_release(clone);
Miklos Szeredibeaf2262016-09-16 12:44:21 +0200657err_release:
Omar Sandovalfed0b582015-02-08 21:45:25 -0800658 posix_acl_release(p);
Miklos Szeredibeaf2262016-09-16 12:44:21 +0200659 return ret;
Christoph Hellwig37bc1532013-12-20 05:16:42 -0800660}
661EXPORT_SYMBOL_GPL(posix_acl_create);
662
Jan Kara07393102016-09-19 17:39:09 +0200663/**
664 * posix_acl_update_mode - update mode in set_acl
Christian Braunere65ce2a2021-01-21 14:19:27 +0100665 * @mnt_userns: user namespace of the mount @inode was found from
666 * @inode: target inode
667 * @mode_p: mode (pointer) for update
668 * @acl: acl pointer
Jan Kara07393102016-09-19 17:39:09 +0200669 *
670 * Update the file mode when setting an ACL: compute the new file permission
671 * bits based on the ACL. In addition, if the ACL is equivalent to the new
Randy Dunlape39e7732020-01-04 13:00:05 -0800672 * file mode, set *@acl to NULL to indicate that no ACL should be set.
Jan Kara07393102016-09-19 17:39:09 +0200673 *
Randy Dunlape39e7732020-01-04 13:00:05 -0800674 * As with chmod, clear the setgid bit if the caller is not in the owning group
Jan Kara07393102016-09-19 17:39:09 +0200675 * or capable of CAP_FSETID (see inode_change_ok).
676 *
Christian Braunere65ce2a2021-01-21 14:19:27 +0100677 * If the inode has been found through an idmapped mount the user namespace of
678 * the vfsmount must be passed through @mnt_userns. This function will then
679 * take care to map the inode according to @mnt_userns before checking
680 * permissions. On non-idmapped mounts or if permission checking is to be
681 * performed on the raw inode simply passs init_user_ns.
682 *
Jan Kara07393102016-09-19 17:39:09 +0200683 * Called from set_acl inode operations.
684 */
Christian Braunere65ce2a2021-01-21 14:19:27 +0100685int posix_acl_update_mode(struct user_namespace *mnt_userns,
686 struct inode *inode, umode_t *mode_p,
Jan Kara07393102016-09-19 17:39:09 +0200687 struct posix_acl **acl)
688{
689 umode_t mode = inode->i_mode;
690 int error;
691
692 error = posix_acl_equiv_mode(*acl, &mode);
693 if (error < 0)
694 return error;
695 if (error == 0)
696 *acl = NULL;
Christian Braunere65ce2a2021-01-21 14:19:27 +0100697 if (!in_group_p(i_gid_into_mnt(mnt_userns, inode)) &&
698 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
Jan Kara07393102016-09-19 17:39:09 +0200699 mode &= ~S_ISGID;
700 *mode_p = mode;
701 return 0;
702}
703EXPORT_SYMBOL(posix_acl_update_mode);
704
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800705/*
706 * Fix up the uids and gids in posix acl extended attributes in place.
707 */
708static void posix_acl_fix_xattr_userns(
709 struct user_namespace *to, struct user_namespace *from,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100710 struct user_namespace *mnt_userns,
711 void *value, size_t size, bool from_user)
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800712{
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +0200713 struct posix_acl_xattr_header *header = value;
714 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800715 int count;
716 kuid_t uid;
717 kgid_t gid;
718
719 if (!value)
720 return;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +0200721 if (size < sizeof(struct posix_acl_xattr_header))
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800722 return;
723 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
724 return;
725
726 count = posix_acl_xattr_count(size);
727 if (count < 0)
728 return;
729 if (count == 0)
730 return;
731
732 for (end = entry + count; entry != end; entry++) {
733 switch(le16_to_cpu(entry->e_tag)) {
734 case ACL_USER:
735 uid = make_kuid(from, le32_to_cpu(entry->e_id));
Christian Braunere65ce2a2021-01-21 14:19:27 +0100736 if (from_user)
737 uid = kuid_from_mnt(mnt_userns, uid);
738 else
739 uid = kuid_into_mnt(mnt_userns, uid);
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800740 entry->e_id = cpu_to_le32(from_kuid(to, uid));
741 break;
742 case ACL_GROUP:
743 gid = make_kgid(from, le32_to_cpu(entry->e_id));
Christian Braunere65ce2a2021-01-21 14:19:27 +0100744 if (from_user)
745 gid = kgid_from_mnt(mnt_userns, gid);
746 else
747 gid = kgid_into_mnt(mnt_userns, gid);
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800748 entry->e_id = cpu_to_le32(from_kgid(to, gid));
749 break;
750 default:
751 break;
752 }
753 }
754}
755
Christian Braunere65ce2a2021-01-21 14:19:27 +0100756void posix_acl_fix_xattr_from_user(struct user_namespace *mnt_userns,
757 void *value, size_t size)
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800758{
759 struct user_namespace *user_ns = current_user_ns();
Christian Braunere65ce2a2021-01-21 14:19:27 +0100760 if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns))
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800761 return;
Christian Braunere65ce2a2021-01-21 14:19:27 +0100762 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, mnt_userns, value,
763 size, true);
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800764}
765
Christian Braunere65ce2a2021-01-21 14:19:27 +0100766void posix_acl_fix_xattr_to_user(struct user_namespace *mnt_userns,
767 void *value, size_t size)
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800768{
769 struct user_namespace *user_ns = current_user_ns();
Christian Braunere65ce2a2021-01-21 14:19:27 +0100770 if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns))
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800771 return;
Christian Braunere65ce2a2021-01-21 14:19:27 +0100772 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, mnt_userns, value,
773 size, false);
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800774}
775
776/*
777 * Convert from extended attribute to in-memory representation.
778 */
779struct posix_acl *
780posix_acl_from_xattr(struct user_namespace *user_ns,
781 const void *value, size_t size)
782{
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +0200783 const struct posix_acl_xattr_header *header = value;
784 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800785 int count;
786 struct posix_acl *acl;
787 struct posix_acl_entry *acl_e;
788
789 if (!value)
790 return NULL;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +0200791 if (size < sizeof(struct posix_acl_xattr_header))
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800792 return ERR_PTR(-EINVAL);
793 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
794 return ERR_PTR(-EOPNOTSUPP);
795
796 count = posix_acl_xattr_count(size);
797 if (count < 0)
798 return ERR_PTR(-EINVAL);
799 if (count == 0)
800 return NULL;
801
802 acl = posix_acl_alloc(count, GFP_NOFS);
803 if (!acl)
804 return ERR_PTR(-ENOMEM);
805 acl_e = acl->a_entries;
806
807 for (end = entry + count; entry != end; acl_e++, entry++) {
808 acl_e->e_tag = le16_to_cpu(entry->e_tag);
809 acl_e->e_perm = le16_to_cpu(entry->e_perm);
810
811 switch(acl_e->e_tag) {
812 case ACL_USER_OBJ:
813 case ACL_GROUP_OBJ:
814 case ACL_MASK:
815 case ACL_OTHER:
816 break;
817
818 case ACL_USER:
819 acl_e->e_uid =
820 make_kuid(user_ns,
821 le32_to_cpu(entry->e_id));
822 if (!uid_valid(acl_e->e_uid))
823 goto fail;
824 break;
825 case ACL_GROUP:
826 acl_e->e_gid =
827 make_kgid(user_ns,
828 le32_to_cpu(entry->e_id));
829 if (!gid_valid(acl_e->e_gid))
830 goto fail;
831 break;
832
833 default:
834 goto fail;
835 }
836 }
837 return acl;
838
839fail:
840 posix_acl_release(acl);
841 return ERR_PTR(-EINVAL);
842}
843EXPORT_SYMBOL (posix_acl_from_xattr);
844
845/*
846 * Convert from in-memory to extended attribute representation.
847 */
848int
849posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
850 void *buffer, size_t size)
851{
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +0200852 struct posix_acl_xattr_header *ext_acl = buffer;
853 struct posix_acl_xattr_entry *ext_entry;
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800854 int real_size, n;
855
856 real_size = posix_acl_xattr_size(acl->a_count);
857 if (!buffer)
858 return real_size;
859 if (real_size > size)
860 return -ERANGE;
Dan Carpenter47ba9732014-02-14 12:05:49 +0300861
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +0200862 ext_entry = (void *)(ext_acl + 1);
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800863 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
864
865 for (n=0; n < acl->a_count; n++, ext_entry++) {
866 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
867 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
868 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
869 switch(acl_e->e_tag) {
870 case ACL_USER:
871 ext_entry->e_id =
872 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
873 break;
874 case ACL_GROUP:
875 ext_entry->e_id =
876 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
877 break;
878 default:
879 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
880 break;
881 }
882 }
883 return real_size;
884}
885EXPORT_SYMBOL (posix_acl_to_xattr);
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800886
887static int
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200888posix_acl_xattr_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -0400889 struct dentry *unused, struct inode *inode,
890 const char *name, void *value, size_t size)
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800891{
892 struct posix_acl *acl;
893 int error;
894
Al Virob2968212016-04-10 20:48:24 -0400895 if (!IS_POSIXACL(inode))
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800896 return -EOPNOTSUPP;
Al Virob2968212016-04-10 20:48:24 -0400897 if (S_ISLNK(inode->i_mode))
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800898 return -EOPNOTSUPP;
899
Al Virob2968212016-04-10 20:48:24 -0400900 acl = get_acl(inode, handler->flags);
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800901 if (IS_ERR(acl))
902 return PTR_ERR(acl);
903 if (acl == NULL)
904 return -ENODATA;
905
906 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
907 posix_acl_release(acl);
908
909 return error;
910}
911
Andreas Gruenbacher485e71e2016-06-22 23:57:25 +0200912int
Christian Braunere65ce2a2021-01-21 14:19:27 +0100913set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode,
914 int type, struct posix_acl *acl)
Andreas Gruenbacher485e71e2016-06-22 23:57:25 +0200915{
916 if (!IS_POSIXACL(inode))
917 return -EOPNOTSUPP;
918 if (!inode->i_op->set_acl)
919 return -EOPNOTSUPP;
920
921 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
922 return acl ? -EACCES : 0;
Christian Braunere65ce2a2021-01-21 14:19:27 +0100923 if (!inode_owner_or_capable(mnt_userns, inode))
Andreas Gruenbacher485e71e2016-06-22 23:57:25 +0200924 return -EPERM;
925
926 if (acl) {
Linus Torvaldsa867d732016-07-29 15:54:19 -0700927 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
Andreas Gruenbacher485e71e2016-06-22 23:57:25 +0200928 if (ret)
929 return ret;
930 }
Christian Brauner549c7292021-01-21 14:19:43 +0100931 return inode->i_op->set_acl(mnt_userns, inode, acl, type);
Andreas Gruenbacher485e71e2016-06-22 23:57:25 +0200932}
933EXPORT_SYMBOL(set_posix_acl);
934
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800935static int
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200936posix_acl_xattr_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100937 struct user_namespace *mnt_userns,
938 struct dentry *unused, struct inode *inode,
939 const char *name, const void *value, size_t size,
940 int flags)
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800941{
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800942 struct posix_acl *acl = NULL;
943 int ret;
944
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800945 if (value) {
946 acl = posix_acl_from_xattr(&init_user_ns, value, size);
947 if (IS_ERR(acl))
948 return PTR_ERR(acl);
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800949 }
Christian Braunere65ce2a2021-01-21 14:19:27 +0100950 ret = set_posix_acl(mnt_userns, inode, handler->flags, acl);
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800951 posix_acl_release(acl);
952 return ret;
953}
954
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100955static bool
956posix_acl_xattr_list(struct dentry *dentry)
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800957{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100958 return IS_POSIXACL(d_backing_inode(dentry));
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800959}
960
961const struct xattr_handler posix_acl_access_xattr_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100962 .name = XATTR_NAME_POSIX_ACL_ACCESS,
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800963 .flags = ACL_TYPE_ACCESS,
964 .list = posix_acl_xattr_list,
965 .get = posix_acl_xattr_get,
966 .set = posix_acl_xattr_set,
967};
968EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
969
970const struct xattr_handler posix_acl_default_xattr_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100971 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800972 .flags = ACL_TYPE_DEFAULT,
973 .list = posix_acl_xattr_list,
974 .get = posix_acl_xattr_get,
975 .set = posix_acl_xattr_set,
976};
977EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
Christoph Hellwigfeda8212013-12-20 05:16:54 -0800978
Christian Brauner549c7292021-01-21 14:19:43 +0100979int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
980 struct posix_acl *acl, int type)
Christoph Hellwigfeda8212013-12-20 05:16:54 -0800981{
982 int error;
983
984 if (type == ACL_TYPE_ACCESS) {
Christian Brauner549c7292021-01-21 14:19:43 +0100985 error = posix_acl_update_mode(mnt_userns, inode,
Gu Zheng497de072017-01-09 09:34:48 +0800986 &inode->i_mode, &acl);
987 if (error)
988 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -0800989 }
990
Deepa Dinamani078cd822016-09-14 07:48:04 -0700991 inode->i_ctime = current_time(inode);
Christoph Hellwigfeda8212013-12-20 05:16:54 -0800992 set_cached_acl(inode, type, acl);
993 return 0;
994}
995
996int simple_acl_create(struct inode *dir, struct inode *inode)
997{
998 struct posix_acl *default_acl, *acl;
999 int error;
1000
1001 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1002 if (error)
1003 return error;
1004
1005 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1006 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1007
1008 if (default_acl)
1009 posix_acl_release(default_acl);
1010 if (acl)
1011 posix_acl_release(acl);
1012 return 0;
1013}