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_valid);
28 EXPORT_SYMBOL(posix_acl_equiv_mode);
29 EXPORT_SYMBOL(posix_acl_from_mode);
30 EXPORT_SYMBOL(posix_acl_permission);
33 * Init a fresh posix_acl
36 posix_acl_init(struct posix_acl *acl, int count)
38 atomic_set(&acl->a_refcount, 1);
43 * Allocate a new ACL with the specified number of entries.
46 posix_acl_alloc(int count, gfp_t flags)
48 const size_t size = sizeof(struct posix_acl) +
49 count * sizeof(struct posix_acl_entry);
50 struct posix_acl *acl = kmalloc(size, flags);
52 posix_acl_init(acl, count);
59 static struct posix_acl *
60 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
62 struct posix_acl *clone = NULL;
65 int size = sizeof(struct posix_acl) + acl->a_count *
66 sizeof(struct posix_acl_entry);
67 clone = kmemdup(acl, size, flags);
69 atomic_set(&clone->a_refcount, 1);
75 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
78 posix_acl_valid(const struct posix_acl *acl)
80 const struct posix_acl_entry *pa, *pe;
81 int state = ACL_USER_OBJ;
82 unsigned int id = 0; /* keep gcc happy */
85 FOREACH_ACL_ENTRY(pa, acl, pe) {
86 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
90 if (state == ACL_USER_OBJ) {
98 if (state != ACL_USER)
100 if (pa->e_id == ACL_UNDEFINED_ID ||
108 if (state == ACL_USER) {
116 if (state != ACL_GROUP)
118 if (pa->e_id == ACL_UNDEFINED_ID ||
126 if (state != ACL_GROUP)
132 if (state == ACL_OTHER ||
133 (state == ACL_GROUP && !needs_mask)) {
149 * Returns 0 if the acl can be exactly represented in the traditional
150 * file mode permission bits, or else 1. Returns -E... on error.
153 posix_acl_equiv_mode(const struct posix_acl *acl, mode_t *mode_p)
155 const struct posix_acl_entry *pa, *pe;
159 FOREACH_ACL_ENTRY(pa, acl, pe) {
162 mode |= (pa->e_perm & S_IRWXO) << 6;
165 mode |= (pa->e_perm & S_IRWXO) << 3;
168 mode |= pa->e_perm & S_IRWXO;
171 mode = (mode & ~S_IRWXG) |
172 ((pa->e_perm & S_IRWXO) << 3);
184 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
189 * Create an ACL representing the file mode permission bits of an inode.
192 posix_acl_from_mode(mode_t mode, gfp_t flags)
194 struct posix_acl *acl = posix_acl_alloc(3, flags);
196 return ERR_PTR(-ENOMEM);
198 acl->a_entries[0].e_tag = ACL_USER_OBJ;
199 acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
200 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
202 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
203 acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
204 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
206 acl->a_entries[2].e_tag = ACL_OTHER;
207 acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
208 acl->a_entries[2].e_perm = (mode & S_IRWXO);
213 * Return 0 if current is granted want access to the inode
214 * by the acl. Returns -E... otherwise.
217 posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
219 const struct posix_acl_entry *pa, *pe, *mask_obj;
222 FOREACH_ACL_ENTRY(pa, acl, pe) {
225 /* (May have been checked already) */
226 if (inode->i_uid == current_fsuid())
230 if (pa->e_id == current_fsuid())
234 if (in_group_p(inode->i_gid)) {
236 if ((pa->e_perm & want) == want)
241 if (in_group_p(pa->e_id)) {
243 if ((pa->e_perm & want) == want)
261 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
262 if (mask_obj->e_tag == ACL_MASK) {
263 if ((pa->e_perm & mask_obj->e_perm & want) == want)
270 if ((pa->e_perm & want) == want)
276 * Modify acl when creating a new inode. The caller must ensure the acl is
277 * only referenced once.
279 * mode_p initially must contain the mode parameter to the open() / creat()
280 * system calls. All permissions that are not granted by the acl are removed.
281 * The permissions in the acl are changed to reflect the mode_p parameter.
283 static int posix_acl_create_masq(struct posix_acl *acl, mode_t *mode_p)
285 struct posix_acl_entry *pa, *pe;
286 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
287 mode_t mode = *mode_p;
290 /* assert(atomic_read(acl->a_refcount) == 1); */
292 FOREACH_ACL_ENTRY(pa, acl, pe) {
295 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
296 mode &= (pa->e_perm << 6) | ~S_IRWXU;
309 pa->e_perm &= mode | ~S_IRWXO;
310 mode &= pa->e_perm | ~S_IRWXO;
324 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
325 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
329 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
330 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
333 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
338 * Modify the ACL for the chmod syscall.
340 static int posix_acl_chmod_masq(struct posix_acl *acl, mode_t mode)
342 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
343 struct posix_acl_entry *pa, *pe;
345 /* assert(atomic_read(acl->a_refcount) == 1); */
347 FOREACH_ACL_ENTRY(pa, acl, pe) {
350 pa->e_perm = (mode & S_IRWXU) >> 6;
366 pa->e_perm = (mode & S_IRWXO);
375 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
379 group_obj->e_perm = (mode & S_IRWXG) >> 3;
386 posix_acl_create(struct posix_acl **acl, gfp_t gfp, mode_t *mode_p)
388 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
391 err = posix_acl_create_masq(clone, mode_p);
393 posix_acl_release(clone);
397 posix_acl_release(*acl);
401 EXPORT_SYMBOL(posix_acl_create);
404 posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, mode_t mode)
406 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
409 err = posix_acl_chmod_masq(clone, mode);
411 posix_acl_release(clone);
415 posix_acl_release(*acl);
419 EXPORT_SYMBOL(posix_acl_chmod);