- fix 2.6.10 merge regression
[linux-2.6.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_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);
33
34 /*
35  * Allocate a new ACL with the specified number of entries.
36  */
37 struct posix_acl *
38 posix_acl_alloc(int count, int flags)
39 {
40         const size_t size = sizeof(struct posix_acl) +
41                             count * sizeof(struct posix_acl_entry);
42         struct posix_acl *acl = kmalloc(size, flags);
43         if (acl) {
44                 atomic_set(&acl->a_refcount, 1);
45                 acl->a_count = count;
46         }
47         return acl;
48 }
49
50 /*
51  * Clone an ACL.
52  */
53 struct posix_acl *
54 posix_acl_clone(const struct posix_acl *acl, int flags)
55 {
56         struct posix_acl *clone = NULL;
57
58         if (acl) {
59                 int size = sizeof(struct posix_acl) + acl->a_count *
60                            sizeof(struct posix_acl_entry);
61                 clone = kmalloc(size, flags);
62                 if (clone) {
63                         memcpy(clone, acl, size);
64                         atomic_set(&clone->a_refcount, 1);
65                 }
66         }
67         return clone;
68 }
69
70 /*
71  * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
72  */
73 int
74 posix_acl_valid(const struct posix_acl *acl)
75 {
76         const struct posix_acl_entry *pa, *pe;
77         int state = ACL_USER_OBJ;
78         unsigned int id = 0;  /* keep gcc happy */
79         int needs_mask = 0;
80
81         FOREACH_ACL_ENTRY(pa, acl, pe) {
82                 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
83                         return -EINVAL;
84                 switch (pa->e_tag) {
85                         case ACL_USER_OBJ:
86                                 if (state == ACL_USER_OBJ) {
87                                         id = 0;
88                                         state = ACL_USER;
89                                         break;
90                                 }
91                                 return -EINVAL;
92
93                         case ACL_USER:
94                                 if (state != ACL_USER)
95                                         return -EINVAL;
96                                 if (pa->e_id == ACL_UNDEFINED_ID ||
97                                     pa->e_id < id)
98                                         return -EINVAL;
99                                 id = pa->e_id + 1;
100                                 needs_mask = 1;
101                                 break;
102
103                         case ACL_GROUP_OBJ:
104                                 if (state == ACL_USER) {
105                                         id = 0;
106                                         state = ACL_GROUP;
107                                         break;
108                                 }
109                                 return -EINVAL;
110
111                         case ACL_GROUP:
112                                 if (state != ACL_GROUP)
113                                         return -EINVAL;
114                                 if (pa->e_id == ACL_UNDEFINED_ID ||
115                                     pa->e_id < id)
116                                         return -EINVAL;
117                                 id = pa->e_id + 1;
118                                 needs_mask = 1;
119                                 break;
120
121                         case ACL_MASK:
122                                 if (state != ACL_GROUP)
123                                         return -EINVAL;
124                                 state = ACL_OTHER;
125                                 break;
126
127                         case ACL_OTHER:
128                                 if (state == ACL_OTHER ||
129                                     (state == ACL_GROUP && !needs_mask)) {
130                                         state = 0;
131                                         break;
132                                 }
133                                 return -EINVAL;
134
135                         default:
136                                 return -EINVAL;
137                 }
138         }
139         if (state == 0)
140                 return 0;
141         return -EINVAL;
142 }
143
144 /*
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.
147  */
148 int
149 posix_acl_equiv_mode(const struct posix_acl *acl, mode_t *mode_p)
150 {
151         const struct posix_acl_entry *pa, *pe;
152         mode_t mode = 0;
153         int not_equiv = 0;
154
155         FOREACH_ACL_ENTRY(pa, acl, pe) {
156                 switch (pa->e_tag) {
157                         case ACL_USER_OBJ:
158                                 mode |= (pa->e_perm & S_IRWXO) << 6;
159                                 break;
160                         case ACL_GROUP_OBJ:
161                                 mode |= (pa->e_perm & S_IRWXO) << 3;
162                                 break;
163                         case ACL_OTHER:
164                                 mode |= pa->e_perm & S_IRWXO;
165                                 break;
166                         case ACL_MASK:
167                                 mode = (mode & ~S_IRWXG) |
168                                        ((pa->e_perm & S_IRWXO) << 3);
169                                 not_equiv = 1;
170                                 break;
171                         case ACL_USER:
172                         case ACL_GROUP:
173                                 not_equiv = 1;
174                                 break;
175                         default:
176                                 return -EINVAL;
177                 }
178         }
179         if (mode_p)
180                 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
181         return not_equiv;
182 }
183
184 /*
185  * Create an ACL representing the file mode permission bits of an inode.
186  */
187 struct posix_acl *
188 posix_acl_from_mode(mode_t mode, int flags)
189 {
190         struct posix_acl *acl = posix_acl_alloc(3, flags);
191         if (!acl)
192                 return ERR_PTR(-ENOMEM);
193
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;
197
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;
201
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);
205         return acl;
206 }
207
208 /*
209  * Return 0 if current is granted want access to the inode
210  * by the acl. Returns -E... otherwise.
211  */
212 int
213 posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
214 {
215         const struct posix_acl_entry *pa, *pe, *mask_obj;
216         int found = 0;
217
218         /* Prevent vservers from escaping chroot() barriers */
219         if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
220                 return -EACCES;
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 int
284 posix_acl_create_masq(struct posix_acl *acl, mode_t *mode_p)
285 {
286         struct posix_acl_entry *pa, *pe;
287         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
288         mode_t mode = *mode_p;
289         int not_equiv = 0;
290
291         /* assert(atomic_read(acl->a_refcount) == 1); */
292
293         FOREACH_ACL_ENTRY(pa, acl, pe) {
294                 switch(pa->e_tag) {
295                         case ACL_USER_OBJ:
296                                 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
297                                 mode &= (pa->e_perm << 6) | ~S_IRWXU;
298                                 break;
299
300                         case ACL_USER:
301                         case ACL_GROUP:
302                                 not_equiv = 1;
303                                 break;
304
305                         case ACL_GROUP_OBJ:
306                                 group_obj = pa;
307                                 break;
308
309                         case ACL_OTHER:
310                                 pa->e_perm &= mode | ~S_IRWXO;
311                                 mode &= pa->e_perm | ~S_IRWXO;
312                                 break;
313
314                         case ACL_MASK:
315                                 mask_obj = pa;
316                                 not_equiv = 1;
317                                 break;
318
319                         default:
320                                 return -EIO;
321                 }
322         }
323
324         if (mask_obj) {
325                 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
326                 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
327         } else {
328                 if (!group_obj)
329                         return -EIO;
330                 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
331                 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
332         }
333
334         *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
335         return not_equiv;
336 }
337
338 /*
339  * Modify the ACL for the chmod syscall.
340  */
341 int
342 posix_acl_chmod_masq(struct posix_acl *acl, mode_t mode)
343 {
344         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
345         struct posix_acl_entry *pa, *pe;
346
347         /* assert(atomic_read(acl->a_refcount) == 1); */
348
349         FOREACH_ACL_ENTRY(pa, acl, pe) {
350                 switch(pa->e_tag) {
351                         case ACL_USER_OBJ:
352                                 pa->e_perm = (mode & S_IRWXU) >> 6;
353                                 break;
354
355                         case ACL_USER:
356                         case ACL_GROUP:
357                                 break;
358
359                         case ACL_GROUP_OBJ:
360                                 group_obj = pa;
361                                 break;
362
363                         case ACL_MASK:
364                                 mask_obj = pa;
365                                 break;
366
367                         case ACL_OTHER:
368                                 pa->e_perm = (mode & S_IRWXO);
369                                 break;
370
371                         default:
372                                 return -EIO;
373                 }
374         }
375
376         if (mask_obj) {
377                 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
378         } else {
379                 if (!group_obj)
380                         return -EIO;
381                 group_obj->e_perm = (mode & S_IRWXG) >> 3;
382         }
383
384         return 0;
385 }