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