4 * Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org>
6 * Fixes from William Schumacher incorporated on 15 March 2001.
7 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
11 * This file contains generic functions for manipulating
12 * POSIX 1003.1e draft standard 17 ACLs.
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <asm/atomic.h>
19 #include <linux/sched.h>
20 #include <linux/posix_acl.h>
21 #include <linux/module.h>
23 #include <linux/errno.h>
25 EXPORT_SYMBOL(posix_acl_alloc
);
26 EXPORT_SYMBOL(posix_acl_clone
);
27 EXPORT_SYMBOL(posix_acl_valid
);
28 EXPORT_SYMBOL(posix_acl_equiv_mode
);
29 EXPORT_SYMBOL(posix_acl_from_mode
);
30 EXPORT_SYMBOL(posix_acl_create_masq
);
31 EXPORT_SYMBOL(posix_acl_chmod_masq
);
32 EXPORT_SYMBOL(posix_acl_masq_nfs_mode
);
33 EXPORT_SYMBOL(posix_acl_permission
);
36 * Allocate a new ACL with the specified number of entries.
39 posix_acl_alloc(int count
, int flags
)
41 const size_t size
= sizeof(struct posix_acl
) +
42 count
* sizeof(struct posix_acl_entry
);
43 struct posix_acl
*acl
= kmalloc(size
, flags
);
45 atomic_set(&acl
->a_refcount
, 1);
55 posix_acl_clone(const struct posix_acl
*acl
, int flags
)
57 struct posix_acl
*clone
= NULL
;
60 int size
= sizeof(struct posix_acl
) + acl
->a_count
*
61 sizeof(struct posix_acl_entry
);
62 clone
= kmalloc(size
, flags
);
64 memcpy(clone
, acl
, size
);
65 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 unsigned int id
= 0; /* keep gcc happy */
82 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
83 if (pa
->e_perm
& ~(ACL_READ
|ACL_WRITE
|ACL_EXECUTE
))
87 if (state
== ACL_USER_OBJ
) {
95 if (state
!= ACL_USER
)
97 if (pa
->e_id
== ACL_UNDEFINED_ID
||
105 if (state
== ACL_USER
) {
113 if (state
!= ACL_GROUP
)
115 if (pa
->e_id
== ACL_UNDEFINED_ID
||
123 if (state
!= ACL_GROUP
)
129 if (state
== ACL_OTHER
||
130 (state
== ACL_GROUP
&& !needs_mask
)) {
146 * Returns 0 if the acl can be exactly represented in the traditional
147 * file mode permission bits, or else 1. Returns -E... on error.
150 posix_acl_equiv_mode(const struct posix_acl
*acl
, mode_t
*mode_p
)
152 const struct posix_acl_entry
*pa
, *pe
;
156 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
159 mode
|= (pa
->e_perm
& S_IRWXO
) << 6;
162 mode
|= (pa
->e_perm
& S_IRWXO
) << 3;
165 mode
|= pa
->e_perm
& S_IRWXO
;
168 mode
= (mode
& ~S_IRWXG
) |
169 ((pa
->e_perm
& S_IRWXO
) << 3);
181 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
186 * Create an ACL representing the file mode permission bits of an inode.
189 posix_acl_from_mode(mode_t mode
, int flags
)
191 struct posix_acl
*acl
= posix_acl_alloc(3, flags
);
193 return ERR_PTR(-ENOMEM
);
195 acl
->a_entries
[0].e_tag
= ACL_USER_OBJ
;
196 acl
->a_entries
[0].e_id
= ACL_UNDEFINED_ID
;
197 acl
->a_entries
[0].e_perm
= (mode
& S_IRWXU
) >> 6;
199 acl
->a_entries
[1].e_tag
= ACL_GROUP_OBJ
;
200 acl
->a_entries
[1].e_id
= ACL_UNDEFINED_ID
;
201 acl
->a_entries
[1].e_perm
= (mode
& S_IRWXG
) >> 3;
203 acl
->a_entries
[2].e_tag
= ACL_OTHER
;
204 acl
->a_entries
[2].e_id
= ACL_UNDEFINED_ID
;
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 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
222 /* (May have been checked already) */
223 if (inode
->i_uid
== current
->fsuid
)
227 if (pa
->e_id
== current
->fsuid
)
231 if (in_group_p(inode
->i_gid
)) {
233 if ((pa
->e_perm
& want
) == want
)
238 if (in_group_p(pa
->e_id
)) {
240 if ((pa
->e_perm
& want
) == want
)
258 for (mask_obj
= pa
+1; mask_obj
!= pe
; mask_obj
++) {
259 if (mask_obj
->e_tag
== ACL_MASK
) {
260 if ((pa
->e_perm
& mask_obj
->e_perm
& want
) == want
)
267 if ((pa
->e_perm
& want
) == want
)
273 * Modify acl when creating a new inode. The caller must ensure the acl is
274 * only referenced once.
276 * mode_p initially must contain the mode parameter to the open() / creat()
277 * system calls. All permissions that are not granted by the acl are removed.
278 * The permissions in the acl are changed to reflect the mode_p parameter.
281 posix_acl_create_masq(struct posix_acl
*acl
, mode_t
*mode_p
)
283 struct posix_acl_entry
*pa
, *pe
;
284 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
285 mode_t mode
= *mode_p
;
288 /* assert(atomic_read(acl->a_refcount) == 1); */
290 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
293 pa
->e_perm
&= (mode
>> 6) | ~S_IRWXO
;
294 mode
&= (pa
->e_perm
<< 6) | ~S_IRWXU
;
307 pa
->e_perm
&= mode
| ~S_IRWXO
;
308 mode
&= pa
->e_perm
| ~S_IRWXO
;
322 mask_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
323 mode
&= (mask_obj
->e_perm
<< 3) | ~S_IRWXG
;
327 group_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
328 mode
&= (group_obj
->e_perm
<< 3) | ~S_IRWXG
;
331 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
336 * Modify the ACL for the chmod syscall.
339 posix_acl_chmod_masq(struct posix_acl
*acl
, mode_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 * Adjust the mode parameter so that NFSv2 grants nobody permissions
386 * that may not be granted by the ACL. This is necessary because NFSv2
387 * may compute access permissions on the client side, and may serve cached
388 * data whenever it assumes access would be granted. Since ACLs may also
389 * be used to deny access to specific users, the minimal permissions
390 * for secure operation over NFSv2 are very restrictive. Permissions
391 * granted to users via Access Control Lists will not be effective over
394 * Privilege escalation can only happen for read operations, as writes are
395 * always carried out on the NFS server, where the proper access checks are
399 posix_acl_masq_nfs_mode(struct posix_acl
*acl
, mode_t
*mode_p
)
401 struct posix_acl_entry
*pa
, *pe
; int min_perm
= S_IRWXO
;
403 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
413 min_perm
&= pa
->e_perm
;
420 *mode_p
= (*mode_p
& ~(S_IRWXG
|S_IRWXO
)) | (min_perm
<< 3) | min_perm
;