svcauth_unix.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/sched.h>
  4. #include <linux/module.h>
  5. #include <linux/sunrpc/types.h>
  6. #include <linux/sunrpc/xdr.h>
  7. #include <linux/sunrpc/svcsock.h>
  8. #include <linux/sunrpc/svcauth.h>
  9. #include <linux/sunrpc/gss_api.h>
  10. #include <linux/sunrpc/addr.h>
  11. #include <linux/err.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/hash.h>
  14. #include <linux/string.h>
  15. #include <linux/slab.h>
  16. #include <net/sock.h>
  17. #include <net/ipv6.h>
  18. #include <linux/kernel.h>
  19. #include <linux/user_namespace.h>
  20. #include <trace/events/sunrpc.h>
  21. #define RPCDBG_FACILITY RPCDBG_AUTH
  22. #include "netns.h"
  23. /*
  24. * AUTHUNIX and AUTHNULL credentials are both handled here.
  25. * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
  26. * are always nobody (-2). i.e. we do the same IP address checks for
  27. * AUTHNULL as for AUTHUNIX, and that is done here.
  28. */
  29. struct unix_domain {
  30. struct auth_domain h;
  31. /* other stuff later */
  32. };
  33. extern struct auth_ops svcauth_null;
  34. extern struct auth_ops svcauth_unix;
  35. extern struct auth_ops svcauth_tls;
  36. static void svcauth_unix_domain_release_rcu(struct rcu_head *head)
  37. {
  38. struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
  39. struct unix_domain *ud = container_of(dom, struct unix_domain, h);
  40. kfree(dom->name);
  41. kfree(ud);
  42. }
  43. static void svcauth_unix_domain_release(struct auth_domain *dom)
  44. {
  45. call_rcu(&dom->rcu_head, svcauth_unix_domain_release_rcu);
  46. }
  47. struct auth_domain *unix_domain_find(char *name)
  48. {
  49. struct auth_domain *rv;
  50. struct unix_domain *new = NULL;
  51. rv = auth_domain_find(name);
  52. while(1) {
  53. if (rv) {
  54. if (new && rv != &new->h)
  55. svcauth_unix_domain_release(&new->h);
  56. if (rv->flavour != &svcauth_unix) {
  57. auth_domain_put(rv);
  58. return NULL;
  59. }
  60. return rv;
  61. }
  62. new = kmalloc_obj(*new);
  63. if (new == NULL)
  64. return NULL;
  65. kref_init(&new->h.ref);
  66. new->h.name = kstrdup(name, GFP_KERNEL);
  67. if (new->h.name == NULL) {
  68. kfree(new);
  69. return NULL;
  70. }
  71. new->h.flavour = &svcauth_unix;
  72. rv = auth_domain_lookup(name, &new->h);
  73. }
  74. }
  75. EXPORT_SYMBOL_GPL(unix_domain_find);
  76. /**************************************************
  77. * cache for IP address to unix_domain
  78. * as needed by AUTH_UNIX
  79. */
  80. #define IP_HASHBITS 8
  81. #define IP_HASHMAX (1<<IP_HASHBITS)
  82. struct ip_map {
  83. struct cache_head h;
  84. char m_class[8]; /* e.g. "nfsd" */
  85. struct in6_addr m_addr;
  86. struct unix_domain *m_client;
  87. struct rcu_head m_rcu;
  88. };
  89. static void ip_map_put(struct kref *kref)
  90. {
  91. struct cache_head *item = container_of(kref, struct cache_head, ref);
  92. struct ip_map *im = container_of(item, struct ip_map,h);
  93. if (test_bit(CACHE_VALID, &item->flags) &&
  94. !test_bit(CACHE_NEGATIVE, &item->flags))
  95. auth_domain_put(&im->m_client->h);
  96. kfree_rcu(im, m_rcu);
  97. }
  98. static inline int hash_ip6(const struct in6_addr *ip)
  99. {
  100. return hash_32(ipv6_addr_hash(ip), IP_HASHBITS);
  101. }
  102. static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
  103. {
  104. struct ip_map *orig = container_of(corig, struct ip_map, h);
  105. struct ip_map *new = container_of(cnew, struct ip_map, h);
  106. return strcmp(orig->m_class, new->m_class) == 0 &&
  107. ipv6_addr_equal(&orig->m_addr, &new->m_addr);
  108. }
  109. static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
  110. {
  111. struct ip_map *new = container_of(cnew, struct ip_map, h);
  112. struct ip_map *item = container_of(citem, struct ip_map, h);
  113. strcpy(new->m_class, item->m_class);
  114. new->m_addr = item->m_addr;
  115. }
  116. static void update(struct cache_head *cnew, struct cache_head *citem)
  117. {
  118. struct ip_map *new = container_of(cnew, struct ip_map, h);
  119. struct ip_map *item = container_of(citem, struct ip_map, h);
  120. kref_get(&item->m_client->h.ref);
  121. new->m_client = item->m_client;
  122. }
  123. static struct cache_head *ip_map_alloc(void)
  124. {
  125. struct ip_map *i = kmalloc_obj(*i);
  126. if (i)
  127. return &i->h;
  128. else
  129. return NULL;
  130. }
  131. static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
  132. {
  133. return sunrpc_cache_pipe_upcall(cd, h);
  134. }
  135. static void ip_map_request(struct cache_detail *cd,
  136. struct cache_head *h,
  137. char **bpp, int *blen)
  138. {
  139. char text_addr[40];
  140. struct ip_map *im = container_of(h, struct ip_map, h);
  141. if (ipv6_addr_v4mapped(&(im->m_addr))) {
  142. snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
  143. } else {
  144. snprintf(text_addr, 40, "%pI6", &im->m_addr);
  145. }
  146. qword_add(bpp, blen, im->m_class);
  147. qword_add(bpp, blen, text_addr);
  148. (*bpp)[-1] = '\n';
  149. }
  150. static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
  151. static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time64_t expiry);
  152. static int ip_map_parse(struct cache_detail *cd,
  153. char *mesg, int mlen)
  154. {
  155. /* class ipaddress [domainname] */
  156. /* should be safe just to use the start of the input buffer
  157. * for scratch: */
  158. char *buf = mesg;
  159. int len;
  160. char class[8];
  161. union {
  162. struct sockaddr sa;
  163. struct sockaddr_in s4;
  164. struct sockaddr_in6 s6;
  165. } address;
  166. struct sockaddr_in6 sin6;
  167. int err;
  168. struct ip_map *ipmp;
  169. struct auth_domain *dom;
  170. time64_t expiry;
  171. if (mesg[mlen-1] != '\n')
  172. return -EINVAL;
  173. mesg[mlen-1] = 0;
  174. /* class */
  175. len = qword_get(&mesg, class, sizeof(class));
  176. if (len <= 0) return -EINVAL;
  177. /* ip address */
  178. len = qword_get(&mesg, buf, mlen);
  179. if (len <= 0) return -EINVAL;
  180. if (rpc_pton(cd->net, buf, len, &address.sa, sizeof(address)) == 0)
  181. return -EINVAL;
  182. switch (address.sa.sa_family) {
  183. case AF_INET:
  184. /* Form a mapped IPv4 address in sin6 */
  185. sin6.sin6_family = AF_INET6;
  186. ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
  187. &sin6.sin6_addr);
  188. break;
  189. #if IS_ENABLED(CONFIG_IPV6)
  190. case AF_INET6:
  191. memcpy(&sin6, &address.s6, sizeof(sin6));
  192. break;
  193. #endif
  194. default:
  195. return -EINVAL;
  196. }
  197. err = get_expiry(&mesg, &expiry);
  198. if (err)
  199. return err;
  200. /* domainname, or empty for NEGATIVE */
  201. len = qword_get(&mesg, buf, mlen);
  202. if (len < 0) return -EINVAL;
  203. if (len) {
  204. dom = unix_domain_find(buf);
  205. if (dom == NULL)
  206. return -ENOENT;
  207. } else
  208. dom = NULL;
  209. /* IPv6 scope IDs are ignored for now */
  210. ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
  211. if (ipmp) {
  212. err = __ip_map_update(cd, ipmp,
  213. container_of(dom, struct unix_domain, h),
  214. expiry);
  215. } else
  216. err = -ENOMEM;
  217. if (dom)
  218. auth_domain_put(dom);
  219. cache_flush();
  220. return err;
  221. }
  222. static int ip_map_show(struct seq_file *m,
  223. struct cache_detail *cd,
  224. struct cache_head *h)
  225. {
  226. struct ip_map *im;
  227. struct in6_addr addr;
  228. char *dom = "-no-domain-";
  229. if (h == NULL) {
  230. seq_puts(m, "#class IP domain\n");
  231. return 0;
  232. }
  233. im = container_of(h, struct ip_map, h);
  234. /* class addr domain */
  235. addr = im->m_addr;
  236. if (test_bit(CACHE_VALID, &h->flags) &&
  237. !test_bit(CACHE_NEGATIVE, &h->flags))
  238. dom = im->m_client->h.name;
  239. if (ipv6_addr_v4mapped(&addr)) {
  240. seq_printf(m, "%s %pI4 %s\n",
  241. im->m_class, &addr.s6_addr32[3], dom);
  242. } else {
  243. seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
  244. }
  245. return 0;
  246. }
  247. static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
  248. struct in6_addr *addr)
  249. {
  250. struct ip_map ip;
  251. struct cache_head *ch;
  252. strcpy(ip.m_class, class);
  253. ip.m_addr = *addr;
  254. ch = sunrpc_cache_lookup_rcu(cd, &ip.h,
  255. hash_str(class, IP_HASHBITS) ^
  256. hash_ip6(addr));
  257. if (ch)
  258. return container_of(ch, struct ip_map, h);
  259. else
  260. return NULL;
  261. }
  262. static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
  263. struct unix_domain *udom, time64_t expiry)
  264. {
  265. struct ip_map ip;
  266. struct cache_head *ch;
  267. ip.m_client = udom;
  268. ip.h.flags = 0;
  269. if (!udom)
  270. set_bit(CACHE_NEGATIVE, &ip.h.flags);
  271. ip.h.expiry_time = expiry;
  272. ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
  273. hash_str(ipm->m_class, IP_HASHBITS) ^
  274. hash_ip6(&ipm->m_addr));
  275. if (!ch)
  276. return -ENOMEM;
  277. cache_put(ch, cd);
  278. return 0;
  279. }
  280. void svcauth_unix_purge(struct net *net)
  281. {
  282. struct sunrpc_net *sn;
  283. sn = net_generic(net, sunrpc_net_id);
  284. cache_purge(sn->ip_map_cache);
  285. }
  286. EXPORT_SYMBOL_GPL(svcauth_unix_purge);
  287. static inline struct ip_map *
  288. ip_map_cached_get(struct svc_xprt *xprt)
  289. {
  290. struct ip_map *ipm = NULL;
  291. struct sunrpc_net *sn;
  292. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  293. spin_lock(&xprt->xpt_lock);
  294. ipm = xprt->xpt_auth_cache;
  295. if (ipm != NULL) {
  296. sn = net_generic(xprt->xpt_net, sunrpc_net_id);
  297. if (cache_is_expired(sn->ip_map_cache, &ipm->h)) {
  298. /*
  299. * The entry has been invalidated since it was
  300. * remembered, e.g. by a second mount from the
  301. * same IP address.
  302. */
  303. xprt->xpt_auth_cache = NULL;
  304. spin_unlock(&xprt->xpt_lock);
  305. cache_put(&ipm->h, sn->ip_map_cache);
  306. return NULL;
  307. }
  308. cache_get(&ipm->h);
  309. }
  310. spin_unlock(&xprt->xpt_lock);
  311. }
  312. return ipm;
  313. }
  314. static inline void
  315. ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
  316. {
  317. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  318. spin_lock(&xprt->xpt_lock);
  319. if (xprt->xpt_auth_cache == NULL) {
  320. /* newly cached, keep the reference */
  321. xprt->xpt_auth_cache = ipm;
  322. ipm = NULL;
  323. }
  324. spin_unlock(&xprt->xpt_lock);
  325. }
  326. if (ipm) {
  327. struct sunrpc_net *sn;
  328. sn = net_generic(xprt->xpt_net, sunrpc_net_id);
  329. cache_put(&ipm->h, sn->ip_map_cache);
  330. }
  331. }
  332. void
  333. svcauth_unix_info_release(struct svc_xprt *xpt)
  334. {
  335. struct ip_map *ipm;
  336. ipm = xpt->xpt_auth_cache;
  337. if (ipm != NULL) {
  338. struct sunrpc_net *sn;
  339. sn = net_generic(xpt->xpt_net, sunrpc_net_id);
  340. cache_put(&ipm->h, sn->ip_map_cache);
  341. }
  342. }
  343. /****************************************************************************
  344. * auth.unix.gid cache
  345. * simple cache to map a UID to a list of GIDs
  346. * because AUTH_UNIX aka AUTH_SYS has a max of UNX_NGROUPS
  347. */
  348. #define GID_HASHBITS 8
  349. #define GID_HASHMAX (1<<GID_HASHBITS)
  350. struct unix_gid {
  351. struct cache_head h;
  352. kuid_t uid;
  353. struct group_info *gi;
  354. struct rcu_head rcu;
  355. };
  356. static int unix_gid_hash(kuid_t uid)
  357. {
  358. return hash_long(from_kuid(&init_user_ns, uid), GID_HASHBITS);
  359. }
  360. static void unix_gid_free(struct rcu_head *rcu)
  361. {
  362. struct unix_gid *ug = container_of(rcu, struct unix_gid, rcu);
  363. struct cache_head *item = &ug->h;
  364. if (test_bit(CACHE_VALID, &item->flags) &&
  365. !test_bit(CACHE_NEGATIVE, &item->flags))
  366. put_group_info(ug->gi);
  367. kfree(ug);
  368. }
  369. static void unix_gid_put(struct kref *kref)
  370. {
  371. struct cache_head *item = container_of(kref, struct cache_head, ref);
  372. struct unix_gid *ug = container_of(item, struct unix_gid, h);
  373. call_rcu(&ug->rcu, unix_gid_free);
  374. }
  375. static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
  376. {
  377. struct unix_gid *orig = container_of(corig, struct unix_gid, h);
  378. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  379. return uid_eq(orig->uid, new->uid);
  380. }
  381. static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
  382. {
  383. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  384. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  385. new->uid = item->uid;
  386. }
  387. static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
  388. {
  389. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  390. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  391. get_group_info(item->gi);
  392. new->gi = item->gi;
  393. }
  394. static struct cache_head *unix_gid_alloc(void)
  395. {
  396. struct unix_gid *g = kmalloc_obj(*g);
  397. if (g)
  398. return &g->h;
  399. else
  400. return NULL;
  401. }
  402. static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
  403. {
  404. return sunrpc_cache_pipe_upcall_timeout(cd, h);
  405. }
  406. static void unix_gid_request(struct cache_detail *cd,
  407. struct cache_head *h,
  408. char **bpp, int *blen)
  409. {
  410. char tuid[20];
  411. struct unix_gid *ug = container_of(h, struct unix_gid, h);
  412. snprintf(tuid, 20, "%u", from_kuid(&init_user_ns, ug->uid));
  413. qword_add(bpp, blen, tuid);
  414. (*bpp)[-1] = '\n';
  415. }
  416. static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid);
  417. static int unix_gid_parse(struct cache_detail *cd,
  418. char *mesg, int mlen)
  419. {
  420. /* uid expiry Ngid gid0 gid1 ... gidN-1 */
  421. int id;
  422. kuid_t uid;
  423. int gids;
  424. int rv;
  425. int i;
  426. int err;
  427. time64_t expiry;
  428. struct unix_gid ug, *ugp;
  429. if (mesg[mlen - 1] != '\n')
  430. return -EINVAL;
  431. mesg[mlen-1] = 0;
  432. rv = get_int(&mesg, &id);
  433. if (rv)
  434. return -EINVAL;
  435. uid = make_kuid(current_user_ns(), id);
  436. ug.uid = uid;
  437. err = get_expiry(&mesg, &expiry);
  438. if (err)
  439. return err;
  440. rv = get_int(&mesg, &gids);
  441. if (rv || gids < 0 || gids > 8192)
  442. return -EINVAL;
  443. ug.gi = groups_alloc(gids);
  444. if (!ug.gi)
  445. return -ENOMEM;
  446. for (i = 0 ; i < gids ; i++) {
  447. int gid;
  448. kgid_t kgid;
  449. rv = get_int(&mesg, &gid);
  450. err = -EINVAL;
  451. if (rv)
  452. goto out;
  453. kgid = make_kgid(current_user_ns(), gid);
  454. if (!gid_valid(kgid))
  455. goto out;
  456. ug.gi->gid[i] = kgid;
  457. }
  458. groups_sort(ug.gi);
  459. ugp = unix_gid_lookup(cd, uid);
  460. if (ugp) {
  461. struct cache_head *ch;
  462. ug.h.flags = 0;
  463. ug.h.expiry_time = expiry;
  464. ch = sunrpc_cache_update(cd,
  465. &ug.h, &ugp->h,
  466. unix_gid_hash(uid));
  467. if (!ch)
  468. err = -ENOMEM;
  469. else {
  470. err = 0;
  471. cache_put(ch, cd);
  472. }
  473. } else
  474. err = -ENOMEM;
  475. out:
  476. if (ug.gi)
  477. put_group_info(ug.gi);
  478. return err;
  479. }
  480. static int unix_gid_show(struct seq_file *m,
  481. struct cache_detail *cd,
  482. struct cache_head *h)
  483. {
  484. struct user_namespace *user_ns = m->file->f_cred->user_ns;
  485. struct unix_gid *ug;
  486. int i;
  487. int glen;
  488. if (h == NULL) {
  489. seq_puts(m, "#uid cnt: gids...\n");
  490. return 0;
  491. }
  492. ug = container_of(h, struct unix_gid, h);
  493. if (test_bit(CACHE_VALID, &h->flags) &&
  494. !test_bit(CACHE_NEGATIVE, &h->flags))
  495. glen = ug->gi->ngroups;
  496. else
  497. glen = 0;
  498. seq_printf(m, "%u %d:", from_kuid_munged(user_ns, ug->uid), glen);
  499. for (i = 0; i < glen; i++)
  500. seq_printf(m, " %d", from_kgid_munged(user_ns, ug->gi->gid[i]));
  501. seq_printf(m, "\n");
  502. return 0;
  503. }
  504. static const struct cache_detail unix_gid_cache_template = {
  505. .owner = THIS_MODULE,
  506. .hash_size = GID_HASHMAX,
  507. .name = "auth.unix.gid",
  508. .cache_put = unix_gid_put,
  509. .cache_upcall = unix_gid_upcall,
  510. .cache_request = unix_gid_request,
  511. .cache_parse = unix_gid_parse,
  512. .cache_show = unix_gid_show,
  513. .match = unix_gid_match,
  514. .init = unix_gid_init,
  515. .update = unix_gid_update,
  516. .alloc = unix_gid_alloc,
  517. };
  518. int unix_gid_cache_create(struct net *net)
  519. {
  520. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  521. struct cache_detail *cd;
  522. int err;
  523. cd = cache_create_net(&unix_gid_cache_template, net);
  524. if (IS_ERR(cd))
  525. return PTR_ERR(cd);
  526. err = cache_register_net(cd, net);
  527. if (err) {
  528. cache_destroy_net(cd, net);
  529. return err;
  530. }
  531. sn->unix_gid_cache = cd;
  532. return 0;
  533. }
  534. void unix_gid_cache_destroy(struct net *net)
  535. {
  536. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  537. struct cache_detail *cd = sn->unix_gid_cache;
  538. sn->unix_gid_cache = NULL;
  539. cache_purge(cd);
  540. cache_unregister_net(cd, net);
  541. cache_destroy_net(cd, net);
  542. }
  543. static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid)
  544. {
  545. struct unix_gid ug;
  546. struct cache_head *ch;
  547. ug.uid = uid;
  548. ch = sunrpc_cache_lookup_rcu(cd, &ug.h, unix_gid_hash(uid));
  549. if (ch)
  550. return container_of(ch, struct unix_gid, h);
  551. else
  552. return NULL;
  553. }
  554. static struct group_info *unix_gid_find(kuid_t uid, struct svc_rqst *rqstp)
  555. {
  556. struct unix_gid *ug;
  557. struct group_info *gi;
  558. int ret;
  559. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
  560. sunrpc_net_id);
  561. ug = unix_gid_lookup(sn->unix_gid_cache, uid);
  562. if (!ug)
  563. return ERR_PTR(-EAGAIN);
  564. ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
  565. switch (ret) {
  566. case -ENOENT:
  567. return ERR_PTR(-ENOENT);
  568. case -ETIMEDOUT:
  569. return ERR_PTR(-ESHUTDOWN);
  570. case 0:
  571. gi = get_group_info(ug->gi);
  572. cache_put(&ug->h, sn->unix_gid_cache);
  573. return gi;
  574. default:
  575. return ERR_PTR(-EAGAIN);
  576. }
  577. }
  578. enum svc_auth_status
  579. svcauth_unix_set_client(struct svc_rqst *rqstp)
  580. {
  581. struct sockaddr_in *sin;
  582. struct sockaddr_in6 *sin6, sin6_storage;
  583. struct ip_map *ipm;
  584. struct group_info *gi;
  585. struct svc_cred *cred = &rqstp->rq_cred;
  586. struct svc_xprt *xprt = rqstp->rq_xprt;
  587. struct net *net = xprt->xpt_net;
  588. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  589. switch (rqstp->rq_addr.ss_family) {
  590. case AF_INET:
  591. sin = svc_addr_in(rqstp);
  592. sin6 = &sin6_storage;
  593. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
  594. break;
  595. case AF_INET6:
  596. sin6 = svc_addr_in6(rqstp);
  597. break;
  598. default:
  599. BUG();
  600. }
  601. rqstp->rq_client = NULL;
  602. if (rqstp->rq_proc == 0)
  603. goto out;
  604. rqstp->rq_auth_stat = rpc_autherr_badcred;
  605. ipm = ip_map_cached_get(xprt);
  606. if (ipm == NULL)
  607. ipm = __ip_map_lookup(sn->ip_map_cache,
  608. rqstp->rq_server->sv_programs->pg_class,
  609. &sin6->sin6_addr);
  610. if (ipm == NULL)
  611. return SVC_DENIED;
  612. switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  613. default:
  614. BUG();
  615. case -ETIMEDOUT:
  616. return SVC_CLOSE;
  617. case -EAGAIN:
  618. return SVC_DROP;
  619. case -ENOENT:
  620. return SVC_DENIED;
  621. case 0:
  622. rqstp->rq_client = &ipm->m_client->h;
  623. kref_get(&rqstp->rq_client->ref);
  624. ip_map_cached_put(xprt, ipm);
  625. break;
  626. }
  627. gi = unix_gid_find(cred->cr_uid, rqstp);
  628. switch (PTR_ERR(gi)) {
  629. case -EAGAIN:
  630. return SVC_DROP;
  631. case -ESHUTDOWN:
  632. return SVC_CLOSE;
  633. case -ENOENT:
  634. break;
  635. default:
  636. put_group_info(cred->cr_group_info);
  637. cred->cr_group_info = gi;
  638. }
  639. out:
  640. rqstp->rq_auth_stat = rpc_auth_ok;
  641. return SVC_OK;
  642. }
  643. EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
  644. /**
  645. * svcauth_null_accept - Decode and validate incoming RPC_AUTH_NULL credential
  646. * @rqstp: RPC transaction
  647. *
  648. * Return values:
  649. * %SVC_OK: Both credential and verifier are valid
  650. * %SVC_DENIED: Credential or verifier is not valid
  651. * %SVC_GARBAGE: Failed to decode credential or verifier
  652. * %SVC_CLOSE: Temporary failure
  653. *
  654. * rqstp->rq_auth_stat is set as mandated by RFC 5531.
  655. */
  656. static enum svc_auth_status
  657. svcauth_null_accept(struct svc_rqst *rqstp)
  658. {
  659. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  660. struct svc_cred *cred = &rqstp->rq_cred;
  661. u32 flavor, len;
  662. void *body;
  663. /* Length of Call's credential body field: */
  664. if (xdr_stream_decode_u32(xdr, &len) < 0)
  665. return SVC_GARBAGE;
  666. if (len != 0) {
  667. rqstp->rq_auth_stat = rpc_autherr_badcred;
  668. return SVC_DENIED;
  669. }
  670. /* Call's verf field: */
  671. if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
  672. return SVC_GARBAGE;
  673. if (flavor != RPC_AUTH_NULL || len != 0) {
  674. rqstp->rq_auth_stat = rpc_autherr_badverf;
  675. return SVC_DENIED;
  676. }
  677. /* Signal that mapping to nobody uid/gid is required */
  678. cred->cr_uid = INVALID_UID;
  679. cred->cr_gid = INVALID_GID;
  680. cred->cr_group_info = groups_alloc(0);
  681. if (cred->cr_group_info == NULL)
  682. return SVC_CLOSE; /* kmalloc failure - client must retry */
  683. if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream,
  684. RPC_AUTH_NULL, NULL, 0) < 0)
  685. return SVC_CLOSE;
  686. if (!svcxdr_set_accept_stat(rqstp))
  687. return SVC_CLOSE;
  688. rqstp->rq_cred.cr_flavor = RPC_AUTH_NULL;
  689. return SVC_OK;
  690. }
  691. static int
  692. svcauth_null_release(struct svc_rqst *rqstp)
  693. {
  694. if (rqstp->rq_client)
  695. auth_domain_put(rqstp->rq_client);
  696. rqstp->rq_client = NULL;
  697. if (rqstp->rq_cred.cr_group_info)
  698. put_group_info(rqstp->rq_cred.cr_group_info);
  699. rqstp->rq_cred.cr_group_info = NULL;
  700. return 0; /* don't drop */
  701. }
  702. struct auth_ops svcauth_null = {
  703. .name = "null",
  704. .owner = THIS_MODULE,
  705. .flavour = RPC_AUTH_NULL,
  706. .accept = svcauth_null_accept,
  707. .release = svcauth_null_release,
  708. .set_client = svcauth_unix_set_client,
  709. };
  710. /**
  711. * svcauth_tls_accept - Decode and validate incoming RPC_AUTH_TLS credential
  712. * @rqstp: RPC transaction
  713. *
  714. * Return values:
  715. * %SVC_OK: Both credential and verifier are valid
  716. * %SVC_DENIED: Credential or verifier is not valid
  717. * %SVC_GARBAGE: Failed to decode credential or verifier
  718. * %SVC_CLOSE: Temporary failure
  719. *
  720. * rqstp->rq_auth_stat is set as mandated by RFC 5531.
  721. */
  722. static enum svc_auth_status
  723. svcauth_tls_accept(struct svc_rqst *rqstp)
  724. {
  725. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  726. struct svc_cred *cred = &rqstp->rq_cred;
  727. struct svc_xprt *xprt = rqstp->rq_xprt;
  728. u32 flavor, len;
  729. void *body;
  730. __be32 *p;
  731. /* Length of Call's credential body field: */
  732. if (xdr_stream_decode_u32(xdr, &len) < 0)
  733. return SVC_GARBAGE;
  734. if (len != 0) {
  735. rqstp->rq_auth_stat = rpc_autherr_badcred;
  736. return SVC_DENIED;
  737. }
  738. /* Call's verf field: */
  739. if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
  740. return SVC_GARBAGE;
  741. if (flavor != RPC_AUTH_NULL || len != 0) {
  742. rqstp->rq_auth_stat = rpc_autherr_badverf;
  743. return SVC_DENIED;
  744. }
  745. /* AUTH_TLS is not valid on non-NULL procedures */
  746. if (rqstp->rq_proc != 0) {
  747. rqstp->rq_auth_stat = rpc_autherr_badcred;
  748. return SVC_DENIED;
  749. }
  750. /* Signal that mapping to nobody uid/gid is required */
  751. cred->cr_uid = INVALID_UID;
  752. cred->cr_gid = INVALID_GID;
  753. cred->cr_group_info = groups_alloc(0);
  754. if (cred->cr_group_info == NULL)
  755. return SVC_CLOSE;
  756. if (xprt->xpt_ops->xpo_handshake) {
  757. p = xdr_reserve_space(&rqstp->rq_res_stream, XDR_UNIT * 2 + 8);
  758. if (!p)
  759. return SVC_CLOSE;
  760. trace_svc_tls_start(xprt);
  761. *p++ = rpc_auth_null;
  762. *p++ = cpu_to_be32(8);
  763. memcpy(p, "STARTTLS", 8);
  764. set_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
  765. svc_xprt_enqueue(xprt);
  766. } else {
  767. trace_svc_tls_unavailable(xprt);
  768. if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream,
  769. RPC_AUTH_NULL, NULL, 0) < 0)
  770. return SVC_CLOSE;
  771. }
  772. if (!svcxdr_set_accept_stat(rqstp))
  773. return SVC_CLOSE;
  774. rqstp->rq_cred.cr_flavor = RPC_AUTH_TLS;
  775. return SVC_OK;
  776. }
  777. struct auth_ops svcauth_tls = {
  778. .name = "tls",
  779. .owner = THIS_MODULE,
  780. .flavour = RPC_AUTH_TLS,
  781. .accept = svcauth_tls_accept,
  782. .release = svcauth_null_release,
  783. .set_client = svcauth_unix_set_client,
  784. };
  785. /**
  786. * svcauth_unix_accept - Decode and validate incoming RPC_AUTH_SYS credential
  787. * @rqstp: RPC transaction
  788. *
  789. * Return values:
  790. * %SVC_OK: Both credential and verifier are valid
  791. * %SVC_DENIED: Credential or verifier is not valid
  792. * %SVC_GARBAGE: Failed to decode credential or verifier
  793. * %SVC_CLOSE: Temporary failure
  794. *
  795. * rqstp->rq_auth_stat is set as mandated by RFC 5531.
  796. */
  797. static enum svc_auth_status
  798. svcauth_unix_accept(struct svc_rqst *rqstp)
  799. {
  800. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  801. struct svc_cred *cred = &rqstp->rq_cred;
  802. struct user_namespace *userns;
  803. u32 flavor, len, i;
  804. void *body;
  805. __be32 *p;
  806. /*
  807. * This implementation ignores the length of the Call's
  808. * credential body field and the timestamp and machinename
  809. * fields.
  810. */
  811. p = xdr_inline_decode(xdr, XDR_UNIT * 3);
  812. if (!p)
  813. return SVC_GARBAGE;
  814. len = be32_to_cpup(p + 2);
  815. if (len > RPC_MAX_MACHINENAME)
  816. return SVC_GARBAGE;
  817. if (!xdr_inline_decode(xdr, len))
  818. return SVC_GARBAGE;
  819. /*
  820. * Note: we skip uid_valid()/gid_valid() checks here for
  821. * backwards compatibility with clients that use -1 id's.
  822. * Instead, -1 uid or gid is later mapped to the
  823. * (export-specific) anonymous id by nfsd_setuser.
  824. * Supplementary gid's will be left alone.
  825. */
  826. userns = (rqstp->rq_xprt && rqstp->rq_xprt->xpt_cred) ?
  827. rqstp->rq_xprt->xpt_cred->user_ns : &init_user_ns;
  828. if (xdr_stream_decode_u32(xdr, &i) < 0)
  829. return SVC_GARBAGE;
  830. cred->cr_uid = make_kuid(userns, i);
  831. if (xdr_stream_decode_u32(xdr, &i) < 0)
  832. return SVC_GARBAGE;
  833. cred->cr_gid = make_kgid(userns, i);
  834. if (xdr_stream_decode_u32(xdr, &len) < 0)
  835. return SVC_GARBAGE;
  836. if (len > UNX_NGROUPS)
  837. goto badcred;
  838. p = xdr_inline_decode(xdr, XDR_UNIT * len);
  839. if (!p)
  840. return SVC_GARBAGE;
  841. cred->cr_group_info = groups_alloc(len);
  842. if (cred->cr_group_info == NULL)
  843. return SVC_CLOSE;
  844. for (i = 0; i < len; i++) {
  845. kgid_t kgid = make_kgid(userns, be32_to_cpup(p++));
  846. cred->cr_group_info->gid[i] = kgid;
  847. }
  848. groups_sort(cred->cr_group_info);
  849. /* Call's verf field: */
  850. if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
  851. return SVC_GARBAGE;
  852. if (flavor != RPC_AUTH_NULL || len != 0) {
  853. rqstp->rq_auth_stat = rpc_autherr_badverf;
  854. return SVC_DENIED;
  855. }
  856. if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream,
  857. RPC_AUTH_NULL, NULL, 0) < 0)
  858. return SVC_CLOSE;
  859. if (!svcxdr_set_accept_stat(rqstp))
  860. return SVC_CLOSE;
  861. rqstp->rq_cred.cr_flavor = RPC_AUTH_UNIX;
  862. return SVC_OK;
  863. badcred:
  864. rqstp->rq_auth_stat = rpc_autherr_badcred;
  865. return SVC_DENIED;
  866. }
  867. static int
  868. svcauth_unix_release(struct svc_rqst *rqstp)
  869. {
  870. /* Verifier (such as it is) is already in place.
  871. */
  872. if (rqstp->rq_client)
  873. auth_domain_put(rqstp->rq_client);
  874. rqstp->rq_client = NULL;
  875. if (rqstp->rq_cred.cr_group_info)
  876. put_group_info(rqstp->rq_cred.cr_group_info);
  877. rqstp->rq_cred.cr_group_info = NULL;
  878. return 0;
  879. }
  880. struct auth_ops svcauth_unix = {
  881. .name = "unix",
  882. .owner = THIS_MODULE,
  883. .flavour = RPC_AUTH_UNIX,
  884. .accept = svcauth_unix_accept,
  885. .release = svcauth_unix_release,
  886. .domain_release = svcauth_unix_domain_release,
  887. .set_client = svcauth_unix_set_client,
  888. };
  889. static const struct cache_detail ip_map_cache_template = {
  890. .owner = THIS_MODULE,
  891. .hash_size = IP_HASHMAX,
  892. .name = "auth.unix.ip",
  893. .cache_put = ip_map_put,
  894. .cache_upcall = ip_map_upcall,
  895. .cache_request = ip_map_request,
  896. .cache_parse = ip_map_parse,
  897. .cache_show = ip_map_show,
  898. .match = ip_map_match,
  899. .init = ip_map_init,
  900. .update = update,
  901. .alloc = ip_map_alloc,
  902. };
  903. int ip_map_cache_create(struct net *net)
  904. {
  905. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  906. struct cache_detail *cd;
  907. int err;
  908. cd = cache_create_net(&ip_map_cache_template, net);
  909. if (IS_ERR(cd))
  910. return PTR_ERR(cd);
  911. err = cache_register_net(cd, net);
  912. if (err) {
  913. cache_destroy_net(cd, net);
  914. return err;
  915. }
  916. sn->ip_map_cache = cd;
  917. return 0;
  918. }
  919. void ip_map_cache_destroy(struct net *net)
  920. {
  921. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  922. struct cache_detail *cd = sn->ip_map_cache;
  923. sn->ip_map_cache = NULL;
  924. cache_purge(cd);
  925. cache_unregister_net(cd, net);
  926. cache_destroy_net(cd, net);
  927. }