Linux 3.4.102
[linux/fpc-iii.git] / fs / posix_acl.c
blob64496020bf192460f1668dd2df8b1a2f66c14f18
1 /*
2 * linux/fs/posix_acl.c
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>).
8 */
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>
18 #include <linux/fs.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
34 void
35 posix_acl_init(struct posix_acl *acl, int count)
37 atomic_set(&acl->a_refcount, 1);
38 acl->a_count = count;
42 * Allocate a new ACL with the specified number of entries.
44 struct posix_acl *
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);
50 if (acl)
51 posix_acl_init(acl, count);
52 return acl;
56 * Clone an ACL.
58 static struct posix_acl *
59 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
61 struct posix_acl *clone = NULL;
63 if (acl) {
64 int size = sizeof(struct posix_acl) + acl->a_count *
65 sizeof(struct posix_acl_entry);
66 clone = kmemdup(acl, size, flags);
67 if (clone)
68 atomic_set(&clone->a_refcount, 1);
70 return clone;
74 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
76 int
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 */
82 int needs_mask = 0;
84 FOREACH_ACL_ENTRY(pa, acl, pe) {
85 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
86 return -EINVAL;
87 switch (pa->e_tag) {
88 case ACL_USER_OBJ:
89 if (state == ACL_USER_OBJ) {
90 id = 0;
91 state = ACL_USER;
92 break;
94 return -EINVAL;
96 case ACL_USER:
97 if (state != ACL_USER)
98 return -EINVAL;
99 if (pa->e_id == ACL_UNDEFINED_ID ||
100 pa->e_id < id)
101 return -EINVAL;
102 id = pa->e_id + 1;
103 needs_mask = 1;
104 break;
106 case ACL_GROUP_OBJ:
107 if (state == ACL_USER) {
108 id = 0;
109 state = ACL_GROUP;
110 break;
112 return -EINVAL;
114 case ACL_GROUP:
115 if (state != ACL_GROUP)
116 return -EINVAL;
117 if (pa->e_id == ACL_UNDEFINED_ID ||
118 pa->e_id < id)
119 return -EINVAL;
120 id = pa->e_id + 1;
121 needs_mask = 1;
122 break;
124 case ACL_MASK:
125 if (state != ACL_GROUP)
126 return -EINVAL;
127 state = ACL_OTHER;
128 break;
130 case ACL_OTHER:
131 if (state == ACL_OTHER ||
132 (state == ACL_GROUP && !needs_mask)) {
133 state = 0;
134 break;
136 return -EINVAL;
138 default:
139 return -EINVAL;
142 if (state == 0)
143 return 0;
144 return -EINVAL;
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, umode_t *mode_p)
154 const struct posix_acl_entry *pa, *pe;
155 umode_t mode = 0;
156 int not_equiv = 0;
159 * A null ACL can always be presented as mode bits.
161 if (!acl)
162 return 0;
164 FOREACH_ACL_ENTRY(pa, acl, pe) {
165 switch (pa->e_tag) {
166 case ACL_USER_OBJ:
167 mode |= (pa->e_perm & S_IRWXO) << 6;
168 break;
169 case ACL_GROUP_OBJ:
170 mode |= (pa->e_perm & S_IRWXO) << 3;
171 break;
172 case ACL_OTHER:
173 mode |= pa->e_perm & S_IRWXO;
174 break;
175 case ACL_MASK:
176 mode = (mode & ~S_IRWXG) |
177 ((pa->e_perm & S_IRWXO) << 3);
178 not_equiv = 1;
179 break;
180 case ACL_USER:
181 case ACL_GROUP:
182 not_equiv = 1;
183 break;
184 default:
185 return -EINVAL;
188 if (mode_p)
189 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
190 return not_equiv;
194 * Create an ACL representing the file mode permission bits of an inode.
196 struct posix_acl *
197 posix_acl_from_mode(umode_t mode, gfp_t flags)
199 struct posix_acl *acl = posix_acl_alloc(3, flags);
200 if (!acl)
201 return ERR_PTR(-ENOMEM);
203 acl->a_entries[0].e_tag = ACL_USER_OBJ;
204 acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
205 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
207 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
208 acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
209 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
211 acl->a_entries[2].e_tag = ACL_OTHER;
212 acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
213 acl->a_entries[2].e_perm = (mode & S_IRWXO);
214 return acl;
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;
225 int found = 0;
227 want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
229 FOREACH_ACL_ENTRY(pa, acl, pe) {
230 switch(pa->e_tag) {
231 case ACL_USER_OBJ:
232 /* (May have been checked already) */
233 if (inode->i_uid == current_fsuid())
234 goto check_perm;
235 break;
236 case ACL_USER:
237 if (pa->e_id == current_fsuid())
238 goto mask;
239 break;
240 case ACL_GROUP_OBJ:
241 if (in_group_p(inode->i_gid)) {
242 found = 1;
243 if ((pa->e_perm & want) == want)
244 goto mask;
246 break;
247 case ACL_GROUP:
248 if (in_group_p(pa->e_id)) {
249 found = 1;
250 if ((pa->e_perm & want) == want)
251 goto mask;
253 break;
254 case ACL_MASK:
255 break;
256 case ACL_OTHER:
257 if (found)
258 return -EACCES;
259 else
260 goto check_perm;
261 default:
262 return -EIO;
265 return -EIO;
267 mask:
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)
271 return 0;
272 return -EACCES;
276 check_perm:
277 if ((pa->e_perm & want) == want)
278 return 0;
279 return -EACCES;
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;
295 int not_equiv = 0;
297 /* assert(atomic_read(acl->a_refcount) == 1); */
299 FOREACH_ACL_ENTRY(pa, acl, pe) {
300 switch(pa->e_tag) {
301 case ACL_USER_OBJ:
302 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
303 mode &= (pa->e_perm << 6) | ~S_IRWXU;
304 break;
306 case ACL_USER:
307 case ACL_GROUP:
308 not_equiv = 1;
309 break;
311 case ACL_GROUP_OBJ:
312 group_obj = pa;
313 break;
315 case ACL_OTHER:
316 pa->e_perm &= mode | ~S_IRWXO;
317 mode &= pa->e_perm | ~S_IRWXO;
318 break;
320 case ACL_MASK:
321 mask_obj = pa;
322 not_equiv = 1;
323 break;
325 default:
326 return -EIO;
330 if (mask_obj) {
331 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
332 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
333 } else {
334 if (!group_obj)
335 return -EIO;
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;
341 return not_equiv;
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) {
355 switch(pa->e_tag) {
356 case ACL_USER_OBJ:
357 pa->e_perm = (mode & S_IRWXU) >> 6;
358 break;
360 case ACL_USER:
361 case ACL_GROUP:
362 break;
364 case ACL_GROUP_OBJ:
365 group_obj = pa;
366 break;
368 case ACL_MASK:
369 mask_obj = pa;
370 break;
372 case ACL_OTHER:
373 pa->e_perm = (mode & S_IRWXO);
374 break;
376 default:
377 return -EIO;
381 if (mask_obj) {
382 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
383 } else {
384 if (!group_obj)
385 return -EIO;
386 group_obj->e_perm = (mode & S_IRWXG) >> 3;
389 return 0;
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);
396 int err = -ENOMEM;
397 if (clone) {
398 err = posix_acl_create_masq(clone, mode_p);
399 if (err < 0) {
400 posix_acl_release(clone);
401 clone = NULL;
404 posix_acl_release(*acl);
405 *acl = clone;
406 return err;
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);
414 int err = -ENOMEM;
415 if (clone) {
416 err = posix_acl_chmod_masq(clone, mode);
417 if (err) {
418 posix_acl_release(clone);
419 clone = NULL;
422 posix_acl_release(*acl);
423 *acl = clone;
424 return err;
426 EXPORT_SYMBOL(posix_acl_chmod);