svcauth_unix.c 13 KB

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