svcauth_gss.c 39 KB

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