svcauth_gss.c 39 KB

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