svcauth_gss.c 36 KB

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