svcauth_unix.c 21 KB

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