svcauth_unix.c 21 KB

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