svcauth_unix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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/err.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/hash.h>
  11. #include <linux/string.h>
  12. #include <net/sock.h>
  13. #define RPCDBG_FACILITY RPCDBG_AUTH
  14. /*
  15. * AUTHUNIX and AUTHNULL credentials are both handled here.
  16. * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
  17. * are always nobody (-2). i.e. we do the same IP address checks for
  18. * AUTHNULL as for AUTHUNIX, and that is done here.
  19. */
  20. struct unix_domain {
  21. struct auth_domain h;
  22. int addr_changes;
  23. /* other stuff later */
  24. };
  25. extern struct auth_ops svcauth_unix;
  26. struct auth_domain *unix_domain_find(char *name)
  27. {
  28. struct auth_domain *rv;
  29. struct unix_domain *new = NULL;
  30. rv = auth_domain_lookup(name, NULL);
  31. while(1) {
  32. if (rv) {
  33. if (new && rv != &new->h)
  34. auth_domain_put(&new->h);
  35. if (rv->flavour != &svcauth_unix) {
  36. auth_domain_put(rv);
  37. return NULL;
  38. }
  39. return rv;
  40. }
  41. new = kmalloc(sizeof(*new), GFP_KERNEL);
  42. if (new == NULL)
  43. return NULL;
  44. kref_init(&new->h.ref);
  45. new->h.name = kstrdup(name, GFP_KERNEL);
  46. if (new->h.name == NULL) {
  47. kfree(new);
  48. return NULL;
  49. }
  50. new->h.flavour = &svcauth_unix;
  51. new->addr_changes = 0;
  52. rv = auth_domain_lookup(name, &new->h);
  53. }
  54. }
  55. static void svcauth_unix_domain_release(struct auth_domain *dom)
  56. {
  57. struct unix_domain *ud = container_of(dom, struct unix_domain, h);
  58. kfree(dom->name);
  59. kfree(ud);
  60. }
  61. /**************************************************
  62. * cache for IP address to unix_domain
  63. * as needed by AUTH_UNIX
  64. */
  65. #define IP_HASHBITS 8
  66. #define IP_HASHMAX (1<<IP_HASHBITS)
  67. #define IP_HASHMASK (IP_HASHMAX-1)
  68. struct ip_map {
  69. struct cache_head h;
  70. char m_class[8]; /* e.g. "nfsd" */
  71. struct in_addr m_addr;
  72. struct unix_domain *m_client;
  73. int m_add_change;
  74. };
  75. static struct cache_head *ip_table[IP_HASHMAX];
  76. static void ip_map_put(struct kref *kref)
  77. {
  78. struct cache_head *item = container_of(kref, struct cache_head, ref);
  79. struct ip_map *im = container_of(item, struct ip_map,h);
  80. if (test_bit(CACHE_VALID, &item->flags) &&
  81. !test_bit(CACHE_NEGATIVE, &item->flags))
  82. auth_domain_put(&im->m_client->h);
  83. kfree(im);
  84. }
  85. #if IP_HASHBITS == 8
  86. /* hash_long on a 64 bit machine is currently REALLY BAD for
  87. * IP addresses in reverse-endian (i.e. on a little-endian machine).
  88. * So use a trivial but reliable hash instead
  89. */
  90. static inline int hash_ip(__be32 ip)
  91. {
  92. int hash = (__force u32)ip ^ ((__force u32)ip>>16);
  93. return (hash ^ (hash>>8)) & 0xff;
  94. }
  95. #endif
  96. static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
  97. {
  98. struct ip_map *orig = container_of(corig, struct ip_map, h);
  99. struct ip_map *new = container_of(cnew, struct ip_map, h);
  100. return strcmp(orig->m_class, new->m_class) == 0
  101. && orig->m_addr.s_addr == new->m_addr.s_addr;
  102. }
  103. static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
  104. {
  105. struct ip_map *new = container_of(cnew, struct ip_map, h);
  106. struct ip_map *item = container_of(citem, struct ip_map, h);
  107. strcpy(new->m_class, item->m_class);
  108. new->m_addr.s_addr = item->m_addr.s_addr;
  109. }
  110. static void update(struct cache_head *cnew, struct cache_head *citem)
  111. {
  112. struct ip_map *new = container_of(cnew, struct ip_map, h);
  113. struct ip_map *item = container_of(citem, struct ip_map, h);
  114. kref_get(&item->m_client->h.ref);
  115. new->m_client = item->m_client;
  116. new->m_add_change = item->m_add_change;
  117. }
  118. static struct cache_head *ip_map_alloc(void)
  119. {
  120. struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
  121. if (i)
  122. return &i->h;
  123. else
  124. return NULL;
  125. }
  126. static void ip_map_request(struct cache_detail *cd,
  127. struct cache_head *h,
  128. char **bpp, int *blen)
  129. {
  130. char text_addr[20];
  131. struct ip_map *im = container_of(h, struct ip_map, h);
  132. __be32 addr = im->m_addr.s_addr;
  133. snprintf(text_addr, 20, "%u.%u.%u.%u",
  134. ntohl(addr) >> 24 & 0xff,
  135. ntohl(addr) >> 16 & 0xff,
  136. ntohl(addr) >> 8 & 0xff,
  137. ntohl(addr) >> 0 & 0xff);
  138. qword_add(bpp, blen, im->m_class);
  139. qword_add(bpp, blen, text_addr);
  140. (*bpp)[-1] = '\n';
  141. }
  142. static struct ip_map *ip_map_lookup(char *class, struct in_addr addr);
  143. static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
  144. static int ip_map_parse(struct cache_detail *cd,
  145. char *mesg, int mlen)
  146. {
  147. /* class ipaddress [domainname] */
  148. /* should be safe just to use the start of the input buffer
  149. * for scratch: */
  150. char *buf = mesg;
  151. int len;
  152. int b1,b2,b3,b4;
  153. char c;
  154. char class[8];
  155. struct in_addr addr;
  156. int err;
  157. struct ip_map *ipmp;
  158. struct auth_domain *dom;
  159. time_t expiry;
  160. if (mesg[mlen-1] != '\n')
  161. return -EINVAL;
  162. mesg[mlen-1] = 0;
  163. /* class */
  164. len = qword_get(&mesg, class, sizeof(class));
  165. if (len <= 0) return -EINVAL;
  166. /* ip address */
  167. len = qword_get(&mesg, buf, mlen);
  168. if (len <= 0) return -EINVAL;
  169. if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
  170. return -EINVAL;
  171. expiry = get_expiry(&mesg);
  172. if (expiry ==0)
  173. return -EINVAL;
  174. /* domainname, or empty for NEGATIVE */
  175. len = qword_get(&mesg, buf, mlen);
  176. if (len < 0) return -EINVAL;
  177. if (len) {
  178. dom = unix_domain_find(buf);
  179. if (dom == NULL)
  180. return -ENOENT;
  181. } else
  182. dom = NULL;
  183. addr.s_addr =
  184. htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
  185. ipmp = ip_map_lookup(class,addr);
  186. if (ipmp) {
  187. err = ip_map_update(ipmp,
  188. container_of(dom, struct unix_domain, h),
  189. expiry);
  190. } else
  191. err = -ENOMEM;
  192. if (dom)
  193. auth_domain_put(dom);
  194. cache_flush();
  195. return err;
  196. }
  197. static int ip_map_show(struct seq_file *m,
  198. struct cache_detail *cd,
  199. struct cache_head *h)
  200. {
  201. struct ip_map *im;
  202. struct in_addr addr;
  203. char *dom = "-no-domain-";
  204. if (h == NULL) {
  205. seq_puts(m, "#class IP domain\n");
  206. return 0;
  207. }
  208. im = container_of(h, struct ip_map, h);
  209. /* class addr domain */
  210. addr = im->m_addr;
  211. if (test_bit(CACHE_VALID, &h->flags) &&
  212. !test_bit(CACHE_NEGATIVE, &h->flags))
  213. dom = im->m_client->h.name;
  214. seq_printf(m, "%s %d.%d.%d.%d %s\n",
  215. im->m_class,
  216. ntohl(addr.s_addr) >> 24 & 0xff,
  217. ntohl(addr.s_addr) >> 16 & 0xff,
  218. ntohl(addr.s_addr) >> 8 & 0xff,
  219. ntohl(addr.s_addr) >> 0 & 0xff,
  220. dom
  221. );
  222. return 0;
  223. }
  224. struct cache_detail ip_map_cache = {
  225. .owner = THIS_MODULE,
  226. .hash_size = IP_HASHMAX,
  227. .hash_table = ip_table,
  228. .name = "auth.unix.ip",
  229. .cache_put = ip_map_put,
  230. .cache_request = ip_map_request,
  231. .cache_parse = ip_map_parse,
  232. .cache_show = ip_map_show,
  233. .match = ip_map_match,
  234. .init = ip_map_init,
  235. .update = update,
  236. .alloc = ip_map_alloc,
  237. };
  238. static struct ip_map *ip_map_lookup(char *class, struct in_addr addr)
  239. {
  240. struct ip_map ip;
  241. struct cache_head *ch;
  242. strcpy(ip.m_class, class);
  243. ip.m_addr = addr;
  244. ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
  245. hash_str(class, IP_HASHBITS) ^
  246. hash_ip(addr.s_addr));
  247. if (ch)
  248. return container_of(ch, struct ip_map, h);
  249. else
  250. return NULL;
  251. }
  252. static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
  253. {
  254. struct ip_map ip;
  255. struct cache_head *ch;
  256. ip.m_client = udom;
  257. ip.h.flags = 0;
  258. if (!udom)
  259. set_bit(CACHE_NEGATIVE, &ip.h.flags);
  260. else {
  261. ip.m_add_change = udom->addr_changes;
  262. /* if this is from the legacy set_client system call,
  263. * we need m_add_change to be one higher
  264. */
  265. if (expiry == NEVER)
  266. ip.m_add_change++;
  267. }
  268. ip.h.expiry_time = expiry;
  269. ch = sunrpc_cache_update(&ip_map_cache,
  270. &ip.h, &ipm->h,
  271. hash_str(ipm->m_class, IP_HASHBITS) ^
  272. hash_ip(ipm->m_addr.s_addr));
  273. if (!ch)
  274. return -ENOMEM;
  275. cache_put(ch, &ip_map_cache);
  276. return 0;
  277. }
  278. int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
  279. {
  280. struct unix_domain *udom;
  281. struct ip_map *ipmp;
  282. if (dom->flavour != &svcauth_unix)
  283. return -EINVAL;
  284. udom = container_of(dom, struct unix_domain, h);
  285. ipmp = ip_map_lookup("nfsd", addr);
  286. if (ipmp)
  287. return ip_map_update(ipmp, udom, NEVER);
  288. else
  289. return -ENOMEM;
  290. }
  291. int auth_unix_forget_old(struct auth_domain *dom)
  292. {
  293. struct unix_domain *udom;
  294. if (dom->flavour != &svcauth_unix)
  295. return -EINVAL;
  296. udom = container_of(dom, struct unix_domain, h);
  297. udom->addr_changes++;
  298. return 0;
  299. }
  300. struct auth_domain *auth_unix_lookup(struct in_addr addr)
  301. {
  302. struct ip_map *ipm;
  303. struct auth_domain *rv;
  304. ipm = ip_map_lookup("nfsd", addr);
  305. if (!ipm)
  306. return NULL;
  307. if (cache_check(&ip_map_cache, &ipm->h, NULL))
  308. return NULL;
  309. if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
  310. if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
  311. auth_domain_put(&ipm->m_client->h);
  312. rv = NULL;
  313. } else {
  314. rv = &ipm->m_client->h;
  315. kref_get(&rv->ref);
  316. }
  317. cache_put(&ipm->h, &ip_map_cache);
  318. return rv;
  319. }
  320. void svcauth_unix_purge(void)
  321. {
  322. cache_purge(&ip_map_cache);
  323. }
  324. static inline struct ip_map *
  325. ip_map_cached_get(struct svc_rqst *rqstp)
  326. {
  327. struct ip_map *ipm = rqstp->rq_sock->sk_info_authunix;
  328. if (ipm != NULL) {
  329. if (!cache_valid(&ipm->h)) {
  330. /*
  331. * The entry has been invalidated since it was
  332. * remembered, e.g. by a second mount from the
  333. * same IP address.
  334. */
  335. rqstp->rq_sock->sk_info_authunix = NULL;
  336. cache_put(&ipm->h, &ip_map_cache);
  337. return NULL;
  338. }
  339. cache_get(&ipm->h);
  340. }
  341. return ipm;
  342. }
  343. static inline void
  344. ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
  345. {
  346. struct svc_sock *svsk = rqstp->rq_sock;
  347. if (svsk->sk_sock->type == SOCK_STREAM && svsk->sk_info_authunix == NULL)
  348. svsk->sk_info_authunix = ipm; /* newly cached, keep the reference */
  349. else
  350. cache_put(&ipm->h, &ip_map_cache);
  351. }
  352. void
  353. svcauth_unix_info_release(void *info)
  354. {
  355. struct ip_map *ipm = info;
  356. cache_put(&ipm->h, &ip_map_cache);
  357. }
  358. static int
  359. svcauth_unix_set_client(struct svc_rqst *rqstp)
  360. {
  361. struct sockaddr_in *sin = svc_addr_in(rqstp);
  362. struct ip_map *ipm;
  363. rqstp->rq_client = NULL;
  364. if (rqstp->rq_proc == 0)
  365. return SVC_OK;
  366. ipm = ip_map_cached_get(rqstp);
  367. if (ipm == NULL)
  368. ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
  369. sin->sin_addr);
  370. if (ipm == NULL)
  371. return SVC_DENIED;
  372. switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  373. default:
  374. BUG();
  375. case -EAGAIN:
  376. case -ETIMEDOUT:
  377. return SVC_DROP;
  378. case -ENOENT:
  379. return SVC_DENIED;
  380. case 0:
  381. rqstp->rq_client = &ipm->m_client->h;
  382. kref_get(&rqstp->rq_client->ref);
  383. ip_map_cached_put(rqstp, ipm);
  384. break;
  385. }
  386. return SVC_OK;
  387. }
  388. static int
  389. svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
  390. {
  391. struct kvec *argv = &rqstp->rq_arg.head[0];
  392. struct kvec *resv = &rqstp->rq_res.head[0];
  393. struct svc_cred *cred = &rqstp->rq_cred;
  394. cred->cr_group_info = NULL;
  395. rqstp->rq_client = NULL;
  396. if (argv->iov_len < 3*4)
  397. return SVC_GARBAGE;
  398. if (svc_getu32(argv) != 0) {
  399. dprintk("svc: bad null cred\n");
  400. *authp = rpc_autherr_badcred;
  401. return SVC_DENIED;
  402. }
  403. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  404. dprintk("svc: bad null verf\n");
  405. *authp = rpc_autherr_badverf;
  406. return SVC_DENIED;
  407. }
  408. /* Signal that mapping to nobody uid/gid is required */
  409. cred->cr_uid = (uid_t) -1;
  410. cred->cr_gid = (gid_t) -1;
  411. cred->cr_group_info = groups_alloc(0);
  412. if (cred->cr_group_info == NULL)
  413. return SVC_DROP; /* kmalloc failure - client must retry */
  414. /* Put NULL verifier */
  415. svc_putnl(resv, RPC_AUTH_NULL);
  416. svc_putnl(resv, 0);
  417. return SVC_OK;
  418. }
  419. static int
  420. svcauth_null_release(struct svc_rqst *rqstp)
  421. {
  422. if (rqstp->rq_client)
  423. auth_domain_put(rqstp->rq_client);
  424. rqstp->rq_client = NULL;
  425. if (rqstp->rq_cred.cr_group_info)
  426. put_group_info(rqstp->rq_cred.cr_group_info);
  427. rqstp->rq_cred.cr_group_info = NULL;
  428. return 0; /* don't drop */
  429. }
  430. struct auth_ops svcauth_null = {
  431. .name = "null",
  432. .owner = THIS_MODULE,
  433. .flavour = RPC_AUTH_NULL,
  434. .accept = svcauth_null_accept,
  435. .release = svcauth_null_release,
  436. .set_client = svcauth_unix_set_client,
  437. };
  438. static int
  439. svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
  440. {
  441. struct kvec *argv = &rqstp->rq_arg.head[0];
  442. struct kvec *resv = &rqstp->rq_res.head[0];
  443. struct svc_cred *cred = &rqstp->rq_cred;
  444. u32 slen, i;
  445. int len = argv->iov_len;
  446. cred->cr_group_info = NULL;
  447. rqstp->rq_client = NULL;
  448. if ((len -= 3*4) < 0)
  449. return SVC_GARBAGE;
  450. svc_getu32(argv); /* length */
  451. svc_getu32(argv); /* time stamp */
  452. slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
  453. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  454. goto badcred;
  455. argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
  456. argv->iov_len -= slen*4;
  457. cred->cr_uid = svc_getnl(argv); /* uid */
  458. cred->cr_gid = svc_getnl(argv); /* gid */
  459. slen = svc_getnl(argv); /* gids length */
  460. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  461. goto badcred;
  462. cred->cr_group_info = groups_alloc(slen);
  463. if (cred->cr_group_info == NULL)
  464. return SVC_DROP;
  465. for (i = 0; i < slen; i++)
  466. GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
  467. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  468. *authp = rpc_autherr_badverf;
  469. return SVC_DENIED;
  470. }
  471. /* Put NULL verifier */
  472. svc_putnl(resv, RPC_AUTH_NULL);
  473. svc_putnl(resv, 0);
  474. return SVC_OK;
  475. badcred:
  476. *authp = rpc_autherr_badcred;
  477. return SVC_DENIED;
  478. }
  479. static int
  480. svcauth_unix_release(struct svc_rqst *rqstp)
  481. {
  482. /* Verifier (such as it is) is already in place.
  483. */
  484. if (rqstp->rq_client)
  485. auth_domain_put(rqstp->rq_client);
  486. rqstp->rq_client = NULL;
  487. if (rqstp->rq_cred.cr_group_info)
  488. put_group_info(rqstp->rq_cred.cr_group_info);
  489. rqstp->rq_cred.cr_group_info = NULL;
  490. return 0;
  491. }
  492. struct auth_ops svcauth_unix = {
  493. .name = "unix",
  494. .owner = THIS_MODULE,
  495. .flavour = RPC_AUTH_UNIX,
  496. .accept = svcauth_unix_accept,
  497. .release = svcauth_unix_release,
  498. .domain_release = svcauth_unix_domain_release,
  499. .set_client = svcauth_unix_set_client,
  500. };