security.c 9.1 KB

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