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 <linux/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_valid
);
28 EXPORT_SYMBOL(posix_acl_equiv_mode
);
29 EXPORT_SYMBOL(posix_acl_from_mode
);
32 * Init a fresh posix_acl
35 posix_acl_init(struct posix_acl
*acl
, int count
)
37 atomic_set(&acl
->a_refcount
, 1);
42 * Allocate a new ACL with the specified number of entries.
45 posix_acl_alloc(int count
, gfp_t flags
)
47 const size_t size
= sizeof(struct posix_acl
) +
48 count
* sizeof(struct posix_acl_entry
);
49 struct posix_acl
*acl
= kmalloc(size
, flags
);
51 posix_acl_init(acl
, count
);
58 static struct posix_acl
*
59 posix_acl_clone(const struct posix_acl
*acl
, gfp_t flags
)
61 struct posix_acl
*clone
= NULL
;
64 int size
= sizeof(struct posix_acl
) + acl
->a_count
*
65 sizeof(struct posix_acl_entry
);
66 clone
= kmemdup(acl
, size
, flags
);
68 atomic_set(&clone
->a_refcount
, 1);
74 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
77 posix_acl_valid(const struct posix_acl
*acl
)
79 const struct posix_acl_entry
*pa
, *pe
;
80 int state
= ACL_USER_OBJ
;
81 unsigned int id
= 0; /* keep gcc happy */
84 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
85 if (pa
->e_perm
& ~(ACL_READ
|ACL_WRITE
|ACL_EXECUTE
))
89 if (state
== ACL_USER_OBJ
) {
97 if (state
!= ACL_USER
)
99 if (pa
->e_id
== ACL_UNDEFINED_ID
||
107 if (state
== ACL_USER
) {
115 if (state
!= ACL_GROUP
)
117 if (pa
->e_id
== ACL_UNDEFINED_ID
||
125 if (state
!= ACL_GROUP
)
131 if (state
== ACL_OTHER
||
132 (state
== ACL_GROUP
&& !needs_mask
)) {
148 * Returns 0 if the acl can be exactly represented in the traditional
149 * file mode permission bits, or else 1. Returns -E... on error.
152 posix_acl_equiv_mode(const struct posix_acl
*acl
, mode_t
*mode_p
)
154 const struct posix_acl_entry
*pa
, *pe
;
158 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
161 mode
|= (pa
->e_perm
& S_IRWXO
) << 6;
164 mode
|= (pa
->e_perm
& S_IRWXO
) << 3;
167 mode
|= pa
->e_perm
& S_IRWXO
;
170 mode
= (mode
& ~S_IRWXG
) |
171 ((pa
->e_perm
& S_IRWXO
) << 3);
183 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
188 * Create an ACL representing the file mode permission bits of an inode.
191 posix_acl_from_mode(mode_t mode
, gfp_t flags
)
193 struct posix_acl
*acl
= posix_acl_alloc(3, flags
);
195 return ERR_PTR(-ENOMEM
);
197 acl
->a_entries
[0].e_tag
= ACL_USER_OBJ
;
198 acl
->a_entries
[0].e_id
= ACL_UNDEFINED_ID
;
199 acl
->a_entries
[0].e_perm
= (mode
& S_IRWXU
) >> 6;
201 acl
->a_entries
[1].e_tag
= ACL_GROUP_OBJ
;
202 acl
->a_entries
[1].e_id
= ACL_UNDEFINED_ID
;
203 acl
->a_entries
[1].e_perm
= (mode
& S_IRWXG
) >> 3;
205 acl
->a_entries
[2].e_tag
= ACL_OTHER
;
206 acl
->a_entries
[2].e_id
= ACL_UNDEFINED_ID
;
207 acl
->a_entries
[2].e_perm
= (mode
& S_IRWXO
);
212 * Return 0 if current is granted want access to the inode
213 * by the acl. Returns -E... otherwise.
216 posix_acl_permission(struct inode
*inode
, const struct posix_acl
*acl
, int want
)
218 const struct posix_acl_entry
*pa
, *pe
, *mask_obj
;
221 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
224 /* (May have been checked already) */
225 if (inode
->i_uid
== current_fsuid())
229 if (pa
->e_id
== current_fsuid())
233 if (in_group_p(inode
->i_gid
)) {
235 if ((pa
->e_perm
& want
) == want
)
240 if (in_group_p(pa
->e_id
)) {
242 if ((pa
->e_perm
& want
) == want
)
260 for (mask_obj
= pa
+1; mask_obj
!= pe
; mask_obj
++) {
261 if (mask_obj
->e_tag
== ACL_MASK
) {
262 if ((pa
->e_perm
& mask_obj
->e_perm
& want
) == want
)
269 if ((pa
->e_perm
& want
) == want
)
275 * Modify acl when creating a new inode. The caller must ensure the acl is
276 * only referenced once.
278 * mode_p initially must contain the mode parameter to the open() / creat()
279 * system calls. All permissions that are not granted by the acl are removed.
280 * The permissions in the acl are changed to reflect the mode_p parameter.
282 static int posix_acl_create_masq(struct posix_acl
*acl
, mode_t
*mode_p
)
284 struct posix_acl_entry
*pa
, *pe
;
285 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
286 mode_t mode
= *mode_p
;
289 /* assert(atomic_read(acl->a_refcount) == 1); */
291 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
294 pa
->e_perm
&= (mode
>> 6) | ~S_IRWXO
;
295 mode
&= (pa
->e_perm
<< 6) | ~S_IRWXU
;
308 pa
->e_perm
&= mode
| ~S_IRWXO
;
309 mode
&= pa
->e_perm
| ~S_IRWXO
;
323 mask_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
324 mode
&= (mask_obj
->e_perm
<< 3) | ~S_IRWXG
;
328 group_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
329 mode
&= (group_obj
->e_perm
<< 3) | ~S_IRWXG
;
332 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
337 * Modify the ACL for the chmod syscall.
339 static int posix_acl_chmod_masq(struct posix_acl
*acl
, mode_t mode
)
341 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
342 struct posix_acl_entry
*pa
, *pe
;
344 /* assert(atomic_read(acl->a_refcount) == 1); */
346 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
349 pa
->e_perm
= (mode
& S_IRWXU
) >> 6;
365 pa
->e_perm
= (mode
& S_IRWXO
);
374 mask_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
378 group_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
385 posix_acl_create(struct posix_acl
**acl
, gfp_t gfp
, mode_t
*mode_p
)
387 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
390 err
= posix_acl_create_masq(clone
, mode_p
);
392 posix_acl_release(clone
);
396 posix_acl_release(*acl
);
400 EXPORT_SYMBOL(posix_acl_create
);
403 posix_acl_chmod(struct posix_acl
**acl
, gfp_t gfp
, mode_t mode
)
405 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
408 err
= posix_acl_chmod_masq(clone
, mode
);
410 posix_acl_release(clone
);
414 posix_acl_release(*acl
);
418 EXPORT_SYMBOL(posix_acl_chmod
);