svcauth_gss.c 48 KB

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