blob: 2b00097101b37bdfcf8fd5e4780245ddf8f09c39 [file] [log] [blame]
David Howells00d3b7a2007-04-26 15:57:07 -07001/* AFS security handling
2 *
David Howellsbe080a62017-11-02 15:27:49 +00003 * Copyright (C) 2007, 2017 Red Hat, Inc. All Rights Reserved.
David Howells00d3b7a2007-04-26 15:57:07 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/init.h>
13#include <linux/slab.h>
14#include <linux/fs.h>
15#include <linux/ctype.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
David Howellsbe080a62017-11-02 15:27:49 +000017#include <linux/hashtable.h>
David Howells00d3b7a2007-04-26 15:57:07 -070018#include <keys/rxrpc-type.h>
19#include "internal.h"
20
David Howellsbe080a62017-11-02 15:27:49 +000021static DEFINE_HASHTABLE(afs_permits_cache, 10);
22static DEFINE_SPINLOCK(afs_permits_lock);
23
David Howells00d3b7a2007-04-26 15:57:07 -070024/*
25 * get a key
26 */
27struct key *afs_request_key(struct afs_cell *cell)
28{
29 struct key *key;
30
31 _enter("{%x}", key_serial(cell->anonymous_key));
32
33 _debug("key %s", cell->anonymous_key->description);
34 key = request_key(&key_type_rxrpc, cell->anonymous_key->description,
35 NULL);
36 if (IS_ERR(key)) {
37 if (PTR_ERR(key) != -ENOKEY) {
38 _leave(" = %ld", PTR_ERR(key));
39 return key;
40 }
41
42 /* act as anonymous user */
43 _leave(" = {%x} [anon]", key_serial(cell->anonymous_key));
44 return key_get(cell->anonymous_key);
45 } else {
46 /* act as authorised user */
47 _leave(" = {%x} [auth]", key_serial(key));
48 return key;
49 }
50}
51
52/*
David Howellsbe080a62017-11-02 15:27:49 +000053 * Dispose of a list of permits.
David Howells00d3b7a2007-04-26 15:57:07 -070054 */
David Howellsbe080a62017-11-02 15:27:49 +000055static void afs_permits_rcu(struct rcu_head *rcu)
David Howells00d3b7a2007-04-26 15:57:07 -070056{
57 struct afs_permits *permits =
58 container_of(rcu, struct afs_permits, rcu);
David Howellsbe080a62017-11-02 15:27:49 +000059 int i;
David Howells00d3b7a2007-04-26 15:57:07 -070060
David Howellsbe080a62017-11-02 15:27:49 +000061 for (i = 0; i < permits->nr_permits; i++)
62 key_put(permits->permits[i].key);
David Howells00d3b7a2007-04-26 15:57:07 -070063 kfree(permits);
64}
65
66/*
David Howellsbe080a62017-11-02 15:27:49 +000067 * Discard a permission cache.
David Howells00d3b7a2007-04-26 15:57:07 -070068 */
David Howellsbe080a62017-11-02 15:27:49 +000069void afs_put_permits(struct afs_permits *permits)
David Howells00d3b7a2007-04-26 15:57:07 -070070{
David Howellsbe080a62017-11-02 15:27:49 +000071 if (permits && refcount_dec_and_test(&permits->usage)) {
72 spin_lock(&afs_permits_lock);
73 hash_del_rcu(&permits->hash_node);
74 spin_unlock(&afs_permits_lock);
75 call_rcu(&permits->rcu, afs_permits_rcu);
David Howells00d3b7a2007-04-26 15:57:07 -070076 }
David Howells00d3b7a2007-04-26 15:57:07 -070077}
78
79/*
David Howellsbe080a62017-11-02 15:27:49 +000080 * Clear a permit cache on callback break.
David Howells00d3b7a2007-04-26 15:57:07 -070081 */
82void afs_clear_permits(struct afs_vnode *vnode)
83{
84 struct afs_permits *permits;
85
David Howellsbe080a62017-11-02 15:27:49 +000086 spin_lock(&vnode->lock);
87 permits = rcu_dereference_protected(vnode->permit_cache,
88 lockdep_is_held(&vnode->lock));
89 RCU_INIT_POINTER(vnode->permit_cache, NULL);
David Howellsc435ee32017-11-02 15:27:49 +000090 vnode->cb_break++;
David Howellsbe080a62017-11-02 15:27:49 +000091 spin_unlock(&vnode->lock);
David Howells00d3b7a2007-04-26 15:57:07 -070092
93 if (permits)
David Howellsbe080a62017-11-02 15:27:49 +000094 afs_put_permits(permits);
David Howells00d3b7a2007-04-26 15:57:07 -070095}
96
97/*
David Howellsbe080a62017-11-02 15:27:49 +000098 * Hash a list of permits. Use simple addition to make it easy to add an extra
99 * one at an as-yet indeterminate position in the list.
David Howells00d3b7a2007-04-26 15:57:07 -0700100 */
David Howellsbe080a62017-11-02 15:27:49 +0000101static void afs_hash_permits(struct afs_permits *permits)
David Howells00d3b7a2007-04-26 15:57:07 -0700102{
David Howellsbe080a62017-11-02 15:27:49 +0000103 unsigned long h = permits->nr_permits;
104 int i;
David Howells00d3b7a2007-04-26 15:57:07 -0700105
David Howellsbe080a62017-11-02 15:27:49 +0000106 for (i = 0; i < permits->nr_permits; i++) {
107 h += (unsigned long)permits->permits[i].key / sizeof(void *);
108 h += permits->permits[i].access;
David Howells00d3b7a2007-04-26 15:57:07 -0700109 }
110
David Howellsbe080a62017-11-02 15:27:49 +0000111 permits->h = h;
112}
David Howells00d3b7a2007-04-26 15:57:07 -0700113
David Howellsbe080a62017-11-02 15:27:49 +0000114/*
115 * Cache the CallerAccess result obtained from doing a fileserver operation
116 * that returned a vnode status for a particular key. If a callback break
117 * occurs whilst the operation was in progress then we have to ditch the cache
118 * as the ACL *may* have changed.
119 */
120void afs_cache_permit(struct afs_vnode *vnode, struct key *key,
121 unsigned int cb_break)
122{
123 struct afs_permits *permits, *xpermits, *replacement, *new = NULL;
124 afs_access_t caller_access = READ_ONCE(vnode->status.caller_access);
125 size_t size = 0;
126 bool changed = false;
127 int i, j;
David Howells00d3b7a2007-04-26 15:57:07 -0700128
David Howellsbe080a62017-11-02 15:27:49 +0000129 _enter("{%x:%u},%x,%x",
130 vnode->fid.vid, vnode->fid.vnode, key_serial(key), caller_access);
David Howells00d3b7a2007-04-26 15:57:07 -0700131
David Howellsbe080a62017-11-02 15:27:49 +0000132 rcu_read_lock();
David Howells00d3b7a2007-04-26 15:57:07 -0700133
David Howellsbe080a62017-11-02 15:27:49 +0000134 /* Check for the common case first: We got back the same access as last
135 * time we tried and already have it recorded.
136 */
137 permits = rcu_dereference(vnode->permit_cache);
138 if (permits) {
139 if (!permits->invalidated) {
140 for (i = 0; i < permits->nr_permits; i++) {
141 if (permits->permits[i].key < key)
142 continue;
143 if (permits->permits[i].key > key)
144 break;
145 if (permits->permits[i].access != caller_access) {
146 changed = true;
147 break;
148 }
149
150 if (cb_break != (vnode->cb_break +
151 vnode->cb_interest->server->cb_s_break)) {
152 changed = true;
153 break;
154 }
155
156 /* The cache is still good. */
157 rcu_read_unlock();
158 return;
David Howells00d3b7a2007-04-26 15:57:07 -0700159 }
David Howellsbe080a62017-11-02 15:27:49 +0000160 }
161
162 changed |= permits->invalidated;
163 size = permits->nr_permits;
164
165 /* If this set of permits is now wrong, clear the permits
166 * pointer so that no one tries to use the stale information.
167 */
168 if (changed) {
169 spin_lock(&vnode->lock);
170 if (permits != rcu_access_pointer(vnode->permit_cache))
171 goto someone_else_changed_it_unlock;
172 RCU_INIT_POINTER(vnode->permit_cache, NULL);
173 spin_unlock(&vnode->lock);
174
175 afs_put_permits(permits);
176 permits = NULL;
177 size = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700178 }
179 }
180
David Howellsbe080a62017-11-02 15:27:49 +0000181 if (cb_break != (vnode->cb_break + vnode->cb_interest->server->cb_s_break)) {
182 rcu_read_unlock();
183 goto someone_else_changed_it;
184 }
David Howells00d3b7a2007-04-26 15:57:07 -0700185
David Howellsbe080a62017-11-02 15:27:49 +0000186 /* We need a ref on any permits list we want to copy as we'll have to
187 * drop the lock to do memory allocation.
188 */
189 if (permits && !refcount_inc_not_zero(&permits->usage)) {
190 rcu_read_unlock();
191 goto someone_else_changed_it;
192 }
David Howells00d3b7a2007-04-26 15:57:07 -0700193
David Howellsbe080a62017-11-02 15:27:49 +0000194 rcu_read_unlock();
David Howells00d3b7a2007-04-26 15:57:07 -0700195
David Howellsbe080a62017-11-02 15:27:49 +0000196 /* Speculatively create a new list with the revised permission set. We
197 * discard this if we find an extant match already in the hash, but
198 * it's easier to compare with memcmp this way.
199 *
200 * We fill in the key pointers at this time, but we don't get the refs
201 * yet.
202 */
203 size++;
204 new = kzalloc(sizeof(struct afs_permits) +
205 sizeof(struct afs_permit) * size, GFP_NOFS);
206 if (!new)
207 return;
David Howells00d3b7a2007-04-26 15:57:07 -0700208
David Howellsbe080a62017-11-02 15:27:49 +0000209 refcount_set(&new->usage, 1);
210 new->nr_permits = size;
211 i = j = 0;
212 if (permits) {
213 for (i = 0; i < permits->nr_permits; i++) {
214 if (j == i && permits->permits[i].key > key) {
215 new->permits[j].key = key;
216 new->permits[j].access = caller_access;
217 j++;
218 }
219 new->permits[j].key = permits->permits[i].key;
220 new->permits[j].access = permits->permits[i].access;
221 j++;
222 }
223 }
224
225 if (j == i) {
226 new->permits[j].key = key;
227 new->permits[j].access = caller_access;
228 }
229
230 afs_hash_permits(new);
231
232 afs_put_permits(permits);
233
234 /* Now see if the permit list we want is actually already available */
235 spin_lock(&afs_permits_lock);
236
237 hash_for_each_possible(afs_permits_cache, xpermits, hash_node, new->h) {
238 if (xpermits->h != new->h ||
239 xpermits->invalidated ||
240 xpermits->nr_permits != new->nr_permits ||
241 memcmp(xpermits->permits, new->permits,
242 new->nr_permits * sizeof(struct afs_permit)) != 0)
243 continue;
244
245 if (refcount_inc_not_zero(&xpermits->usage)) {
246 replacement = xpermits;
247 goto found;
248 }
249
250 break;
251 }
252
253 for (i = 0; i < new->nr_permits; i++)
254 key_get(new->permits[i].key);
255 hash_add_rcu(afs_permits_cache, &new->hash_node, new->h);
256 replacement = new;
257 new = NULL;
258
259found:
260 spin_unlock(&afs_permits_lock);
261
262 kfree(new);
263
264 spin_lock(&vnode->lock);
265 if (cb_break != (vnode->cb_break + vnode->cb_interest->server->cb_s_break) ||
266 permits != rcu_access_pointer(vnode->permit_cache))
267 goto someone_else_changed_it_unlock;
268 rcu_assign_pointer(vnode->permit_cache, replacement);
269 spin_unlock(&vnode->lock);
270 afs_put_permits(permits);
271 return;
272
273someone_else_changed_it_unlock:
274 spin_unlock(&vnode->lock);
275someone_else_changed_it:
276 /* Someone else changed the cache under us - don't recheck at this
277 * time.
278 */
279 return;
David Howells00d3b7a2007-04-26 15:57:07 -0700280}
281
282/*
283 * check with the fileserver to see if the directory or parent directory is
284 * permitted to be accessed with this authorisation, and if so, what access it
285 * is granted
286 */
David Howells0fafdc92017-11-13 16:59:50 +0000287int afs_check_permit(struct afs_vnode *vnode, struct key *key,
288 afs_access_t *_access)
David Howells00d3b7a2007-04-26 15:57:07 -0700289{
290 struct afs_permits *permits;
David Howellsbe080a62017-11-02 15:27:49 +0000291 bool valid = false;
292 int i, ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700293
David Howells416351f2007-05-09 02:33:45 -0700294 _enter("{%x:%u},%x",
295 vnode->fid.vid, vnode->fid.vnode, key_serial(key));
David Howells00d3b7a2007-04-26 15:57:07 -0700296
David Howellsbe080a62017-11-02 15:27:49 +0000297 permits = vnode->permit_cache;
David Howells00d3b7a2007-04-26 15:57:07 -0700298
299 /* check the permits to see if we've got one yet */
David Howellsbe080a62017-11-02 15:27:49 +0000300 if (key == vnode->volume->cell->anonymous_key) {
David Howells00d3b7a2007-04-26 15:57:07 -0700301 _debug("anon");
David Howellsbe080a62017-11-02 15:27:49 +0000302 *_access = vnode->status.anon_access;
David Howells00d3b7a2007-04-26 15:57:07 -0700303 valid = true;
304 } else {
David Howells00d3b7a2007-04-26 15:57:07 -0700305 rcu_read_lock();
David Howellsbe080a62017-11-02 15:27:49 +0000306 permits = rcu_dereference(vnode->permit_cache);
David Howells00d3b7a2007-04-26 15:57:07 -0700307 if (permits) {
David Howellsbe080a62017-11-02 15:27:49 +0000308 for (i = 0; i < permits->nr_permits; i++) {
309 if (permits->permits[i].key < key)
310 continue;
311 if (permits->permits[i].key > key)
David Howells00d3b7a2007-04-26 15:57:07 -0700312 break;
David Howellsbe080a62017-11-02 15:27:49 +0000313
314 *_access = permits->permits[i].access;
315 valid = !permits->invalidated;
316 break;
David Howells00d3b7a2007-04-26 15:57:07 -0700317 }
318 }
319 rcu_read_unlock();
320 }
321
322 if (!valid) {
David Howellsbe080a62017-11-02 15:27:49 +0000323 /* Check the status on the file we're actually interested in
324 * (the post-processing will cache the result).
325 */
David Howells00d3b7a2007-04-26 15:57:07 -0700326 _debug("no valid permit");
327
David Howellsd2ddc772017-11-02 15:27:50 +0000328 ret = afs_fetch_status(vnode, key);
David Howells00d3b7a2007-04-26 15:57:07 -0700329 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700330 *_access = 0;
331 _leave(" = %d", ret);
332 return ret;
333 }
David Howells416351f2007-05-09 02:33:45 -0700334 *_access = vnode->status.caller_access;
David Howells00d3b7a2007-04-26 15:57:07 -0700335 }
336
David Howells00d3b7a2007-04-26 15:57:07 -0700337 _leave(" = 0 [access %x]", *_access);
338 return 0;
339}
340
341/*
342 * check the permissions on an AFS file
343 * - AFS ACLs are attached to directories only, and a file is controlled by its
344 * parent directory's ACL
345 */
Al Viro10556cb2011-06-20 19:28:19 -0400346int afs_permission(struct inode *inode, int mask)
David Howells00d3b7a2007-04-26 15:57:07 -0700347{
348 struct afs_vnode *vnode = AFS_FS_I(inode);
Andrew Morton69759452008-02-08 04:20:53 -0800349 afs_access_t uninitialized_var(access);
David Howells00d3b7a2007-04-26 15:57:07 -0700350 struct key *key;
351 int ret;
352
Al Viro10556cb2011-06-20 19:28:19 -0400353 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100354 return -ECHILD;
355
David Howells416351f2007-05-09 02:33:45 -0700356 _enter("{{%x:%u},%lx},%x,",
David Howells260a9802007-04-26 15:59:35 -0700357 vnode->fid.vid, vnode->fid.vnode, vnode->flags, mask);
David Howells00d3b7a2007-04-26 15:57:07 -0700358
359 key = afs_request_key(vnode->volume->cell);
360 if (IS_ERR(key)) {
361 _leave(" = %ld [key]", PTR_ERR(key));
362 return PTR_ERR(key);
363 }
364
David Howellsc435ee32017-11-02 15:27:49 +0000365 ret = afs_validate(vnode, key);
366 if (ret < 0)
367 goto error;
David Howells260a9802007-04-26 15:59:35 -0700368
David Howells00d3b7a2007-04-26 15:57:07 -0700369 /* check the permits to see if we've got one yet */
370 ret = afs_check_permit(vnode, key, &access);
David Howells260a9802007-04-26 15:59:35 -0700371 if (ret < 0)
372 goto error;
David Howells00d3b7a2007-04-26 15:57:07 -0700373
374 /* interpret the access mask */
375 _debug("REQ %x ACC %x on %s",
376 mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file");
377
378 if (S_ISDIR(inode->i_mode)) {
379 if (mask & MAY_EXEC) {
380 if (!(access & AFS_ACE_LOOKUP))
381 goto permission_denied;
382 } else if (mask & MAY_READ) {
Marc Dionnefd249822017-07-06 15:50:18 +0100383 if (!(access & AFS_ACE_LOOKUP))
David Howells00d3b7a2007-04-26 15:57:07 -0700384 goto permission_denied;
385 } else if (mask & MAY_WRITE) {
386 if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */
Marc Dionnefd249822017-07-06 15:50:18 +0100387 AFS_ACE_INSERT))) /* create, mkdir, symlink, rename to */
David Howells00d3b7a2007-04-26 15:57:07 -0700388 goto permission_denied;
389 } else {
390 BUG();
391 }
392 } else {
393 if (!(access & AFS_ACE_LOOKUP))
394 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000395 if ((mask & MAY_EXEC) && !(inode->i_mode & S_IXUSR))
396 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700397 if (mask & (MAY_EXEC | MAY_READ)) {
398 if (!(access & AFS_ACE_READ))
399 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000400 if (!(inode->i_mode & S_IRUSR))
401 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700402 } else if (mask & MAY_WRITE) {
403 if (!(access & AFS_ACE_WRITE))
404 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000405 if (!(inode->i_mode & S_IWUSR))
406 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700407 }
408 }
409
410 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700411 _leave(" = %d", ret);
412 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700413
414permission_denied:
David Howells260a9802007-04-26 15:59:35 -0700415 ret = -EACCES;
416error:
David Howells00d3b7a2007-04-26 15:57:07 -0700417 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700418 _leave(" = %d", ret);
419 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700420}
David Howellsbe080a62017-11-02 15:27:49 +0000421
422void __exit afs_clean_up_permit_cache(void)
423{
424 int i;
425
426 for (i = 0; i < HASH_SIZE(afs_permits_cache); i++)
427 WARN_ON_ONCE(!hlist_empty(&afs_permits_cache[i]));
428
429}