svcauth_gss.c 34 KB

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