security.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* AFS security handling
  2. *
  3. * Copyright (C) 2007 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 <keys/rxrpc-type.h>
  16. #include "internal.h"
  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(&key_type_rxrpc, cell->anonymous_key->description,
  26. 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. * dispose of a permits list
  43. */
  44. void afs_zap_permits(struct rcu_head *rcu)
  45. {
  46. struct afs_permits *permits =
  47. container_of(rcu, struct afs_permits, rcu);
  48. int loop;
  49. _enter("{%d}", permits->count);
  50. for (loop = permits->count - 1; loop >= 0; loop--)
  51. key_put(permits->permits[loop].key);
  52. kfree(permits);
  53. }
  54. /*
  55. * dispose of a permits list in which all the key pointers have been copied
  56. */
  57. static void afs_dispose_of_permits(struct rcu_head *rcu)
  58. {
  59. struct afs_permits *permits =
  60. container_of(rcu, struct afs_permits, rcu);
  61. _enter("{%d}", permits->count);
  62. kfree(permits);
  63. }
  64. /*
  65. * get the authorising vnode - this is the specified inode itself if it's a
  66. * directory or it's the parent directory if the specified inode is a file or
  67. * symlink
  68. * - the caller must release the ref on the inode
  69. */
  70. static struct afs_vnode *afs_get_auth_inode(struct afs_vnode *vnode,
  71. struct key *key)
  72. {
  73. struct afs_vnode *auth_vnode;
  74. struct inode *auth_inode;
  75. _enter("");
  76. if (S_ISDIR(vnode->vfs_inode.i_mode)) {
  77. auth_inode = igrab(&vnode->vfs_inode);
  78. ASSERT(auth_inode != NULL);
  79. } else {
  80. auth_inode = afs_iget(vnode->vfs_inode.i_sb, key,
  81. &vnode->status.parent, NULL, NULL);
  82. if (IS_ERR(auth_inode))
  83. return ERR_PTR(PTR_ERR(auth_inode));
  84. }
  85. auth_vnode = AFS_FS_I(auth_inode);
  86. _leave(" = {%x}", auth_vnode->fid.vnode);
  87. return auth_vnode;
  88. }
  89. /*
  90. * clear the permit cache on a directory vnode
  91. */
  92. void afs_clear_permits(struct afs_vnode *vnode)
  93. {
  94. struct afs_permits *permits;
  95. _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
  96. mutex_lock(&vnode->permits_lock);
  97. permits = vnode->permits;
  98. rcu_assign_pointer(vnode->permits, NULL);
  99. mutex_unlock(&vnode->permits_lock);
  100. if (permits)
  101. call_rcu(&permits->rcu, afs_zap_permits);
  102. _leave("");
  103. }
  104. /*
  105. * add the result obtained for a vnode to its or its parent directory's cache
  106. * for the key used to access it
  107. */
  108. void afs_cache_permit(struct afs_vnode *vnode, struct key *key, long acl_order)
  109. {
  110. struct afs_permits *permits, *xpermits;
  111. struct afs_permit *permit;
  112. struct afs_vnode *auth_vnode;
  113. int count, loop;
  114. _enter("{%x:%u},%x,%lx",
  115. vnode->fid.vid, vnode->fid.vnode, key_serial(key), acl_order);
  116. auth_vnode = afs_get_auth_inode(vnode, key);
  117. if (IS_ERR(auth_vnode)) {
  118. _leave(" [get error %ld]", PTR_ERR(auth_vnode));
  119. return;
  120. }
  121. mutex_lock(&auth_vnode->permits_lock);
  122. /* guard against a rename being detected whilst we waited for the
  123. * lock */
  124. if (memcmp(&auth_vnode->fid, &vnode->status.parent,
  125. sizeof(struct afs_fid)) != 0) {
  126. _debug("renamed");
  127. goto out_unlock;
  128. }
  129. /* have to be careful as the directory's callback may be broken between
  130. * us receiving the status we're trying to cache and us getting the
  131. * lock to update the cache for the status */
  132. if (auth_vnode->acl_order - acl_order > 0) {
  133. _debug("ACL changed?");
  134. goto out_unlock;
  135. }
  136. /* always update the anonymous mask */
  137. _debug("anon access %x", vnode->status.anon_access);
  138. auth_vnode->status.anon_access = vnode->status.anon_access;
  139. if (key == vnode->volume->cell->anonymous_key)
  140. goto out_unlock;
  141. xpermits = auth_vnode->permits;
  142. count = 0;
  143. if (xpermits) {
  144. /* see if the permit is already in the list
  145. * - if it is then we just amend the list
  146. */
  147. count = xpermits->count;
  148. permit = xpermits->permits;
  149. for (loop = count; loop > 0; loop--) {
  150. if (permit->key == key) {
  151. permit->access_mask =
  152. vnode->status.caller_access;
  153. goto out_unlock;
  154. }
  155. permit++;
  156. }
  157. }
  158. permits = kmalloc(sizeof(*permits) + sizeof(*permit) * (count + 1),
  159. GFP_NOFS);
  160. if (!permits)
  161. goto out_unlock;
  162. memcpy(permits->permits, xpermits->permits,
  163. count * sizeof(struct afs_permit));
  164. _debug("key %x access %x",
  165. key_serial(key), vnode->status.caller_access);
  166. permits->permits[count].access_mask = vnode->status.caller_access;
  167. permits->permits[count].key = key_get(key);
  168. permits->count = count + 1;
  169. rcu_assign_pointer(auth_vnode->permits, permits);
  170. if (xpermits)
  171. call_rcu(&xpermits->rcu, afs_dispose_of_permits);
  172. out_unlock:
  173. mutex_unlock(&auth_vnode->permits_lock);
  174. iput(&auth_vnode->vfs_inode);
  175. _leave("");
  176. }
  177. /*
  178. * check with the fileserver to see if the directory or parent directory is
  179. * permitted to be accessed with this authorisation, and if so, what access it
  180. * is granted
  181. */
  182. static int afs_check_permit(struct afs_vnode *vnode, struct key *key,
  183. afs_access_t *_access)
  184. {
  185. struct afs_permits *permits;
  186. struct afs_permit *permit;
  187. struct afs_vnode *auth_vnode;
  188. bool valid;
  189. int loop, ret;
  190. _enter("{%x:%u},%x",
  191. vnode->fid.vid, vnode->fid.vnode, key_serial(key));
  192. auth_vnode = afs_get_auth_inode(vnode, key);
  193. if (IS_ERR(auth_vnode)) {
  194. *_access = 0;
  195. _leave(" = %ld", PTR_ERR(auth_vnode));
  196. return PTR_ERR(auth_vnode);
  197. }
  198. ASSERT(S_ISDIR(auth_vnode->vfs_inode.i_mode));
  199. /* check the permits to see if we've got one yet */
  200. if (key == auth_vnode->volume->cell->anonymous_key) {
  201. _debug("anon");
  202. *_access = auth_vnode->status.anon_access;
  203. valid = true;
  204. } else {
  205. valid = false;
  206. rcu_read_lock();
  207. permits = rcu_dereference(auth_vnode->permits);
  208. if (permits) {
  209. permit = permits->permits;
  210. for (loop = permits->count; loop > 0; loop--) {
  211. if (permit->key == key) {
  212. _debug("found in cache");
  213. *_access = permit->access_mask;
  214. valid = true;
  215. break;
  216. }
  217. permit++;
  218. }
  219. }
  220. rcu_read_unlock();
  221. }
  222. if (!valid) {
  223. /* check the status on the file we're actually interested in
  224. * (the post-processing will cache the result on auth_vnode) */
  225. _debug("no valid permit");
  226. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  227. ret = afs_vnode_fetch_status(vnode, auth_vnode, key);
  228. if (ret < 0) {
  229. iput(&auth_vnode->vfs_inode);
  230. *_access = 0;
  231. _leave(" = %d", ret);
  232. return ret;
  233. }
  234. *_access = vnode->status.caller_access;
  235. }
  236. iput(&auth_vnode->vfs_inode);
  237. _leave(" = 0 [access %x]", *_access);
  238. return 0;
  239. }
  240. /*
  241. * check the permissions on an AFS file
  242. * - AFS ACLs are attached to directories only, and a file is controlled by its
  243. * parent directory's ACL
  244. */
  245. int afs_permission(struct inode *inode, int mask, struct nameidata *nd)
  246. {
  247. struct afs_vnode *vnode = AFS_FS_I(inode);
  248. afs_access_t access;
  249. struct key *key;
  250. int ret;
  251. _enter("{{%x:%u},%lx},%x,",
  252. vnode->fid.vid, vnode->fid.vnode, vnode->flags, mask);
  253. key = afs_request_key(vnode->volume->cell);
  254. if (IS_ERR(key)) {
  255. _leave(" = %ld [key]", PTR_ERR(key));
  256. return PTR_ERR(key);
  257. }
  258. /* if the promise has expired, we need to check the server again */
  259. if (!vnode->cb_promised) {
  260. _debug("not promised");
  261. ret = afs_vnode_fetch_status(vnode, NULL, key);
  262. if (ret < 0)
  263. goto error;
  264. _debug("new promise [fl=%lx]", vnode->flags);
  265. }
  266. /* check the permits to see if we've got one yet */
  267. ret = afs_check_permit(vnode, key, &access);
  268. if (ret < 0)
  269. goto error;
  270. /* interpret the access mask */
  271. _debug("REQ %x ACC %x on %s",
  272. mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file");
  273. if (S_ISDIR(inode->i_mode)) {
  274. if (mask & MAY_EXEC) {
  275. if (!(access & AFS_ACE_LOOKUP))
  276. goto permission_denied;
  277. } else if (mask & MAY_READ) {
  278. if (!(access & AFS_ACE_READ))
  279. goto permission_denied;
  280. } else if (mask & MAY_WRITE) {
  281. if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */
  282. AFS_ACE_INSERT | /* create, mkdir, symlink, rename to */
  283. AFS_ACE_WRITE))) /* chmod */
  284. goto permission_denied;
  285. } else {
  286. BUG();
  287. }
  288. } else {
  289. if (!(access & AFS_ACE_LOOKUP))
  290. goto permission_denied;
  291. if (mask & (MAY_EXEC | MAY_READ)) {
  292. if (!(access & AFS_ACE_READ))
  293. goto permission_denied;
  294. } else if (mask & MAY_WRITE) {
  295. if (!(access & AFS_ACE_WRITE))
  296. goto permission_denied;
  297. }
  298. }
  299. key_put(key);
  300. ret = generic_permission(inode, mask, NULL);
  301. _leave(" = %d", ret);
  302. return ret;
  303. permission_denied:
  304. ret = -EACCES;
  305. error:
  306. key_put(key);
  307. _leave(" = %d", ret);
  308. return ret;
  309. }