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/export.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 kuid_t prev_uid = INVALID_UID;
82 kgid_t prev_gid = INVALID_GID;
85 FOREACH_ACL_ENTRY(pa, acl, pe) {
86 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
90 if (state == ACL_USER_OBJ) {
97 if (state != ACL_USER)
99 if (!uid_valid(pa->e_uid))
101 if (uid_valid(prev_uid) &&
102 uid_lte(pa->e_uid, prev_uid))
104 prev_uid = pa->e_uid;
109 if (state == ACL_USER) {
116 if (state != ACL_GROUP)
118 if (!gid_valid(pa->e_gid))
120 if (gid_valid(prev_gid) &&
121 gid_lte(pa->e_gid, prev_gid))
123 prev_gid = pa->e_gid;
128 if (state != ACL_GROUP)
134 if (state == ACL_OTHER ||
135 (state == ACL_GROUP && !needs_mask)) {
151 * Returns 0 if the acl can be exactly represented in the traditional
152 * file mode permission bits, or else 1. Returns -E... on error.
155 posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
157 const struct posix_acl_entry *pa, *pe;
162 * A null ACL can always be presented as mode bits.
167 FOREACH_ACL_ENTRY(pa, acl, pe) {
170 mode |= (pa->e_perm & S_IRWXO) << 6;
173 mode |= (pa->e_perm & S_IRWXO) << 3;
176 mode |= pa->e_perm & S_IRWXO;
179 mode = (mode & ~S_IRWXG) |
180 ((pa->e_perm & S_IRWXO) << 3);
192 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
197 * Create an ACL representing the file mode permission bits of an inode.
200 posix_acl_from_mode(umode_t mode, gfp_t flags)
202 struct posix_acl *acl = posix_acl_alloc(3, flags);
204 return ERR_PTR(-ENOMEM);
206 acl->a_entries[0].e_tag = ACL_USER_OBJ;
207 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
209 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
210 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
212 acl->a_entries[2].e_tag = ACL_OTHER;
213 acl->a_entries[2].e_perm = (mode & S_IRWXO);
218 * Return 0 if current is granted want access to the inode
219 * by the acl. Returns -E... otherwise.
222 posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
224 const struct posix_acl_entry *pa, *pe, *mask_obj;
227 want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
229 FOREACH_ACL_ENTRY(pa, acl, pe) {
232 /* (May have been checked already) */
233 if (uid_eq(inode->i_uid, current_fsuid()))
237 if (uid_eq(pa->e_uid, current_fsuid()))
241 if (in_group_p(inode->i_gid)) {
243 if ((pa->e_perm & want) == want)
248 if (in_group_p(pa->e_gid)) {
250 if ((pa->e_perm & want) == want)
268 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
269 if (mask_obj->e_tag == ACL_MASK) {
270 if ((pa->e_perm & mask_obj->e_perm & want) == want)
277 if ((pa->e_perm & want) == want)
283 * Modify acl when creating a new inode. The caller must ensure the acl is
284 * only referenced once.
286 * mode_p initially must contain the mode parameter to the open() / creat()
287 * system calls. All permissions that are not granted by the acl are removed.
288 * The permissions in the acl are changed to reflect the mode_p parameter.
290 static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
292 struct posix_acl_entry *pa, *pe;
293 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
294 umode_t mode = *mode_p;
297 /* assert(atomic_read(acl->a_refcount) == 1); */
299 FOREACH_ACL_ENTRY(pa, acl, pe) {
302 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
303 mode &= (pa->e_perm << 6) | ~S_IRWXU;
316 pa->e_perm &= mode | ~S_IRWXO;
317 mode &= pa->e_perm | ~S_IRWXO;
331 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
332 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
336 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
337 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
340 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
345 * Modify the ACL for the chmod syscall.
347 static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
349 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
350 struct posix_acl_entry *pa, *pe;
352 /* assert(atomic_read(acl->a_refcount) == 1); */
354 FOREACH_ACL_ENTRY(pa, acl, pe) {
357 pa->e_perm = (mode & S_IRWXU) >> 6;
373 pa->e_perm = (mode & S_IRWXO);
382 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
386 group_obj->e_perm = (mode & S_IRWXG) >> 3;
393 posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
395 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
398 err = posix_acl_create_masq(clone, mode_p);
400 posix_acl_release(clone);
404 posix_acl_release(*acl);
408 EXPORT_SYMBOL(posix_acl_create);
411 posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
413 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
416 err = posix_acl_chmod_masq(clone, mode);
418 posix_acl_release(clone);
422 posix_acl_release(*acl);
426 EXPORT_SYMBOL(posix_acl_chmod);