svcauth_gss.c 35 KB

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