security.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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);
  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}", 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},%x,%lx", vnode->fid.vnode, key_serial(key), acl_order);
  115. auth_vnode = afs_get_auth_inode(vnode, key);
  116. if (IS_ERR(auth_vnode)) {
  117. _leave(" [get error %ld]", PTR_ERR(auth_vnode));
  118. return;
  119. }
  120. mutex_lock(&auth_vnode->permits_lock);
  121. /* guard against a rename being detected whilst we waited for the
  122. * lock */
  123. if (memcmp(&auth_vnode->fid, &vnode->status.parent,
  124. sizeof(struct afs_fid)) != 0) {
  125. _debug("renamed");
  126. goto out_unlock;
  127. }
  128. /* have to be careful as the directory's callback may be broken between
  129. * us receiving the status we're trying to cache and us getting the
  130. * lock to update the cache for the status */
  131. if (auth_vnode->acl_order - acl_order > 0) {
  132. _debug("ACL changed?");
  133. goto out_unlock;
  134. }
  135. /* always update the anonymous mask */
  136. _debug("anon access %x", vnode->status.anon_access);
  137. auth_vnode->status.anon_access = vnode->status.anon_access;
  138. if (key == vnode->volume->cell->anonymous_key)
  139. goto out_unlock;
  140. xpermits = auth_vnode->permits;
  141. count = 0;
  142. if (xpermits) {
  143. /* see if the permit is already in the list
  144. * - if it is then we just amend the list
  145. */
  146. count = xpermits->count;
  147. permit = xpermits->permits;
  148. for (loop = count; loop > 0; loop--) {
  149. if (permit->key == key) {
  150. permit->access_mask =
  151. vnode->status.caller_access;
  152. goto out_unlock;
  153. }
  154. permit++;
  155. }
  156. }
  157. permits = kmalloc(sizeof(*permits) + sizeof(*permit) * (count + 1),
  158. GFP_NOFS);
  159. if (!permits)
  160. goto out_unlock;
  161. memcpy(permits->permits, xpermits->permits,
  162. count * sizeof(struct afs_permit));
  163. _debug("key %x access %x",
  164. key_serial(key), vnode->status.caller_access);
  165. permits->permits[count].access_mask = vnode->status.caller_access;
  166. permits->permits[count].key = key_get(key);
  167. permits->count = count + 1;
  168. rcu_assign_pointer(auth_vnode->permits, permits);
  169. if (xpermits)
  170. call_rcu(&xpermits->rcu, afs_dispose_of_permits);
  171. out_unlock:
  172. mutex_unlock(&auth_vnode->permits_lock);
  173. iput(&auth_vnode->vfs_inode);
  174. _leave("");
  175. }
  176. /*
  177. * check with the fileserver to see if the directory or parent directory is
  178. * permitted to be accessed with this authorisation, and if so, what access it
  179. * is granted
  180. */
  181. static int afs_check_permit(struct afs_vnode *vnode, struct key *key,
  182. afs_access_t *_access)
  183. {
  184. struct afs_permits *permits;
  185. struct afs_permit *permit;
  186. struct afs_vnode *auth_vnode;
  187. bool valid;
  188. int loop, ret;
  189. _enter("");
  190. auth_vnode = afs_get_auth_inode(vnode, key);
  191. if (IS_ERR(auth_vnode)) {
  192. *_access = 0;
  193. _leave(" = %ld", PTR_ERR(auth_vnode));
  194. return PTR_ERR(auth_vnode);
  195. }
  196. ASSERT(S_ISDIR(auth_vnode->vfs_inode.i_mode));
  197. /* check the permits to see if we've got one yet */
  198. if (key == auth_vnode->volume->cell->anonymous_key) {
  199. _debug("anon");
  200. *_access = auth_vnode->status.anon_access;
  201. valid = true;
  202. } else {
  203. valid = false;
  204. rcu_read_lock();
  205. permits = rcu_dereference(auth_vnode->permits);
  206. if (permits) {
  207. permit = permits->permits;
  208. for (loop = permits->count; loop > 0; loop--) {
  209. if (permit->key == key) {
  210. _debug("found in cache");
  211. *_access = permit->access_mask;
  212. valid = true;
  213. break;
  214. }
  215. permit++;
  216. }
  217. }
  218. rcu_read_unlock();
  219. }
  220. if (!valid) {
  221. /* check the status on the file we're actually interested in
  222. * (the post-processing will cache the result on auth_vnode) */
  223. _debug("no valid permit");
  224. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  225. ret = afs_vnode_fetch_status(vnode, auth_vnode, key);
  226. if (ret < 0) {
  227. iput(&auth_vnode->vfs_inode);
  228. *_access = 0;
  229. _leave(" = %d", ret);
  230. return ret;
  231. }
  232. }
  233. *_access = vnode->status.caller_access;
  234. iput(&auth_vnode->vfs_inode);
  235. _leave(" = 0 [access %x]", *_access);
  236. return 0;
  237. }
  238. /*
  239. * check the permissions on an AFS file
  240. * - AFS ACLs are attached to directories only, and a file is controlled by its
  241. * parent directory's ACL
  242. */
  243. int afs_permission(struct inode *inode, int mask, struct nameidata *nd)
  244. {
  245. struct afs_vnode *vnode = AFS_FS_I(inode);
  246. afs_access_t access;
  247. struct key *key;
  248. int ret;
  249. _enter("{%x:%x},%x,", vnode->fid.vid, vnode->fid.vnode, mask);
  250. key = afs_request_key(vnode->volume->cell);
  251. if (IS_ERR(key)) {
  252. _leave(" = %ld [key]", PTR_ERR(key));
  253. return PTR_ERR(key);
  254. }
  255. /* check the permits to see if we've got one yet */
  256. ret = afs_check_permit(vnode, key, &access);
  257. if (ret < 0) {
  258. key_put(key);
  259. _leave(" = %d [check]", ret);
  260. return ret;
  261. }
  262. /* interpret the access mask */
  263. _debug("REQ %x ACC %x on %s",
  264. mask, access, S_ISDIR(inode->i_mode) ? "dir" : "file");
  265. if (S_ISDIR(inode->i_mode)) {
  266. if (mask & MAY_EXEC) {
  267. if (!(access & AFS_ACE_LOOKUP))
  268. goto permission_denied;
  269. } else if (mask & MAY_READ) {
  270. if (!(access & AFS_ACE_READ))
  271. goto permission_denied;
  272. } else if (mask & MAY_WRITE) {
  273. if (!(access & (AFS_ACE_DELETE | /* rmdir, unlink, rename from */
  274. AFS_ACE_INSERT | /* create, mkdir, symlink, rename to */
  275. AFS_ACE_WRITE))) /* chmod */
  276. goto permission_denied;
  277. } else {
  278. BUG();
  279. }
  280. } else {
  281. if (!(access & AFS_ACE_LOOKUP))
  282. goto permission_denied;
  283. if (mask & (MAY_EXEC | MAY_READ)) {
  284. if (!(access & AFS_ACE_READ))
  285. goto permission_denied;
  286. } else if (mask & MAY_WRITE) {
  287. if (!(access & AFS_ACE_WRITE))
  288. goto permission_denied;
  289. }
  290. }
  291. key_put(key);
  292. return generic_permission(inode, mask, NULL);
  293. permission_denied:
  294. key_put(key);
  295. _leave(" = -EACCES");
  296. return -EACCES;
  297. }