svcauth_gss.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Neil Brown <neilb@cse.unsw.edu.au>
  4. * J. Bruce Fields <bfields@umich.edu>
  5. * Andy Adamson <andros@umich.edu>
  6. * Dug Song <dugsong@monkey.org>
  7. *
  8. * RPCSEC_GSS server authentication.
  9. * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
  10. * (gssapi)
  11. *
  12. * The RPCSEC_GSS involves three stages:
  13. * 1/ context creation
  14. * 2/ data exchange
  15. * 3/ context destruction
  16. *
  17. * Context creation is handled largely by upcalls to user-space.
  18. * In particular, GSS_Accept_sec_context is handled by an upcall
  19. * Data exchange is handled entirely within the kernel
  20. * In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
  21. * Context destruction is handled in-kernel
  22. * GSS_Delete_sec_context is in-kernel
  23. *
  24. * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
  25. * The context handle and gss_token are used as a key into the rpcsec_init cache.
  26. * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
  27. * being major_status, minor_status, context_handle, reply_token.
  28. * These are sent back to the client.
  29. * Sequence window management is handled by the kernel. The window size if currently
  30. * a compile time constant.
  31. *
  32. * When user-space is happy that a context is established, it places an entry
  33. * in the rpcsec_context cache. The key for this cache is the context_handle.
  34. * The content includes:
  35. * uid/gidlist - for determining access rights
  36. * mechanism type
  37. * mechanism specific information, such as a key
  38. *
  39. */
  40. #include <linux/slab.h>
  41. #include <linux/types.h>
  42. #include <linux/module.h>
  43. #include <linux/pagemap.h>
  44. #include <linux/user_namespace.h>
  45. #include <linux/sunrpc/auth_gss.h>
  46. #include <linux/sunrpc/gss_err.h>
  47. #include <linux/sunrpc/svcauth.h>
  48. #include <linux/sunrpc/svcauth_gss.h>
  49. #include <linux/sunrpc/cache.h>
  50. #include <linux/sunrpc/gss_krb5.h>
  51. #include <trace/events/rpcgss.h>
  52. #include "gss_rpc_upcall.h"
  53. /*
  54. * Unfortunately there isn't a maximum checksum size exported via the
  55. * GSS API. Manufacture one based on GSS mechanisms supported by this
  56. * implementation.
  57. */
  58. #define GSS_MAX_CKSUMSIZE (GSS_KRB5_TOK_HDR_LEN + GSS_KRB5_MAX_CKSUM_LEN)
  59. /*
  60. * This value may be increased in the future to accommodate other
  61. * usage of the scratch buffer.
  62. */
  63. #define GSS_SCRATCH_SIZE GSS_MAX_CKSUMSIZE
  64. struct gss_svc_data {
  65. /* decoded gss client cred: */
  66. struct rpc_gss_wire_cred clcred;
  67. u32 gsd_databody_offset;
  68. struct rsc *rsci;
  69. /* for temporary results */
  70. __be32 gsd_seq_num;
  71. u8 gsd_scratch[GSS_SCRATCH_SIZE];
  72. };
  73. /* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
  74. * into replies.
  75. *
  76. * Key is context handle (\x if empty) and gss_token.
  77. * Content is major_status minor_status (integers) context_handle, reply_token.
  78. *
  79. */
  80. static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
  81. {
  82. return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
  83. }
  84. #define RSI_HASHBITS 6
  85. #define RSI_HASHMAX (1<<RSI_HASHBITS)
  86. struct rsi {
  87. struct cache_head h;
  88. struct xdr_netobj in_handle, in_token;
  89. struct xdr_netobj out_handle, out_token;
  90. int major_status, minor_status;
  91. struct rcu_head rcu_head;
  92. };
  93. static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
  94. static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
  95. static void rsi_free(struct rsi *rsii)
  96. {
  97. kfree(rsii->in_handle.data);
  98. kfree(rsii->in_token.data);
  99. kfree(rsii->out_handle.data);
  100. kfree(rsii->out_token.data);
  101. }
  102. static void rsi_free_rcu(struct rcu_head *head)
  103. {
  104. struct rsi *rsii = container_of(head, struct rsi, rcu_head);
  105. rsi_free(rsii);
  106. kfree(rsii);
  107. }
  108. static void rsi_put(struct kref *ref)
  109. {
  110. struct rsi *rsii = container_of(ref, struct rsi, h.ref);
  111. call_rcu(&rsii->rcu_head, rsi_free_rcu);
  112. }
  113. static inline int rsi_hash(struct rsi *item)
  114. {
  115. return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
  116. ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
  117. }
  118. static int rsi_match(struct cache_head *a, struct cache_head *b)
  119. {
  120. struct rsi *item = container_of(a, struct rsi, h);
  121. struct rsi *tmp = container_of(b, struct rsi, h);
  122. return netobj_equal(&item->in_handle, &tmp->in_handle) &&
  123. netobj_equal(&item->in_token, &tmp->in_token);
  124. }
  125. static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
  126. {
  127. dst->len = len;
  128. dst->data = (len ? kmemdup(src, len, GFP_KERNEL) : NULL);
  129. if (len && !dst->data)
  130. return -ENOMEM;
  131. return 0;
  132. }
  133. static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
  134. {
  135. return dup_to_netobj(dst, src->data, src->len);
  136. }
  137. static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
  138. {
  139. struct rsi *new = container_of(cnew, struct rsi, h);
  140. struct rsi *item = container_of(citem, struct rsi, h);
  141. new->out_handle.data = NULL;
  142. new->out_handle.len = 0;
  143. new->out_token.data = NULL;
  144. new->out_token.len = 0;
  145. new->in_handle.len = item->in_handle.len;
  146. item->in_handle.len = 0;
  147. new->in_token.len = item->in_token.len;
  148. item->in_token.len = 0;
  149. new->in_handle.data = item->in_handle.data;
  150. item->in_handle.data = NULL;
  151. new->in_token.data = item->in_token.data;
  152. item->in_token.data = NULL;
  153. }
  154. static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
  155. {
  156. struct rsi *new = container_of(cnew, struct rsi, h);
  157. struct rsi *item = container_of(citem, struct rsi, h);
  158. BUG_ON(new->out_handle.data || new->out_token.data);
  159. new->out_handle.len = item->out_handle.len;
  160. item->out_handle.len = 0;
  161. new->out_token.len = item->out_token.len;
  162. item->out_token.len = 0;
  163. new->out_handle.data = item->out_handle.data;
  164. item->out_handle.data = NULL;
  165. new->out_token.data = item->out_token.data;
  166. item->out_token.data = NULL;
  167. new->major_status = item->major_status;
  168. new->minor_status = item->minor_status;
  169. }
  170. static struct cache_head *rsi_alloc(void)
  171. {
  172. struct rsi *rsii = kmalloc_obj(*rsii);
  173. if (rsii)
  174. return &rsii->h;
  175. else
  176. return NULL;
  177. }
  178. static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
  179. {
  180. return sunrpc_cache_pipe_upcall_timeout(cd, h);
  181. }
  182. static void rsi_request(struct cache_detail *cd,
  183. struct cache_head *h,
  184. char **bpp, int *blen)
  185. {
  186. struct rsi *rsii = container_of(h, struct rsi, h);
  187. qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
  188. qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
  189. (*bpp)[-1] = '\n';
  190. WARN_ONCE(*blen < 0,
  191. "RPCSEC/GSS credential too large - please use gssproxy\n");
  192. }
  193. static int rsi_parse(struct cache_detail *cd,
  194. char *mesg, int mlen)
  195. {
  196. /* context token expiry major minor context token */
  197. char *buf = mesg;
  198. char *ep;
  199. int len;
  200. struct rsi rsii, *rsip = NULL;
  201. time64_t expiry;
  202. int status = -EINVAL;
  203. memset(&rsii, 0, sizeof(rsii));
  204. /* handle */
  205. len = qword_get(&mesg, buf, mlen);
  206. if (len < 0)
  207. goto out;
  208. status = -ENOMEM;
  209. if (dup_to_netobj(&rsii.in_handle, buf, len))
  210. goto out;
  211. /* token */
  212. len = qword_get(&mesg, buf, mlen);
  213. status = -EINVAL;
  214. if (len < 0)
  215. goto out;
  216. status = -ENOMEM;
  217. if (dup_to_netobj(&rsii.in_token, buf, len))
  218. goto out;
  219. rsip = rsi_lookup(cd, &rsii);
  220. if (!rsip)
  221. goto out;
  222. rsii.h.flags = 0;
  223. /* expiry */
  224. status = get_expiry(&mesg, &expiry);
  225. if (status)
  226. goto out;
  227. status = -EINVAL;
  228. /* major/minor */
  229. len = qword_get(&mesg, buf, mlen);
  230. if (len <= 0)
  231. goto out;
  232. rsii.major_status = simple_strtoul(buf, &ep, 10);
  233. if (*ep)
  234. goto out;
  235. len = qword_get(&mesg, buf, mlen);
  236. if (len <= 0)
  237. goto out;
  238. rsii.minor_status = simple_strtoul(buf, &ep, 10);
  239. if (*ep)
  240. goto out;
  241. /* out_handle */
  242. len = qword_get(&mesg, buf, mlen);
  243. if (len < 0)
  244. goto out;
  245. status = -ENOMEM;
  246. if (dup_to_netobj(&rsii.out_handle, buf, len))
  247. goto out;
  248. /* out_token */
  249. len = qword_get(&mesg, buf, mlen);
  250. status = -EINVAL;
  251. if (len < 0)
  252. goto out;
  253. status = -ENOMEM;
  254. if (dup_to_netobj(&rsii.out_token, buf, len))
  255. goto out;
  256. rsii.h.expiry_time = expiry;
  257. rsip = rsi_update(cd, &rsii, rsip);
  258. status = 0;
  259. out:
  260. rsi_free(&rsii);
  261. if (rsip)
  262. cache_put(&rsip->h, cd);
  263. else
  264. status = -ENOMEM;
  265. return status;
  266. }
  267. static const struct cache_detail rsi_cache_template = {
  268. .owner = THIS_MODULE,
  269. .hash_size = RSI_HASHMAX,
  270. .name = "auth.rpcsec.init",
  271. .cache_put = rsi_put,
  272. .cache_upcall = rsi_upcall,
  273. .cache_request = rsi_request,
  274. .cache_parse = rsi_parse,
  275. .match = rsi_match,
  276. .init = rsi_init,
  277. .update = update_rsi,
  278. .alloc = rsi_alloc,
  279. };
  280. static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
  281. {
  282. struct cache_head *ch;
  283. int hash = rsi_hash(item);
  284. ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
  285. if (ch)
  286. return container_of(ch, struct rsi, h);
  287. else
  288. return NULL;
  289. }
  290. static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old)
  291. {
  292. struct cache_head *ch;
  293. int hash = rsi_hash(new);
  294. ch = sunrpc_cache_update(cd, &new->h,
  295. &old->h, hash);
  296. if (ch)
  297. return container_of(ch, struct rsi, h);
  298. else
  299. return NULL;
  300. }
  301. /*
  302. * The rpcsec_context cache is used to store a context that is
  303. * used in data exchange.
  304. * The key is a context handle. The content is:
  305. * uid, gidlist, mechanism, service-set, mech-specific-data
  306. */
  307. #define RSC_HASHBITS 10
  308. #define RSC_HASHMAX (1<<RSC_HASHBITS)
  309. #define GSS_SEQ_WIN 128
  310. struct gss_svc_seq_data {
  311. /* highest seq number seen so far: */
  312. u32 sd_max;
  313. /* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
  314. * sd_win is nonzero iff sequence number i has been seen already: */
  315. unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
  316. spinlock_t sd_lock;
  317. };
  318. struct rsc {
  319. struct cache_head h;
  320. struct xdr_netobj handle;
  321. struct svc_cred cred;
  322. struct gss_svc_seq_data seqdata;
  323. struct gss_ctx *mechctx;
  324. struct rcu_head rcu_head;
  325. };
  326. static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old);
  327. static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item);
  328. static void rsc_free(struct rsc *rsci)
  329. {
  330. kfree(rsci->handle.data);
  331. if (rsci->mechctx)
  332. gss_delete_sec_context(&rsci->mechctx);
  333. free_svc_cred(&rsci->cred);
  334. }
  335. static void rsc_free_rcu(struct rcu_head *head)
  336. {
  337. struct rsc *rsci = container_of(head, struct rsc, rcu_head);
  338. kfree(rsci->handle.data);
  339. kfree(rsci);
  340. }
  341. static void rsc_put(struct kref *ref)
  342. {
  343. struct rsc *rsci = container_of(ref, struct rsc, h.ref);
  344. if (rsci->mechctx)
  345. gss_delete_sec_context(&rsci->mechctx);
  346. free_svc_cred(&rsci->cred);
  347. call_rcu(&rsci->rcu_head, rsc_free_rcu);
  348. }
  349. static inline int
  350. rsc_hash(struct rsc *rsci)
  351. {
  352. return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
  353. }
  354. static int
  355. rsc_match(struct cache_head *a, struct cache_head *b)
  356. {
  357. struct rsc *new = container_of(a, struct rsc, h);
  358. struct rsc *tmp = container_of(b, struct rsc, h);
  359. return netobj_equal(&new->handle, &tmp->handle);
  360. }
  361. static void
  362. rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
  363. {
  364. struct rsc *new = container_of(cnew, struct rsc, h);
  365. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  366. new->handle.len = tmp->handle.len;
  367. tmp->handle.len = 0;
  368. new->handle.data = tmp->handle.data;
  369. tmp->handle.data = NULL;
  370. new->mechctx = NULL;
  371. init_svc_cred(&new->cred);
  372. }
  373. static void
  374. update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
  375. {
  376. struct rsc *new = container_of(cnew, struct rsc, h);
  377. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  378. new->mechctx = tmp->mechctx;
  379. tmp->mechctx = NULL;
  380. memset(&new->seqdata, 0, sizeof(new->seqdata));
  381. spin_lock_init(&new->seqdata.sd_lock);
  382. new->cred = tmp->cred;
  383. init_svc_cred(&tmp->cred);
  384. }
  385. static struct cache_head *
  386. rsc_alloc(void)
  387. {
  388. struct rsc *rsci = kmalloc_obj(*rsci);
  389. if (rsci)
  390. return &rsci->h;
  391. else
  392. return NULL;
  393. }
  394. static int rsc_upcall(struct cache_detail *cd, struct cache_head *h)
  395. {
  396. return -EINVAL;
  397. }
  398. static int rsc_parse(struct cache_detail *cd,
  399. char *mesg, int mlen)
  400. {
  401. /* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
  402. char *buf = mesg;
  403. int id;
  404. int len, rv;
  405. struct rsc rsci, *rscp = NULL;
  406. time64_t expiry;
  407. int status = -EINVAL;
  408. struct gss_api_mech *gm = NULL;
  409. memset(&rsci, 0, sizeof(rsci));
  410. /* context handle */
  411. len = qword_get(&mesg, buf, mlen);
  412. if (len < 0) goto out;
  413. status = -ENOMEM;
  414. if (dup_to_netobj(&rsci.handle, buf, len))
  415. goto out;
  416. rsci.h.flags = 0;
  417. /* expiry */
  418. status = get_expiry(&mesg, &expiry);
  419. if (status)
  420. goto out;
  421. status = -EINVAL;
  422. rscp = rsc_lookup(cd, &rsci);
  423. if (!rscp)
  424. goto out;
  425. /* uid, or NEGATIVE */
  426. rv = get_int(&mesg, &id);
  427. if (rv == -EINVAL)
  428. goto out;
  429. if (rv == -ENOENT)
  430. set_bit(CACHE_NEGATIVE, &rsci.h.flags);
  431. else {
  432. int N, i;
  433. /*
  434. * NOTE: we skip uid_valid()/gid_valid() checks here:
  435. * instead, * -1 id's are later mapped to the
  436. * (export-specific) anonymous id by nfsd_setuser.
  437. *
  438. * (But supplementary gid's get no such special
  439. * treatment so are checked for validity here.)
  440. */
  441. /* uid */
  442. rsci.cred.cr_uid = make_kuid(current_user_ns(), id);
  443. /* gid */
  444. if (get_int(&mesg, &id))
  445. goto out;
  446. rsci.cred.cr_gid = make_kgid(current_user_ns(), id);
  447. /* number of additional gid's */
  448. if (get_int(&mesg, &N))
  449. goto out;
  450. if (N < 0 || N > NGROUPS_MAX)
  451. goto out;
  452. status = -ENOMEM;
  453. rsci.cred.cr_group_info = groups_alloc(N);
  454. if (rsci.cred.cr_group_info == NULL)
  455. goto out;
  456. /* gid's */
  457. status = -EINVAL;
  458. for (i=0; i<N; i++) {
  459. kgid_t kgid;
  460. if (get_int(&mesg, &id))
  461. goto out;
  462. kgid = make_kgid(current_user_ns(), id);
  463. if (!gid_valid(kgid))
  464. goto out;
  465. rsci.cred.cr_group_info->gid[i] = kgid;
  466. }
  467. groups_sort(rsci.cred.cr_group_info);
  468. /* mech name */
  469. len = qword_get(&mesg, buf, mlen);
  470. if (len < 0)
  471. goto out;
  472. gm = rsci.cred.cr_gss_mech = gss_mech_get_by_name(buf);
  473. status = -EOPNOTSUPP;
  474. if (!gm)
  475. goto out;
  476. status = -EINVAL;
  477. /* mech-specific data: */
  478. len = qword_get(&mesg, buf, mlen);
  479. if (len < 0)
  480. goto out;
  481. status = gss_import_sec_context(buf, len, gm, &rsci.mechctx,
  482. NULL, GFP_KERNEL);
  483. if (status)
  484. goto out;
  485. /* get client name */
  486. len = qword_get(&mesg, buf, mlen);
  487. if (len > 0) {
  488. rsci.cred.cr_principal = kstrdup(buf, GFP_KERNEL);
  489. if (!rsci.cred.cr_principal) {
  490. status = -ENOMEM;
  491. goto out;
  492. }
  493. }
  494. }
  495. rsci.h.expiry_time = expiry;
  496. rscp = rsc_update(cd, &rsci, rscp);
  497. status = 0;
  498. out:
  499. rsc_free(&rsci);
  500. if (rscp)
  501. cache_put(&rscp->h, cd);
  502. else
  503. status = -ENOMEM;
  504. return status;
  505. }
  506. static const struct cache_detail rsc_cache_template = {
  507. .owner = THIS_MODULE,
  508. .hash_size = RSC_HASHMAX,
  509. .name = "auth.rpcsec.context",
  510. .cache_put = rsc_put,
  511. .cache_upcall = rsc_upcall,
  512. .cache_parse = rsc_parse,
  513. .match = rsc_match,
  514. .init = rsc_init,
  515. .update = update_rsc,
  516. .alloc = rsc_alloc,
  517. };
  518. static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item)
  519. {
  520. struct cache_head *ch;
  521. int hash = rsc_hash(item);
  522. ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
  523. if (ch)
  524. return container_of(ch, struct rsc, h);
  525. else
  526. return NULL;
  527. }
  528. static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old)
  529. {
  530. struct cache_head *ch;
  531. int hash = rsc_hash(new);
  532. ch = sunrpc_cache_update(cd, &new->h,
  533. &old->h, hash);
  534. if (ch)
  535. return container_of(ch, struct rsc, h);
  536. else
  537. return NULL;
  538. }
  539. static struct rsc *
  540. gss_svc_searchbyctx(struct cache_detail *cd, struct xdr_netobj *handle)
  541. {
  542. struct rsc rsci;
  543. struct rsc *found;
  544. memset(&rsci, 0, sizeof(rsci));
  545. if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
  546. return NULL;
  547. found = rsc_lookup(cd, &rsci);
  548. rsc_free(&rsci);
  549. if (!found)
  550. return NULL;
  551. if (cache_check(cd, &found->h, NULL))
  552. return NULL;
  553. return found;
  554. }
  555. /**
  556. * gss_check_seq_num - GSS sequence number window check
  557. * @rqstp: RPC Call to use when reporting errors
  558. * @rsci: cached GSS context state (updated on return)
  559. * @seq_num: sequence number to check
  560. *
  561. * Implements sequence number algorithm as specified in
  562. * RFC 2203, Section 5.3.3.1. "Context Management".
  563. *
  564. * Return values:
  565. * %true: @rqstp's GSS sequence number is inside the window
  566. * %false: @rqstp's GSS sequence number is outside the window
  567. */
  568. static bool gss_check_seq_num(const struct svc_rqst *rqstp, struct rsc *rsci,
  569. u32 seq_num)
  570. {
  571. struct gss_svc_seq_data *sd = &rsci->seqdata;
  572. bool result = false;
  573. spin_lock(&sd->sd_lock);
  574. if (seq_num > sd->sd_max) {
  575. if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
  576. memset(sd->sd_win, 0, sizeof(sd->sd_win));
  577. sd->sd_max = seq_num;
  578. } else while (sd->sd_max < seq_num) {
  579. sd->sd_max++;
  580. __clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
  581. }
  582. __set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
  583. goto ok;
  584. } else if (seq_num + GSS_SEQ_WIN <= sd->sd_max) {
  585. goto toolow;
  586. }
  587. if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
  588. goto alreadyseen;
  589. ok:
  590. result = true;
  591. out:
  592. spin_unlock(&sd->sd_lock);
  593. return result;
  594. toolow:
  595. trace_rpcgss_svc_seqno_low(rqstp, seq_num,
  596. sd->sd_max - GSS_SEQ_WIN,
  597. sd->sd_max);
  598. goto out;
  599. alreadyseen:
  600. trace_rpcgss_svc_seqno_seen(rqstp, seq_num);
  601. goto out;
  602. }
  603. /*
  604. * Decode and verify a Call's verifier field. For RPC_AUTH_GSS Calls,
  605. * the body of this field contains a variable length checksum.
  606. *
  607. * GSS-specific auth_stat values are mandated by RFC 2203 Section
  608. * 5.3.3.3.
  609. */
  610. static int
  611. svcauth_gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
  612. __be32 *rpcstart, struct rpc_gss_wire_cred *gc)
  613. {
  614. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  615. struct gss_ctx *ctx_id = rsci->mechctx;
  616. u32 flavor, maj_stat;
  617. struct xdr_buf rpchdr;
  618. struct xdr_netobj checksum;
  619. struct kvec iov;
  620. /*
  621. * Compute the checksum of the incoming Call from the
  622. * XID field to credential field:
  623. */
  624. iov.iov_base = rpcstart;
  625. iov.iov_len = (u8 *)xdr->p - (u8 *)rpcstart;
  626. xdr_buf_from_iov(&iov, &rpchdr);
  627. /* Call's verf field: */
  628. if (xdr_stream_decode_opaque_auth(xdr, &flavor,
  629. (void **)&checksum.data,
  630. &checksum.len) < 0) {
  631. rqstp->rq_auth_stat = rpc_autherr_badverf;
  632. return SVC_DENIED;
  633. }
  634. if (flavor != RPC_AUTH_GSS || checksum.len < XDR_UNIT) {
  635. rqstp->rq_auth_stat = rpc_autherr_badverf;
  636. return SVC_DENIED;
  637. }
  638. if (rqstp->rq_deferred)
  639. return SVC_OK;
  640. maj_stat = gss_verify_mic(ctx_id, &rpchdr, &checksum);
  641. if (maj_stat != GSS_S_COMPLETE) {
  642. trace_rpcgss_svc_mic(rqstp, maj_stat);
  643. rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
  644. return SVC_DENIED;
  645. }
  646. if (gc->gc_seq > MAXSEQ) {
  647. trace_rpcgss_svc_seqno_large(rqstp, gc->gc_seq);
  648. rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
  649. return SVC_DENIED;
  650. }
  651. if (!gss_check_seq_num(rqstp, rsci, gc->gc_seq))
  652. return SVC_DROP;
  653. return SVC_OK;
  654. }
  655. /*
  656. * Construct and encode a Reply's verifier field. The verifier's body
  657. * field contains a variable-length checksum of the GSS sequence
  658. * number.
  659. */
  660. static bool
  661. svcauth_gss_encode_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
  662. {
  663. struct gss_svc_data *gsd = rqstp->rq_auth_data;
  664. u32 maj_stat;
  665. struct xdr_buf verf_data;
  666. struct xdr_netobj checksum;
  667. struct kvec iov;
  668. gsd->gsd_seq_num = cpu_to_be32(seq);
  669. iov.iov_base = &gsd->gsd_seq_num;
  670. iov.iov_len = XDR_UNIT;
  671. xdr_buf_from_iov(&iov, &verf_data);
  672. checksum.data = gsd->gsd_scratch;
  673. maj_stat = gss_get_mic(ctx_id, &verf_data, &checksum);
  674. if (maj_stat != GSS_S_COMPLETE)
  675. goto bad_mic;
  676. return xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream, RPC_AUTH_GSS,
  677. checksum.data, checksum.len) > 0;
  678. bad_mic:
  679. trace_rpcgss_svc_get_mic(rqstp, maj_stat);
  680. return false;
  681. }
  682. struct gss_domain {
  683. struct auth_domain h;
  684. u32 pseudoflavor;
  685. };
  686. static struct auth_domain *
  687. find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
  688. {
  689. char *name;
  690. name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
  691. if (!name)
  692. return NULL;
  693. return auth_domain_find(name);
  694. }
  695. static struct auth_ops svcauthops_gss;
  696. u32 svcauth_gss_flavor(struct auth_domain *dom)
  697. {
  698. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  699. return gd->pseudoflavor;
  700. }
  701. EXPORT_SYMBOL_GPL(svcauth_gss_flavor);
  702. struct auth_domain *
  703. svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
  704. {
  705. struct gss_domain *new;
  706. struct auth_domain *test;
  707. int stat = -ENOMEM;
  708. new = kmalloc_obj(*new);
  709. if (!new)
  710. goto out;
  711. kref_init(&new->h.ref);
  712. new->h.name = kstrdup(name, GFP_KERNEL);
  713. if (!new->h.name)
  714. goto out_free_dom;
  715. new->h.flavour = &svcauthops_gss;
  716. new->pseudoflavor = pseudoflavor;
  717. test = auth_domain_lookup(name, &new->h);
  718. if (test != &new->h) {
  719. pr_warn("svc: duplicate registration of gss pseudo flavour %s.\n",
  720. name);
  721. stat = -EADDRINUSE;
  722. auth_domain_put(test);
  723. goto out_free_name;
  724. }
  725. return test;
  726. out_free_name:
  727. kfree(new->h.name);
  728. out_free_dom:
  729. kfree(new);
  730. out:
  731. return ERR_PTR(stat);
  732. }
  733. EXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);
  734. /*
  735. * RFC 2203, Section 5.3.2.2
  736. *
  737. * struct rpc_gss_integ_data {
  738. * opaque databody_integ<>;
  739. * opaque checksum<>;
  740. * };
  741. *
  742. * struct rpc_gss_data_t {
  743. * unsigned int seq_num;
  744. * proc_req_arg_t arg;
  745. * };
  746. */
  747. static noinline_for_stack int
  748. svcauth_gss_unwrap_integ(struct svc_rqst *rqstp, u32 seq, struct gss_ctx *ctx)
  749. {
  750. struct gss_svc_data *gsd = rqstp->rq_auth_data;
  751. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  752. u32 len, offset, seq_num, maj_stat;
  753. struct xdr_buf *buf = xdr->buf;
  754. struct xdr_buf databody_integ;
  755. struct xdr_netobj checksum;
  756. /* Did we already verify the signature on the original pass through? */
  757. if (rqstp->rq_deferred)
  758. return 0;
  759. if (xdr_stream_decode_u32(xdr, &len) < 0)
  760. goto unwrap_failed;
  761. if (len & 3)
  762. goto unwrap_failed;
  763. offset = xdr_stream_pos(xdr);
  764. if (xdr_buf_subsegment(buf, &databody_integ, offset, len))
  765. goto unwrap_failed;
  766. /*
  767. * The xdr_stream now points to the @seq_num field. The next
  768. * XDR data item is the @arg field, which contains the clear
  769. * text RPC program payload. The checksum, which follows the
  770. * @arg field, is located and decoded without updating the
  771. * xdr_stream.
  772. */
  773. offset += len;
  774. if (xdr_decode_word(buf, offset, &checksum.len))
  775. goto unwrap_failed;
  776. if (checksum.len > sizeof(gsd->gsd_scratch))
  777. goto unwrap_failed;
  778. checksum.data = gsd->gsd_scratch;
  779. if (read_bytes_from_xdr_buf(buf, offset + XDR_UNIT, checksum.data,
  780. checksum.len))
  781. goto unwrap_failed;
  782. maj_stat = gss_verify_mic(ctx, &databody_integ, &checksum);
  783. if (maj_stat != GSS_S_COMPLETE)
  784. goto bad_mic;
  785. /* The received seqno is protected by the checksum. */
  786. if (xdr_stream_decode_u32(xdr, &seq_num) < 0)
  787. goto unwrap_failed;
  788. if (seq_num != seq)
  789. goto bad_seqno;
  790. xdr_truncate_decode(xdr, XDR_UNIT + checksum.len);
  791. return 0;
  792. unwrap_failed:
  793. trace_rpcgss_svc_unwrap_failed(rqstp);
  794. return -EINVAL;
  795. bad_seqno:
  796. trace_rpcgss_svc_seqno_bad(rqstp, seq, seq_num);
  797. return -EINVAL;
  798. bad_mic:
  799. trace_rpcgss_svc_mic(rqstp, maj_stat);
  800. return -EINVAL;
  801. }
  802. /*
  803. * RFC 2203, Section 5.3.2.3
  804. *
  805. * struct rpc_gss_priv_data {
  806. * opaque databody_priv<>
  807. * };
  808. *
  809. * struct rpc_gss_data_t {
  810. * unsigned int seq_num;
  811. * proc_req_arg_t arg;
  812. * };
  813. */
  814. static noinline_for_stack int
  815. svcauth_gss_unwrap_priv(struct svc_rqst *rqstp, u32 seq, struct gss_ctx *ctx)
  816. {
  817. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  818. u32 len, maj_stat, seq_num, offset;
  819. struct xdr_buf *buf = xdr->buf;
  820. unsigned int saved_len;
  821. if (xdr_stream_decode_u32(xdr, &len) < 0)
  822. goto unwrap_failed;
  823. if (rqstp->rq_deferred) {
  824. /* Already decrypted last time through! The sequence number
  825. * check at out_seq is unnecessary but harmless: */
  826. goto out_seq;
  827. }
  828. if (len > xdr_stream_remaining(xdr))
  829. goto unwrap_failed;
  830. offset = xdr_stream_pos(xdr);
  831. saved_len = buf->len;
  832. maj_stat = gss_unwrap(ctx, offset, offset + len, buf);
  833. if (maj_stat != GSS_S_COMPLETE)
  834. goto bad_unwrap;
  835. xdr->nwords -= XDR_QUADLEN(saved_len - buf->len);
  836. out_seq:
  837. /* gss_unwrap() decrypted the sequence number. */
  838. if (xdr_stream_decode_u32(xdr, &seq_num) < 0)
  839. goto unwrap_failed;
  840. if (seq_num != seq)
  841. goto bad_seqno;
  842. return 0;
  843. unwrap_failed:
  844. trace_rpcgss_svc_unwrap_failed(rqstp);
  845. return -EINVAL;
  846. bad_seqno:
  847. trace_rpcgss_svc_seqno_bad(rqstp, seq, seq_num);
  848. return -EINVAL;
  849. bad_unwrap:
  850. trace_rpcgss_svc_unwrap(rqstp, maj_stat);
  851. return -EINVAL;
  852. }
  853. static enum svc_auth_status
  854. svcauth_gss_set_client(struct svc_rqst *rqstp)
  855. {
  856. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  857. struct rsc *rsci = svcdata->rsci;
  858. struct rpc_gss_wire_cred *gc = &svcdata->clcred;
  859. int stat;
  860. rqstp->rq_auth_stat = rpc_autherr_badcred;
  861. /*
  862. * A gss export can be specified either by:
  863. * export *(sec=krb5,rw)
  864. * or by
  865. * export gss/krb5(rw)
  866. * The latter is deprecated; but for backwards compatibility reasons
  867. * the nfsd code will still fall back on trying it if the former
  868. * doesn't work; so we try to make both available to nfsd, below.
  869. */
  870. rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
  871. if (rqstp->rq_gssclient == NULL)
  872. return SVC_DENIED;
  873. stat = svcauth_unix_set_client(rqstp);
  874. if (stat == SVC_DROP || stat == SVC_CLOSE)
  875. return stat;
  876. rqstp->rq_auth_stat = rpc_auth_ok;
  877. return SVC_OK;
  878. }
  879. static bool
  880. svcauth_gss_proc_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
  881. struct xdr_netobj *out_handle, int *major_status,
  882. u32 seq_num)
  883. {
  884. struct xdr_stream *xdr = &rqstp->rq_res_stream;
  885. struct rsc *rsci;
  886. bool rc;
  887. if (*major_status != GSS_S_COMPLETE)
  888. goto null_verifier;
  889. rsci = gss_svc_searchbyctx(cd, out_handle);
  890. if (rsci == NULL) {
  891. *major_status = GSS_S_NO_CONTEXT;
  892. goto null_verifier;
  893. }
  894. rc = svcauth_gss_encode_verf(rqstp, rsci->mechctx, seq_num);
  895. cache_put(&rsci->h, cd);
  896. return rc;
  897. null_verifier:
  898. return xdr_stream_encode_opaque_auth(xdr, RPC_AUTH_NULL, NULL, 0) > 0;
  899. }
  900. static void gss_free_in_token_pages(struct gssp_in_token *in_token)
  901. {
  902. int i;
  903. i = 0;
  904. while (in_token->pages[i])
  905. put_page(in_token->pages[i++]);
  906. kfree(in_token->pages);
  907. in_token->pages = NULL;
  908. }
  909. static int gss_read_proxy_verf(struct svc_rqst *rqstp,
  910. struct rpc_gss_wire_cred *gc,
  911. struct xdr_netobj *in_handle,
  912. struct gssp_in_token *in_token)
  913. {
  914. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  915. unsigned int length, pgto_offs, pgfrom_offs;
  916. int pages, i, pgto, pgfrom;
  917. size_t to_offs, from_offs;
  918. u32 inlen;
  919. if (dup_netobj(in_handle, &gc->gc_ctx))
  920. return SVC_CLOSE;
  921. /*
  922. * RFC 2203 Section 5.2.2
  923. *
  924. * struct rpc_gss_init_arg {
  925. * opaque gss_token<>;
  926. * };
  927. */
  928. if (xdr_stream_decode_u32(xdr, &inlen) < 0)
  929. goto out_denied_free;
  930. if (inlen > xdr_stream_remaining(xdr))
  931. goto out_denied_free;
  932. pages = DIV_ROUND_UP(inlen, PAGE_SIZE);
  933. in_token->pages = kzalloc_objs(struct page *, pages + 1);
  934. if (!in_token->pages)
  935. goto out_denied_free;
  936. in_token->page_base = 0;
  937. in_token->page_len = inlen;
  938. for (i = 0; i < pages; i++) {
  939. in_token->pages[i] = alloc_page(GFP_KERNEL);
  940. if (!in_token->pages[i]) {
  941. gss_free_in_token_pages(in_token);
  942. goto out_denied_free;
  943. }
  944. }
  945. length = min_t(unsigned int, inlen, (char *)xdr->end - (char *)xdr->p);
  946. if (length)
  947. memcpy(page_address(in_token->pages[0]), xdr->p, length);
  948. inlen -= length;
  949. to_offs = length;
  950. from_offs = rqstp->rq_arg.page_base;
  951. while (inlen) {
  952. pgto = to_offs >> PAGE_SHIFT;
  953. pgfrom = from_offs >> PAGE_SHIFT;
  954. pgto_offs = to_offs & ~PAGE_MASK;
  955. pgfrom_offs = from_offs & ~PAGE_MASK;
  956. length = min_t(unsigned int, inlen,
  957. min_t(unsigned int, PAGE_SIZE - pgto_offs,
  958. PAGE_SIZE - pgfrom_offs));
  959. memcpy(page_address(in_token->pages[pgto]) + pgto_offs,
  960. page_address(rqstp->rq_arg.pages[pgfrom]) + pgfrom_offs,
  961. length);
  962. to_offs += length;
  963. from_offs += length;
  964. inlen -= length;
  965. }
  966. return 0;
  967. out_denied_free:
  968. kfree(in_handle->data);
  969. return SVC_DENIED;
  970. }
  971. /*
  972. * RFC 2203, Section 5.2.3.1.
  973. *
  974. * struct rpc_gss_init_res {
  975. * opaque handle<>;
  976. * unsigned int gss_major;
  977. * unsigned int gss_minor;
  978. * unsigned int seq_window;
  979. * opaque gss_token<>;
  980. * };
  981. */
  982. static bool
  983. svcxdr_encode_gss_init_res(struct xdr_stream *xdr,
  984. struct xdr_netobj *handle,
  985. struct xdr_netobj *gss_token,
  986. unsigned int major_status,
  987. unsigned int minor_status, u32 seq_num)
  988. {
  989. if (xdr_stream_encode_opaque(xdr, handle->data, handle->len) < 0)
  990. return false;
  991. if (xdr_stream_encode_u32(xdr, major_status) < 0)
  992. return false;
  993. if (xdr_stream_encode_u32(xdr, minor_status) < 0)
  994. return false;
  995. if (xdr_stream_encode_u32(xdr, seq_num) < 0)
  996. return false;
  997. if (xdr_stream_encode_opaque(xdr, gss_token->data, gss_token->len) < 0)
  998. return false;
  999. return true;
  1000. }
  1001. /*
  1002. * Having read the cred already and found we're in the context
  1003. * initiation case, read the verifier and initiate (or check the results
  1004. * of) upcalls to userspace for help with context initiation. If
  1005. * the upcall results are available, write the verifier and result.
  1006. * Otherwise, drop the request pending an answer to the upcall.
  1007. */
  1008. static int
  1009. svcauth_gss_legacy_init(struct svc_rqst *rqstp,
  1010. struct rpc_gss_wire_cred *gc)
  1011. {
  1012. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  1013. struct rsi *rsip, rsikey;
  1014. __be32 *p;
  1015. u32 len;
  1016. int ret;
  1017. struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
  1018. memset(&rsikey, 0, sizeof(rsikey));
  1019. if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))
  1020. return SVC_CLOSE;
  1021. /*
  1022. * RFC 2203 Section 5.2.2
  1023. *
  1024. * struct rpc_gss_init_arg {
  1025. * opaque gss_token<>;
  1026. * };
  1027. */
  1028. if (xdr_stream_decode_u32(xdr, &len) < 0) {
  1029. kfree(rsikey.in_handle.data);
  1030. return SVC_DENIED;
  1031. }
  1032. p = xdr_inline_decode(xdr, len);
  1033. if (!p) {
  1034. kfree(rsikey.in_handle.data);
  1035. return SVC_DENIED;
  1036. }
  1037. rsikey.in_token.data = kmalloc(len, GFP_KERNEL);
  1038. if (ZERO_OR_NULL_PTR(rsikey.in_token.data)) {
  1039. kfree(rsikey.in_handle.data);
  1040. return SVC_CLOSE;
  1041. }
  1042. memcpy(rsikey.in_token.data, p, len);
  1043. rsikey.in_token.len = len;
  1044. /* Perform upcall, or find upcall result: */
  1045. rsip = rsi_lookup(sn->rsi_cache, &rsikey);
  1046. rsi_free(&rsikey);
  1047. if (!rsip)
  1048. return SVC_CLOSE;
  1049. if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
  1050. /* No upcall result: */
  1051. return SVC_CLOSE;
  1052. ret = SVC_CLOSE;
  1053. if (!svcauth_gss_proc_init_verf(sn->rsc_cache, rqstp, &rsip->out_handle,
  1054. &rsip->major_status, GSS_SEQ_WIN))
  1055. goto out;
  1056. if (!svcxdr_set_accept_stat(rqstp))
  1057. goto out;
  1058. if (!svcxdr_encode_gss_init_res(&rqstp->rq_res_stream, &rsip->out_handle,
  1059. &rsip->out_token, rsip->major_status,
  1060. rsip->minor_status, GSS_SEQ_WIN))
  1061. goto out;
  1062. ret = SVC_COMPLETE;
  1063. out:
  1064. cache_put(&rsip->h, sn->rsi_cache);
  1065. return ret;
  1066. }
  1067. static int gss_proxy_save_rsc(struct cache_detail *cd,
  1068. struct gssp_upcall_data *ud,
  1069. uint64_t *handle)
  1070. {
  1071. struct rsc rsci, *rscp = NULL;
  1072. static atomic64_t ctxhctr;
  1073. long long ctxh;
  1074. struct gss_api_mech *gm = NULL;
  1075. time64_t expiry;
  1076. int status;
  1077. memset(&rsci, 0, sizeof(rsci));
  1078. /* context handle */
  1079. status = -ENOMEM;
  1080. /* the handle needs to be just a unique id,
  1081. * use a static counter */
  1082. ctxh = atomic64_inc_return(&ctxhctr);
  1083. /* make a copy for the caller */
  1084. *handle = ctxh;
  1085. /* make a copy for the rsc cache */
  1086. if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
  1087. goto out;
  1088. rscp = rsc_lookup(cd, &rsci);
  1089. if (!rscp)
  1090. goto out;
  1091. /* creds */
  1092. if (!ud->found_creds) {
  1093. /* userspace seem buggy, we should always get at least a
  1094. * mapping to nobody */
  1095. goto out;
  1096. } else {
  1097. struct timespec64 boot;
  1098. /* steal creds */
  1099. rsci.cred = ud->creds;
  1100. memset(&ud->creds, 0, sizeof(struct svc_cred));
  1101. status = -EOPNOTSUPP;
  1102. /* get mech handle from OID */
  1103. gm = gss_mech_get_by_OID(&ud->mech_oid);
  1104. if (!gm)
  1105. goto out;
  1106. rsci.cred.cr_gss_mech = gm;
  1107. status = -EINVAL;
  1108. /* mech-specific data: */
  1109. status = gss_import_sec_context(ud->out_handle.data,
  1110. ud->out_handle.len,
  1111. gm, &rsci.mechctx,
  1112. &expiry, GFP_KERNEL);
  1113. if (status)
  1114. goto out;
  1115. getboottime64(&boot);
  1116. expiry -= boot.tv_sec;
  1117. }
  1118. rsci.h.expiry_time = expiry;
  1119. rscp = rsc_update(cd, &rsci, rscp);
  1120. status = 0;
  1121. out:
  1122. rsc_free(&rsci);
  1123. if (rscp)
  1124. cache_put(&rscp->h, cd);
  1125. else
  1126. status = -ENOMEM;
  1127. return status;
  1128. }
  1129. static int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
  1130. struct rpc_gss_wire_cred *gc)
  1131. {
  1132. struct xdr_netobj cli_handle;
  1133. struct gssp_upcall_data ud;
  1134. uint64_t handle;
  1135. int status;
  1136. int ret;
  1137. struct net *net = SVC_NET(rqstp);
  1138. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1139. memset(&ud, 0, sizeof(ud));
  1140. ret = gss_read_proxy_verf(rqstp, gc, &ud.in_handle, &ud.in_token);
  1141. if (ret)
  1142. return ret;
  1143. ret = SVC_CLOSE;
  1144. /* Perform synchronous upcall to gss-proxy */
  1145. status = gssp_accept_sec_context_upcall(net, &ud);
  1146. if (status)
  1147. goto out;
  1148. trace_rpcgss_svc_accept_upcall(rqstp, ud.major_status, ud.minor_status);
  1149. switch (ud.major_status) {
  1150. case GSS_S_CONTINUE_NEEDED:
  1151. cli_handle = ud.out_handle;
  1152. break;
  1153. case GSS_S_COMPLETE:
  1154. status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
  1155. if (status)
  1156. goto out;
  1157. cli_handle.data = (u8 *)&handle;
  1158. cli_handle.len = sizeof(handle);
  1159. break;
  1160. default:
  1161. goto out;
  1162. }
  1163. if (!svcauth_gss_proc_init_verf(sn->rsc_cache, rqstp, &cli_handle,
  1164. &ud.major_status, GSS_SEQ_WIN))
  1165. goto out;
  1166. if (!svcxdr_set_accept_stat(rqstp))
  1167. goto out;
  1168. if (!svcxdr_encode_gss_init_res(&rqstp->rq_res_stream, &cli_handle,
  1169. &ud.out_token, ud.major_status,
  1170. ud.minor_status, GSS_SEQ_WIN))
  1171. goto out;
  1172. ret = SVC_COMPLETE;
  1173. out:
  1174. gss_free_in_token_pages(&ud.in_token);
  1175. gssp_free_upcall_data(&ud);
  1176. return ret;
  1177. }
  1178. /*
  1179. * Try to set the sn->use_gss_proxy variable to a new value. We only allow
  1180. * it to be changed if it's currently undefined (-1). If it's any other value
  1181. * then return -EBUSY unless the type wouldn't have changed anyway.
  1182. */
  1183. static int set_gss_proxy(struct net *net, int type)
  1184. {
  1185. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1186. int ret;
  1187. WARN_ON_ONCE(type != 0 && type != 1);
  1188. ret = cmpxchg(&sn->use_gss_proxy, -1, type);
  1189. if (ret != -1 && ret != type)
  1190. return -EBUSY;
  1191. return 0;
  1192. }
  1193. static bool use_gss_proxy(struct net *net)
  1194. {
  1195. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1196. /* If use_gss_proxy is still undefined, then try to disable it */
  1197. if (sn->use_gss_proxy == -1)
  1198. set_gss_proxy(net, 0);
  1199. return sn->use_gss_proxy;
  1200. }
  1201. static noinline_for_stack int
  1202. svcauth_gss_proc_init(struct svc_rqst *rqstp, struct rpc_gss_wire_cred *gc)
  1203. {
  1204. struct xdr_stream *xdr = &rqstp->rq_arg_stream;
  1205. u32 flavor, len;
  1206. void *body;
  1207. /* Call's verf field: */
  1208. if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
  1209. return SVC_GARBAGE;
  1210. if (flavor != RPC_AUTH_NULL || len != 0) {
  1211. rqstp->rq_auth_stat = rpc_autherr_badverf;
  1212. return SVC_DENIED;
  1213. }
  1214. if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0) {
  1215. rqstp->rq_auth_stat = rpc_autherr_badcred;
  1216. return SVC_DENIED;
  1217. }
  1218. if (!use_gss_proxy(SVC_NET(rqstp)))
  1219. return svcauth_gss_legacy_init(rqstp, gc);
  1220. return svcauth_gss_proxy_init(rqstp, gc);
  1221. }
  1222. #ifdef CONFIG_PROC_FS
  1223. static ssize_t write_gssp(struct file *file, const char __user *buf,
  1224. size_t count, loff_t *ppos)
  1225. {
  1226. struct net *net = pde_data(file_inode(file));
  1227. char tbuf[20];
  1228. unsigned long i;
  1229. int res;
  1230. if (*ppos || count > sizeof(tbuf)-1)
  1231. return -EINVAL;
  1232. if (copy_from_user(tbuf, buf, count))
  1233. return -EFAULT;
  1234. tbuf[count] = 0;
  1235. res = kstrtoul(tbuf, 0, &i);
  1236. if (res)
  1237. return res;
  1238. if (i != 1)
  1239. return -EINVAL;
  1240. res = set_gssp_clnt(net);
  1241. if (res)
  1242. return res;
  1243. res = set_gss_proxy(net, 1);
  1244. if (res)
  1245. return res;
  1246. return count;
  1247. }
  1248. static ssize_t read_gssp(struct file *file, char __user *buf,
  1249. size_t count, loff_t *ppos)
  1250. {
  1251. struct net *net = pde_data(file_inode(file));
  1252. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1253. unsigned long p = *ppos;
  1254. char tbuf[10];
  1255. size_t len;
  1256. snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
  1257. len = strlen(tbuf);
  1258. if (p >= len)
  1259. return 0;
  1260. len -= p;
  1261. if (len > count)
  1262. len = count;
  1263. if (copy_to_user(buf, (void *)(tbuf+p), len))
  1264. return -EFAULT;
  1265. *ppos += len;
  1266. return len;
  1267. }
  1268. static const struct proc_ops use_gss_proxy_proc_ops = {
  1269. .proc_open = nonseekable_open,
  1270. .proc_write = write_gssp,
  1271. .proc_read = read_gssp,
  1272. };
  1273. static int create_use_gss_proxy_proc_entry(struct net *net)
  1274. {
  1275. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1276. struct proc_dir_entry **p = &sn->use_gssp_proc;
  1277. sn->use_gss_proxy = -1;
  1278. *p = proc_create_data("use-gss-proxy", S_IFREG | 0600,
  1279. sn->proc_net_rpc,
  1280. &use_gss_proxy_proc_ops, net);
  1281. if (!*p)
  1282. return -ENOMEM;
  1283. init_gssp_clnt(sn);
  1284. return 0;
  1285. }
  1286. static void destroy_use_gss_proxy_proc_entry(struct net *net)
  1287. {
  1288. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1289. if (sn->use_gssp_proc) {
  1290. remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
  1291. clear_gssp_clnt(sn);
  1292. }
  1293. }
  1294. static ssize_t read_gss_krb5_enctypes(struct file *file, char __user *buf,
  1295. size_t count, loff_t *ppos)
  1296. {
  1297. struct rpcsec_gss_oid oid = {
  1298. .len = 9,
  1299. .data = "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02",
  1300. };
  1301. struct gss_api_mech *mech;
  1302. ssize_t ret;
  1303. mech = gss_mech_get_by_OID(&oid);
  1304. if (!mech)
  1305. return 0;
  1306. if (!mech->gm_upcall_enctypes) {
  1307. gss_mech_put(mech);
  1308. return 0;
  1309. }
  1310. ret = simple_read_from_buffer(buf, count, ppos,
  1311. mech->gm_upcall_enctypes,
  1312. strlen(mech->gm_upcall_enctypes));
  1313. gss_mech_put(mech);
  1314. return ret;
  1315. }
  1316. static const struct proc_ops gss_krb5_enctypes_proc_ops = {
  1317. .proc_open = nonseekable_open,
  1318. .proc_read = read_gss_krb5_enctypes,
  1319. };
  1320. static int create_krb5_enctypes_proc_entry(struct net *net)
  1321. {
  1322. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1323. sn->gss_krb5_enctypes =
  1324. proc_create_data("gss_krb5_enctypes", S_IFREG | 0444,
  1325. sn->proc_net_rpc, &gss_krb5_enctypes_proc_ops,
  1326. net);
  1327. return sn->gss_krb5_enctypes ? 0 : -ENOMEM;
  1328. }
  1329. static void destroy_krb5_enctypes_proc_entry(struct net *net)
  1330. {
  1331. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1332. if (sn->gss_krb5_enctypes)
  1333. remove_proc_entry("gss_krb5_enctypes", sn->proc_net_rpc);
  1334. }
  1335. #else /* CONFIG_PROC_FS */
  1336. static int create_use_gss_proxy_proc_entry(struct net *net)
  1337. {
  1338. return 0;
  1339. }
  1340. static void destroy_use_gss_proxy_proc_entry(struct net *net) {}
  1341. static int create_krb5_enctypes_proc_entry(struct net *net)
  1342. {
  1343. return 0;
  1344. }
  1345. static void destroy_krb5_enctypes_proc_entry(struct net *net) {}
  1346. #endif /* CONFIG_PROC_FS */
  1347. /*
  1348. * The Call's credential body should contain a struct rpc_gss_cred_t.
  1349. *
  1350. * RFC 2203 Section 5
  1351. *
  1352. * struct rpc_gss_cred_t {
  1353. * union switch (unsigned int version) {
  1354. * case RPCSEC_GSS_VERS_1:
  1355. * struct {
  1356. * rpc_gss_proc_t gss_proc;
  1357. * unsigned int seq_num;
  1358. * rpc_gss_service_t service;
  1359. * opaque handle<>;
  1360. * } rpc_gss_cred_vers_1_t;
  1361. * }
  1362. * };
  1363. */
  1364. static bool
  1365. svcauth_gss_decode_credbody(struct xdr_stream *xdr,
  1366. struct rpc_gss_wire_cred *gc,
  1367. __be32 **rpcstart)
  1368. {
  1369. ssize_t handle_len;
  1370. u32 body_len;
  1371. __be32 *p;
  1372. p = xdr_inline_decode(xdr, XDR_UNIT);
  1373. if (!p)
  1374. return false;
  1375. /*
  1376. * start of rpc packet is 7 u32's back from here:
  1377. * xid direction rpcversion prog vers proc flavour
  1378. */
  1379. *rpcstart = p - 7;
  1380. body_len = be32_to_cpup(p);
  1381. if (body_len > RPC_MAX_AUTH_SIZE)
  1382. return false;
  1383. /* struct rpc_gss_cred_t */
  1384. if (xdr_stream_decode_u32(xdr, &gc->gc_v) < 0)
  1385. return false;
  1386. if (xdr_stream_decode_u32(xdr, &gc->gc_proc) < 0)
  1387. return false;
  1388. if (xdr_stream_decode_u32(xdr, &gc->gc_seq) < 0)
  1389. return false;
  1390. if (xdr_stream_decode_u32(xdr, &gc->gc_svc) < 0)
  1391. return false;
  1392. handle_len = xdr_stream_decode_opaque_inline(xdr,
  1393. (void **)&gc->gc_ctx.data,
  1394. body_len);
  1395. if (handle_len < 0)
  1396. return false;
  1397. if (body_len != XDR_UNIT * 5 + xdr_align_size(handle_len))
  1398. return false;
  1399. gc->gc_ctx.len = handle_len;
  1400. return true;
  1401. }
  1402. /**
  1403. * svcauth_gss_accept - Decode and validate incoming RPC_AUTH_GSS credential
  1404. * @rqstp: RPC transaction
  1405. *
  1406. * Return values:
  1407. * %SVC_OK: Success
  1408. * %SVC_COMPLETE: GSS context lifetime event
  1409. * %SVC_DENIED: Credential or verifier is not valid
  1410. * %SVC_GARBAGE: Failed to decode credential or verifier
  1411. * %SVC_CLOSE: Temporary failure
  1412. *
  1413. * The rqstp->rq_auth_stat field is also set (see RFCs 2203 and 5531).
  1414. */
  1415. static enum svc_auth_status
  1416. svcauth_gss_accept(struct svc_rqst *rqstp)
  1417. {
  1418. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  1419. __be32 *rpcstart;
  1420. struct rpc_gss_wire_cred *gc;
  1421. struct rsc *rsci = NULL;
  1422. int ret;
  1423. struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
  1424. rqstp->rq_auth_stat = rpc_autherr_failed;
  1425. if (!svcdata)
  1426. svcdata = kmalloc_obj(*svcdata);
  1427. if (!svcdata)
  1428. goto auth_err;
  1429. rqstp->rq_auth_data = svcdata;
  1430. svcdata->gsd_databody_offset = 0;
  1431. svcdata->rsci = NULL;
  1432. gc = &svcdata->clcred;
  1433. rqstp->rq_auth_stat = rpc_autherr_badcred;
  1434. if (!svcauth_gss_decode_credbody(&rqstp->rq_arg_stream, gc, &rpcstart))
  1435. goto auth_err;
  1436. if (gc->gc_v != RPC_GSS_VERSION)
  1437. goto auth_err;
  1438. switch (gc->gc_proc) {
  1439. case RPC_GSS_PROC_INIT:
  1440. case RPC_GSS_PROC_CONTINUE_INIT:
  1441. if (rqstp->rq_proc != 0)
  1442. goto auth_err;
  1443. return svcauth_gss_proc_init(rqstp, gc);
  1444. case RPC_GSS_PROC_DESTROY:
  1445. if (rqstp->rq_proc != 0)
  1446. goto auth_err;
  1447. fallthrough;
  1448. case RPC_GSS_PROC_DATA:
  1449. rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
  1450. rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
  1451. if (!rsci)
  1452. goto auth_err;
  1453. switch (svcauth_gss_verify_header(rqstp, rsci, rpcstart, gc)) {
  1454. case SVC_OK:
  1455. break;
  1456. case SVC_DENIED:
  1457. goto auth_err;
  1458. case SVC_DROP:
  1459. goto drop;
  1460. }
  1461. break;
  1462. default:
  1463. if (rqstp->rq_proc != 0)
  1464. goto auth_err;
  1465. rqstp->rq_auth_stat = rpc_autherr_rejectedcred;
  1466. goto auth_err;
  1467. }
  1468. /* now act upon the command: */
  1469. switch (gc->gc_proc) {
  1470. case RPC_GSS_PROC_DESTROY:
  1471. if (!svcauth_gss_encode_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1472. goto auth_err;
  1473. if (!svcxdr_set_accept_stat(rqstp))
  1474. goto auth_err;
  1475. /* Delete the entry from the cache_list and call cache_put */
  1476. sunrpc_cache_unhash(sn->rsc_cache, &rsci->h);
  1477. goto complete;
  1478. case RPC_GSS_PROC_DATA:
  1479. rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
  1480. if (!svcauth_gss_encode_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1481. goto auth_err;
  1482. if (!svcxdr_set_accept_stat(rqstp))
  1483. goto auth_err;
  1484. svcdata->gsd_databody_offset = xdr_stream_pos(&rqstp->rq_res_stream);
  1485. rqstp->rq_cred = rsci->cred;
  1486. get_group_info(rsci->cred.cr_group_info);
  1487. rqstp->rq_auth_stat = rpc_autherr_badcred;
  1488. switch (gc->gc_svc) {
  1489. case RPC_GSS_SVC_NONE:
  1490. break;
  1491. case RPC_GSS_SVC_INTEGRITY:
  1492. /* placeholders for body length and seq. number: */
  1493. xdr_reserve_space(&rqstp->rq_res_stream, XDR_UNIT * 2);
  1494. if (svcauth_gss_unwrap_integ(rqstp, gc->gc_seq,
  1495. rsci->mechctx))
  1496. goto garbage_args;
  1497. svcxdr_set_auth_slack(rqstp, RPC_MAX_AUTH_SIZE);
  1498. break;
  1499. case RPC_GSS_SVC_PRIVACY:
  1500. /* placeholders for body length and seq. number: */
  1501. xdr_reserve_space(&rqstp->rq_res_stream, XDR_UNIT * 2);
  1502. if (svcauth_gss_unwrap_priv(rqstp, gc->gc_seq,
  1503. rsci->mechctx))
  1504. goto garbage_args;
  1505. svcxdr_set_auth_slack(rqstp, RPC_MAX_AUTH_SIZE * 2);
  1506. break;
  1507. default:
  1508. goto auth_err;
  1509. }
  1510. svcdata->rsci = rsci;
  1511. cache_get(&rsci->h);
  1512. rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
  1513. rsci->mechctx->mech_type,
  1514. GSS_C_QOP_DEFAULT,
  1515. gc->gc_svc);
  1516. ret = SVC_OK;
  1517. trace_rpcgss_svc_authenticate(rqstp, gc);
  1518. goto out;
  1519. }
  1520. garbage_args:
  1521. ret = SVC_GARBAGE;
  1522. goto out;
  1523. auth_err:
  1524. xdr_truncate_encode(&rqstp->rq_res_stream, XDR_UNIT * 2);
  1525. ret = SVC_DENIED;
  1526. goto out;
  1527. complete:
  1528. ret = SVC_COMPLETE;
  1529. goto out;
  1530. drop:
  1531. ret = SVC_CLOSE;
  1532. out:
  1533. if (rsci)
  1534. cache_put(&rsci->h, sn->rsc_cache);
  1535. return ret;
  1536. }
  1537. static u32
  1538. svcauth_gss_prepare_to_wrap(struct svc_rqst *rqstp, struct gss_svc_data *gsd)
  1539. {
  1540. u32 offset;
  1541. /* Release can be called twice, but we only wrap once. */
  1542. offset = gsd->gsd_databody_offset;
  1543. gsd->gsd_databody_offset = 0;
  1544. /* AUTH_ERROR replies are not wrapped. */
  1545. if (rqstp->rq_auth_stat != rpc_auth_ok)
  1546. return 0;
  1547. /* Also don't wrap if the accept_stat is nonzero: */
  1548. if (*rqstp->rq_accept_statp != rpc_success)
  1549. return 0;
  1550. return offset;
  1551. }
  1552. /*
  1553. * RFC 2203, Section 5.3.2.2
  1554. *
  1555. * struct rpc_gss_integ_data {
  1556. * opaque databody_integ<>;
  1557. * opaque checksum<>;
  1558. * };
  1559. *
  1560. * struct rpc_gss_data_t {
  1561. * unsigned int seq_num;
  1562. * proc_req_arg_t arg;
  1563. * };
  1564. *
  1565. * The RPC Reply message has already been XDR-encoded. rq_res_stream
  1566. * is now positioned so that the checksum can be written just past
  1567. * the RPC Reply message.
  1568. */
  1569. static int svcauth_gss_wrap_integ(struct svc_rqst *rqstp)
  1570. {
  1571. struct gss_svc_data *gsd = rqstp->rq_auth_data;
  1572. struct xdr_stream *xdr = &rqstp->rq_res_stream;
  1573. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1574. struct xdr_buf *buf = xdr->buf;
  1575. struct xdr_buf databody_integ;
  1576. struct xdr_netobj checksum;
  1577. u32 offset, maj_stat;
  1578. offset = svcauth_gss_prepare_to_wrap(rqstp, gsd);
  1579. if (!offset)
  1580. goto out;
  1581. if (xdr_buf_subsegment(buf, &databody_integ, offset + XDR_UNIT,
  1582. buf->len - offset - XDR_UNIT))
  1583. goto wrap_failed;
  1584. /* Buffer space for these has already been reserved in
  1585. * svcauth_gss_accept(). */
  1586. if (xdr_encode_word(buf, offset, databody_integ.len))
  1587. goto wrap_failed;
  1588. if (xdr_encode_word(buf, offset + XDR_UNIT, gc->gc_seq))
  1589. goto wrap_failed;
  1590. checksum.data = gsd->gsd_scratch;
  1591. maj_stat = gss_get_mic(gsd->rsci->mechctx, &databody_integ, &checksum);
  1592. if (maj_stat != GSS_S_COMPLETE)
  1593. goto bad_mic;
  1594. if (xdr_stream_encode_opaque(xdr, checksum.data, checksum.len) < 0)
  1595. goto wrap_failed;
  1596. xdr_commit_encode(xdr);
  1597. out:
  1598. return 0;
  1599. bad_mic:
  1600. trace_rpcgss_svc_get_mic(rqstp, maj_stat);
  1601. return -EINVAL;
  1602. wrap_failed:
  1603. trace_rpcgss_svc_wrap_failed(rqstp);
  1604. return -EINVAL;
  1605. }
  1606. /*
  1607. * RFC 2203, Section 5.3.2.3
  1608. *
  1609. * struct rpc_gss_priv_data {
  1610. * opaque databody_priv<>
  1611. * };
  1612. *
  1613. * struct rpc_gss_data_t {
  1614. * unsigned int seq_num;
  1615. * proc_req_arg_t arg;
  1616. * };
  1617. *
  1618. * gss_wrap() expands the size of the RPC message payload in the
  1619. * response buffer. The main purpose of svcauth_gss_wrap_priv()
  1620. * is to ensure there is adequate space in the response buffer to
  1621. * avoid overflow during the wrap.
  1622. */
  1623. static int svcauth_gss_wrap_priv(struct svc_rqst *rqstp)
  1624. {
  1625. struct gss_svc_data *gsd = rqstp->rq_auth_data;
  1626. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1627. struct xdr_buf *buf = &rqstp->rq_res;
  1628. struct kvec *head = buf->head;
  1629. struct kvec *tail = buf->tail;
  1630. u32 offset, pad, maj_stat;
  1631. __be32 *p;
  1632. offset = svcauth_gss_prepare_to_wrap(rqstp, gsd);
  1633. if (!offset)
  1634. return 0;
  1635. /*
  1636. * Buffer space for this field has already been reserved
  1637. * in svcauth_gss_accept(). Note that the GSS sequence
  1638. * number is encrypted along with the RPC reply payload.
  1639. */
  1640. if (xdr_encode_word(buf, offset + XDR_UNIT, gc->gc_seq))
  1641. goto wrap_failed;
  1642. /*
  1643. * If there is currently tail data, make sure there is
  1644. * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
  1645. * the page, and move the current tail data such that
  1646. * there is RPC_MAX_AUTH_SIZE slack space available in
  1647. * both the head and tail.
  1648. */
  1649. if (tail->iov_base) {
  1650. if (tail->iov_base >= head->iov_base + PAGE_SIZE)
  1651. goto wrap_failed;
  1652. if (tail->iov_base < head->iov_base)
  1653. goto wrap_failed;
  1654. if (tail->iov_len + head->iov_len
  1655. + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1656. goto wrap_failed;
  1657. memmove(tail->iov_base + RPC_MAX_AUTH_SIZE, tail->iov_base,
  1658. tail->iov_len);
  1659. tail->iov_base += RPC_MAX_AUTH_SIZE;
  1660. }
  1661. /*
  1662. * If there is no current tail data, make sure there is
  1663. * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
  1664. * allotted page, and set up tail information such that there
  1665. * is RPC_MAX_AUTH_SIZE slack space available in both the
  1666. * head and tail.
  1667. */
  1668. if (!tail->iov_base) {
  1669. if (head->iov_len + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1670. goto wrap_failed;
  1671. tail->iov_base = head->iov_base
  1672. + head->iov_len + RPC_MAX_AUTH_SIZE;
  1673. tail->iov_len = 0;
  1674. }
  1675. maj_stat = gss_wrap(gsd->rsci->mechctx, offset + XDR_UNIT, buf,
  1676. buf->pages);
  1677. if (maj_stat != GSS_S_COMPLETE)
  1678. goto bad_wrap;
  1679. /* Wrapping can change the size of databody_priv. */
  1680. if (xdr_encode_word(buf, offset, buf->len - offset - XDR_UNIT))
  1681. goto wrap_failed;
  1682. pad = xdr_pad_size(buf->len - offset - XDR_UNIT);
  1683. p = (__be32 *)(tail->iov_base + tail->iov_len);
  1684. memset(p, 0, pad);
  1685. tail->iov_len += pad;
  1686. buf->len += pad;
  1687. return 0;
  1688. wrap_failed:
  1689. trace_rpcgss_svc_wrap_failed(rqstp);
  1690. return -EINVAL;
  1691. bad_wrap:
  1692. trace_rpcgss_svc_wrap(rqstp, maj_stat);
  1693. return -ENOMEM;
  1694. }
  1695. /**
  1696. * svcauth_gss_release - Wrap payload and release resources
  1697. * @rqstp: RPC transaction context
  1698. *
  1699. * Return values:
  1700. * %0: the Reply is ready to be sent
  1701. * %-ENOMEM: failed to allocate memory
  1702. * %-EINVAL: encoding error
  1703. */
  1704. static int
  1705. svcauth_gss_release(struct svc_rqst *rqstp)
  1706. {
  1707. struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
  1708. struct gss_svc_data *gsd = rqstp->rq_auth_data;
  1709. struct rpc_gss_wire_cred *gc;
  1710. int stat;
  1711. if (!gsd)
  1712. goto out;
  1713. gc = &gsd->clcred;
  1714. if (gc->gc_proc != RPC_GSS_PROC_DATA)
  1715. goto out;
  1716. switch (gc->gc_svc) {
  1717. case RPC_GSS_SVC_NONE:
  1718. break;
  1719. case RPC_GSS_SVC_INTEGRITY:
  1720. stat = svcauth_gss_wrap_integ(rqstp);
  1721. if (stat)
  1722. goto out_err;
  1723. break;
  1724. case RPC_GSS_SVC_PRIVACY:
  1725. stat = svcauth_gss_wrap_priv(rqstp);
  1726. if (stat)
  1727. goto out_err;
  1728. break;
  1729. /*
  1730. * For any other gc_svc value, svcauth_gss_accept() already set
  1731. * the auth_error appropriately; just fall through:
  1732. */
  1733. }
  1734. out:
  1735. stat = 0;
  1736. out_err:
  1737. if (rqstp->rq_client)
  1738. auth_domain_put(rqstp->rq_client);
  1739. rqstp->rq_client = NULL;
  1740. if (rqstp->rq_gssclient)
  1741. auth_domain_put(rqstp->rq_gssclient);
  1742. rqstp->rq_gssclient = NULL;
  1743. if (rqstp->rq_cred.cr_group_info)
  1744. put_group_info(rqstp->rq_cred.cr_group_info);
  1745. rqstp->rq_cred.cr_group_info = NULL;
  1746. if (gsd && gsd->rsci) {
  1747. cache_put(&gsd->rsci->h, sn->rsc_cache);
  1748. gsd->rsci = NULL;
  1749. }
  1750. return stat;
  1751. }
  1752. static void
  1753. svcauth_gss_domain_release_rcu(struct rcu_head *head)
  1754. {
  1755. struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
  1756. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  1757. kfree(dom->name);
  1758. kfree(gd);
  1759. }
  1760. static void
  1761. svcauth_gss_domain_release(struct auth_domain *dom)
  1762. {
  1763. call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu);
  1764. }
  1765. static rpc_authflavor_t svcauth_gss_pseudoflavor(struct svc_rqst *rqstp)
  1766. {
  1767. return svcauth_gss_flavor(rqstp->rq_gssclient);
  1768. }
  1769. static struct auth_ops svcauthops_gss = {
  1770. .name = "rpcsec_gss",
  1771. .owner = THIS_MODULE,
  1772. .flavour = RPC_AUTH_GSS,
  1773. .accept = svcauth_gss_accept,
  1774. .release = svcauth_gss_release,
  1775. .domain_release = svcauth_gss_domain_release,
  1776. .set_client = svcauth_gss_set_client,
  1777. .pseudoflavor = svcauth_gss_pseudoflavor,
  1778. };
  1779. static int rsi_cache_create_net(struct net *net)
  1780. {
  1781. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1782. struct cache_detail *cd;
  1783. int err;
  1784. cd = cache_create_net(&rsi_cache_template, net);
  1785. if (IS_ERR(cd))
  1786. return PTR_ERR(cd);
  1787. err = cache_register_net(cd, net);
  1788. if (err) {
  1789. cache_destroy_net(cd, net);
  1790. return err;
  1791. }
  1792. sn->rsi_cache = cd;
  1793. return 0;
  1794. }
  1795. static void rsi_cache_destroy_net(struct net *net)
  1796. {
  1797. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1798. struct cache_detail *cd = sn->rsi_cache;
  1799. sn->rsi_cache = NULL;
  1800. cache_purge(cd);
  1801. cache_unregister_net(cd, net);
  1802. cache_destroy_net(cd, net);
  1803. }
  1804. static int rsc_cache_create_net(struct net *net)
  1805. {
  1806. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1807. struct cache_detail *cd;
  1808. int err;
  1809. cd = cache_create_net(&rsc_cache_template, net);
  1810. if (IS_ERR(cd))
  1811. return PTR_ERR(cd);
  1812. err = cache_register_net(cd, net);
  1813. if (err) {
  1814. cache_destroy_net(cd, net);
  1815. return err;
  1816. }
  1817. sn->rsc_cache = cd;
  1818. return 0;
  1819. }
  1820. static void rsc_cache_destroy_net(struct net *net)
  1821. {
  1822. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1823. struct cache_detail *cd = sn->rsc_cache;
  1824. sn->rsc_cache = NULL;
  1825. cache_purge(cd);
  1826. cache_unregister_net(cd, net);
  1827. cache_destroy_net(cd, net);
  1828. }
  1829. int
  1830. gss_svc_init_net(struct net *net)
  1831. {
  1832. int rv;
  1833. rv = rsc_cache_create_net(net);
  1834. if (rv)
  1835. return rv;
  1836. rv = rsi_cache_create_net(net);
  1837. if (rv)
  1838. goto out1;
  1839. rv = create_use_gss_proxy_proc_entry(net);
  1840. if (rv)
  1841. goto out2;
  1842. rv = create_krb5_enctypes_proc_entry(net);
  1843. if (rv)
  1844. goto out3;
  1845. return 0;
  1846. out3:
  1847. destroy_use_gss_proxy_proc_entry(net);
  1848. out2:
  1849. rsi_cache_destroy_net(net);
  1850. out1:
  1851. rsc_cache_destroy_net(net);
  1852. return rv;
  1853. }
  1854. void
  1855. gss_svc_shutdown_net(struct net *net)
  1856. {
  1857. destroy_krb5_enctypes_proc_entry(net);
  1858. destroy_use_gss_proxy_proc_entry(net);
  1859. rsi_cache_destroy_net(net);
  1860. rsc_cache_destroy_net(net);
  1861. }
  1862. int
  1863. gss_svc_init(void)
  1864. {
  1865. return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
  1866. }
  1867. void
  1868. gss_svc_shutdown(void)
  1869. {
  1870. svc_auth_unregister(RPC_AUTH_GSS);
  1871. }