blob: 71e71c07568f6f0675662ae976a0061c7132b8c7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells00d3b7a2007-04-26 15:57:07 -07002/* AFS security handling
3 *
David Howellsbe080a62017-11-02 15:27:49 +00004 * Copyright (C) 2007, 2017 Red Hat, Inc. All Rights Reserved.
David Howells00d3b7a2007-04-26 15:57:07 -07005 * Written by David Howells (dhowells@redhat.com)
David Howells00d3b7a2007-04-26 15:57:07 -07006 */
7
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/fs.h>
11#include <linux/ctype.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040012#include <linux/sched.h>
David Howellsbe080a62017-11-02 15:27:49 +000013#include <linux/hashtable.h>
David Howells00d3b7a2007-04-26 15:57:07 -070014#include <keys/rxrpc-type.h>
15#include "internal.h"
16
David Howellsbe080a62017-11-02 15:27:49 +000017static DEFINE_HASHTABLE(afs_permits_cache, 10);
18static DEFINE_SPINLOCK(afs_permits_lock);
19
David Howells00d3b7a2007-04-26 15:57:07 -070020/*
21 * get a key
22 */
23struct key *afs_request_key(struct afs_cell *cell)
24{
25 struct key *key;
26
27 _enter("{%x}", key_serial(cell->anonymous_key));
28
29 _debug("key %s", cell->anonymous_key->description);
30 key = request_key(&key_type_rxrpc, cell->anonymous_key->description,
31 NULL);
32 if (IS_ERR(key)) {
33 if (PTR_ERR(key) != -ENOKEY) {
34 _leave(" = %ld", PTR_ERR(key));
35 return key;
36 }
37
38 /* act as anonymous user */
39 _leave(" = {%x} [anon]", key_serial(cell->anonymous_key));
40 return key_get(cell->anonymous_key);
41 } else {
42 /* act as authorised user */
43 _leave(" = {%x} [auth]", key_serial(key));
44 return key;
45 }
46}
47
48/*
David Howellsbe080a62017-11-02 15:27:49 +000049 * Dispose of a list of permits.
David Howells00d3b7a2007-04-26 15:57:07 -070050 */
David Howellsbe080a62017-11-02 15:27:49 +000051static void afs_permits_rcu(struct rcu_head *rcu)
David Howells00d3b7a2007-04-26 15:57:07 -070052{
53 struct afs_permits *permits =
54 container_of(rcu, struct afs_permits, rcu);
David Howellsbe080a62017-11-02 15:27:49 +000055 int i;
David Howells00d3b7a2007-04-26 15:57:07 -070056
David Howellsbe080a62017-11-02 15:27:49 +000057 for (i = 0; i < permits->nr_permits; i++)
58 key_put(permits->permits[i].key);
David Howells00d3b7a2007-04-26 15:57:07 -070059 kfree(permits);
60}
61
62/*
David Howellsbe080a62017-11-02 15:27:49 +000063 * Discard a permission cache.
David Howells00d3b7a2007-04-26 15:57:07 -070064 */
David Howellsbe080a62017-11-02 15:27:49 +000065void afs_put_permits(struct afs_permits *permits)
David Howells00d3b7a2007-04-26 15:57:07 -070066{
David Howellsbe080a62017-11-02 15:27:49 +000067 if (permits && refcount_dec_and_test(&permits->usage)) {
68 spin_lock(&afs_permits_lock);
69 hash_del_rcu(&permits->hash_node);
70 spin_unlock(&afs_permits_lock);
71 call_rcu(&permits->rcu, afs_permits_rcu);
David Howells00d3b7a2007-04-26 15:57:07 -070072 }
David Howells00d3b7a2007-04-26 15:57:07 -070073}
74
75/*
David Howellsbe080a62017-11-02 15:27:49 +000076 * Clear a permit cache on callback break.
David Howells00d3b7a2007-04-26 15:57:07 -070077 */
78void afs_clear_permits(struct afs_vnode *vnode)
79{
80 struct afs_permits *permits;
81
David Howellsbe080a62017-11-02 15:27:49 +000082 spin_lock(&vnode->lock);
83 permits = rcu_dereference_protected(vnode->permit_cache,
84 lockdep_is_held(&vnode->lock));
85 RCU_INIT_POINTER(vnode->permit_cache, NULL);
David Howellsbe080a62017-11-02 15:27:49 +000086 spin_unlock(&vnode->lock);
David Howells00d3b7a2007-04-26 15:57:07 -070087
David Howellsfd711582019-05-10 23:14:41 +010088 afs_put_permits(permits);
David Howells00d3b7a2007-04-26 15:57:07 -070089}
90
91/*
David Howellsbe080a62017-11-02 15:27:49 +000092 * Hash a list of permits. Use simple addition to make it easy to add an extra
93 * one at an as-yet indeterminate position in the list.
David Howells00d3b7a2007-04-26 15:57:07 -070094 */
David Howellsbe080a62017-11-02 15:27:49 +000095static void afs_hash_permits(struct afs_permits *permits)
David Howells00d3b7a2007-04-26 15:57:07 -070096{
David Howellsbe080a62017-11-02 15:27:49 +000097 unsigned long h = permits->nr_permits;
98 int i;
David Howells00d3b7a2007-04-26 15:57:07 -070099
David Howellsbe080a62017-11-02 15:27:49 +0000100 for (i = 0; i < permits->nr_permits; i++) {
101 h += (unsigned long)permits->permits[i].key / sizeof(void *);
102 h += permits->permits[i].access;
David Howells00d3b7a2007-04-26 15:57:07 -0700103 }
104
David Howellsbe080a62017-11-02 15:27:49 +0000105 permits->h = h;
106}
David Howells00d3b7a2007-04-26 15:57:07 -0700107
David Howellsbe080a62017-11-02 15:27:49 +0000108/*
109 * Cache the CallerAccess result obtained from doing a fileserver operation
110 * that returned a vnode status for a particular key. If a callback break
111 * occurs whilst the operation was in progress then we have to ditch the cache
112 * as the ACL *may* have changed.
113 */
114void afs_cache_permit(struct afs_vnode *vnode, struct key *key,
David Howellsa58823a2019-05-09 15:16:10 +0100115 unsigned int cb_break, struct afs_status_cb *scb)
David Howellsbe080a62017-11-02 15:27:49 +0000116{
David Howells1bcab122017-12-01 11:40:43 +0000117 struct afs_permits *permits, *xpermits, *replacement, *zap, *new = NULL;
David Howellsa58823a2019-05-09 15:16:10 +0100118 afs_access_t caller_access = scb->status.caller_access;
David Howellsbe080a62017-11-02 15:27:49 +0000119 size_t size = 0;
120 bool changed = false;
121 int i, j;
David Howells00d3b7a2007-04-26 15:57:07 -0700122
David Howells3b6492d2018-10-20 00:57:57 +0100123 _enter("{%llx:%llu},%x,%x",
David Howellsbe080a62017-11-02 15:27:49 +0000124 vnode->fid.vid, vnode->fid.vnode, key_serial(key), caller_access);
David Howells00d3b7a2007-04-26 15:57:07 -0700125
David Howellsbe080a62017-11-02 15:27:49 +0000126 rcu_read_lock();
David Howells00d3b7a2007-04-26 15:57:07 -0700127
David Howellsbe080a62017-11-02 15:27:49 +0000128 /* Check for the common case first: We got back the same access as last
129 * time we tried and already have it recorded.
130 */
131 permits = rcu_dereference(vnode->permit_cache);
132 if (permits) {
133 if (!permits->invalidated) {
134 for (i = 0; i < permits->nr_permits; i++) {
135 if (permits->permits[i].key < key)
136 continue;
137 if (permits->permits[i].key > key)
138 break;
139 if (permits->permits[i].access != caller_access) {
140 changed = true;
141 break;
142 }
143
David Howells18ac6182018-10-20 00:57:59 +0100144 if (afs_cb_is_broken(cb_break, vnode,
David Howellsf6424042019-05-13 16:14:32 +0100145 rcu_dereference(vnode->cb_interest))) {
David Howellsbe080a62017-11-02 15:27:49 +0000146 changed = true;
147 break;
148 }
149
150 /* The cache is still good. */
151 rcu_read_unlock();
152 return;
David Howells00d3b7a2007-04-26 15:57:07 -0700153 }
David Howellsbe080a62017-11-02 15:27:49 +0000154 }
155
156 changed |= permits->invalidated;
157 size = permits->nr_permits;
158
159 /* If this set of permits is now wrong, clear the permits
160 * pointer so that no one tries to use the stale information.
161 */
162 if (changed) {
163 spin_lock(&vnode->lock);
164 if (permits != rcu_access_pointer(vnode->permit_cache))
165 goto someone_else_changed_it_unlock;
166 RCU_INIT_POINTER(vnode->permit_cache, NULL);
167 spin_unlock(&vnode->lock);
168
169 afs_put_permits(permits);
170 permits = NULL;
171 size = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700172 }
173 }
174
David Howellsf6424042019-05-13 16:14:32 +0100175 if (afs_cb_is_broken(cb_break, vnode, rcu_dereference(vnode->cb_interest)))
David Howellsbe080a62017-11-02 15:27:49 +0000176 goto someone_else_changed_it;
David Howells00d3b7a2007-04-26 15:57:07 -0700177
David Howellsbe080a62017-11-02 15:27:49 +0000178 /* We need a ref on any permits list we want to copy as we'll have to
179 * drop the lock to do memory allocation.
180 */
David Howellsfe342cf2018-04-09 21:12:31 +0100181 if (permits && !refcount_inc_not_zero(&permits->usage))
David Howellsbe080a62017-11-02 15:27:49 +0000182 goto someone_else_changed_it;
David Howells00d3b7a2007-04-26 15:57:07 -0700183
David Howellsbe080a62017-11-02 15:27:49 +0000184 rcu_read_unlock();
David Howells00d3b7a2007-04-26 15:57:07 -0700185
David Howellsbe080a62017-11-02 15:27:49 +0000186 /* Speculatively create a new list with the revised permission set. We
187 * discard this if we find an extant match already in the hash, but
188 * it's easier to compare with memcmp this way.
189 *
190 * We fill in the key pointers at this time, but we don't get the refs
191 * yet.
192 */
193 size++;
194 new = kzalloc(sizeof(struct afs_permits) +
195 sizeof(struct afs_permit) * size, GFP_NOFS);
196 if (!new)
David Howells1bcab122017-12-01 11:40:43 +0000197 goto out_put;
David Howells00d3b7a2007-04-26 15:57:07 -0700198
David Howellsbe080a62017-11-02 15:27:49 +0000199 refcount_set(&new->usage, 1);
200 new->nr_permits = size;
201 i = j = 0;
202 if (permits) {
203 for (i = 0; i < permits->nr_permits; i++) {
204 if (j == i && permits->permits[i].key > key) {
205 new->permits[j].key = key;
206 new->permits[j].access = caller_access;
207 j++;
208 }
209 new->permits[j].key = permits->permits[i].key;
210 new->permits[j].access = permits->permits[i].access;
211 j++;
212 }
213 }
214
215 if (j == i) {
216 new->permits[j].key = key;
217 new->permits[j].access = caller_access;
218 }
219
220 afs_hash_permits(new);
221
David Howellsbe080a62017-11-02 15:27:49 +0000222 /* Now see if the permit list we want is actually already available */
223 spin_lock(&afs_permits_lock);
224
225 hash_for_each_possible(afs_permits_cache, xpermits, hash_node, new->h) {
226 if (xpermits->h != new->h ||
227 xpermits->invalidated ||
228 xpermits->nr_permits != new->nr_permits ||
229 memcmp(xpermits->permits, new->permits,
230 new->nr_permits * sizeof(struct afs_permit)) != 0)
231 continue;
232
233 if (refcount_inc_not_zero(&xpermits->usage)) {
234 replacement = xpermits;
235 goto found;
236 }
237
238 break;
239 }
240
241 for (i = 0; i < new->nr_permits; i++)
242 key_get(new->permits[i].key);
243 hash_add_rcu(afs_permits_cache, &new->hash_node, new->h);
244 replacement = new;
245 new = NULL;
246
247found:
248 spin_unlock(&afs_permits_lock);
249
250 kfree(new);
251
David Howellsf6424042019-05-13 16:14:32 +0100252 rcu_read_lock();
David Howellsbe080a62017-11-02 15:27:49 +0000253 spin_lock(&vnode->lock);
David Howells1bcab122017-12-01 11:40:43 +0000254 zap = rcu_access_pointer(vnode->permit_cache);
David Howellsf6424042019-05-13 16:14:32 +0100255 if (!afs_cb_is_broken(cb_break, vnode, rcu_dereference(vnode->cb_interest)) &&
David Howells1bcab122017-12-01 11:40:43 +0000256 zap == permits)
257 rcu_assign_pointer(vnode->permit_cache, replacement);
258 else
259 zap = replacement;
David Howellsbe080a62017-11-02 15:27:49 +0000260 spin_unlock(&vnode->lock);
David Howellsf6424042019-05-13 16:14:32 +0100261 rcu_read_unlock();
David Howells1bcab122017-12-01 11:40:43 +0000262 afs_put_permits(zap);
263out_put:
David Howellsbe080a62017-11-02 15:27:49 +0000264 afs_put_permits(permits);
265 return;
266
267someone_else_changed_it_unlock:
268 spin_unlock(&vnode->lock);
269someone_else_changed_it:
270 /* Someone else changed the cache under us - don't recheck at this
271 * time.
272 */
David Howellsfe342cf2018-04-09 21:12:31 +0100273 rcu_read_unlock();
David Howellsbe080a62017-11-02 15:27:49 +0000274 return;
David Howells00d3b7a2007-04-26 15:57:07 -0700275}
276
277/*
278 * check with the fileserver to see if the directory or parent directory is
279 * permitted to be accessed with this authorisation, and if so, what access it
280 * is granted
281 */
David Howells0fafdc92017-11-13 16:59:50 +0000282int afs_check_permit(struct afs_vnode *vnode, struct key *key,
283 afs_access_t *_access)
David Howells00d3b7a2007-04-26 15:57:07 -0700284{
285 struct afs_permits *permits;
David Howellsbe080a62017-11-02 15:27:49 +0000286 bool valid = false;
287 int i, ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700288
David Howells3b6492d2018-10-20 00:57:57 +0100289 _enter("{%llx:%llu},%x",
David Howells416351f2007-05-09 02:33:45 -0700290 vnode->fid.vid, vnode->fid.vnode, key_serial(key));
David Howells00d3b7a2007-04-26 15:57:07 -0700291
David Howells00d3b7a2007-04-26 15:57:07 -0700292 /* check the permits to see if we've got one yet */
David Howellsbe080a62017-11-02 15:27:49 +0000293 if (key == vnode->volume->cell->anonymous_key) {
David Howells00d3b7a2007-04-26 15:57:07 -0700294 _debug("anon");
David Howellsbe080a62017-11-02 15:27:49 +0000295 *_access = vnode->status.anon_access;
David Howells00d3b7a2007-04-26 15:57:07 -0700296 valid = true;
297 } else {
David Howells00d3b7a2007-04-26 15:57:07 -0700298 rcu_read_lock();
David Howellsbe080a62017-11-02 15:27:49 +0000299 permits = rcu_dereference(vnode->permit_cache);
David Howells00d3b7a2007-04-26 15:57:07 -0700300 if (permits) {
David Howellsbe080a62017-11-02 15:27:49 +0000301 for (i = 0; i < permits->nr_permits; i++) {
302 if (permits->permits[i].key < key)
303 continue;
304 if (permits->permits[i].key > key)
David Howells00d3b7a2007-04-26 15:57:07 -0700305 break;
David Howellsbe080a62017-11-02 15:27:49 +0000306
307 *_access = permits->permits[i].access;
308 valid = !permits->invalidated;
309 break;
David Howells00d3b7a2007-04-26 15:57:07 -0700310 }
311 }
312 rcu_read_unlock();
313 }
314
315 if (!valid) {
David Howellsbe080a62017-11-02 15:27:49 +0000316 /* Check the status on the file we're actually interested in
317 * (the post-processing will cache the result).
318 */
David Howells00d3b7a2007-04-26 15:57:07 -0700319 _debug("no valid permit");
320
David Howellsa58823a2019-05-09 15:16:10 +0100321 ret = afs_fetch_status(vnode, key, false, _access);
David Howells00d3b7a2007-04-26 15:57:07 -0700322 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700323 *_access = 0;
324 _leave(" = %d", ret);
325 return ret;
326 }
327 }
328
David Howells00d3b7a2007-04-26 15:57:07 -0700329 _leave(" = 0 [access %x]", *_access);
330 return 0;
331}
332
333/*
334 * check the permissions on an AFS file
335 * - AFS ACLs are attached to directories only, and a file is controlled by its
336 * parent directory's ACL
337 */
Al Viro10556cb22011-06-20 19:28:19 -0400338int afs_permission(struct inode *inode, int mask)
David Howells00d3b7a2007-04-26 15:57:07 -0700339{
340 struct afs_vnode *vnode = AFS_FS_I(inode);
Andrew Morton69759452008-02-08 04:20:53 -0800341 afs_access_t uninitialized_var(access);
David Howells00d3b7a2007-04-26 15:57:07 -0700342 struct key *key;
343 int ret;
344
Al Viro10556cb22011-06-20 19:28:19 -0400345 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100346 return -ECHILD;
347
David Howells3b6492d2018-10-20 00:57:57 +0100348 _enter("{{%llx:%llu},%lx},%x,",
David Howells260a9802007-04-26 15:59:35 -0700349 vnode->fid.vid, vnode->fid.vnode, vnode->flags, mask);
David Howells00d3b7a2007-04-26 15:57:07 -0700350
351 key = afs_request_key(vnode->volume->cell);
352 if (IS_ERR(key)) {
353 _leave(" = %ld [key]", PTR_ERR(key));
354 return PTR_ERR(key);
355 }
356
David Howellsc435ee32017-11-02 15:27:49 +0000357 ret = afs_validate(vnode, key);
358 if (ret < 0)
359 goto error;
David Howells260a9802007-04-26 15:59:35 -0700360
David Howells00d3b7a2007-04-26 15:57:07 -0700361 /* check the permits to see if we've got one yet */
362 ret = afs_check_permit(vnode, key, &access);
David Howells260a9802007-04-26 15:59:35 -0700363 if (ret < 0)
364 goto error;
David Howells00d3b7a2007-04-26 15:57:07 -0700365
366 /* interpret the access mask */
367 _debug("REQ %x ACC %x on %s",
368 mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file");
369
370 if (S_ISDIR(inode->i_mode)) {
David Howells378831e2018-05-16 21:25:46 +0100371 if (mask & (MAY_EXEC | MAY_READ | MAY_CHDIR)) {
David Howells00d3b7a2007-04-26 15:57:07 -0700372 if (!(access & AFS_ACE_LOOKUP))
373 goto permission_denied;
David Howells378831e2018-05-16 21:25:46 +0100374 }
375 if (mask & MAY_WRITE) {
David Howells00d3b7a2007-04-26 15:57:07 -0700376 if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */
Marc Dionnefd249822017-07-06 15:50:18 +0100377 AFS_ACE_INSERT))) /* create, mkdir, symlink, rename to */
David Howells00d3b7a2007-04-26 15:57:07 -0700378 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700379 }
380 } else {
381 if (!(access & AFS_ACE_LOOKUP))
382 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000383 if ((mask & MAY_EXEC) && !(inode->i_mode & S_IXUSR))
384 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700385 if (mask & (MAY_EXEC | MAY_READ)) {
386 if (!(access & AFS_ACE_READ))
387 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000388 if (!(inode->i_mode & S_IRUSR))
389 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700390 } else if (mask & MAY_WRITE) {
391 if (!(access & AFS_ACE_WRITE))
392 goto permission_denied;
Marc Dionne627f4692017-03-16 16:27:44 +0000393 if (!(inode->i_mode & S_IWUSR))
394 goto permission_denied;
David Howells00d3b7a2007-04-26 15:57:07 -0700395 }
396 }
397
398 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700399 _leave(" = %d", ret);
400 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700401
402permission_denied:
David Howells260a9802007-04-26 15:59:35 -0700403 ret = -EACCES;
404error:
David Howells00d3b7a2007-04-26 15:57:07 -0700405 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700406 _leave(" = %d", ret);
407 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700408}
David Howellsbe080a62017-11-02 15:27:49 +0000409
410void __exit afs_clean_up_permit_cache(void)
411{
412 int i;
413
414 for (i = 0; i < HASH_SIZE(afs_permits_cache); i++)
415 WARN_ON_ONCE(!hlist_empty(&afs_permits_cache[i]));
416
417}