blob: 30524de49a6bc47c7c47e6f1f58ccddb1432d6b9 [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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050020#include <linux/export.h>
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -080021#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Chuck Leverf61f6da2011-01-21 03:05:38 +000023EXPORT_SYMBOL(posix_acl_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024EXPORT_SYMBOL(posix_acl_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025EXPORT_SYMBOL(posix_acl_valid);
26EXPORT_SYMBOL(posix_acl_equiv_mode);
27EXPORT_SYMBOL(posix_acl_from_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Christoph Hellwig2982baa2013-12-20 05:16:38 -080029struct posix_acl *get_acl(struct inode *inode, int type)
30{
31 struct posix_acl *acl;
32
33 acl = get_cached_acl(inode, type);
34 if (acl != ACL_NOT_CACHED)
35 return acl;
36
37 if (!IS_POSIXACL(inode))
38 return NULL;
39
40 /*
41 * A filesystem can force a ACL callback by just never filling the
42 * ACL cache. But normally you'd fill the cache either at inode
43 * instantiation time, or on the first ->get_acl call.
44 *
45 * If the filesystem doesn't have a get_acl() function at all, we'll
46 * just create the negative cache entry.
47 */
48 if (!inode->i_op->get_acl) {
49 set_cached_acl(inode, type, NULL);
50 return NULL;
51 }
52 return inode->i_op->get_acl(inode, type);
53}
54EXPORT_SYMBOL(get_acl);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
Chuck Leverf61f6da2011-01-21 03:05:38 +000057 * Init a fresh posix_acl
58 */
59void
60posix_acl_init(struct posix_acl *acl, int count)
61{
62 atomic_set(&acl->a_refcount, 1);
63 acl->a_count = count;
64}
65
66/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * Allocate a new ACL with the specified number of entries.
68 */
69struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +010070posix_acl_alloc(int count, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 const size_t size = sizeof(struct posix_acl) +
73 count * sizeof(struct posix_acl_entry);
74 struct posix_acl *acl = kmalloc(size, flags);
Chuck Leverf61f6da2011-01-21 03:05:38 +000075 if (acl)
76 posix_acl_init(acl, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return acl;
78}
79
80/*
81 * Clone an ACL.
82 */
Al Viroedde8542011-07-23 03:27:37 -040083static struct posix_acl *
Al Virodd0fc662005-10-07 07:46:04 +010084posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct posix_acl *clone = NULL;
87
88 if (acl) {
89 int size = sizeof(struct posix_acl) + acl->a_count *
90 sizeof(struct posix_acl_entry);
Alexey Dobriyan52978be2006-09-30 23:27:21 -070091 clone = kmemdup(acl, size, flags);
92 if (clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 atomic_set(&clone->a_refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95 return clone;
96}
97
98/*
99 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
100 */
101int
102posix_acl_valid(const struct posix_acl *acl)
103{
104 const struct posix_acl_entry *pa, *pe;
105 int state = ACL_USER_OBJ;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800106 kuid_t prev_uid = INVALID_UID;
107 kgid_t prev_gid = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int needs_mask = 0;
109
110 FOREACH_ACL_ENTRY(pa, acl, pe) {
111 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
112 return -EINVAL;
113 switch (pa->e_tag) {
114 case ACL_USER_OBJ:
115 if (state == ACL_USER_OBJ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 state = ACL_USER;
117 break;
118 }
119 return -EINVAL;
120
121 case ACL_USER:
122 if (state != ACL_USER)
123 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800124 if (!uid_valid(pa->e_uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800126 if (uid_valid(prev_uid) &&
127 uid_lte(pa->e_uid, prev_uid))
128 return -EINVAL;
129 prev_uid = pa->e_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 needs_mask = 1;
131 break;
132
133 case ACL_GROUP_OBJ:
134 if (state == ACL_USER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 state = ACL_GROUP;
136 break;
137 }
138 return -EINVAL;
139
140 case ACL_GROUP:
141 if (state != ACL_GROUP)
142 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800143 if (!gid_valid(pa->e_gid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -EINVAL;
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800145 if (gid_valid(prev_gid) &&
146 gid_lte(pa->e_gid, prev_gid))
147 return -EINVAL;
148 prev_gid = pa->e_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 needs_mask = 1;
150 break;
151
152 case ACL_MASK:
153 if (state != ACL_GROUP)
154 return -EINVAL;
155 state = ACL_OTHER;
156 break;
157
158 case ACL_OTHER:
159 if (state == ACL_OTHER ||
160 (state == ACL_GROUP && !needs_mask)) {
161 state = 0;
162 break;
163 }
164 return -EINVAL;
165
166 default:
167 return -EINVAL;
168 }
169 }
170 if (state == 0)
171 return 0;
172 return -EINVAL;
173}
174
175/*
176 * Returns 0 if the acl can be exactly represented in the traditional
177 * file mode permission bits, or else 1. Returns -E... on error.
178 */
179int
Al Virod6952122011-07-23 18:56:36 -0400180posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 const struct posix_acl_entry *pa, *pe;
Al Virod6952122011-07-23 18:56:36 -0400183 umode_t mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int not_equiv = 0;
185
186 FOREACH_ACL_ENTRY(pa, acl, pe) {
187 switch (pa->e_tag) {
188 case ACL_USER_OBJ:
189 mode |= (pa->e_perm & S_IRWXO) << 6;
190 break;
191 case ACL_GROUP_OBJ:
192 mode |= (pa->e_perm & S_IRWXO) << 3;
193 break;
194 case ACL_OTHER:
195 mode |= pa->e_perm & S_IRWXO;
196 break;
197 case ACL_MASK:
198 mode = (mode & ~S_IRWXG) |
199 ((pa->e_perm & S_IRWXO) << 3);
200 not_equiv = 1;
201 break;
202 case ACL_USER:
203 case ACL_GROUP:
204 not_equiv = 1;
205 break;
206 default:
207 return -EINVAL;
208 }
209 }
210 if (mode_p)
211 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
212 return not_equiv;
213}
214
215/*
216 * Create an ACL representing the file mode permission bits of an inode.
217 */
218struct posix_acl *
Al Viro3a5fba12011-07-23 19:01:48 -0400219posix_acl_from_mode(umode_t mode, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct posix_acl *acl = posix_acl_alloc(3, flags);
222 if (!acl)
223 return ERR_PTR(-ENOMEM);
224
225 acl->a_entries[0].e_tag = ACL_USER_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
227
228 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
230
231 acl->a_entries[2].e_tag = ACL_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 acl->a_entries[2].e_perm = (mode & S_IRWXO);
233 return acl;
234}
235
236/*
237 * Return 0 if current is granted want access to the inode
238 * by the acl. Returns -E... otherwise.
239 */
240int
241posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
242{
243 const struct posix_acl_entry *pa, *pe, *mask_obj;
244 int found = 0;
245
Andreas Gruenbacherd124b602011-10-23 23:13:32 +0530246 want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 FOREACH_ACL_ENTRY(pa, acl, pe) {
249 switch(pa->e_tag) {
250 case ACL_USER_OBJ:
251 /* (May have been checked already) */
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800252 if (uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto check_perm;
254 break;
255 case ACL_USER:
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800256 if (uid_eq(pa->e_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto mask;
258 break;
259 case ACL_GROUP_OBJ:
260 if (in_group_p(inode->i_gid)) {
261 found = 1;
262 if ((pa->e_perm & want) == want)
263 goto mask;
264 }
265 break;
266 case ACL_GROUP:
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800267 if (in_group_p(pa->e_gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 found = 1;
269 if ((pa->e_perm & want) == want)
270 goto mask;
271 }
272 break;
273 case ACL_MASK:
274 break;
275 case ACL_OTHER:
276 if (found)
277 return -EACCES;
278 else
279 goto check_perm;
280 default:
281 return -EIO;
282 }
283 }
284 return -EIO;
285
286mask:
287 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
288 if (mask_obj->e_tag == ACL_MASK) {
289 if ((pa->e_perm & mask_obj->e_perm & want) == want)
290 return 0;
291 return -EACCES;
292 }
293 }
294
295check_perm:
296 if ((pa->e_perm & want) == want)
297 return 0;
298 return -EACCES;
299}
300
301/*
302 * Modify acl when creating a new inode. The caller must ensure the acl is
303 * only referenced once.
304 *
305 * mode_p initially must contain the mode parameter to the open() / creat()
306 * system calls. All permissions that are not granted by the acl are removed.
307 * The permissions in the acl are changed to reflect the mode_p parameter.
308 */
Al Virod3fb6122011-07-23 18:37:50 -0400309static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct posix_acl_entry *pa, *pe;
312 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
Al Virod3fb6122011-07-23 18:37:50 -0400313 umode_t mode = *mode_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int not_equiv = 0;
315
316 /* assert(atomic_read(acl->a_refcount) == 1); */
317
318 FOREACH_ACL_ENTRY(pa, acl, pe) {
319 switch(pa->e_tag) {
320 case ACL_USER_OBJ:
321 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
322 mode &= (pa->e_perm << 6) | ~S_IRWXU;
323 break;
324
325 case ACL_USER:
326 case ACL_GROUP:
327 not_equiv = 1;
328 break;
329
330 case ACL_GROUP_OBJ:
331 group_obj = pa;
332 break;
333
334 case ACL_OTHER:
335 pa->e_perm &= mode | ~S_IRWXO;
336 mode &= pa->e_perm | ~S_IRWXO;
337 break;
338
339 case ACL_MASK:
340 mask_obj = pa;
341 not_equiv = 1;
342 break;
343
344 default:
345 return -EIO;
346 }
347 }
348
349 if (mask_obj) {
350 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
351 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
352 } else {
353 if (!group_obj)
354 return -EIO;
355 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
356 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
357 }
358
359 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
360 return not_equiv;
361}
362
363/*
364 * Modify the ACL for the chmod syscall.
365 */
Al Viro86bc7042011-07-23 19:03:11 -0400366static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
369 struct posix_acl_entry *pa, *pe;
370
371 /* assert(atomic_read(acl->a_refcount) == 1); */
372
373 FOREACH_ACL_ENTRY(pa, acl, pe) {
374 switch(pa->e_tag) {
375 case ACL_USER_OBJ:
376 pa->e_perm = (mode & S_IRWXU) >> 6;
377 break;
378
379 case ACL_USER:
380 case ACL_GROUP:
381 break;
382
383 case ACL_GROUP_OBJ:
384 group_obj = pa;
385 break;
386
387 case ACL_MASK:
388 mask_obj = pa;
389 break;
390
391 case ACL_OTHER:
392 pa->e_perm = (mode & S_IRWXO);
393 break;
394
395 default:
396 return -EIO;
397 }
398 }
399
400 if (mask_obj) {
401 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
402 } else {
403 if (!group_obj)
404 return -EIO;
405 group_obj->e_perm = (mode & S_IRWXG) >> 3;
406 }
407
408 return 0;
409}
Al Virobc26ab52011-07-23 00:18:02 -0400410
411int
Al Virod3fb6122011-07-23 18:37:50 -0400412posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
Al Viro826cae22011-07-23 03:10:32 -0400413{
414 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
415 int err = -ENOMEM;
416 if (clone) {
417 err = posix_acl_create_masq(clone, mode_p);
418 if (err < 0) {
419 posix_acl_release(clone);
420 clone = NULL;
421 }
422 }
423 posix_acl_release(*acl);
424 *acl = clone;
425 return err;
426}
427EXPORT_SYMBOL(posix_acl_create);
428
429int
Al Viro86bc7042011-07-23 19:03:11 -0400430posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
Al Virobc26ab52011-07-23 00:18:02 -0400431{
432 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
433 int err = -ENOMEM;
434 if (clone) {
435 err = posix_acl_chmod_masq(clone, mode);
436 if (err) {
437 posix_acl_release(clone);
438 clone = NULL;
439 }
440 }
441 posix_acl_release(*acl);
442 *acl = clone;
443 return err;
444}
445EXPORT_SYMBOL(posix_acl_chmod);
Christoph Hellwig5c8ebd52013-12-20 05:16:37 -0800446
447/*
448 * Fix up the uids and gids in posix acl extended attributes in place.
449 */
450static void posix_acl_fix_xattr_userns(
451 struct user_namespace *to, struct user_namespace *from,
452 void *value, size_t size)
453{
454 posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
455 posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
456 int count;
457 kuid_t uid;
458 kgid_t gid;
459
460 if (!value)
461 return;
462 if (size < sizeof(posix_acl_xattr_header))
463 return;
464 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
465 return;
466
467 count = posix_acl_xattr_count(size);
468 if (count < 0)
469 return;
470 if (count == 0)
471 return;
472
473 for (end = entry + count; entry != end; entry++) {
474 switch(le16_to_cpu(entry->e_tag)) {
475 case ACL_USER:
476 uid = make_kuid(from, le32_to_cpu(entry->e_id));
477 entry->e_id = cpu_to_le32(from_kuid(to, uid));
478 break;
479 case ACL_GROUP:
480 gid = make_kgid(from, le32_to_cpu(entry->e_id));
481 entry->e_id = cpu_to_le32(from_kgid(to, gid));
482 break;
483 default:
484 break;
485 }
486 }
487}
488
489void posix_acl_fix_xattr_from_user(void *value, size_t size)
490{
491 struct user_namespace *user_ns = current_user_ns();
492 if (user_ns == &init_user_ns)
493 return;
494 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
495}
496
497void posix_acl_fix_xattr_to_user(void *value, size_t size)
498{
499 struct user_namespace *user_ns = current_user_ns();
500 if (user_ns == &init_user_ns)
501 return;
502 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
503}
504
505/*
506 * Convert from extended attribute to in-memory representation.
507 */
508struct posix_acl *
509posix_acl_from_xattr(struct user_namespace *user_ns,
510 const void *value, size_t size)
511{
512 posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
513 posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
514 int count;
515 struct posix_acl *acl;
516 struct posix_acl_entry *acl_e;
517
518 if (!value)
519 return NULL;
520 if (size < sizeof(posix_acl_xattr_header))
521 return ERR_PTR(-EINVAL);
522 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
523 return ERR_PTR(-EOPNOTSUPP);
524
525 count = posix_acl_xattr_count(size);
526 if (count < 0)
527 return ERR_PTR(-EINVAL);
528 if (count == 0)
529 return NULL;
530
531 acl = posix_acl_alloc(count, GFP_NOFS);
532 if (!acl)
533 return ERR_PTR(-ENOMEM);
534 acl_e = acl->a_entries;
535
536 for (end = entry + count; entry != end; acl_e++, entry++) {
537 acl_e->e_tag = le16_to_cpu(entry->e_tag);
538 acl_e->e_perm = le16_to_cpu(entry->e_perm);
539
540 switch(acl_e->e_tag) {
541 case ACL_USER_OBJ:
542 case ACL_GROUP_OBJ:
543 case ACL_MASK:
544 case ACL_OTHER:
545 break;
546
547 case ACL_USER:
548 acl_e->e_uid =
549 make_kuid(user_ns,
550 le32_to_cpu(entry->e_id));
551 if (!uid_valid(acl_e->e_uid))
552 goto fail;
553 break;
554 case ACL_GROUP:
555 acl_e->e_gid =
556 make_kgid(user_ns,
557 le32_to_cpu(entry->e_id));
558 if (!gid_valid(acl_e->e_gid))
559 goto fail;
560 break;
561
562 default:
563 goto fail;
564 }
565 }
566 return acl;
567
568fail:
569 posix_acl_release(acl);
570 return ERR_PTR(-EINVAL);
571}
572EXPORT_SYMBOL (posix_acl_from_xattr);
573
574/*
575 * Convert from in-memory to extended attribute representation.
576 */
577int
578posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
579 void *buffer, size_t size)
580{
581 posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
582 posix_acl_xattr_entry *ext_entry = ext_acl->a_entries;
583 int real_size, n;
584
585 real_size = posix_acl_xattr_size(acl->a_count);
586 if (!buffer)
587 return real_size;
588 if (real_size > size)
589 return -ERANGE;
590
591 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
592
593 for (n=0; n < acl->a_count; n++, ext_entry++) {
594 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
595 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
596 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
597 switch(acl_e->e_tag) {
598 case ACL_USER:
599 ext_entry->e_id =
600 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
601 break;
602 case ACL_GROUP:
603 ext_entry->e_id =
604 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
605 break;
606 default:
607 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
608 break;
609 }
610 }
611 return real_size;
612}
613EXPORT_SYMBOL (posix_acl_to_xattr);