auth_unix.c 6.0 KB

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