blob: d2ffddc12117e8c87512ce87f7f98bd4f49e5869 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext2/acl.c
3 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 */
6
Randy Dunlap16f7e0f2006-01-11 12:17:46 -08007#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
9#include <linux/sched.h>
10#include <linux/slab.h>
11#include <linux/fs.h>
12#include "ext2.h"
13#include "xattr.h"
14#include "acl.h"
15
16/*
17 * Convert from filesystem to in-memory representation.
18 */
19static struct posix_acl *
20ext2_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
76fail:
77 posix_acl_release(acl);
78 return ERR_PTR(-EINVAL);
79}
80
81/*
82 * Convert from in-memory to filesystem representation.
83 */
84static void *
85ext2_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);
Panagiotis Issarisf52720c2006-09-27 01:49:39 -070092 ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
93 sizeof(ext2_acl_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 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
123fail:
124 kfree(ext_acl);
125 return ERR_PTR(-EINVAL);
126}
127
128static inline struct posix_acl *
129ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
130{
Al Viro5e78b432009-06-08 19:52:55 -0400131 struct posix_acl *acl = ACL_NOT_CACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 spin_lock(&inode->i_lock);
Al Viro5e78b432009-06-08 19:52:55 -0400134 if (*i_acl != ACL_NOT_CACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 acl = posix_acl_dup(*i_acl);
136 spin_unlock(&inode->i_lock);
137
138 return acl;
139}
140
141static inline void
142ext2_iset_acl(struct inode *inode, struct posix_acl **i_acl,
143 struct posix_acl *acl)
144{
145 spin_lock(&inode->i_lock);
Al Viro5e78b432009-06-08 19:52:55 -0400146 if (*i_acl != ACL_NOT_CACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 posix_acl_release(*i_acl);
148 *i_acl = posix_acl_dup(acl);
149 spin_unlock(&inode->i_lock);
150}
151
152/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800153 * inode->i_mutex: don't care
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
155static struct posix_acl *
156ext2_get_acl(struct inode *inode, int type)
157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 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 NULL;
165
166 switch(type) {
167 case ACL_TYPE_ACCESS:
Al Viro5e78b432009-06-08 19:52:55 -0400168 acl = ext2_iget_acl(inode, &inode->i_acl);
169 if (acl != ACL_NOT_CACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return acl;
171 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
172 break;
173
174 case ACL_TYPE_DEFAULT:
Al Viro5e78b432009-06-08 19:52:55 -0400175 acl = ext2_iget_acl(inode, &inode->i_default_acl);
176 if (acl != ACL_NOT_CACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 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);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800197 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (!IS_ERR(acl)) {
200 switch(type) {
201 case ACL_TYPE_ACCESS:
Al Viro5e78b432009-06-08 19:52:55 -0400202 ext2_iset_acl(inode, &inode->i_acl, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204
205 case ACL_TYPE_DEFAULT:
Al Viro5e78b432009-06-08 19:52:55 -0400206 ext2_iset_acl(inode, &inode->i_default_acl, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
208 }
209 }
210 return acl;
211}
212
213/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800214 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
216static int
217ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
218{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int name_index;
220 void *value = NULL;
Andreas Gruenbacherdfa08592006-02-03 03:04:13 -0800221 size_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int error;
223
224 if (S_ISLNK(inode->i_mode))
225 return -EOPNOTSUPP;
226 if (!test_opt(inode->i_sb, POSIX_ACL))
227 return 0;
228
229 switch(type) {
230 case ACL_TYPE_ACCESS:
231 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
232 if (acl) {
233 mode_t mode = inode->i_mode;
234 error = posix_acl_equiv_mode(acl, &mode);
235 if (error < 0)
236 return error;
237 else {
238 inode->i_mode = mode;
239 mark_inode_dirty(inode);
240 if (error == 0)
241 acl = NULL;
242 }
243 }
244 break;
245
246 case ACL_TYPE_DEFAULT:
247 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
248 if (!S_ISDIR(inode->i_mode))
249 return acl ? -EACCES : 0;
250 break;
251
252 default:
253 return -EINVAL;
254 }
255 if (acl) {
256 value = ext2_acl_to_disk(acl, &size);
257 if (IS_ERR(value))
258 return (int)PTR_ERR(value);
259 }
260
261 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
262
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800263 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!error) {
265 switch(type) {
266 case ACL_TYPE_ACCESS:
Al Viro5e78b432009-06-08 19:52:55 -0400267 ext2_iset_acl(inode, &inode->i_acl, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269
270 case ACL_TYPE_DEFAULT:
Al Viro5e78b432009-06-08 19:52:55 -0400271 ext2_iset_acl(inode, &inode->i_default_acl, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
273 }
274 }
275 return error;
276}
277
278static int
279ext2_check_acl(struct inode *inode, int mask)
280{
281 struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
282
akpm@osdl.orge4930732005-04-16 15:24:00 -0700283 if (IS_ERR(acl))
284 return PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (acl) {
286 int error = posix_acl_permission(inode, acl, mask);
287 posix_acl_release(acl);
288 return error;
289 }
290
291 return -EAGAIN;
292}
293
294int
Al Viroe6305c42008-07-15 21:03:57 -0400295ext2_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 return generic_permission(inode, mask, ext2_check_acl);
298}
299
300/*
301 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
302 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800303 * dir->i_mutex: down
304 * inode->i_mutex: up (access to inode is still exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306int
307ext2_init_acl(struct inode *inode, struct inode *dir)
308{
309 struct posix_acl *acl = NULL;
310 int error = 0;
311
312 if (!S_ISLNK(inode->i_mode)) {
313 if (test_opt(dir->i_sb, POSIX_ACL)) {
314 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
315 if (IS_ERR(acl))
316 return PTR_ERR(acl);
317 }
318 if (!acl)
Al Viroce3b0f82009-03-29 19:08:22 -0400319 inode->i_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
322 struct posix_acl *clone;
323 mode_t mode;
324
325 if (S_ISDIR(inode->i_mode)) {
326 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
327 if (error)
328 goto cleanup;
329 }
330 clone = posix_acl_clone(acl, GFP_KERNEL);
331 error = -ENOMEM;
332 if (!clone)
333 goto cleanup;
334 mode = inode->i_mode;
335 error = posix_acl_create_masq(clone, &mode);
336 if (error >= 0) {
337 inode->i_mode = mode;
338 if (error > 0) {
339 /* This is an extended ACL */
340 error = ext2_set_acl(inode,
341 ACL_TYPE_ACCESS, clone);
342 }
343 }
344 posix_acl_release(clone);
345 }
346cleanup:
347 posix_acl_release(acl);
348 return error;
349}
350
351/*
352 * Does chmod for an inode that may have an Access Control List. The
353 * inode->i_mode field must be updated to the desired value by the caller
354 * before calling this function.
355 * Returns 0 on success, or a negative error number.
356 *
357 * We change the ACL rather than storing some ACL entries in the file
358 * mode permission bits (which would be more efficient), because that
359 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
360 * for directories) are added. There are no more bits available in the
361 * file mode.
362 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800363 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365int
366ext2_acl_chmod(struct inode *inode)
367{
368 struct posix_acl *acl, *clone;
369 int error;
370
371 if (!test_opt(inode->i_sb, POSIX_ACL))
372 return 0;
373 if (S_ISLNK(inode->i_mode))
374 return -EOPNOTSUPP;
375 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
376 if (IS_ERR(acl) || !acl)
377 return PTR_ERR(acl);
378 clone = posix_acl_clone(acl, GFP_KERNEL);
379 posix_acl_release(acl);
380 if (!clone)
381 return -ENOMEM;
382 error = posix_acl_chmod_masq(clone, inode->i_mode);
383 if (!error)
384 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
385 posix_acl_release(clone);
386 return error;
387}
388
389/*
390 * Extended attribut handlers
391 */
392static size_t
393ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
394 const char *name, size_t name_len)
395{
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700396 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (!test_opt(inode->i_sb, POSIX_ACL))
399 return 0;
400 if (list && size <= list_size)
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700401 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return size;
403}
404
405static size_t
406ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
407 const char *name, size_t name_len)
408{
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700409 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (!test_opt(inode->i_sb, POSIX_ACL))
412 return 0;
413 if (list && size <= list_size)
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700414 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return size;
416}
417
418static int
419ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
420{
421 struct posix_acl *acl;
422 int error;
423
424 if (!test_opt(inode->i_sb, POSIX_ACL))
425 return -EOPNOTSUPP;
426
427 acl = ext2_get_acl(inode, type);
428 if (IS_ERR(acl))
429 return PTR_ERR(acl);
430 if (acl == NULL)
431 return -ENODATA;
432 error = posix_acl_to_xattr(acl, buffer, size);
433 posix_acl_release(acl);
434
435 return error;
436}
437
438static int
439ext2_xattr_get_acl_access(struct inode *inode, const char *name,
440 void *buffer, size_t size)
441{
442 if (strcmp(name, "") != 0)
443 return -EINVAL;
444 return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
445}
446
447static int
448ext2_xattr_get_acl_default(struct inode *inode, const char *name,
449 void *buffer, size_t size)
450{
451 if (strcmp(name, "") != 0)
452 return -EINVAL;
453 return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
454}
455
456static int
457ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
458 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;
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530465 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -EPERM;
467
468 if (value) {
469 acl = posix_acl_from_xattr(value, size);
470 if (IS_ERR(acl))
471 return PTR_ERR(acl);
472 else if (acl) {
473 error = posix_acl_valid(acl);
474 if (error)
475 goto release_and_out;
476 }
477 } else
478 acl = NULL;
479
480 error = ext2_set_acl(inode, type, acl);
481
482release_and_out:
483 posix_acl_release(acl);
484 return error;
485}
486
487static int
488ext2_xattr_set_acl_access(struct inode *inode, const char *name,
489 const void *value, size_t size, int flags)
490{
491 if (strcmp(name, "") != 0)
492 return -EINVAL;
493 return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
494}
495
496static int
497ext2_xattr_set_acl_default(struct inode *inode, const char *name,
498 const void *value, size_t size, int flags)
499{
500 if (strcmp(name, "") != 0)
501 return -EINVAL;
502 return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
503}
504
505struct xattr_handler ext2_xattr_acl_access_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700506 .prefix = POSIX_ACL_XATTR_ACCESS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .list = ext2_xattr_list_acl_access,
508 .get = ext2_xattr_get_acl_access,
509 .set = ext2_xattr_set_acl_access,
510};
511
512struct xattr_handler ext2_xattr_acl_default_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700513 .prefix = POSIX_ACL_XATTR_DEFAULT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 .list = ext2_xattr_list_acl_default,
515 .get = ext2_xattr_get_acl_default,
516 .set = ext2_xattr_set_acl_default,
517};