auth_unix.c 5.7 KB

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