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_alloc);
26 EXPORT_SYMBOL(posix_acl_clone);
27 EXPORT_SYMBOL(posix_acl_valid);
28 EXPORT_SYMBOL(posix_acl_equiv_mode);
29 EXPORT_SYMBOL(posix_acl_from_mode);
30 EXPORT_SYMBOL(posix_acl_create_masq);
31 EXPORT_SYMBOL(posix_acl_chmod_masq);
32 EXPORT_SYMBOL(posix_acl_permission);
35 * Allocate a new ACL with the specified number of entries.
38 posix_acl_alloc(int count, gfp_t flags)
40 const size_t size = sizeof(struct posix_acl) +
41 count * sizeof(struct posix_acl_entry);
42 struct posix_acl *acl = kmalloc(size, flags);
44 atomic_set(&acl->a_refcount, 1);
54 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
56 struct posix_acl *clone = NULL;
59 int size = sizeof(struct posix_acl) + acl->a_count *
60 sizeof(struct posix_acl_entry);
61 clone = kmemdup(acl, size, flags);
63 atomic_set(&clone->a_refcount, 1);
69 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
72 posix_acl_valid(const struct posix_acl *acl)
74 const struct posix_acl_entry *pa, *pe;
75 int state = ACL_USER_OBJ;
76 unsigned int id = 0; /* keep gcc happy */
79 FOREACH_ACL_ENTRY(pa, acl, pe) {
80 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
84 if (state == ACL_USER_OBJ) {
92 if (state != ACL_USER)
94 if (pa->e_id == ACL_UNDEFINED_ID ||
102 if (state == ACL_USER) {
110 if (state != ACL_GROUP)
112 if (pa->e_id == ACL_UNDEFINED_ID ||
120 if (state != ACL_GROUP)
126 if (state == ACL_OTHER ||
127 (state == ACL_GROUP && !needs_mask)) {
143 * Returns 0 if the acl can be exactly represented in the traditional
144 * file mode permission bits, or else 1. Returns -E... on error.
147 posix_acl_equiv_mode(const struct posix_acl *acl, mode_t *mode_p)
149 const struct posix_acl_entry *pa, *pe;
153 FOREACH_ACL_ENTRY(pa, acl, pe) {
156 mode |= (pa->e_perm & S_IRWXO) << 6;
159 mode |= (pa->e_perm & S_IRWXO) << 3;
162 mode |= pa->e_perm & S_IRWXO;
165 mode = (mode & ~S_IRWXG) |
166 ((pa->e_perm & S_IRWXO) << 3);
178 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
183 * Create an ACL representing the file mode permission bits of an inode.
186 posix_acl_from_mode(mode_t mode, gfp_t flags)
188 struct posix_acl *acl = posix_acl_alloc(3, flags);
190 return ERR_PTR(-ENOMEM);
192 acl->a_entries[0].e_tag = ACL_USER_OBJ;
193 acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
194 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
196 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
197 acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
198 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
200 acl->a_entries[2].e_tag = ACL_OTHER;
201 acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
202 acl->a_entries[2].e_perm = (mode & S_IRWXO);
207 * Return 0 if current is granted want access to the inode
208 * by the acl. Returns -E... otherwise.
211 posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
213 const struct posix_acl_entry *pa, *pe, *mask_obj;
216 FOREACH_ACL_ENTRY(pa, acl, pe) {
219 /* (May have been checked already) */
220 if (inode->i_uid == current_fsuid())
224 if (pa->e_id == current_fsuid())
228 if (in_group_p(inode->i_gid)) {
230 if ((pa->e_perm & want) == want)
235 if (in_group_p(pa->e_id)) {
237 if ((pa->e_perm & want) == want)
255 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
256 if (mask_obj->e_tag == ACL_MASK) {
257 if ((pa->e_perm & mask_obj->e_perm & want) == want)
264 if ((pa->e_perm & want) == want)
270 * Modify acl when creating a new inode. The caller must ensure the acl is
271 * only referenced once.
273 * mode_p initially must contain the mode parameter to the open() / creat()
274 * system calls. All permissions that are not granted by the acl are removed.
275 * The permissions in the acl are changed to reflect the mode_p parameter.
278 posix_acl_create_masq(struct posix_acl *acl, mode_t *mode_p)
280 struct posix_acl_entry *pa, *pe;
281 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
282 mode_t mode = *mode_p;
285 /* assert(atomic_read(acl->a_refcount) == 1); */
287 FOREACH_ACL_ENTRY(pa, acl, pe) {
290 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
291 mode &= (pa->e_perm << 6) | ~S_IRWXU;
304 pa->e_perm &= mode | ~S_IRWXO;
305 mode &= pa->e_perm | ~S_IRWXO;
319 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
320 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
324 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
325 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
328 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
333 * Modify the ACL for the chmod syscall.
336 posix_acl_chmod_masq(struct posix_acl *acl, mode_t mode)
338 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
339 struct posix_acl_entry *pa, *pe;
341 /* assert(atomic_read(acl->a_refcount) == 1); */
343 FOREACH_ACL_ENTRY(pa, acl, pe) {
346 pa->e_perm = (mode & S_IRWXU) >> 6;
362 pa->e_perm = (mode & S_IRWXO);
371 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
375 group_obj->e_perm = (mode & S_IRWXG) >> 3;