security.c 10 KB

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