security.c 12 KB

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