blob: d636e1297cad71e13c6cc6b24e516369bde9bd3c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800129 * inode->i_mutex: don't care
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131static struct posix_acl *
132ext2_get_acl(struct inode *inode, int type)
133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int name_index;
135 char *value = NULL;
136 struct posix_acl *acl;
137 int retval;
138
139 if (!test_opt(inode->i_sb, POSIX_ACL))
140 return NULL;
141
Al Viro073aaa12009-06-09 12:11:54 -0400142 acl = get_cached_acl(inode, type);
143 if (acl != ACL_NOT_CACHED)
144 return acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Al Viro073aaa12009-06-09 12:11:54 -0400146 switch (type) {
147 case ACL_TYPE_ACCESS:
148 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
149 break;
150 case ACL_TYPE_DEFAULT:
151 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
152 break;
153 default:
154 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
157 if (retval > 0) {
158 value = kmalloc(retval, GFP_KERNEL);
159 if (!value)
160 return ERR_PTR(-ENOMEM);
161 retval = ext2_xattr_get(inode, name_index, "", value, retval);
162 }
163 if (retval > 0)
164 acl = ext2_acl_from_disk(value, retval);
165 else if (retval == -ENODATA || retval == -ENOSYS)
166 acl = NULL;
167 else
168 acl = ERR_PTR(retval);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800169 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Al Viro073aaa12009-06-09 12:11:54 -0400171 if (!IS_ERR(acl))
172 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return acl;
175}
176
177/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800178 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
180static int
181ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int name_index;
184 void *value = NULL;
Andreas Gruenbacherdfa08592006-02-03 03:04:13 -0800185 size_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int error;
187
188 if (S_ISLNK(inode->i_mode))
189 return -EOPNOTSUPP;
190 if (!test_opt(inode->i_sb, POSIX_ACL))
191 return 0;
192
193 switch(type) {
194 case ACL_TYPE_ACCESS:
195 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
196 if (acl) {
197 mode_t mode = inode->i_mode;
198 error = posix_acl_equiv_mode(acl, &mode);
199 if (error < 0)
200 return error;
201 else {
202 inode->i_mode = mode;
203 mark_inode_dirty(inode);
204 if (error == 0)
205 acl = NULL;
206 }
207 }
208 break;
209
210 case ACL_TYPE_DEFAULT:
211 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
212 if (!S_ISDIR(inode->i_mode))
213 return acl ? -EACCES : 0;
214 break;
215
216 default:
217 return -EINVAL;
218 }
219 if (acl) {
220 value = ext2_acl_to_disk(acl, &size);
221 if (IS_ERR(value))
222 return (int)PTR_ERR(value);
223 }
224
225 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
226
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800227 kfree(value);
Al Viro073aaa12009-06-09 12:11:54 -0400228 if (!error)
229 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return error;
231}
232
233static int
234ext2_check_acl(struct inode *inode, int mask)
235{
236 struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
237
akpm@osdl.orge4930732005-04-16 15:24:00 -0700238 if (IS_ERR(acl))
239 return PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (acl) {
241 int error = posix_acl_permission(inode, acl, mask);
242 posix_acl_release(acl);
243 return error;
244 }
245
246 return -EAGAIN;
247}
248
249int
Al Viroe6305c42008-07-15 21:03:57 -0400250ext2_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 return generic_permission(inode, mask, ext2_check_acl);
253}
254
255/*
256 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
257 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800258 * dir->i_mutex: down
259 * inode->i_mutex: up (access to inode is still exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
261int
262ext2_init_acl(struct inode *inode, struct inode *dir)
263{
264 struct posix_acl *acl = NULL;
265 int error = 0;
266
267 if (!S_ISLNK(inode->i_mode)) {
268 if (test_opt(dir->i_sb, POSIX_ACL)) {
269 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
270 if (IS_ERR(acl))
271 return PTR_ERR(acl);
272 }
273 if (!acl)
Al Viroce3b0f82009-03-29 19:08:22 -0400274 inode->i_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
277 struct posix_acl *clone;
278 mode_t mode;
279
280 if (S_ISDIR(inode->i_mode)) {
281 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
282 if (error)
283 goto cleanup;
284 }
285 clone = posix_acl_clone(acl, GFP_KERNEL);
286 error = -ENOMEM;
287 if (!clone)
288 goto cleanup;
289 mode = inode->i_mode;
290 error = posix_acl_create_masq(clone, &mode);
291 if (error >= 0) {
292 inode->i_mode = mode;
293 if (error > 0) {
294 /* This is an extended ACL */
295 error = ext2_set_acl(inode,
296 ACL_TYPE_ACCESS, clone);
297 }
298 }
299 posix_acl_release(clone);
300 }
301cleanup:
302 posix_acl_release(acl);
303 return error;
304}
305
306/*
307 * Does chmod for an inode that may have an Access Control List. The
308 * inode->i_mode field must be updated to the desired value by the caller
309 * before calling this function.
310 * Returns 0 on success, or a negative error number.
311 *
312 * We change the ACL rather than storing some ACL entries in the file
313 * mode permission bits (which would be more efficient), because that
314 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
315 * for directories) are added. There are no more bits available in the
316 * file mode.
317 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800318 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320int
321ext2_acl_chmod(struct inode *inode)
322{
323 struct posix_acl *acl, *clone;
324 int error;
325
326 if (!test_opt(inode->i_sb, POSIX_ACL))
327 return 0;
328 if (S_ISLNK(inode->i_mode))
329 return -EOPNOTSUPP;
330 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
331 if (IS_ERR(acl) || !acl)
332 return PTR_ERR(acl);
333 clone = posix_acl_clone(acl, GFP_KERNEL);
334 posix_acl_release(acl);
335 if (!clone)
336 return -ENOMEM;
337 error = posix_acl_chmod_masq(clone, inode->i_mode);
338 if (!error)
339 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
340 posix_acl_release(clone);
341 return error;
342}
343
344/*
345 * Extended attribut handlers
346 */
347static size_t
348ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
349 const char *name, size_t name_len)
350{
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700351 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (!test_opt(inode->i_sb, POSIX_ACL))
354 return 0;
355 if (list && size <= list_size)
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700356 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return size;
358}
359
360static size_t
361ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
362 const char *name, size_t name_len)
363{
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700364 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (!test_opt(inode->i_sb, POSIX_ACL))
367 return 0;
368 if (list && size <= list_size)
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700369 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return size;
371}
372
373static int
374ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
375{
376 struct posix_acl *acl;
377 int error;
378
379 if (!test_opt(inode->i_sb, POSIX_ACL))
380 return -EOPNOTSUPP;
381
382 acl = ext2_get_acl(inode, type);
383 if (IS_ERR(acl))
384 return PTR_ERR(acl);
385 if (acl == NULL)
386 return -ENODATA;
387 error = posix_acl_to_xattr(acl, buffer, size);
388 posix_acl_release(acl);
389
390 return error;
391}
392
393static int
394ext2_xattr_get_acl_access(struct inode *inode, const char *name,
395 void *buffer, size_t size)
396{
397 if (strcmp(name, "") != 0)
398 return -EINVAL;
399 return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
400}
401
402static int
403ext2_xattr_get_acl_default(struct inode *inode, const char *name,
404 void *buffer, size_t size)
405{
406 if (strcmp(name, "") != 0)
407 return -EINVAL;
408 return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
409}
410
411static int
412ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
413 size_t size)
414{
415 struct posix_acl *acl;
416 int error;
417
418 if (!test_opt(inode->i_sb, POSIX_ACL))
419 return -EOPNOTSUPP;
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530420 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -EPERM;
422
423 if (value) {
424 acl = posix_acl_from_xattr(value, size);
425 if (IS_ERR(acl))
426 return PTR_ERR(acl);
427 else if (acl) {
428 error = posix_acl_valid(acl);
429 if (error)
430 goto release_and_out;
431 }
432 } else
433 acl = NULL;
434
435 error = ext2_set_acl(inode, type, acl);
436
437release_and_out:
438 posix_acl_release(acl);
439 return error;
440}
441
442static int
443ext2_xattr_set_acl_access(struct inode *inode, const char *name,
444 const void *value, size_t size, int flags)
445{
446 if (strcmp(name, "") != 0)
447 return -EINVAL;
448 return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
449}
450
451static int
452ext2_xattr_set_acl_default(struct inode *inode, const char *name,
453 const void *value, size_t size, int flags)
454{
455 if (strcmp(name, "") != 0)
456 return -EINVAL;
457 return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
458}
459
460struct xattr_handler ext2_xattr_acl_access_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700461 .prefix = POSIX_ACL_XATTR_ACCESS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 .list = ext2_xattr_list_acl_access,
463 .get = ext2_xattr_get_acl_access,
464 .set = ext2_xattr_set_acl_access,
465};
466
467struct xattr_handler ext2_xattr_acl_default_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700468 .prefix = POSIX_ACL_XATTR_DEFAULT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .list = ext2_xattr_list_acl_default,
470 .get = ext2_xattr_get_acl_default,
471 .set = ext2_xattr_set_acl_default,
472};