security.c 10 KB

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