2 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
4 * Fixes from William Schumacher incorporated on 15 March 2001.
5 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
9 * This file contains generic functions for manipulating
10 * POSIX 1003.1e draft standard 17 ACLs.
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/atomic.h>
17 #include <linux/sched.h>
18 #include <linux/posix_acl.h>
19 #include <linux/posix_acl_xattr.h>
20 #include <linux/xattr.h>
21 #include <linux/export.h>
22 #include <linux/user_namespace.h>
24 EXPORT_SYMBOL(posix_acl_init
);
25 EXPORT_SYMBOL(posix_acl_alloc
);
26 EXPORT_SYMBOL(posix_acl_valid
);
27 EXPORT_SYMBOL(posix_acl_equiv_mode
);
28 EXPORT_SYMBOL(posix_acl_from_mode
);
30 struct posix_acl
*get_acl(struct inode
*inode
, int type
)
32 struct posix_acl
*acl
;
34 acl
= get_cached_acl(inode
, type
);
35 if (acl
!= ACL_NOT_CACHED
)
38 if (!IS_POSIXACL(inode
))
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.
46 * If the filesystem doesn't have a get_acl() function at all, we'll
47 * just create the negative cache entry.
49 if (!inode
->i_op
->get_acl
) {
50 set_cached_acl(inode
, type
, NULL
);
53 return inode
->i_op
->get_acl(inode
, type
);
55 EXPORT_SYMBOL(get_acl
);
58 * Init a fresh posix_acl
61 posix_acl_init(struct posix_acl
*acl
, int count
)
63 atomic_set(&acl
->a_refcount
, 1);
68 * Allocate a new ACL with the specified number of entries.
71 posix_acl_alloc(int count
, gfp_t flags
)
73 const size_t size
= sizeof(struct posix_acl
) +
74 count
* sizeof(struct posix_acl_entry
);
75 struct posix_acl
*acl
= kmalloc(size
, flags
);
77 posix_acl_init(acl
, count
);
84 static struct posix_acl
*
85 posix_acl_clone(const struct posix_acl
*acl
, gfp_t flags
)
87 struct posix_acl
*clone
= NULL
;
90 int size
= sizeof(struct posix_acl
) + acl
->a_count
*
91 sizeof(struct posix_acl_entry
);
92 clone
= kmemdup(acl
, size
, flags
);
94 atomic_set(&clone
->a_refcount
, 1);
100 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
103 posix_acl_valid(const struct posix_acl
*acl
)
105 const struct posix_acl_entry
*pa
, *pe
;
106 int state
= ACL_USER_OBJ
;
107 kuid_t prev_uid
= INVALID_UID
;
108 kgid_t prev_gid
= INVALID_GID
;
111 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
112 if (pa
->e_perm
& ~(ACL_READ
|ACL_WRITE
|ACL_EXECUTE
))
116 if (state
== ACL_USER_OBJ
) {
123 if (state
!= ACL_USER
)
125 if (!uid_valid(pa
->e_uid
))
127 if (uid_valid(prev_uid
) &&
128 uid_lte(pa
->e_uid
, prev_uid
))
130 prev_uid
= pa
->e_uid
;
135 if (state
== ACL_USER
) {
142 if (state
!= ACL_GROUP
)
144 if (!gid_valid(pa
->e_gid
))
146 if (gid_valid(prev_gid
) &&
147 gid_lte(pa
->e_gid
, prev_gid
))
149 prev_gid
= pa
->e_gid
;
154 if (state
!= ACL_GROUP
)
160 if (state
== ACL_OTHER
||
161 (state
== ACL_GROUP
&& !needs_mask
)) {
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.
181 posix_acl_equiv_mode(const struct posix_acl
*acl
, umode_t
*mode_p
)
183 const struct posix_acl_entry
*pa
, *pe
;
187 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
190 mode
|= (pa
->e_perm
& S_IRWXO
) << 6;
193 mode
|= (pa
->e_perm
& S_IRWXO
) << 3;
196 mode
|= pa
->e_perm
& S_IRWXO
;
199 mode
= (mode
& ~S_IRWXG
) |
200 ((pa
->e_perm
& S_IRWXO
) << 3);
212 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
217 * Create an ACL representing the file mode permission bits of an inode.
220 posix_acl_from_mode(umode_t mode
, gfp_t flags
)
222 struct posix_acl
*acl
= posix_acl_alloc(3, flags
);
224 return ERR_PTR(-ENOMEM
);
226 acl
->a_entries
[0].e_tag
= ACL_USER_OBJ
;
227 acl
->a_entries
[0].e_perm
= (mode
& S_IRWXU
) >> 6;
229 acl
->a_entries
[1].e_tag
= ACL_GROUP_OBJ
;
230 acl
->a_entries
[1].e_perm
= (mode
& S_IRWXG
) >> 3;
232 acl
->a_entries
[2].e_tag
= ACL_OTHER
;
233 acl
->a_entries
[2].e_perm
= (mode
& S_IRWXO
);
238 * Return 0 if current is granted want access to the inode
239 * by the acl. Returns -E... otherwise.
242 posix_acl_permission(struct inode
*inode
, const struct posix_acl
*acl
, int want
)
244 const struct posix_acl_entry
*pa
, *pe
, *mask_obj
;
247 want
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
| MAY_NOT_BLOCK
;
249 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
252 /* (May have been checked already) */
253 if (uid_eq(inode
->i_uid
, current_fsuid()))
257 if (uid_eq(pa
->e_uid
, current_fsuid()))
261 if (in_group_p(inode
->i_gid
)) {
263 if ((pa
->e_perm
& want
) == want
)
268 if (in_group_p(pa
->e_gid
)) {
270 if ((pa
->e_perm
& want
) == want
)
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
)
297 if ((pa
->e_perm
& want
) == want
)
303 * Modify acl when creating a new inode. The caller must ensure the acl is
304 * only referenced once.
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.
310 static int posix_acl_create_masq(struct posix_acl
*acl
, umode_t
*mode_p
)
312 struct posix_acl_entry
*pa
, *pe
;
313 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
314 umode_t mode
= *mode_p
;
317 /* assert(atomic_read(acl->a_refcount) == 1); */
319 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
322 pa
->e_perm
&= (mode
>> 6) | ~S_IRWXO
;
323 mode
&= (pa
->e_perm
<< 6) | ~S_IRWXU
;
336 pa
->e_perm
&= mode
| ~S_IRWXO
;
337 mode
&= pa
->e_perm
| ~S_IRWXO
;
351 mask_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
352 mode
&= (mask_obj
->e_perm
<< 3) | ~S_IRWXG
;
356 group_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
357 mode
&= (group_obj
->e_perm
<< 3) | ~S_IRWXG
;
360 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
365 * Modify the ACL for the chmod syscall.
367 static int posix_acl_chmod_masq(struct posix_acl
*acl
, umode_t mode
)
369 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
370 struct posix_acl_entry
*pa
, *pe
;
372 /* assert(atomic_read(acl->a_refcount) == 1); */
374 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
377 pa
->e_perm
= (mode
& S_IRWXU
) >> 6;
393 pa
->e_perm
= (mode
& S_IRWXO
);
402 mask_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
406 group_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
413 posix_acl_create(struct posix_acl
**acl
, gfp_t gfp
, umode_t
*mode_p
)
415 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
418 err
= posix_acl_create_masq(clone
, mode_p
);
420 posix_acl_release(clone
);
424 posix_acl_release(*acl
);
428 EXPORT_SYMBOL(posix_acl_create
);
431 posix_acl_chmod(struct posix_acl
**acl
, gfp_t gfp
, umode_t mode
)
433 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
436 err
= posix_acl_chmod_masq(clone
, mode
);
438 posix_acl_release(clone
);
442 posix_acl_release(*acl
);
446 EXPORT_SYMBOL(posix_acl_chmod
);
449 * Fix up the uids and gids in posix acl extended attributes in place.
451 static void posix_acl_fix_xattr_userns(
452 struct user_namespace
*to
, struct user_namespace
*from
,
453 void *value
, size_t size
)
455 posix_acl_xattr_header
*header
= (posix_acl_xattr_header
*)value
;
456 posix_acl_xattr_entry
*entry
= (posix_acl_xattr_entry
*)(header
+1), *end
;
463 if (size
< sizeof(posix_acl_xattr_header
))
465 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
468 count
= posix_acl_xattr_count(size
);
474 for (end
= entry
+ count
; entry
!= end
; entry
++) {
475 switch(le16_to_cpu(entry
->e_tag
)) {
477 uid
= make_kuid(from
, le32_to_cpu(entry
->e_id
));
478 entry
->e_id
= cpu_to_le32(from_kuid(to
, uid
));
481 gid
= make_kgid(from
, le32_to_cpu(entry
->e_id
));
482 entry
->e_id
= cpu_to_le32(from_kgid(to
, gid
));
490 void posix_acl_fix_xattr_from_user(void *value
, size_t size
)
492 struct user_namespace
*user_ns
= current_user_ns();
493 if (user_ns
== &init_user_ns
)
495 posix_acl_fix_xattr_userns(&init_user_ns
, user_ns
, value
, size
);
498 void posix_acl_fix_xattr_to_user(void *value
, size_t size
)
500 struct user_namespace
*user_ns
= current_user_ns();
501 if (user_ns
== &init_user_ns
)
503 posix_acl_fix_xattr_userns(user_ns
, &init_user_ns
, value
, size
);
507 * Convert from extended attribute to in-memory representation.
510 posix_acl_from_xattr(struct user_namespace
*user_ns
,
511 const void *value
, size_t size
)
513 posix_acl_xattr_header
*header
= (posix_acl_xattr_header
*)value
;
514 posix_acl_xattr_entry
*entry
= (posix_acl_xattr_entry
*)(header
+1), *end
;
516 struct posix_acl
*acl
;
517 struct posix_acl_entry
*acl_e
;
521 if (size
< sizeof(posix_acl_xattr_header
))
522 return ERR_PTR(-EINVAL
);
523 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
524 return ERR_PTR(-EOPNOTSUPP
);
526 count
= posix_acl_xattr_count(size
);
528 return ERR_PTR(-EINVAL
);
532 acl
= posix_acl_alloc(count
, GFP_NOFS
);
534 return ERR_PTR(-ENOMEM
);
535 acl_e
= acl
->a_entries
;
537 for (end
= entry
+ count
; entry
!= end
; acl_e
++, entry
++) {
538 acl_e
->e_tag
= le16_to_cpu(entry
->e_tag
);
539 acl_e
->e_perm
= le16_to_cpu(entry
->e_perm
);
541 switch(acl_e
->e_tag
) {
551 le32_to_cpu(entry
->e_id
));
552 if (!uid_valid(acl_e
->e_uid
))
558 le32_to_cpu(entry
->e_id
));
559 if (!gid_valid(acl_e
->e_gid
))
570 posix_acl_release(acl
);
571 return ERR_PTR(-EINVAL
);
573 EXPORT_SYMBOL (posix_acl_from_xattr
);
576 * Convert from in-memory to extended attribute representation.
579 posix_acl_to_xattr(struct user_namespace
*user_ns
, const struct posix_acl
*acl
,
580 void *buffer
, size_t size
)
582 posix_acl_xattr_header
*ext_acl
= (posix_acl_xattr_header
*)buffer
;
583 posix_acl_xattr_entry
*ext_entry
= ext_acl
->a_entries
;
586 real_size
= posix_acl_xattr_size(acl
->a_count
);
589 if (real_size
> size
)
592 ext_acl
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
594 for (n
=0; n
< acl
->a_count
; n
++, ext_entry
++) {
595 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
596 ext_entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
597 ext_entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
598 switch(acl_e
->e_tag
) {
601 cpu_to_le32(from_kuid(user_ns
, acl_e
->e_uid
));
605 cpu_to_le32(from_kgid(user_ns
, acl_e
->e_gid
));
608 ext_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
614 EXPORT_SYMBOL (posix_acl_to_xattr
);
617 posix_acl_xattr_get(struct dentry
*dentry
, const char *name
,
618 void *value
, size_t size
, int type
)
620 struct posix_acl
*acl
;
623 if (!IS_POSIXACL(dentry
->d_inode
))
625 if (S_ISLNK(dentry
->d_inode
->i_mode
))
628 acl
= get_acl(dentry
->d_inode
, type
);
634 error
= posix_acl_to_xattr(&init_user_ns
, acl
, value
, size
);
635 posix_acl_release(acl
);
641 posix_acl_xattr_set(struct dentry
*dentry
, const char *name
,
642 const void *value
, size_t size
, int flags
, int type
)
644 struct inode
*inode
= dentry
->d_inode
;
645 struct posix_acl
*acl
= NULL
;
648 if (!IS_POSIXACL(inode
))
650 if (!inode
->i_op
->set_acl
)
653 if (type
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
))
654 return value
? -EACCES
: 0;
655 if (!inode_owner_or_capable(inode
))
659 acl
= posix_acl_from_xattr(&init_user_ns
, value
, size
);
664 ret
= posix_acl_valid(acl
);
670 ret
= inode
->i_op
->set_acl(inode
, acl
, type
);
672 posix_acl_release(acl
);
677 posix_acl_xattr_list(struct dentry
*dentry
, char *list
, size_t list_size
,
678 const char *name
, size_t name_len
, int type
)
683 if (!IS_POSIXACL(dentry
->d_inode
))
685 if (S_ISLNK(dentry
->d_inode
->i_mode
))
688 if (type
== ACL_TYPE_ACCESS
)
689 xname
= POSIX_ACL_XATTR_ACCESS
;
691 xname
= POSIX_ACL_XATTR_DEFAULT
;
693 size
= strlen(xname
) + 1;
694 if (list
&& size
<= list_size
)
695 memcpy(list
, xname
, size
);
699 const struct xattr_handler posix_acl_access_xattr_handler
= {
700 .prefix
= POSIX_ACL_XATTR_ACCESS
,
701 .flags
= ACL_TYPE_ACCESS
,
702 .list
= posix_acl_xattr_list
,
703 .get
= posix_acl_xattr_get
,
704 .set
= posix_acl_xattr_set
,
706 EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler
);
708 const struct xattr_handler posix_acl_default_xattr_handler
= {
709 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
710 .flags
= ACL_TYPE_DEFAULT
,
711 .list
= posix_acl_xattr_list
,
712 .get
= posix_acl_xattr_get
,
713 .set
= posix_acl_xattr_set
,
715 EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler
);