1 /* AFS security handling
2 *
3 * Copyright (C) 2007, 2017 Red Hat, Inc. All Rights Reserved.
4 * 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>
16 #include <linux/sched.h>
17 #include <linux/hashtable.h>
18 #include <keys/rxrpc-type.h>
19 #include "internal.h"
20
21 static DEFINE_HASHTABLE(afs_permits_cache, 10);
22 static DEFINE_SPINLOCK(afs_permits_lock);
23
24 /*
25 * get a key
26 */
afs_request_key(struct afs_cell * cell)27 struct 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 /*
53 * Dispose of a list of permits.
54 */
afs_permits_rcu(struct rcu_head * rcu)55 static void afs_permits_rcu(struct rcu_head *rcu)
56 {
57 struct afs_permits *permits =
58 container_of(rcu, struct afs_permits, rcu);
59 int i;
60
61 for (i = 0; i < permits->nr_permits; i++)
62 key_put(permits->permits[i].key);
63 kfree(permits);
64 }
65
66 /*
67 * Discard a permission cache.
68 */
afs_put_permits(struct afs_permits * permits)69 void afs_put_permits(struct afs_permits *permits)
70 {
71 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);
76 }
77 }
78
79 /*
80 * Clear a permit cache on callback break.
81 */
afs_clear_permits(struct afs_vnode * vnode)82 void afs_clear_permits(struct afs_vnode *vnode)
83 {
84 struct afs_permits *permits;
85
86 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);
90 spin_unlock(&vnode->lock);
91
92 afs_put_permits(permits);
93 }
94
95 /*
96 * Hash a list of permits. Use simple addition to make it easy to add an extra
97 * one at an as-yet indeterminate position in the list.
98 */
afs_hash_permits(struct afs_permits * permits)99 static void afs_hash_permits(struct afs_permits *permits)
100 {
101 unsigned long h = permits->nr_permits;
102 int i;
103
104 for (i = 0; i < permits->nr_permits; i++) {
105 h += (unsigned long)permits->permits[i].key / sizeof(void *);
106 h += permits->permits[i].access;
107 }
108
109 permits->h = h;
110 }
111
112 /*
113 * Cache the CallerAccess result obtained from doing a fileserver operation
114 * that returned a vnode status for a particular key. If a callback break
115 * occurs whilst the operation was in progress then we have to ditch the cache
116 * as the ACL *may* have changed.
117 */
afs_cache_permit(struct afs_vnode * vnode,struct key * key,unsigned int cb_break)118 void afs_cache_permit(struct afs_vnode *vnode, struct key *key,
119 unsigned int cb_break)
120 {
121 struct afs_permits *permits, *xpermits, *replacement, *zap, *new = NULL;
122 afs_access_t caller_access = READ_ONCE(vnode->status.caller_access);
123 size_t size = 0;
124 bool changed = false;
125 int i, j;
126
127 _enter("{%x:%u},%x,%x",
128 vnode->fid.vid, vnode->fid.vnode, key_serial(key), caller_access);
129
130 rcu_read_lock();
131
132 /* Check for the common case first: We got back the same access as last
133 * time we tried and already have it recorded.
134 */
135 permits = rcu_dereference(vnode->permit_cache);
136 if (permits) {
137 if (!permits->invalidated) {
138 for (i = 0; i < permits->nr_permits; i++) {
139 if (permits->permits[i].key < key)
140 continue;
141 if (permits->permits[i].key > key)
142 break;
143 if (permits->permits[i].access != caller_access) {
144 changed = true;
145 break;
146 }
147
148 if (cb_break != afs_cb_break_sum(vnode, vnode->cb_interest)) {
149 changed = true;
150 break;
151 }
152
153 /* The cache is still good. */
154 rcu_read_unlock();
155 return;
156 }
157 }
158
159 changed |= permits->invalidated;
160 size = permits->nr_permits;
161
162 /* If this set of permits is now wrong, clear the permits
163 * pointer so that no one tries to use the stale information.
164 */
165 if (changed) {
166 spin_lock(&vnode->lock);
167 if (permits != rcu_access_pointer(vnode->permit_cache))
168 goto someone_else_changed_it_unlock;
169 RCU_INIT_POINTER(vnode->permit_cache, NULL);
170 spin_unlock(&vnode->lock);
171
172 afs_put_permits(permits);
173 permits = NULL;
174 size = 0;
175 }
176 }
177
178 if (cb_break != afs_cb_break_sum(vnode, vnode->cb_interest))
179 goto someone_else_changed_it;
180
181 /* We need a ref on any permits list we want to copy as we'll have to
182 * drop the lock to do memory allocation.
183 */
184 if (permits && !refcount_inc_not_zero(&permits->usage))
185 goto someone_else_changed_it;
186
187 rcu_read_unlock();
188
189 /* Speculatively create a new list with the revised permission set. We
190 * discard this if we find an extant match already in the hash, but
191 * it's easier to compare with memcmp this way.
192 *
193 * We fill in the key pointers at this time, but we don't get the refs
194 * yet.
195 */
196 size++;
197 new = kzalloc(sizeof(struct afs_permits) +
198 sizeof(struct afs_permit) * size, GFP_NOFS);
199 if (!new)
200 goto out_put;
201
202 refcount_set(&new->usage, 1);
203 new->nr_permits = size;
204 i = j = 0;
205 if (permits) {
206 for (i = 0; i < permits->nr_permits; i++) {
207 if (j == i && permits->permits[i].key > key) {
208 new->permits[j].key = key;
209 new->permits[j].access = caller_access;
210 j++;
211 }
212 new->permits[j].key = permits->permits[i].key;
213 new->permits[j].access = permits->permits[i].access;
214 j++;
215 }
216 }
217
218 if (j == i) {
219 new->permits[j].key = key;
220 new->permits[j].access = caller_access;
221 }
222
223 afs_hash_permits(new);
224
225 /* Now see if the permit list we want is actually already available */
226 spin_lock(&afs_permits_lock);
227
228 hash_for_each_possible(afs_permits_cache, xpermits, hash_node, new->h) {
229 if (xpermits->h != new->h ||
230 xpermits->invalidated ||
231 xpermits->nr_permits != new->nr_permits ||
232 memcmp(xpermits->permits, new->permits,
233 new->nr_permits * sizeof(struct afs_permit)) != 0)
234 continue;
235
236 if (refcount_inc_not_zero(&xpermits->usage)) {
237 replacement = xpermits;
238 goto found;
239 }
240
241 break;
242 }
243
244 for (i = 0; i < new->nr_permits; i++)
245 key_get(new->permits[i].key);
246 hash_add_rcu(afs_permits_cache, &new->hash_node, new->h);
247 replacement = new;
248 new = NULL;
249
250 found:
251 spin_unlock(&afs_permits_lock);
252
253 kfree(new);
254
255 spin_lock(&vnode->lock);
256 zap = rcu_access_pointer(vnode->permit_cache);
257 if (cb_break == afs_cb_break_sum(vnode, vnode->cb_interest) &&
258 zap == permits)
259 rcu_assign_pointer(vnode->permit_cache, replacement);
260 else
261 zap = replacement;
262 spin_unlock(&vnode->lock);
263 afs_put_permits(zap);
264 out_put:
265 afs_put_permits(permits);
266 return;
267
268 someone_else_changed_it_unlock:
269 spin_unlock(&vnode->lock);
270 someone_else_changed_it:
271 /* Someone else changed the cache under us - don't recheck at this
272 * time.
273 */
274 rcu_read_unlock();
275 return;
276 }
277
278 /*
279 * check with the fileserver to see if the directory or parent directory is
280 * permitted to be accessed with this authorisation, and if so, what access it
281 * is granted
282 */
afs_check_permit(struct afs_vnode * vnode,struct key * key,afs_access_t * _access)283 int afs_check_permit(struct afs_vnode *vnode, struct key *key,
284 afs_access_t *_access)
285 {
286 struct afs_permits *permits;
287 bool valid = false;
288 int i, ret;
289
290 _enter("{%x:%u},%x",
291 vnode->fid.vid, vnode->fid.vnode, key_serial(key));
292
293 /* check the permits to see if we've got one yet */
294 if (key == vnode->volume->cell->anonymous_key) {
295 _debug("anon");
296 *_access = vnode->status.anon_access;
297 valid = true;
298 } else {
299 rcu_read_lock();
300 permits = rcu_dereference(vnode->permit_cache);
301 if (permits) {
302 for (i = 0; i < permits->nr_permits; i++) {
303 if (permits->permits[i].key < key)
304 continue;
305 if (permits->permits[i].key > key)
306 break;
307
308 *_access = permits->permits[i].access;
309 valid = !permits->invalidated;
310 break;
311 }
312 }
313 rcu_read_unlock();
314 }
315
316 if (!valid) {
317 /* Check the status on the file we're actually interested in
318 * (the post-processing will cache the result).
319 */
320 _debug("no valid permit");
321
322 ret = afs_fetch_status(vnode, key, false);
323 if (ret < 0) {
324 *_access = 0;
325 _leave(" = %d", ret);
326 return ret;
327 }
328 *_access = vnode->status.caller_access;
329 }
330
331 _leave(" = 0 [access %x]", *_access);
332 return 0;
333 }
334
335 /*
336 * check the permissions on an AFS file
337 * - AFS ACLs are attached to directories only, and a file is controlled by its
338 * parent directory's ACL
339 */
afs_permission(struct inode * inode,int mask)340 int afs_permission(struct inode *inode, int mask)
341 {
342 struct afs_vnode *vnode = AFS_FS_I(inode);
343 afs_access_t access;
344 struct key *key;
345 int ret;
346
347 if (mask & MAY_NOT_BLOCK)
348 return -ECHILD;
349
350 _enter("{{%x:%u},%lx},%x,",
351 vnode->fid.vid, vnode->fid.vnode, vnode->flags, mask);
352
353 key = afs_request_key(vnode->volume->cell);
354 if (IS_ERR(key)) {
355 _leave(" = %ld [key]", PTR_ERR(key));
356 return PTR_ERR(key);
357 }
358
359 ret = afs_validate(vnode, key);
360 if (ret < 0)
361 goto error;
362
363 /* check the permits to see if we've got one yet */
364 ret = afs_check_permit(vnode, key, &access);
365 if (ret < 0)
366 goto error;
367
368 /* interpret the access mask */
369 _debug("REQ %x ACC %x on %s",
370 mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file");
371
372 if (S_ISDIR(inode->i_mode)) {
373 if (mask & (MAY_EXEC | MAY_READ | MAY_CHDIR)) {
374 if (!(access & AFS_ACE_LOOKUP))
375 goto permission_denied;
376 }
377 if (mask & MAY_WRITE) {
378 if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */
379 AFS_ACE_INSERT))) /* create, mkdir, symlink, rename to */
380 goto permission_denied;
381 }
382 } else {
383 if (!(access & AFS_ACE_LOOKUP))
384 goto permission_denied;
385 if ((mask & MAY_EXEC) && !(inode->i_mode & S_IXUSR))
386 goto permission_denied;
387 if (mask & (MAY_EXEC | MAY_READ)) {
388 if (!(access & AFS_ACE_READ))
389 goto permission_denied;
390 if (!(inode->i_mode & S_IRUSR))
391 goto permission_denied;
392 } else if (mask & MAY_WRITE) {
393 if (!(access & AFS_ACE_WRITE))
394 goto permission_denied;
395 if (!(inode->i_mode & S_IWUSR))
396 goto permission_denied;
397 }
398 }
399
400 key_put(key);
401 _leave(" = %d", ret);
402 return ret;
403
404 permission_denied:
405 ret = -EACCES;
406 error:
407 key_put(key);
408 _leave(" = %d", ret);
409 return ret;
410 }
411
afs_clean_up_permit_cache(void)412 void __exit afs_clean_up_permit_cache(void)
413 {
414 int i;
415
416 for (i = 0; i < HASH_SIZE(afs_permits_cache); i++)
417 WARN_ON_ONCE(!hlist_empty(&afs_permits_cache[i]));
418
419 }
420