svcauth_gss.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  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 "gss_rpc_upcall.h"
  50. #if IS_ENABLED(CONFIG_SUNRPC_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. struct rcu_head rcu_head;
  72. };
  73. static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
  74. static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
  75. static void rsi_free(struct rsi *rsii)
  76. {
  77. kfree(rsii->in_handle.data);
  78. kfree(rsii->in_token.data);
  79. kfree(rsii->out_handle.data);
  80. kfree(rsii->out_token.data);
  81. }
  82. static void rsi_free_rcu(struct rcu_head *head)
  83. {
  84. struct rsi *rsii = container_of(head, struct rsi, rcu_head);
  85. rsi_free(rsii);
  86. kfree(rsii);
  87. }
  88. static void rsi_put(struct kref *ref)
  89. {
  90. struct rsi *rsii = container_of(ref, struct rsi, h.ref);
  91. call_rcu(&rsii->rcu_head, rsi_free_rcu);
  92. }
  93. static inline int rsi_hash(struct rsi *item)
  94. {
  95. return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
  96. ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
  97. }
  98. static int rsi_match(struct cache_head *a, struct cache_head *b)
  99. {
  100. struct rsi *item = container_of(a, struct rsi, h);
  101. struct rsi *tmp = container_of(b, struct rsi, h);
  102. return netobj_equal(&item->in_handle, &tmp->in_handle) &&
  103. netobj_equal(&item->in_token, &tmp->in_token);
  104. }
  105. static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
  106. {
  107. dst->len = len;
  108. dst->data = (len ? kmemdup(src, len, GFP_KERNEL) : NULL);
  109. if (len && !dst->data)
  110. return -ENOMEM;
  111. return 0;
  112. }
  113. static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
  114. {
  115. return dup_to_netobj(dst, src->data, src->len);
  116. }
  117. static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
  118. {
  119. struct rsi *new = container_of(cnew, struct rsi, h);
  120. struct rsi *item = container_of(citem, struct rsi, h);
  121. new->out_handle.data = NULL;
  122. new->out_handle.len = 0;
  123. new->out_token.data = NULL;
  124. new->out_token.len = 0;
  125. new->in_handle.len = item->in_handle.len;
  126. item->in_handle.len = 0;
  127. new->in_token.len = item->in_token.len;
  128. item->in_token.len = 0;
  129. new->in_handle.data = item->in_handle.data;
  130. item->in_handle.data = NULL;
  131. new->in_token.data = item->in_token.data;
  132. item->in_token.data = NULL;
  133. }
  134. static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
  135. {
  136. struct rsi *new = container_of(cnew, struct rsi, h);
  137. struct rsi *item = container_of(citem, struct rsi, h);
  138. BUG_ON(new->out_handle.data || new->out_token.data);
  139. new->out_handle.len = item->out_handle.len;
  140. item->out_handle.len = 0;
  141. new->out_token.len = item->out_token.len;
  142. item->out_token.len = 0;
  143. new->out_handle.data = item->out_handle.data;
  144. item->out_handle.data = NULL;
  145. new->out_token.data = item->out_token.data;
  146. item->out_token.data = NULL;
  147. new->major_status = item->major_status;
  148. new->minor_status = item->minor_status;
  149. }
  150. static struct cache_head *rsi_alloc(void)
  151. {
  152. struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);
  153. if (rsii)
  154. return &rsii->h;
  155. else
  156. return NULL;
  157. }
  158. static void rsi_request(struct cache_detail *cd,
  159. struct cache_head *h,
  160. char **bpp, int *blen)
  161. {
  162. struct rsi *rsii = container_of(h, struct rsi, h);
  163. qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
  164. qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
  165. (*bpp)[-1] = '\n';
  166. }
  167. static int rsi_parse(struct cache_detail *cd,
  168. char *mesg, int mlen)
  169. {
  170. /* context token expiry major minor context token */
  171. char *buf = mesg;
  172. char *ep;
  173. int len;
  174. struct rsi rsii, *rsip = NULL;
  175. time_t expiry;
  176. int status = -EINVAL;
  177. memset(&rsii, 0, sizeof(rsii));
  178. /* handle */
  179. len = qword_get(&mesg, buf, mlen);
  180. if (len < 0)
  181. goto out;
  182. status = -ENOMEM;
  183. if (dup_to_netobj(&rsii.in_handle, buf, len))
  184. goto out;
  185. /* token */
  186. len = qword_get(&mesg, buf, mlen);
  187. status = -EINVAL;
  188. if (len < 0)
  189. goto out;
  190. status = -ENOMEM;
  191. if (dup_to_netobj(&rsii.in_token, buf, len))
  192. goto out;
  193. rsip = rsi_lookup(cd, &rsii);
  194. if (!rsip)
  195. goto out;
  196. rsii.h.flags = 0;
  197. /* expiry */
  198. expiry = get_expiry(&mesg);
  199. status = -EINVAL;
  200. if (expiry == 0)
  201. goto out;
  202. /* major/minor */
  203. len = qword_get(&mesg, buf, mlen);
  204. if (len <= 0)
  205. goto out;
  206. rsii.major_status = simple_strtoul(buf, &ep, 10);
  207. if (*ep)
  208. goto out;
  209. len = qword_get(&mesg, buf, mlen);
  210. if (len <= 0)
  211. goto out;
  212. rsii.minor_status = simple_strtoul(buf, &ep, 10);
  213. if (*ep)
  214. goto out;
  215. /* out_handle */
  216. len = qword_get(&mesg, buf, mlen);
  217. if (len < 0)
  218. goto out;
  219. status = -ENOMEM;
  220. if (dup_to_netobj(&rsii.out_handle, buf, len))
  221. goto out;
  222. /* out_token */
  223. len = qword_get(&mesg, buf, mlen);
  224. status = -EINVAL;
  225. if (len < 0)
  226. goto out;
  227. status = -ENOMEM;
  228. if (dup_to_netobj(&rsii.out_token, buf, len))
  229. goto out;
  230. rsii.h.expiry_time = expiry;
  231. rsip = rsi_update(cd, &rsii, rsip);
  232. status = 0;
  233. out:
  234. rsi_free(&rsii);
  235. if (rsip)
  236. cache_put(&rsip->h, cd);
  237. else
  238. status = -ENOMEM;
  239. return status;
  240. }
  241. static const struct cache_detail rsi_cache_template = {
  242. .owner = THIS_MODULE,
  243. .hash_size = RSI_HASHMAX,
  244. .name = "auth.rpcsec.init",
  245. .cache_put = rsi_put,
  246. .cache_request = rsi_request,
  247. .cache_parse = rsi_parse,
  248. .match = rsi_match,
  249. .init = rsi_init,
  250. .update = update_rsi,
  251. .alloc = rsi_alloc,
  252. };
  253. static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
  254. {
  255. struct cache_head *ch;
  256. int hash = rsi_hash(item);
  257. ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
  258. if (ch)
  259. return container_of(ch, struct rsi, h);
  260. else
  261. return NULL;
  262. }
  263. static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old)
  264. {
  265. struct cache_head *ch;
  266. int hash = rsi_hash(new);
  267. ch = sunrpc_cache_update(cd, &new->h,
  268. &old->h, hash);
  269. if (ch)
  270. return container_of(ch, struct rsi, h);
  271. else
  272. return NULL;
  273. }
  274. /*
  275. * The rpcsec_context cache is used to store a context that is
  276. * used in data exchange.
  277. * The key is a context handle. The content is:
  278. * uid, gidlist, mechanism, service-set, mech-specific-data
  279. */
  280. #define RSC_HASHBITS 10
  281. #define RSC_HASHMAX (1<<RSC_HASHBITS)
  282. #define GSS_SEQ_WIN 128
  283. struct gss_svc_seq_data {
  284. /* highest seq number seen so far: */
  285. int sd_max;
  286. /* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
  287. * sd_win is nonzero iff sequence number i has been seen already: */
  288. unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
  289. spinlock_t sd_lock;
  290. };
  291. struct rsc {
  292. struct cache_head h;
  293. struct xdr_netobj handle;
  294. struct svc_cred cred;
  295. struct gss_svc_seq_data seqdata;
  296. struct gss_ctx *mechctx;
  297. struct rcu_head rcu_head;
  298. };
  299. static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old);
  300. static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item);
  301. static void rsc_free(struct rsc *rsci)
  302. {
  303. kfree(rsci->handle.data);
  304. if (rsci->mechctx)
  305. gss_delete_sec_context(&rsci->mechctx);
  306. free_svc_cred(&rsci->cred);
  307. }
  308. static void rsc_free_rcu(struct rcu_head *head)
  309. {
  310. struct rsc *rsci = container_of(head, struct rsc, rcu_head);
  311. kfree(rsci->handle.data);
  312. kfree(rsci);
  313. }
  314. static void rsc_put(struct kref *ref)
  315. {
  316. struct rsc *rsci = container_of(ref, struct rsc, h.ref);
  317. if (rsci->mechctx)
  318. gss_delete_sec_context(&rsci->mechctx);
  319. free_svc_cred(&rsci->cred);
  320. call_rcu(&rsci->rcu_head, rsc_free_rcu);
  321. }
  322. static inline int
  323. rsc_hash(struct rsc *rsci)
  324. {
  325. return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
  326. }
  327. static int
  328. rsc_match(struct cache_head *a, struct cache_head *b)
  329. {
  330. struct rsc *new = container_of(a, struct rsc, h);
  331. struct rsc *tmp = container_of(b, struct rsc, h);
  332. return netobj_equal(&new->handle, &tmp->handle);
  333. }
  334. static void
  335. rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
  336. {
  337. struct rsc *new = container_of(cnew, struct rsc, h);
  338. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  339. new->handle.len = tmp->handle.len;
  340. tmp->handle.len = 0;
  341. new->handle.data = tmp->handle.data;
  342. tmp->handle.data = NULL;
  343. new->mechctx = NULL;
  344. init_svc_cred(&new->cred);
  345. }
  346. static void
  347. update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
  348. {
  349. struct rsc *new = container_of(cnew, struct rsc, h);
  350. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  351. new->mechctx = tmp->mechctx;
  352. tmp->mechctx = NULL;
  353. memset(&new->seqdata, 0, sizeof(new->seqdata));
  354. spin_lock_init(&new->seqdata.sd_lock);
  355. new->cred = tmp->cred;
  356. init_svc_cred(&tmp->cred);
  357. }
  358. static struct cache_head *
  359. rsc_alloc(void)
  360. {
  361. struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
  362. if (rsci)
  363. return &rsci->h;
  364. else
  365. return NULL;
  366. }
  367. static int rsc_parse(struct cache_detail *cd,
  368. char *mesg, int mlen)
  369. {
  370. /* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
  371. char *buf = mesg;
  372. int id;
  373. int len, rv;
  374. struct rsc rsci, *rscp = NULL;
  375. time_t expiry;
  376. int status = -EINVAL;
  377. struct gss_api_mech *gm = NULL;
  378. memset(&rsci, 0, sizeof(rsci));
  379. /* context handle */
  380. len = qword_get(&mesg, buf, mlen);
  381. if (len < 0) goto out;
  382. status = -ENOMEM;
  383. if (dup_to_netobj(&rsci.handle, buf, len))
  384. goto out;
  385. rsci.h.flags = 0;
  386. /* expiry */
  387. expiry = get_expiry(&mesg);
  388. status = -EINVAL;
  389. if (expiry == 0)
  390. goto out;
  391. rscp = rsc_lookup(cd, &rsci);
  392. if (!rscp)
  393. goto out;
  394. /* uid, or NEGATIVE */
  395. rv = get_int(&mesg, &id);
  396. if (rv == -EINVAL)
  397. goto out;
  398. if (rv == -ENOENT)
  399. set_bit(CACHE_NEGATIVE, &rsci.h.flags);
  400. else {
  401. int N, i;
  402. /*
  403. * NOTE: we skip uid_valid()/gid_valid() checks here:
  404. * instead, * -1 id's are later mapped to the
  405. * (export-specific) anonymous id by nfsd_setuser.
  406. *
  407. * (But supplementary gid's get no such special
  408. * treatment so are checked for validity here.)
  409. */
  410. /* uid */
  411. rsci.cred.cr_uid = make_kuid(&init_user_ns, id);
  412. /* gid */
  413. if (get_int(&mesg, &id))
  414. goto out;
  415. rsci.cred.cr_gid = make_kgid(&init_user_ns, id);
  416. /* number of additional gid's */
  417. if (get_int(&mesg, &N))
  418. goto out;
  419. if (N < 0 || N > NGROUPS_MAX)
  420. goto out;
  421. status = -ENOMEM;
  422. rsci.cred.cr_group_info = groups_alloc(N);
  423. if (rsci.cred.cr_group_info == NULL)
  424. goto out;
  425. /* gid's */
  426. status = -EINVAL;
  427. for (i=0; i<N; i++) {
  428. kgid_t kgid;
  429. if (get_int(&mesg, &id))
  430. goto out;
  431. kgid = make_kgid(&init_user_ns, id);
  432. if (!gid_valid(kgid))
  433. goto out;
  434. rsci.cred.cr_group_info->gid[i] = kgid;
  435. }
  436. groups_sort(rsci.cred.cr_group_info);
  437. /* mech name */
  438. len = qword_get(&mesg, buf, mlen);
  439. if (len < 0)
  440. goto out;
  441. gm = rsci.cred.cr_gss_mech = gss_mech_get_by_name(buf);
  442. status = -EOPNOTSUPP;
  443. if (!gm)
  444. goto out;
  445. status = -EINVAL;
  446. /* mech-specific data: */
  447. len = qword_get(&mesg, buf, mlen);
  448. if (len < 0)
  449. goto out;
  450. status = gss_import_sec_context(buf, len, gm, &rsci.mechctx,
  451. NULL, GFP_KERNEL);
  452. if (status)
  453. goto out;
  454. /* get client name */
  455. len = qword_get(&mesg, buf, mlen);
  456. if (len > 0) {
  457. rsci.cred.cr_principal = kstrdup(buf, GFP_KERNEL);
  458. if (!rsci.cred.cr_principal) {
  459. status = -ENOMEM;
  460. goto out;
  461. }
  462. }
  463. }
  464. rsci.h.expiry_time = expiry;
  465. rscp = rsc_update(cd, &rsci, rscp);
  466. status = 0;
  467. out:
  468. rsc_free(&rsci);
  469. if (rscp)
  470. cache_put(&rscp->h, cd);
  471. else
  472. status = -ENOMEM;
  473. return status;
  474. }
  475. static const struct cache_detail rsc_cache_template = {
  476. .owner = THIS_MODULE,
  477. .hash_size = RSC_HASHMAX,
  478. .name = "auth.rpcsec.context",
  479. .cache_put = rsc_put,
  480. .cache_parse = rsc_parse,
  481. .match = rsc_match,
  482. .init = rsc_init,
  483. .update = update_rsc,
  484. .alloc = rsc_alloc,
  485. };
  486. static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item)
  487. {
  488. struct cache_head *ch;
  489. int hash = rsc_hash(item);
  490. ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
  491. if (ch)
  492. return container_of(ch, struct rsc, h);
  493. else
  494. return NULL;
  495. }
  496. static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old)
  497. {
  498. struct cache_head *ch;
  499. int hash = rsc_hash(new);
  500. ch = sunrpc_cache_update(cd, &new->h,
  501. &old->h, hash);
  502. if (ch)
  503. return container_of(ch, struct rsc, h);
  504. else
  505. return NULL;
  506. }
  507. static struct rsc *
  508. gss_svc_searchbyctx(struct cache_detail *cd, struct xdr_netobj *handle)
  509. {
  510. struct rsc rsci;
  511. struct rsc *found;
  512. memset(&rsci, 0, sizeof(rsci));
  513. if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
  514. return NULL;
  515. found = rsc_lookup(cd, &rsci);
  516. rsc_free(&rsci);
  517. if (!found)
  518. return NULL;
  519. if (cache_check(cd, &found->h, NULL))
  520. return NULL;
  521. return found;
  522. }
  523. /* Implements sequence number algorithm as specified in RFC 2203. */
  524. static int
  525. gss_check_seq_num(struct rsc *rsci, int seq_num)
  526. {
  527. struct gss_svc_seq_data *sd = &rsci->seqdata;
  528. spin_lock(&sd->sd_lock);
  529. if (seq_num > sd->sd_max) {
  530. if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
  531. memset(sd->sd_win,0,sizeof(sd->sd_win));
  532. sd->sd_max = seq_num;
  533. } else while (sd->sd_max < seq_num) {
  534. sd->sd_max++;
  535. __clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
  536. }
  537. __set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
  538. goto ok;
  539. } else if (seq_num <= sd->sd_max - GSS_SEQ_WIN) {
  540. goto drop;
  541. }
  542. /* sd_max - GSS_SEQ_WIN < seq_num <= sd_max */
  543. if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
  544. goto drop;
  545. ok:
  546. spin_unlock(&sd->sd_lock);
  547. return 1;
  548. drop:
  549. spin_unlock(&sd->sd_lock);
  550. return 0;
  551. }
  552. static inline u32 round_up_to_quad(u32 i)
  553. {
  554. return (i + 3 ) & ~3;
  555. }
  556. static inline int
  557. svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
  558. {
  559. int l;
  560. if (argv->iov_len < 4)
  561. return -1;
  562. o->len = svc_getnl(argv);
  563. l = round_up_to_quad(o->len);
  564. if (argv->iov_len < l)
  565. return -1;
  566. o->data = argv->iov_base;
  567. argv->iov_base += l;
  568. argv->iov_len -= l;
  569. return 0;
  570. }
  571. static inline int
  572. svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
  573. {
  574. u8 *p;
  575. if (resv->iov_len + 4 > PAGE_SIZE)
  576. return -1;
  577. svc_putnl(resv, o->len);
  578. p = resv->iov_base + resv->iov_len;
  579. resv->iov_len += round_up_to_quad(o->len);
  580. if (resv->iov_len > PAGE_SIZE)
  581. return -1;
  582. memcpy(p, o->data, o->len);
  583. memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);
  584. return 0;
  585. }
  586. /*
  587. * Verify the checksum on the header and return SVC_OK on success.
  588. * Otherwise, return SVC_DROP (in the case of a bad sequence number)
  589. * or return SVC_DENIED and indicate error in authp.
  590. */
  591. static int
  592. gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
  593. __be32 *rpcstart, struct rpc_gss_wire_cred *gc, __be32 *authp)
  594. {
  595. struct gss_ctx *ctx_id = rsci->mechctx;
  596. struct xdr_buf rpchdr;
  597. struct xdr_netobj checksum;
  598. u32 flavor = 0;
  599. struct kvec *argv = &rqstp->rq_arg.head[0];
  600. struct kvec iov;
  601. /* data to compute the checksum over: */
  602. iov.iov_base = rpcstart;
  603. iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
  604. xdr_buf_from_iov(&iov, &rpchdr);
  605. *authp = rpc_autherr_badverf;
  606. if (argv->iov_len < 4)
  607. return SVC_DENIED;
  608. flavor = svc_getnl(argv);
  609. if (flavor != RPC_AUTH_GSS)
  610. return SVC_DENIED;
  611. if (svc_safe_getnetobj(argv, &checksum))
  612. return SVC_DENIED;
  613. if (rqstp->rq_deferred) /* skip verification of revisited request */
  614. return SVC_OK;
  615. if (gss_verify_mic(ctx_id, &rpchdr, &checksum) != GSS_S_COMPLETE) {
  616. *authp = rpcsec_gsserr_credproblem;
  617. return SVC_DENIED;
  618. }
  619. if (gc->gc_seq > MAXSEQ) {
  620. dprintk("RPC: svcauth_gss: discarding request with "
  621. "large sequence number %d\n", gc->gc_seq);
  622. *authp = rpcsec_gsserr_ctxproblem;
  623. return SVC_DENIED;
  624. }
  625. if (!gss_check_seq_num(rsci, gc->gc_seq)) {
  626. dprintk("RPC: svcauth_gss: discarding request with "
  627. "old sequence number %d\n", gc->gc_seq);
  628. return SVC_DROP;
  629. }
  630. return SVC_OK;
  631. }
  632. static int
  633. gss_write_null_verf(struct svc_rqst *rqstp)
  634. {
  635. __be32 *p;
  636. svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);
  637. p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
  638. /* don't really need to check if head->iov_len > PAGE_SIZE ... */
  639. *p++ = 0;
  640. if (!xdr_ressize_check(rqstp, p))
  641. return -1;
  642. return 0;
  643. }
  644. static int
  645. gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
  646. {
  647. __be32 *xdr_seq;
  648. u32 maj_stat;
  649. struct xdr_buf verf_data;
  650. struct xdr_netobj mic;
  651. __be32 *p;
  652. struct kvec iov;
  653. int err = -1;
  654. svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);
  655. xdr_seq = kmalloc(4, GFP_KERNEL);
  656. if (!xdr_seq)
  657. return -1;
  658. *xdr_seq = htonl(seq);
  659. iov.iov_base = xdr_seq;
  660. iov.iov_len = 4;
  661. xdr_buf_from_iov(&iov, &verf_data);
  662. p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
  663. mic.data = (u8 *)(p + 1);
  664. maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
  665. if (maj_stat != GSS_S_COMPLETE)
  666. goto out;
  667. *p++ = htonl(mic.len);
  668. memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
  669. p += XDR_QUADLEN(mic.len);
  670. if (!xdr_ressize_check(rqstp, p))
  671. goto out;
  672. err = 0;
  673. out:
  674. kfree(xdr_seq);
  675. return err;
  676. }
  677. struct gss_domain {
  678. struct auth_domain h;
  679. u32 pseudoflavor;
  680. };
  681. static struct auth_domain *
  682. find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
  683. {
  684. char *name;
  685. name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
  686. if (!name)
  687. return NULL;
  688. return auth_domain_find(name);
  689. }
  690. static struct auth_ops svcauthops_gss;
  691. u32 svcauth_gss_flavor(struct auth_domain *dom)
  692. {
  693. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  694. return gd->pseudoflavor;
  695. }
  696. EXPORT_SYMBOL_GPL(svcauth_gss_flavor);
  697. int
  698. svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
  699. {
  700. struct gss_domain *new;
  701. struct auth_domain *test;
  702. int stat = -ENOMEM;
  703. new = kmalloc(sizeof(*new), GFP_KERNEL);
  704. if (!new)
  705. goto out;
  706. kref_init(&new->h.ref);
  707. new->h.name = kstrdup(name, GFP_KERNEL);
  708. if (!new->h.name)
  709. goto out_free_dom;
  710. new->h.flavour = &svcauthops_gss;
  711. new->pseudoflavor = pseudoflavor;
  712. stat = 0;
  713. test = auth_domain_lookup(name, &new->h);
  714. if (test != &new->h) { /* Duplicate registration */
  715. auth_domain_put(test);
  716. kfree(new->h.name);
  717. goto out_free_dom;
  718. }
  719. return 0;
  720. out_free_dom:
  721. kfree(new);
  722. out:
  723. return stat;
  724. }
  725. EXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);
  726. static inline int
  727. read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
  728. {
  729. __be32 raw;
  730. int status;
  731. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  732. if (status)
  733. return status;
  734. *obj = ntohl(raw);
  735. return 0;
  736. }
  737. /* It would be nice if this bit of code could be shared with the client.
  738. * Obstacles:
  739. * The client shouldn't malloc(), would have to pass in own memory.
  740. * The server uses base of head iovec as read pointer, while the
  741. * client uses separate pointer. */
  742. static int
  743. unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  744. {
  745. int stat = -EINVAL;
  746. u32 integ_len, maj_stat;
  747. struct xdr_netobj mic;
  748. struct xdr_buf integ_buf;
  749. /* NFS READ normally uses splice to send data in-place. However
  750. * the data in cache can change after the reply's MIC is computed
  751. * but before the RPC reply is sent. To prevent the client from
  752. * rejecting the server-computed MIC in this somewhat rare case,
  753. * do not use splice with the GSS integrity service.
  754. */
  755. clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  756. /* Did we already verify the signature on the original pass through? */
  757. if (rqstp->rq_deferred)
  758. return 0;
  759. integ_len = svc_getnl(&buf->head[0]);
  760. if (integ_len & 3)
  761. return stat;
  762. if (integ_len > buf->len)
  763. return stat;
  764. if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len)) {
  765. WARN_ON_ONCE(1);
  766. return stat;
  767. }
  768. /* copy out mic... */
  769. if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
  770. return stat;
  771. if (mic.len > RPC_MAX_AUTH_SIZE)
  772. return stat;
  773. mic.data = kmalloc(mic.len, GFP_KERNEL);
  774. if (!mic.data)
  775. return stat;
  776. if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
  777. goto out;
  778. maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);
  779. if (maj_stat != GSS_S_COMPLETE)
  780. goto out;
  781. if (svc_getnl(&buf->head[0]) != seq)
  782. goto out;
  783. /* trim off the mic and padding at the end before returning */
  784. xdr_buf_trim(buf, round_up_to_quad(mic.len) + 4);
  785. stat = 0;
  786. out:
  787. kfree(mic.data);
  788. return stat;
  789. }
  790. static inline int
  791. total_buf_len(struct xdr_buf *buf)
  792. {
  793. return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;
  794. }
  795. static void
  796. fix_priv_head(struct xdr_buf *buf, int pad)
  797. {
  798. if (buf->page_len == 0) {
  799. /* We need to adjust head and buf->len in tandem in this
  800. * case to make svc_defer() work--it finds the original
  801. * buffer start using buf->len - buf->head[0].iov_len. */
  802. buf->head[0].iov_len -= pad;
  803. }
  804. }
  805. static int
  806. unwrap_priv_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  807. {
  808. u32 priv_len, maj_stat;
  809. int pad, saved_len, remaining_len, offset;
  810. clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  811. priv_len = svc_getnl(&buf->head[0]);
  812. if (rqstp->rq_deferred) {
  813. /* Already decrypted last time through! The sequence number
  814. * check at out_seq is unnecessary but harmless: */
  815. goto out_seq;
  816. }
  817. /* buf->len is the number of bytes from the original start of the
  818. * request to the end, where head[0].iov_len is just the bytes
  819. * not yet read from the head, so these two values are different: */
  820. remaining_len = total_buf_len(buf);
  821. if (priv_len > remaining_len)
  822. return -EINVAL;
  823. pad = remaining_len - priv_len;
  824. buf->len -= pad;
  825. fix_priv_head(buf, pad);
  826. /* Maybe it would be better to give gss_unwrap a length parameter: */
  827. saved_len = buf->len;
  828. buf->len = priv_len;
  829. maj_stat = gss_unwrap(ctx, 0, buf);
  830. pad = priv_len - buf->len;
  831. buf->len = saved_len;
  832. buf->len -= pad;
  833. /* The upper layers assume the buffer is aligned on 4-byte boundaries.
  834. * In the krb5p case, at least, the data ends up offset, so we need to
  835. * move it around. */
  836. /* XXX: This is very inefficient. It would be better to either do
  837. * this while we encrypt, or maybe in the receive code, if we can peak
  838. * ahead and work out the service and mechanism there. */
  839. offset = buf->head[0].iov_len % 4;
  840. if (offset) {
  841. buf->buflen = RPCSVC_MAXPAYLOAD;
  842. xdr_shift_buf(buf, offset);
  843. fix_priv_head(buf, pad);
  844. }
  845. if (maj_stat != GSS_S_COMPLETE)
  846. return -EINVAL;
  847. out_seq:
  848. if (svc_getnl(&buf->head[0]) != seq)
  849. return -EINVAL;
  850. return 0;
  851. }
  852. struct gss_svc_data {
  853. /* decoded gss client cred: */
  854. struct rpc_gss_wire_cred clcred;
  855. /* save a pointer to the beginning of the encoded verifier,
  856. * for use in encryption/checksumming in svcauth_gss_release: */
  857. __be32 *verf_start;
  858. struct rsc *rsci;
  859. };
  860. static int
  861. svcauth_gss_set_client(struct svc_rqst *rqstp)
  862. {
  863. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  864. struct rsc *rsci = svcdata->rsci;
  865. struct rpc_gss_wire_cred *gc = &svcdata->clcred;
  866. int stat;
  867. /*
  868. * A gss export can be specified either by:
  869. * export *(sec=krb5,rw)
  870. * or by
  871. * export gss/krb5(rw)
  872. * The latter is deprecated; but for backwards compatibility reasons
  873. * the nfsd code will still fall back on trying it if the former
  874. * doesn't work; so we try to make both available to nfsd, below.
  875. */
  876. rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
  877. if (rqstp->rq_gssclient == NULL)
  878. return SVC_DENIED;
  879. stat = svcauth_unix_set_client(rqstp);
  880. if (stat == SVC_DROP || stat == SVC_CLOSE)
  881. return stat;
  882. return SVC_OK;
  883. }
  884. static inline int
  885. gss_write_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
  886. struct xdr_netobj *out_handle, int *major_status)
  887. {
  888. struct rsc *rsci;
  889. int rc;
  890. if (*major_status != GSS_S_COMPLETE)
  891. return gss_write_null_verf(rqstp);
  892. rsci = gss_svc_searchbyctx(cd, out_handle);
  893. if (rsci == NULL) {
  894. *major_status = GSS_S_NO_CONTEXT;
  895. return gss_write_null_verf(rqstp);
  896. }
  897. rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
  898. cache_put(&rsci->h, cd);
  899. return rc;
  900. }
  901. static inline int
  902. gss_read_common_verf(struct rpc_gss_wire_cred *gc,
  903. struct kvec *argv, __be32 *authp,
  904. struct xdr_netobj *in_handle)
  905. {
  906. /* Read the verifier; should be NULL: */
  907. *authp = rpc_autherr_badverf;
  908. if (argv->iov_len < 2 * 4)
  909. return SVC_DENIED;
  910. if (svc_getnl(argv) != RPC_AUTH_NULL)
  911. return SVC_DENIED;
  912. if (svc_getnl(argv) != 0)
  913. return SVC_DENIED;
  914. /* Martial context handle and token for upcall: */
  915. *authp = rpc_autherr_badcred;
  916. if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
  917. return SVC_DENIED;
  918. if (dup_netobj(in_handle, &gc->gc_ctx))
  919. return SVC_CLOSE;
  920. *authp = rpc_autherr_badverf;
  921. return 0;
  922. }
  923. static inline int
  924. gss_read_verf(struct rpc_gss_wire_cred *gc,
  925. struct kvec *argv, __be32 *authp,
  926. struct xdr_netobj *in_handle,
  927. struct xdr_netobj *in_token)
  928. {
  929. struct xdr_netobj tmpobj;
  930. int res;
  931. res = gss_read_common_verf(gc, argv, authp, in_handle);
  932. if (res)
  933. return res;
  934. if (svc_safe_getnetobj(argv, &tmpobj)) {
  935. kfree(in_handle->data);
  936. return SVC_DENIED;
  937. }
  938. if (dup_netobj(in_token, &tmpobj)) {
  939. kfree(in_handle->data);
  940. return SVC_CLOSE;
  941. }
  942. return 0;
  943. }
  944. /* Ok this is really heavily depending on a set of semantics in
  945. * how rqstp is set up by svc_recv and pages laid down by the
  946. * server when reading a request. We are basically guaranteed that
  947. * the token lays all down linearly across a set of pages, starting
  948. * at iov_base in rq_arg.head[0] which happens to be the first of a
  949. * set of pages stored in rq_pages[].
  950. * rq_arg.head[0].iov_base will provide us the page_base to pass
  951. * to the upcall.
  952. */
  953. static inline int
  954. gss_read_proxy_verf(struct svc_rqst *rqstp,
  955. struct rpc_gss_wire_cred *gc, __be32 *authp,
  956. struct xdr_netobj *in_handle,
  957. struct gssp_in_token *in_token)
  958. {
  959. struct kvec *argv = &rqstp->rq_arg.head[0];
  960. u32 inlen;
  961. int res;
  962. res = gss_read_common_verf(gc, argv, authp, in_handle);
  963. if (res)
  964. return res;
  965. inlen = svc_getnl(argv);
  966. if (inlen > (argv->iov_len + rqstp->rq_arg.page_len))
  967. return SVC_DENIED;
  968. in_token->pages = rqstp->rq_pages;
  969. in_token->page_base = (ulong)argv->iov_base & ~PAGE_MASK;
  970. in_token->page_len = inlen;
  971. return 0;
  972. }
  973. static inline int
  974. gss_write_resv(struct kvec *resv, size_t size_limit,
  975. struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
  976. int major_status, int minor_status)
  977. {
  978. if (resv->iov_len + 4 > size_limit)
  979. return -1;
  980. svc_putnl(resv, RPC_SUCCESS);
  981. if (svc_safe_putnetobj(resv, out_handle))
  982. return -1;
  983. if (resv->iov_len + 3 * 4 > size_limit)
  984. return -1;
  985. svc_putnl(resv, major_status);
  986. svc_putnl(resv, minor_status);
  987. svc_putnl(resv, GSS_SEQ_WIN);
  988. if (svc_safe_putnetobj(resv, out_token))
  989. return -1;
  990. return 0;
  991. }
  992. /*
  993. * Having read the cred already and found we're in the context
  994. * initiation case, read the verifier and initiate (or check the results
  995. * of) upcalls to userspace for help with context initiation. If
  996. * the upcall results are available, write the verifier and result.
  997. * Otherwise, drop the request pending an answer to the upcall.
  998. */
  999. static int svcauth_gss_legacy_init(struct svc_rqst *rqstp,
  1000. struct rpc_gss_wire_cred *gc, __be32 *authp)
  1001. {
  1002. struct kvec *argv = &rqstp->rq_arg.head[0];
  1003. struct kvec *resv = &rqstp->rq_res.head[0];
  1004. struct rsi *rsip, rsikey;
  1005. int ret;
  1006. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  1007. memset(&rsikey, 0, sizeof(rsikey));
  1008. ret = gss_read_verf(gc, argv, authp,
  1009. &rsikey.in_handle, &rsikey.in_token);
  1010. if (ret)
  1011. return ret;
  1012. /* Perform upcall, or find upcall result: */
  1013. rsip = rsi_lookup(sn->rsi_cache, &rsikey);
  1014. rsi_free(&rsikey);
  1015. if (!rsip)
  1016. return SVC_CLOSE;
  1017. if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
  1018. /* No upcall result: */
  1019. return SVC_CLOSE;
  1020. ret = SVC_CLOSE;
  1021. /* Got an answer to the upcall; use it: */
  1022. if (gss_write_init_verf(sn->rsc_cache, rqstp,
  1023. &rsip->out_handle, &rsip->major_status))
  1024. goto out;
  1025. if (gss_write_resv(resv, PAGE_SIZE,
  1026. &rsip->out_handle, &rsip->out_token,
  1027. rsip->major_status, rsip->minor_status))
  1028. goto out;
  1029. ret = SVC_COMPLETE;
  1030. out:
  1031. cache_put(&rsip->h, sn->rsi_cache);
  1032. return ret;
  1033. }
  1034. static int gss_proxy_save_rsc(struct cache_detail *cd,
  1035. struct gssp_upcall_data *ud,
  1036. uint64_t *handle)
  1037. {
  1038. struct rsc rsci, *rscp = NULL;
  1039. static atomic64_t ctxhctr;
  1040. long long ctxh;
  1041. struct gss_api_mech *gm = NULL;
  1042. time_t expiry;
  1043. int status = -EINVAL;
  1044. memset(&rsci, 0, sizeof(rsci));
  1045. /* context handle */
  1046. status = -ENOMEM;
  1047. /* the handle needs to be just a unique id,
  1048. * use a static counter */
  1049. ctxh = atomic64_inc_return(&ctxhctr);
  1050. /* make a copy for the caller */
  1051. *handle = ctxh;
  1052. /* make a copy for the rsc cache */
  1053. if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
  1054. goto out;
  1055. rscp = rsc_lookup(cd, &rsci);
  1056. if (!rscp)
  1057. goto out;
  1058. /* creds */
  1059. if (!ud->found_creds) {
  1060. /* userspace seem buggy, we should always get at least a
  1061. * mapping to nobody */
  1062. dprintk("RPC: No creds found!\n");
  1063. goto out;
  1064. } else {
  1065. /* steal creds */
  1066. rsci.cred = ud->creds;
  1067. memset(&ud->creds, 0, sizeof(struct svc_cred));
  1068. status = -EOPNOTSUPP;
  1069. /* get mech handle from OID */
  1070. gm = gss_mech_get_by_OID(&ud->mech_oid);
  1071. if (!gm)
  1072. goto out;
  1073. rsci.cred.cr_gss_mech = gm;
  1074. status = -EINVAL;
  1075. /* mech-specific data: */
  1076. status = gss_import_sec_context(ud->out_handle.data,
  1077. ud->out_handle.len,
  1078. gm, &rsci.mechctx,
  1079. &expiry, GFP_KERNEL);
  1080. if (status)
  1081. goto out;
  1082. }
  1083. rsci.h.expiry_time = expiry;
  1084. rscp = rsc_update(cd, &rsci, rscp);
  1085. status = 0;
  1086. out:
  1087. rsc_free(&rsci);
  1088. if (rscp)
  1089. cache_put(&rscp->h, cd);
  1090. else
  1091. status = -ENOMEM;
  1092. return status;
  1093. }
  1094. static int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
  1095. struct rpc_gss_wire_cred *gc, __be32 *authp)
  1096. {
  1097. struct kvec *resv = &rqstp->rq_res.head[0];
  1098. struct xdr_netobj cli_handle;
  1099. struct gssp_upcall_data ud;
  1100. uint64_t handle;
  1101. int status;
  1102. int ret;
  1103. struct net *net = rqstp->rq_xprt->xpt_net;
  1104. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1105. memset(&ud, 0, sizeof(ud));
  1106. ret = gss_read_proxy_verf(rqstp, gc, authp,
  1107. &ud.in_handle, &ud.in_token);
  1108. if (ret)
  1109. return ret;
  1110. ret = SVC_CLOSE;
  1111. /* Perform synchronous upcall to gss-proxy */
  1112. status = gssp_accept_sec_context_upcall(net, &ud);
  1113. if (status)
  1114. goto out;
  1115. dprintk("RPC: svcauth_gss: gss major status = %d "
  1116. "minor status = %d\n",
  1117. ud.major_status, ud.minor_status);
  1118. switch (ud.major_status) {
  1119. case GSS_S_CONTINUE_NEEDED:
  1120. cli_handle = ud.out_handle;
  1121. break;
  1122. case GSS_S_COMPLETE:
  1123. status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
  1124. if (status)
  1125. goto out;
  1126. cli_handle.data = (u8 *)&handle;
  1127. cli_handle.len = sizeof(handle);
  1128. break;
  1129. default:
  1130. ret = SVC_CLOSE;
  1131. goto out;
  1132. }
  1133. /* Got an answer to the upcall; use it: */
  1134. if (gss_write_init_verf(sn->rsc_cache, rqstp,
  1135. &cli_handle, &ud.major_status))
  1136. goto out;
  1137. if (gss_write_resv(resv, PAGE_SIZE,
  1138. &cli_handle, &ud.out_token,
  1139. ud.major_status, ud.minor_status))
  1140. goto out;
  1141. ret = SVC_COMPLETE;
  1142. out:
  1143. gssp_free_upcall_data(&ud);
  1144. return ret;
  1145. }
  1146. /*
  1147. * Try to set the sn->use_gss_proxy variable to a new value. We only allow
  1148. * it to be changed if it's currently undefined (-1). If it's any other value
  1149. * then return -EBUSY unless the type wouldn't have changed anyway.
  1150. */
  1151. static int set_gss_proxy(struct net *net, int type)
  1152. {
  1153. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1154. int ret;
  1155. WARN_ON_ONCE(type != 0 && type != 1);
  1156. ret = cmpxchg(&sn->use_gss_proxy, -1, type);
  1157. if (ret != -1 && ret != type)
  1158. return -EBUSY;
  1159. return 0;
  1160. }
  1161. static bool use_gss_proxy(struct net *net)
  1162. {
  1163. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1164. /* If use_gss_proxy is still undefined, then try to disable it */
  1165. if (sn->use_gss_proxy == -1)
  1166. set_gss_proxy(net, 0);
  1167. return sn->use_gss_proxy;
  1168. }
  1169. #ifdef CONFIG_PROC_FS
  1170. static ssize_t write_gssp(struct file *file, const char __user *buf,
  1171. size_t count, loff_t *ppos)
  1172. {
  1173. struct net *net = PDE_DATA(file_inode(file));
  1174. char tbuf[20];
  1175. unsigned long i;
  1176. int res;
  1177. if (*ppos || count > sizeof(tbuf)-1)
  1178. return -EINVAL;
  1179. if (copy_from_user(tbuf, buf, count))
  1180. return -EFAULT;
  1181. tbuf[count] = 0;
  1182. res = kstrtoul(tbuf, 0, &i);
  1183. if (res)
  1184. return res;
  1185. if (i != 1)
  1186. return -EINVAL;
  1187. res = set_gssp_clnt(net);
  1188. if (res)
  1189. return res;
  1190. res = set_gss_proxy(net, 1);
  1191. if (res)
  1192. return res;
  1193. return count;
  1194. }
  1195. static ssize_t read_gssp(struct file *file, char __user *buf,
  1196. size_t count, loff_t *ppos)
  1197. {
  1198. struct net *net = PDE_DATA(file_inode(file));
  1199. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1200. unsigned long p = *ppos;
  1201. char tbuf[10];
  1202. size_t len;
  1203. snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
  1204. len = strlen(tbuf);
  1205. if (p >= len)
  1206. return 0;
  1207. len -= p;
  1208. if (len > count)
  1209. len = count;
  1210. if (copy_to_user(buf, (void *)(tbuf+p), len))
  1211. return -EFAULT;
  1212. *ppos += len;
  1213. return len;
  1214. }
  1215. static const struct file_operations use_gss_proxy_ops = {
  1216. .open = nonseekable_open,
  1217. .write = write_gssp,
  1218. .read = read_gssp,
  1219. };
  1220. static int create_use_gss_proxy_proc_entry(struct net *net)
  1221. {
  1222. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1223. struct proc_dir_entry **p = &sn->use_gssp_proc;
  1224. sn->use_gss_proxy = -1;
  1225. *p = proc_create_data("use-gss-proxy", S_IFREG | 0600,
  1226. sn->proc_net_rpc,
  1227. &use_gss_proxy_ops, net);
  1228. if (!*p)
  1229. return -ENOMEM;
  1230. init_gssp_clnt(sn);
  1231. return 0;
  1232. }
  1233. static void destroy_use_gss_proxy_proc_entry(struct net *net)
  1234. {
  1235. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1236. if (sn->use_gssp_proc) {
  1237. remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
  1238. clear_gssp_clnt(sn);
  1239. }
  1240. }
  1241. #else /* CONFIG_PROC_FS */
  1242. static int create_use_gss_proxy_proc_entry(struct net *net)
  1243. {
  1244. return 0;
  1245. }
  1246. static void destroy_use_gss_proxy_proc_entry(struct net *net) {}
  1247. #endif /* CONFIG_PROC_FS */
  1248. /*
  1249. * Accept an rpcsec packet.
  1250. * If context establishment, punt to user space
  1251. * If data exchange, verify/decrypt
  1252. * If context destruction, handle here
  1253. * In the context establishment and destruction case we encode
  1254. * response here and return SVC_COMPLETE.
  1255. */
  1256. static int
  1257. svcauth_gss_accept(struct svc_rqst *rqstp, __be32 *authp)
  1258. {
  1259. struct kvec *argv = &rqstp->rq_arg.head[0];
  1260. struct kvec *resv = &rqstp->rq_res.head[0];
  1261. u32 crlen;
  1262. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  1263. struct rpc_gss_wire_cred *gc;
  1264. struct rsc *rsci = NULL;
  1265. __be32 *rpcstart;
  1266. __be32 *reject_stat = resv->iov_base + resv->iov_len;
  1267. int ret;
  1268. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  1269. dprintk("RPC: svcauth_gss: argv->iov_len = %zd\n",
  1270. argv->iov_len);
  1271. *authp = rpc_autherr_badcred;
  1272. if (!svcdata)
  1273. svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
  1274. if (!svcdata)
  1275. goto auth_err;
  1276. rqstp->rq_auth_data = svcdata;
  1277. svcdata->verf_start = NULL;
  1278. svcdata->rsci = NULL;
  1279. gc = &svcdata->clcred;
  1280. /* start of rpc packet is 7 u32's back from here:
  1281. * xid direction rpcversion prog vers proc flavour
  1282. */
  1283. rpcstart = argv->iov_base;
  1284. rpcstart -= 7;
  1285. /* credential is:
  1286. * version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
  1287. * at least 5 u32s, and is preceded by length, so that makes 6.
  1288. */
  1289. if (argv->iov_len < 5 * 4)
  1290. goto auth_err;
  1291. crlen = svc_getnl(argv);
  1292. if (svc_getnl(argv) != RPC_GSS_VERSION)
  1293. goto auth_err;
  1294. gc->gc_proc = svc_getnl(argv);
  1295. gc->gc_seq = svc_getnl(argv);
  1296. gc->gc_svc = svc_getnl(argv);
  1297. if (svc_safe_getnetobj(argv, &gc->gc_ctx))
  1298. goto auth_err;
  1299. if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
  1300. goto auth_err;
  1301. if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
  1302. goto auth_err;
  1303. *authp = rpc_autherr_badverf;
  1304. switch (gc->gc_proc) {
  1305. case RPC_GSS_PROC_INIT:
  1306. case RPC_GSS_PROC_CONTINUE_INIT:
  1307. if (use_gss_proxy(SVC_NET(rqstp)))
  1308. return svcauth_gss_proxy_init(rqstp, gc, authp);
  1309. else
  1310. return svcauth_gss_legacy_init(rqstp, gc, authp);
  1311. case RPC_GSS_PROC_DATA:
  1312. case RPC_GSS_PROC_DESTROY:
  1313. /* Look up the context, and check the verifier: */
  1314. *authp = rpcsec_gsserr_credproblem;
  1315. rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
  1316. if (!rsci)
  1317. goto auth_err;
  1318. switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
  1319. case SVC_OK:
  1320. break;
  1321. case SVC_DENIED:
  1322. goto auth_err;
  1323. case SVC_DROP:
  1324. goto drop;
  1325. }
  1326. break;
  1327. default:
  1328. *authp = rpc_autherr_rejectedcred;
  1329. goto auth_err;
  1330. }
  1331. /* now act upon the command: */
  1332. switch (gc->gc_proc) {
  1333. case RPC_GSS_PROC_DESTROY:
  1334. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1335. goto auth_err;
  1336. /* Delete the entry from the cache_list and call cache_put */
  1337. sunrpc_cache_unhash(sn->rsc_cache, &rsci->h);
  1338. if (resv->iov_len + 4 > PAGE_SIZE)
  1339. goto drop;
  1340. svc_putnl(resv, RPC_SUCCESS);
  1341. goto complete;
  1342. case RPC_GSS_PROC_DATA:
  1343. *authp = rpcsec_gsserr_ctxproblem;
  1344. svcdata->verf_start = resv->iov_base + resv->iov_len;
  1345. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1346. goto auth_err;
  1347. rqstp->rq_cred = rsci->cred;
  1348. get_group_info(rsci->cred.cr_group_info);
  1349. *authp = rpc_autherr_badcred;
  1350. switch (gc->gc_svc) {
  1351. case RPC_GSS_SVC_NONE:
  1352. break;
  1353. case RPC_GSS_SVC_INTEGRITY:
  1354. /* placeholders for length and seq. number: */
  1355. svc_putnl(resv, 0);
  1356. svc_putnl(resv, 0);
  1357. if (unwrap_integ_data(rqstp, &rqstp->rq_arg,
  1358. gc->gc_seq, rsci->mechctx))
  1359. goto garbage_args;
  1360. rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE;
  1361. break;
  1362. case RPC_GSS_SVC_PRIVACY:
  1363. /* placeholders for length and seq. number: */
  1364. svc_putnl(resv, 0);
  1365. svc_putnl(resv, 0);
  1366. if (unwrap_priv_data(rqstp, &rqstp->rq_arg,
  1367. gc->gc_seq, rsci->mechctx))
  1368. goto garbage_args;
  1369. rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE * 2;
  1370. break;
  1371. default:
  1372. goto auth_err;
  1373. }
  1374. svcdata->rsci = rsci;
  1375. cache_get(&rsci->h);
  1376. rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
  1377. rsci->mechctx->mech_type,
  1378. GSS_C_QOP_DEFAULT,
  1379. gc->gc_svc);
  1380. ret = SVC_OK;
  1381. goto out;
  1382. }
  1383. garbage_args:
  1384. ret = SVC_GARBAGE;
  1385. goto out;
  1386. auth_err:
  1387. /* Restore write pointer to its original value: */
  1388. xdr_ressize_check(rqstp, reject_stat);
  1389. ret = SVC_DENIED;
  1390. goto out;
  1391. complete:
  1392. ret = SVC_COMPLETE;
  1393. goto out;
  1394. drop:
  1395. ret = SVC_CLOSE;
  1396. out:
  1397. if (rsci)
  1398. cache_put(&rsci->h, sn->rsc_cache);
  1399. return ret;
  1400. }
  1401. static __be32 *
  1402. svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
  1403. {
  1404. __be32 *p;
  1405. u32 verf_len;
  1406. p = gsd->verf_start;
  1407. gsd->verf_start = NULL;
  1408. /* If the reply stat is nonzero, don't wrap: */
  1409. if (*(p-1) != rpc_success)
  1410. return NULL;
  1411. /* Skip the verifier: */
  1412. p += 1;
  1413. verf_len = ntohl(*p++);
  1414. p += XDR_QUADLEN(verf_len);
  1415. /* move accept_stat to right place: */
  1416. memcpy(p, p + 2, 4);
  1417. /* Also don't wrap if the accept stat is nonzero: */
  1418. if (*p != rpc_success) {
  1419. resbuf->head[0].iov_len -= 2 * 4;
  1420. return NULL;
  1421. }
  1422. p++;
  1423. return p;
  1424. }
  1425. static inline int
  1426. svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
  1427. {
  1428. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1429. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1430. struct xdr_buf *resbuf = &rqstp->rq_res;
  1431. struct xdr_buf integ_buf;
  1432. struct xdr_netobj mic;
  1433. struct kvec *resv;
  1434. __be32 *p;
  1435. int integ_offset, integ_len;
  1436. int stat = -EINVAL;
  1437. p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
  1438. if (p == NULL)
  1439. goto out;
  1440. integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
  1441. integ_len = resbuf->len - integ_offset;
  1442. BUG_ON(integ_len % 4);
  1443. *p++ = htonl(integ_len);
  1444. *p++ = htonl(gc->gc_seq);
  1445. if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len)) {
  1446. WARN_ON_ONCE(1);
  1447. goto out_err;
  1448. }
  1449. if (resbuf->tail[0].iov_base == NULL) {
  1450. if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1451. goto out_err;
  1452. resbuf->tail[0].iov_base = resbuf->head[0].iov_base
  1453. + resbuf->head[0].iov_len;
  1454. resbuf->tail[0].iov_len = 0;
  1455. }
  1456. resv = &resbuf->tail[0];
  1457. mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
  1458. if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
  1459. goto out_err;
  1460. svc_putnl(resv, mic.len);
  1461. memset(mic.data + mic.len, 0,
  1462. round_up_to_quad(mic.len) - mic.len);
  1463. resv->iov_len += XDR_QUADLEN(mic.len) << 2;
  1464. /* not strictly required: */
  1465. resbuf->len += XDR_QUADLEN(mic.len) << 2;
  1466. BUG_ON(resv->iov_len > PAGE_SIZE);
  1467. out:
  1468. stat = 0;
  1469. out_err:
  1470. return stat;
  1471. }
  1472. static inline int
  1473. svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
  1474. {
  1475. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1476. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1477. struct xdr_buf *resbuf = &rqstp->rq_res;
  1478. struct page **inpages = NULL;
  1479. __be32 *p, *len;
  1480. int offset;
  1481. int pad;
  1482. p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
  1483. if (p == NULL)
  1484. return 0;
  1485. len = p++;
  1486. offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
  1487. *p++ = htonl(gc->gc_seq);
  1488. inpages = resbuf->pages;
  1489. /* XXX: Would be better to write some xdr helper functions for
  1490. * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
  1491. /*
  1492. * If there is currently tail data, make sure there is
  1493. * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
  1494. * the page, and move the current tail data such that
  1495. * there is RPC_MAX_AUTH_SIZE slack space available in
  1496. * both the head and tail.
  1497. */
  1498. if (resbuf->tail[0].iov_base) {
  1499. BUG_ON(resbuf->tail[0].iov_base >= resbuf->head[0].iov_base
  1500. + PAGE_SIZE);
  1501. BUG_ON(resbuf->tail[0].iov_base < resbuf->head[0].iov_base);
  1502. if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
  1503. + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1504. return -ENOMEM;
  1505. memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
  1506. resbuf->tail[0].iov_base,
  1507. resbuf->tail[0].iov_len);
  1508. resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
  1509. }
  1510. /*
  1511. * If there is no current tail data, make sure there is
  1512. * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
  1513. * allotted page, and set up tail information such that there
  1514. * is RPC_MAX_AUTH_SIZE slack space available in both the
  1515. * head and tail.
  1516. */
  1517. if (resbuf->tail[0].iov_base == NULL) {
  1518. if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1519. return -ENOMEM;
  1520. resbuf->tail[0].iov_base = resbuf->head[0].iov_base
  1521. + resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
  1522. resbuf->tail[0].iov_len = 0;
  1523. }
  1524. if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
  1525. return -ENOMEM;
  1526. *len = htonl(resbuf->len - offset);
  1527. pad = 3 - ((resbuf->len - offset - 1)&3);
  1528. p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
  1529. memset(p, 0, pad);
  1530. resbuf->tail[0].iov_len += pad;
  1531. resbuf->len += pad;
  1532. return 0;
  1533. }
  1534. static int
  1535. svcauth_gss_release(struct svc_rqst *rqstp)
  1536. {
  1537. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1538. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1539. struct xdr_buf *resbuf = &rqstp->rq_res;
  1540. int stat = -EINVAL;
  1541. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  1542. if (gc->gc_proc != RPC_GSS_PROC_DATA)
  1543. goto out;
  1544. /* Release can be called twice, but we only wrap once. */
  1545. if (gsd->verf_start == NULL)
  1546. goto out;
  1547. /* normally not set till svc_send, but we need it here: */
  1548. /* XXX: what for? Do we mess it up the moment we call svc_putu32
  1549. * or whatever? */
  1550. resbuf->len = total_buf_len(resbuf);
  1551. switch (gc->gc_svc) {
  1552. case RPC_GSS_SVC_NONE:
  1553. break;
  1554. case RPC_GSS_SVC_INTEGRITY:
  1555. stat = svcauth_gss_wrap_resp_integ(rqstp);
  1556. if (stat)
  1557. goto out_err;
  1558. break;
  1559. case RPC_GSS_SVC_PRIVACY:
  1560. stat = svcauth_gss_wrap_resp_priv(rqstp);
  1561. if (stat)
  1562. goto out_err;
  1563. break;
  1564. /*
  1565. * For any other gc_svc value, svcauth_gss_accept() already set
  1566. * the auth_error appropriately; just fall through:
  1567. */
  1568. }
  1569. out:
  1570. stat = 0;
  1571. out_err:
  1572. if (rqstp->rq_client)
  1573. auth_domain_put(rqstp->rq_client);
  1574. rqstp->rq_client = NULL;
  1575. if (rqstp->rq_gssclient)
  1576. auth_domain_put(rqstp->rq_gssclient);
  1577. rqstp->rq_gssclient = NULL;
  1578. if (rqstp->rq_cred.cr_group_info)
  1579. put_group_info(rqstp->rq_cred.cr_group_info);
  1580. rqstp->rq_cred.cr_group_info = NULL;
  1581. if (gsd->rsci)
  1582. cache_put(&gsd->rsci->h, sn->rsc_cache);
  1583. gsd->rsci = NULL;
  1584. return stat;
  1585. }
  1586. static void
  1587. svcauth_gss_domain_release_rcu(struct rcu_head *head)
  1588. {
  1589. struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
  1590. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  1591. kfree(dom->name);
  1592. kfree(gd);
  1593. }
  1594. static void
  1595. svcauth_gss_domain_release(struct auth_domain *dom)
  1596. {
  1597. call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu);
  1598. }
  1599. static struct auth_ops svcauthops_gss = {
  1600. .name = "rpcsec_gss",
  1601. .owner = THIS_MODULE,
  1602. .flavour = RPC_AUTH_GSS,
  1603. .accept = svcauth_gss_accept,
  1604. .release = svcauth_gss_release,
  1605. .domain_release = svcauth_gss_domain_release,
  1606. .set_client = svcauth_gss_set_client,
  1607. };
  1608. static int rsi_cache_create_net(struct net *net)
  1609. {
  1610. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1611. struct cache_detail *cd;
  1612. int err;
  1613. cd = cache_create_net(&rsi_cache_template, net);
  1614. if (IS_ERR(cd))
  1615. return PTR_ERR(cd);
  1616. err = cache_register_net(cd, net);
  1617. if (err) {
  1618. cache_destroy_net(cd, net);
  1619. return err;
  1620. }
  1621. sn->rsi_cache = cd;
  1622. return 0;
  1623. }
  1624. static void rsi_cache_destroy_net(struct net *net)
  1625. {
  1626. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1627. struct cache_detail *cd = sn->rsi_cache;
  1628. sn->rsi_cache = NULL;
  1629. cache_purge(cd);
  1630. cache_unregister_net(cd, net);
  1631. cache_destroy_net(cd, net);
  1632. }
  1633. static int rsc_cache_create_net(struct net *net)
  1634. {
  1635. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1636. struct cache_detail *cd;
  1637. int err;
  1638. cd = cache_create_net(&rsc_cache_template, net);
  1639. if (IS_ERR(cd))
  1640. return PTR_ERR(cd);
  1641. err = cache_register_net(cd, net);
  1642. if (err) {
  1643. cache_destroy_net(cd, net);
  1644. return err;
  1645. }
  1646. sn->rsc_cache = cd;
  1647. return 0;
  1648. }
  1649. static void rsc_cache_destroy_net(struct net *net)
  1650. {
  1651. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1652. struct cache_detail *cd = sn->rsc_cache;
  1653. sn->rsc_cache = NULL;
  1654. cache_purge(cd);
  1655. cache_unregister_net(cd, net);
  1656. cache_destroy_net(cd, net);
  1657. }
  1658. int
  1659. gss_svc_init_net(struct net *net)
  1660. {
  1661. int rv;
  1662. rv = rsc_cache_create_net(net);
  1663. if (rv)
  1664. return rv;
  1665. rv = rsi_cache_create_net(net);
  1666. if (rv)
  1667. goto out1;
  1668. rv = create_use_gss_proxy_proc_entry(net);
  1669. if (rv)
  1670. goto out2;
  1671. return 0;
  1672. out2:
  1673. destroy_use_gss_proxy_proc_entry(net);
  1674. out1:
  1675. rsc_cache_destroy_net(net);
  1676. return rv;
  1677. }
  1678. void
  1679. gss_svc_shutdown_net(struct net *net)
  1680. {
  1681. destroy_use_gss_proxy_proc_entry(net);
  1682. rsi_cache_destroy_net(net);
  1683. rsc_cache_destroy_net(net);
  1684. }
  1685. int
  1686. gss_svc_init(void)
  1687. {
  1688. return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
  1689. }
  1690. void
  1691. gss_svc_shutdown(void)
  1692. {
  1693. svc_auth_unregister(RPC_AUTH_GSS);
  1694. }