svcauth_gss.c 34 KB

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