auth_unix.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <okir@monad.swb.de>
  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 *
  37. unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  38. {
  39. struct rpc_cred *ret = mempool_alloc(unix_pool, GFP_NOFS);
  40. rpcauth_init_cred(ret, acred, auth, &unix_credops);
  41. ret->cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
  42. return ret;
  43. }
  44. static void
  45. unx_free_cred_callback(struct rcu_head *head)
  46. {
  47. struct rpc_cred *rpc_cred = container_of(head, struct rpc_cred, cr_rcu);
  48. put_cred(rpc_cred->cr_cred);
  49. mempool_free(rpc_cred, unix_pool);
  50. }
  51. static void
  52. unx_destroy_cred(struct rpc_cred *cred)
  53. {
  54. call_rcu(&cred->cr_rcu, unx_free_cred_callback);
  55. }
  56. /*
  57. * Match credentials against current the auth_cred.
  58. */
  59. static int
  60. unx_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
  61. {
  62. unsigned int groups = 0;
  63. unsigned int i;
  64. if (cred->cr_cred == acred->cred)
  65. return 1;
  66. if (!uid_eq(cred->cr_cred->fsuid, acred->cred->fsuid) || !gid_eq(cred->cr_cred->fsgid, acred->cred->fsgid))
  67. return 0;
  68. if (acred->cred->group_info != NULL)
  69. groups = acred->cred->group_info->ngroups;
  70. if (groups > UNX_NGROUPS)
  71. groups = UNX_NGROUPS;
  72. if (cred->cr_cred->group_info == NULL)
  73. return groups == 0;
  74. if (groups != cred->cr_cred->group_info->ngroups)
  75. return 0;
  76. for (i = 0; i < groups ; i++)
  77. if (!gid_eq(cred->cr_cred->group_info->gid[i], acred->cred->group_info->gid[i]))
  78. return 0;
  79. return 1;
  80. }
  81. /*
  82. * Marshal credentials.
  83. * Maybe we should keep a cached credential for performance reasons.
  84. */
  85. static int
  86. unx_marshal(struct rpc_task *task, struct xdr_stream *xdr)
  87. {
  88. struct rpc_clnt *clnt = task->tk_client;
  89. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  90. __be32 *p, *cred_len, *gidarr_len;
  91. int i;
  92. struct group_info *gi = cred->cr_cred->group_info;
  93. struct user_namespace *userns = clnt->cl_cred ?
  94. clnt->cl_cred->user_ns : &init_user_ns;
  95. /* Credential */
  96. p = xdr_reserve_space(xdr, 3 * sizeof(*p));
  97. if (!p)
  98. goto marshal_failed;
  99. *p++ = rpc_auth_unix;
  100. cred_len = p++;
  101. *p++ = xdr_zero; /* stamp */
  102. if (xdr_stream_encode_opaque(xdr, clnt->cl_nodename,
  103. clnt->cl_nodelen) < 0)
  104. goto marshal_failed;
  105. p = xdr_reserve_space(xdr, 3 * sizeof(*p));
  106. if (!p)
  107. goto marshal_failed;
  108. *p++ = cpu_to_be32(from_kuid_munged(userns, cred->cr_cred->fsuid));
  109. *p++ = cpu_to_be32(from_kgid_munged(userns, cred->cr_cred->fsgid));
  110. gidarr_len = p++;
  111. if (gi)
  112. for (i = 0; i < UNX_NGROUPS && i < gi->ngroups; i++)
  113. *p++ = cpu_to_be32(from_kgid_munged(userns, gi->gid[i]));
  114. *gidarr_len = cpu_to_be32(p - gidarr_len - 1);
  115. *cred_len = cpu_to_be32((p - cred_len - 1) << 2);
  116. p = xdr_reserve_space(xdr, (p - gidarr_len - 1) << 2);
  117. if (!p)
  118. goto marshal_failed;
  119. /* Verifier */
  120. p = xdr_reserve_space(xdr, 2 * sizeof(*p));
  121. if (!p)
  122. goto marshal_failed;
  123. *p++ = rpc_auth_null;
  124. *p = xdr_zero;
  125. return 0;
  126. marshal_failed:
  127. return -EMSGSIZE;
  128. }
  129. /*
  130. * Refresh credentials. This is a no-op for AUTH_UNIX
  131. */
  132. static int
  133. unx_refresh(struct rpc_task *task)
  134. {
  135. set_bit(RPCAUTH_CRED_UPTODATE, &task->tk_rqstp->rq_cred->cr_flags);
  136. return 0;
  137. }
  138. static int
  139. unx_validate(struct rpc_task *task, struct xdr_stream *xdr)
  140. {
  141. struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
  142. __be32 *p;
  143. u32 size;
  144. p = xdr_inline_decode(xdr, 2 * sizeof(*p));
  145. if (!p)
  146. return -EIO;
  147. switch (*p++) {
  148. case rpc_auth_null:
  149. case rpc_auth_unix:
  150. case rpc_auth_short:
  151. break;
  152. default:
  153. return -EIO;
  154. }
  155. size = be32_to_cpup(p);
  156. if (size > RPC_MAX_AUTH_SIZE)
  157. return -EIO;
  158. p = xdr_inline_decode(xdr, size);
  159. if (!p)
  160. return -EIO;
  161. auth->au_verfsize = XDR_QUADLEN(size) + 2;
  162. auth->au_rslack = XDR_QUADLEN(size) + 2;
  163. auth->au_ralign = XDR_QUADLEN(size) + 2;
  164. return 0;
  165. }
  166. int __init rpc_init_authunix(void)
  167. {
  168. unix_pool = mempool_create_kmalloc_pool(16, sizeof(struct rpc_cred));
  169. return unix_pool ? 0 : -ENOMEM;
  170. }
  171. void rpc_destroy_authunix(void)
  172. {
  173. mempool_destroy(unix_pool);
  174. }
  175. const struct rpc_authops authunix_ops = {
  176. .owner = THIS_MODULE,
  177. .au_flavor = RPC_AUTH_UNIX,
  178. .au_name = "UNIX",
  179. .create = unx_create,
  180. .destroy = unx_destroy,
  181. .lookup_cred = unx_lookup_cred,
  182. };
  183. static
  184. struct rpc_auth unix_auth = {
  185. .au_cslack = UNX_CALLSLACK,
  186. .au_rslack = NUL_REPLYSLACK,
  187. .au_verfsize = NUL_REPLYSLACK,
  188. .au_ops = &authunix_ops,
  189. .au_flavor = RPC_AUTH_UNIX,
  190. .au_count = REFCOUNT_INIT(1),
  191. };
  192. static
  193. const struct rpc_credops unix_credops = {
  194. .cr_name = "AUTH_UNIX",
  195. .crdestroy = unx_destroy_cred,
  196. .crmatch = unx_match,
  197. .crmarshal = unx_marshal,
  198. .crwrap_req = rpcauth_wrap_req_encode,
  199. .crrefresh = unx_refresh,
  200. .crvalidate = unx_validate,
  201. .crunwrap_resp = rpcauth_unwrap_resp_decode,
  202. };