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