svcauth_gss.c 38 KB

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