auth_unix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * linux/net/sunrpc/auth_unix.c
  3. *
  4. * UNIX-style authentication; no AUTH_SHORT support
  5. *
  6. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. #include <linux/module.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/sunrpc/auth.h>
  14. #include <linux/user_namespace.h>
  15. struct unx_cred {
  16. struct rpc_cred uc_base;
  17. kgid_t uc_gid;
  18. kgid_t uc_gids[UNX_NGROUPS];
  19. };
  20. #define uc_uid uc_base.cr_uid
  21. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  22. # define RPCDBG_FACILITY RPCDBG_AUTH
  23. #endif
  24. static struct rpc_auth unix_auth;
  25. static const struct rpc_credops unix_credops;
  26. static struct rpc_auth *
  27. unx_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  28. {
  29. dprintk("RPC: creating UNIX authenticator for client %p\n",
  30. clnt);
  31. atomic_inc(&unix_auth.au_count);
  32. return &unix_auth;
  33. }
  34. static void
  35. unx_destroy(struct rpc_auth *auth)
  36. {
  37. dprintk("RPC: destroying UNIX authenticator %p\n", auth);
  38. rpcauth_clear_credcache(auth->au_credcache);
  39. }
  40. static int
  41. unx_hash_cred(struct auth_cred *acred, unsigned int hashbits)
  42. {
  43. return hash_64(from_kgid(&init_user_ns, acred->gid) |
  44. ((u64)from_kuid(&init_user_ns, acred->uid) <<
  45. (sizeof(gid_t) * 8)), hashbits);
  46. }
  47. /*
  48. * Lookup AUTH_UNIX creds for current process
  49. */
  50. static struct rpc_cred *
  51. unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  52. {
  53. return rpcauth_lookup_credcache(auth, acred, flags, GFP_NOFS);
  54. }
  55. static struct rpc_cred *
  56. unx_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags, gfp_t gfp)
  57. {
  58. struct unx_cred *cred;
  59. unsigned int groups = 0;
  60. unsigned int i;
  61. dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
  62. from_kuid(&init_user_ns, acred->uid),
  63. from_kgid(&init_user_ns, acred->gid));
  64. if (!(cred = kmalloc(sizeof(*cred), gfp)))
  65. return ERR_PTR(-ENOMEM);
  66. rpcauth_init_cred(&cred->uc_base, acred, auth, &unix_credops);
  67. cred->uc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
  68. if (acred->group_info != NULL)
  69. groups = acred->group_info->ngroups;
  70. if (groups > UNX_NGROUPS)
  71. groups = UNX_NGROUPS;
  72. cred->uc_gid = acred->gid;
  73. for (i = 0; i < groups; i++)
  74. cred->uc_gids[i] = acred->group_info->gid[i];
  75. if (i < UNX_NGROUPS)
  76. cred->uc_gids[i] = INVALID_GID;
  77. return &cred->uc_base;
  78. }
  79. static void
  80. unx_free_cred(struct unx_cred *unx_cred)
  81. {
  82. dprintk("RPC: unx_free_cred %p\n", unx_cred);
  83. kfree(unx_cred);
  84. }
  85. static void
  86. unx_free_cred_callback(struct rcu_head *head)
  87. {
  88. struct unx_cred *unx_cred = container_of(head, struct unx_cred, uc_base.cr_rcu);
  89. unx_free_cred(unx_cred);
  90. }
  91. static void
  92. unx_destroy_cred(struct rpc_cred *cred)
  93. {
  94. call_rcu(&cred->cr_rcu, unx_free_cred_callback);
  95. }
  96. /*
  97. * Match credentials against current process creds.
  98. * The root_override argument takes care of cases where the caller may
  99. * request root creds (e.g. for NFS swapping).
  100. */
  101. static int
  102. unx_match(struct auth_cred *acred, struct rpc_cred *rcred, int flags)
  103. {
  104. struct unx_cred *cred = container_of(rcred, struct unx_cred, uc_base);
  105. unsigned int groups = 0;
  106. unsigned int i;
  107. if (!uid_eq(cred->uc_uid, acred->uid) || !gid_eq(cred->uc_gid, acred->gid))
  108. return 0;
  109. if (acred->group_info != NULL)
  110. groups = acred->group_info->ngroups;
  111. if (groups > UNX_NGROUPS)
  112. groups = UNX_NGROUPS;
  113. for (i = 0; i < groups ; i++)
  114. if (!gid_eq(cred->uc_gids[i], acred->group_info->gid[i]))
  115. return 0;
  116. if (groups < UNX_NGROUPS && gid_valid(cred->uc_gids[groups]))
  117. return 0;
  118. return 1;
  119. }
  120. /*
  121. * Marshal credentials.
  122. * Maybe we should keep a cached credential for performance reasons.
  123. */
  124. static __be32 *
  125. unx_marshal(struct rpc_task *task, __be32 *p)
  126. {
  127. struct rpc_clnt *clnt = task->tk_client;
  128. struct unx_cred *cred = container_of(task->tk_rqstp->rq_cred, struct unx_cred, uc_base);
  129. __be32 *base, *hold;
  130. int i;
  131. *p++ = htonl(RPC_AUTH_UNIX);
  132. base = p++;
  133. *p++ = htonl(jiffies/HZ);
  134. /*
  135. * Copy the UTS nodename captured when the client was created.
  136. */
  137. p = xdr_encode_array(p, clnt->cl_nodename, clnt->cl_nodelen);
  138. *p++ = htonl((u32) from_kuid(&init_user_ns, cred->uc_uid));
  139. *p++ = htonl((u32) from_kgid(&init_user_ns, cred->uc_gid));
  140. hold = p++;
  141. for (i = 0; i < UNX_NGROUPS && gid_valid(cred->uc_gids[i]); i++)
  142. *p++ = htonl((u32) from_kgid(&init_user_ns, cred->uc_gids[i]));
  143. *hold = htonl(p - hold - 1); /* gid array length */
  144. *base = htonl((p - base - 1) << 2); /* cred length */
  145. *p++ = htonl(RPC_AUTH_NULL);
  146. *p++ = htonl(0);
  147. return p;
  148. }
  149. /*
  150. * Refresh credentials. This is a no-op for AUTH_UNIX
  151. */
  152. static int
  153. unx_refresh(struct rpc_task *task)
  154. {
  155. set_bit(RPCAUTH_CRED_UPTODATE, &task->tk_rqstp->rq_cred->cr_flags);
  156. return 0;
  157. }
  158. static __be32 *
  159. unx_validate(struct rpc_task *task, __be32 *p)
  160. {
  161. rpc_authflavor_t flavor;
  162. u32 size;
  163. flavor = ntohl(*p++);
  164. if (flavor != RPC_AUTH_NULL &&
  165. flavor != RPC_AUTH_UNIX &&
  166. flavor != RPC_AUTH_SHORT) {
  167. printk("RPC: bad verf flavor: %u\n", flavor);
  168. return ERR_PTR(-EIO);
  169. }
  170. size = ntohl(*p++);
  171. if (size > RPC_MAX_AUTH_SIZE) {
  172. printk("RPC: giant verf size: %u\n", size);
  173. return ERR_PTR(-EIO);
  174. }
  175. task->tk_rqstp->rq_cred->cr_auth->au_rslack = (size >> 2) + 2;
  176. p += (size >> 2);
  177. return p;
  178. }
  179. int __init rpc_init_authunix(void)
  180. {
  181. return rpcauth_init_credcache(&unix_auth);
  182. }
  183. void rpc_destroy_authunix(void)
  184. {
  185. rpcauth_destroy_credcache(&unix_auth);
  186. }
  187. const struct rpc_authops authunix_ops = {
  188. .owner = THIS_MODULE,
  189. .au_flavor = RPC_AUTH_UNIX,
  190. .au_name = "UNIX",
  191. .create = unx_create,
  192. .destroy = unx_destroy,
  193. .hash_cred = unx_hash_cred,
  194. .lookup_cred = unx_lookup_cred,
  195. .crcreate = unx_create_cred,
  196. };
  197. static
  198. struct rpc_auth unix_auth = {
  199. .au_cslack = UNX_CALLSLACK,
  200. .au_rslack = NUL_REPLYSLACK,
  201. .au_flags = RPCAUTH_AUTH_NO_CRKEY_TIMEOUT,
  202. .au_ops = &authunix_ops,
  203. .au_flavor = RPC_AUTH_UNIX,
  204. .au_count = ATOMIC_INIT(0),
  205. };
  206. static
  207. const struct rpc_credops unix_credops = {
  208. .cr_name = "AUTH_UNIX",
  209. .crdestroy = unx_destroy_cred,
  210. .crbind = rpcauth_generic_bind_cred,
  211. .crmatch = unx_match,
  212. .crmarshal = unx_marshal,
  213. .crrefresh = unx_refresh,
  214. .crvalidate = unx_validate,
  215. };