auth_generic.c 7.5 KB

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