svcauth_unix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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(struct net *net)
  300. {
  301. struct sunrpc_net *sn;
  302. sn = net_generic(net, sunrpc_net_id);
  303. cache_purge(sn->ip_map_cache);
  304. }
  305. EXPORT_SYMBOL_GPL(svcauth_unix_purge);
  306. static inline struct ip_map *
  307. ip_map_cached_get(struct svc_xprt *xprt)
  308. {
  309. struct ip_map *ipm = NULL;
  310. struct sunrpc_net *sn;
  311. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  312. spin_lock(&xprt->xpt_lock);
  313. ipm = xprt->xpt_auth_cache;
  314. if (ipm != NULL) {
  315. if (!cache_valid(&ipm->h)) {
  316. /*
  317. * The entry has been invalidated since it was
  318. * remembered, e.g. by a second mount from the
  319. * same IP address.
  320. */
  321. sn = net_generic(xprt->xpt_net, sunrpc_net_id);
  322. xprt->xpt_auth_cache = NULL;
  323. spin_unlock(&xprt->xpt_lock);
  324. cache_put(&ipm->h, sn->ip_map_cache);
  325. return NULL;
  326. }
  327. cache_get(&ipm->h);
  328. }
  329. spin_unlock(&xprt->xpt_lock);
  330. }
  331. return ipm;
  332. }
  333. static inline void
  334. ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
  335. {
  336. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  337. spin_lock(&xprt->xpt_lock);
  338. if (xprt->xpt_auth_cache == NULL) {
  339. /* newly cached, keep the reference */
  340. xprt->xpt_auth_cache = ipm;
  341. ipm = NULL;
  342. }
  343. spin_unlock(&xprt->xpt_lock);
  344. }
  345. if (ipm) {
  346. struct sunrpc_net *sn;
  347. sn = net_generic(xprt->xpt_net, sunrpc_net_id);
  348. cache_put(&ipm->h, sn->ip_map_cache);
  349. }
  350. }
  351. void
  352. svcauth_unix_info_release(struct svc_xprt *xpt)
  353. {
  354. struct ip_map *ipm;
  355. ipm = xpt->xpt_auth_cache;
  356. if (ipm != NULL) {
  357. struct sunrpc_net *sn;
  358. sn = net_generic(xpt->xpt_net, sunrpc_net_id);
  359. cache_put(&ipm->h, sn->ip_map_cache);
  360. }
  361. }
  362. /****************************************************************************
  363. * auth.unix.gid cache
  364. * simple cache to map a UID to a list of GIDs
  365. * because AUTH_UNIX aka AUTH_SYS has a max of 16
  366. */
  367. #define GID_HASHBITS 8
  368. #define GID_HASHMAX (1<<GID_HASHBITS)
  369. struct unix_gid {
  370. struct cache_head h;
  371. uid_t uid;
  372. struct group_info *gi;
  373. };
  374. static void unix_gid_put(struct kref *kref)
  375. {
  376. struct cache_head *item = container_of(kref, struct cache_head, ref);
  377. struct unix_gid *ug = container_of(item, struct unix_gid, h);
  378. if (test_bit(CACHE_VALID, &item->flags) &&
  379. !test_bit(CACHE_NEGATIVE, &item->flags))
  380. put_group_info(ug->gi);
  381. kfree(ug);
  382. }
  383. static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
  384. {
  385. struct unix_gid *orig = container_of(corig, struct unix_gid, h);
  386. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  387. return orig->uid == new->uid;
  388. }
  389. static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
  390. {
  391. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  392. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  393. new->uid = item->uid;
  394. }
  395. static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
  396. {
  397. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  398. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  399. get_group_info(item->gi);
  400. new->gi = item->gi;
  401. }
  402. static struct cache_head *unix_gid_alloc(void)
  403. {
  404. struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
  405. if (g)
  406. return &g->h;
  407. else
  408. return NULL;
  409. }
  410. static void unix_gid_request(struct cache_detail *cd,
  411. struct cache_head *h,
  412. char **bpp, int *blen)
  413. {
  414. char tuid[20];
  415. struct unix_gid *ug = container_of(h, struct unix_gid, h);
  416. snprintf(tuid, 20, "%u", ug->uid);
  417. qword_add(bpp, blen, tuid);
  418. (*bpp)[-1] = '\n';
  419. }
  420. static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
  421. {
  422. return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
  423. }
  424. static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, uid_t uid);
  425. static int unix_gid_parse(struct cache_detail *cd,
  426. char *mesg, int mlen)
  427. {
  428. /* uid expiry Ngid gid0 gid1 ... gidN-1 */
  429. int uid;
  430. int gids;
  431. int rv;
  432. int i;
  433. int err;
  434. time_t expiry;
  435. struct unix_gid ug, *ugp;
  436. if (mesg[mlen - 1] != '\n')
  437. return -EINVAL;
  438. mesg[mlen-1] = 0;
  439. rv = get_int(&mesg, &uid);
  440. if (rv)
  441. return -EINVAL;
  442. ug.uid = uid;
  443. expiry = get_expiry(&mesg);
  444. if (expiry == 0)
  445. return -EINVAL;
  446. rv = get_int(&mesg, &gids);
  447. if (rv || gids < 0 || gids > 8192)
  448. return -EINVAL;
  449. ug.gi = groups_alloc(gids);
  450. if (!ug.gi)
  451. return -ENOMEM;
  452. for (i = 0 ; i < gids ; i++) {
  453. int gid;
  454. kgid_t kgid;
  455. rv = get_int(&mesg, &gid);
  456. err = -EINVAL;
  457. if (rv)
  458. goto out;
  459. kgid = make_kgid(&init_user_ns, gid);
  460. if (!gid_valid(kgid))
  461. goto out;
  462. GROUP_AT(ug.gi, i) = kgid;
  463. }
  464. ugp = unix_gid_lookup(cd, uid);
  465. if (ugp) {
  466. struct cache_head *ch;
  467. ug.h.flags = 0;
  468. ug.h.expiry_time = expiry;
  469. ch = sunrpc_cache_update(cd,
  470. &ug.h, &ugp->h,
  471. hash_long(uid, GID_HASHBITS));
  472. if (!ch)
  473. err = -ENOMEM;
  474. else {
  475. err = 0;
  476. cache_put(ch, cd);
  477. }
  478. } else
  479. err = -ENOMEM;
  480. out:
  481. if (ug.gi)
  482. put_group_info(ug.gi);
  483. return err;
  484. }
  485. static int unix_gid_show(struct seq_file *m,
  486. struct cache_detail *cd,
  487. struct cache_head *h)
  488. {
  489. struct user_namespace *user_ns = current_user_ns();
  490. struct unix_gid *ug;
  491. int i;
  492. int glen;
  493. if (h == NULL) {
  494. seq_puts(m, "#uid cnt: gids...\n");
  495. return 0;
  496. }
  497. ug = container_of(h, struct unix_gid, h);
  498. if (test_bit(CACHE_VALID, &h->flags) &&
  499. !test_bit(CACHE_NEGATIVE, &h->flags))
  500. glen = ug->gi->ngroups;
  501. else
  502. glen = 0;
  503. seq_printf(m, "%u %d:", ug->uid, glen);
  504. for (i = 0; i < glen; i++)
  505. seq_printf(m, " %d", from_kgid_munged(user_ns, GROUP_AT(ug->gi, i)));
  506. seq_printf(m, "\n");
  507. return 0;
  508. }
  509. static struct cache_detail unix_gid_cache_template = {
  510. .owner = THIS_MODULE,
  511. .hash_size = GID_HASHMAX,
  512. .name = "auth.unix.gid",
  513. .cache_put = unix_gid_put,
  514. .cache_upcall = unix_gid_upcall,
  515. .cache_parse = unix_gid_parse,
  516. .cache_show = unix_gid_show,
  517. .match = unix_gid_match,
  518. .init = unix_gid_init,
  519. .update = unix_gid_update,
  520. .alloc = unix_gid_alloc,
  521. };
  522. int unix_gid_cache_create(struct net *net)
  523. {
  524. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  525. struct cache_detail *cd;
  526. int err;
  527. cd = cache_create_net(&unix_gid_cache_template, net);
  528. if (IS_ERR(cd))
  529. return PTR_ERR(cd);
  530. err = cache_register_net(cd, net);
  531. if (err) {
  532. cache_destroy_net(cd, net);
  533. return err;
  534. }
  535. sn->unix_gid_cache = cd;
  536. return 0;
  537. }
  538. void unix_gid_cache_destroy(struct net *net)
  539. {
  540. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  541. struct cache_detail *cd = sn->unix_gid_cache;
  542. sn->unix_gid_cache = NULL;
  543. cache_purge(cd);
  544. cache_unregister_net(cd, net);
  545. cache_destroy_net(cd, net);
  546. }
  547. static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, uid_t uid)
  548. {
  549. struct unix_gid ug;
  550. struct cache_head *ch;
  551. ug.uid = uid;
  552. ch = sunrpc_cache_lookup(cd, &ug.h, hash_long(uid, GID_HASHBITS));
  553. if (ch)
  554. return container_of(ch, struct unix_gid, h);
  555. else
  556. return NULL;
  557. }
  558. static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
  559. {
  560. struct unix_gid *ug;
  561. struct group_info *gi;
  562. int ret;
  563. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
  564. sunrpc_net_id);
  565. ug = unix_gid_lookup(sn->unix_gid_cache, uid);
  566. if (!ug)
  567. return ERR_PTR(-EAGAIN);
  568. ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
  569. switch (ret) {
  570. case -ENOENT:
  571. return ERR_PTR(-ENOENT);
  572. case -ETIMEDOUT:
  573. return ERR_PTR(-ESHUTDOWN);
  574. case 0:
  575. gi = get_group_info(ug->gi);
  576. cache_put(&ug->h, sn->unix_gid_cache);
  577. return gi;
  578. default:
  579. return ERR_PTR(-EAGAIN);
  580. }
  581. }
  582. int
  583. svcauth_unix_set_client(struct svc_rqst *rqstp)
  584. {
  585. struct sockaddr_in *sin;
  586. struct sockaddr_in6 *sin6, sin6_storage;
  587. struct ip_map *ipm;
  588. struct group_info *gi;
  589. struct svc_cred *cred = &rqstp->rq_cred;
  590. struct svc_xprt *xprt = rqstp->rq_xprt;
  591. struct net *net = xprt->xpt_net;
  592. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  593. switch (rqstp->rq_addr.ss_family) {
  594. case AF_INET:
  595. sin = svc_addr_in(rqstp);
  596. sin6 = &sin6_storage;
  597. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
  598. break;
  599. case AF_INET6:
  600. sin6 = svc_addr_in6(rqstp);
  601. break;
  602. default:
  603. BUG();
  604. }
  605. rqstp->rq_client = NULL;
  606. if (rqstp->rq_proc == 0)
  607. return SVC_OK;
  608. ipm = ip_map_cached_get(xprt);
  609. if (ipm == NULL)
  610. ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
  611. &sin6->sin6_addr);
  612. if (ipm == NULL)
  613. return SVC_DENIED;
  614. switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  615. default:
  616. BUG();
  617. case -ETIMEDOUT:
  618. return SVC_CLOSE;
  619. case -EAGAIN:
  620. return SVC_DROP;
  621. case -ENOENT:
  622. return SVC_DENIED;
  623. case 0:
  624. rqstp->rq_client = &ipm->m_client->h;
  625. kref_get(&rqstp->rq_client->ref);
  626. ip_map_cached_put(xprt, ipm);
  627. break;
  628. }
  629. gi = unix_gid_find(cred->cr_uid, rqstp);
  630. switch (PTR_ERR(gi)) {
  631. case -EAGAIN:
  632. return SVC_DROP;
  633. case -ESHUTDOWN:
  634. return SVC_CLOSE;
  635. case -ENOENT:
  636. break;
  637. default:
  638. put_group_info(cred->cr_group_info);
  639. cred->cr_group_info = gi;
  640. }
  641. return SVC_OK;
  642. }
  643. EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
  644. static int
  645. svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
  646. {
  647. struct kvec *argv = &rqstp->rq_arg.head[0];
  648. struct kvec *resv = &rqstp->rq_res.head[0];
  649. struct svc_cred *cred = &rqstp->rq_cred;
  650. cred->cr_group_info = NULL;
  651. rqstp->rq_client = NULL;
  652. if (argv->iov_len < 3*4)
  653. return SVC_GARBAGE;
  654. if (svc_getu32(argv) != 0) {
  655. dprintk("svc: bad null cred\n");
  656. *authp = rpc_autherr_badcred;
  657. return SVC_DENIED;
  658. }
  659. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  660. dprintk("svc: bad null verf\n");
  661. *authp = rpc_autherr_badverf;
  662. return SVC_DENIED;
  663. }
  664. /* Signal that mapping to nobody uid/gid is required */
  665. cred->cr_uid = (uid_t) -1;
  666. cred->cr_gid = (gid_t) -1;
  667. cred->cr_group_info = groups_alloc(0);
  668. if (cred->cr_group_info == NULL)
  669. return SVC_CLOSE; /* kmalloc failure - client must retry */
  670. /* Put NULL verifier */
  671. svc_putnl(resv, RPC_AUTH_NULL);
  672. svc_putnl(resv, 0);
  673. rqstp->rq_flavor = RPC_AUTH_NULL;
  674. return SVC_OK;
  675. }
  676. static int
  677. svcauth_null_release(struct svc_rqst *rqstp)
  678. {
  679. if (rqstp->rq_client)
  680. auth_domain_put(rqstp->rq_client);
  681. rqstp->rq_client = NULL;
  682. if (rqstp->rq_cred.cr_group_info)
  683. put_group_info(rqstp->rq_cred.cr_group_info);
  684. rqstp->rq_cred.cr_group_info = NULL;
  685. return 0; /* don't drop */
  686. }
  687. struct auth_ops svcauth_null = {
  688. .name = "null",
  689. .owner = THIS_MODULE,
  690. .flavour = RPC_AUTH_NULL,
  691. .accept = svcauth_null_accept,
  692. .release = svcauth_null_release,
  693. .set_client = svcauth_unix_set_client,
  694. };
  695. static int
  696. svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
  697. {
  698. struct kvec *argv = &rqstp->rq_arg.head[0];
  699. struct kvec *resv = &rqstp->rq_res.head[0];
  700. struct svc_cred *cred = &rqstp->rq_cred;
  701. u32 slen, i;
  702. int len = argv->iov_len;
  703. cred->cr_group_info = NULL;
  704. rqstp->rq_client = NULL;
  705. if ((len -= 3*4) < 0)
  706. return SVC_GARBAGE;
  707. svc_getu32(argv); /* length */
  708. svc_getu32(argv); /* time stamp */
  709. slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
  710. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  711. goto badcred;
  712. argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
  713. argv->iov_len -= slen*4;
  714. cred->cr_uid = svc_getnl(argv); /* uid */
  715. cred->cr_gid = svc_getnl(argv); /* gid */
  716. slen = svc_getnl(argv); /* gids length */
  717. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  718. goto badcred;
  719. cred->cr_group_info = groups_alloc(slen);
  720. if (cred->cr_group_info == NULL)
  721. return SVC_CLOSE;
  722. for (i = 0; i < slen; i++) {
  723. kgid_t kgid = make_kgid(&init_user_ns, svc_getnl(argv));
  724. if (!gid_valid(kgid))
  725. goto badcred;
  726. GROUP_AT(cred->cr_group_info, i) = kgid;
  727. }
  728. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  729. *authp = rpc_autherr_badverf;
  730. return SVC_DENIED;
  731. }
  732. /* Put NULL verifier */
  733. svc_putnl(resv, RPC_AUTH_NULL);
  734. svc_putnl(resv, 0);
  735. rqstp->rq_flavor = RPC_AUTH_UNIX;
  736. return SVC_OK;
  737. badcred:
  738. *authp = rpc_autherr_badcred;
  739. return SVC_DENIED;
  740. }
  741. static int
  742. svcauth_unix_release(struct svc_rqst *rqstp)
  743. {
  744. /* Verifier (such as it is) is already in place.
  745. */
  746. if (rqstp->rq_client)
  747. auth_domain_put(rqstp->rq_client);
  748. rqstp->rq_client = NULL;
  749. if (rqstp->rq_cred.cr_group_info)
  750. put_group_info(rqstp->rq_cred.cr_group_info);
  751. rqstp->rq_cred.cr_group_info = NULL;
  752. return 0;
  753. }
  754. struct auth_ops svcauth_unix = {
  755. .name = "unix",
  756. .owner = THIS_MODULE,
  757. .flavour = RPC_AUTH_UNIX,
  758. .accept = svcauth_unix_accept,
  759. .release = svcauth_unix_release,
  760. .domain_release = svcauth_unix_domain_release,
  761. .set_client = svcauth_unix_set_client,
  762. };
  763. static struct cache_detail ip_map_cache_template = {
  764. .owner = THIS_MODULE,
  765. .hash_size = IP_HASHMAX,
  766. .name = "auth.unix.ip",
  767. .cache_put = ip_map_put,
  768. .cache_upcall = ip_map_upcall,
  769. .cache_parse = ip_map_parse,
  770. .cache_show = ip_map_show,
  771. .match = ip_map_match,
  772. .init = ip_map_init,
  773. .update = update,
  774. .alloc = ip_map_alloc,
  775. };
  776. int ip_map_cache_create(struct net *net)
  777. {
  778. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  779. struct cache_detail *cd;
  780. int err;
  781. cd = cache_create_net(&ip_map_cache_template, net);
  782. if (IS_ERR(cd))
  783. return PTR_ERR(cd);
  784. err = cache_register_net(cd, net);
  785. if (err) {
  786. cache_destroy_net(cd, net);
  787. return err;
  788. }
  789. sn->ip_map_cache = cd;
  790. return 0;
  791. }
  792. void ip_map_cache_destroy(struct net *net)
  793. {
  794. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  795. struct cache_detail *cd = sn->ip_map_cache;
  796. sn->ip_map_cache = NULL;
  797. cache_purge(cd);
  798. cache_unregister_net(cd, net);
  799. cache_destroy_net(cd, net);
  800. }