svcauth_gss.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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/gss_err.h>
  44. #include <linux/sunrpc/svcauth.h>
  45. #include <linux/sunrpc/svcauth_gss.h>
  46. #include <linux/sunrpc/cache.h>
  47. #ifdef RPC_DEBUG
  48. # define RPCDBG_FACILITY RPCDBG_AUTH
  49. #endif
  50. /* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
  51. * into replies.
  52. *
  53. * Key is context handle (\x if empty) and gss_token.
  54. * Content is major_status minor_status (integers) context_handle, reply_token.
  55. *
  56. */
  57. static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
  58. {
  59. return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
  60. }
  61. #define RSI_HASHBITS 6
  62. #define RSI_HASHMAX (1<<RSI_HASHBITS)
  63. #define RSI_HASHMASK (RSI_HASHMAX-1)
  64. struct rsi {
  65. struct cache_head h;
  66. struct xdr_netobj in_handle, in_token;
  67. struct xdr_netobj out_handle, out_token;
  68. int major_status, minor_status;
  69. };
  70. static struct cache_head *rsi_table[RSI_HASHMAX];
  71. static struct cache_detail rsi_cache;
  72. static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
  73. static struct rsi *rsi_lookup(struct rsi *item);
  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 kref *ref)
  82. {
  83. struct rsi *rsii = container_of(ref, struct rsi, h.ref);
  84. rsi_free(rsii);
  85. kfree(rsii);
  86. }
  87. static inline int rsi_hash(struct rsi *item)
  88. {
  89. return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
  90. ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
  91. }
  92. static int rsi_match(struct cache_head *a, struct cache_head *b)
  93. {
  94. struct rsi *item = container_of(a, struct rsi, h);
  95. struct rsi *tmp = container_of(b, struct rsi, h);
  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 ? kmemdup(src, len, GFP_KERNEL) : NULL);
  103. if (len && !dst->data)
  104. return -ENOMEM;
  105. return 0;
  106. }
  107. static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
  108. {
  109. return dup_to_netobj(dst, src->data, src->len);
  110. }
  111. static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
  112. {
  113. struct rsi *new = container_of(cnew, struct rsi, h);
  114. struct rsi *item = container_of(citem, struct rsi, h);
  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 void update_rsi(struct cache_head *cnew, struct cache_head *citem)
  129. {
  130. struct rsi *new = container_of(cnew, struct rsi, h);
  131. struct rsi *item = container_of(citem, struct rsi, h);
  132. BUG_ON(new->out_handle.data || new->out_token.data);
  133. new->out_handle.len = item->out_handle.len;
  134. item->out_handle.len = 0;
  135. new->out_token.len = item->out_token.len;
  136. item->out_token.len = 0;
  137. new->out_handle.data = item->out_handle.data;
  138. item->out_handle.data = NULL;
  139. new->out_token.data = item->out_token.data;
  140. item->out_token.data = NULL;
  141. new->major_status = item->major_status;
  142. new->minor_status = item->minor_status;
  143. }
  144. static struct cache_head *rsi_alloc(void)
  145. {
  146. struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);
  147. if (rsii)
  148. return &rsii->h;
  149. else
  150. return NULL;
  151. }
  152. static void rsi_request(struct cache_detail *cd,
  153. struct cache_head *h,
  154. char **bpp, int *blen)
  155. {
  156. struct rsi *rsii = container_of(h, struct rsi, h);
  157. qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
  158. qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
  159. (*bpp)[-1] = '\n';
  160. }
  161. static int rsi_parse(struct cache_detail *cd,
  162. char *mesg, int mlen)
  163. {
  164. /* context token expiry major minor context token */
  165. char *buf = mesg;
  166. char *ep;
  167. int len;
  168. struct rsi rsii, *rsip = NULL;
  169. time_t expiry;
  170. int status = -EINVAL;
  171. memset(&rsii, 0, sizeof(rsii));
  172. /* handle */
  173. len = qword_get(&mesg, buf, mlen);
  174. if (len < 0)
  175. goto out;
  176. status = -ENOMEM;
  177. if (dup_to_netobj(&rsii.in_handle, buf, len))
  178. goto out;
  179. /* token */
  180. len = qword_get(&mesg, buf, mlen);
  181. status = -EINVAL;
  182. if (len < 0)
  183. goto out;
  184. status = -ENOMEM;
  185. if (dup_to_netobj(&rsii.in_token, buf, len))
  186. goto out;
  187. rsip = rsi_lookup(&rsii);
  188. if (!rsip)
  189. goto out;
  190. rsii.h.flags = 0;
  191. /* expiry */
  192. expiry = get_expiry(&mesg);
  193. status = -EINVAL;
  194. if (expiry == 0)
  195. goto out;
  196. /* major/minor */
  197. len = qword_get(&mesg, buf, mlen);
  198. if (len < 0)
  199. goto out;
  200. if (len == 0) {
  201. goto out;
  202. } else {
  203. rsii.major_status = simple_strtoul(buf, &ep, 10);
  204. if (*ep)
  205. goto out;
  206. len = qword_get(&mesg, buf, mlen);
  207. if (len <= 0)
  208. goto out;
  209. rsii.minor_status = simple_strtoul(buf, &ep, 10);
  210. if (*ep)
  211. goto out;
  212. /* out_handle */
  213. len = qword_get(&mesg, buf, mlen);
  214. if (len < 0)
  215. goto out;
  216. status = -ENOMEM;
  217. if (dup_to_netobj(&rsii.out_handle, buf, len))
  218. goto out;
  219. /* out_token */
  220. len = qword_get(&mesg, buf, mlen);
  221. status = -EINVAL;
  222. if (len < 0)
  223. goto out;
  224. status = -ENOMEM;
  225. if (dup_to_netobj(&rsii.out_token, buf, len))
  226. goto out;
  227. }
  228. rsii.h.expiry_time = expiry;
  229. rsip = rsi_update(&rsii, rsip);
  230. status = 0;
  231. out:
  232. rsi_free(&rsii);
  233. if (rsip)
  234. cache_put(&rsip->h, &rsi_cache);
  235. else
  236. status = -ENOMEM;
  237. return status;
  238. }
  239. static struct cache_detail rsi_cache = {
  240. .owner = THIS_MODULE,
  241. .hash_size = RSI_HASHMAX,
  242. .hash_table = rsi_table,
  243. .name = "auth.rpcsec.init",
  244. .cache_put = rsi_put,
  245. .cache_request = rsi_request,
  246. .cache_parse = rsi_parse,
  247. .match = rsi_match,
  248. .init = rsi_init,
  249. .update = update_rsi,
  250. .alloc = rsi_alloc,
  251. };
  252. static struct rsi *rsi_lookup(struct rsi *item)
  253. {
  254. struct cache_head *ch;
  255. int hash = rsi_hash(item);
  256. ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
  257. if (ch)
  258. return container_of(ch, struct rsi, h);
  259. else
  260. return NULL;
  261. }
  262. static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
  263. {
  264. struct cache_head *ch;
  265. int hash = rsi_hash(new);
  266. ch = sunrpc_cache_update(&rsi_cache, &new->h,
  267. &old->h, hash);
  268. if (ch)
  269. return container_of(ch, struct rsi, h);
  270. else
  271. return NULL;
  272. }
  273. /*
  274. * The rpcsec_context cache is used to store a context that is
  275. * used in data exchange.
  276. * The key is a context handle. The content is:
  277. * uid, gidlist, mechanism, service-set, mech-specific-data
  278. */
  279. #define RSC_HASHBITS 10
  280. #define RSC_HASHMAX (1<<RSC_HASHBITS)
  281. #define RSC_HASHMASK (RSC_HASHMAX-1)
  282. #define GSS_SEQ_WIN 128
  283. struct gss_svc_seq_data {
  284. /* highest seq number seen so far: */
  285. int sd_max;
  286. /* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
  287. * sd_win is nonzero iff sequence number i has been seen already: */
  288. unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
  289. spinlock_t sd_lock;
  290. };
  291. struct rsc {
  292. struct cache_head h;
  293. struct xdr_netobj handle;
  294. struct svc_cred cred;
  295. struct gss_svc_seq_data seqdata;
  296. struct gss_ctx *mechctx;
  297. };
  298. static struct cache_head *rsc_table[RSC_HASHMAX];
  299. static struct cache_detail rsc_cache;
  300. static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
  301. static struct rsc *rsc_lookup(struct rsc *item);
  302. static void rsc_free(struct rsc *rsci)
  303. {
  304. kfree(rsci->handle.data);
  305. if (rsci->mechctx)
  306. gss_delete_sec_context(&rsci->mechctx);
  307. if (rsci->cred.cr_group_info)
  308. put_group_info(rsci->cred.cr_group_info);
  309. }
  310. static void rsc_put(struct kref *ref)
  311. {
  312. struct rsc *rsci = container_of(ref, struct rsc, h.ref);
  313. rsc_free(rsci);
  314. kfree(rsci);
  315. }
  316. static inline int
  317. rsc_hash(struct rsc *rsci)
  318. {
  319. return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
  320. }
  321. static int
  322. rsc_match(struct cache_head *a, struct cache_head *b)
  323. {
  324. struct rsc *new = container_of(a, struct rsc, h);
  325. struct rsc *tmp = container_of(b, struct rsc, h);
  326. return netobj_equal(&new->handle, &tmp->handle);
  327. }
  328. static void
  329. rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
  330. {
  331. struct rsc *new = container_of(cnew, struct rsc, h);
  332. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  333. new->handle.len = tmp->handle.len;
  334. tmp->handle.len = 0;
  335. new->handle.data = tmp->handle.data;
  336. tmp->handle.data = NULL;
  337. new->mechctx = NULL;
  338. new->cred.cr_group_info = NULL;
  339. }
  340. static void
  341. update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
  342. {
  343. struct rsc *new = container_of(cnew, struct rsc, h);
  344. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  345. new->mechctx = tmp->mechctx;
  346. tmp->mechctx = NULL;
  347. memset(&new->seqdata, 0, sizeof(new->seqdata));
  348. spin_lock_init(&new->seqdata.sd_lock);
  349. new->cred = tmp->cred;
  350. tmp->cred.cr_group_info = NULL;
  351. }
  352. static struct cache_head *
  353. rsc_alloc(void)
  354. {
  355. struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
  356. if (rsci)
  357. return &rsci->h;
  358. else
  359. return NULL;
  360. }
  361. static int rsc_parse(struct cache_detail *cd,
  362. char *mesg, int mlen)
  363. {
  364. /* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
  365. char *buf = mesg;
  366. int len, rv;
  367. struct rsc rsci, *rscp = NULL;
  368. time_t expiry;
  369. int status = -EINVAL;
  370. struct gss_api_mech *gm = NULL;
  371. memset(&rsci, 0, sizeof(rsci));
  372. /* context handle */
  373. len = qword_get(&mesg, buf, mlen);
  374. if (len < 0) goto out;
  375. status = -ENOMEM;
  376. if (dup_to_netobj(&rsci.handle, buf, len))
  377. goto out;
  378. rsci.h.flags = 0;
  379. /* expiry */
  380. expiry = get_expiry(&mesg);
  381. status = -EINVAL;
  382. if (expiry == 0)
  383. goto out;
  384. rscp = rsc_lookup(&rsci);
  385. if (!rscp)
  386. goto out;
  387. /* uid, or NEGATIVE */
  388. rv = get_int(&mesg, &rsci.cred.cr_uid);
  389. if (rv == -EINVAL)
  390. goto out;
  391. if (rv == -ENOENT)
  392. set_bit(CACHE_NEGATIVE, &rsci.h.flags);
  393. else {
  394. int N, i;
  395. /* gid */
  396. if (get_int(&mesg, &rsci.cred.cr_gid))
  397. goto out;
  398. /* number of additional gid's */
  399. if (get_int(&mesg, &N))
  400. goto out;
  401. status = -ENOMEM;
  402. rsci.cred.cr_group_info = groups_alloc(N);
  403. if (rsci.cred.cr_group_info == NULL)
  404. goto out;
  405. /* gid's */
  406. status = -EINVAL;
  407. for (i=0; i<N; i++) {
  408. gid_t gid;
  409. if (get_int(&mesg, &gid))
  410. goto out;
  411. GROUP_AT(rsci.cred.cr_group_info, i) = gid;
  412. }
  413. /* mech name */
  414. len = qword_get(&mesg, buf, mlen);
  415. if (len < 0)
  416. goto out;
  417. gm = gss_mech_get_by_name(buf);
  418. status = -EOPNOTSUPP;
  419. if (!gm)
  420. goto out;
  421. status = -EINVAL;
  422. /* mech-specific data: */
  423. len = qword_get(&mesg, buf, mlen);
  424. if (len < 0)
  425. goto out;
  426. status = gss_import_sec_context(buf, len, gm, &rsci.mechctx);
  427. if (status)
  428. goto out;
  429. }
  430. rsci.h.expiry_time = expiry;
  431. rscp = rsc_update(&rsci, rscp);
  432. status = 0;
  433. out:
  434. gss_mech_put(gm);
  435. rsc_free(&rsci);
  436. if (rscp)
  437. cache_put(&rscp->h, &rsc_cache);
  438. else
  439. status = -ENOMEM;
  440. return status;
  441. }
  442. static struct cache_detail rsc_cache = {
  443. .owner = THIS_MODULE,
  444. .hash_size = RSC_HASHMAX,
  445. .hash_table = rsc_table,
  446. .name = "auth.rpcsec.context",
  447. .cache_put = rsc_put,
  448. .cache_parse = rsc_parse,
  449. .match = rsc_match,
  450. .init = rsc_init,
  451. .update = update_rsc,
  452. .alloc = rsc_alloc,
  453. };
  454. static struct rsc *rsc_lookup(struct rsc *item)
  455. {
  456. struct cache_head *ch;
  457. int hash = rsc_hash(item);
  458. ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
  459. if (ch)
  460. return container_of(ch, struct rsc, h);
  461. else
  462. return NULL;
  463. }
  464. static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
  465. {
  466. struct cache_head *ch;
  467. int hash = rsc_hash(new);
  468. ch = sunrpc_cache_update(&rsc_cache, &new->h,
  469. &old->h, hash);
  470. if (ch)
  471. return container_of(ch, struct rsc, h);
  472. else
  473. return NULL;
  474. }
  475. static struct rsc *
  476. gss_svc_searchbyctx(struct xdr_netobj *handle)
  477. {
  478. struct rsc rsci;
  479. struct rsc *found;
  480. memset(&rsci, 0, sizeof(rsci));
  481. if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
  482. return NULL;
  483. found = rsc_lookup(&rsci);
  484. rsc_free(&rsci);
  485. if (!found)
  486. return NULL;
  487. if (cache_check(&rsc_cache, &found->h, NULL))
  488. return NULL;
  489. return found;
  490. }
  491. /* Implements sequence number algorithm as specified in RFC 2203. */
  492. static int
  493. gss_check_seq_num(struct rsc *rsci, int seq_num)
  494. {
  495. struct gss_svc_seq_data *sd = &rsci->seqdata;
  496. spin_lock(&sd->sd_lock);
  497. if (seq_num > sd->sd_max) {
  498. if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
  499. memset(sd->sd_win,0,sizeof(sd->sd_win));
  500. sd->sd_max = seq_num;
  501. } else while (sd->sd_max < seq_num) {
  502. sd->sd_max++;
  503. __clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
  504. }
  505. __set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
  506. goto ok;
  507. } else if (seq_num <= sd->sd_max - GSS_SEQ_WIN) {
  508. goto drop;
  509. }
  510. /* sd_max - GSS_SEQ_WIN < seq_num <= sd_max */
  511. if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
  512. goto drop;
  513. ok:
  514. spin_unlock(&sd->sd_lock);
  515. return 1;
  516. drop:
  517. spin_unlock(&sd->sd_lock);
  518. return 0;
  519. }
  520. static inline u32 round_up_to_quad(u32 i)
  521. {
  522. return (i + 3 ) & ~3;
  523. }
  524. static inline int
  525. svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
  526. {
  527. int l;
  528. if (argv->iov_len < 4)
  529. return -1;
  530. o->len = svc_getnl(argv);
  531. l = round_up_to_quad(o->len);
  532. if (argv->iov_len < l)
  533. return -1;
  534. o->data = argv->iov_base;
  535. argv->iov_base += l;
  536. argv->iov_len -= l;
  537. return 0;
  538. }
  539. static inline int
  540. svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
  541. {
  542. u8 *p;
  543. if (resv->iov_len + 4 > PAGE_SIZE)
  544. return -1;
  545. svc_putnl(resv, o->len);
  546. p = resv->iov_base + resv->iov_len;
  547. resv->iov_len += round_up_to_quad(o->len);
  548. if (resv->iov_len > PAGE_SIZE)
  549. return -1;
  550. memcpy(p, o->data, o->len);
  551. memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);
  552. return 0;
  553. }
  554. /* Verify the checksum on the header and return SVC_OK on success.
  555. * Otherwise, return SVC_DROP (in the case of a bad sequence number)
  556. * or return SVC_DENIED and indicate error in authp.
  557. */
  558. static int
  559. gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
  560. __be32 *rpcstart, struct rpc_gss_wire_cred *gc, __be32 *authp)
  561. {
  562. struct gss_ctx *ctx_id = rsci->mechctx;
  563. struct xdr_buf rpchdr;
  564. struct xdr_netobj checksum;
  565. u32 flavor = 0;
  566. struct kvec *argv = &rqstp->rq_arg.head[0];
  567. struct kvec iov;
  568. /* data to compute the checksum over: */
  569. iov.iov_base = rpcstart;
  570. iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
  571. xdr_buf_from_iov(&iov, &rpchdr);
  572. *authp = rpc_autherr_badverf;
  573. if (argv->iov_len < 4)
  574. return SVC_DENIED;
  575. flavor = svc_getnl(argv);
  576. if (flavor != RPC_AUTH_GSS)
  577. return SVC_DENIED;
  578. if (svc_safe_getnetobj(argv, &checksum))
  579. return SVC_DENIED;
  580. if (rqstp->rq_deferred) /* skip verification of revisited request */
  581. return SVC_OK;
  582. if (gss_verify_mic(ctx_id, &rpchdr, &checksum) != GSS_S_COMPLETE) {
  583. *authp = rpcsec_gsserr_credproblem;
  584. return SVC_DENIED;
  585. }
  586. if (gc->gc_seq > MAXSEQ) {
  587. dprintk("RPC: svcauth_gss: discarding request with "
  588. "large sequence number %d\n", gc->gc_seq);
  589. *authp = rpcsec_gsserr_ctxproblem;
  590. return SVC_DENIED;
  591. }
  592. if (!gss_check_seq_num(rsci, gc->gc_seq)) {
  593. dprintk("RPC: svcauth_gss: discarding request with "
  594. "old sequence number %d\n", gc->gc_seq);
  595. return SVC_DROP;
  596. }
  597. return SVC_OK;
  598. }
  599. static int
  600. gss_write_null_verf(struct svc_rqst *rqstp)
  601. {
  602. __be32 *p;
  603. svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);
  604. p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
  605. /* don't really need to check if head->iov_len > PAGE_SIZE ... */
  606. *p++ = 0;
  607. if (!xdr_ressize_check(rqstp, p))
  608. return -1;
  609. return 0;
  610. }
  611. static int
  612. gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
  613. {
  614. __be32 xdr_seq;
  615. u32 maj_stat;
  616. struct xdr_buf verf_data;
  617. struct xdr_netobj mic;
  618. __be32 *p;
  619. struct kvec iov;
  620. svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);
  621. xdr_seq = htonl(seq);
  622. iov.iov_base = &xdr_seq;
  623. iov.iov_len = sizeof(xdr_seq);
  624. xdr_buf_from_iov(&iov, &verf_data);
  625. p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
  626. mic.data = (u8 *)(p + 1);
  627. maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
  628. if (maj_stat != GSS_S_COMPLETE)
  629. return -1;
  630. *p++ = htonl(mic.len);
  631. memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
  632. p += XDR_QUADLEN(mic.len);
  633. if (!xdr_ressize_check(rqstp, p))
  634. return -1;
  635. return 0;
  636. }
  637. struct gss_domain {
  638. struct auth_domain h;
  639. u32 pseudoflavor;
  640. };
  641. static struct auth_domain *
  642. find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
  643. {
  644. char *name;
  645. name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
  646. if (!name)
  647. return NULL;
  648. return auth_domain_find(name);
  649. }
  650. static struct auth_ops svcauthops_gss;
  651. u32 svcauth_gss_flavor(struct auth_domain *dom)
  652. {
  653. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  654. return gd->pseudoflavor;
  655. }
  656. EXPORT_SYMBOL(svcauth_gss_flavor);
  657. int
  658. svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
  659. {
  660. struct gss_domain *new;
  661. struct auth_domain *test;
  662. int stat = -ENOMEM;
  663. new = kmalloc(sizeof(*new), GFP_KERNEL);
  664. if (!new)
  665. goto out;
  666. kref_init(&new->h.ref);
  667. new->h.name = kstrdup(name, GFP_KERNEL);
  668. if (!new->h.name)
  669. goto out_free_dom;
  670. new->h.flavour = &svcauthops_gss;
  671. new->pseudoflavor = pseudoflavor;
  672. stat = 0;
  673. test = auth_domain_lookup(name, &new->h);
  674. if (test != &new->h) { /* Duplicate registration */
  675. auth_domain_put(test);
  676. kfree(new->h.name);
  677. goto out_free_dom;
  678. }
  679. return 0;
  680. out_free_dom:
  681. kfree(new);
  682. out:
  683. return stat;
  684. }
  685. EXPORT_SYMBOL(svcauth_gss_register_pseudoflavor);
  686. static inline int
  687. read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
  688. {
  689. __be32 raw;
  690. int status;
  691. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  692. if (status)
  693. return status;
  694. *obj = ntohl(raw);
  695. return 0;
  696. }
  697. /* It would be nice if this bit of code could be shared with the client.
  698. * Obstacles:
  699. * The client shouldn't malloc(), would have to pass in own memory.
  700. * The server uses base of head iovec as read pointer, while the
  701. * client uses separate pointer. */
  702. static int
  703. unwrap_integ_data(struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  704. {
  705. int stat = -EINVAL;
  706. u32 integ_len, maj_stat;
  707. struct xdr_netobj mic;
  708. struct xdr_buf integ_buf;
  709. integ_len = svc_getnl(&buf->head[0]);
  710. if (integ_len & 3)
  711. return stat;
  712. if (integ_len > buf->len)
  713. return stat;
  714. if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
  715. BUG();
  716. /* copy out mic... */
  717. if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
  718. BUG();
  719. if (mic.len > RPC_MAX_AUTH_SIZE)
  720. return stat;
  721. mic.data = kmalloc(mic.len, GFP_KERNEL);
  722. if (!mic.data)
  723. return stat;
  724. if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
  725. goto out;
  726. maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);
  727. if (maj_stat != GSS_S_COMPLETE)
  728. goto out;
  729. if (svc_getnl(&buf->head[0]) != seq)
  730. goto out;
  731. stat = 0;
  732. out:
  733. kfree(mic.data);
  734. return stat;
  735. }
  736. static inline int
  737. total_buf_len(struct xdr_buf *buf)
  738. {
  739. return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;
  740. }
  741. static void
  742. fix_priv_head(struct xdr_buf *buf, int pad)
  743. {
  744. if (buf->page_len == 0) {
  745. /* We need to adjust head and buf->len in tandem in this
  746. * case to make svc_defer() work--it finds the original
  747. * buffer start using buf->len - buf->head[0].iov_len. */
  748. buf->head[0].iov_len -= pad;
  749. }
  750. }
  751. static int
  752. unwrap_priv_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  753. {
  754. u32 priv_len, maj_stat;
  755. int pad, saved_len, remaining_len, offset;
  756. rqstp->rq_splice_ok = 0;
  757. priv_len = svc_getnl(&buf->head[0]);
  758. if (rqstp->rq_deferred) {
  759. /* Already decrypted last time through! The sequence number
  760. * check at out_seq is unnecessary but harmless: */
  761. goto out_seq;
  762. }
  763. /* buf->len is the number of bytes from the original start of the
  764. * request to the end, where head[0].iov_len is just the bytes
  765. * not yet read from the head, so these two values are different: */
  766. remaining_len = total_buf_len(buf);
  767. if (priv_len > remaining_len)
  768. return -EINVAL;
  769. pad = remaining_len - priv_len;
  770. buf->len -= pad;
  771. fix_priv_head(buf, pad);
  772. /* Maybe it would be better to give gss_unwrap a length parameter: */
  773. saved_len = buf->len;
  774. buf->len = priv_len;
  775. maj_stat = gss_unwrap(ctx, 0, buf);
  776. pad = priv_len - buf->len;
  777. buf->len = saved_len;
  778. buf->len -= pad;
  779. /* The upper layers assume the buffer is aligned on 4-byte boundaries.
  780. * In the krb5p case, at least, the data ends up offset, so we need to
  781. * move it around. */
  782. /* XXX: This is very inefficient. It would be better to either do
  783. * this while we encrypt, or maybe in the receive code, if we can peak
  784. * ahead and work out the service and mechanism there. */
  785. offset = buf->head[0].iov_len % 4;
  786. if (offset) {
  787. buf->buflen = RPCSVC_MAXPAYLOAD;
  788. xdr_shift_buf(buf, offset);
  789. fix_priv_head(buf, pad);
  790. }
  791. if (maj_stat != GSS_S_COMPLETE)
  792. return -EINVAL;
  793. out_seq:
  794. if (svc_getnl(&buf->head[0]) != seq)
  795. return -EINVAL;
  796. return 0;
  797. }
  798. struct gss_svc_data {
  799. /* decoded gss client cred: */
  800. struct rpc_gss_wire_cred clcred;
  801. /* save a pointer to the beginning of the encoded verifier,
  802. * for use in encryption/checksumming in svcauth_gss_release: */
  803. __be32 *verf_start;
  804. struct rsc *rsci;
  805. };
  806. static int
  807. svcauth_gss_set_client(struct svc_rqst *rqstp)
  808. {
  809. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  810. struct rsc *rsci = svcdata->rsci;
  811. struct rpc_gss_wire_cred *gc = &svcdata->clcred;
  812. int stat;
  813. /*
  814. * A gss export can be specified either by:
  815. * export *(sec=krb5,rw)
  816. * or by
  817. * export gss/krb5(rw)
  818. * The latter is deprecated; but for backwards compatibility reasons
  819. * the nfsd code will still fall back on trying it if the former
  820. * doesn't work; so we try to make both available to nfsd, below.
  821. */
  822. rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
  823. if (rqstp->rq_gssclient == NULL)
  824. return SVC_DENIED;
  825. stat = svcauth_unix_set_client(rqstp);
  826. if (stat == SVC_DROP)
  827. return stat;
  828. return SVC_OK;
  829. }
  830. static inline int
  831. gss_write_init_verf(struct svc_rqst *rqstp, struct rsi *rsip)
  832. {
  833. struct rsc *rsci;
  834. int rc;
  835. if (rsip->major_status != GSS_S_COMPLETE)
  836. return gss_write_null_verf(rqstp);
  837. rsci = gss_svc_searchbyctx(&rsip->out_handle);
  838. if (rsci == NULL) {
  839. rsip->major_status = GSS_S_NO_CONTEXT;
  840. return gss_write_null_verf(rqstp);
  841. }
  842. rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
  843. cache_put(&rsci->h, &rsc_cache);
  844. return rc;
  845. }
  846. /*
  847. * Accept an rpcsec packet.
  848. * If context establishment, punt to user space
  849. * If data exchange, verify/decrypt
  850. * If context destruction, handle here
  851. * In the context establishment and destruction case we encode
  852. * response here and return SVC_COMPLETE.
  853. */
  854. static int
  855. svcauth_gss_accept(struct svc_rqst *rqstp, __be32 *authp)
  856. {
  857. struct kvec *argv = &rqstp->rq_arg.head[0];
  858. struct kvec *resv = &rqstp->rq_res.head[0];
  859. u32 crlen;
  860. struct xdr_netobj tmpobj;
  861. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  862. struct rpc_gss_wire_cred *gc;
  863. struct rsc *rsci = NULL;
  864. struct rsi *rsip, rsikey;
  865. __be32 *rpcstart;
  866. __be32 *reject_stat = resv->iov_base + resv->iov_len;
  867. int ret;
  868. dprintk("RPC: svcauth_gss: argv->iov_len = %zd\n",
  869. argv->iov_len);
  870. *authp = rpc_autherr_badcred;
  871. if (!svcdata)
  872. svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
  873. if (!svcdata)
  874. goto auth_err;
  875. rqstp->rq_auth_data = svcdata;
  876. svcdata->verf_start = NULL;
  877. svcdata->rsci = NULL;
  878. gc = &svcdata->clcred;
  879. /* start of rpc packet is 7 u32's back from here:
  880. * xid direction rpcversion prog vers proc flavour
  881. */
  882. rpcstart = argv->iov_base;
  883. rpcstart -= 7;
  884. /* credential is:
  885. * version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
  886. * at least 5 u32s, and is preceeded by length, so that makes 6.
  887. */
  888. if (argv->iov_len < 5 * 4)
  889. goto auth_err;
  890. crlen = svc_getnl(argv);
  891. if (svc_getnl(argv) != RPC_GSS_VERSION)
  892. goto auth_err;
  893. gc->gc_proc = svc_getnl(argv);
  894. gc->gc_seq = svc_getnl(argv);
  895. gc->gc_svc = svc_getnl(argv);
  896. if (svc_safe_getnetobj(argv, &gc->gc_ctx))
  897. goto auth_err;
  898. if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
  899. goto auth_err;
  900. if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
  901. goto auth_err;
  902. /*
  903. * We've successfully parsed the credential. Let's check out the
  904. * verifier. An AUTH_NULL verifier is allowed (and required) for
  905. * INIT and CONTINUE_INIT requests. AUTH_RPCSEC_GSS is required for
  906. * PROC_DATA and PROC_DESTROY.
  907. *
  908. * AUTH_NULL verifier is 0 (AUTH_NULL), 0 (length).
  909. * AUTH_RPCSEC_GSS verifier is:
  910. * 6 (AUTH_RPCSEC_GSS), length, checksum.
  911. * checksum is calculated over rpcheader from xid up to here.
  912. */
  913. *authp = rpc_autherr_badverf;
  914. switch (gc->gc_proc) {
  915. case RPC_GSS_PROC_INIT:
  916. case RPC_GSS_PROC_CONTINUE_INIT:
  917. if (argv->iov_len < 2 * 4)
  918. goto auth_err;
  919. if (svc_getnl(argv) != RPC_AUTH_NULL)
  920. goto auth_err;
  921. if (svc_getnl(argv) != 0)
  922. goto auth_err;
  923. break;
  924. case RPC_GSS_PROC_DATA:
  925. case RPC_GSS_PROC_DESTROY:
  926. *authp = rpcsec_gsserr_credproblem;
  927. rsci = gss_svc_searchbyctx(&gc->gc_ctx);
  928. if (!rsci)
  929. goto auth_err;
  930. switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
  931. case SVC_OK:
  932. break;
  933. case SVC_DENIED:
  934. goto auth_err;
  935. case SVC_DROP:
  936. goto drop;
  937. }
  938. break;
  939. default:
  940. *authp = rpc_autherr_rejectedcred;
  941. goto auth_err;
  942. }
  943. /* now act upon the command: */
  944. switch (gc->gc_proc) {
  945. case RPC_GSS_PROC_INIT:
  946. case RPC_GSS_PROC_CONTINUE_INIT:
  947. *authp = rpc_autherr_badcred;
  948. if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
  949. goto auth_err;
  950. memset(&rsikey, 0, sizeof(rsikey));
  951. if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))
  952. goto drop;
  953. *authp = rpc_autherr_badverf;
  954. if (svc_safe_getnetobj(argv, &tmpobj)) {
  955. kfree(rsikey.in_handle.data);
  956. goto auth_err;
  957. }
  958. if (dup_netobj(&rsikey.in_token, &tmpobj)) {
  959. kfree(rsikey.in_handle.data);
  960. goto drop;
  961. }
  962. rsip = rsi_lookup(&rsikey);
  963. rsi_free(&rsikey);
  964. if (!rsip) {
  965. goto drop;
  966. }
  967. switch(cache_check(&rsi_cache, &rsip->h, &rqstp->rq_chandle)) {
  968. case -EAGAIN:
  969. case -ETIMEDOUT:
  970. case -ENOENT:
  971. goto drop;
  972. case 0:
  973. if (gss_write_init_verf(rqstp, rsip))
  974. goto drop;
  975. if (resv->iov_len + 4 > PAGE_SIZE)
  976. goto drop;
  977. svc_putnl(resv, RPC_SUCCESS);
  978. if (svc_safe_putnetobj(resv, &rsip->out_handle))
  979. goto drop;
  980. if (resv->iov_len + 3 * 4 > PAGE_SIZE)
  981. goto drop;
  982. svc_putnl(resv, rsip->major_status);
  983. svc_putnl(resv, rsip->minor_status);
  984. svc_putnl(resv, GSS_SEQ_WIN);
  985. if (svc_safe_putnetobj(resv, &rsip->out_token))
  986. goto drop;
  987. }
  988. goto complete;
  989. case RPC_GSS_PROC_DESTROY:
  990. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  991. goto auth_err;
  992. set_bit(CACHE_NEGATIVE, &rsci->h.flags);
  993. if (resv->iov_len + 4 > PAGE_SIZE)
  994. goto drop;
  995. svc_putnl(resv, RPC_SUCCESS);
  996. goto complete;
  997. case RPC_GSS_PROC_DATA:
  998. *authp = rpcsec_gsserr_ctxproblem;
  999. svcdata->verf_start = resv->iov_base + resv->iov_len;
  1000. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1001. goto auth_err;
  1002. rqstp->rq_cred = rsci->cred;
  1003. get_group_info(rsci->cred.cr_group_info);
  1004. *authp = rpc_autherr_badcred;
  1005. switch (gc->gc_svc) {
  1006. case RPC_GSS_SVC_NONE:
  1007. break;
  1008. case RPC_GSS_SVC_INTEGRITY:
  1009. if (unwrap_integ_data(&rqstp->rq_arg,
  1010. gc->gc_seq, rsci->mechctx))
  1011. goto auth_err;
  1012. /* placeholders for length and seq. number: */
  1013. svc_putnl(resv, 0);
  1014. svc_putnl(resv, 0);
  1015. break;
  1016. case RPC_GSS_SVC_PRIVACY:
  1017. if (unwrap_priv_data(rqstp, &rqstp->rq_arg,
  1018. gc->gc_seq, rsci->mechctx))
  1019. goto auth_err;
  1020. /* placeholders for length and seq. number: */
  1021. svc_putnl(resv, 0);
  1022. svc_putnl(resv, 0);
  1023. break;
  1024. default:
  1025. goto auth_err;
  1026. }
  1027. svcdata->rsci = rsci;
  1028. cache_get(&rsci->h);
  1029. rqstp->rq_flavor = gss_svc_to_pseudoflavor(
  1030. rsci->mechctx->mech_type, gc->gc_svc);
  1031. ret = SVC_OK;
  1032. goto out;
  1033. }
  1034. auth_err:
  1035. /* Restore write pointer to original value: */
  1036. xdr_ressize_check(rqstp, reject_stat);
  1037. ret = SVC_DENIED;
  1038. goto out;
  1039. complete:
  1040. ret = SVC_COMPLETE;
  1041. goto out;
  1042. drop:
  1043. ret = SVC_DROP;
  1044. out:
  1045. if (rsci)
  1046. cache_put(&rsci->h, &rsc_cache);
  1047. return ret;
  1048. }
  1049. static __be32 *
  1050. svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
  1051. {
  1052. __be32 *p;
  1053. u32 verf_len;
  1054. p = gsd->verf_start;
  1055. gsd->verf_start = NULL;
  1056. /* If the reply stat is nonzero, don't wrap: */
  1057. if (*(p-1) != rpc_success)
  1058. return NULL;
  1059. /* Skip the verifier: */
  1060. p += 1;
  1061. verf_len = ntohl(*p++);
  1062. p += XDR_QUADLEN(verf_len);
  1063. /* move accept_stat to right place: */
  1064. memcpy(p, p + 2, 4);
  1065. /* Also don't wrap if the accept stat is nonzero: */
  1066. if (*p != rpc_success) {
  1067. resbuf->head[0].iov_len -= 2 * 4;
  1068. return NULL;
  1069. }
  1070. p++;
  1071. return p;
  1072. }
  1073. static inline int
  1074. svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
  1075. {
  1076. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1077. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1078. struct xdr_buf *resbuf = &rqstp->rq_res;
  1079. struct xdr_buf integ_buf;
  1080. struct xdr_netobj mic;
  1081. struct kvec *resv;
  1082. __be32 *p;
  1083. int integ_offset, integ_len;
  1084. int stat = -EINVAL;
  1085. p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
  1086. if (p == NULL)
  1087. goto out;
  1088. integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
  1089. integ_len = resbuf->len - integ_offset;
  1090. BUG_ON(integ_len % 4);
  1091. *p++ = htonl(integ_len);
  1092. *p++ = htonl(gc->gc_seq);
  1093. if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset,
  1094. integ_len))
  1095. BUG();
  1096. if (resbuf->tail[0].iov_base == NULL) {
  1097. if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1098. goto out_err;
  1099. resbuf->tail[0].iov_base = resbuf->head[0].iov_base
  1100. + resbuf->head[0].iov_len;
  1101. resbuf->tail[0].iov_len = 0;
  1102. resv = &resbuf->tail[0];
  1103. } else {
  1104. resv = &resbuf->tail[0];
  1105. }
  1106. mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
  1107. if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
  1108. goto out_err;
  1109. svc_putnl(resv, mic.len);
  1110. memset(mic.data + mic.len, 0,
  1111. round_up_to_quad(mic.len) - mic.len);
  1112. resv->iov_len += XDR_QUADLEN(mic.len) << 2;
  1113. /* not strictly required: */
  1114. resbuf->len += XDR_QUADLEN(mic.len) << 2;
  1115. BUG_ON(resv->iov_len > PAGE_SIZE);
  1116. out:
  1117. stat = 0;
  1118. out_err:
  1119. return stat;
  1120. }
  1121. static inline int
  1122. svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
  1123. {
  1124. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1125. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1126. struct xdr_buf *resbuf = &rqstp->rq_res;
  1127. struct page **inpages = NULL;
  1128. __be32 *p, *len;
  1129. int offset;
  1130. int pad;
  1131. p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
  1132. if (p == NULL)
  1133. return 0;
  1134. len = p++;
  1135. offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
  1136. *p++ = htonl(gc->gc_seq);
  1137. inpages = resbuf->pages;
  1138. /* XXX: Would be better to write some xdr helper functions for
  1139. * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
  1140. if (resbuf->tail[0].iov_base) {
  1141. BUG_ON(resbuf->tail[0].iov_base >= resbuf->head[0].iov_base
  1142. + PAGE_SIZE);
  1143. BUG_ON(resbuf->tail[0].iov_base < resbuf->head[0].iov_base);
  1144. if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
  1145. + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1146. return -ENOMEM;
  1147. memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
  1148. resbuf->tail[0].iov_base,
  1149. resbuf->tail[0].iov_len);
  1150. resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
  1151. }
  1152. if (resbuf->tail[0].iov_base == NULL) {
  1153. if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1154. return -ENOMEM;
  1155. resbuf->tail[0].iov_base = resbuf->head[0].iov_base
  1156. + resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
  1157. resbuf->tail[0].iov_len = 0;
  1158. }
  1159. if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
  1160. return -ENOMEM;
  1161. *len = htonl(resbuf->len - offset);
  1162. pad = 3 - ((resbuf->len - offset - 1)&3);
  1163. p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
  1164. memset(p, 0, pad);
  1165. resbuf->tail[0].iov_len += pad;
  1166. resbuf->len += pad;
  1167. return 0;
  1168. }
  1169. static int
  1170. svcauth_gss_release(struct svc_rqst *rqstp)
  1171. {
  1172. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1173. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1174. struct xdr_buf *resbuf = &rqstp->rq_res;
  1175. int stat = -EINVAL;
  1176. if (gc->gc_proc != RPC_GSS_PROC_DATA)
  1177. goto out;
  1178. /* Release can be called twice, but we only wrap once. */
  1179. if (gsd->verf_start == NULL)
  1180. goto out;
  1181. /* normally not set till svc_send, but we need it here: */
  1182. /* XXX: what for? Do we mess it up the moment we call svc_putu32
  1183. * or whatever? */
  1184. resbuf->len = total_buf_len(resbuf);
  1185. switch (gc->gc_svc) {
  1186. case RPC_GSS_SVC_NONE:
  1187. break;
  1188. case RPC_GSS_SVC_INTEGRITY:
  1189. stat = svcauth_gss_wrap_resp_integ(rqstp);
  1190. if (stat)
  1191. goto out_err;
  1192. break;
  1193. case RPC_GSS_SVC_PRIVACY:
  1194. stat = svcauth_gss_wrap_resp_priv(rqstp);
  1195. if (stat)
  1196. goto out_err;
  1197. break;
  1198. default:
  1199. goto out_err;
  1200. }
  1201. out:
  1202. stat = 0;
  1203. out_err:
  1204. if (rqstp->rq_client)
  1205. auth_domain_put(rqstp->rq_client);
  1206. rqstp->rq_client = NULL;
  1207. if (rqstp->rq_gssclient)
  1208. auth_domain_put(rqstp->rq_gssclient);
  1209. rqstp->rq_gssclient = NULL;
  1210. if (rqstp->rq_cred.cr_group_info)
  1211. put_group_info(rqstp->rq_cred.cr_group_info);
  1212. rqstp->rq_cred.cr_group_info = NULL;
  1213. if (gsd->rsci)
  1214. cache_put(&gsd->rsci->h, &rsc_cache);
  1215. gsd->rsci = NULL;
  1216. return stat;
  1217. }
  1218. static void
  1219. svcauth_gss_domain_release(struct auth_domain *dom)
  1220. {
  1221. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  1222. kfree(dom->name);
  1223. kfree(gd);
  1224. }
  1225. static struct auth_ops svcauthops_gss = {
  1226. .name = "rpcsec_gss",
  1227. .owner = THIS_MODULE,
  1228. .flavour = RPC_AUTH_GSS,
  1229. .accept = svcauth_gss_accept,
  1230. .release = svcauth_gss_release,
  1231. .domain_release = svcauth_gss_domain_release,
  1232. .set_client = svcauth_gss_set_client,
  1233. };
  1234. int
  1235. gss_svc_init(void)
  1236. {
  1237. int rv = svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
  1238. if (rv == 0) {
  1239. cache_register(&rsc_cache);
  1240. cache_register(&rsi_cache);
  1241. }
  1242. return rv;
  1243. }
  1244. void
  1245. gss_svc_shutdown(void)
  1246. {
  1247. if (cache_unregister(&rsc_cache))
  1248. printk(KERN_ERR "auth_rpcgss: failed to unregister rsc cache\n");
  1249. if (cache_unregister(&rsi_cache))
  1250. printk(KERN_ERR "auth_rpcgss: failed to unregister rsi cache\n");
  1251. svc_auth_unregister(RPC_AUTH_GSS);
  1252. }