blob: 81dfedb7879ff9bf56ab4fcca26ef6c90d835de2 [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{
David Howells1bcab122017-12-01 11:40:43 +0000123 struct afs_permits *permits, *xpermits, *replacement, *zap, *new = NULL;
David Howellsbe080a62017-11-02 15:27:49 +0000124 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
David Howells68251f02018-05-12 22:31:33 +0100150 if (cb_break != afs_cb_break_sum(vnode, vnode->cb_interest)) {
David Howellsbe080a62017-11-02 15:27:49 +0000151 changed = true;
152 break;
153 }
154
155 /* The cache is still good. */
156 rcu_read_unlock();
157 return;
David Howells00d3b7a2007-04-26 15:57:07 -0700158 }
David Howellsbe080a62017-11-02 15:27:49 +0000159 }
160
161 changed |= permits->invalidated;
162 size = permits->nr_permits;
163
164 /* If this set of permits is now wrong, clear the permits
165 * pointer so that no one tries to use the stale information.
166 */
167 if (changed) {
168 spin_lock(&vnode->lock);
169 if (permits != rcu_access_pointer(vnode->permit_cache))
170 goto someone_else_changed_it_unlock;
171 RCU_INIT_POINTER(vnode->permit_cache, NULL);
172 spin_unlock(&vnode->lock);
173
174 afs_put_permits(permits);
175 permits = NULL;
176 size = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700177 }
178 }
179
David Howells68251f02018-05-12 22:31:33 +0100180 if (cb_break != afs_cb_break_sum(vnode, vnode->cb_interest))
David Howellsbe080a62017-11-02 15:27:49 +0000181 goto someone_else_changed_it;
David Howells00d3b7a2007-04-26 15:57:07 -0700182
David Howellsbe080a62017-11-02 15:27:49 +0000183 /* We need a ref on any permits list we want to copy as we'll have to
184 * drop the lock to do memory allocation.
185 */
David Howellsfe342cf2018-04-09 21:12:31 +0100186 if (permits && !refcount_inc_not_zero(&permits->usage))
David Howellsbe080a62017-11-02 15:27:49 +0000187 goto someone_else_changed_it;
David Howells00d3b7a2007-04-26 15:57:07 -0700188
David Howellsbe080a62017-11-02 15:27:49 +0000189 rcu_read_unlock();
David Howells00d3b7a2007-04-26 15:57:07 -0700190
David Howellsbe080a62017-11-02 15:27:49 +0000191 /* Speculatively create a new list with the revised permission set. We
192 * discard this if we find an extant match already in the hash, but
193 * it's easier to compare with memcmp this way.
194 *
195 * We fill in the key pointers at this time, but we don't get the refs
196 * yet.
197 */
198 size++;
199 new = kzalloc(sizeof(struct afs_permits) +
200 sizeof(struct afs_permit) * size, GFP_NOFS);
201 if (!new)
David Howells1bcab122017-12-01 11:40:43 +0000202 goto out_put;
David Howells00d3b7a2007-04-26 15:57:07 -0700203
David Howellsbe080a62017-11-02 15:27:49 +0000204 refcount_set(&new->usage, 1);
205 new->nr_permits = size;
206 i = j = 0;
207 if (permits) {
208 for (i = 0; i < permits->nr_permits; i++) {
209 if (j == i && permits->permits[i].key > key) {
210 new->permits[j].key = key;
211 new->permits[j].access = caller_access;
212 j++;
213 }
214 new->permits[j].key = permits->permits[i].key;
215 new->permits[j].access = permits->permits[i].access;
216 j++;
217 }
218 }
219
220 if (j == i) {
221 new->permits[j].key = key;
222 new->permits[j].access = caller_access;
223 }
224
225 afs_hash_permits(new);
226
David Howellsbe080a62017-11-02 15:27:49 +0000227 /* Now see if the permit list we want is actually already available */
228 spin_lock(&afs_permits_lock);
229
230 hash_for_each_possible(afs_permits_cache, xpermits, hash_node, new->h) {
231 if (xpermits->h != new->h ||
232 xpermits->invalidated ||
233 xpermits->nr_permits != new->nr_permits ||
234 memcmp(xpermits->permits, new->permits,
235 new->nr_permits * sizeof(struct afs_permit)) != 0)
236 continue;
237
238 if (refcount_inc_not_zero(&xpermits->usage)) {
239 replacement = xpermits;
240 goto found;
241 }
242
243 break;
244 }
245
246 for (i = 0; i < new->nr_permits; i++)
247 key_get(new->permits[i].key);
248 hash_add_rcu(afs_permits_cache, &new->hash_node, new->h);
249 replacement = new;
250 new = NULL;
251
252found:
253 spin_unlock(&afs_permits_lock);
254
255 kfree(new);
256
257 spin_lock(&vnode->lock);
David Howells1bcab122017-12-01 11:40:43 +0000258 zap = rcu_access_pointer(vnode->permit_cache);
David Howells68251f02018-05-12 22:31:33 +0100259 if (cb_break == afs_cb_break_sum(vnode, vnode->cb_interest) &&
David Howells1bcab122017-12-01 11:40:43 +0000260 zap == permits)
261 rcu_assign_pointer(vnode->permit_cache, replacement);
262 else
263 zap = replacement;
David Howellsbe080a62017-11-02 15:27:49 +0000264 spin_unlock(&vnode->lock);
David Howells1bcab122017-12-01 11:40:43 +0000265 afs_put_permits(zap);
266out_put:
David Howellsbe080a62017-11-02 15:27:49 +0000267 afs_put_permits(permits);
268 return;
269
270someone_else_changed_it_unlock:
271 spin_unlock(&vnode->lock);
272someone_else_changed_it:
273 /* Someone else changed the cache under us - don't recheck at this
274 * time.
275 */
David Howellsfe342cf2018-04-09 21:12:31 +0100276 rcu_read_unlock();
David Howellsbe080a62017-11-02 15:27:49 +0000277 return;
David Howells00d3b7a2007-04-26 15:57:07 -0700278}
279
280/*
281 * check with the fileserver to see if the directory or parent directory is
282 * permitted to be accessed with this authorisation, and if so, what access it
283 * is granted
284 */
David Howells0fafdc92017-11-13 16:59:50 +0000285int afs_check_permit(struct afs_vnode *vnode, struct key *key,
286 afs_access_t *_access)
David Howells00d3b7a2007-04-26 15:57:07 -0700287{
288 struct afs_permits *permits;
David Howellsbe080a62017-11-02 15:27:49 +0000289 bool valid = false;
290 int i, ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700291
David Howells416351f2007-05-09 02:33:45 -0700292 _enter("{%x:%u},%x",
293 vnode->fid.vid, vnode->fid.vnode, key_serial(key));
David Howells00d3b7a2007-04-26 15:57:07 -0700294
David Howells00d3b7a2007-04-26 15:57:07 -0700295 /* check the permits to see if we've got one yet */
David Howellsbe080a62017-11-02 15:27:49 +0000296 if (key == vnode->volume->cell->anonymous_key) {
David Howells00d3b7a2007-04-26 15:57:07 -0700297 _debug("anon");
David Howellsbe080a62017-11-02 15:27:49 +0000298 *_access = vnode->status.anon_access;
David Howells00d3b7a2007-04-26 15:57:07 -0700299 valid = true;
300 } else {
David Howells00d3b7a2007-04-26 15:57:07 -0700301 rcu_read_lock();
David Howellsbe080a62017-11-02 15:27:49 +0000302 permits = rcu_dereference(vnode->permit_cache);
David Howells00d3b7a2007-04-26 15:57:07 -0700303 if (permits) {
David Howellsbe080a62017-11-02 15:27:49 +0000304 for (i = 0; i < permits->nr_permits; i++) {
305 if (permits->permits[i].key < key)
306 continue;
307 if (permits->permits[i].key > key)
David Howells00d3b7a2007-04-26 15:57:07 -0700308 break;
David Howellsbe080a62017-11-02 15:27:49 +0000309
310 *_access = permits->permits[i].access;
311 valid = !permits->invalidated;
312 break;
David Howells00d3b7a2007-04-26 15:57:07 -0700313 }
314 }
315 rcu_read_unlock();
316 }
317
318 if (!valid) {
David Howellsbe080a62017-11-02 15:27:49 +0000319 /* Check the status on the file we're actually interested in
320 * (the post-processing will cache the result).
321 */
David Howells00d3b7a2007-04-26 15:57:07 -0700322 _debug("no valid permit");
323
David Howells0c3a5ac2018-04-06 14:17:24 +0100324 ret = afs_fetch_status(vnode, key, false);
David Howells00d3b7a2007-04-26 15:57:07 -0700325 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700326 *_access = 0;
327 _leave(" = %d", ret);
328 return ret;
329 }
David Howells416351f2007-05-09 02:33:45 -0700330 *_access = vnode->status.caller_access;
David Howells00d3b7a2007-04-26 15:57:07 -0700331 }
332
David Howells00d3b7a2007-04-26 15:57:07 -0700333 _leave(" = 0 [access %x]", *_access);
334 return 0;
335}
336
337/*
338 * check the permissions on an AFS file
339 * - AFS ACLs are attached to directories only, and a file is controlled by its
340 * parent directory's ACL
341 */
Al Viro10556cb22011-06-20 19:28:19 -0400342int afs_permission(struct inode *inode, int mask)
David Howells00d3b7a2007-04-26 15:57:07 -0700343{
344 struct afs_vnode *vnode = AFS_FS_I(inode);
Andrew Morton69759452008-02-08 04:20:53 -0800345 afs_access_t uninitialized_var(access);
David Howells00d3b7a2007-04-26 15:57:07 -0700346 struct key *key;
347 int ret;
348
Al Viro10556cb22011-06-20 19:28:19 -0400349 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100350 return -ECHILD;
351
David Howells416351f2007-05-09 02:33:45 -0700352 _enter("{{%x:%u},%lx},%x,",
David Howells260a9802007-04-26 15:59:35 -0700353 vnode->fid.vid, vnode->fid.vnode, vnode->flags, mask);
David Howells00d3b7a2007-04-26 15:57:07 -0700354
355 key = afs_request_key(vnode->volume->cell);
356 if (IS_ERR(key)) {
357 _leave(" = %ld [key]", PTR_ERR(key));
358 return PTR_ERR(key);
359 }
360
David Howellsc435ee32017-11-02 15:27:49 +0000361 ret = afs_validate(vnode, key);
362 if (ret < 0)
363 goto error;
David Howells260a9802007-04-26 15:59:35 -0700364
David Howells00d3b7a2007-04-26 15:57:07 -0700365 /* check the permits to see if we've got one yet */
366 ret = afs_check_permit(vnode, key, &access);
David Howells260a9802007-04-26 15:59:35 -0700367 if (ret < 0)
368 goto error;
David Howells00d3b7a2007-04-26 15:57:07 -0700369
370 /* interpret the access mask */
371 _debug("REQ %x ACC %x on %s",
372 mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file");
373
374 if (S_ISDIR(inode->i_mode)) {
David Howells378831e2018-05-16 21:25:46 +0100375 if (mask & (MAY_EXEC | MAY_READ | MAY_CHDIR)) {
David Howells00d3b7a2007-04-26 15:57:07 -0700376 if (!(access & AFS_ACE_LOOKUP))
377 goto permission_denied;
David Howells378831e2018-05-16 21:25:46 +0100378 }
379 if (mask & MAY_WRITE) {
David Howells00d3b7a2007-04-26 15:57:07 -0700380 if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */
Marc Dionnefd249822017-07-06 15:50:18 +0100381 AFS_ACE_INSERT))) /* create, mkdir, symlink, rename to */
David Howells00d3b7a2007-04-26 15:57:07 -0700382 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700383 }
384 } else {
385 if (!(access & AFS_ACE_LOOKUP))
386 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000387 if ((mask & MAY_EXEC) && !(inode->i_mode & S_IXUSR))
388 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700389 if (mask & (MAY_EXEC | MAY_READ)) {
390 if (!(access & AFS_ACE_READ))
391 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000392 if (!(inode->i_mode & S_IRUSR))
393 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700394 } else if (mask & MAY_WRITE) {
395 if (!(access & AFS_ACE_WRITE))
396 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000397 if (!(inode->i_mode & S_IWUSR))
398 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700399 }
400 }
401
402 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700403 _leave(" = %d", ret);
404 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700405
406permission_denied:
David Howells260a9802007-04-26 15:59:35 -0700407 ret = -EACCES;
408error:
David Howells00d3b7a2007-04-26 15:57:07 -0700409 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700410 _leave(" = %d", ret);
411 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700412}
David Howellsbe080a62017-11-02 15:27:49 +0000413
414void __exit afs_clean_up_permit_cache(void)
415{
416 int i;
417
418 for (i = 0; i < HASH_SIZE(afs_permits_cache); i++)
419 WARN_ON_ONCE(!hlist_empty(&afs_permits_cache[i]));
420
421}