]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
5c8ebd57 | 2 | * Copyright (C) 2002,2003 by Andreas Gruenbacher <[email protected]> |
1da177e4 | 3 | * |
5c8ebd57 CH |
4 | * Fixes from William Schumacher incorporated on 15 March 2001. |
5 | * (Reported by Charles Bertsch, <[email protected]>). | |
1da177e4 LT |
6 | */ |
7 | ||
8 | /* | |
9 | * This file contains generic functions for manipulating | |
10 | * POSIX 1003.1e draft standard 17 ACLs. | |
11 | */ | |
12 | ||
13 | #include <linux/kernel.h> | |
14 | #include <linux/slab.h> | |
60063497 | 15 | #include <linux/atomic.h> |
1da177e4 LT |
16 | #include <linux/fs.h> |
17 | #include <linux/sched.h> | |
18 | #include <linux/posix_acl.h> | |
5c8ebd57 | 19 | #include <linux/posix_acl_xattr.h> |
2aeccbe9 | 20 | #include <linux/xattr.h> |
630d9c47 | 21 | #include <linux/export.h> |
5c8ebd57 | 22 | #include <linux/user_namespace.h> |
1da177e4 | 23 | |
f61f6da0 | 24 | EXPORT_SYMBOL(posix_acl_init); |
1da177e4 | 25 | EXPORT_SYMBOL(posix_acl_alloc); |
1da177e4 LT |
26 | EXPORT_SYMBOL(posix_acl_valid); |
27 | EXPORT_SYMBOL(posix_acl_equiv_mode); | |
28 | EXPORT_SYMBOL(posix_acl_from_mode); | |
1da177e4 | 29 | |
2982baa2 CH |
30 | struct posix_acl *get_acl(struct inode *inode, int type) |
31 | { | |
32 | struct posix_acl *acl; | |
33 | ||
34 | acl = get_cached_acl(inode, type); | |
35 | if (acl != ACL_NOT_CACHED) | |
36 | return acl; | |
37 | ||
38 | if (!IS_POSIXACL(inode)) | |
39 | return NULL; | |
40 | ||
41 | /* | |
42 | * A filesystem can force a ACL callback by just never filling the | |
43 | * ACL cache. But normally you'd fill the cache either at inode | |
44 | * instantiation time, or on the first ->get_acl call. | |
45 | * | |
46 | * If the filesystem doesn't have a get_acl() function at all, we'll | |
47 | * just create the negative cache entry. | |
48 | */ | |
49 | if (!inode->i_op->get_acl) { | |
50 | set_cached_acl(inode, type, NULL); | |
51 | return NULL; | |
52 | } | |
53 | return inode->i_op->get_acl(inode, type); | |
54 | } | |
55 | EXPORT_SYMBOL(get_acl); | |
56 | ||
f61f6da0 CL |
57 | /* |
58 | * Init a fresh posix_acl | |
59 | */ | |
60 | void | |
61 | posix_acl_init(struct posix_acl *acl, int count) | |
62 | { | |
63 | atomic_set(&acl->a_refcount, 1); | |
64 | acl->a_count = count; | |
65 | } | |
66 | ||
1da177e4 LT |
67 | /* |
68 | * Allocate a new ACL with the specified number of entries. | |
69 | */ | |
70 | struct posix_acl * | |
dd0fc66f | 71 | posix_acl_alloc(int count, gfp_t flags) |
1da177e4 LT |
72 | { |
73 | const size_t size = sizeof(struct posix_acl) + | |
74 | count * sizeof(struct posix_acl_entry); | |
75 | struct posix_acl *acl = kmalloc(size, flags); | |
f61f6da0 CL |
76 | if (acl) |
77 | posix_acl_init(acl, count); | |
1da177e4 LT |
78 | return acl; |
79 | } | |
80 | ||
81 | /* | |
82 | * Clone an ACL. | |
83 | */ | |
edde854e | 84 | static struct posix_acl * |
dd0fc66f | 85 | posix_acl_clone(const struct posix_acl *acl, gfp_t flags) |
1da177e4 LT |
86 | { |
87 | struct posix_acl *clone = NULL; | |
88 | ||
89 | if (acl) { | |
90 | int size = sizeof(struct posix_acl) + acl->a_count * | |
91 | sizeof(struct posix_acl_entry); | |
52978be6 AD |
92 | clone = kmemdup(acl, size, flags); |
93 | if (clone) | |
1da177e4 | 94 | atomic_set(&clone->a_refcount, 1); |
1da177e4 LT |
95 | } |
96 | return clone; | |
97 | } | |
98 | ||
99 | /* | |
100 | * Check if an acl is valid. Returns 0 if it is, or -E... otherwise. | |
101 | */ | |
102 | int | |
103 | posix_acl_valid(const struct posix_acl *acl) | |
104 | { | |
105 | const struct posix_acl_entry *pa, *pe; | |
106 | int state = ACL_USER_OBJ; | |
2f6f0654 EB |
107 | kuid_t prev_uid = INVALID_UID; |
108 | kgid_t prev_gid = INVALID_GID; | |
1da177e4 LT |
109 | int needs_mask = 0; |
110 | ||
111 | FOREACH_ACL_ENTRY(pa, acl, pe) { | |
112 | if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE)) | |
113 | return -EINVAL; | |
114 | switch (pa->e_tag) { | |
115 | case ACL_USER_OBJ: | |
116 | if (state == ACL_USER_OBJ) { | |
1da177e4 LT |
117 | state = ACL_USER; |
118 | break; | |
119 | } | |
120 | return -EINVAL; | |
121 | ||
122 | case ACL_USER: | |
123 | if (state != ACL_USER) | |
124 | return -EINVAL; | |
2f6f0654 | 125 | if (!uid_valid(pa->e_uid)) |
1da177e4 | 126 | return -EINVAL; |
2f6f0654 EB |
127 | if (uid_valid(prev_uid) && |
128 | uid_lte(pa->e_uid, prev_uid)) | |
129 | return -EINVAL; | |
130 | prev_uid = pa->e_uid; | |
1da177e4 LT |
131 | needs_mask = 1; |
132 | break; | |
133 | ||
134 | case ACL_GROUP_OBJ: | |
135 | if (state == ACL_USER) { | |
1da177e4 LT |
136 | state = ACL_GROUP; |
137 | break; | |
138 | } | |
139 | return -EINVAL; | |
140 | ||
141 | case ACL_GROUP: | |
142 | if (state != ACL_GROUP) | |
143 | return -EINVAL; | |
2f6f0654 EB |
144 | if (!gid_valid(pa->e_gid)) |
145 | return -EINVAL; | |
146 | if (gid_valid(prev_gid) && | |
147 | gid_lte(pa->e_gid, prev_gid)) | |
1da177e4 | 148 | return -EINVAL; |
2f6f0654 | 149 | prev_gid = pa->e_gid; |
1da177e4 LT |
150 | needs_mask = 1; |
151 | break; | |
152 | ||
153 | case ACL_MASK: | |
154 | if (state != ACL_GROUP) | |
155 | return -EINVAL; | |
156 | state = ACL_OTHER; | |
157 | break; | |
158 | ||
159 | case ACL_OTHER: | |
160 | if (state == ACL_OTHER || | |
161 | (state == ACL_GROUP && !needs_mask)) { | |
162 | state = 0; | |
163 | break; | |
164 | } | |
165 | return -EINVAL; | |
166 | ||
167 | default: | |
168 | return -EINVAL; | |
169 | } | |
170 | } | |
171 | if (state == 0) | |
172 | return 0; | |
173 | return -EINVAL; | |
174 | } | |
175 | ||
176 | /* | |
177 | * Returns 0 if the acl can be exactly represented in the traditional | |
178 | * file mode permission bits, or else 1. Returns -E... on error. | |
179 | */ | |
180 | int | |
d6952123 | 181 | posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p) |
1da177e4 LT |
182 | { |
183 | const struct posix_acl_entry *pa, *pe; | |
d6952123 | 184 | umode_t mode = 0; |
1da177e4 LT |
185 | int not_equiv = 0; |
186 | ||
187 | FOREACH_ACL_ENTRY(pa, acl, pe) { | |
188 | switch (pa->e_tag) { | |
189 | case ACL_USER_OBJ: | |
190 | mode |= (pa->e_perm & S_IRWXO) << 6; | |
191 | break; | |
192 | case ACL_GROUP_OBJ: | |
193 | mode |= (pa->e_perm & S_IRWXO) << 3; | |
194 | break; | |
195 | case ACL_OTHER: | |
196 | mode |= pa->e_perm & S_IRWXO; | |
197 | break; | |
198 | case ACL_MASK: | |
199 | mode = (mode & ~S_IRWXG) | | |
200 | ((pa->e_perm & S_IRWXO) << 3); | |
201 | not_equiv = 1; | |
202 | break; | |
203 | case ACL_USER: | |
204 | case ACL_GROUP: | |
205 | not_equiv = 1; | |
206 | break; | |
207 | default: | |
208 | return -EINVAL; | |
209 | } | |
210 | } | |
211 | if (mode_p) | |
212 | *mode_p = (*mode_p & ~S_IRWXUGO) | mode; | |
213 | return not_equiv; | |
214 | } | |
215 | ||
216 | /* | |
217 | * Create an ACL representing the file mode permission bits of an inode. | |
218 | */ | |
219 | struct posix_acl * | |
3a5fba19 | 220 | posix_acl_from_mode(umode_t mode, gfp_t flags) |
1da177e4 LT |
221 | { |
222 | struct posix_acl *acl = posix_acl_alloc(3, flags); | |
223 | if (!acl) | |
224 | return ERR_PTR(-ENOMEM); | |
225 | ||
226 | acl->a_entries[0].e_tag = ACL_USER_OBJ; | |
1da177e4 LT |
227 | acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6; |
228 | ||
229 | acl->a_entries[1].e_tag = ACL_GROUP_OBJ; | |
1da177e4 LT |
230 | acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3; |
231 | ||
232 | acl->a_entries[2].e_tag = ACL_OTHER; | |
1da177e4 LT |
233 | acl->a_entries[2].e_perm = (mode & S_IRWXO); |
234 | return acl; | |
235 | } | |
236 | ||
237 | /* | |
238 | * Return 0 if current is granted want access to the inode | |
239 | * by the acl. Returns -E... otherwise. | |
240 | */ | |
241 | int | |
242 | posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want) | |
243 | { | |
244 | const struct posix_acl_entry *pa, *pe, *mask_obj; | |
245 | int found = 0; | |
246 | ||
d124b60a AG |
247 | want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK; |
248 | ||
1da177e4 LT |
249 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
250 | switch(pa->e_tag) { | |
251 | case ACL_USER_OBJ: | |
252 | /* (May have been checked already) */ | |
2f6f0654 | 253 | if (uid_eq(inode->i_uid, current_fsuid())) |
1da177e4 LT |
254 | goto check_perm; |
255 | break; | |
256 | case ACL_USER: | |
2f6f0654 | 257 | if (uid_eq(pa->e_uid, current_fsuid())) |
1da177e4 LT |
258 | goto mask; |
259 | break; | |
260 | case ACL_GROUP_OBJ: | |
261 | if (in_group_p(inode->i_gid)) { | |
262 | found = 1; | |
263 | if ((pa->e_perm & want) == want) | |
264 | goto mask; | |
265 | } | |
266 | break; | |
267 | case ACL_GROUP: | |
2f6f0654 | 268 | if (in_group_p(pa->e_gid)) { |
1da177e4 LT |
269 | found = 1; |
270 | if ((pa->e_perm & want) == want) | |
271 | goto mask; | |
272 | } | |
273 | break; | |
274 | case ACL_MASK: | |
275 | break; | |
276 | case ACL_OTHER: | |
277 | if (found) | |
278 | return -EACCES; | |
279 | else | |
280 | goto check_perm; | |
281 | default: | |
282 | return -EIO; | |
283 | } | |
284 | } | |
285 | return -EIO; | |
286 | ||
287 | mask: | |
288 | for (mask_obj = pa+1; mask_obj != pe; mask_obj++) { | |
289 | if (mask_obj->e_tag == ACL_MASK) { | |
290 | if ((pa->e_perm & mask_obj->e_perm & want) == want) | |
291 | return 0; | |
292 | return -EACCES; | |
293 | } | |
294 | } | |
295 | ||
296 | check_perm: | |
297 | if ((pa->e_perm & want) == want) | |
298 | return 0; | |
299 | return -EACCES; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Modify acl when creating a new inode. The caller must ensure the acl is | |
304 | * only referenced once. | |
305 | * | |
306 | * mode_p initially must contain the mode parameter to the open() / creat() | |
307 | * system calls. All permissions that are not granted by the acl are removed. | |
308 | * The permissions in the acl are changed to reflect the mode_p parameter. | |
309 | */ | |
d3fb6120 | 310 | static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p) |
1da177e4 LT |
311 | { |
312 | struct posix_acl_entry *pa, *pe; | |
313 | struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; | |
d3fb6120 | 314 | umode_t mode = *mode_p; |
1da177e4 LT |
315 | int not_equiv = 0; |
316 | ||
317 | /* assert(atomic_read(acl->a_refcount) == 1); */ | |
318 | ||
319 | FOREACH_ACL_ENTRY(pa, acl, pe) { | |
320 | switch(pa->e_tag) { | |
321 | case ACL_USER_OBJ: | |
322 | pa->e_perm &= (mode >> 6) | ~S_IRWXO; | |
323 | mode &= (pa->e_perm << 6) | ~S_IRWXU; | |
324 | break; | |
325 | ||
326 | case ACL_USER: | |
327 | case ACL_GROUP: | |
328 | not_equiv = 1; | |
329 | break; | |
330 | ||
331 | case ACL_GROUP_OBJ: | |
332 | group_obj = pa; | |
333 | break; | |
334 | ||
335 | case ACL_OTHER: | |
336 | pa->e_perm &= mode | ~S_IRWXO; | |
337 | mode &= pa->e_perm | ~S_IRWXO; | |
338 | break; | |
339 | ||
340 | case ACL_MASK: | |
341 | mask_obj = pa; | |
342 | not_equiv = 1; | |
343 | break; | |
344 | ||
345 | default: | |
346 | return -EIO; | |
347 | } | |
348 | } | |
349 | ||
350 | if (mask_obj) { | |
351 | mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO; | |
352 | mode &= (mask_obj->e_perm << 3) | ~S_IRWXG; | |
353 | } else { | |
354 | if (!group_obj) | |
355 | return -EIO; | |
356 | group_obj->e_perm &= (mode >> 3) | ~S_IRWXO; | |
357 | mode &= (group_obj->e_perm << 3) | ~S_IRWXG; | |
358 | } | |
359 | ||
360 | *mode_p = (*mode_p & ~S_IRWXUGO) | mode; | |
361 | return not_equiv; | |
362 | } | |
363 | ||
364 | /* | |
365 | * Modify the ACL for the chmod syscall. | |
366 | */ | |
5bf3258f | 367 | static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode) |
1da177e4 LT |
368 | { |
369 | struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; | |
370 | struct posix_acl_entry *pa, *pe; | |
371 | ||
372 | /* assert(atomic_read(acl->a_refcount) == 1); */ | |
373 | ||
374 | FOREACH_ACL_ENTRY(pa, acl, pe) { | |
375 | switch(pa->e_tag) { | |
376 | case ACL_USER_OBJ: | |
377 | pa->e_perm = (mode & S_IRWXU) >> 6; | |
378 | break; | |
379 | ||
380 | case ACL_USER: | |
381 | case ACL_GROUP: | |
382 | break; | |
383 | ||
384 | case ACL_GROUP_OBJ: | |
385 | group_obj = pa; | |
386 | break; | |
387 | ||
388 | case ACL_MASK: | |
389 | mask_obj = pa; | |
390 | break; | |
391 | ||
392 | case ACL_OTHER: | |
393 | pa->e_perm = (mode & S_IRWXO); | |
394 | break; | |
395 | ||
396 | default: | |
397 | return -EIO; | |
398 | } | |
399 | } | |
400 | ||
401 | if (mask_obj) { | |
402 | mask_obj->e_perm = (mode & S_IRWXG) >> 3; | |
403 | } else { | |
404 | if (!group_obj) | |
405 | return -EIO; | |
406 | group_obj->e_perm = (mode & S_IRWXG) >> 3; | |
407 | } | |
408 | ||
409 | return 0; | |
410 | } | |
bc26ab5f | 411 | |
826cae2f | 412 | int |
37bc1539 | 413 | __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p) |
826cae2f AV |
414 | { |
415 | struct posix_acl *clone = posix_acl_clone(*acl, gfp); | |
416 | int err = -ENOMEM; | |
417 | if (clone) { | |
418 | err = posix_acl_create_masq(clone, mode_p); | |
419 | if (err < 0) { | |
420 | posix_acl_release(clone); | |
421 | clone = NULL; | |
422 | } | |
423 | } | |
424 | posix_acl_release(*acl); | |
425 | *acl = clone; | |
426 | return err; | |
427 | } | |
37bc1539 | 428 | EXPORT_SYMBOL(__posix_acl_create); |
826cae2f | 429 | |
bc26ab5f | 430 | int |
5bf3258f | 431 | __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode) |
bc26ab5f AV |
432 | { |
433 | struct posix_acl *clone = posix_acl_clone(*acl, gfp); | |
434 | int err = -ENOMEM; | |
435 | if (clone) { | |
5bf3258f | 436 | err = __posix_acl_chmod_masq(clone, mode); |
bc26ab5f AV |
437 | if (err) { |
438 | posix_acl_release(clone); | |
439 | clone = NULL; | |
440 | } | |
441 | } | |
442 | posix_acl_release(*acl); | |
443 | *acl = clone; | |
444 | return err; | |
445 | } | |
5bf3258f CH |
446 | EXPORT_SYMBOL(__posix_acl_chmod); |
447 | ||
448 | int | |
37bc1539 | 449 | posix_acl_chmod(struct inode *inode, umode_t mode) |
5bf3258f CH |
450 | { |
451 | struct posix_acl *acl; | |
452 | int ret = 0; | |
453 | ||
454 | if (!IS_POSIXACL(inode)) | |
455 | return 0; | |
456 | if (!inode->i_op->set_acl) | |
457 | return -EOPNOTSUPP; | |
458 | ||
459 | acl = get_acl(inode, ACL_TYPE_ACCESS); | |
460 | if (IS_ERR_OR_NULL(acl)) | |
461 | return PTR_ERR(acl); | |
462 | ||
37bc1539 | 463 | ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode); |
5bf3258f CH |
464 | if (ret) |
465 | return ret; | |
466 | ret = inode->i_op->set_acl(inode, acl, ACL_TYPE_ACCESS); | |
467 | posix_acl_release(acl); | |
468 | return ret; | |
469 | } | |
bc26ab5f | 470 | EXPORT_SYMBOL(posix_acl_chmod); |
5c8ebd57 | 471 | |
37bc1539 CH |
472 | int |
473 | posix_acl_create(struct inode *dir, umode_t *mode, | |
474 | struct posix_acl **default_acl, struct posix_acl **acl) | |
475 | { | |
476 | struct posix_acl *p; | |
477 | int ret; | |
478 | ||
479 | if (S_ISLNK(*mode) || !IS_POSIXACL(dir)) | |
480 | goto no_acl; | |
481 | ||
482 | p = get_acl(dir, ACL_TYPE_DEFAULT); | |
483 | if (IS_ERR(p)) | |
484 | return PTR_ERR(p); | |
485 | ||
486 | if (!p) { | |
487 | *mode &= ~current_umask(); | |
488 | goto no_acl; | |
489 | } | |
490 | ||
491 | *acl = posix_acl_clone(p, GFP_NOFS); | |
492 | if (!*acl) | |
493 | return -ENOMEM; | |
494 | ||
495 | ret = posix_acl_create_masq(*acl, mode); | |
496 | if (ret < 0) { | |
497 | posix_acl_release(*acl); | |
498 | return -ENOMEM; | |
499 | } | |
500 | ||
501 | if (ret == 0) { | |
502 | posix_acl_release(*acl); | |
503 | *acl = NULL; | |
504 | } | |
505 | ||
506 | if (!S_ISDIR(*mode)) { | |
507 | posix_acl_release(p); | |
508 | *default_acl = NULL; | |
509 | } else { | |
510 | *default_acl = p; | |
511 | } | |
512 | return 0; | |
513 | ||
514 | no_acl: | |
515 | *default_acl = NULL; | |
516 | *acl = NULL; | |
517 | return 0; | |
518 | } | |
519 | EXPORT_SYMBOL_GPL(posix_acl_create); | |
520 | ||
5c8ebd57 CH |
521 | /* |
522 | * Fix up the uids and gids in posix acl extended attributes in place. | |
523 | */ | |
524 | static void posix_acl_fix_xattr_userns( | |
525 | struct user_namespace *to, struct user_namespace *from, | |
526 | void *value, size_t size) | |
527 | { | |
528 | posix_acl_xattr_header *header = (posix_acl_xattr_header *)value; | |
529 | posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end; | |
530 | int count; | |
531 | kuid_t uid; | |
532 | kgid_t gid; | |
533 | ||
534 | if (!value) | |
535 | return; | |
536 | if (size < sizeof(posix_acl_xattr_header)) | |
537 | return; | |
538 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) | |
539 | return; | |
540 | ||
541 | count = posix_acl_xattr_count(size); | |
542 | if (count < 0) | |
543 | return; | |
544 | if (count == 0) | |
545 | return; | |
546 | ||
547 | for (end = entry + count; entry != end; entry++) { | |
548 | switch(le16_to_cpu(entry->e_tag)) { | |
549 | case ACL_USER: | |
550 | uid = make_kuid(from, le32_to_cpu(entry->e_id)); | |
551 | entry->e_id = cpu_to_le32(from_kuid(to, uid)); | |
552 | break; | |
553 | case ACL_GROUP: | |
554 | gid = make_kgid(from, le32_to_cpu(entry->e_id)); | |
555 | entry->e_id = cpu_to_le32(from_kgid(to, gid)); | |
556 | break; | |
557 | default: | |
558 | break; | |
559 | } | |
560 | } | |
561 | } | |
562 | ||
563 | void posix_acl_fix_xattr_from_user(void *value, size_t size) | |
564 | { | |
565 | struct user_namespace *user_ns = current_user_ns(); | |
566 | if (user_ns == &init_user_ns) | |
567 | return; | |
568 | posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size); | |
569 | } | |
570 | ||
571 | void posix_acl_fix_xattr_to_user(void *value, size_t size) | |
572 | { | |
573 | struct user_namespace *user_ns = current_user_ns(); | |
574 | if (user_ns == &init_user_ns) | |
575 | return; | |
576 | posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size); | |
577 | } | |
578 | ||
579 | /* | |
580 | * Convert from extended attribute to in-memory representation. | |
581 | */ | |
582 | struct posix_acl * | |
583 | posix_acl_from_xattr(struct user_namespace *user_ns, | |
584 | const void *value, size_t size) | |
585 | { | |
586 | posix_acl_xattr_header *header = (posix_acl_xattr_header *)value; | |
587 | posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end; | |
588 | int count; | |
589 | struct posix_acl *acl; | |
590 | struct posix_acl_entry *acl_e; | |
591 | ||
592 | if (!value) | |
593 | return NULL; | |
594 | if (size < sizeof(posix_acl_xattr_header)) | |
595 | return ERR_PTR(-EINVAL); | |
596 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) | |
597 | return ERR_PTR(-EOPNOTSUPP); | |
598 | ||
599 | count = posix_acl_xattr_count(size); | |
600 | if (count < 0) | |
601 | return ERR_PTR(-EINVAL); | |
602 | if (count == 0) | |
603 | return NULL; | |
604 | ||
605 | acl = posix_acl_alloc(count, GFP_NOFS); | |
606 | if (!acl) | |
607 | return ERR_PTR(-ENOMEM); | |
608 | acl_e = acl->a_entries; | |
609 | ||
610 | for (end = entry + count; entry != end; acl_e++, entry++) { | |
611 | acl_e->e_tag = le16_to_cpu(entry->e_tag); | |
612 | acl_e->e_perm = le16_to_cpu(entry->e_perm); | |
613 | ||
614 | switch(acl_e->e_tag) { | |
615 | case ACL_USER_OBJ: | |
616 | case ACL_GROUP_OBJ: | |
617 | case ACL_MASK: | |
618 | case ACL_OTHER: | |
619 | break; | |
620 | ||
621 | case ACL_USER: | |
622 | acl_e->e_uid = | |
623 | make_kuid(user_ns, | |
624 | le32_to_cpu(entry->e_id)); | |
625 | if (!uid_valid(acl_e->e_uid)) | |
626 | goto fail; | |
627 | break; | |
628 | case ACL_GROUP: | |
629 | acl_e->e_gid = | |
630 | make_kgid(user_ns, | |
631 | le32_to_cpu(entry->e_id)); | |
632 | if (!gid_valid(acl_e->e_gid)) | |
633 | goto fail; | |
634 | break; | |
635 | ||
636 | default: | |
637 | goto fail; | |
638 | } | |
639 | } | |
640 | return acl; | |
641 | ||
642 | fail: | |
643 | posix_acl_release(acl); | |
644 | return ERR_PTR(-EINVAL); | |
645 | } | |
646 | EXPORT_SYMBOL (posix_acl_from_xattr); | |
647 | ||
648 | /* | |
649 | * Convert from in-memory to extended attribute representation. | |
650 | */ | |
651 | int | |
652 | posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, | |
653 | void *buffer, size_t size) | |
654 | { | |
655 | posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer; | |
656 | posix_acl_xattr_entry *ext_entry = ext_acl->a_entries; | |
657 | int real_size, n; | |
658 | ||
659 | real_size = posix_acl_xattr_size(acl->a_count); | |
660 | if (!buffer) | |
661 | return real_size; | |
662 | if (real_size > size) | |
663 | return -ERANGE; | |
664 | ||
665 | ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); | |
666 | ||
667 | for (n=0; n < acl->a_count; n++, ext_entry++) { | |
668 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; | |
669 | ext_entry->e_tag = cpu_to_le16(acl_e->e_tag); | |
670 | ext_entry->e_perm = cpu_to_le16(acl_e->e_perm); | |
671 | switch(acl_e->e_tag) { | |
672 | case ACL_USER: | |
673 | ext_entry->e_id = | |
674 | cpu_to_le32(from_kuid(user_ns, acl_e->e_uid)); | |
675 | break; | |
676 | case ACL_GROUP: | |
677 | ext_entry->e_id = | |
678 | cpu_to_le32(from_kgid(user_ns, acl_e->e_gid)); | |
679 | break; | |
680 | default: | |
681 | ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID); | |
682 | break; | |
683 | } | |
684 | } | |
685 | return real_size; | |
686 | } | |
687 | EXPORT_SYMBOL (posix_acl_to_xattr); | |
2aeccbe9 CH |
688 | |
689 | static int | |
690 | posix_acl_xattr_get(struct dentry *dentry, const char *name, | |
691 | void *value, size_t size, int type) | |
692 | { | |
693 | struct posix_acl *acl; | |
694 | int error; | |
695 | ||
696 | if (!IS_POSIXACL(dentry->d_inode)) | |
697 | return -EOPNOTSUPP; | |
698 | if (S_ISLNK(dentry->d_inode->i_mode)) | |
699 | return -EOPNOTSUPP; | |
700 | ||
701 | acl = get_acl(dentry->d_inode, type); | |
702 | if (IS_ERR(acl)) | |
703 | return PTR_ERR(acl); | |
704 | if (acl == NULL) | |
705 | return -ENODATA; | |
706 | ||
707 | error = posix_acl_to_xattr(&init_user_ns, acl, value, size); | |
708 | posix_acl_release(acl); | |
709 | ||
710 | return error; | |
711 | } | |
712 | ||
713 | static int | |
714 | posix_acl_xattr_set(struct dentry *dentry, const char *name, | |
715 | const void *value, size_t size, int flags, int type) | |
716 | { | |
717 | struct inode *inode = dentry->d_inode; | |
718 | struct posix_acl *acl = NULL; | |
719 | int ret; | |
720 | ||
721 | if (!IS_POSIXACL(inode)) | |
722 | return -EOPNOTSUPP; | |
723 | if (!inode->i_op->set_acl) | |
724 | return -EOPNOTSUPP; | |
725 | ||
726 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) | |
727 | return value ? -EACCES : 0; | |
728 | if (!inode_owner_or_capable(inode)) | |
729 | return -EPERM; | |
730 | ||
731 | if (value) { | |
732 | acl = posix_acl_from_xattr(&init_user_ns, value, size); | |
733 | if (IS_ERR(acl)) | |
734 | return PTR_ERR(acl); | |
735 | ||
736 | if (acl) { | |
737 | ret = posix_acl_valid(acl); | |
738 | if (ret) | |
739 | goto out; | |
740 | } | |
741 | } | |
742 | ||
743 | ret = inode->i_op->set_acl(inode, acl, type); | |
744 | out: | |
745 | posix_acl_release(acl); | |
746 | return ret; | |
747 | } | |
748 | ||
749 | static size_t | |
750 | posix_acl_xattr_list(struct dentry *dentry, char *list, size_t list_size, | |
751 | const char *name, size_t name_len, int type) | |
752 | { | |
753 | const char *xname; | |
754 | size_t size; | |
755 | ||
756 | if (!IS_POSIXACL(dentry->d_inode)) | |
757 | return -EOPNOTSUPP; | |
758 | if (S_ISLNK(dentry->d_inode->i_mode)) | |
759 | return -EOPNOTSUPP; | |
760 | ||
761 | if (type == ACL_TYPE_ACCESS) | |
762 | xname = POSIX_ACL_XATTR_ACCESS; | |
763 | else | |
764 | xname = POSIX_ACL_XATTR_DEFAULT; | |
765 | ||
766 | size = strlen(xname) + 1; | |
767 | if (list && size <= list_size) | |
768 | memcpy(list, xname, size); | |
769 | return size; | |
770 | } | |
771 | ||
772 | const struct xattr_handler posix_acl_access_xattr_handler = { | |
773 | .prefix = POSIX_ACL_XATTR_ACCESS, | |
774 | .flags = ACL_TYPE_ACCESS, | |
775 | .list = posix_acl_xattr_list, | |
776 | .get = posix_acl_xattr_get, | |
777 | .set = posix_acl_xattr_set, | |
778 | }; | |
779 | EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler); | |
780 | ||
781 | const struct xattr_handler posix_acl_default_xattr_handler = { | |
782 | .prefix = POSIX_ACL_XATTR_DEFAULT, | |
783 | .flags = ACL_TYPE_DEFAULT, | |
784 | .list = posix_acl_xattr_list, | |
785 | .get = posix_acl_xattr_get, | |
786 | .set = posix_acl_xattr_set, | |
787 | }; | |
788 | EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler); | |
feda821e CH |
789 | |
790 | int simple_set_acl(struct inode *inode, struct posix_acl *acl, int type) | |
791 | { | |
792 | int error; | |
793 | ||
794 | if (type == ACL_TYPE_ACCESS) { | |
795 | error = posix_acl_equiv_mode(acl, &inode->i_mode); | |
796 | if (error < 0) | |
797 | return 0; | |
798 | if (error == 0) | |
799 | acl = NULL; | |
800 | } | |
801 | ||
802 | inode->i_ctime = CURRENT_TIME; | |
803 | set_cached_acl(inode, type, acl); | |
804 | return 0; | |
805 | } | |
806 | ||
807 | int simple_acl_create(struct inode *dir, struct inode *inode) | |
808 | { | |
809 | struct posix_acl *default_acl, *acl; | |
810 | int error; | |
811 | ||
812 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); | |
813 | if (error) | |
814 | return error; | |
815 | ||
816 | set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl); | |
817 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); | |
818 | ||
819 | if (default_acl) | |
820 | posix_acl_release(default_acl); | |
821 | if (acl) | |
822 | posix_acl_release(acl); | |
823 | return 0; | |
824 | } |