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