David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 1 | /* AFS security handling |
| 2 | * |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 3 | * Copyright (C) 2007, 2017 Red Hat, Inc. All Rights Reserved. |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 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> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 17 | #include <linux/hashtable.h> |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 18 | #include <keys/rxrpc-type.h> |
| 19 | #include "internal.h" |
| 20 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 21 | static DEFINE_HASHTABLE(afs_permits_cache, 10); |
| 22 | static DEFINE_SPINLOCK(afs_permits_lock); |
| 23 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 24 | /* |
| 25 | * get a key |
| 26 | */ |
| 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 | /* |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 53 | * Dispose of a list of permits. |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 54 | */ |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 55 | static void afs_permits_rcu(struct rcu_head *rcu) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 56 | { |
| 57 | struct afs_permits *permits = |
| 58 | container_of(rcu, struct afs_permits, rcu); |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 59 | int i; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 60 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 61 | for (i = 0; i < permits->nr_permits; i++) |
| 62 | key_put(permits->permits[i].key); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 63 | kfree(permits); |
| 64 | } |
| 65 | |
| 66 | /* |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 67 | * Discard a permission cache. |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 68 | */ |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 69 | void afs_put_permits(struct afs_permits *permits) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 70 | { |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 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); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 76 | } |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 80 | * Clear a permit cache on callback break. |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 81 | */ |
| 82 | void afs_clear_permits(struct afs_vnode *vnode) |
| 83 | { |
| 84 | struct afs_permits *permits; |
| 85 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 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); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 90 | vnode->cb_break++; |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 91 | spin_unlock(&vnode->lock); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 92 | |
| 93 | if (permits) |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 94 | afs_put_permits(permits); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 98 | * 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 Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 100 | */ |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 101 | static void afs_hash_permits(struct afs_permits *permits) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 102 | { |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 103 | unsigned long h = permits->nr_permits; |
| 104 | int i; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 105 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 106 | 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 Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 109 | } |
| 110 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 111 | permits->h = h; |
| 112 | } |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 113 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 114 | /* |
| 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 | */ |
| 120 | void afs_cache_permit(struct afs_vnode *vnode, struct key *key, |
| 121 | unsigned int cb_break) |
| 122 | { |
David Howells | 1bcab12 | 2017-12-01 11:40:43 +0000 | [diff] [blame] | 123 | struct afs_permits *permits, *xpermits, *replacement, *zap, *new = NULL; |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 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 Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 128 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 129 | _enter("{%x:%u},%x,%x", |
| 130 | vnode->fid.vid, vnode->fid.vnode, key_serial(key), caller_access); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 131 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 132 | rcu_read_lock(); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 133 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 134 | /* 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 Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 159 | } |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 160 | } |
| 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 Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 181 | 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 Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 185 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 186 | /* 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 Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 193 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 194 | rcu_read_unlock(); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 195 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 196 | /* 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) |
David Howells | 1bcab12 | 2017-12-01 11:40:43 +0000 | [diff] [blame] | 207 | goto out_put; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 208 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 209 | 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 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 232 | /* Now see if the permit list we want is actually already available */ |
| 233 | spin_lock(&afs_permits_lock); |
| 234 | |
| 235 | hash_for_each_possible(afs_permits_cache, xpermits, hash_node, new->h) { |
| 236 | if (xpermits->h != new->h || |
| 237 | xpermits->invalidated || |
| 238 | xpermits->nr_permits != new->nr_permits || |
| 239 | memcmp(xpermits->permits, new->permits, |
| 240 | new->nr_permits * sizeof(struct afs_permit)) != 0) |
| 241 | continue; |
| 242 | |
| 243 | if (refcount_inc_not_zero(&xpermits->usage)) { |
| 244 | replacement = xpermits; |
| 245 | goto found; |
| 246 | } |
| 247 | |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | for (i = 0; i < new->nr_permits; i++) |
| 252 | key_get(new->permits[i].key); |
| 253 | hash_add_rcu(afs_permits_cache, &new->hash_node, new->h); |
| 254 | replacement = new; |
| 255 | new = NULL; |
| 256 | |
| 257 | found: |
| 258 | spin_unlock(&afs_permits_lock); |
| 259 | |
| 260 | kfree(new); |
| 261 | |
| 262 | spin_lock(&vnode->lock); |
David Howells | 1bcab12 | 2017-12-01 11:40:43 +0000 | [diff] [blame] | 263 | zap = rcu_access_pointer(vnode->permit_cache); |
| 264 | if (cb_break == (vnode->cb_break + vnode->cb_interest->server->cb_s_break) && |
| 265 | zap == permits) |
| 266 | rcu_assign_pointer(vnode->permit_cache, replacement); |
| 267 | else |
| 268 | zap = replacement; |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 269 | spin_unlock(&vnode->lock); |
David Howells | 1bcab12 | 2017-12-01 11:40:43 +0000 | [diff] [blame] | 270 | afs_put_permits(zap); |
| 271 | out_put: |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 272 | afs_put_permits(permits); |
| 273 | return; |
| 274 | |
| 275 | someone_else_changed_it_unlock: |
| 276 | spin_unlock(&vnode->lock); |
| 277 | someone_else_changed_it: |
| 278 | /* Someone else changed the cache under us - don't recheck at this |
| 279 | * time. |
| 280 | */ |
| 281 | return; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
| 285 | * check with the fileserver to see if the directory or parent directory is |
| 286 | * permitted to be accessed with this authorisation, and if so, what access it |
| 287 | * is granted |
| 288 | */ |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 289 | int afs_check_permit(struct afs_vnode *vnode, struct key *key, |
| 290 | afs_access_t *_access) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 291 | { |
| 292 | struct afs_permits *permits; |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 293 | bool valid = false; |
| 294 | int i, ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 295 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 296 | _enter("{%x:%u},%x", |
| 297 | vnode->fid.vid, vnode->fid.vnode, key_serial(key)); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 298 | |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 299 | permits = vnode->permit_cache; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 300 | |
| 301 | /* check the permits to see if we've got one yet */ |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 302 | if (key == vnode->volume->cell->anonymous_key) { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 303 | _debug("anon"); |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 304 | *_access = vnode->status.anon_access; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 305 | valid = true; |
| 306 | } else { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 307 | rcu_read_lock(); |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 308 | permits = rcu_dereference(vnode->permit_cache); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 309 | if (permits) { |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 310 | for (i = 0; i < permits->nr_permits; i++) { |
| 311 | if (permits->permits[i].key < key) |
| 312 | continue; |
| 313 | if (permits->permits[i].key > key) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 314 | break; |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 315 | |
| 316 | *_access = permits->permits[i].access; |
| 317 | valid = !permits->invalidated; |
| 318 | break; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | rcu_read_unlock(); |
| 322 | } |
| 323 | |
| 324 | if (!valid) { |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 325 | /* Check the status on the file we're actually interested in |
| 326 | * (the post-processing will cache the result). |
| 327 | */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 328 | _debug("no valid permit"); |
| 329 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 330 | ret = afs_fetch_status(vnode, key); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 331 | if (ret < 0) { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 332 | *_access = 0; |
| 333 | _leave(" = %d", ret); |
| 334 | return ret; |
| 335 | } |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 336 | *_access = vnode->status.caller_access; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 337 | } |
| 338 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 339 | _leave(" = 0 [access %x]", *_access); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * check the permissions on an AFS file |
| 345 | * - AFS ACLs are attached to directories only, and a file is controlled by its |
| 346 | * parent directory's ACL |
| 347 | */ |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 348 | int afs_permission(struct inode *inode, int mask) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 349 | { |
| 350 | struct afs_vnode *vnode = AFS_FS_I(inode); |
Andrew Morton | 6975945 | 2008-02-08 04:20:53 -0800 | [diff] [blame] | 351 | afs_access_t uninitialized_var(access); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 352 | struct key *key; |
| 353 | int ret; |
| 354 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 355 | if (mask & MAY_NOT_BLOCK) |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 356 | return -ECHILD; |
| 357 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 358 | _enter("{{%x:%u},%lx},%x,", |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 359 | vnode->fid.vid, vnode->fid.vnode, vnode->flags, mask); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 360 | |
| 361 | key = afs_request_key(vnode->volume->cell); |
| 362 | if (IS_ERR(key)) { |
| 363 | _leave(" = %ld [key]", PTR_ERR(key)); |
| 364 | return PTR_ERR(key); |
| 365 | } |
| 366 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 367 | ret = afs_validate(vnode, key); |
| 368 | if (ret < 0) |
| 369 | goto error; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 370 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 371 | /* check the permits to see if we've got one yet */ |
| 372 | ret = afs_check_permit(vnode, key, &access); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 373 | if (ret < 0) |
| 374 | goto error; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 375 | |
| 376 | /* interpret the access mask */ |
| 377 | _debug("REQ %x ACC %x on %s", |
| 378 | mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file"); |
| 379 | |
| 380 | if (S_ISDIR(inode->i_mode)) { |
| 381 | if (mask & MAY_EXEC) { |
| 382 | if (!(access & AFS_ACE_LOOKUP)) |
| 383 | goto permission_denied; |
| 384 | } else if (mask & MAY_READ) { |
Marc Dionne | fd24982 | 2017-07-06 15:50:18 +0100 | [diff] [blame] | 385 | if (!(access & AFS_ACE_LOOKUP)) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 386 | goto permission_denied; |
| 387 | } else if (mask & MAY_WRITE) { |
| 388 | if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */ |
Marc Dionne | fd24982 | 2017-07-06 15:50:18 +0100 | [diff] [blame] | 389 | AFS_ACE_INSERT))) /* create, mkdir, symlink, rename to */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 390 | goto permission_denied; |
| 391 | } else { |
| 392 | BUG(); |
| 393 | } |
| 394 | } else { |
| 395 | if (!(access & AFS_ACE_LOOKUP)) |
| 396 | goto permission_denied; |
Marc Dionne | 627f469 | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 397 | if ((mask & MAY_EXEC) && !(inode->i_mode & S_IXUSR)) |
| 398 | goto permission_denied; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 399 | if (mask & (MAY_EXEC | MAY_READ)) { |
| 400 | if (!(access & AFS_ACE_READ)) |
| 401 | goto permission_denied; |
Marc Dionne | 627f469 | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 402 | if (!(inode->i_mode & S_IRUSR)) |
| 403 | goto permission_denied; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 404 | } else if (mask & MAY_WRITE) { |
| 405 | if (!(access & AFS_ACE_WRITE)) |
| 406 | goto permission_denied; |
Marc Dionne | 627f469 | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 407 | if (!(inode->i_mode & S_IWUSR)) |
| 408 | goto permission_denied; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | key_put(key); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 413 | _leave(" = %d", ret); |
| 414 | return ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 415 | |
| 416 | permission_denied: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 417 | ret = -EACCES; |
| 418 | error: |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 419 | key_put(key); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 420 | _leave(" = %d", ret); |
| 421 | return ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 422 | } |
David Howells | be080a6 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 423 | |
| 424 | void __exit afs_clean_up_permit_cache(void) |
| 425 | { |
| 426 | int i; |
| 427 | |
| 428 | for (i = 0; i < HASH_SIZE(afs_permits_cache); i++) |
| 429 | WARN_ON_ONCE(!hlist_empty(&afs_permits_cache[i])); |
| 430 | |
| 431 | } |