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}", 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},%x,%lx", vnode->fid.vnode, key_serial(key), acl_order);
137 auth_vnode = afs_get_auth_inode(vnode, key);
138 if (IS_ERR(auth_vnode)) {
139 _leave(" [get error %ld]", PTR_ERR(auth_vnode));
143 mutex_lock(&auth_vnode->permits_lock);
145 /* guard against a rename being detected whilst we waited for the
147 if (memcmp(&auth_vnode->fid, &vnode->status.parent,
148 sizeof(struct afs_fid)) != 0) {
153 /* have to be careful as the directory's callback may be broken between
154 * us receiving the status we're trying to cache and us getting the
155 * lock to update the cache for the status */
156 if (auth_vnode->acl_order - acl_order > 0) {
157 _debug("ACL changed?");
161 /* always update the anonymous mask */
162 _debug("anon access %x", vnode->status.anon_access);
163 auth_vnode->status.anon_access = vnode->status.anon_access;
164 if (key == vnode->volume->cell->anonymous_key)
167 xpermits = auth_vnode->permits;
170 /* see if the permit is already in the list
171 * - if it is then we just amend the list
173 count = xpermits->count;
174 permit = xpermits->permits;
175 for (loop = count; loop > 0; loop--) {
176 if (permit->key == key) {
177 permit->access_mask =
178 vnode->status.caller_access;
185 permits = kmalloc(sizeof(*permits) + sizeof(*permit) * (count + 1),
190 memcpy(permits->permits, xpermits->permits,
191 count * sizeof(struct afs_permit));
193 _debug("key %x access %x",
194 key_serial(key), vnode->status.caller_access);
195 permits->permits[count].access_mask = vnode->status.caller_access;
196 permits->permits[count].key = key_get(key);
197 permits->count = count + 1;
199 rcu_assign_pointer(auth_vnode->permits, permits);
201 call_rcu(&xpermits->rcu, afs_dispose_of_permits);
204 mutex_unlock(&auth_vnode->permits_lock);
205 iput(&auth_vnode->vfs_inode);
210 * check with the fileserver to see if the directory or parent directory is
211 * permitted to be accessed with this authorisation, and if so, what access it
214 static int afs_check_permit(struct afs_vnode *vnode, struct key *key,
215 afs_access_t *_access)
217 struct afs_permits *permits;
218 struct afs_permit *permit;
219 struct afs_vnode *auth_vnode;
225 auth_vnode = afs_get_auth_inode(vnode, key);
226 if (IS_ERR(auth_vnode)) {
228 _leave(" = %ld", PTR_ERR(auth_vnode));
229 return PTR_ERR(auth_vnode);
232 ASSERT(S_ISDIR(auth_vnode->vfs_inode.i_mode));
234 /* check the permits to see if we've got one yet */
235 if (key == auth_vnode->volume->cell->anonymous_key) {
237 *_access = auth_vnode->status.anon_access;
242 permits = rcu_dereference(auth_vnode->permits);
244 permit = permits->permits;
245 for (loop = permits->count; loop > 0; loop--) {
246 if (permit->key == key) {
247 _debug("found in cache");
248 *_access = permit->access_mask;
259 /* check the status on the file we're actually interested in
260 * (the post-processing will cache the result on auth_vnode) */
261 _debug("no valid permit");
263 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
264 ret = afs_vnode_fetch_status(vnode, auth_vnode, key);
266 iput(&auth_vnode->vfs_inode);
268 _leave(" = %d", ret);
273 *_access = vnode->status.caller_access;
274 iput(&auth_vnode->vfs_inode);
275 _leave(" = 0 [access %x]", *_access);
280 * check the permissions on an AFS file
281 * - AFS ACLs are attached to directories only, and a file is controlled by its
282 * parent directory's ACL
284 int afs_permission(struct inode *inode, int mask, struct nameidata *nd)
286 struct afs_vnode *vnode = AFS_FS_I(inode);
291 _enter("{{%x:%x},%lx},%x,",
292 vnode->fid.vid, vnode->fid.vnode, vnode->flags, mask);
294 key = afs_request_key(vnode->volume->cell);
296 _leave(" = %ld [key]", PTR_ERR(key));
300 /* if the promise has expired, we need to check the server again */
301 if (!vnode->cb_promised) {
302 _debug("not promised");
303 ret = afs_vnode_fetch_status(vnode, NULL, key);
306 _debug("new promise [fl=%lx]", vnode->flags);
309 /* check the permits to see if we've got one yet */
310 ret = afs_check_permit(vnode, key, &access);
314 /* interpret the access mask */
315 _debug("REQ %x ACC %x on %s",
316 mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file");
318 if (S_ISDIR(inode->i_mode)) {
319 if (mask & MAY_EXEC) {
320 if (!(access & AFS_ACE_LOOKUP))
321 goto permission_denied;
322 } else if (mask & MAY_READ) {
323 if (!(access & AFS_ACE_READ))
324 goto permission_denied;
325 } else if (mask & MAY_WRITE) {
326 if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */
327 AFS_ACE_INSERT | /* create, mkdir, symlink, rename to */
328 AFS_ACE_WRITE))) /* chmod */
329 goto permission_denied;
334 if (!(access & AFS_ACE_LOOKUP))
335 goto permission_denied;
336 if (mask & (MAY_EXEC | MAY_READ)) {
337 if (!(access & AFS_ACE_READ))
338 goto permission_denied;
339 } else if (mask & MAY_WRITE) {
340 if (!(access & AFS_ACE_WRITE))
341 goto permission_denied;
346 ret = generic_permission(inode, mask, NULL);
347 _leave(" = %d", ret);
354 _leave(" = %d", ret);