]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/posix_acl.c
359d70b0e947d332ffd716479ac36f406ba39749
2 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
4 * Fixes from William Schumacher incorporated on 15 March 2001.
5 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
9 * This file contains generic functions for manipulating
10 * POSIX 1003.1e draft standard 17 ACLs.
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/atomic.h>
17 #include <linux/sched.h>
18 #include <linux/posix_acl.h>
19 #include <linux/posix_acl_xattr.h>
20 #include <linux/export.h>
21 #include <linux/user_namespace.h>
23 EXPORT_SYMBOL(posix_acl_init
);
24 EXPORT_SYMBOL(posix_acl_alloc
);
25 EXPORT_SYMBOL(posix_acl_valid
);
26 EXPORT_SYMBOL(posix_acl_equiv_mode
);
27 EXPORT_SYMBOL(posix_acl_from_mode
);
30 * Init a fresh posix_acl
33 posix_acl_init(struct posix_acl
*acl
, int count
)
35 atomic_set(&acl
->a_refcount
, 1);
40 * Allocate a new ACL with the specified number of entries.
43 posix_acl_alloc(int count
, gfp_t flags
)
45 const size_t size
= sizeof(struct posix_acl
) +
46 count
* sizeof(struct posix_acl_entry
);
47 struct posix_acl
*acl
= kmalloc(size
, flags
);
49 posix_acl_init(acl
, count
);
56 static struct posix_acl
*
57 posix_acl_clone(const struct posix_acl
*acl
, gfp_t flags
)
59 struct posix_acl
*clone
= NULL
;
62 int size
= sizeof(struct posix_acl
) + acl
->a_count
*
63 sizeof(struct posix_acl_entry
);
64 clone
= kmemdup(acl
, size
, flags
);
66 atomic_set(&clone
->a_refcount
, 1);
72 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
75 posix_acl_valid(const struct posix_acl
*acl
)
77 const struct posix_acl_entry
*pa
, *pe
;
78 int state
= ACL_USER_OBJ
;
79 kuid_t prev_uid
= INVALID_UID
;
80 kgid_t prev_gid
= INVALID_GID
;
83 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
84 if (pa
->e_perm
& ~(ACL_READ
|ACL_WRITE
|ACL_EXECUTE
))
88 if (state
== ACL_USER_OBJ
) {
95 if (state
!= ACL_USER
)
97 if (!uid_valid(pa
->e_uid
))
99 if (uid_valid(prev_uid
) &&
100 uid_lte(pa
->e_uid
, prev_uid
))
102 prev_uid
= pa
->e_uid
;
107 if (state
== ACL_USER
) {
114 if (state
!= ACL_GROUP
)
116 if (!gid_valid(pa
->e_gid
))
118 if (gid_valid(prev_gid
) &&
119 gid_lte(pa
->e_gid
, prev_gid
))
121 prev_gid
= pa
->e_gid
;
126 if (state
!= ACL_GROUP
)
132 if (state
== ACL_OTHER
||
133 (state
== ACL_GROUP
&& !needs_mask
)) {
149 * Returns 0 if the acl can be exactly represented in the traditional
150 * file mode permission bits, or else 1. Returns -E... on error.
153 posix_acl_equiv_mode(const struct posix_acl
*acl
, umode_t
*mode_p
)
155 const struct posix_acl_entry
*pa
, *pe
;
159 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
162 mode
|= (pa
->e_perm
& S_IRWXO
) << 6;
165 mode
|= (pa
->e_perm
& S_IRWXO
) << 3;
168 mode
|= pa
->e_perm
& S_IRWXO
;
171 mode
= (mode
& ~S_IRWXG
) |
172 ((pa
->e_perm
& S_IRWXO
) << 3);
184 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
189 * Create an ACL representing the file mode permission bits of an inode.
192 posix_acl_from_mode(umode_t mode
, gfp_t flags
)
194 struct posix_acl
*acl
= posix_acl_alloc(3, flags
);
196 return ERR_PTR(-ENOMEM
);
198 acl
->a_entries
[0].e_tag
= ACL_USER_OBJ
;
199 acl
->a_entries
[0].e_perm
= (mode
& S_IRWXU
) >> 6;
201 acl
->a_entries
[1].e_tag
= ACL_GROUP_OBJ
;
202 acl
->a_entries
[1].e_perm
= (mode
& S_IRWXG
) >> 3;
204 acl
->a_entries
[2].e_tag
= ACL_OTHER
;
205 acl
->a_entries
[2].e_perm
= (mode
& S_IRWXO
);
210 * Return 0 if current is granted want access to the inode
211 * by the acl. Returns -E... otherwise.
214 posix_acl_permission(struct inode
*inode
, const struct posix_acl
*acl
, int want
)
216 const struct posix_acl_entry
*pa
, *pe
, *mask_obj
;
219 want
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
| MAY_NOT_BLOCK
;
221 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
224 /* (May have been checked already) */
225 if (uid_eq(inode
->i_uid
, current_fsuid()))
229 if (uid_eq(pa
->e_uid
, current_fsuid()))
233 if (in_group_p(inode
->i_gid
)) {
235 if ((pa
->e_perm
& want
) == want
)
240 if (in_group_p(pa
->e_gid
)) {
242 if ((pa
->e_perm
& want
) == want
)
260 for (mask_obj
= pa
+1; mask_obj
!= pe
; mask_obj
++) {
261 if (mask_obj
->e_tag
== ACL_MASK
) {
262 if ((pa
->e_perm
& mask_obj
->e_perm
& want
) == want
)
269 if ((pa
->e_perm
& want
) == want
)
275 * Modify acl when creating a new inode. The caller must ensure the acl is
276 * only referenced once.
278 * mode_p initially must contain the mode parameter to the open() / creat()
279 * system calls. All permissions that are not granted by the acl are removed.
280 * The permissions in the acl are changed to reflect the mode_p parameter.
282 static int posix_acl_create_masq(struct posix_acl
*acl
, umode_t
*mode_p
)
284 struct posix_acl_entry
*pa
, *pe
;
285 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
286 umode_t mode
= *mode_p
;
289 /* assert(atomic_read(acl->a_refcount) == 1); */
291 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
294 pa
->e_perm
&= (mode
>> 6) | ~S_IRWXO
;
295 mode
&= (pa
->e_perm
<< 6) | ~S_IRWXU
;
308 pa
->e_perm
&= mode
| ~S_IRWXO
;
309 mode
&= pa
->e_perm
| ~S_IRWXO
;
323 mask_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
324 mode
&= (mask_obj
->e_perm
<< 3) | ~S_IRWXG
;
328 group_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
329 mode
&= (group_obj
->e_perm
<< 3) | ~S_IRWXG
;
332 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
337 * Modify the ACL for the chmod syscall.
339 static int posix_acl_chmod_masq(struct posix_acl
*acl
, umode_t mode
)
341 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
342 struct posix_acl_entry
*pa
, *pe
;
344 /* assert(atomic_read(acl->a_refcount) == 1); */
346 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
349 pa
->e_perm
= (mode
& S_IRWXU
) >> 6;
365 pa
->e_perm
= (mode
& S_IRWXO
);
374 mask_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
378 group_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
385 posix_acl_create(struct posix_acl
**acl
, gfp_t gfp
, umode_t
*mode_p
)
387 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
390 err
= posix_acl_create_masq(clone
, mode_p
);
392 posix_acl_release(clone
);
396 posix_acl_release(*acl
);
400 EXPORT_SYMBOL(posix_acl_create
);
403 posix_acl_chmod(struct posix_acl
**acl
, gfp_t gfp
, umode_t mode
)
405 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
408 err
= posix_acl_chmod_masq(clone
, mode
);
410 posix_acl_release(clone
);
414 posix_acl_release(*acl
);
418 EXPORT_SYMBOL(posix_acl_chmod
);
421 * Fix up the uids and gids in posix acl extended attributes in place.
423 static void posix_acl_fix_xattr_userns(
424 struct user_namespace
*to
, struct user_namespace
*from
,
425 void *value
, size_t size
)
427 posix_acl_xattr_header
*header
= (posix_acl_xattr_header
*)value
;
428 posix_acl_xattr_entry
*entry
= (posix_acl_xattr_entry
*)(header
+1), *end
;
435 if (size
< sizeof(posix_acl_xattr_header
))
437 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
440 count
= posix_acl_xattr_count(size
);
446 for (end
= entry
+ count
; entry
!= end
; entry
++) {
447 switch(le16_to_cpu(entry
->e_tag
)) {
449 uid
= make_kuid(from
, le32_to_cpu(entry
->e_id
));
450 entry
->e_id
= cpu_to_le32(from_kuid(to
, uid
));
453 gid
= make_kgid(from
, le32_to_cpu(entry
->e_id
));
454 entry
->e_id
= cpu_to_le32(from_kgid(to
, gid
));
462 void posix_acl_fix_xattr_from_user(void *value
, size_t size
)
464 struct user_namespace
*user_ns
= current_user_ns();
465 if (user_ns
== &init_user_ns
)
467 posix_acl_fix_xattr_userns(&init_user_ns
, user_ns
, value
, size
);
470 void posix_acl_fix_xattr_to_user(void *value
, size_t size
)
472 struct user_namespace
*user_ns
= current_user_ns();
473 if (user_ns
== &init_user_ns
)
475 posix_acl_fix_xattr_userns(user_ns
, &init_user_ns
, value
, size
);
479 * Convert from extended attribute to in-memory representation.
482 posix_acl_from_xattr(struct user_namespace
*user_ns
,
483 const void *value
, size_t size
)
485 posix_acl_xattr_header
*header
= (posix_acl_xattr_header
*)value
;
486 posix_acl_xattr_entry
*entry
= (posix_acl_xattr_entry
*)(header
+1), *end
;
488 struct posix_acl
*acl
;
489 struct posix_acl_entry
*acl_e
;
493 if (size
< sizeof(posix_acl_xattr_header
))
494 return ERR_PTR(-EINVAL
);
495 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
496 return ERR_PTR(-EOPNOTSUPP
);
498 count
= posix_acl_xattr_count(size
);
500 return ERR_PTR(-EINVAL
);
504 acl
= posix_acl_alloc(count
, GFP_NOFS
);
506 return ERR_PTR(-ENOMEM
);
507 acl_e
= acl
->a_entries
;
509 for (end
= entry
+ count
; entry
!= end
; acl_e
++, entry
++) {
510 acl_e
->e_tag
= le16_to_cpu(entry
->e_tag
);
511 acl_e
->e_perm
= le16_to_cpu(entry
->e_perm
);
513 switch(acl_e
->e_tag
) {
523 le32_to_cpu(entry
->e_id
));
524 if (!uid_valid(acl_e
->e_uid
))
530 le32_to_cpu(entry
->e_id
));
531 if (!gid_valid(acl_e
->e_gid
))
542 posix_acl_release(acl
);
543 return ERR_PTR(-EINVAL
);
545 EXPORT_SYMBOL (posix_acl_from_xattr
);
548 * Convert from in-memory to extended attribute representation.
551 posix_acl_to_xattr(struct user_namespace
*user_ns
, const struct posix_acl
*acl
,
552 void *buffer
, size_t size
)
554 posix_acl_xattr_header
*ext_acl
= (posix_acl_xattr_header
*)buffer
;
555 posix_acl_xattr_entry
*ext_entry
= ext_acl
->a_entries
;
558 real_size
= posix_acl_xattr_size(acl
->a_count
);
561 if (real_size
> size
)
564 ext_acl
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
566 for (n
=0; n
< acl
->a_count
; n
++, ext_entry
++) {
567 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
568 ext_entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
569 ext_entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
570 switch(acl_e
->e_tag
) {
573 cpu_to_le32(from_kuid(user_ns
, acl_e
->e_uid
));
577 cpu_to_le32(from_kgid(user_ns
, acl_e
->e_gid
));
580 ext_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
586 EXPORT_SYMBOL (posix_acl_to_xattr
);