svcauth_gss.c 35 KB

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