blob: e38a9b61af3f345b19bf493bf4db99e055724119 [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 */
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200131struct posix_acl *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132ext2_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) {
Jan Kara9451fcb2016-09-19 17:39:09 +0200197 error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
198 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return error;
Jan Kara9451fcb2016-09-19 17:39:09 +0200200 inode->i_ctime = CURRENT_TIME_SEC;
201 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 break;
204
205 case ACL_TYPE_DEFAULT:
206 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
207 if (!S_ISDIR(inode->i_mode))
208 return acl ? -EACCES : 0;
209 break;
210
211 default:
212 return -EINVAL;
213 }
214 if (acl) {
215 value = ext2_acl_to_disk(acl, &size);
216 if (IS_ERR(value))
217 return (int)PTR_ERR(value);
218 }
219
220 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
221
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800222 kfree(value);
Al Viro073aaa12009-06-09 12:11:54 -0400223 if (!error)
224 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return error;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
230 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800231 * dir->i_mutex: down
232 * inode->i_mutex: up (access to inode is still exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234int
235ext2_init_acl(struct inode *inode, struct inode *dir)
236{
237 struct posix_acl *acl = NULL;
238 int error = 0;
239
240 if (!S_ISLNK(inode->i_mode)) {
241 if (test_opt(dir->i_sb, POSIX_ACL)) {
242 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
243 if (IS_ERR(acl))
244 return PTR_ERR(acl);
245 }
246 if (!acl)
Al Viroce3b0f82009-03-29 19:08:22 -0400247 inode->i_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (S_ISDIR(inode->i_mode)) {
251 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
252 if (error)
253 goto cleanup;
254 }
Al Virod3fb6122011-07-23 18:37:50 -0400255 error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
Al Viro826cae22011-07-23 03:10:32 -0400256 if (error < 0)
257 return error;
Al Viro826cae22011-07-23 03:10:32 -0400258 if (error > 0) {
259 /* This is an extended ACL */
260 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263cleanup:
264 posix_acl_release(acl);
265 return error;
266}
267
268/*
269 * Does chmod for an inode that may have an Access Control List. The
270 * inode->i_mode field must be updated to the desired value by the caller
271 * before calling this function.
272 * Returns 0 on success, or a negative error number.
273 *
274 * We change the ACL rather than storing some ACL entries in the file
275 * mode permission bits (which would be more efficient), because that
276 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
277 * for directories) are added. There are no more bits available in the
278 * file mode.
279 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800280 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282int
283ext2_acl_chmod(struct inode *inode)
284{
Al Virobc26ab52011-07-23 00:18:02 -0400285 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int error;
287
288 if (!test_opt(inode->i_sb, POSIX_ACL))
289 return 0;
290 if (S_ISLNK(inode->i_mode))
291 return -EOPNOTSUPP;
292 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
293 if (IS_ERR(acl) || !acl)
294 return PTR_ERR(acl);
Al Virobc26ab52011-07-23 00:18:02 -0400295 error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
296 if (error)
297 return error;
298 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 posix_acl_release(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return error;
301}
302
303/*
304 * Extended attribut handlers
305 */
306static size_t
Christoph Hellwig431547b2009-11-13 09:52:56 +0000307ext2_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_size,
308 const char *name, size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700310 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Christoph Hellwig431547b2009-11-13 09:52:56 +0000312 if (!test_opt(dentry->d_sb, POSIX_ACL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return 0;
314 if (list && size <= list_size)
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700315 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return size;
317}
318
319static size_t
Christoph Hellwig431547b2009-11-13 09:52:56 +0000320ext2_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_size,
321 const char *name, size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700323 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Christoph Hellwig431547b2009-11-13 09:52:56 +0000325 if (!test_opt(dentry->d_sb, POSIX_ACL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327 if (list && size <= list_size)
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700328 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return size;
330}
331
332static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000333ext2_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
334 size_t size, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct posix_acl *acl;
337 int error;
338
Christoph Hellwig431547b2009-11-13 09:52:56 +0000339 if (strcmp(name, "") != 0)
340 return -EINVAL;
341 if (!test_opt(dentry->d_sb, POSIX_ACL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return -EOPNOTSUPP;
343
Christoph Hellwig431547b2009-11-13 09:52:56 +0000344 acl = ext2_get_acl(dentry->d_inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (IS_ERR(acl))
346 return PTR_ERR(acl);
347 if (acl == NULL)
348 return -ENODATA;
349 error = posix_acl_to_xattr(acl, buffer, size);
350 posix_acl_release(acl);
351
352 return error;
353}
354
355static int
Christoph Hellwig431547b2009-11-13 09:52:56 +0000356ext2_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
357 size_t size, int flags, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct posix_acl *acl;
360 int error;
361
Christoph Hellwig431547b2009-11-13 09:52:56 +0000362 if (strcmp(name, "") != 0)
363 return -EINVAL;
364 if (!test_opt(dentry->d_sb, POSIX_ACL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -EOPNOTSUPP;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700366 if (!inode_owner_or_capable(dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -EPERM;
368
369 if (value) {
370 acl = posix_acl_from_xattr(value, size);
371 if (IS_ERR(acl))
372 return PTR_ERR(acl);
373 else if (acl) {
374 error = posix_acl_valid(acl);
375 if (error)
376 goto release_and_out;
377 }
378 } else
379 acl = NULL;
380
Christoph Hellwig431547b2009-11-13 09:52:56 +0000381 error = ext2_set_acl(dentry->d_inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383release_and_out:
384 posix_acl_release(acl);
385 return error;
386}
387
Stephen Hemminger749c72e2010-05-13 17:53:16 -0700388const struct xattr_handler ext2_xattr_acl_access_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700389 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000390 .flags = ACL_TYPE_ACCESS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 .list = ext2_xattr_list_acl_access,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000392 .get = ext2_xattr_get_acl,
393 .set = ext2_xattr_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394};
395
Stephen Hemminger749c72e2010-05-13 17:53:16 -0700396const struct xattr_handler ext2_xattr_acl_default_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700397 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000398 .flags = ACL_TYPE_DEFAULT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 .list = ext2_xattr_list_acl_default,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000400 .get = ext2_xattr_get_acl,
401 .set = ext2_xattr_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};