d232026b4e317b68a1e7e50d8259989afe3b5ff3
[linux-2.6.git] / fs / ext2 / acl.c
1 /*
2  * linux/fs/ext2/acl.c
3  *
4  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5  */
6
7 #include <linux/init.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include <linux/fs.h>
11 #include <linux/namei.h> 
12 #include <linux/vs_base.h>
13 #include "ext2.h"
14 #include "xattr.h"
15 #include "acl.h"
16
17 /*
18  * Convert from filesystem to in-memory representation.
19  */
20 static struct posix_acl *
21 ext2_acl_from_disk(const void *value, size_t size)
22 {
23         const char *end = (char *)value + size;
24         int n, count;
25         struct posix_acl *acl;
26
27         if (!value)
28                 return NULL;
29         if (size < sizeof(ext2_acl_header))
30                  return ERR_PTR(-EINVAL);
31         if (((ext2_acl_header *)value)->a_version !=
32             cpu_to_le32(EXT2_ACL_VERSION))
33                 return ERR_PTR(-EINVAL);
34         value = (char *)value + sizeof(ext2_acl_header);
35         count = ext2_acl_count(size);
36         if (count < 0)
37                 return ERR_PTR(-EINVAL);
38         if (count == 0)
39                 return NULL;
40         acl = posix_acl_alloc(count, GFP_KERNEL);
41         if (!acl)
42                 return ERR_PTR(-ENOMEM);
43         for (n=0; n < count; n++) {
44                 ext2_acl_entry *entry =
45                         (ext2_acl_entry *)value;
46                 if ((char *)value + sizeof(ext2_acl_entry_short) > end)
47                         goto fail;
48                 acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
49                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
50                 switch(acl->a_entries[n].e_tag) {
51                         case ACL_USER_OBJ:
52                         case ACL_GROUP_OBJ:
53                         case ACL_MASK:
54                         case ACL_OTHER:
55                                 value = (char *)value +
56                                         sizeof(ext2_acl_entry_short);
57                                 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
58                                 break;
59
60                         case ACL_USER:
61                         case ACL_GROUP:
62                                 value = (char *)value + sizeof(ext2_acl_entry);
63                                 if ((char *)value > end)
64                                         goto fail;
65                                 acl->a_entries[n].e_id =
66                                         le32_to_cpu(entry->e_id);
67                                 break;
68
69                         default:
70                                 goto fail;
71                 }
72         }
73         if (value != end)
74                 goto fail;
75         return acl;
76
77 fail:
78         posix_acl_release(acl);
79         return ERR_PTR(-EINVAL);
80 }
81
82 /*
83  * Convert from in-memory to filesystem representation.
84  */
85 static void *
86 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
87 {
88         ext2_acl_header *ext_acl;
89         char *e;
90         size_t n;
91
92         *size = ext2_acl_size(acl->a_count);
93         ext_acl = (ext2_acl_header *)kmalloc(sizeof(ext2_acl_header) +
94                 acl->a_count * sizeof(ext2_acl_entry), GFP_KERNEL);
95         if (!ext_acl)
96                 return ERR_PTR(-ENOMEM);
97         ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
98         e = (char *)ext_acl + sizeof(ext2_acl_header);
99         for (n=0; n < acl->a_count; n++) {
100                 ext2_acl_entry *entry = (ext2_acl_entry *)e;
101                 entry->e_tag  = cpu_to_le16(acl->a_entries[n].e_tag);
102                 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
103                 switch(acl->a_entries[n].e_tag) {
104                         case ACL_USER:
105                         case ACL_GROUP:
106                                 entry->e_id =
107                                         cpu_to_le32(acl->a_entries[n].e_id);
108                                 e += sizeof(ext2_acl_entry);
109                                 break;
110
111                         case ACL_USER_OBJ:
112                         case ACL_GROUP_OBJ:
113                         case ACL_MASK:
114                         case ACL_OTHER:
115                                 e += sizeof(ext2_acl_entry_short);
116                                 break;
117
118                         default:
119                                 goto fail;
120                 }
121         }
122         return (char *)ext_acl;
123
124 fail:
125         kfree(ext_acl);
126         return ERR_PTR(-EINVAL);
127 }
128
129 static inline struct posix_acl *
130 ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
131 {
132         struct posix_acl *acl = EXT2_ACL_NOT_CACHED;
133
134         spin_lock(&inode->i_lock);
135         if (*i_acl != EXT2_ACL_NOT_CACHED)
136                 acl = posix_acl_dup(*i_acl);
137         spin_unlock(&inode->i_lock);
138
139         return acl;
140 }
141
142 static inline void
143 ext2_iset_acl(struct inode *inode, struct posix_acl **i_acl,
144                    struct posix_acl *acl)
145 {
146         spin_lock(&inode->i_lock);
147         if (*i_acl != EXT2_ACL_NOT_CACHED)
148                 posix_acl_release(*i_acl);
149         *i_acl = posix_acl_dup(acl);
150         spin_unlock(&inode->i_lock);
151 }
152
153 /*
154  * inode->i_sem: don't care
155  */
156 static struct posix_acl *
157 ext2_get_acl(struct inode *inode, int type)
158 {
159         struct ext2_inode_info *ei = EXT2_I(inode);
160         int name_index;
161         char *value = NULL;
162         struct posix_acl *acl;
163         int retval;
164
165         if (!test_opt(inode->i_sb, POSIX_ACL))
166                 return NULL;
167
168         switch(type) {
169                 case ACL_TYPE_ACCESS:
170                         acl = ext2_iget_acl(inode, &ei->i_acl);
171                         if (acl != EXT2_ACL_NOT_CACHED)
172                                 return acl;
173                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
174                         break;
175
176                 case ACL_TYPE_DEFAULT:
177                         acl = ext2_iget_acl(inode, &ei->i_default_acl);
178                         if (acl != EXT2_ACL_NOT_CACHED)
179                                 return acl;
180                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
181                         break;
182
183                 default:
184                         return ERR_PTR(-EINVAL);
185         }
186         retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
187         if (retval > 0) {
188                 value = kmalloc(retval, GFP_KERNEL);
189                 if (!value)
190                         return ERR_PTR(-ENOMEM);
191                 retval = ext2_xattr_get(inode, name_index, "", value, retval);
192         }
193         if (retval > 0)
194                 acl = ext2_acl_from_disk(value, retval);
195         else if (retval == -ENODATA || retval == -ENOSYS)
196                 acl = NULL;
197         else
198                 acl = ERR_PTR(retval);
199         if (value)
200                 kfree(value);
201
202         if (!IS_ERR(acl)) {
203                 switch(type) {
204                         case ACL_TYPE_ACCESS:
205                                 ext2_iset_acl(inode, &ei->i_acl, acl);
206                                 break;
207
208                         case ACL_TYPE_DEFAULT:
209                                 ext2_iset_acl(inode, &ei->i_default_acl, acl);
210                                 break;
211                 }
212         }
213         return acl;
214 }
215
216 /*
217  * inode->i_sem: down
218  */
219 static int
220 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
221 {
222         struct ext2_inode_info *ei = EXT2_I(inode);
223         int name_index;
224         void *value = NULL;
225         size_t size;
226         int error;
227
228         if (S_ISLNK(inode->i_mode))
229                 return -EOPNOTSUPP;
230         if (!test_opt(inode->i_sb, POSIX_ACL))
231                 return 0;
232
233         switch(type) {
234                 case ACL_TYPE_ACCESS:
235                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
236                         if (acl) {
237                                 mode_t mode = inode->i_mode;
238                                 error = posix_acl_equiv_mode(acl, &mode);
239                                 if (error < 0)
240                                         return error;
241                                 else {
242                                         inode->i_mode = mode;
243                                         mark_inode_dirty(inode);
244                                         if (error == 0)
245                                                 acl = NULL;
246                                 }
247                         }
248                         break;
249
250                 case ACL_TYPE_DEFAULT:
251                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
252                         if (!S_ISDIR(inode->i_mode))
253                                 return acl ? -EACCES : 0;
254                         break;
255
256                 default:
257                         return -EINVAL;
258         }
259         if (acl) {
260                 if (acl->a_count > EXT2_ACL_MAX_ENTRIES)
261                         return -EINVAL;
262                 value = ext2_acl_to_disk(acl, &size);
263                 if (IS_ERR(value))
264                         return (int)PTR_ERR(value);
265         }
266
267         error = ext2_xattr_set(inode, name_index, "", value, size, 0);
268
269         if (value)
270                 kfree(value);
271         if (!error) {
272                 switch(type) {
273                         case ACL_TYPE_ACCESS:
274                                 ext2_iset_acl(inode, &ei->i_acl, acl);
275                                 break;
276
277                         case ACL_TYPE_DEFAULT:
278                                 ext2_iset_acl(inode, &ei->i_default_acl, acl);
279                                 break;
280                 }
281         }
282         return error;
283 }
284
285 /*
286  * Inode operation permission().
287  *
288  * inode->i_sem: don't care
289  */
290 int
291 ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
292 {
293         int mode = inode->i_mode;
294
295         /* Prevent vservers from escaping chroot() barriers */
296         if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
297                 return -EACCES;
298         /* Nobody gets write access to a read-only fs */
299         if ((mask & MAY_WRITE) && (IS_RDONLY(inode) ||
300             (nd && MNT_IS_RDONLY(nd->mnt))) &&
301             (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
302                 return -EROFS;
303         /* Nobody gets write access to an immutable file */
304         if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
305             return -EACCES;
306         if (current->fsuid == inode->i_uid) {
307                 mode >>= 6;
308         } else if (test_opt(inode->i_sb, POSIX_ACL)) {
309                 struct posix_acl *acl;
310
311                 /* The access ACL cannot grant access if the group class
312                    permission bits don't contain all requested permissions. */
313                 if (((mode >> 3) & mask & S_IRWXO) != mask)
314                         goto check_groups;
315                 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
316                 if (acl) {
317                         int error = posix_acl_permission(inode, acl, mask);
318                         posix_acl_release(acl);
319                         if (error == -EACCES)
320                                 goto check_capabilities;
321                         return error;
322                 } else
323                         goto check_groups;
324         } else {
325 check_groups:
326                 if (in_group_p(inode->i_gid))
327                         mode >>= 3;
328         }
329         if ((mode & mask & S_IRWXO) == mask)
330                 return 0;
331
332 check_capabilities:
333         /* Allowed to override Discretionary Access Control? */
334         if (!(mask & MAY_EXEC) ||
335             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
336                 if (capable(CAP_DAC_OVERRIDE))
337                         return 0;
338         /* Read and search granted if capable(CAP_DAC_READ_SEARCH) */
339         if (capable(CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
340             (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
341                 return 0;
342         return -EACCES;
343 }
344
345 /*
346  * Initialize the ACLs of a new inode. Called from ext2_new_inode.
347  *
348  * dir->i_sem: down
349  * inode->i_sem: up (access to inode is still exclusive)
350  */
351 int
352 ext2_init_acl(struct inode *inode, struct inode *dir)
353 {
354         struct posix_acl *acl = NULL;
355         int error = 0;
356
357         if (!S_ISLNK(inode->i_mode)) {
358                 if (test_opt(dir->i_sb, POSIX_ACL)) {
359                         acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
360                         if (IS_ERR(acl))
361                                 return PTR_ERR(acl);
362                 }
363                 if (!acl)
364                         inode->i_mode &= ~current->fs->umask;
365         }
366         if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
367                struct posix_acl *clone;
368                mode_t mode;
369
370                 if (S_ISDIR(inode->i_mode)) {
371                         error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
372                         if (error)
373                                 goto cleanup;
374                 }
375                 clone = posix_acl_clone(acl, GFP_KERNEL);
376                 error = -ENOMEM;
377                 if (!clone)
378                         goto cleanup;
379                 mode = inode->i_mode;
380                 error = posix_acl_create_masq(clone, &mode);
381                 if (error >= 0) {
382                         inode->i_mode = mode;
383                         if (error > 0) {
384                                 /* This is an extended ACL */
385                                 error = ext2_set_acl(inode,
386                                                      ACL_TYPE_ACCESS, clone);
387                         }
388                 }
389                 posix_acl_release(clone);
390         }
391 cleanup:
392        posix_acl_release(acl);
393        return error;
394 }
395
396 /*
397  * Does chmod for an inode that may have an Access Control List. The
398  * inode->i_mode field must be updated to the desired value by the caller
399  * before calling this function.
400  * Returns 0 on success, or a negative error number.
401  *
402  * We change the ACL rather than storing some ACL entries in the file
403  * mode permission bits (which would be more efficient), because that
404  * would break once additional permissions (like  ACL_APPEND, ACL_DELETE
405  * for directories) are added. There are no more bits available in the
406  * file mode.
407  *
408  * inode->i_sem: down
409  */
410 int
411 ext2_acl_chmod(struct inode *inode)
412 {
413         struct posix_acl *acl, *clone;
414         int error;
415
416         if (!test_opt(inode->i_sb, POSIX_ACL))
417                 return 0;
418         if (S_ISLNK(inode->i_mode))
419                 return -EOPNOTSUPP;
420         acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
421         if (IS_ERR(acl) || !acl)
422                 return PTR_ERR(acl);
423         clone = posix_acl_clone(acl, GFP_KERNEL);
424         posix_acl_release(acl);
425         if (!clone)
426                 return -ENOMEM;
427         error = posix_acl_chmod_masq(clone, inode->i_mode);
428         if (!error)
429                 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
430         posix_acl_release(clone);
431         return error;
432 }
433
434 /*
435  * Extended attribut handlers
436  */
437 static size_t
438 ext2_xattr_list_acl_access(char *list, struct inode *inode,
439                            const char *name, int name_len)
440 {
441         const size_t size = sizeof(XATTR_NAME_ACL_ACCESS);
442
443         if (!test_opt(inode->i_sb, POSIX_ACL))
444                 return 0;
445         if (list)
446                 memcpy(list, XATTR_NAME_ACL_ACCESS, size);
447         return size;
448 }
449
450 static size_t
451 ext2_xattr_list_acl_default(char *list, struct inode *inode,
452                             const char *name, int name_len)
453 {
454         const size_t size = sizeof(XATTR_NAME_ACL_DEFAULT);
455
456         if (!test_opt(inode->i_sb, POSIX_ACL))
457                 return 0;
458         if (list)
459                 memcpy(list, XATTR_NAME_ACL_DEFAULT, size);
460         return size;
461 }
462
463 static int
464 ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
465 {
466         struct posix_acl *acl;
467         int error;
468
469         if (!test_opt(inode->i_sb, POSIX_ACL))
470                 return -EOPNOTSUPP;
471
472         acl = ext2_get_acl(inode, type);
473         if (IS_ERR(acl))
474                 return PTR_ERR(acl);
475         if (acl == NULL)
476                 return -ENODATA;
477         error = posix_acl_to_xattr(acl, buffer, size);
478         posix_acl_release(acl);
479
480         return error;
481 }
482
483 static int
484 ext2_xattr_get_acl_access(struct inode *inode, const char *name,
485                           void *buffer, size_t size)
486 {
487         if (strcmp(name, "") != 0)
488                 return -EINVAL;
489         return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
490 }
491
492 static int
493 ext2_xattr_get_acl_default(struct inode *inode, const char *name,
494                            void *buffer, size_t size)
495 {
496         if (strcmp(name, "") != 0)
497                 return -EINVAL;
498         return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
499 }
500
501 static int
502 ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
503                    size_t size)
504 {
505         struct posix_acl *acl;
506         int error;
507
508         if (!test_opt(inode->i_sb, POSIX_ACL))
509                 return -EOPNOTSUPP;
510         if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
511                 return -EPERM;
512
513         if (value) {
514                 acl = posix_acl_from_xattr(value, size);
515                 if (IS_ERR(acl))
516                         return PTR_ERR(acl);
517                 else if (acl) {
518                         error = posix_acl_valid(acl);
519                         if (error)
520                                 goto release_and_out;
521                 }
522         } else
523                 acl = NULL;
524
525         error = ext2_set_acl(inode, type, acl);
526
527 release_and_out:
528         posix_acl_release(acl);
529         return error;
530 }
531
532 static int
533 ext2_xattr_set_acl_access(struct inode *inode, const char *name,
534                           const void *value, size_t size, int flags)
535 {
536         if (strcmp(name, "") != 0)
537                 return -EINVAL;
538         return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
539 }
540
541 static int
542 ext2_xattr_set_acl_default(struct inode *inode, const char *name,
543                            const void *value, size_t size, int flags)
544 {
545         if (strcmp(name, "") != 0)
546                 return -EINVAL;
547         return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
548 }
549
550 struct ext2_xattr_handler ext2_xattr_acl_access_handler = {
551         .prefix = XATTR_NAME_ACL_ACCESS,
552         .list   = ext2_xattr_list_acl_access,
553         .get    = ext2_xattr_get_acl_access,
554         .set    = ext2_xattr_set_acl_access,
555 };
556
557 struct ext2_xattr_handler ext2_xattr_acl_default_handler = {
558         .prefix = XATTR_NAME_ACL_DEFAULT,
559         .list   = ext2_xattr_list_acl_default,
560         .get    = ext2_xattr_get_acl_default,
561         .set    = ext2_xattr_set_acl_default,
562 };
563
564 void
565 exit_ext2_acl(void)
566 {
567         ext2_xattr_unregister(EXT2_XATTR_INDEX_POSIX_ACL_ACCESS,
568                               &ext2_xattr_acl_access_handler);
569         ext2_xattr_unregister(EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT,
570                               &ext2_xattr_acl_default_handler);
571 }
572
573 int __init
574 init_ext2_acl(void)
575 {
576         int error;
577
578         error = ext2_xattr_register(EXT2_XATTR_INDEX_POSIX_ACL_ACCESS,
579                                     &ext2_xattr_acl_access_handler);
580         if (error)
581                 goto fail;
582         error = ext2_xattr_register(EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT,
583                                     &ext2_xattr_acl_default_handler);
584         if (error)
585                 goto fail;
586         return 0;
587
588 fail:
589         exit_ext2_acl();
590         return error;
591 }