svcauth_gss.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * Neil Brown <neilb@cse.unsw.edu.au>
  3. * J. Bruce Fields <bfields@umich.edu>
  4. * Andy Adamson <andros@umich.edu>
  5. * Dug Song <dugsong@monkey.org>
  6. *
  7. * RPCSEC_GSS server authentication.
  8. * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
  9. * (gssapi)
  10. *
  11. * The RPCSEC_GSS involves three stages:
  12. * 1/ context creation
  13. * 2/ data exchange
  14. * 3/ context destruction
  15. *
  16. * Context creation is handled largely by upcalls to user-space.
  17. * In particular, GSS_Accept_sec_context is handled by an upcall
  18. * Data exchange is handled entirely within the kernel
  19. * In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
  20. * Context destruction is handled in-kernel
  21. * GSS_Delete_sec_context is in-kernel
  22. *
  23. * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
  24. * The context handle and gss_token are used as a key into the rpcsec_init cache.
  25. * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
  26. * being major_status, minor_status, context_handle, reply_token.
  27. * These are sent back to the client.
  28. * Sequence window management is handled by the kernel. The window size if currently
  29. * a compile time constant.
  30. *
  31. * When user-space is happy that a context is established, it places an entry
  32. * in the rpcsec_context cache. The key for this cache is the context_handle.
  33. * The content includes:
  34. * uid/gidlist - for determining access rights
  35. * mechanism type
  36. * mechanism specific information, such as a key
  37. *
  38. */
  39. #include <linux/types.h>
  40. #include <linux/module.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/sunrpc/auth_gss.h>
  43. #include <linux/sunrpc/svcauth.h>
  44. #include <linux/sunrpc/gss_err.h>
  45. #include <linux/sunrpc/svcauth.h>
  46. #include <linux/sunrpc/svcauth_gss.h>
  47. #include <linux/sunrpc/cache.h>
  48. #ifdef RPC_DEBUG
  49. # define RPCDBG_FACILITY RPCDBG_AUTH
  50. #endif
  51. /* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
  52. * into replies.
  53. *
  54. * Key is context handle (\x if empty) and gss_token.
  55. * Content is major_status minor_status (integers) context_handle, reply_token.
  56. *
  57. */
  58. static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
  59. {
  60. return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
  61. }
  62. #define RSI_HASHBITS 6
  63. #define RSI_HASHMAX (1<<RSI_HASHBITS)
  64. #define RSI_HASHMASK (RSI_HASHMAX-1)
  65. struct rsi {
  66. struct cache_head h;
  67. struct xdr_netobj in_handle, in_token;
  68. struct xdr_netobj out_handle, out_token;
  69. int major_status, minor_status;
  70. };
  71. static struct cache_head *rsi_table[RSI_HASHMAX];
  72. static struct cache_detail rsi_cache;
  73. static struct rsi *rsi_lookup(struct rsi *item, int set);
  74. static void rsi_free(struct rsi *rsii)
  75. {
  76. kfree(rsii->in_handle.data);
  77. kfree(rsii->in_token.data);
  78. kfree(rsii->out_handle.data);
  79. kfree(rsii->out_token.data);
  80. }
  81. static void rsi_put(struct cache_head *item, struct cache_detail *cd)
  82. {
  83. struct rsi *rsii = container_of(item, struct rsi, h);
  84. if (cache_put(item, cd)) {
  85. rsi_free(rsii);
  86. kfree(rsii);
  87. }
  88. }
  89. static inline int rsi_hash(struct rsi *item)
  90. {
  91. return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
  92. ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
  93. }
  94. static inline int rsi_match(struct rsi *item, struct rsi *tmp)
  95. {
  96. return netobj_equal(&item->in_handle, &tmp->in_handle)
  97. && netobj_equal(&item->in_token, &tmp->in_token);
  98. }
  99. static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
  100. {
  101. dst->len = len;
  102. dst->data = (len ? kmalloc(len, GFP_KERNEL) : NULL);
  103. if (dst->data)
  104. memcpy(dst->data, src, len);
  105. if (len && !dst->data)
  106. return -ENOMEM;
  107. return 0;
  108. }
  109. static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
  110. {
  111. return dup_to_netobj(dst, src->data, src->len);
  112. }
  113. static inline void rsi_init(struct rsi *new, struct rsi *item)
  114. {
  115. new->out_handle.data = NULL;
  116. new->out_handle.len = 0;
  117. new->out_token.data = NULL;
  118. new->out_token.len = 0;
  119. new->in_handle.len = item->in_handle.len;
  120. item->in_handle.len = 0;
  121. new->in_token.len = item->in_token.len;
  122. item->in_token.len = 0;
  123. new->in_handle.data = item->in_handle.data;
  124. item->in_handle.data = NULL;
  125. new->in_token.data = item->in_token.data;
  126. item->in_token.data = NULL;
  127. }
  128. static inline void rsi_update(struct rsi *new, struct rsi *item)
  129. {
  130. BUG_ON(new->out_handle.data || new->out_token.data);
  131. new->out_handle.len = item->out_handle.len;
  132. item->out_handle.len = 0;
  133. new->out_token.len = item->out_token.len;
  134. item->out_token.len = 0;
  135. new->out_handle.data = item->out_handle.data;
  136. item->out_handle.data = NULL;
  137. new->out_token.data = item->out_token.data;
  138. item->out_token.data = NULL;
  139. new->major_status = item->major_status;
  140. new->minor_status = item->minor_status;
  141. }
  142. static void rsi_request(struct cache_detail *cd,
  143. struct cache_head *h,
  144. char **bpp, int *blen)
  145. {
  146. struct rsi *rsii = container_of(h, struct rsi, h);
  147. qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
  148. qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
  149. (*bpp)[-1] = '\n';
  150. }
  151. static int rsi_parse(struct cache_detail *cd,
  152. char *mesg, int mlen)
  153. {
  154. /* context token expiry major minor context token */
  155. char *buf = mesg;
  156. char *ep;
  157. int len;
  158. struct rsi rsii, *rsip = NULL;
  159. time_t expiry;
  160. int status = -EINVAL;
  161. memset(&rsii, 0, sizeof(rsii));
  162. /* handle */
  163. len = qword_get(&mesg, buf, mlen);
  164. if (len < 0)
  165. goto out;
  166. status = -ENOMEM;
  167. if (dup_to_netobj(&rsii.in_handle, buf, len))
  168. goto out;
  169. /* token */
  170. len = qword_get(&mesg, buf, mlen);
  171. status = -EINVAL;
  172. if (len < 0)
  173. goto out;
  174. status = -ENOMEM;
  175. if (dup_to_netobj(&rsii.in_token, buf, len))
  176. goto out;
  177. rsii.h.flags = 0;
  178. /* expiry */
  179. expiry = get_expiry(&mesg);
  180. status = -EINVAL;
  181. if (expiry == 0)
  182. goto out;
  183. /* major/minor */
  184. len = qword_get(&mesg, buf, mlen);
  185. if (len < 0)
  186. goto out;
  187. if (len == 0) {
  188. goto out;
  189. } else {
  190. rsii.major_status = simple_strtoul(buf, &ep, 10);
  191. if (*ep)
  192. goto out;
  193. len = qword_get(&mesg, buf, mlen);
  194. if (len <= 0)
  195. goto out;
  196. rsii.minor_status = simple_strtoul(buf, &ep, 10);
  197. if (*ep)
  198. goto out;
  199. /* out_handle */
  200. len = qword_get(&mesg, buf, mlen);
  201. if (len < 0)
  202. goto out;
  203. status = -ENOMEM;
  204. if (dup_to_netobj(&rsii.out_handle, buf, len))
  205. goto out;
  206. /* out_token */
  207. len = qword_get(&mesg, buf, mlen);
  208. status = -EINVAL;
  209. if (len < 0)
  210. goto out;
  211. status = -ENOMEM;
  212. if (dup_to_netobj(&rsii.out_token, buf, len))
  213. goto out;
  214. }
  215. rsii.h.expiry_time = expiry;
  216. rsip = rsi_lookup(&rsii, 1);
  217. status = 0;
  218. out:
  219. rsi_free(&rsii);
  220. if (rsip)
  221. rsi_put(&rsip->h, &rsi_cache);
  222. return status;
  223. }
  224. static struct cache_detail rsi_cache = {
  225. .owner = THIS_MODULE,
  226. .hash_size = RSI_HASHMAX,
  227. .hash_table = rsi_table,
  228. .name = "auth.rpcsec.init",
  229. .cache_put = rsi_put,
  230. .cache_request = rsi_request,
  231. .cache_parse = rsi_parse,
  232. };
  233. static DefineSimpleCacheLookup(rsi, 0)
  234. /*
  235. * The rpcsec_context cache is used to store a context that is
  236. * used in data exchange.
  237. * The key is a context handle. The content is:
  238. * uid, gidlist, mechanism, service-set, mech-specific-data
  239. */
  240. #define RSC_HASHBITS 10
  241. #define RSC_HASHMAX (1<<RSC_HASHBITS)
  242. #define RSC_HASHMASK (RSC_HASHMAX-1)
  243. #define GSS_SEQ_WIN 128
  244. struct gss_svc_seq_data {
  245. /* highest seq number seen so far: */
  246. int sd_max;
  247. /* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
  248. * sd_win is nonzero iff sequence number i has been seen already: */
  249. unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
  250. spinlock_t sd_lock;
  251. };
  252. struct rsc {
  253. struct cache_head h;
  254. struct xdr_netobj handle;
  255. struct svc_cred cred;
  256. struct gss_svc_seq_data seqdata;
  257. struct gss_ctx *mechctx;
  258. };
  259. static struct cache_head *rsc_table[RSC_HASHMAX];
  260. static struct cache_detail rsc_cache;
  261. static struct rsc *rsc_lookup(struct rsc *item, int set);
  262. static void rsc_free(struct rsc *rsci)
  263. {
  264. kfree(rsci->handle.data);
  265. if (rsci->mechctx)
  266. gss_delete_sec_context(&rsci->mechctx);
  267. if (rsci->cred.cr_group_info)
  268. put_group_info(rsci->cred.cr_group_info);
  269. }
  270. static void rsc_put(struct cache_head *item, struct cache_detail *cd)
  271. {
  272. struct rsc *rsci = container_of(item, struct rsc, h);
  273. if (cache_put(item, cd)) {
  274. rsc_free(rsci);
  275. kfree(rsci);
  276. }
  277. }
  278. static inline int
  279. rsc_hash(struct rsc *rsci)
  280. {
  281. return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
  282. }
  283. static inline int
  284. rsc_match(struct rsc *new, struct rsc *tmp)
  285. {
  286. return netobj_equal(&new->handle, &tmp->handle);
  287. }
  288. static inline void
  289. rsc_init(struct rsc *new, struct rsc *tmp)
  290. {
  291. new->handle.len = tmp->handle.len;
  292. tmp->handle.len = 0;
  293. new->handle.data = tmp->handle.data;
  294. tmp->handle.data = NULL;
  295. new->mechctx = NULL;
  296. new->cred.cr_group_info = NULL;
  297. }
  298. static inline void
  299. rsc_update(struct rsc *new, struct rsc *tmp)
  300. {
  301. new->mechctx = tmp->mechctx;
  302. tmp->mechctx = NULL;
  303. memset(&new->seqdata, 0, sizeof(new->seqdata));
  304. spin_lock_init(&new->seqdata.sd_lock);
  305. new->cred = tmp->cred;
  306. tmp->cred.cr_group_info = NULL;
  307. }
  308. static int rsc_parse(struct cache_detail *cd,
  309. char *mesg, int mlen)
  310. {
  311. /* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
  312. char *buf = mesg;
  313. int len, rv;
  314. struct rsc rsci, *rscp = NULL;
  315. time_t expiry;
  316. int status = -EINVAL;
  317. memset(&rsci, 0, sizeof(rsci));
  318. /* context handle */
  319. len = qword_get(&mesg, buf, mlen);
  320. if (len < 0) goto out;
  321. status = -ENOMEM;
  322. if (dup_to_netobj(&rsci.handle, buf, len))
  323. goto out;
  324. rsci.h.flags = 0;
  325. /* expiry */
  326. expiry = get_expiry(&mesg);
  327. status = -EINVAL;
  328. if (expiry == 0)
  329. goto out;
  330. /* uid, or NEGATIVE */
  331. rv = get_int(&mesg, &rsci.cred.cr_uid);
  332. if (rv == -EINVAL)
  333. goto out;
  334. if (rv == -ENOENT)
  335. set_bit(CACHE_NEGATIVE, &rsci.h.flags);
  336. else {
  337. int N, i;
  338. struct gss_api_mech *gm;
  339. /* gid */
  340. if (get_int(&mesg, &rsci.cred.cr_gid))
  341. goto out;
  342. /* number of additional gid's */
  343. if (get_int(&mesg, &N))
  344. goto out;
  345. status = -ENOMEM;
  346. rsci.cred.cr_group_info = groups_alloc(N);
  347. if (rsci.cred.cr_group_info == NULL)
  348. goto out;
  349. /* gid's */
  350. status = -EINVAL;
  351. for (i=0; i<N; i++) {
  352. gid_t gid;
  353. if (get_int(&mesg, &gid))
  354. goto out;
  355. GROUP_AT(rsci.cred.cr_group_info, i) = gid;
  356. }
  357. /* mech name */
  358. len = qword_get(&mesg, buf, mlen);
  359. if (len < 0)
  360. goto out;
  361. gm = gss_mech_get_by_name(buf);
  362. status = -EOPNOTSUPP;
  363. if (!gm)
  364. goto out;
  365. status = -EINVAL;
  366. /* mech-specific data: */
  367. len = qword_get(&mesg, buf, mlen);
  368. if (len < 0) {
  369. gss_mech_put(gm);
  370. goto out;
  371. }
  372. if (gss_import_sec_context(buf, len, gm, &rsci.mechctx)) {
  373. gss_mech_put(gm);
  374. goto out;
  375. }
  376. gss_mech_put(gm);
  377. }
  378. rsci.h.expiry_time = expiry;
  379. rscp = rsc_lookup(&rsci, 1);
  380. status = 0;
  381. out:
  382. rsc_free(&rsci);
  383. if (rscp)
  384. rsc_put(&rscp->h, &rsc_cache);
  385. return status;
  386. }
  387. static struct cache_detail rsc_cache = {
  388. .owner = THIS_MODULE,
  389. .hash_size = RSC_HASHMAX,
  390. .hash_table = rsc_table,
  391. .name = "auth.rpcsec.context",
  392. .cache_put = rsc_put,
  393. .cache_parse = rsc_parse,
  394. };
  395. static DefineSimpleCacheLookup(rsc, 0);
  396. static struct rsc *
  397. gss_svc_searchbyctx(struct xdr_netobj *handle)
  398. {
  399. struct rsc rsci;
  400. struct rsc *found;
  401. memset(&rsci, 0, sizeof(rsci));
  402. if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
  403. return NULL;
  404. found = rsc_lookup(&rsci, 0);
  405. rsc_free(&rsci);
  406. if (!found)
  407. return NULL;
  408. if (cache_check(&rsc_cache, &found->h, NULL))
  409. return NULL;
  410. return found;
  411. }
  412. /* Implements sequence number algorithm as specified in RFC 2203. */
  413. static int
  414. gss_check_seq_num(struct rsc *rsci, int seq_num)
  415. {
  416. struct gss_svc_seq_data *sd = &rsci->seqdata;
  417. spin_lock(&sd->sd_lock);
  418. if (seq_num > sd->sd_max) {
  419. if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
  420. memset(sd->sd_win,0,sizeof(sd->sd_win));
  421. sd->sd_max = seq_num;
  422. } else while (sd->sd_max < seq_num) {
  423. sd->sd_max++;
  424. __clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
  425. }
  426. __set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
  427. goto ok;
  428. } else if (seq_num <= sd->sd_max - GSS_SEQ_WIN) {
  429. goto drop;
  430. }
  431. /* sd_max - GSS_SEQ_WIN < seq_num <= sd_max */
  432. if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
  433. goto drop;
  434. ok:
  435. spin_unlock(&sd->sd_lock);
  436. return 1;
  437. drop:
  438. spin_unlock(&sd->sd_lock);
  439. return 0;
  440. }
  441. static inline u32 round_up_to_quad(u32 i)
  442. {
  443. return (i + 3 ) & ~3;
  444. }
  445. static inline int
  446. svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
  447. {
  448. int l;
  449. if (argv->iov_len < 4)
  450. return -1;
  451. o->len = ntohl(svc_getu32(argv));
  452. l = round_up_to_quad(o->len);
  453. if (argv->iov_len < l)
  454. return -1;
  455. o->data = argv->iov_base;
  456. argv->iov_base += l;
  457. argv->iov_len -= l;
  458. return 0;
  459. }
  460. static inline int
  461. svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
  462. {
  463. u32 *p;
  464. if (resv->iov_len + 4 > PAGE_SIZE)
  465. return -1;
  466. svc_putu32(resv, htonl(o->len));
  467. p = resv->iov_base + resv->iov_len;
  468. resv->iov_len += round_up_to_quad(o->len);
  469. if (resv->iov_len > PAGE_SIZE)
  470. return -1;
  471. memcpy(p, o->data, o->len);
  472. memset((u8 *)p + o->len, 0, round_up_to_quad(o->len) - o->len);
  473. return 0;
  474. }
  475. /* Verify the checksum on the header and return SVC_OK on success.
  476. * Otherwise, return SVC_DROP (in the case of a bad sequence number)
  477. * or return SVC_DENIED and indicate error in authp.
  478. */
  479. static int
  480. gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
  481. u32 *rpcstart, struct rpc_gss_wire_cred *gc, u32 *authp)
  482. {
  483. struct gss_ctx *ctx_id = rsci->mechctx;
  484. struct xdr_buf rpchdr;
  485. struct xdr_netobj checksum;
  486. u32 flavor = 0;
  487. struct kvec *argv = &rqstp->rq_arg.head[0];
  488. struct kvec iov;
  489. /* data to compute the checksum over: */
  490. iov.iov_base = rpcstart;
  491. iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
  492. xdr_buf_from_iov(&iov, &rpchdr);
  493. *authp = rpc_autherr_badverf;
  494. if (argv->iov_len < 4)
  495. return SVC_DENIED;
  496. flavor = ntohl(svc_getu32(argv));
  497. if (flavor != RPC_AUTH_GSS)
  498. return SVC_DENIED;
  499. if (svc_safe_getnetobj(argv, &checksum))
  500. return SVC_DENIED;
  501. if (rqstp->rq_deferred) /* skip verification of revisited request */
  502. return SVC_OK;
  503. if (gss_verify_mic(ctx_id, &rpchdr, &checksum, NULL)
  504. != GSS_S_COMPLETE) {
  505. *authp = rpcsec_gsserr_credproblem;
  506. return SVC_DENIED;
  507. }
  508. if (gc->gc_seq > MAXSEQ) {
  509. dprintk("RPC: svcauth_gss: discarding request with large sequence number %d\n",
  510. gc->gc_seq);
  511. *authp = rpcsec_gsserr_ctxproblem;
  512. return SVC_DENIED;
  513. }
  514. if (!gss_check_seq_num(rsci, gc->gc_seq)) {
  515. dprintk("RPC: svcauth_gss: discarding request with old sequence number %d\n",
  516. gc->gc_seq);
  517. return SVC_DROP;
  518. }
  519. return SVC_OK;
  520. }
  521. static int
  522. gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
  523. {
  524. u32 xdr_seq;
  525. u32 maj_stat;
  526. struct xdr_buf verf_data;
  527. struct xdr_netobj mic;
  528. u32 *p;
  529. struct kvec iov;
  530. svc_putu32(rqstp->rq_res.head, htonl(RPC_AUTH_GSS));
  531. xdr_seq = htonl(seq);
  532. iov.iov_base = &xdr_seq;
  533. iov.iov_len = sizeof(xdr_seq);
  534. xdr_buf_from_iov(&iov, &verf_data);
  535. p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
  536. mic.data = (u8 *)(p + 1);
  537. maj_stat = gss_get_mic(ctx_id, 0, &verf_data, &mic);
  538. if (maj_stat != GSS_S_COMPLETE)
  539. return -1;
  540. *p++ = htonl(mic.len);
  541. memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
  542. p += XDR_QUADLEN(mic.len);
  543. if (!xdr_ressize_check(rqstp, p))
  544. return -1;
  545. return 0;
  546. }
  547. struct gss_domain {
  548. struct auth_domain h;
  549. u32 pseudoflavor;
  550. };
  551. static struct auth_domain *
  552. find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
  553. {
  554. char *name;
  555. name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
  556. if (!name)
  557. return NULL;
  558. return auth_domain_find(name);
  559. }
  560. int
  561. svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
  562. {
  563. struct gss_domain *new;
  564. struct auth_domain *test;
  565. int stat = -ENOMEM;
  566. new = kmalloc(sizeof(*new), GFP_KERNEL);
  567. if (!new)
  568. goto out;
  569. cache_init(&new->h.h);
  570. new->h.name = kmalloc(strlen(name) + 1, GFP_KERNEL);
  571. if (!new->h.name)
  572. goto out_free_dom;
  573. strcpy(new->h.name, name);
  574. new->h.flavour = RPC_AUTH_GSS;
  575. new->pseudoflavor = pseudoflavor;
  576. new->h.h.expiry_time = NEVER;
  577. test = auth_domain_lookup(&new->h, 1);
  578. if (test == &new->h) {
  579. BUG_ON(atomic_dec_and_test(&new->h.h.refcnt));
  580. } else { /* XXX Duplicate registration? */
  581. auth_domain_put(&new->h);
  582. goto out;
  583. }
  584. return 0;
  585. out_free_dom:
  586. kfree(new);
  587. out:
  588. return stat;
  589. }
  590. EXPORT_SYMBOL(svcauth_gss_register_pseudoflavor);
  591. static inline int
  592. read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
  593. {
  594. u32 raw;
  595. int status;
  596. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  597. if (status)
  598. return status;
  599. *obj = ntohl(raw);
  600. return 0;
  601. }
  602. /* It would be nice if this bit of code could be shared with the client.
  603. * Obstacles:
  604. * The client shouldn't malloc(), would have to pass in own memory.
  605. * The server uses base of head iovec as read pointer, while the
  606. * client uses separate pointer. */
  607. static int
  608. unwrap_integ_data(struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  609. {
  610. int stat = -EINVAL;
  611. u32 integ_len, maj_stat;
  612. struct xdr_netobj mic;
  613. struct xdr_buf integ_buf;
  614. integ_len = ntohl(svc_getu32(&buf->head[0]));
  615. if (integ_len & 3)
  616. goto out;
  617. if (integ_len > buf->len)
  618. goto out;
  619. if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
  620. BUG();
  621. /* copy out mic... */
  622. if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
  623. BUG();
  624. if (mic.len > RPC_MAX_AUTH_SIZE)
  625. goto out;
  626. mic.data = kmalloc(mic.len, GFP_KERNEL);
  627. if (!mic.data)
  628. goto out;
  629. if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
  630. goto out;
  631. maj_stat = gss_verify_mic(ctx, &integ_buf, &mic, NULL);
  632. if (maj_stat != GSS_S_COMPLETE)
  633. goto out;
  634. if (ntohl(svc_getu32(&buf->head[0])) != seq)
  635. goto out;
  636. stat = 0;
  637. out:
  638. return stat;
  639. }
  640. struct gss_svc_data {
  641. /* decoded gss client cred: */
  642. struct rpc_gss_wire_cred clcred;
  643. /* pointer to the beginning of the procedure-specific results,
  644. * which may be encrypted/checksummed in svcauth_gss_release: */
  645. u32 *body_start;
  646. struct rsc *rsci;
  647. };
  648. static int
  649. svcauth_gss_set_client(struct svc_rqst *rqstp)
  650. {
  651. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  652. struct rsc *rsci = svcdata->rsci;
  653. struct rpc_gss_wire_cred *gc = &svcdata->clcred;
  654. rqstp->rq_client = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
  655. if (rqstp->rq_client == NULL)
  656. return SVC_DENIED;
  657. return SVC_OK;
  658. }
  659. /*
  660. * Accept an rpcsec packet.
  661. * If context establishment, punt to user space
  662. * If data exchange, verify/decrypt
  663. * If context destruction, handle here
  664. * In the context establishment and destruction case we encode
  665. * response here and return SVC_COMPLETE.
  666. */
  667. static int
  668. svcauth_gss_accept(struct svc_rqst *rqstp, u32 *authp)
  669. {
  670. struct kvec *argv = &rqstp->rq_arg.head[0];
  671. struct kvec *resv = &rqstp->rq_res.head[0];
  672. u32 crlen;
  673. struct xdr_netobj tmpobj;
  674. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  675. struct rpc_gss_wire_cred *gc;
  676. struct rsc *rsci = NULL;
  677. struct rsi *rsip, rsikey;
  678. u32 *rpcstart;
  679. u32 *reject_stat = resv->iov_base + resv->iov_len;
  680. int ret;
  681. dprintk("RPC: svcauth_gss: argv->iov_len = %zd\n",argv->iov_len);
  682. *authp = rpc_autherr_badcred;
  683. if (!svcdata)
  684. svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
  685. if (!svcdata)
  686. goto auth_err;
  687. rqstp->rq_auth_data = svcdata;
  688. svcdata->body_start = NULL;
  689. svcdata->rsci = NULL;
  690. gc = &svcdata->clcred;
  691. /* start of rpc packet is 7 u32's back from here:
  692. * xid direction rpcversion prog vers proc flavour
  693. */
  694. rpcstart = argv->iov_base;
  695. rpcstart -= 7;
  696. /* credential is:
  697. * version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
  698. * at least 5 u32s, and is preceeded by length, so that makes 6.
  699. */
  700. if (argv->iov_len < 5 * 4)
  701. goto auth_err;
  702. crlen = ntohl(svc_getu32(argv));
  703. if (ntohl(svc_getu32(argv)) != RPC_GSS_VERSION)
  704. goto auth_err;
  705. gc->gc_proc = ntohl(svc_getu32(argv));
  706. gc->gc_seq = ntohl(svc_getu32(argv));
  707. gc->gc_svc = ntohl(svc_getu32(argv));
  708. if (svc_safe_getnetobj(argv, &gc->gc_ctx))
  709. goto auth_err;
  710. if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
  711. goto auth_err;
  712. if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
  713. goto auth_err;
  714. /*
  715. * We've successfully parsed the credential. Let's check out the
  716. * verifier. An AUTH_NULL verifier is allowed (and required) for
  717. * INIT and CONTINUE_INIT requests. AUTH_RPCSEC_GSS is required for
  718. * PROC_DATA and PROC_DESTROY.
  719. *
  720. * AUTH_NULL verifier is 0 (AUTH_NULL), 0 (length).
  721. * AUTH_RPCSEC_GSS verifier is:
  722. * 6 (AUTH_RPCSEC_GSS), length, checksum.
  723. * checksum is calculated over rpcheader from xid up to here.
  724. */
  725. *authp = rpc_autherr_badverf;
  726. switch (gc->gc_proc) {
  727. case RPC_GSS_PROC_INIT:
  728. case RPC_GSS_PROC_CONTINUE_INIT:
  729. if (argv->iov_len < 2 * 4)
  730. goto auth_err;
  731. if (ntohl(svc_getu32(argv)) != RPC_AUTH_NULL)
  732. goto auth_err;
  733. if (ntohl(svc_getu32(argv)) != 0)
  734. goto auth_err;
  735. break;
  736. case RPC_GSS_PROC_DATA:
  737. case RPC_GSS_PROC_DESTROY:
  738. *authp = rpcsec_gsserr_credproblem;
  739. rsci = gss_svc_searchbyctx(&gc->gc_ctx);
  740. if (!rsci)
  741. goto auth_err;
  742. switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
  743. case SVC_OK:
  744. break;
  745. case SVC_DENIED:
  746. goto auth_err;
  747. case SVC_DROP:
  748. goto drop;
  749. }
  750. break;
  751. default:
  752. *authp = rpc_autherr_rejectedcred;
  753. goto auth_err;
  754. }
  755. /* now act upon the command: */
  756. switch (gc->gc_proc) {
  757. case RPC_GSS_PROC_INIT:
  758. case RPC_GSS_PROC_CONTINUE_INIT:
  759. *authp = rpc_autherr_badcred;
  760. if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
  761. goto auth_err;
  762. memset(&rsikey, 0, sizeof(rsikey));
  763. if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))
  764. goto drop;
  765. *authp = rpc_autherr_badverf;
  766. if (svc_safe_getnetobj(argv, &tmpobj)) {
  767. kfree(rsikey.in_handle.data);
  768. goto auth_err;
  769. }
  770. if (dup_netobj(&rsikey.in_token, &tmpobj)) {
  771. kfree(rsikey.in_handle.data);
  772. goto drop;
  773. }
  774. rsip = rsi_lookup(&rsikey, 0);
  775. rsi_free(&rsikey);
  776. if (!rsip) {
  777. goto drop;
  778. }
  779. switch(cache_check(&rsi_cache, &rsip->h, &rqstp->rq_chandle)) {
  780. case -EAGAIN:
  781. goto drop;
  782. case -ENOENT:
  783. goto drop;
  784. case 0:
  785. rsci = gss_svc_searchbyctx(&rsip->out_handle);
  786. if (!rsci) {
  787. goto drop;
  788. }
  789. if (gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN))
  790. goto drop;
  791. if (resv->iov_len + 4 > PAGE_SIZE)
  792. goto drop;
  793. svc_putu32(resv, rpc_success);
  794. if (svc_safe_putnetobj(resv, &rsip->out_handle))
  795. goto drop;
  796. if (resv->iov_len + 3 * 4 > PAGE_SIZE)
  797. goto drop;
  798. svc_putu32(resv, htonl(rsip->major_status));
  799. svc_putu32(resv, htonl(rsip->minor_status));
  800. svc_putu32(resv, htonl(GSS_SEQ_WIN));
  801. if (svc_safe_putnetobj(resv, &rsip->out_token))
  802. goto drop;
  803. rqstp->rq_client = NULL;
  804. }
  805. goto complete;
  806. case RPC_GSS_PROC_DESTROY:
  807. set_bit(CACHE_NEGATIVE, &rsci->h.flags);
  808. if (resv->iov_len + 4 > PAGE_SIZE)
  809. goto drop;
  810. svc_putu32(resv, rpc_success);
  811. goto complete;
  812. case RPC_GSS_PROC_DATA:
  813. *authp = rpcsec_gsserr_ctxproblem;
  814. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  815. goto auth_err;
  816. rqstp->rq_cred = rsci->cred;
  817. get_group_info(rsci->cred.cr_group_info);
  818. *authp = rpc_autherr_badcred;
  819. switch (gc->gc_svc) {
  820. case RPC_GSS_SVC_NONE:
  821. break;
  822. case RPC_GSS_SVC_INTEGRITY:
  823. if (unwrap_integ_data(&rqstp->rq_arg,
  824. gc->gc_seq, rsci->mechctx))
  825. goto auth_err;
  826. /* placeholders for length and seq. number: */
  827. svcdata->body_start = resv->iov_base + resv->iov_len;
  828. svc_putu32(resv, 0);
  829. svc_putu32(resv, 0);
  830. break;
  831. case RPC_GSS_SVC_PRIVACY:
  832. /* currently unsupported */
  833. default:
  834. goto auth_err;
  835. }
  836. svcdata->rsci = rsci;
  837. cache_get(&rsci->h);
  838. ret = SVC_OK;
  839. goto out;
  840. }
  841. auth_err:
  842. /* Restore write pointer to original value: */
  843. xdr_ressize_check(rqstp, reject_stat);
  844. ret = SVC_DENIED;
  845. goto out;
  846. complete:
  847. ret = SVC_COMPLETE;
  848. goto out;
  849. drop:
  850. ret = SVC_DROP;
  851. out:
  852. if (rsci)
  853. rsc_put(&rsci->h, &rsc_cache);
  854. return ret;
  855. }
  856. static int
  857. svcauth_gss_release(struct svc_rqst *rqstp)
  858. {
  859. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  860. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  861. struct xdr_buf *resbuf = &rqstp->rq_res;
  862. struct xdr_buf integ_buf;
  863. struct xdr_netobj mic;
  864. struct kvec *resv;
  865. u32 *p;
  866. int integ_offset, integ_len;
  867. int stat = -EINVAL;
  868. if (gc->gc_proc != RPC_GSS_PROC_DATA)
  869. goto out;
  870. /* Release can be called twice, but we only wrap once. */
  871. if (gsd->body_start == NULL)
  872. goto out;
  873. /* normally not set till svc_send, but we need it here: */
  874. resbuf->len = resbuf->head[0].iov_len
  875. + resbuf->page_len + resbuf->tail[0].iov_len;
  876. switch (gc->gc_svc) {
  877. case RPC_GSS_SVC_NONE:
  878. break;
  879. case RPC_GSS_SVC_INTEGRITY:
  880. p = gsd->body_start;
  881. gsd->body_start = NULL;
  882. /* move accept_stat to right place: */
  883. memcpy(p, p + 2, 4);
  884. /* don't wrap in failure case: */
  885. /* Note: counting on not getting here if call was not even
  886. * accepted! */
  887. if (*p != rpc_success) {
  888. resbuf->head[0].iov_len -= 2 * 4;
  889. goto out;
  890. }
  891. p++;
  892. integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
  893. integ_len = resbuf->len - integ_offset;
  894. BUG_ON(integ_len % 4);
  895. *p++ = htonl(integ_len);
  896. *p++ = htonl(gc->gc_seq);
  897. if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset,
  898. integ_len))
  899. BUG();
  900. if (resbuf->page_len == 0
  901. && resbuf->tail[0].iov_len + RPC_MAX_AUTH_SIZE
  902. < PAGE_SIZE) {
  903. BUG_ON(resbuf->tail[0].iov_len);
  904. /* Use head for everything */
  905. resv = &resbuf->head[0];
  906. } else if (resbuf->tail[0].iov_base == NULL) {
  907. /* copied from nfsd4_encode_read */
  908. svc_take_page(rqstp);
  909. resbuf->tail[0].iov_base = page_address(rqstp
  910. ->rq_respages[rqstp->rq_resused-1]);
  911. rqstp->rq_restailpage = rqstp->rq_resused-1;
  912. resbuf->tail[0].iov_len = 0;
  913. resv = &resbuf->tail[0];
  914. } else {
  915. resv = &resbuf->tail[0];
  916. }
  917. mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
  918. if (gss_get_mic(gsd->rsci->mechctx, 0, &integ_buf, &mic))
  919. goto out_err;
  920. svc_putu32(resv, htonl(mic.len));
  921. memset(mic.data + mic.len, 0,
  922. round_up_to_quad(mic.len) - mic.len);
  923. resv->iov_len += XDR_QUADLEN(mic.len) << 2;
  924. /* not strictly required: */
  925. resbuf->len += XDR_QUADLEN(mic.len) << 2;
  926. BUG_ON(resv->iov_len > PAGE_SIZE);
  927. break;
  928. case RPC_GSS_SVC_PRIVACY:
  929. default:
  930. goto out_err;
  931. }
  932. out:
  933. stat = 0;
  934. out_err:
  935. if (rqstp->rq_client)
  936. auth_domain_put(rqstp->rq_client);
  937. rqstp->rq_client = NULL;
  938. if (rqstp->rq_cred.cr_group_info)
  939. put_group_info(rqstp->rq_cred.cr_group_info);
  940. rqstp->rq_cred.cr_group_info = NULL;
  941. if (gsd->rsci)
  942. rsc_put(&gsd->rsci->h, &rsc_cache);
  943. gsd->rsci = NULL;
  944. return stat;
  945. }
  946. static void
  947. svcauth_gss_domain_release(struct auth_domain *dom)
  948. {
  949. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  950. kfree(dom->name);
  951. kfree(gd);
  952. }
  953. static struct auth_ops svcauthops_gss = {
  954. .name = "rpcsec_gss",
  955. .owner = THIS_MODULE,
  956. .flavour = RPC_AUTH_GSS,
  957. .accept = svcauth_gss_accept,
  958. .release = svcauth_gss_release,
  959. .domain_release = svcauth_gss_domain_release,
  960. .set_client = svcauth_gss_set_client,
  961. };
  962. int
  963. gss_svc_init(void)
  964. {
  965. int rv = svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
  966. if (rv == 0) {
  967. cache_register(&rsc_cache);
  968. cache_register(&rsi_cache);
  969. }
  970. return rv;
  971. }
  972. void
  973. gss_svc_shutdown(void)
  974. {
  975. if (cache_unregister(&rsc_cache))
  976. printk(KERN_ERR "auth_rpcgss: failed to unregister rsc cache\n");
  977. if (cache_unregister(&rsi_cache))
  978. printk(KERN_ERR "auth_rpcgss: failed to unregister rsi cache\n");
  979. svc_auth_unregister(RPC_AUTH_GSS);
  980. }