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 ([email protected])
  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(struct_size(new, permits, size), GFP_NOFS);
  191. if (!new)
  192. goto out_put;
  193. refcount_set(&new->usage, 1);
  194. new->nr_permits = size;
  195. i = j = 0;
  196. if (permits) {
  197. for (i = 0; i < permits->nr_permits; i++) {
  198. if (j == i && permits->permits[i].key > key) {
  199. new->permits[j].key = key;
  200. new->permits[j].access = caller_access;
  201. j++;
  202. }
  203. new->permits[j].key = permits->permits[i].key;
  204. new->permits[j].access = permits->permits[i].access;
  205. j++;
  206. }
  207. }
  208. if (j == i) {
  209. new->permits[j].key = key;
  210. new->permits[j].access = caller_access;
  211. }
  212. afs_hash_permits(new);
  213. /* Now see if the permit list we want is actually already available */
  214. spin_lock(&afs_permits_lock);
  215. hash_for_each_possible(afs_permits_cache, xpermits, hash_node, new->h) {
  216. if (xpermits->h != new->h ||
  217. xpermits->invalidated ||
  218. xpermits->nr_permits != new->nr_permits ||
  219. memcmp(xpermits->permits, new->permits,
  220. new->nr_permits * sizeof(struct afs_permit)) != 0)
  221. continue;
  222. if (refcount_inc_not_zero(&xpermits->usage)) {
  223. replacement = xpermits;
  224. goto found;
  225. }
  226. break;
  227. }
  228. for (i = 0; i < new->nr_permits; i++)
  229. key_get(new->permits[i].key);
  230. hash_add_rcu(afs_permits_cache, &new->hash_node, new->h);
  231. replacement = new;
  232. new = NULL;
  233. found:
  234. spin_unlock(&afs_permits_lock);
  235. kfree(new);
  236. rcu_read_lock();
  237. spin_lock(&vnode->lock);
  238. zap = rcu_access_pointer(vnode->permit_cache);
  239. if (!afs_cb_is_broken(cb_break, vnode) && zap == permits)
  240. rcu_assign_pointer(vnode->permit_cache, replacement);
  241. else
  242. zap = replacement;
  243. spin_unlock(&vnode->lock);
  244. rcu_read_unlock();
  245. afs_put_permits(zap);
  246. out_put:
  247. afs_put_permits(permits);
  248. return;
  249. someone_else_changed_it_unlock:
  250. spin_unlock(&vnode->lock);
  251. someone_else_changed_it:
  252. /* Someone else changed the cache under us - don't recheck at this
  253. * time.
  254. */
  255. rcu_read_unlock();
  256. return;
  257. }
  258. static bool afs_check_permit_rcu(struct afs_vnode *vnode, struct key *key,
  259. afs_access_t *_access)
  260. {
  261. const struct afs_permits *permits;
  262. int i;
  263. _enter("{%llx:%llu},%x",
  264. vnode->fid.vid, vnode->fid.vnode, key_serial(key));
  265. /* check the permits to see if we've got one yet */
  266. if (key == vnode->volume->cell->anonymous_key) {
  267. *_access = vnode->status.anon_access;
  268. _leave(" = t [anon %x]", *_access);
  269. return true;
  270. }
  271. permits = rcu_dereference(vnode->permit_cache);
  272. if (permits) {
  273. for (i = 0; i < permits->nr_permits; i++) {
  274. if (permits->permits[i].key < key)
  275. continue;
  276. if (permits->permits[i].key > key)
  277. break;
  278. *_access = permits->permits[i].access;
  279. _leave(" = %u [perm %x]", !permits->invalidated, *_access);
  280. return !permits->invalidated;
  281. }
  282. }
  283. _leave(" = f");
  284. return false;
  285. }
  286. /*
  287. * check with the fileserver to see if the directory or parent directory is
  288. * permitted to be accessed with this authorisation, and if so, what access it
  289. * is granted
  290. */
  291. int afs_check_permit(struct afs_vnode *vnode, struct key *key,
  292. afs_access_t *_access)
  293. {
  294. struct afs_permits *permits;
  295. bool valid = false;
  296. int i, ret;
  297. _enter("{%llx:%llu},%x",
  298. vnode->fid.vid, vnode->fid.vnode, key_serial(key));
  299. /* check the permits to see if we've got one yet */
  300. if (key == vnode->volume->cell->anonymous_key) {
  301. _debug("anon");
  302. *_access = vnode->status.anon_access;
  303. valid = true;
  304. } else {
  305. rcu_read_lock();
  306. permits = rcu_dereference(vnode->permit_cache);
  307. if (permits) {
  308. for (i = 0; i < permits->nr_permits; i++) {
  309. if (permits->permits[i].key < key)
  310. continue;
  311. if (permits->permits[i].key > key)
  312. break;
  313. *_access = permits->permits[i].access;
  314. valid = !permits->invalidated;
  315. break;
  316. }
  317. }
  318. rcu_read_unlock();
  319. }
  320. if (!valid) {
  321. /* Check the status on the file we're actually interested in
  322. * (the post-processing will cache the result).
  323. */
  324. _debug("no valid permit");
  325. ret = afs_fetch_status(vnode, key, false, _access);
  326. if (ret < 0) {
  327. *_access = 0;
  328. _leave(" = %d", ret);
  329. return ret;
  330. }
  331. }
  332. _leave(" = 0 [access %x]", *_access);
  333. return 0;
  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. */
  340. int afs_permission(struct user_namespace *mnt_userns, struct inode *inode,
  341. 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. }