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, int 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, int 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 = kmalloc(size, flags);
63 memcpy(clone, acl, size);
64 atomic_set(&clone->a_refcount, 1);
71 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
74 posix_acl_valid(const struct posix_acl *acl)
76 const struct posix_acl_entry *pa, *pe;
77 int state = ACL_USER_OBJ;
78 unsigned int id = 0; /* keep gcc happy */
81 FOREACH_ACL_ENTRY(pa, acl, pe) {
82 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
86 if (state == ACL_USER_OBJ) {
94 if (state != ACL_USER)
96 if (pa->e_id == ACL_UNDEFINED_ID ||
104 if (state == ACL_USER) {
112 if (state != ACL_GROUP)
114 if (pa->e_id == ACL_UNDEFINED_ID ||
122 if (state != ACL_GROUP)
128 if (state == ACL_OTHER ||
129 (state == ACL_GROUP && !needs_mask)) {
145 * Returns 0 if the acl can be exactly represented in the traditional
146 * file mode permission bits, or else 1. Returns -E... on error.
149 posix_acl_equiv_mode(const struct posix_acl *acl, mode_t *mode_p)
151 const struct posix_acl_entry *pa, *pe;
155 FOREACH_ACL_ENTRY(pa, acl, pe) {
158 mode |= (pa->e_perm & S_IRWXO) << 6;
161 mode |= (pa->e_perm & S_IRWXO) << 3;
164 mode |= pa->e_perm & S_IRWXO;
167 mode = (mode & ~S_IRWXG) |
168 ((pa->e_perm & S_IRWXO) << 3);
180 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
185 * Create an ACL representing the file mode permission bits of an inode.
188 posix_acl_from_mode(mode_t mode, int flags)
190 struct posix_acl *acl = posix_acl_alloc(3, flags);
192 return ERR_PTR(-ENOMEM);
194 acl->a_entries[0].e_tag = ACL_USER_OBJ;
195 acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
196 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
198 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
199 acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
200 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
202 acl->a_entries[2].e_tag = ACL_OTHER;
203 acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
204 acl->a_entries[2].e_perm = (mode & S_IRWXO);
209 * Return 0 if current is granted want access to the inode
210 * by the acl. Returns -E... otherwise.
213 posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
215 const struct posix_acl_entry *pa, *pe, *mask_obj;
218 /* Prevent vservers from escaping chroot() barriers */
219 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
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.
284 posix_acl_create_masq(struct posix_acl *acl, mode_t *mode_p)
286 struct posix_acl_entry *pa, *pe;
287 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
288 mode_t mode = *mode_p;
291 /* assert(atomic_read(acl->a_refcount) == 1); */
293 FOREACH_ACL_ENTRY(pa, acl, pe) {
296 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
297 mode &= (pa->e_perm << 6) | ~S_IRWXU;
310 pa->e_perm &= mode | ~S_IRWXO;
311 mode &= pa->e_perm | ~S_IRWXO;
325 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
326 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
330 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
331 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
334 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
339 * Modify the ACL for the chmod syscall.
342 posix_acl_chmod_masq(struct posix_acl *acl, mode_t mode)
344 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
345 struct posix_acl_entry *pa, *pe;
347 /* assert(atomic_read(acl->a_refcount) == 1); */
349 FOREACH_ACL_ENTRY(pa, acl, pe) {
352 pa->e_perm = (mode & S_IRWXU) >> 6;
368 pa->e_perm = (mode & S_IRWXO);
377 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
381 group_obj->e_perm = (mode & S_IRWXG) >> 3;