auth_generic.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Generic RPC credential
  3. *
  4. * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
  5. */
  6. #include <linux/err.h>
  7. #include <linux/slab.h>
  8. #include <linux/types.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/sunrpc/auth.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/sunrpc/debug.h>
  14. #include <linux/sunrpc/sched.h>
  15. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  16. # define RPCDBG_FACILITY RPCDBG_AUTH
  17. #endif
  18. #define RPC_MACHINE_CRED_USERID GLOBAL_ROOT_UID
  19. #define RPC_MACHINE_CRED_GROUPID GLOBAL_ROOT_GID
  20. struct generic_cred {
  21. struct rpc_cred gc_base;
  22. struct auth_cred acred;
  23. };
  24. static struct rpc_auth generic_auth;
  25. static const struct rpc_credops generic_credops;
  26. /*
  27. * Public call interface
  28. */
  29. struct rpc_cred *rpc_lookup_cred(void)
  30. {
  31. return rpcauth_lookupcred(&generic_auth, 0);
  32. }
  33. EXPORT_SYMBOL_GPL(rpc_lookup_cred);
  34. struct rpc_cred *
  35. rpc_lookup_generic_cred(struct auth_cred *acred, int flags, gfp_t gfp)
  36. {
  37. return rpcauth_lookup_credcache(&generic_auth, acred, flags, gfp);
  38. }
  39. EXPORT_SYMBOL_GPL(rpc_lookup_generic_cred);
  40. struct rpc_cred *rpc_lookup_cred_nonblock(void)
  41. {
  42. return rpcauth_lookupcred(&generic_auth, RPCAUTH_LOOKUP_RCU);
  43. }
  44. EXPORT_SYMBOL_GPL(rpc_lookup_cred_nonblock);
  45. /*
  46. * Public call interface for looking up machine creds.
  47. */
  48. struct rpc_cred *rpc_lookup_machine_cred(const char *service_name)
  49. {
  50. struct auth_cred acred = {
  51. .uid = RPC_MACHINE_CRED_USERID,
  52. .gid = RPC_MACHINE_CRED_GROUPID,
  53. .principal = service_name,
  54. .machine_cred = 1,
  55. };
  56. dprintk("RPC: looking up machine cred for service %s\n",
  57. service_name);
  58. return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
  59. }
  60. EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
  61. static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
  62. struct rpc_cred *cred, int lookupflags)
  63. {
  64. struct rpc_auth *auth = task->tk_client->cl_auth;
  65. struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
  66. return auth->au_ops->lookup_cred(auth, acred, lookupflags);
  67. }
  68. static int
  69. generic_hash_cred(struct auth_cred *acred, unsigned int hashbits)
  70. {
  71. return hash_64(from_kgid(&init_user_ns, acred->gid) |
  72. ((u64)from_kuid(&init_user_ns, acred->uid) <<
  73. (sizeof(gid_t) * 8)), hashbits);
  74. }
  75. /*
  76. * Lookup generic creds for current process
  77. */
  78. static struct rpc_cred *
  79. generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  80. {
  81. return rpcauth_lookup_credcache(&generic_auth, acred, flags, GFP_KERNEL);
  82. }
  83. static struct rpc_cred *
  84. generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags, gfp_t gfp)
  85. {
  86. struct generic_cred *gcred;
  87. gcred = kmalloc(sizeof(*gcred), gfp);
  88. if (gcred == NULL)
  89. return ERR_PTR(-ENOMEM);
  90. rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops);
  91. gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
  92. gcred->acred.uid = acred->uid;
  93. gcred->acred.gid = acred->gid;
  94. gcred->acred.group_info = acred->group_info;
  95. gcred->acred.ac_flags = 0;
  96. if (gcred->acred.group_info != NULL)
  97. get_group_info(gcred->acred.group_info);
  98. gcred->acred.machine_cred = acred->machine_cred;
  99. gcred->acred.principal = acred->principal;
  100. dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
  101. gcred->acred.machine_cred ? "machine" : "generic",
  102. gcred,
  103. from_kuid(&init_user_ns, acred->uid),
  104. from_kgid(&init_user_ns, acred->gid));
  105. return &gcred->gc_base;
  106. }
  107. static void
  108. generic_free_cred(struct rpc_cred *cred)
  109. {
  110. struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
  111. dprintk("RPC: generic_free_cred %p\n", gcred);
  112. if (gcred->acred.group_info != NULL)
  113. put_group_info(gcred->acred.group_info);
  114. kfree(gcred);
  115. }
  116. static void
  117. generic_free_cred_callback(struct rcu_head *head)
  118. {
  119. struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
  120. generic_free_cred(cred);
  121. }
  122. static void
  123. generic_destroy_cred(struct rpc_cred *cred)
  124. {
  125. call_rcu(&cred->cr_rcu, generic_free_cred_callback);
  126. }
  127. static int
  128. machine_cred_match(struct auth_cred *acred, struct generic_cred *gcred, int flags)
  129. {
  130. if (!gcred->acred.machine_cred ||
  131. gcred->acred.principal != acred->principal ||
  132. !uid_eq(gcred->acred.uid, acred->uid) ||
  133. !gid_eq(gcred->acred.gid, acred->gid))
  134. return 0;
  135. return 1;
  136. }
  137. /*
  138. * Match credentials against current process creds.
  139. */
  140. static int
  141. generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
  142. {
  143. struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
  144. int i;
  145. if (acred->machine_cred)
  146. return machine_cred_match(acred, gcred, flags);
  147. if (!uid_eq(gcred->acred.uid, acred->uid) ||
  148. !gid_eq(gcred->acred.gid, acred->gid) ||
  149. gcred->acred.machine_cred != 0)
  150. goto out_nomatch;
  151. /* Optimisation in the case where pointers are identical... */
  152. if (gcred->acred.group_info == acred->group_info)
  153. goto out_match;
  154. /* Slow path... */
  155. if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
  156. goto out_nomatch;
  157. for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
  158. if (!gid_eq(gcred->acred.group_info->gid[i],
  159. acred->group_info->gid[i]))
  160. goto out_nomatch;
  161. }
  162. out_match:
  163. return 1;
  164. out_nomatch:
  165. return 0;
  166. }
  167. int __init rpc_init_generic_auth(void)
  168. {
  169. return rpcauth_init_credcache(&generic_auth);
  170. }
  171. void rpc_destroy_generic_auth(void)
  172. {
  173. rpcauth_destroy_credcache(&generic_auth);
  174. }
  175. /*
  176. * Test the the current time (now) against the underlying credential key expiry
  177. * minus a timeout and setup notification.
  178. *
  179. * The normal case:
  180. * If 'now' is before the key expiry minus RPC_KEY_EXPIRE_TIMEO, set
  181. * the RPC_CRED_NOTIFY_TIMEOUT flag to setup the underlying credential
  182. * rpc_credops crmatch routine to notify this generic cred when it's key
  183. * expiration is within RPC_KEY_EXPIRE_TIMEO, and return 0.
  184. *
  185. * The error case:
  186. * If the underlying cred lookup fails, return -EACCES.
  187. *
  188. * The 'almost' error case:
  189. * If 'now' is within key expiry minus RPC_KEY_EXPIRE_TIMEO, but not within
  190. * key expiry minus RPC_KEY_EXPIRE_FAIL, set the RPC_CRED_EXPIRE_SOON bit
  191. * on the acred ac_flags and return 0.
  192. */
  193. static int
  194. generic_key_timeout(struct rpc_auth *auth, struct rpc_cred *cred)
  195. {
  196. struct auth_cred *acred = &container_of(cred, struct generic_cred,
  197. gc_base)->acred;
  198. struct rpc_cred *tcred;
  199. int ret = 0;
  200. /* Fast track for non crkey_timeout (no key) underlying credentials */
  201. if (auth->au_flags & RPCAUTH_AUTH_NO_CRKEY_TIMEOUT)
  202. return 0;
  203. /* Fast track for the normal case */
  204. if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags))
  205. return 0;
  206. /* lookup_cred either returns a valid referenced rpc_cred, or PTR_ERR */
  207. tcred = auth->au_ops->lookup_cred(auth, acred, 0);
  208. if (IS_ERR(tcred))
  209. return -EACCES;
  210. /* Test for the almost error case */
  211. ret = tcred->cr_ops->crkey_timeout(tcred);
  212. if (ret != 0) {
  213. set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
  214. ret = 0;
  215. } else {
  216. /* In case underlying cred key has been reset */
  217. if (test_and_clear_bit(RPC_CRED_KEY_EXPIRE_SOON,
  218. &acred->ac_flags))
  219. dprintk("RPC: UID %d Credential key reset\n",
  220. from_kuid(&init_user_ns, tcred->cr_uid));
  221. /* set up fasttrack for the normal case */
  222. set_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags);
  223. }
  224. put_rpccred(tcred);
  225. return ret;
  226. }
  227. static const struct rpc_authops generic_auth_ops = {
  228. .owner = THIS_MODULE,
  229. .au_name = "Generic",
  230. .hash_cred = generic_hash_cred,
  231. .lookup_cred = generic_lookup_cred,
  232. .crcreate = generic_create_cred,
  233. .key_timeout = generic_key_timeout,
  234. };
  235. static struct rpc_auth generic_auth = {
  236. .au_ops = &generic_auth_ops,
  237. .au_count = ATOMIC_INIT(0),
  238. };
  239. static bool generic_key_to_expire(struct rpc_cred *cred)
  240. {
  241. struct auth_cred *acred = &container_of(cred, struct generic_cred,
  242. gc_base)->acred;
  243. return test_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
  244. }
  245. static const struct rpc_credops generic_credops = {
  246. .cr_name = "Generic cred",
  247. .crdestroy = generic_destroy_cred,
  248. .crbind = generic_bind_cred,
  249. .crmatch = generic_match,
  250. .crkey_to_expire = generic_key_to_expire,
  251. };