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 <linux/sched.h>
17 #include <keys/rxrpc-type.h>
23 struct key *afs_request_key(struct afs_cell *cell)
27 _enter("{%x}", key_serial(cell->anonymous_key));
29 _debug("key %s", cell->anonymous_key->description);
30 key = request_key(&key_type_rxrpc, cell->anonymous_key->description,
33 if (PTR_ERR(key) != -ENOKEY) {
34 _leave(" = %ld", PTR_ERR(key));
38 /* act as anonymous user */
39 _leave(" = {%x} [anon]", key_serial(cell->anonymous_key));
40 return key_get(cell->anonymous_key);
42 /* act as authorised user */
43 _leave(" = {%x} [auth]", key_serial(key));
49 * dispose of a permits list
51 void afs_zap_permits(struct rcu_head *rcu)
53 struct afs_permits *permits =
54 container_of(rcu, struct afs_permits, rcu);
57 _enter("{%d}", permits->count);
59 for (loop = permits->count - 1; loop >= 0; loop--)
60 key_put(permits->permits[loop].key);
65 * dispose of a permits list in which all the key pointers have been copied
67 static void afs_dispose_of_permits(struct rcu_head *rcu)
69 struct afs_permits *permits =
70 container_of(rcu, struct afs_permits, rcu);
72 _enter("{%d}", permits->count);
78 * get the authorising vnode - this is the specified inode itself if it's a
79 * directory or it's the parent directory if the specified inode is a file or
81 * - the caller must release the ref on the inode
83 static struct afs_vnode *afs_get_auth_inode(struct afs_vnode *vnode,
86 struct afs_vnode *auth_vnode;
87 struct inode *auth_inode;
91 if (S_ISDIR(vnode->vfs_inode.i_mode)) {
92 auth_inode = igrab(&vnode->vfs_inode);
93 ASSERT(auth_inode != NULL);
95 auth_inode = afs_iget(vnode->vfs_inode.i_sb, key,
96 &vnode->status.parent, NULL, NULL);
97 if (IS_ERR(auth_inode))
98 return ERR_PTR(PTR_ERR(auth_inode));
101 auth_vnode = AFS_FS_I(auth_inode);
102 _leave(" = {%x}", auth_vnode->fid.vnode);
107 * clear the permit cache on a directory vnode
109 void afs_clear_permits(struct afs_vnode *vnode)
111 struct afs_permits *permits;
113 _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
115 mutex_lock(&vnode->permits_lock);
116 permits = vnode->permits;
117 rcu_assign_pointer(vnode->permits, NULL);
118 mutex_unlock(&vnode->permits_lock);
121 call_rcu(&permits->rcu, afs_zap_permits);
126 * add the result obtained for a vnode to its or its parent directory's cache
127 * for the key used to access it
129 void afs_cache_permit(struct afs_vnode *vnode, struct key *key, long acl_order)
131 struct afs_permits *permits, *xpermits;
132 struct afs_permit *permit;
133 struct afs_vnode *auth_vnode;
136 _enter("{%x:%u},%x,%lx",
137 vnode->fid.vid, vnode->fid.vnode, key_serial(key), acl_order);
139 auth_vnode = afs_get_auth_inode(vnode, key);
140 if (IS_ERR(auth_vnode)) {
141 _leave(" [get error %ld]", PTR_ERR(auth_vnode));
145 mutex_lock(&auth_vnode->permits_lock);
147 /* guard against a rename being detected whilst we waited for the
149 if (memcmp(&auth_vnode->fid, &vnode->status.parent,
150 sizeof(struct afs_fid)) != 0) {
155 /* have to be careful as the directory's callback may be broken between
156 * us receiving the status we're trying to cache and us getting the
157 * lock to update the cache for the status */
158 if (auth_vnode->acl_order - acl_order > 0) {
159 _debug("ACL changed?");
163 /* always update the anonymous mask */
164 _debug("anon access %x", vnode->status.anon_access);
165 auth_vnode->status.anon_access = vnode->status.anon_access;
166 if (key == vnode->volume->cell->anonymous_key)
169 xpermits = auth_vnode->permits;
172 /* see if the permit is already in the list
173 * - if it is then we just amend the list
175 count = xpermits->count;
176 permit = xpermits->permits;
177 for (loop = count; loop > 0; loop--) {
178 if (permit->key == key) {
179 permit->access_mask =
180 vnode->status.caller_access;
187 permits = kmalloc(sizeof(*permits) + sizeof(*permit) * (count + 1),
192 memcpy(permits->permits, xpermits->permits,
193 count * sizeof(struct afs_permit));
195 _debug("key %x access %x",
196 key_serial(key), vnode->status.caller_access);
197 permits->permits[count].access_mask = vnode->status.caller_access;
198 permits->permits[count].key = key_get(key);
199 permits->count = count + 1;
201 rcu_assign_pointer(auth_vnode->permits, permits);
203 call_rcu(&xpermits->rcu, afs_dispose_of_permits);
206 mutex_unlock(&auth_vnode->permits_lock);
207 iput(&auth_vnode->vfs_inode);
212 * check with the fileserver to see if the directory or parent directory is
213 * permitted to be accessed with this authorisation, and if so, what access it
216 static int afs_check_permit(struct afs_vnode *vnode, struct key *key,
217 afs_access_t *_access)
219 struct afs_permits *permits;
220 struct afs_permit *permit;
221 struct afs_vnode *auth_vnode;
226 vnode->fid.vid, vnode->fid.vnode, key_serial(key));
228 auth_vnode = afs_get_auth_inode(vnode, key);
229 if (IS_ERR(auth_vnode)) {
231 _leave(" = %ld", PTR_ERR(auth_vnode));
232 return PTR_ERR(auth_vnode);
235 ASSERT(S_ISDIR(auth_vnode->vfs_inode.i_mode));
237 /* check the permits to see if we've got one yet */
238 if (key == auth_vnode->volume->cell->anonymous_key) {
240 *_access = auth_vnode->status.anon_access;
245 permits = rcu_dereference(auth_vnode->permits);
247 permit = permits->permits;
248 for (loop = permits->count; loop > 0; loop--) {
249 if (permit->key == key) {
250 _debug("found in cache");
251 *_access = permit->access_mask;
262 /* check the status on the file we're actually interested in
263 * (the post-processing will cache the result on auth_vnode) */
264 _debug("no valid permit");
266 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
267 ret = afs_vnode_fetch_status(vnode, auth_vnode, key);
269 iput(&auth_vnode->vfs_inode);
271 _leave(" = %d", ret);
274 *_access = vnode->status.caller_access;
277 iput(&auth_vnode->vfs_inode);
278 _leave(" = 0 [access %x]", *_access);
283 * check the permissions on an AFS file
284 * - AFS ACLs are attached to directories only, and a file is controlled by its
285 * parent directory's ACL
287 int afs_permission(struct inode *inode, int mask, struct nameidata *nd)
289 struct afs_vnode *vnode = AFS_FS_I(inode);
294 _enter("{{%x:%u},%lx},%x,",
295 vnode->fid.vid, vnode->fid.vnode, vnode->flags, mask);
297 key = afs_request_key(vnode->volume->cell);
299 _leave(" = %ld [key]", PTR_ERR(key));
303 /* if the promise has expired, we need to check the server again */
304 if (!vnode->cb_promised) {
305 _debug("not promised");
306 ret = afs_vnode_fetch_status(vnode, NULL, key);
309 _debug("new promise [fl=%lx]", vnode->flags);
312 /* check the permits to see if we've got one yet */
313 ret = afs_check_permit(vnode, key, &access);
317 /* interpret the access mask */
318 _debug("REQ %x ACC %x on %s",
319 mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file");
321 if (S_ISDIR(inode->i_mode)) {
322 if (mask & MAY_EXEC) {
323 if (!(access & AFS_ACE_LOOKUP))
324 goto permission_denied;
325 } else if (mask & MAY_READ) {
326 if (!(access & AFS_ACE_READ))
327 goto permission_denied;
328 } else if (mask & MAY_WRITE) {
329 if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */
330 AFS_ACE_INSERT | /* create, mkdir, symlink, rename to */
331 AFS_ACE_WRITE))) /* chmod */
332 goto permission_denied;
337 if (!(access & AFS_ACE_LOOKUP))
338 goto permission_denied;
339 if (mask & (MAY_EXEC | MAY_READ)) {
340 if (!(access & AFS_ACE_READ))
341 goto permission_denied;
342 } else if (mask & MAY_WRITE) {
343 if (!(access & AFS_ACE_WRITE))
344 goto permission_denied;
349 ret = generic_permission(inode, mask, NULL);
350 _leave(" = %d", ret);
357 _leave(" = %d", ret);