1 /* AFS security handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/init.h>
13 #include <linux/slab.h>
15 #include <linux/ctype.h>
16 #include <keys/rxrpc-type.h>
22 struct key
*afs_request_key(struct afs_cell
*cell
)
26 _enter("{%x}", key_serial(cell
->anonymous_key
));
28 _debug("key %s", cell
->anonymous_key
->description
);
29 key
= request_key(&key_type_rxrpc
, cell
->anonymous_key
->description
,
32 if (PTR_ERR(key
) != -ENOKEY
) {
33 _leave(" = %ld", PTR_ERR(key
));
37 /* act as anonymous user */
38 _leave(" = {%x} [anon]", key_serial(cell
->anonymous_key
));
39 return key_get(cell
->anonymous_key
);
41 /* act as authorised user */
42 _leave(" = {%x} [auth]", key_serial(key
));
48 * dispose of a permits list
50 void afs_zap_permits(struct rcu_head
*rcu
)
52 struct afs_permits
*permits
=
53 container_of(rcu
, struct afs_permits
, rcu
);
56 _enter("{%d}", permits
->count
);
58 for (loop
= permits
->count
- 1; loop
>= 0; loop
--)
59 key_put(permits
->permits
[loop
].key
);
64 * dispose of a permits list in which all the key pointers have been copied
66 static void afs_dispose_of_permits(struct rcu_head
*rcu
)
68 struct afs_permits
*permits
=
69 container_of(rcu
, struct afs_permits
, rcu
);
71 _enter("{%d}", permits
->count
);
77 * get the authorising vnode - this is the specified inode itself if it's a
78 * directory or it's the parent directory if the specified inode is a file or
80 * - the caller must release the ref on the inode
82 static struct afs_vnode
*afs_get_auth_inode(struct afs_vnode
*vnode
,
85 struct afs_vnode
*auth_vnode
;
86 struct inode
*auth_inode
;
90 if (S_ISDIR(vnode
->vfs_inode
.i_mode
)) {
91 auth_inode
= igrab(&vnode
->vfs_inode
);
92 ASSERT(auth_inode
!= NULL
);
94 auth_inode
= afs_iget(vnode
->vfs_inode
.i_sb
, key
,
95 &vnode
->status
.parent
, NULL
, NULL
);
96 if (IS_ERR(auth_inode
))
97 return ERR_PTR(PTR_ERR(auth_inode
));
100 auth_vnode
= AFS_FS_I(auth_inode
);
101 _leave(" = {%x}", auth_vnode
->fid
.vnode
);
106 * clear the permit cache on a directory vnode
108 void afs_clear_permits(struct afs_vnode
*vnode
)
110 struct afs_permits
*permits
;
112 _enter("{%x:%u}", vnode
->fid
.vid
, vnode
->fid
.vnode
);
114 mutex_lock(&vnode
->permits_lock
);
115 permits
= vnode
->permits
;
116 rcu_assign_pointer(vnode
->permits
, NULL
);
117 mutex_unlock(&vnode
->permits_lock
);
120 call_rcu(&permits
->rcu
, afs_zap_permits
);
125 * add the result obtained for a vnode to its or its parent directory's cache
126 * for the key used to access it
128 void afs_cache_permit(struct afs_vnode
*vnode
, struct key
*key
, long acl_order
)
130 struct afs_permits
*permits
, *xpermits
;
131 struct afs_permit
*permit
;
132 struct afs_vnode
*auth_vnode
;
135 _enter("{%x:%u},%x,%lx",
136 vnode
->fid
.vid
, vnode
->fid
.vnode
, key_serial(key
), acl_order
);
138 auth_vnode
= afs_get_auth_inode(vnode
, key
);
139 if (IS_ERR(auth_vnode
)) {
140 _leave(" [get error %ld]", PTR_ERR(auth_vnode
));
144 mutex_lock(&auth_vnode
->permits_lock
);
146 /* guard against a rename being detected whilst we waited for the
148 if (memcmp(&auth_vnode
->fid
, &vnode
->status
.parent
,
149 sizeof(struct afs_fid
)) != 0) {
154 /* have to be careful as the directory's callback may be broken between
155 * us receiving the status we're trying to cache and us getting the
156 * lock to update the cache for the status */
157 if (auth_vnode
->acl_order
- acl_order
> 0) {
158 _debug("ACL changed?");
162 /* always update the anonymous mask */
163 _debug("anon access %x", vnode
->status
.anon_access
);
164 auth_vnode
->status
.anon_access
= vnode
->status
.anon_access
;
165 if (key
== vnode
->volume
->cell
->anonymous_key
)
168 xpermits
= auth_vnode
->permits
;
171 /* see if the permit is already in the list
172 * - if it is then we just amend the list
174 count
= xpermits
->count
;
175 permit
= xpermits
->permits
;
176 for (loop
= count
; loop
> 0; loop
--) {
177 if (permit
->key
== key
) {
178 permit
->access_mask
=
179 vnode
->status
.caller_access
;
186 permits
= kmalloc(sizeof(*permits
) + sizeof(*permit
) * (count
+ 1),
191 memcpy(permits
->permits
, xpermits
->permits
,
192 count
* sizeof(struct afs_permit
));
194 _debug("key %x access %x",
195 key_serial(key
), vnode
->status
.caller_access
);
196 permits
->permits
[count
].access_mask
= vnode
->status
.caller_access
;
197 permits
->permits
[count
].key
= key_get(key
);
198 permits
->count
= count
+ 1;
200 rcu_assign_pointer(auth_vnode
->permits
, permits
);
202 call_rcu(&xpermits
->rcu
, afs_dispose_of_permits
);
205 mutex_unlock(&auth_vnode
->permits_lock
);
206 iput(&auth_vnode
->vfs_inode
);
211 * check with the fileserver to see if the directory or parent directory is
212 * permitted to be accessed with this authorisation, and if so, what access it
215 static int afs_check_permit(struct afs_vnode
*vnode
, struct key
*key
,
216 afs_access_t
*_access
)
218 struct afs_permits
*permits
;
219 struct afs_permit
*permit
;
220 struct afs_vnode
*auth_vnode
;
225 vnode
->fid
.vid
, vnode
->fid
.vnode
, key_serial(key
));
227 auth_vnode
= afs_get_auth_inode(vnode
, key
);
228 if (IS_ERR(auth_vnode
)) {
230 _leave(" = %ld", PTR_ERR(auth_vnode
));
231 return PTR_ERR(auth_vnode
);
234 ASSERT(S_ISDIR(auth_vnode
->vfs_inode
.i_mode
));
236 /* check the permits to see if we've got one yet */
237 if (key
== auth_vnode
->volume
->cell
->anonymous_key
) {
239 *_access
= auth_vnode
->status
.anon_access
;
244 permits
= rcu_dereference(auth_vnode
->permits
);
246 permit
= permits
->permits
;
247 for (loop
= permits
->count
; loop
> 0; loop
--) {
248 if (permit
->key
== key
) {
249 _debug("found in cache");
250 *_access
= permit
->access_mask
;
261 /* check the status on the file we're actually interested in
262 * (the post-processing will cache the result on auth_vnode) */
263 _debug("no valid permit");
265 set_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
);
266 ret
= afs_vnode_fetch_status(vnode
, auth_vnode
, key
);
268 iput(&auth_vnode
->vfs_inode
);
270 _leave(" = %d", ret
);
273 *_access
= vnode
->status
.caller_access
;
276 iput(&auth_vnode
->vfs_inode
);
277 _leave(" = 0 [access %x]", *_access
);
282 * check the permissions on an AFS file
283 * - AFS ACLs are attached to directories only, and a file is controlled by its
284 * parent directory's ACL
286 int afs_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
288 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
293 _enter("{{%x:%u},%lx},%x,",
294 vnode
->fid
.vid
, vnode
->fid
.vnode
, vnode
->flags
, mask
);
296 key
= afs_request_key(vnode
->volume
->cell
);
298 _leave(" = %ld [key]", PTR_ERR(key
));
302 /* if the promise has expired, we need to check the server again */
303 if (!vnode
->cb_promised
) {
304 _debug("not promised");
305 ret
= afs_vnode_fetch_status(vnode
, NULL
, key
);
308 _debug("new promise [fl=%lx]", vnode
->flags
);
311 /* check the permits to see if we've got one yet */
312 ret
= afs_check_permit(vnode
, key
, &access
);
316 /* interpret the access mask */
317 _debug("REQ %x ACC %x on %s",
318 mask
, access
, S_ISDIR(inode
->i_mode
) ? "dir" : "file");
320 if (S_ISDIR(inode
->i_mode
)) {
321 if (mask
& MAY_EXEC
) {
322 if (!(access
& AFS_ACE_LOOKUP
))
323 goto permission_denied
;
324 } else if (mask
& MAY_READ
) {
325 if (!(access
& AFS_ACE_READ
))
326 goto permission_denied
;
327 } else if (mask
& MAY_WRITE
) {
328 if (!(access
& (AFS_ACE_DELETE
| /* rmdir, unlink, rename from */
329 AFS_ACE_INSERT
| /* create, mkdir, symlink, rename to */
330 AFS_ACE_WRITE
))) /* chmod */
331 goto permission_denied
;
336 if (!(access
& AFS_ACE_LOOKUP
))
337 goto permission_denied
;
338 if (mask
& (MAY_EXEC
| MAY_READ
)) {
339 if (!(access
& AFS_ACE_READ
))
340 goto permission_denied
;
341 } else if (mask
& MAY_WRITE
) {
342 if (!(access
& AFS_ACE_WRITE
))
343 goto permission_denied
;
348 ret
= generic_permission(inode
, mask
, NULL
);
349 _leave(" = %d", ret
);
356 _leave(" = %d", ret
);