]> Pileus Git - ~andy/linux/blob - fs/posix_acl.c
Merge tag 'virtio-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~andy/linux] / 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 <linux/atomic.h>
18 #include <linux/fs.h>
19 #include <linux/sched.h>
20 #include <linux/posix_acl.h>
21 #include <linux/export.h>
22
23 #include <linux/errno.h>
24
25 struct posix_acl **acl_by_type(struct inode *inode, int type)
26 {
27         switch (type) {
28         case ACL_TYPE_ACCESS:
29                 return &inode->i_acl;
30         case ACL_TYPE_DEFAULT:
31                 return &inode->i_default_acl;
32         default:
33                 BUG();
34         }
35 }
36 EXPORT_SYMBOL(acl_by_type);
37
38 struct posix_acl *get_cached_acl(struct inode *inode, int type)
39 {
40         struct posix_acl **p = acl_by_type(inode, type);
41         struct posix_acl *acl = ACCESS_ONCE(*p);
42         if (acl) {
43                 spin_lock(&inode->i_lock);
44                 acl = *p;
45                 if (acl != ACL_NOT_CACHED)
46                         acl = posix_acl_dup(acl);
47                 spin_unlock(&inode->i_lock);
48         }
49         return acl;
50 }
51 EXPORT_SYMBOL(get_cached_acl);
52
53 struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
54 {
55         return rcu_dereference(*acl_by_type(inode, type));
56 }
57 EXPORT_SYMBOL(get_cached_acl_rcu);
58
59 void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
60 {
61         struct posix_acl **p = acl_by_type(inode, type);
62         struct posix_acl *old;
63         spin_lock(&inode->i_lock);
64         old = *p;
65         rcu_assign_pointer(*p, posix_acl_dup(acl));
66         spin_unlock(&inode->i_lock);
67         if (old != ACL_NOT_CACHED)
68                 posix_acl_release(old);
69 }
70 EXPORT_SYMBOL(set_cached_acl);
71
72 void forget_cached_acl(struct inode *inode, int type)
73 {
74         struct posix_acl **p = acl_by_type(inode, type);
75         struct posix_acl *old;
76         spin_lock(&inode->i_lock);
77         old = *p;
78         *p = ACL_NOT_CACHED;
79         spin_unlock(&inode->i_lock);
80         if (old != ACL_NOT_CACHED)
81                 posix_acl_release(old);
82 }
83 EXPORT_SYMBOL(forget_cached_acl);
84
85 void forget_all_cached_acls(struct inode *inode)
86 {
87         struct posix_acl *old_access, *old_default;
88         spin_lock(&inode->i_lock);
89         old_access = inode->i_acl;
90         old_default = inode->i_default_acl;
91         inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
92         spin_unlock(&inode->i_lock);
93         if (old_access != ACL_NOT_CACHED)
94                 posix_acl_release(old_access);
95         if (old_default != ACL_NOT_CACHED)
96                 posix_acl_release(old_default);
97 }
98 EXPORT_SYMBOL(forget_all_cached_acls);
99
100 /*
101  * Init a fresh posix_acl
102  */
103 void
104 posix_acl_init(struct posix_acl *acl, int count)
105 {
106         atomic_set(&acl->a_refcount, 1);
107         acl->a_count = count;
108 }
109 EXPORT_SYMBOL(posix_acl_init);
110
111 /*
112  * Allocate a new ACL with the specified number of entries.
113  */
114 struct posix_acl *
115 posix_acl_alloc(int count, gfp_t flags)
116 {
117         const size_t size = sizeof(struct posix_acl) +
118                             count * sizeof(struct posix_acl_entry);
119         struct posix_acl *acl = kmalloc(size, flags);
120         if (acl)
121                 posix_acl_init(acl, count);
122         return acl;
123 }
124 EXPORT_SYMBOL(posix_acl_alloc);
125
126 /*
127  * Clone an ACL.
128  */
129 static struct posix_acl *
130 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
131 {
132         struct posix_acl *clone = NULL;
133
134         if (acl) {
135                 int size = sizeof(struct posix_acl) + acl->a_count *
136                            sizeof(struct posix_acl_entry);
137                 clone = kmemdup(acl, size, flags);
138                 if (clone)
139                         atomic_set(&clone->a_refcount, 1);
140         }
141         return clone;
142 }
143
144 /*
145  * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
146  */
147 int
148 posix_acl_valid(const struct posix_acl *acl)
149 {
150         const struct posix_acl_entry *pa, *pe;
151         int state = ACL_USER_OBJ;
152         kuid_t prev_uid = INVALID_UID;
153         kgid_t prev_gid = INVALID_GID;
154         int needs_mask = 0;
155
156         FOREACH_ACL_ENTRY(pa, acl, pe) {
157                 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
158                         return -EINVAL;
159                 switch (pa->e_tag) {
160                         case ACL_USER_OBJ:
161                                 if (state == ACL_USER_OBJ) {
162                                         state = ACL_USER;
163                                         break;
164                                 }
165                                 return -EINVAL;
166
167                         case ACL_USER:
168                                 if (state != ACL_USER)
169                                         return -EINVAL;
170                                 if (!uid_valid(pa->e_uid))
171                                         return -EINVAL;
172                                 if (uid_valid(prev_uid) &&
173                                     uid_lte(pa->e_uid, prev_uid))
174                                         return -EINVAL;
175                                 prev_uid = pa->e_uid;
176                                 needs_mask = 1;
177                                 break;
178
179                         case ACL_GROUP_OBJ:
180                                 if (state == ACL_USER) {
181                                         state = ACL_GROUP;
182                                         break;
183                                 }
184                                 return -EINVAL;
185
186                         case ACL_GROUP:
187                                 if (state != ACL_GROUP)
188                                         return -EINVAL;
189                                 if (!gid_valid(pa->e_gid))
190                                         return -EINVAL;
191                                 if (gid_valid(prev_gid) &&
192                                     gid_lte(pa->e_gid, prev_gid))
193                                         return -EINVAL;
194                                 prev_gid = pa->e_gid;
195                                 needs_mask = 1;
196                                 break;
197
198                         case ACL_MASK:
199                                 if (state != ACL_GROUP)
200                                         return -EINVAL;
201                                 state = ACL_OTHER;
202                                 break;
203
204                         case ACL_OTHER:
205                                 if (state == ACL_OTHER ||
206                                     (state == ACL_GROUP && !needs_mask)) {
207                                         state = 0;
208                                         break;
209                                 }
210                                 return -EINVAL;
211
212                         default:
213                                 return -EINVAL;
214                 }
215         }
216         if (state == 0)
217                 return 0;
218         return -EINVAL;
219 }
220 EXPORT_SYMBOL(posix_acl_valid);
221
222 /*
223  * Returns 0 if the acl can be exactly represented in the traditional
224  * file mode permission bits, or else 1. Returns -E... on error.
225  */
226 int
227 posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
228 {
229         const struct posix_acl_entry *pa, *pe;
230         umode_t mode = 0;
231         int not_equiv = 0;
232
233         FOREACH_ACL_ENTRY(pa, acl, pe) {
234                 switch (pa->e_tag) {
235                         case ACL_USER_OBJ:
236                                 mode |= (pa->e_perm & S_IRWXO) << 6;
237                                 break;
238                         case ACL_GROUP_OBJ:
239                                 mode |= (pa->e_perm & S_IRWXO) << 3;
240                                 break;
241                         case ACL_OTHER:
242                                 mode |= pa->e_perm & S_IRWXO;
243                                 break;
244                         case ACL_MASK:
245                                 mode = (mode & ~S_IRWXG) |
246                                        ((pa->e_perm & S_IRWXO) << 3);
247                                 not_equiv = 1;
248                                 break;
249                         case ACL_USER:
250                         case ACL_GROUP:
251                                 not_equiv = 1;
252                                 break;
253                         default:
254                                 return -EINVAL;
255                 }
256         }
257         if (mode_p)
258                 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
259         return not_equiv;
260 }
261 EXPORT_SYMBOL(posix_acl_equiv_mode);
262
263 /*
264  * Create an ACL representing the file mode permission bits of an inode.
265  */
266 struct posix_acl *
267 posix_acl_from_mode(umode_t mode, gfp_t flags)
268 {
269         struct posix_acl *acl = posix_acl_alloc(3, flags);
270         if (!acl)
271                 return ERR_PTR(-ENOMEM);
272
273         acl->a_entries[0].e_tag  = ACL_USER_OBJ;
274         acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
275
276         acl->a_entries[1].e_tag  = ACL_GROUP_OBJ;
277         acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
278
279         acl->a_entries[2].e_tag  = ACL_OTHER;
280         acl->a_entries[2].e_perm = (mode & S_IRWXO);
281         return acl;
282 }
283 EXPORT_SYMBOL(posix_acl_from_mode);
284
285 /*
286  * Return 0 if current is granted want access to the inode
287  * by the acl. Returns -E... otherwise.
288  */
289 int
290 posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
291 {
292         const struct posix_acl_entry *pa, *pe, *mask_obj;
293         int found = 0;
294
295         want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
296
297         FOREACH_ACL_ENTRY(pa, acl, pe) {
298                 switch(pa->e_tag) {
299                         case ACL_USER_OBJ:
300                                 /* (May have been checked already) */
301                                 if (uid_eq(inode->i_uid, current_fsuid()))
302                                         goto check_perm;
303                                 break;
304                         case ACL_USER:
305                                 if (uid_eq(pa->e_uid, current_fsuid()))
306                                         goto mask;
307                                 break;
308                         case ACL_GROUP_OBJ:
309                                 if (in_group_p(inode->i_gid)) {
310                                         found = 1;
311                                         if ((pa->e_perm & want) == want)
312                                                 goto mask;
313                                 }
314                                 break;
315                         case ACL_GROUP:
316                                 if (in_group_p(pa->e_gid)) {
317                                         found = 1;
318                                         if ((pa->e_perm & want) == want)
319                                                 goto mask;
320                                 }
321                                 break;
322                         case ACL_MASK:
323                                 break;
324                         case ACL_OTHER:
325                                 if (found)
326                                         return -EACCES;
327                                 else
328                                         goto check_perm;
329                         default:
330                                 return -EIO;
331                 }
332         }
333         return -EIO;
334
335 mask:
336         for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
337                 if (mask_obj->e_tag == ACL_MASK) {
338                         if ((pa->e_perm & mask_obj->e_perm & want) == want)
339                                 return 0;
340                         return -EACCES;
341                 }
342         }
343
344 check_perm:
345         if ((pa->e_perm & want) == want)
346                 return 0;
347         return -EACCES;
348 }
349
350 /*
351  * Modify acl when creating a new inode. The caller must ensure the acl is
352  * only referenced once.
353  *
354  * mode_p initially must contain the mode parameter to the open() / creat()
355  * system calls. All permissions that are not granted by the acl are removed.
356  * The permissions in the acl are changed to reflect the mode_p parameter.
357  */
358 static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
359 {
360         struct posix_acl_entry *pa, *pe;
361         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
362         umode_t mode = *mode_p;
363         int not_equiv = 0;
364
365         /* assert(atomic_read(acl->a_refcount) == 1); */
366
367         FOREACH_ACL_ENTRY(pa, acl, pe) {
368                 switch(pa->e_tag) {
369                         case ACL_USER_OBJ:
370                                 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
371                                 mode &= (pa->e_perm << 6) | ~S_IRWXU;
372                                 break;
373
374                         case ACL_USER:
375                         case ACL_GROUP:
376                                 not_equiv = 1;
377                                 break;
378
379                         case ACL_GROUP_OBJ:
380                                 group_obj = pa;
381                                 break;
382
383                         case ACL_OTHER:
384                                 pa->e_perm &= mode | ~S_IRWXO;
385                                 mode &= pa->e_perm | ~S_IRWXO;
386                                 break;
387
388                         case ACL_MASK:
389                                 mask_obj = pa;
390                                 not_equiv = 1;
391                                 break;
392
393                         default:
394                                 return -EIO;
395                 }
396         }
397
398         if (mask_obj) {
399                 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
400                 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
401         } else {
402                 if (!group_obj)
403                         return -EIO;
404                 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
405                 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
406         }
407
408         *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
409         return not_equiv;
410 }
411
412 /*
413  * Modify the ACL for the chmod syscall.
414  */
415 static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
416 {
417         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
418         struct posix_acl_entry *pa, *pe;
419
420         /* assert(atomic_read(acl->a_refcount) == 1); */
421
422         FOREACH_ACL_ENTRY(pa, acl, pe) {
423                 switch(pa->e_tag) {
424                         case ACL_USER_OBJ:
425                                 pa->e_perm = (mode & S_IRWXU) >> 6;
426                                 break;
427
428                         case ACL_USER:
429                         case ACL_GROUP:
430                                 break;
431
432                         case ACL_GROUP_OBJ:
433                                 group_obj = pa;
434                                 break;
435
436                         case ACL_MASK:
437                                 mask_obj = pa;
438                                 break;
439
440                         case ACL_OTHER:
441                                 pa->e_perm = (mode & S_IRWXO);
442                                 break;
443
444                         default:
445                                 return -EIO;
446                 }
447         }
448
449         if (mask_obj) {
450                 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
451         } else {
452                 if (!group_obj)
453                         return -EIO;
454                 group_obj->e_perm = (mode & S_IRWXG) >> 3;
455         }
456
457         return 0;
458 }
459
460 int
461 posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
462 {
463         struct posix_acl *clone = posix_acl_clone(*acl, gfp);
464         int err = -ENOMEM;
465         if (clone) {
466                 err = posix_acl_create_masq(clone, mode_p);
467                 if (err < 0) {
468                         posix_acl_release(clone);
469                         clone = NULL;
470                 }
471         }
472         posix_acl_release(*acl);
473         *acl = clone;
474         return err;
475 }
476 EXPORT_SYMBOL(posix_acl_create);
477
478 int
479 posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
480 {
481         struct posix_acl *clone = posix_acl_clone(*acl, gfp);
482         int err = -ENOMEM;
483         if (clone) {
484                 err = posix_acl_chmod_masq(clone, mode);
485                 if (err) {
486                         posix_acl_release(clone);
487                         clone = NULL;
488                 }
489         }
490         posix_acl_release(*acl);
491         *acl = clone;
492         return err;
493 }
494 EXPORT_SYMBOL(posix_acl_chmod);