]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/ext2/acl.c
52c053763942eb0db19f72b7fb4618bbeeffd012
[mirror_ubuntu-zesty-kernel.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/capability.h>
8 #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 */
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 = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
93 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 /*
129 * inode->i_mutex: don't care
130 */
131 struct posix_acl *
132 ext2_get_acl(struct inode *inode, int type)
133 {
134 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
142 acl = get_cached_acl(inode, type);
143 if (acl != ACL_NOT_CACHED)
144 return acl;
145
146 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();
155 }
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);
169 kfree(value);
170
171 if (!IS_ERR(acl))
172 set_cached_acl(inode, type, acl);
173
174 return acl;
175 }
176
177 /*
178 * inode->i_mutex: down
179 */
180 static int
181 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
182 {
183 int name_index;
184 void *value = NULL;
185 size_t size = 0;
186 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 inode->i_ctime = CURRENT_TIME_SEC;
204 mark_inode_dirty(inode);
205 if (error == 0)
206 acl = NULL;
207 }
208 }
209 break;
210
211 case ACL_TYPE_DEFAULT:
212 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
213 if (!S_ISDIR(inode->i_mode))
214 return acl ? -EACCES : 0;
215 break;
216
217 default:
218 return -EINVAL;
219 }
220 if (acl) {
221 value = ext2_acl_to_disk(acl, &size);
222 if (IS_ERR(value))
223 return (int)PTR_ERR(value);
224 }
225
226 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
227
228 kfree(value);
229 if (!error)
230 set_cached_acl(inode, type, acl);
231 return error;
232 }
233
234 /*
235 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
236 *
237 * dir->i_mutex: down
238 * inode->i_mutex: up (access to inode is still exclusive)
239 */
240 int
241 ext2_init_acl(struct inode *inode, struct inode *dir)
242 {
243 struct posix_acl *acl = NULL;
244 int error = 0;
245
246 if (!S_ISLNK(inode->i_mode)) {
247 if (test_opt(dir->i_sb, POSIX_ACL)) {
248 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
249 if (IS_ERR(acl))
250 return PTR_ERR(acl);
251 }
252 if (!acl)
253 inode->i_mode &= ~current_umask();
254 }
255 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
256 mode_t mode = inode->i_mode;
257 if (S_ISDIR(inode->i_mode)) {
258 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
259 if (error)
260 goto cleanup;
261 }
262 error = posix_acl_create(&acl, GFP_KERNEL, &mode);
263 if (error < 0)
264 return error;
265 inode->i_mode = mode;
266 if (error > 0) {
267 /* This is an extended ACL */
268 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
269 }
270 }
271 cleanup:
272 posix_acl_release(acl);
273 return error;
274 }
275
276 /*
277 * Does chmod for an inode that may have an Access Control List. The
278 * inode->i_mode field must be updated to the desired value by the caller
279 * before calling this function.
280 * Returns 0 on success, or a negative error number.
281 *
282 * We change the ACL rather than storing some ACL entries in the file
283 * mode permission bits (which would be more efficient), because that
284 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
285 * for directories) are added. There are no more bits available in the
286 * file mode.
287 *
288 * inode->i_mutex: down
289 */
290 int
291 ext2_acl_chmod(struct inode *inode)
292 {
293 struct posix_acl *acl;
294 int error;
295
296 if (!test_opt(inode->i_sb, POSIX_ACL))
297 return 0;
298 if (S_ISLNK(inode->i_mode))
299 return -EOPNOTSUPP;
300 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
301 if (IS_ERR(acl) || !acl)
302 return PTR_ERR(acl);
303 error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
304 if (error)
305 return error;
306 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
307 posix_acl_release(acl);
308 return error;
309 }
310
311 /*
312 * Extended attribut handlers
313 */
314 static size_t
315 ext2_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_size,
316 const char *name, size_t name_len, int type)
317 {
318 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
319
320 if (!test_opt(dentry->d_sb, POSIX_ACL))
321 return 0;
322 if (list && size <= list_size)
323 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
324 return size;
325 }
326
327 static size_t
328 ext2_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_size,
329 const char *name, size_t name_len, int type)
330 {
331 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
332
333 if (!test_opt(dentry->d_sb, POSIX_ACL))
334 return 0;
335 if (list && size <= list_size)
336 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
337 return size;
338 }
339
340 static int
341 ext2_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
342 size_t size, int type)
343 {
344 struct posix_acl *acl;
345 int error;
346
347 if (strcmp(name, "") != 0)
348 return -EINVAL;
349 if (!test_opt(dentry->d_sb, POSIX_ACL))
350 return -EOPNOTSUPP;
351
352 acl = ext2_get_acl(dentry->d_inode, type);
353 if (IS_ERR(acl))
354 return PTR_ERR(acl);
355 if (acl == NULL)
356 return -ENODATA;
357 error = posix_acl_to_xattr(acl, buffer, size);
358 posix_acl_release(acl);
359
360 return error;
361 }
362
363 static int
364 ext2_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
365 size_t size, int flags, int type)
366 {
367 struct posix_acl *acl;
368 int error;
369
370 if (strcmp(name, "") != 0)
371 return -EINVAL;
372 if (!test_opt(dentry->d_sb, POSIX_ACL))
373 return -EOPNOTSUPP;
374 if (!inode_owner_or_capable(dentry->d_inode))
375 return -EPERM;
376
377 if (value) {
378 acl = posix_acl_from_xattr(value, size);
379 if (IS_ERR(acl))
380 return PTR_ERR(acl);
381 else if (acl) {
382 error = posix_acl_valid(acl);
383 if (error)
384 goto release_and_out;
385 }
386 } else
387 acl = NULL;
388
389 error = ext2_set_acl(dentry->d_inode, type, acl);
390
391 release_and_out:
392 posix_acl_release(acl);
393 return error;
394 }
395
396 const struct xattr_handler ext2_xattr_acl_access_handler = {
397 .prefix = POSIX_ACL_XATTR_ACCESS,
398 .flags = ACL_TYPE_ACCESS,
399 .list = ext2_xattr_list_acl_access,
400 .get = ext2_xattr_get_acl,
401 .set = ext2_xattr_set_acl,
402 };
403
404 const struct xattr_handler ext2_xattr_acl_default_handler = {
405 .prefix = POSIX_ACL_XATTR_DEFAULT,
406 .flags = ACL_TYPE_DEFAULT,
407 .list = ext2_xattr_list_acl_default,
408 .get = ext2_xattr_get_acl,
409 .set = ext2_xattr_set_acl,
410 };