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