blob: 08218550b0dbc6b393313a89d877b0460085d46d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -08002 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -08004 * Fixes from William Schumacher incorporated on 15 March 2001.
5 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8/*
9 * This file contains generic functions for manipulating
10 * POSIX 1003.1e draft standard 17 ACLs.
11 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070015#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
17#include <linux/sched.h>
18#include <linux/posix_acl.h>
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -080019#include <linux/posix_acl_xattr.h>
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -080020#include <linux/xattr.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050021#include <linux/export.h>
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -080022#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Chuck Leverf61f6da2011-01-21 03:05:38 +000024EXPORT_SYMBOL(posix_acl_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025EXPORT_SYMBOL(posix_acl_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026EXPORT_SYMBOL(posix_acl_valid);
27EXPORT_SYMBOL(posix_acl_equiv_mode);
28EXPORT_SYMBOL(posix_acl_from_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Christoph Hellwig2982baa2013-12-20 05:16:38 -080030struct posix_acl *get_acl(struct inode *inode, int type)
31{
32 struct posix_acl *acl;
33
34 acl = get_cached_acl(inode, type);
35 if (acl != ACL_NOT_CACHED)
36 return acl;
37
38 if (!IS_POSIXACL(inode))
39 return NULL;
40
41 /*
42 * A filesystem can force a ACL callback by just never filling the
43 * ACL cache. But normally you'd fill the cache either at inode
44 * instantiation time, or on the first ->get_acl call.
45 *
46 * If the filesystem doesn't have a get_acl() function at all, we'll
47 * just create the negative cache entry.
48 */
49 if (!inode->i_op->get_acl) {
50 set_cached_acl(inode, type, NULL);
51 return NULL;
52 }
53 return inode->i_op->get_acl(inode, type);
54}
55EXPORT_SYMBOL(get_acl);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Chuck Leverf61f6da2011-01-21 03:05:38 +000058 * Init a fresh posix_acl
59 */
60void
61posix_acl_init(struct posix_acl *acl, int count)
62{
63 atomic_set(&acl->a_refcount, 1);
64 acl->a_count = count;
65}
66
67/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * Allocate a new ACL with the specified number of entries.
69 */
70struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +010071posix_acl_alloc(int count, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 const size_t size = sizeof(struct posix_acl) +
74 count * sizeof(struct posix_acl_entry);
75 struct posix_acl *acl = kmalloc(size, flags);
Chuck Leverf61f6da2011-01-21 03:05:38 +000076 if (acl)
77 posix_acl_init(acl, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return acl;
79}
80
81/*
82 * Clone an ACL.
83 */
Al Viroedde8542011-07-23 03:27:37 -040084static struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +010085posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct posix_acl *clone = NULL;
88
89 if (acl) {
90 int size = sizeof(struct posix_acl) + acl->a_count *
91 sizeof(struct posix_acl_entry);
Alexey Dobriyan52978be2006-09-30 23:27:21 -070092 clone = kmemdup(acl, size, flags);
93 if (clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 atomic_set(&clone->a_refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96 return clone;
97}
98
99/*
100 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
101 */
102int
103posix_acl_valid(const struct posix_acl *acl)
104{
105 const struct posix_acl_entry *pa, *pe;
106 int state = ACL_USER_OBJ;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800107 kuid_t prev_uid = INVALID_UID;
108 kgid_t prev_gid = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int needs_mask = 0;
110
111 FOREACH_ACL_ENTRY(pa, acl, pe) {
112 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
113 return -EINVAL;
114 switch (pa->e_tag) {
115 case ACL_USER_OBJ:
116 if (state == ACL_USER_OBJ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 state = ACL_USER;
118 break;
119 }
120 return -EINVAL;
121
122 case ACL_USER:
123 if (state != ACL_USER)
124 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800125 if (!uid_valid(pa->e_uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800127 if (uid_valid(prev_uid) &&
128 uid_lte(pa->e_uid, prev_uid))
129 return -EINVAL;
130 prev_uid = pa->e_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 needs_mask = 1;
132 break;
133
134 case ACL_GROUP_OBJ:
135 if (state == ACL_USER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 state = ACL_GROUP;
137 break;
138 }
139 return -EINVAL;
140
141 case ACL_GROUP:
142 if (state != ACL_GROUP)
143 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800144 if (!gid_valid(pa->e_gid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800146 if (gid_valid(prev_gid) &&
147 gid_lte(pa->e_gid, prev_gid))
148 return -EINVAL;
149 prev_gid = pa->e_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 needs_mask = 1;
151 break;
152
153 case ACL_MASK:
154 if (state != ACL_GROUP)
155 return -EINVAL;
156 state = ACL_OTHER;
157 break;
158
159 case ACL_OTHER:
160 if (state == ACL_OTHER ||
161 (state == ACL_GROUP && !needs_mask)) {
162 state = 0;
163 break;
164 }
165 return -EINVAL;
166
167 default:
168 return -EINVAL;
169 }
170 }
171 if (state == 0)
172 return 0;
173 return -EINVAL;
174}
175
176/*
177 * Returns 0 if the acl can be exactly represented in the traditional
178 * file mode permission bits, or else 1. Returns -E... on error.
179 */
180int
Al Virod6952122011-07-23 18:56:36 -0400181posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 const struct posix_acl_entry *pa, *pe;
Al Virod6952122011-07-23 18:56:36 -0400184 umode_t mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int not_equiv = 0;
186
187 FOREACH_ACL_ENTRY(pa, acl, pe) {
188 switch (pa->e_tag) {
189 case ACL_USER_OBJ:
190 mode |= (pa->e_perm & S_IRWXO) << 6;
191 break;
192 case ACL_GROUP_OBJ:
193 mode |= (pa->e_perm & S_IRWXO) << 3;
194 break;
195 case ACL_OTHER:
196 mode |= pa->e_perm & S_IRWXO;
197 break;
198 case ACL_MASK:
199 mode = (mode & ~S_IRWXG) |
200 ((pa->e_perm & S_IRWXO) << 3);
201 not_equiv = 1;
202 break;
203 case ACL_USER:
204 case ACL_GROUP:
205 not_equiv = 1;
206 break;
207 default:
208 return -EINVAL;
209 }
210 }
211 if (mode_p)
212 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
213 return not_equiv;
214}
215
216/*
217 * Create an ACL representing the file mode permission bits of an inode.
218 */
219struct posix_acl *
Al Viro3a5fba12011-07-23 19:01:48 -0400220posix_acl_from_mode(umode_t mode, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct posix_acl *acl = posix_acl_alloc(3, flags);
223 if (!acl)
224 return ERR_PTR(-ENOMEM);
225
226 acl->a_entries[0].e_tag = ACL_USER_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
228
229 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
231
232 acl->a_entries[2].e_tag = ACL_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 acl->a_entries[2].e_perm = (mode & S_IRWXO);
234 return acl;
235}
236
237/*
238 * Return 0 if current is granted want access to the inode
239 * by the acl. Returns -E... otherwise.
240 */
241int
242posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
243{
244 const struct posix_acl_entry *pa, *pe, *mask_obj;
245 int found = 0;
246
Andreas Gruenbacherd124b602011-10-23 23:13:32 +0530247 want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 FOREACH_ACL_ENTRY(pa, acl, pe) {
250 switch(pa->e_tag) {
251 case ACL_USER_OBJ:
252 /* (May have been checked already) */
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800253 if (uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto check_perm;
255 break;
256 case ACL_USER:
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800257 if (uid_eq(pa->e_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto mask;
259 break;
260 case ACL_GROUP_OBJ:
261 if (in_group_p(inode->i_gid)) {
262 found = 1;
263 if ((pa->e_perm & want) == want)
264 goto mask;
265 }
266 break;
267 case ACL_GROUP:
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800268 if (in_group_p(pa->e_gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 found = 1;
270 if ((pa->e_perm & want) == want)
271 goto mask;
272 }
273 break;
274 case ACL_MASK:
275 break;
276 case ACL_OTHER:
277 if (found)
278 return -EACCES;
279 else
280 goto check_perm;
281 default:
282 return -EIO;
283 }
284 }
285 return -EIO;
286
287mask:
288 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
289 if (mask_obj->e_tag == ACL_MASK) {
290 if ((pa->e_perm & mask_obj->e_perm & want) == want)
291 return 0;
292 return -EACCES;
293 }
294 }
295
296check_perm:
297 if ((pa->e_perm & want) == want)
298 return 0;
299 return -EACCES;
300}
301
302/*
303 * Modify acl when creating a new inode. The caller must ensure the acl is
304 * only referenced once.
305 *
306 * mode_p initially must contain the mode parameter to the open() / creat()
307 * system calls. All permissions that are not granted by the acl are removed.
308 * The permissions in the acl are changed to reflect the mode_p parameter.
309 */
Al Virod3fb6122011-07-23 18:37:50 -0400310static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct posix_acl_entry *pa, *pe;
313 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
Al Virod3fb6122011-07-23 18:37:50 -0400314 umode_t mode = *mode_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int not_equiv = 0;
316
317 /* assert(atomic_read(acl->a_refcount) == 1); */
318
319 FOREACH_ACL_ENTRY(pa, acl, pe) {
320 switch(pa->e_tag) {
321 case ACL_USER_OBJ:
322 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
323 mode &= (pa->e_perm << 6) | ~S_IRWXU;
324 break;
325
326 case ACL_USER:
327 case ACL_GROUP:
328 not_equiv = 1;
329 break;
330
331 case ACL_GROUP_OBJ:
332 group_obj = pa;
333 break;
334
335 case ACL_OTHER:
336 pa->e_perm &= mode | ~S_IRWXO;
337 mode &= pa->e_perm | ~S_IRWXO;
338 break;
339
340 case ACL_MASK:
341 mask_obj = pa;
342 not_equiv = 1;
343 break;
344
345 default:
346 return -EIO;
347 }
348 }
349
350 if (mask_obj) {
351 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
352 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
353 } else {
354 if (!group_obj)
355 return -EIO;
356 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
357 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
358 }
359
360 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
361 return not_equiv;
362}
363
364/*
365 * Modify the ACL for the chmod syscall.
366 */
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800367static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
370 struct posix_acl_entry *pa, *pe;
371
372 /* assert(atomic_read(acl->a_refcount) == 1); */
373
374 FOREACH_ACL_ENTRY(pa, acl, pe) {
375 switch(pa->e_tag) {
376 case ACL_USER_OBJ:
377 pa->e_perm = (mode & S_IRWXU) >> 6;
378 break;
379
380 case ACL_USER:
381 case ACL_GROUP:
382 break;
383
384 case ACL_GROUP_OBJ:
385 group_obj = pa;
386 break;
387
388 case ACL_MASK:
389 mask_obj = pa;
390 break;
391
392 case ACL_OTHER:
393 pa->e_perm = (mode & S_IRWXO);
394 break;
395
396 default:
397 return -EIO;
398 }
399 }
400
401 if (mask_obj) {
402 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
403 } else {
404 if (!group_obj)
405 return -EIO;
406 group_obj->e_perm = (mode & S_IRWXG) >> 3;
407 }
408
409 return 0;
410}
Al Virobc26ab52011-07-23 00:18:02 -0400411
412int
Al Virod3fb6122011-07-23 18:37:50 -0400413posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
Al Viro826cae22011-07-23 03:10:32 -0400414{
415 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
416 int err = -ENOMEM;
417 if (clone) {
418 err = posix_acl_create_masq(clone, mode_p);
419 if (err < 0) {
420 posix_acl_release(clone);
421 clone = NULL;
422 }
423 }
424 posix_acl_release(*acl);
425 *acl = clone;
426 return err;
427}
428EXPORT_SYMBOL(posix_acl_create);
429
430int
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800431__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
Al Virobc26ab52011-07-23 00:18:02 -0400432{
433 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
434 int err = -ENOMEM;
435 if (clone) {
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800436 err = __posix_acl_chmod_masq(clone, mode);
Al Virobc26ab52011-07-23 00:18:02 -0400437 if (err) {
438 posix_acl_release(clone);
439 clone = NULL;
440 }
441 }
442 posix_acl_release(*acl);
443 *acl = clone;
444 return err;
445}
Christoph Hellwig5bf32582013-12-20 05:16:41 -0800446EXPORT_SYMBOL(__posix_acl_chmod);
447
448int
449posix_acl_chmod(struct inode *inode)
450{
451 struct posix_acl *acl;
452 int ret = 0;
453
454 if (!IS_POSIXACL(inode))
455 return 0;
456 if (!inode->i_op->set_acl)
457 return -EOPNOTSUPP;
458
459 acl = get_acl(inode, ACL_TYPE_ACCESS);
460 if (IS_ERR_OR_NULL(acl))
461 return PTR_ERR(acl);
462
463 ret = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
464 if (ret)
465 return ret;
466 ret = inode->i_op->set_acl(inode, acl, ACL_TYPE_ACCESS);
467 posix_acl_release(acl);
468 return ret;
469}
Al Virobc26ab52011-07-23 00:18:02 -0400470EXPORT_SYMBOL(posix_acl_chmod);
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800471
472/*
473 * Fix up the uids and gids in posix acl extended attributes in place.
474 */
475static void posix_acl_fix_xattr_userns(
476 struct user_namespace *to, struct user_namespace *from,
477 void *value, size_t size)
478{
479 posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
480 posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
481 int count;
482 kuid_t uid;
483 kgid_t gid;
484
485 if (!value)
486 return;
487 if (size < sizeof(posix_acl_xattr_header))
488 return;
489 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
490 return;
491
492 count = posix_acl_xattr_count(size);
493 if (count < 0)
494 return;
495 if (count == 0)
496 return;
497
498 for (end = entry + count; entry != end; entry++) {
499 switch(le16_to_cpu(entry->e_tag)) {
500 case ACL_USER:
501 uid = make_kuid(from, le32_to_cpu(entry->e_id));
502 entry->e_id = cpu_to_le32(from_kuid(to, uid));
503 break;
504 case ACL_GROUP:
505 gid = make_kgid(from, le32_to_cpu(entry->e_id));
506 entry->e_id = cpu_to_le32(from_kgid(to, gid));
507 break;
508 default:
509 break;
510 }
511 }
512}
513
514void posix_acl_fix_xattr_from_user(void *value, size_t size)
515{
516 struct user_namespace *user_ns = current_user_ns();
517 if (user_ns == &init_user_ns)
518 return;
519 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
520}
521
522void posix_acl_fix_xattr_to_user(void *value, size_t size)
523{
524 struct user_namespace *user_ns = current_user_ns();
525 if (user_ns == &init_user_ns)
526 return;
527 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
528}
529
530/*
531 * Convert from extended attribute to in-memory representation.
532 */
533struct posix_acl *
534posix_acl_from_xattr(struct user_namespace *user_ns,
535 const void *value, size_t size)
536{
537 posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
538 posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
539 int count;
540 struct posix_acl *acl;
541 struct posix_acl_entry *acl_e;
542
543 if (!value)
544 return NULL;
545 if (size < sizeof(posix_acl_xattr_header))
546 return ERR_PTR(-EINVAL);
547 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
548 return ERR_PTR(-EOPNOTSUPP);
549
550 count = posix_acl_xattr_count(size);
551 if (count < 0)
552 return ERR_PTR(-EINVAL);
553 if (count == 0)
554 return NULL;
555
556 acl = posix_acl_alloc(count, GFP_NOFS);
557 if (!acl)
558 return ERR_PTR(-ENOMEM);
559 acl_e = acl->a_entries;
560
561 for (end = entry + count; entry != end; acl_e++, entry++) {
562 acl_e->e_tag = le16_to_cpu(entry->e_tag);
563 acl_e->e_perm = le16_to_cpu(entry->e_perm);
564
565 switch(acl_e->e_tag) {
566 case ACL_USER_OBJ:
567 case ACL_GROUP_OBJ:
568 case ACL_MASK:
569 case ACL_OTHER:
570 break;
571
572 case ACL_USER:
573 acl_e->e_uid =
574 make_kuid(user_ns,
575 le32_to_cpu(entry->e_id));
576 if (!uid_valid(acl_e->e_uid))
577 goto fail;
578 break;
579 case ACL_GROUP:
580 acl_e->e_gid =
581 make_kgid(user_ns,
582 le32_to_cpu(entry->e_id));
583 if (!gid_valid(acl_e->e_gid))
584 goto fail;
585 break;
586
587 default:
588 goto fail;
589 }
590 }
591 return acl;
592
593fail:
594 posix_acl_release(acl);
595 return ERR_PTR(-EINVAL);
596}
597EXPORT_SYMBOL (posix_acl_from_xattr);
598
599/*
600 * Convert from in-memory to extended attribute representation.
601 */
602int
603posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
604 void *buffer, size_t size)
605{
606 posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
607 posix_acl_xattr_entry *ext_entry = ext_acl->a_entries;
608 int real_size, n;
609
610 real_size = posix_acl_xattr_size(acl->a_count);
611 if (!buffer)
612 return real_size;
613 if (real_size > size)
614 return -ERANGE;
615
616 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
617
618 for (n=0; n < acl->a_count; n++, ext_entry++) {
619 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
620 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
621 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
622 switch(acl_e->e_tag) {
623 case ACL_USER:
624 ext_entry->e_id =
625 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
626 break;
627 case ACL_GROUP:
628 ext_entry->e_id =
629 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
630 break;
631 default:
632 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
633 break;
634 }
635 }
636 return real_size;
637}
638EXPORT_SYMBOL (posix_acl_to_xattr);
Christoph Hellwig2aeccbe2013-12-20 05:16:40 -0800639
640static int
641posix_acl_xattr_get(struct dentry *dentry, const char *name,
642 void *value, size_t size, int type)
643{
644 struct posix_acl *acl;
645 int error;
646
647 if (!IS_POSIXACL(dentry->d_inode))
648 return -EOPNOTSUPP;
649 if (S_ISLNK(dentry->d_inode->i_mode))
650 return -EOPNOTSUPP;
651
652 acl = get_acl(dentry->d_inode, type);
653 if (IS_ERR(acl))
654 return PTR_ERR(acl);
655 if (acl == NULL)
656 return -ENODATA;
657
658 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
659 posix_acl_release(acl);
660
661 return error;
662}
663
664static int
665posix_acl_xattr_set(struct dentry *dentry, const char *name,
666 const void *value, size_t size, int flags, int type)
667{
668 struct inode *inode = dentry->d_inode;
669 struct posix_acl *acl = NULL;
670 int ret;
671
672 if (!IS_POSIXACL(inode))
673 return -EOPNOTSUPP;
674 if (!inode->i_op->set_acl)
675 return -EOPNOTSUPP;
676
677 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
678 return value ? -EACCES : 0;
679 if (!inode_owner_or_capable(inode))
680 return -EPERM;
681
682 if (value) {
683 acl = posix_acl_from_xattr(&init_user_ns, value, size);
684 if (IS_ERR(acl))
685 return PTR_ERR(acl);
686
687 if (acl) {
688 ret = posix_acl_valid(acl);
689 if (ret)
690 goto out;
691 }
692 }
693
694 ret = inode->i_op->set_acl(inode, acl, type);
695out:
696 posix_acl_release(acl);
697 return ret;
698}
699
700static size_t
701posix_acl_xattr_list(struct dentry *dentry, char *list, size_t list_size,
702 const char *name, size_t name_len, int type)
703{
704 const char *xname;
705 size_t size;
706
707 if (!IS_POSIXACL(dentry->d_inode))
708 return -EOPNOTSUPP;
709 if (S_ISLNK(dentry->d_inode->i_mode))
710 return -EOPNOTSUPP;
711
712 if (type == ACL_TYPE_ACCESS)
713 xname = POSIX_ACL_XATTR_ACCESS;
714 else
715 xname = POSIX_ACL_XATTR_DEFAULT;
716
717 size = strlen(xname) + 1;
718 if (list && size <= list_size)
719 memcpy(list, xname, size);
720 return size;
721}
722
723const struct xattr_handler posix_acl_access_xattr_handler = {
724 .prefix = POSIX_ACL_XATTR_ACCESS,
725 .flags = ACL_TYPE_ACCESS,
726 .list = posix_acl_xattr_list,
727 .get = posix_acl_xattr_get,
728 .set = posix_acl_xattr_set,
729};
730EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
731
732const struct xattr_handler posix_acl_default_xattr_handler = {
733 .prefix = POSIX_ACL_XATTR_DEFAULT,
734 .flags = ACL_TYPE_DEFAULT,
735 .list = posix_acl_xattr_list,
736 .get = posix_acl_xattr_get,
737 .set = posix_acl_xattr_set,
738};
739EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);