auth_unix.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/net/sunrpc/auth_unix.c
  4. *
  5. * UNIX-style authentication; no AUTH_SHORT support
  6. *
  7. * Copyright (C) 1996, Olaf Kirch <[email protected]>
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/types.h>
  11. #include <linux/sched.h>
  12. #include <linux/module.h>
  13. #include <linux/mempool.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/sunrpc/auth.h>
  16. #include <linux/user_namespace.h>
  17. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  18. # define RPCDBG_FACILITY RPCDBG_AUTH
  19. #endif
  20. static struct rpc_auth unix_auth;
  21. static const struct rpc_credops unix_credops;
  22. static mempool_t *unix_pool;
  23. static struct rpc_auth *
  24. unx_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  25. {
  26. refcount_inc(&unix_auth.au_count);
  27. return &unix_auth;
  28. }
  29. static void
  30. unx_destroy(struct rpc_auth *auth)
  31. {
  32. }
  33. /*
  34. * Lookup AUTH_UNIX creds for current process
  35. */
  36. static struct rpc_cred *unx_lookup_cred(struct rpc_auth *auth,
  37. struct auth_cred *acred, int flags)
  38. {
  39. struct rpc_cred *ret;
  40. ret = kmalloc(sizeof(*ret), rpc_task_gfp_mask());
  41. if (!ret) {
  42. if (!(flags & RPCAUTH_LOOKUP_ASYNC))
  43. return ERR_PTR(-ENOMEM);
  44. ret = mempool_alloc(unix_pool, GFP_NOWAIT);
  45. if (!ret)
  46. return ERR_PTR(-ENOMEM);
  47. }
  48. rpcauth_init_cred(ret, acred, auth, &unix_credops);
  49. ret->cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
  50. return ret;
  51. }
  52. static void
  53. unx_free_cred_callback(struct rcu_head *head)
  54. {
  55. struct rpc_cred *rpc_cred = container_of(head, struct rpc_cred, cr_rcu);
  56. put_cred(rpc_cred->cr_cred);
  57. mempool_free(rpc_cred, unix_pool);
  58. }
  59. static void
  60. unx_destroy_cred(struct rpc_cred *cred)
  61. {
  62. call_rcu(&cred->cr_rcu, unx_free_cred_callback);
  63. }
  64. /*
  65. * Match credentials against current the auth_cred.
  66. */
  67. static int
  68. unx_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
  69. {
  70. unsigned int groups = 0;
  71. unsigned int i;
  72. if (cred->cr_cred == acred->cred)
  73. return 1;
  74. if (!uid_eq(cred->cr_cred->fsuid, acred->cred->fsuid) || !gid_eq(cred->cr_cred->fsgid, acred->cred->fsgid))
  75. return 0;
  76. if (acred->cred->group_info != NULL)
  77. groups = acred->cred->group_info->ngroups;
  78. if (groups > UNX_NGROUPS)
  79. groups = UNX_NGROUPS;
  80. if (cred->cr_cred->group_info == NULL)
  81. return groups == 0;
  82. if (groups != cred->cr_cred->group_info->ngroups)
  83. return 0;
  84. for (i = 0; i < groups ; i++)
  85. if (!gid_eq(cred->cr_cred->group_info->gid[i], acred->cred->group_info->gid[i]))
  86. return 0;
  87. return 1;
  88. }
  89. /*
  90. * Marshal credentials.
  91. * Maybe we should keep a cached credential for performance reasons.
  92. */
  93. static int
  94. unx_marshal(struct rpc_task *task, struct xdr_stream *xdr)
  95. {
  96. struct rpc_clnt *clnt = task->tk_client;
  97. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  98. __be32 *p, *cred_len, *gidarr_len;
  99. int i;
  100. struct group_info *gi = cred->cr_cred->group_info;
  101. struct user_namespace *userns = clnt->cl_cred ?
  102. clnt->cl_cred->user_ns : &init_user_ns;
  103. /* Credential */
  104. p = xdr_reserve_space(xdr, 3 * sizeof(*p));
  105. if (!p)
  106. goto marshal_failed;
  107. *p++ = rpc_auth_unix;
  108. cred_len = p++;
  109. *p++ = xdr_zero; /* stamp */
  110. if (xdr_stream_encode_opaque(xdr, clnt->cl_nodename,
  111. clnt->cl_nodelen) < 0)
  112. goto marshal_failed;
  113. p = xdr_reserve_space(xdr, 3 * sizeof(*p));
  114. if (!p)
  115. goto marshal_failed;
  116. *p++ = cpu_to_be32(from_kuid_munged(userns, cred->cr_cred->fsuid));
  117. *p++ = cpu_to_be32(from_kgid_munged(userns, cred->cr_cred->fsgid));
  118. gidarr_len = p++;
  119. if (gi)
  120. for (i = 0; i < UNX_NGROUPS && i < gi->ngroups; i++)
  121. *p++ = cpu_to_be32(from_kgid_munged(userns, gi->gid[i]));
  122. *gidarr_len = cpu_to_be32(p - gidarr_len - 1);
  123. *cred_len = cpu_to_be32((p - cred_len - 1) << 2);
  124. p = xdr_reserve_space(xdr, (p - gidarr_len - 1) << 2);
  125. if (!p)
  126. goto marshal_failed;
  127. /* Verifier */
  128. p = xdr_reserve_space(xdr, 2 * sizeof(*p));
  129. if (!p)
  130. goto marshal_failed;
  131. *p++ = rpc_auth_null;
  132. *p = xdr_zero;
  133. return 0;
  134. marshal_failed:
  135. return -EMSGSIZE;
  136. }
  137. /*
  138. * Refresh credentials. This is a no-op for AUTH_UNIX
  139. */
  140. static int
  141. unx_refresh(struct rpc_task *task)
  142. {
  143. set_bit(RPCAUTH_CRED_UPTODATE, &task->tk_rqstp->rq_cred->cr_flags);
  144. return 0;
  145. }
  146. static int
  147. unx_validate(struct rpc_task *task, struct xdr_stream *xdr)
  148. {
  149. struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
  150. __be32 *p;
  151. u32 size;
  152. p = xdr_inline_decode(xdr, 2 * sizeof(*p));
  153. if (!p)
  154. return -EIO;
  155. switch (*p++) {
  156. case rpc_auth_null:
  157. case rpc_auth_unix:
  158. case rpc_auth_short:
  159. break;
  160. default:
  161. return -EIO;
  162. }
  163. size = be32_to_cpup(p);
  164. if (size > RPC_MAX_AUTH_SIZE)
  165. return -EIO;
  166. p = xdr_inline_decode(xdr, size);
  167. if (!p)
  168. return -EIO;
  169. auth->au_verfsize = XDR_QUADLEN(size) + 2;
  170. auth->au_rslack = XDR_QUADLEN(size) + 2;
  171. auth->au_ralign = XDR_QUADLEN(size) + 2;
  172. return 0;
  173. }
  174. int __init rpc_init_authunix(void)
  175. {
  176. unix_pool = mempool_create_kmalloc_pool(16, sizeof(struct rpc_cred));
  177. return unix_pool ? 0 : -ENOMEM;
  178. }
  179. void rpc_destroy_authunix(void)
  180. {
  181. mempool_destroy(unix_pool);
  182. }
  183. const struct rpc_authops authunix_ops = {
  184. .owner = THIS_MODULE,
  185. .au_flavor = RPC_AUTH_UNIX,
  186. .au_name = "UNIX",
  187. .create = unx_create,
  188. .destroy = unx_destroy,
  189. .lookup_cred = unx_lookup_cred,
  190. };
  191. static
  192. struct rpc_auth unix_auth = {
  193. .au_cslack = UNX_CALLSLACK,
  194. .au_rslack = NUL_REPLYSLACK,
  195. .au_verfsize = NUL_REPLYSLACK,
  196. .au_ops = &authunix_ops,
  197. .au_flavor = RPC_AUTH_UNIX,
  198. .au_count = REFCOUNT_INIT(1),
  199. };
  200. static
  201. const struct rpc_credops unix_credops = {
  202. .cr_name = "AUTH_UNIX",
  203. .crdestroy = unx_destroy_cred,
  204. .crmatch = unx_match,
  205. .crmarshal = unx_marshal,
  206. .crwrap_req = rpcauth_wrap_req_encode,
  207. .crrefresh = unx_refresh,
  208. .crvalidate = unx_validate,
  209. .crunwrap_resp = rpcauth_unwrap_resp_decode,
  210. };