bury posix_acl_..._masq() variants
[linux-3.10.git] / fs / posix_acl.c
1 /*
2  * linux/fs/posix_acl.c
3  *
4  *  Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org>
5  *
6  *  Fixes from William Schumacher incorporated on 15 March 2001.
7  *     (Reported by Charles Bertsch, <CBertsch@microtest.com>).
8  */
9
10 /*
11  *  This file contains generic functions for manipulating
12  *  POSIX 1003.1e draft standard 17 ACLs.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <asm/atomic.h>
18 #include <linux/fs.h>
19 #include <linux/sched.h>
20 #include <linux/posix_acl.h>
21 #include <linux/module.h>
22
23 #include <linux/errno.h>
24
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);
31
32 /*
33  * Init a fresh posix_acl
34  */
35 void
36 posix_acl_init(struct posix_acl *acl, int count)
37 {
38         atomic_set(&acl->a_refcount, 1);
39         acl->a_count = count;
40 }
41
42 /*
43  * Allocate a new ACL with the specified number of entries.
44  */
45 struct posix_acl *
46 posix_acl_alloc(int count, gfp_t flags)
47 {
48         const size_t size = sizeof(struct posix_acl) +
49                             count * sizeof(struct posix_acl_entry);
50         struct posix_acl *acl = kmalloc(size, flags);
51         if (acl)
52                 posix_acl_init(acl, count);
53         return acl;
54 }
55
56 /*
57  * Clone an ACL.
58  */
59 static struct posix_acl *
60 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
61 {
62         struct posix_acl *clone = NULL;
63
64         if (acl) {
65                 int size = sizeof(struct posix_acl) + acl->a_count *
66                            sizeof(struct posix_acl_entry);
67                 clone = kmemdup(acl, size, flags);
68                 if (clone)
69                         atomic_set(&clone->a_refcount, 1);
70         }
71         return clone;
72 }
73
74 /*
75  * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
76  */
77 int
78 posix_acl_valid(const struct posix_acl *acl)
79 {
80         const struct posix_acl_entry *pa, *pe;
81         int state = ACL_USER_OBJ;
82         unsigned int id = 0;  /* keep gcc happy */
83         int needs_mask = 0;
84
85         FOREACH_ACL_ENTRY(pa, acl, pe) {
86                 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
87                         return -EINVAL;
88                 switch (pa->e_tag) {
89                         case ACL_USER_OBJ:
90                                 if (state == ACL_USER_OBJ) {
91                                         id = 0;
92                                         state = ACL_USER;
93                                         break;
94                                 }
95                                 return -EINVAL;
96
97                         case ACL_USER:
98                                 if (state != ACL_USER)
99                                         return -EINVAL;
100                                 if (pa->e_id == ACL_UNDEFINED_ID ||
101                                     pa->e_id < id)
102                                         return -EINVAL;
103                                 id = pa->e_id + 1;
104                                 needs_mask = 1;
105                                 break;
106
107                         case ACL_GROUP_OBJ:
108                                 if (state == ACL_USER) {
109                                         id = 0;
110                                         state = ACL_GROUP;
111                                         break;
112                                 }
113                                 return -EINVAL;
114
115                         case ACL_GROUP:
116                                 if (state != ACL_GROUP)
117                                         return -EINVAL;
118                                 if (pa->e_id == ACL_UNDEFINED_ID ||
119                                     pa->e_id < id)
120                                         return -EINVAL;
121                                 id = pa->e_id + 1;
122                                 needs_mask = 1;
123                                 break;
124
125                         case ACL_MASK:
126                                 if (state != ACL_GROUP)
127                                         return -EINVAL;
128                                 state = ACL_OTHER;
129                                 break;
130
131                         case ACL_OTHER:
132                                 if (state == ACL_OTHER ||
133                                     (state == ACL_GROUP && !needs_mask)) {
134                                         state = 0;
135                                         break;
136                                 }
137                                 return -EINVAL;
138
139                         default:
140                                 return -EINVAL;
141                 }
142         }
143         if (state == 0)
144                 return 0;
145         return -EINVAL;
146 }
147
148 /*
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.
151  */
152 int
153 posix_acl_equiv_mode(const struct posix_acl *acl, mode_t *mode_p)
154 {
155         const struct posix_acl_entry *pa, *pe;
156         mode_t mode = 0;
157         int not_equiv = 0;
158
159         FOREACH_ACL_ENTRY(pa, acl, pe) {
160                 switch (pa->e_tag) {
161                         case ACL_USER_OBJ:
162                                 mode |= (pa->e_perm & S_IRWXO) << 6;
163                                 break;
164                         case ACL_GROUP_OBJ:
165                                 mode |= (pa->e_perm & S_IRWXO) << 3;
166                                 break;
167                         case ACL_OTHER:
168                                 mode |= pa->e_perm & S_IRWXO;
169                                 break;
170                         case ACL_MASK:
171                                 mode = (mode & ~S_IRWXG) |
172                                        ((pa->e_perm & S_IRWXO) << 3);
173                                 not_equiv = 1;
174                                 break;
175                         case ACL_USER:
176                         case ACL_GROUP:
177                                 not_equiv = 1;
178                                 break;
179                         default:
180                                 return -EINVAL;
181                 }
182         }
183         if (mode_p)
184                 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
185         return not_equiv;
186 }
187
188 /*
189  * Create an ACL representing the file mode permission bits of an inode.
190  */
191 struct posix_acl *
192 posix_acl_from_mode(mode_t mode, gfp_t flags)
193 {
194         struct posix_acl *acl = posix_acl_alloc(3, flags);
195         if (!acl)
196                 return ERR_PTR(-ENOMEM);
197
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;
201
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;
205
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);
209         return acl;
210 }
211
212 /*
213  * Return 0 if current is granted want access to the inode
214  * by the acl. Returns -E... otherwise.
215  */
216 int
217 posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
218 {
219         const struct posix_acl_entry *pa, *pe, *mask_obj;
220         int found = 0;
221
222         FOREACH_ACL_ENTRY(pa, acl, pe) {
223                 switch(pa->e_tag) {
224                         case ACL_USER_OBJ:
225                                 /* (May have been checked already) */
226                                 if (inode->i_uid == current_fsuid())
227                                         goto check_perm;
228                                 break;
229                         case ACL_USER:
230                                 if (pa->e_id == current_fsuid())
231                                         goto mask;
232                                 break;
233                         case ACL_GROUP_OBJ:
234                                 if (in_group_p(inode->i_gid)) {
235                                         found = 1;
236                                         if ((pa->e_perm & want) == want)
237                                                 goto mask;
238                                 }
239                                 break;
240                         case ACL_GROUP:
241                                 if (in_group_p(pa->e_id)) {
242                                         found = 1;
243                                         if ((pa->e_perm & want) == want)
244                                                 goto mask;
245                                 }
246                                 break;
247                         case ACL_MASK:
248                                 break;
249                         case ACL_OTHER:
250                                 if (found)
251                                         return -EACCES;
252                                 else
253                                         goto check_perm;
254                         default:
255                                 return -EIO;
256                 }
257         }
258         return -EIO;
259
260 mask:
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)
264                                 return 0;
265                         return -EACCES;
266                 }
267         }
268
269 check_perm:
270         if ((pa->e_perm & want) == want)
271                 return 0;
272         return -EACCES;
273 }
274
275 /*
276  * Modify acl when creating a new inode. The caller must ensure the acl is
277  * only referenced once.
278  *
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.
282  */
283 static int posix_acl_create_masq(struct posix_acl *acl, mode_t *mode_p)
284 {
285         struct posix_acl_entry *pa, *pe;
286         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
287         mode_t mode = *mode_p;
288         int not_equiv = 0;
289
290         /* assert(atomic_read(acl->a_refcount) == 1); */
291
292         FOREACH_ACL_ENTRY(pa, acl, pe) {
293                 switch(pa->e_tag) {
294                         case ACL_USER_OBJ:
295                                 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
296                                 mode &= (pa->e_perm << 6) | ~S_IRWXU;
297                                 break;
298
299                         case ACL_USER:
300                         case ACL_GROUP:
301                                 not_equiv = 1;
302                                 break;
303
304                         case ACL_GROUP_OBJ:
305                                 group_obj = pa;
306                                 break;
307
308                         case ACL_OTHER:
309                                 pa->e_perm &= mode | ~S_IRWXO;
310                                 mode &= pa->e_perm | ~S_IRWXO;
311                                 break;
312
313                         case ACL_MASK:
314                                 mask_obj = pa;
315                                 not_equiv = 1;
316                                 break;
317
318                         default:
319                                 return -EIO;
320                 }
321         }
322
323         if (mask_obj) {
324                 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
325                 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
326         } else {
327                 if (!group_obj)
328                         return -EIO;
329                 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
330                 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
331         }
332
333         *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
334         return not_equiv;
335 }
336
337 /*
338  * Modify the ACL for the chmod syscall.
339  */
340 static int posix_acl_chmod_masq(struct posix_acl *acl, mode_t mode)
341 {
342         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
343         struct posix_acl_entry *pa, *pe;
344
345         /* assert(atomic_read(acl->a_refcount) == 1); */
346
347         FOREACH_ACL_ENTRY(pa, acl, pe) {
348                 switch(pa->e_tag) {
349                         case ACL_USER_OBJ:
350                                 pa->e_perm = (mode & S_IRWXU) >> 6;
351                                 break;
352
353                         case ACL_USER:
354                         case ACL_GROUP:
355                                 break;
356
357                         case ACL_GROUP_OBJ:
358                                 group_obj = pa;
359                                 break;
360
361                         case ACL_MASK:
362                                 mask_obj = pa;
363                                 break;
364
365                         case ACL_OTHER:
366                                 pa->e_perm = (mode & S_IRWXO);
367                                 break;
368
369                         default:
370                                 return -EIO;
371                 }
372         }
373
374         if (mask_obj) {
375                 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
376         } else {
377                 if (!group_obj)
378                         return -EIO;
379                 group_obj->e_perm = (mode & S_IRWXG) >> 3;
380         }
381
382         return 0;
383 }
384
385 int
386 posix_acl_create(struct posix_acl **acl, gfp_t gfp, mode_t *mode_p)
387 {
388         struct posix_acl *clone = posix_acl_clone(*acl, gfp);
389         int err = -ENOMEM;
390         if (clone) {
391                 err = posix_acl_create_masq(clone, mode_p);
392                 if (err < 0) {
393                         posix_acl_release(clone);
394                         clone = NULL;
395                 }
396         }
397         posix_acl_release(*acl);
398         *acl = clone;
399         return err;
400 }
401 EXPORT_SYMBOL(posix_acl_create);
402
403 int
404 posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, mode_t mode)
405 {
406         struct posix_acl *clone = posix_acl_clone(*acl, gfp);
407         int err = -ENOMEM;
408         if (clone) {
409                 err = posix_acl_chmod_masq(clone, mode);
410                 if (err) {
411                         posix_acl_release(clone);
412                         clone = NULL;
413                 }
414         }
415         posix_acl_release(*acl);
416         *acl = clone;
417         return err;
418 }
419 EXPORT_SYMBOL(posix_acl_chmod);