4 * Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org>
6 * Fixes from William Schumacher incorporated on 15 March 2001.
7 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
11 * This file contains generic functions for manipulating
12 * POSIX 1003.1e draft standard 17 ACLs.
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <asm/atomic.h>
19 #include <linux/sched.h>
20 #include <linux/posix_acl.h>
21 #include <linux/module.h>
23 #include <linux/errno.h>
25 EXPORT_SYMBOL(posix_acl_init
);
26 EXPORT_SYMBOL(posix_acl_alloc
);
27 EXPORT_SYMBOL(posix_acl_clone
);
28 EXPORT_SYMBOL(posix_acl_valid
);
29 EXPORT_SYMBOL(posix_acl_equiv_mode
);
30 EXPORT_SYMBOL(posix_acl_from_mode
);
31 EXPORT_SYMBOL(posix_acl_create_masq
);
32 EXPORT_SYMBOL(posix_acl_chmod_masq
);
33 EXPORT_SYMBOL(posix_acl_permission
);
36 * Init a fresh posix_acl
39 posix_acl_init(struct posix_acl
*acl
, int count
)
41 atomic_set(&acl
->a_refcount
, 1);
46 * Allocate a new ACL with the specified number of entries.
49 posix_acl_alloc(int count
, gfp_t flags
)
51 const size_t size
= sizeof(struct posix_acl
) +
52 count
* sizeof(struct posix_acl_entry
);
53 struct posix_acl
*acl
= kmalloc(size
, flags
);
55 posix_acl_init(acl
, count
);
63 posix_acl_clone(const struct posix_acl
*acl
, gfp_t flags
)
65 struct posix_acl
*clone
= NULL
;
68 int size
= sizeof(struct posix_acl
) + acl
->a_count
*
69 sizeof(struct posix_acl_entry
);
70 clone
= kmemdup(acl
, size
, flags
);
72 atomic_set(&clone
->a_refcount
, 1);
78 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
81 posix_acl_valid(const struct posix_acl
*acl
)
83 const struct posix_acl_entry
*pa
, *pe
;
84 int state
= ACL_USER_OBJ
;
85 unsigned int id
= 0; /* keep gcc happy */
88 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
89 if (pa
->e_perm
& ~(ACL_READ
|ACL_WRITE
|ACL_EXECUTE
))
93 if (state
== ACL_USER_OBJ
) {
101 if (state
!= ACL_USER
)
103 if (pa
->e_id
== ACL_UNDEFINED_ID
||
111 if (state
== ACL_USER
) {
119 if (state
!= ACL_GROUP
)
121 if (pa
->e_id
== ACL_UNDEFINED_ID
||
129 if (state
!= ACL_GROUP
)
135 if (state
== ACL_OTHER
||
136 (state
== ACL_GROUP
&& !needs_mask
)) {
152 * Returns 0 if the acl can be exactly represented in the traditional
153 * file mode permission bits, or else 1. Returns -E... on error.
156 posix_acl_equiv_mode(const struct posix_acl
*acl
, mode_t
*mode_p
)
158 const struct posix_acl_entry
*pa
, *pe
;
162 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
165 mode
|= (pa
->e_perm
& S_IRWXO
) << 6;
168 mode
|= (pa
->e_perm
& S_IRWXO
) << 3;
171 mode
|= pa
->e_perm
& S_IRWXO
;
174 mode
= (mode
& ~S_IRWXG
) |
175 ((pa
->e_perm
& S_IRWXO
) << 3);
187 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
192 * Create an ACL representing the file mode permission bits of an inode.
195 posix_acl_from_mode(mode_t mode
, gfp_t flags
)
197 struct posix_acl
*acl
= posix_acl_alloc(3, flags
);
199 return ERR_PTR(-ENOMEM
);
201 acl
->a_entries
[0].e_tag
= ACL_USER_OBJ
;
202 acl
->a_entries
[0].e_id
= ACL_UNDEFINED_ID
;
203 acl
->a_entries
[0].e_perm
= (mode
& S_IRWXU
) >> 6;
205 acl
->a_entries
[1].e_tag
= ACL_GROUP_OBJ
;
206 acl
->a_entries
[1].e_id
= ACL_UNDEFINED_ID
;
207 acl
->a_entries
[1].e_perm
= (mode
& S_IRWXG
) >> 3;
209 acl
->a_entries
[2].e_tag
= ACL_OTHER
;
210 acl
->a_entries
[2].e_id
= ACL_UNDEFINED_ID
;
211 acl
->a_entries
[2].e_perm
= (mode
& S_IRWXO
);
216 * Return 0 if current is granted want access to the inode
217 * by the acl. Returns -E... otherwise.
220 posix_acl_permission(struct inode
*inode
, const struct posix_acl
*acl
, int want
)
222 const struct posix_acl_entry
*pa
, *pe
, *mask_obj
;
225 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
228 /* (May have been checked already) */
229 if (inode
->i_uid
== current_fsuid())
233 if (pa
->e_id
== current_fsuid())
237 if (in_group_p(inode
->i_gid
)) {
239 if ((pa
->e_perm
& want
) == want
)
244 if (in_group_p(pa
->e_id
)) {
246 if ((pa
->e_perm
& want
) == want
)
264 for (mask_obj
= pa
+1; mask_obj
!= pe
; mask_obj
++) {
265 if (mask_obj
->e_tag
== ACL_MASK
) {
266 if ((pa
->e_perm
& mask_obj
->e_perm
& want
) == want
)
273 if ((pa
->e_perm
& want
) == want
)
279 * Modify acl when creating a new inode. The caller must ensure the acl is
280 * only referenced once.
282 * mode_p initially must contain the mode parameter to the open() / creat()
283 * system calls. All permissions that are not granted by the acl are removed.
284 * The permissions in the acl are changed to reflect the mode_p parameter.
287 posix_acl_create_masq(struct posix_acl
*acl
, mode_t
*mode_p
)
289 struct posix_acl_entry
*pa
, *pe
;
290 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
291 mode_t mode
= *mode_p
;
294 /* assert(atomic_read(acl->a_refcount) == 1); */
296 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
299 pa
->e_perm
&= (mode
>> 6) | ~S_IRWXO
;
300 mode
&= (pa
->e_perm
<< 6) | ~S_IRWXU
;
313 pa
->e_perm
&= mode
| ~S_IRWXO
;
314 mode
&= pa
->e_perm
| ~S_IRWXO
;
328 mask_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
329 mode
&= (mask_obj
->e_perm
<< 3) | ~S_IRWXG
;
333 group_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
334 mode
&= (group_obj
->e_perm
<< 3) | ~S_IRWXG
;
337 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
342 * Modify the ACL for the chmod syscall.
345 posix_acl_chmod_masq(struct posix_acl
*acl
, mode_t mode
)
347 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
348 struct posix_acl_entry
*pa
, *pe
;
350 /* assert(atomic_read(acl->a_refcount) == 1); */
352 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
355 pa
->e_perm
= (mode
& S_IRWXU
) >> 6;
371 pa
->e_perm
= (mode
& S_IRWXO
);
380 mask_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
384 group_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;