svcauth_gss.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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_upcall(struct cache_detail *cd, struct cache_head *h)
  162. {
  163. return sunrpc_cache_pipe_upcall(cd, h, rsi_request);
  164. }
  165. static int rsi_parse(struct cache_detail *cd,
  166. char *mesg, int mlen)
  167. {
  168. /* context token expiry major minor context token */
  169. char *buf = mesg;
  170. char *ep;
  171. int len;
  172. struct rsi rsii, *rsip = NULL;
  173. time_t expiry;
  174. int status = -EINVAL;
  175. memset(&rsii, 0, sizeof(rsii));
  176. /* handle */
  177. len = qword_get(&mesg, buf, mlen);
  178. if (len < 0)
  179. goto out;
  180. status = -ENOMEM;
  181. if (dup_to_netobj(&rsii.in_handle, buf, len))
  182. goto out;
  183. /* token */
  184. len = qword_get(&mesg, buf, mlen);
  185. status = -EINVAL;
  186. if (len < 0)
  187. goto out;
  188. status = -ENOMEM;
  189. if (dup_to_netobj(&rsii.in_token, buf, len))
  190. goto out;
  191. rsip = rsi_lookup(cd, &rsii);
  192. if (!rsip)
  193. goto out;
  194. rsii.h.flags = 0;
  195. /* expiry */
  196. expiry = get_expiry(&mesg);
  197. status = -EINVAL;
  198. if (expiry == 0)
  199. goto out;
  200. /* major/minor */
  201. len = qword_get(&mesg, buf, mlen);
  202. if (len <= 0)
  203. goto out;
  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. rsii.h.expiry_time = expiry;
  229. rsip = rsi_update(cd, &rsii, rsip);
  230. status = 0;
  231. out:
  232. rsi_free(&rsii);
  233. if (rsip)
  234. cache_put(&rsip->h, cd);
  235. else
  236. status = -ENOMEM;
  237. return status;
  238. }
  239. static struct cache_detail rsi_cache_template = {
  240. .owner = THIS_MODULE,
  241. .hash_size = RSI_HASHMAX,
  242. .name = "auth.rpcsec.init",
  243. .cache_put = rsi_put,
  244. .cache_upcall = rsi_upcall,
  245. .cache_parse = rsi_parse,
  246. .match = rsi_match,
  247. .init = rsi_init,
  248. .update = update_rsi,
  249. .alloc = rsi_alloc,
  250. };
  251. static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
  252. {
  253. struct cache_head *ch;
  254. int hash = rsi_hash(item);
  255. ch = sunrpc_cache_lookup(cd, &item->h, hash);
  256. if (ch)
  257. return container_of(ch, struct rsi, h);
  258. else
  259. return NULL;
  260. }
  261. static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old)
  262. {
  263. struct cache_head *ch;
  264. int hash = rsi_hash(new);
  265. ch = sunrpc_cache_update(cd, &new->h,
  266. &old->h, hash);
  267. if (ch)
  268. return container_of(ch, struct rsi, h);
  269. else
  270. return NULL;
  271. }
  272. /*
  273. * The rpcsec_context cache is used to store a context that is
  274. * used in data exchange.
  275. * The key is a context handle. The content is:
  276. * uid, gidlist, mechanism, service-set, mech-specific-data
  277. */
  278. #define RSC_HASHBITS 10
  279. #define RSC_HASHMAX (1<<RSC_HASHBITS)
  280. #define GSS_SEQ_WIN 128
  281. struct gss_svc_seq_data {
  282. /* highest seq number seen so far: */
  283. int sd_max;
  284. /* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
  285. * sd_win is nonzero iff sequence number i has been seen already: */
  286. unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
  287. spinlock_t sd_lock;
  288. };
  289. struct rsc {
  290. struct cache_head h;
  291. struct xdr_netobj handle;
  292. struct svc_cred cred;
  293. struct gss_svc_seq_data seqdata;
  294. struct gss_ctx *mechctx;
  295. };
  296. static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old);
  297. static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item);
  298. static void rsc_free(struct rsc *rsci)
  299. {
  300. kfree(rsci->handle.data);
  301. if (rsci->mechctx)
  302. gss_delete_sec_context(&rsci->mechctx);
  303. free_svc_cred(&rsci->cred);
  304. }
  305. static void rsc_put(struct kref *ref)
  306. {
  307. struct rsc *rsci = container_of(ref, struct rsc, h.ref);
  308. rsc_free(rsci);
  309. kfree(rsci);
  310. }
  311. static inline int
  312. rsc_hash(struct rsc *rsci)
  313. {
  314. return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
  315. }
  316. static int
  317. rsc_match(struct cache_head *a, struct cache_head *b)
  318. {
  319. struct rsc *new = container_of(a, struct rsc, h);
  320. struct rsc *tmp = container_of(b, struct rsc, h);
  321. return netobj_equal(&new->handle, &tmp->handle);
  322. }
  323. static void
  324. rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
  325. {
  326. struct rsc *new = container_of(cnew, struct rsc, h);
  327. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  328. new->handle.len = tmp->handle.len;
  329. tmp->handle.len = 0;
  330. new->handle.data = tmp->handle.data;
  331. tmp->handle.data = NULL;
  332. new->mechctx = NULL;
  333. new->cred.cr_group_info = NULL;
  334. new->cred.cr_principal = NULL;
  335. }
  336. static void
  337. update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
  338. {
  339. struct rsc *new = container_of(cnew, struct rsc, h);
  340. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  341. new->mechctx = tmp->mechctx;
  342. tmp->mechctx = NULL;
  343. memset(&new->seqdata, 0, sizeof(new->seqdata));
  344. spin_lock_init(&new->seqdata.sd_lock);
  345. new->cred = tmp->cred;
  346. tmp->cred.cr_group_info = NULL;
  347. new->cred.cr_principal = tmp->cred.cr_principal;
  348. tmp->cred.cr_principal = NULL;
  349. }
  350. static struct cache_head *
  351. rsc_alloc(void)
  352. {
  353. struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
  354. if (rsci)
  355. return &rsci->h;
  356. else
  357. return NULL;
  358. }
  359. static int rsc_parse(struct cache_detail *cd,
  360. char *mesg, int mlen)
  361. {
  362. /* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
  363. char *buf = mesg;
  364. int id;
  365. int len, rv;
  366. struct rsc rsci, *rscp = NULL;
  367. time_t expiry;
  368. int status = -EINVAL;
  369. struct gss_api_mech *gm = NULL;
  370. memset(&rsci, 0, sizeof(rsci));
  371. /* context handle */
  372. len = qword_get(&mesg, buf, mlen);
  373. if (len < 0) goto out;
  374. status = -ENOMEM;
  375. if (dup_to_netobj(&rsci.handle, buf, len))
  376. goto out;
  377. rsci.h.flags = 0;
  378. /* expiry */
  379. expiry = get_expiry(&mesg);
  380. status = -EINVAL;
  381. if (expiry == 0)
  382. goto out;
  383. rscp = rsc_lookup(cd, &rsci);
  384. if (!rscp)
  385. goto out;
  386. /* uid, or NEGATIVE */
  387. rv = get_int(&mesg, &id);
  388. if (rv == -EINVAL)
  389. goto out;
  390. if (rv == -ENOENT)
  391. set_bit(CACHE_NEGATIVE, &rsci.h.flags);
  392. else {
  393. int N, i;
  394. /* uid */
  395. rsci.cred.cr_uid = make_kuid(&init_user_ns, id);
  396. if (!uid_valid(rsci.cred.cr_uid))
  397. goto out;
  398. /* gid */
  399. if (get_int(&mesg, &id))
  400. goto out;
  401. rsci.cred.cr_gid = make_kgid(&init_user_ns, id);
  402. if (!gid_valid(rsci.cred.cr_gid))
  403. goto out;
  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 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. integ_len = svc_getnl(&buf->head[0]);
  726. if (integ_len & 3)
  727. return stat;
  728. if (integ_len > buf->len)
  729. return stat;
  730. if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
  731. BUG();
  732. /* copy out mic... */
  733. if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
  734. BUG();
  735. if (mic.len > RPC_MAX_AUTH_SIZE)
  736. return stat;
  737. mic.data = kmalloc(mic.len, GFP_KERNEL);
  738. if (!mic.data)
  739. return stat;
  740. if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
  741. goto out;
  742. maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);
  743. if (maj_stat != GSS_S_COMPLETE)
  744. goto out;
  745. if (svc_getnl(&buf->head[0]) != seq)
  746. goto out;
  747. stat = 0;
  748. out:
  749. kfree(mic.data);
  750. return stat;
  751. }
  752. static inline int
  753. total_buf_len(struct xdr_buf *buf)
  754. {
  755. return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;
  756. }
  757. static void
  758. fix_priv_head(struct xdr_buf *buf, int pad)
  759. {
  760. if (buf->page_len == 0) {
  761. /* We need to adjust head and buf->len in tandem in this
  762. * case to make svc_defer() work--it finds the original
  763. * buffer start using buf->len - buf->head[0].iov_len. */
  764. buf->head[0].iov_len -= pad;
  765. }
  766. }
  767. static int
  768. unwrap_priv_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  769. {
  770. u32 priv_len, maj_stat;
  771. int pad, saved_len, remaining_len, offset;
  772. rqstp->rq_splice_ok = 0;
  773. priv_len = svc_getnl(&buf->head[0]);
  774. if (rqstp->rq_deferred) {
  775. /* Already decrypted last time through! The sequence number
  776. * check at out_seq is unnecessary but harmless: */
  777. goto out_seq;
  778. }
  779. /* buf->len is the number of bytes from the original start of the
  780. * request to the end, where head[0].iov_len is just the bytes
  781. * not yet read from the head, so these two values are different: */
  782. remaining_len = total_buf_len(buf);
  783. if (priv_len > remaining_len)
  784. return -EINVAL;
  785. pad = remaining_len - priv_len;
  786. buf->len -= pad;
  787. fix_priv_head(buf, pad);
  788. /* Maybe it would be better to give gss_unwrap a length parameter: */
  789. saved_len = buf->len;
  790. buf->len = priv_len;
  791. maj_stat = gss_unwrap(ctx, 0, buf);
  792. pad = priv_len - buf->len;
  793. buf->len = saved_len;
  794. buf->len -= pad;
  795. /* The upper layers assume the buffer is aligned on 4-byte boundaries.
  796. * In the krb5p case, at least, the data ends up offset, so we need to
  797. * move it around. */
  798. /* XXX: This is very inefficient. It would be better to either do
  799. * this while we encrypt, or maybe in the receive code, if we can peak
  800. * ahead and work out the service and mechanism there. */
  801. offset = buf->head[0].iov_len % 4;
  802. if (offset) {
  803. buf->buflen = RPCSVC_MAXPAYLOAD;
  804. xdr_shift_buf(buf, offset);
  805. fix_priv_head(buf, pad);
  806. }
  807. if (maj_stat != GSS_S_COMPLETE)
  808. return -EINVAL;
  809. out_seq:
  810. if (svc_getnl(&buf->head[0]) != seq)
  811. return -EINVAL;
  812. return 0;
  813. }
  814. struct gss_svc_data {
  815. /* decoded gss client cred: */
  816. struct rpc_gss_wire_cred clcred;
  817. /* save a pointer to the beginning of the encoded verifier,
  818. * for use in encryption/checksumming in svcauth_gss_release: */
  819. __be32 *verf_start;
  820. struct rsc *rsci;
  821. };
  822. static int
  823. svcauth_gss_set_client(struct svc_rqst *rqstp)
  824. {
  825. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  826. struct rsc *rsci = svcdata->rsci;
  827. struct rpc_gss_wire_cred *gc = &svcdata->clcred;
  828. int stat;
  829. /*
  830. * A gss export can be specified either by:
  831. * export *(sec=krb5,rw)
  832. * or by
  833. * export gss/krb5(rw)
  834. * The latter is deprecated; but for backwards compatibility reasons
  835. * the nfsd code will still fall back on trying it if the former
  836. * doesn't work; so we try to make both available to nfsd, below.
  837. */
  838. rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
  839. if (rqstp->rq_gssclient == NULL)
  840. return SVC_DENIED;
  841. stat = svcauth_unix_set_client(rqstp);
  842. if (stat == SVC_DROP || stat == SVC_CLOSE)
  843. return stat;
  844. return SVC_OK;
  845. }
  846. static inline int
  847. gss_write_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
  848. struct xdr_netobj *out_handle, int *major_status)
  849. {
  850. struct rsc *rsci;
  851. int rc;
  852. if (*major_status != GSS_S_COMPLETE)
  853. return gss_write_null_verf(rqstp);
  854. rsci = gss_svc_searchbyctx(cd, out_handle);
  855. if (rsci == NULL) {
  856. *major_status = GSS_S_NO_CONTEXT;
  857. return gss_write_null_verf(rqstp);
  858. }
  859. rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
  860. cache_put(&rsci->h, cd);
  861. return rc;
  862. }
  863. static inline int
  864. gss_read_verf(struct rpc_gss_wire_cred *gc,
  865. struct kvec *argv, __be32 *authp,
  866. struct xdr_netobj *in_handle,
  867. struct xdr_netobj *in_token)
  868. {
  869. struct xdr_netobj tmpobj;
  870. /* Read the verifier; should be NULL: */
  871. *authp = rpc_autherr_badverf;
  872. if (argv->iov_len < 2 * 4)
  873. return SVC_DENIED;
  874. if (svc_getnl(argv) != RPC_AUTH_NULL)
  875. return SVC_DENIED;
  876. if (svc_getnl(argv) != 0)
  877. return SVC_DENIED;
  878. /* Martial context handle and token for upcall: */
  879. *authp = rpc_autherr_badcred;
  880. if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
  881. return SVC_DENIED;
  882. if (dup_netobj(in_handle, &gc->gc_ctx))
  883. return SVC_CLOSE;
  884. *authp = rpc_autherr_badverf;
  885. if (svc_safe_getnetobj(argv, &tmpobj)) {
  886. kfree(in_handle->data);
  887. return SVC_DENIED;
  888. }
  889. if (dup_netobj(in_token, &tmpobj)) {
  890. kfree(in_handle->data);
  891. return SVC_CLOSE;
  892. }
  893. return 0;
  894. }
  895. static inline int
  896. gss_write_resv(struct kvec *resv, size_t size_limit,
  897. struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
  898. int major_status, int minor_status)
  899. {
  900. if (resv->iov_len + 4 > size_limit)
  901. return -1;
  902. svc_putnl(resv, RPC_SUCCESS);
  903. if (svc_safe_putnetobj(resv, out_handle))
  904. return -1;
  905. if (resv->iov_len + 3 * 4 > size_limit)
  906. return -1;
  907. svc_putnl(resv, major_status);
  908. svc_putnl(resv, minor_status);
  909. svc_putnl(resv, GSS_SEQ_WIN);
  910. if (svc_safe_putnetobj(resv, out_token))
  911. return -1;
  912. return 0;
  913. }
  914. /*
  915. * Having read the cred already and found we're in the context
  916. * initiation case, read the verifier and initiate (or check the results
  917. * of) upcalls to userspace for help with context initiation. If
  918. * the upcall results are available, write the verifier and result.
  919. * Otherwise, drop the request pending an answer to the upcall.
  920. */
  921. static int svcauth_gss_handle_init(struct svc_rqst *rqstp,
  922. struct rpc_gss_wire_cred *gc, __be32 *authp)
  923. {
  924. struct kvec *argv = &rqstp->rq_arg.head[0];
  925. struct kvec *resv = &rqstp->rq_res.head[0];
  926. struct rsi *rsip, rsikey;
  927. int ret;
  928. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  929. memset(&rsikey, 0, sizeof(rsikey));
  930. ret = gss_read_verf(gc, argv, authp,
  931. &rsikey.in_handle, &rsikey.in_token);
  932. if (ret)
  933. return ret;
  934. /* Perform upcall, or find upcall result: */
  935. rsip = rsi_lookup(sn->rsi_cache, &rsikey);
  936. rsi_free(&rsikey);
  937. if (!rsip)
  938. return SVC_CLOSE;
  939. if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
  940. /* No upcall result: */
  941. return SVC_CLOSE;
  942. ret = SVC_CLOSE;
  943. /* Got an answer to the upcall; use it: */
  944. if (gss_write_init_verf(sn->rsc_cache, rqstp,
  945. &rsip->out_handle, &rsip->major_status))
  946. goto out;
  947. if (gss_write_resv(resv, PAGE_SIZE,
  948. &rsip->out_handle, &rsip->out_token,
  949. rsip->major_status, rsip->minor_status))
  950. goto out;
  951. ret = SVC_COMPLETE;
  952. out:
  953. cache_put(&rsip->h, sn->rsi_cache);
  954. return ret;
  955. }
  956. /*
  957. * Accept an rpcsec packet.
  958. * If context establishment, punt to user space
  959. * If data exchange, verify/decrypt
  960. * If context destruction, handle here
  961. * In the context establishment and destruction case we encode
  962. * response here and return SVC_COMPLETE.
  963. */
  964. static int
  965. svcauth_gss_accept(struct svc_rqst *rqstp, __be32 *authp)
  966. {
  967. struct kvec *argv = &rqstp->rq_arg.head[0];
  968. struct kvec *resv = &rqstp->rq_res.head[0];
  969. u32 crlen;
  970. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  971. struct rpc_gss_wire_cred *gc;
  972. struct rsc *rsci = NULL;
  973. __be32 *rpcstart;
  974. __be32 *reject_stat = resv->iov_base + resv->iov_len;
  975. int ret;
  976. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  977. dprintk("RPC: svcauth_gss: argv->iov_len = %zd\n",
  978. argv->iov_len);
  979. *authp = rpc_autherr_badcred;
  980. if (!svcdata)
  981. svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
  982. if (!svcdata)
  983. goto auth_err;
  984. rqstp->rq_auth_data = svcdata;
  985. svcdata->verf_start = NULL;
  986. svcdata->rsci = NULL;
  987. gc = &svcdata->clcred;
  988. /* start of rpc packet is 7 u32's back from here:
  989. * xid direction rpcversion prog vers proc flavour
  990. */
  991. rpcstart = argv->iov_base;
  992. rpcstart -= 7;
  993. /* credential is:
  994. * version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
  995. * at least 5 u32s, and is preceded by length, so that makes 6.
  996. */
  997. if (argv->iov_len < 5 * 4)
  998. goto auth_err;
  999. crlen = svc_getnl(argv);
  1000. if (svc_getnl(argv) != RPC_GSS_VERSION)
  1001. goto auth_err;
  1002. gc->gc_proc = svc_getnl(argv);
  1003. gc->gc_seq = svc_getnl(argv);
  1004. gc->gc_svc = svc_getnl(argv);
  1005. if (svc_safe_getnetobj(argv, &gc->gc_ctx))
  1006. goto auth_err;
  1007. if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
  1008. goto auth_err;
  1009. if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
  1010. goto auth_err;
  1011. *authp = rpc_autherr_badverf;
  1012. switch (gc->gc_proc) {
  1013. case RPC_GSS_PROC_INIT:
  1014. case RPC_GSS_PROC_CONTINUE_INIT:
  1015. return svcauth_gss_handle_init(rqstp, gc, authp);
  1016. case RPC_GSS_PROC_DATA:
  1017. case RPC_GSS_PROC_DESTROY:
  1018. /* Look up the context, and check the verifier: */
  1019. *authp = rpcsec_gsserr_credproblem;
  1020. rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
  1021. if (!rsci)
  1022. goto auth_err;
  1023. switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
  1024. case SVC_OK:
  1025. break;
  1026. case SVC_DENIED:
  1027. goto auth_err;
  1028. case SVC_DROP:
  1029. goto drop;
  1030. }
  1031. break;
  1032. default:
  1033. *authp = rpc_autherr_rejectedcred;
  1034. goto auth_err;
  1035. }
  1036. /* now act upon the command: */
  1037. switch (gc->gc_proc) {
  1038. case RPC_GSS_PROC_DESTROY:
  1039. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1040. goto auth_err;
  1041. rsci->h.expiry_time = get_seconds();
  1042. set_bit(CACHE_NEGATIVE, &rsci->h.flags);
  1043. if (resv->iov_len + 4 > PAGE_SIZE)
  1044. goto drop;
  1045. svc_putnl(resv, RPC_SUCCESS);
  1046. goto complete;
  1047. case RPC_GSS_PROC_DATA:
  1048. *authp = rpcsec_gsserr_ctxproblem;
  1049. svcdata->verf_start = resv->iov_base + resv->iov_len;
  1050. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1051. goto auth_err;
  1052. rqstp->rq_cred = rsci->cred;
  1053. get_group_info(rsci->cred.cr_group_info);
  1054. *authp = rpc_autherr_badcred;
  1055. switch (gc->gc_svc) {
  1056. case RPC_GSS_SVC_NONE:
  1057. break;
  1058. case RPC_GSS_SVC_INTEGRITY:
  1059. /* placeholders for length and seq. number: */
  1060. svc_putnl(resv, 0);
  1061. svc_putnl(resv, 0);
  1062. if (unwrap_integ_data(&rqstp->rq_arg,
  1063. gc->gc_seq, rsci->mechctx))
  1064. goto garbage_args;
  1065. break;
  1066. case RPC_GSS_SVC_PRIVACY:
  1067. /* placeholders for length and seq. number: */
  1068. svc_putnl(resv, 0);
  1069. svc_putnl(resv, 0);
  1070. if (unwrap_priv_data(rqstp, &rqstp->rq_arg,
  1071. gc->gc_seq, rsci->mechctx))
  1072. goto garbage_args;
  1073. break;
  1074. default:
  1075. goto auth_err;
  1076. }
  1077. svcdata->rsci = rsci;
  1078. cache_get(&rsci->h);
  1079. rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
  1080. rsci->mechctx->mech_type, gc->gc_svc);
  1081. ret = SVC_OK;
  1082. goto out;
  1083. }
  1084. garbage_args:
  1085. ret = SVC_GARBAGE;
  1086. goto out;
  1087. auth_err:
  1088. /* Restore write pointer to its original value: */
  1089. xdr_ressize_check(rqstp, reject_stat);
  1090. ret = SVC_DENIED;
  1091. goto out;
  1092. complete:
  1093. ret = SVC_COMPLETE;
  1094. goto out;
  1095. drop:
  1096. ret = SVC_DROP;
  1097. out:
  1098. if (rsci)
  1099. cache_put(&rsci->h, sn->rsc_cache);
  1100. return ret;
  1101. }
  1102. static __be32 *
  1103. svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
  1104. {
  1105. __be32 *p;
  1106. u32 verf_len;
  1107. p = gsd->verf_start;
  1108. gsd->verf_start = NULL;
  1109. /* If the reply stat is nonzero, don't wrap: */
  1110. if (*(p-1) != rpc_success)
  1111. return NULL;
  1112. /* Skip the verifier: */
  1113. p += 1;
  1114. verf_len = ntohl(*p++);
  1115. p += XDR_QUADLEN(verf_len);
  1116. /* move accept_stat to right place: */
  1117. memcpy(p, p + 2, 4);
  1118. /* Also don't wrap if the accept stat is nonzero: */
  1119. if (*p != rpc_success) {
  1120. resbuf->head[0].iov_len -= 2 * 4;
  1121. return NULL;
  1122. }
  1123. p++;
  1124. return p;
  1125. }
  1126. static inline int
  1127. svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
  1128. {
  1129. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1130. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1131. struct xdr_buf *resbuf = &rqstp->rq_res;
  1132. struct xdr_buf integ_buf;
  1133. struct xdr_netobj mic;
  1134. struct kvec *resv;
  1135. __be32 *p;
  1136. int integ_offset, integ_len;
  1137. int stat = -EINVAL;
  1138. p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
  1139. if (p == NULL)
  1140. goto out;
  1141. integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
  1142. integ_len = resbuf->len - integ_offset;
  1143. BUG_ON(integ_len % 4);
  1144. *p++ = htonl(integ_len);
  1145. *p++ = htonl(gc->gc_seq);
  1146. if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset,
  1147. integ_len))
  1148. BUG();
  1149. if (resbuf->tail[0].iov_base == NULL) {
  1150. if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1151. goto out_err;
  1152. resbuf->tail[0].iov_base = resbuf->head[0].iov_base
  1153. + resbuf->head[0].iov_len;
  1154. resbuf->tail[0].iov_len = 0;
  1155. resv = &resbuf->tail[0];
  1156. } else {
  1157. resv = &resbuf->tail[0];
  1158. }
  1159. mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
  1160. if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
  1161. goto out_err;
  1162. svc_putnl(resv, mic.len);
  1163. memset(mic.data + mic.len, 0,
  1164. round_up_to_quad(mic.len) - mic.len);
  1165. resv->iov_len += XDR_QUADLEN(mic.len) << 2;
  1166. /* not strictly required: */
  1167. resbuf->len += XDR_QUADLEN(mic.len) << 2;
  1168. BUG_ON(resv->iov_len > PAGE_SIZE);
  1169. out:
  1170. stat = 0;
  1171. out_err:
  1172. return stat;
  1173. }
  1174. static inline int
  1175. svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
  1176. {
  1177. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1178. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1179. struct xdr_buf *resbuf = &rqstp->rq_res;
  1180. struct page **inpages = NULL;
  1181. __be32 *p, *len;
  1182. int offset;
  1183. int pad;
  1184. p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
  1185. if (p == NULL)
  1186. return 0;
  1187. len = p++;
  1188. offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
  1189. *p++ = htonl(gc->gc_seq);
  1190. inpages = resbuf->pages;
  1191. /* XXX: Would be better to write some xdr helper functions for
  1192. * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
  1193. /*
  1194. * If there is currently tail data, make sure there is
  1195. * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
  1196. * the page, and move the current tail data such that
  1197. * there is RPC_MAX_AUTH_SIZE slack space available in
  1198. * both the head and tail.
  1199. */
  1200. if (resbuf->tail[0].iov_base) {
  1201. BUG_ON(resbuf->tail[0].iov_base >= resbuf->head[0].iov_base
  1202. + PAGE_SIZE);
  1203. BUG_ON(resbuf->tail[0].iov_base < resbuf->head[0].iov_base);
  1204. if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
  1205. + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1206. return -ENOMEM;
  1207. memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
  1208. resbuf->tail[0].iov_base,
  1209. resbuf->tail[0].iov_len);
  1210. resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
  1211. }
  1212. /*
  1213. * If there is no current tail data, make sure there is
  1214. * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
  1215. * allotted page, and set up tail information such that there
  1216. * is RPC_MAX_AUTH_SIZE slack space available in both the
  1217. * head and tail.
  1218. */
  1219. if (resbuf->tail[0].iov_base == NULL) {
  1220. if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1221. return -ENOMEM;
  1222. resbuf->tail[0].iov_base = resbuf->head[0].iov_base
  1223. + resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
  1224. resbuf->tail[0].iov_len = 0;
  1225. }
  1226. if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
  1227. return -ENOMEM;
  1228. *len = htonl(resbuf->len - offset);
  1229. pad = 3 - ((resbuf->len - offset - 1)&3);
  1230. p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
  1231. memset(p, 0, pad);
  1232. resbuf->tail[0].iov_len += pad;
  1233. resbuf->len += pad;
  1234. return 0;
  1235. }
  1236. static int
  1237. svcauth_gss_release(struct svc_rqst *rqstp)
  1238. {
  1239. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1240. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1241. struct xdr_buf *resbuf = &rqstp->rq_res;
  1242. int stat = -EINVAL;
  1243. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  1244. if (gc->gc_proc != RPC_GSS_PROC_DATA)
  1245. goto out;
  1246. /* Release can be called twice, but we only wrap once. */
  1247. if (gsd->verf_start == NULL)
  1248. goto out;
  1249. /* normally not set till svc_send, but we need it here: */
  1250. /* XXX: what for? Do we mess it up the moment we call svc_putu32
  1251. * or whatever? */
  1252. resbuf->len = total_buf_len(resbuf);
  1253. switch (gc->gc_svc) {
  1254. case RPC_GSS_SVC_NONE:
  1255. break;
  1256. case RPC_GSS_SVC_INTEGRITY:
  1257. stat = svcauth_gss_wrap_resp_integ(rqstp);
  1258. if (stat)
  1259. goto out_err;
  1260. break;
  1261. case RPC_GSS_SVC_PRIVACY:
  1262. stat = svcauth_gss_wrap_resp_priv(rqstp);
  1263. if (stat)
  1264. goto out_err;
  1265. break;
  1266. /*
  1267. * For any other gc_svc value, svcauth_gss_accept() already set
  1268. * the auth_error appropriately; just fall through:
  1269. */
  1270. }
  1271. out:
  1272. stat = 0;
  1273. out_err:
  1274. if (rqstp->rq_client)
  1275. auth_domain_put(rqstp->rq_client);
  1276. rqstp->rq_client = NULL;
  1277. if (rqstp->rq_gssclient)
  1278. auth_domain_put(rqstp->rq_gssclient);
  1279. rqstp->rq_gssclient = NULL;
  1280. if (rqstp->rq_cred.cr_group_info)
  1281. put_group_info(rqstp->rq_cred.cr_group_info);
  1282. rqstp->rq_cred.cr_group_info = NULL;
  1283. if (gsd->rsci)
  1284. cache_put(&gsd->rsci->h, sn->rsc_cache);
  1285. gsd->rsci = NULL;
  1286. return stat;
  1287. }
  1288. static void
  1289. svcauth_gss_domain_release(struct auth_domain *dom)
  1290. {
  1291. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  1292. kfree(dom->name);
  1293. kfree(gd);
  1294. }
  1295. static struct auth_ops svcauthops_gss = {
  1296. .name = "rpcsec_gss",
  1297. .owner = THIS_MODULE,
  1298. .flavour = RPC_AUTH_GSS,
  1299. .accept = svcauth_gss_accept,
  1300. .release = svcauth_gss_release,
  1301. .domain_release = svcauth_gss_domain_release,
  1302. .set_client = svcauth_gss_set_client,
  1303. };
  1304. static int rsi_cache_create_net(struct net *net)
  1305. {
  1306. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1307. struct cache_detail *cd;
  1308. int err;
  1309. cd = cache_create_net(&rsi_cache_template, net);
  1310. if (IS_ERR(cd))
  1311. return PTR_ERR(cd);
  1312. err = cache_register_net(cd, net);
  1313. if (err) {
  1314. cache_destroy_net(cd, net);
  1315. return err;
  1316. }
  1317. sn->rsi_cache = cd;
  1318. return 0;
  1319. }
  1320. static void rsi_cache_destroy_net(struct net *net)
  1321. {
  1322. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1323. struct cache_detail *cd = sn->rsi_cache;
  1324. sn->rsi_cache = NULL;
  1325. cache_purge(cd);
  1326. cache_unregister_net(cd, net);
  1327. cache_destroy_net(cd, net);
  1328. }
  1329. static int rsc_cache_create_net(struct net *net)
  1330. {
  1331. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1332. struct cache_detail *cd;
  1333. int err;
  1334. cd = cache_create_net(&rsc_cache_template, net);
  1335. if (IS_ERR(cd))
  1336. return PTR_ERR(cd);
  1337. err = cache_register_net(cd, net);
  1338. if (err) {
  1339. cache_destroy_net(cd, net);
  1340. return err;
  1341. }
  1342. sn->rsc_cache = cd;
  1343. return 0;
  1344. }
  1345. static void rsc_cache_destroy_net(struct net *net)
  1346. {
  1347. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1348. struct cache_detail *cd = sn->rsc_cache;
  1349. sn->rsc_cache = NULL;
  1350. cache_purge(cd);
  1351. cache_unregister_net(cd, net);
  1352. cache_destroy_net(cd, net);
  1353. }
  1354. int
  1355. gss_svc_init_net(struct net *net)
  1356. {
  1357. int rv;
  1358. rv = rsc_cache_create_net(net);
  1359. if (rv)
  1360. return rv;
  1361. rv = rsi_cache_create_net(net);
  1362. if (rv)
  1363. goto out1;
  1364. return 0;
  1365. out1:
  1366. rsc_cache_destroy_net(net);
  1367. return rv;
  1368. }
  1369. void
  1370. gss_svc_shutdown_net(struct net *net)
  1371. {
  1372. rsi_cache_destroy_net(net);
  1373. rsc_cache_destroy_net(net);
  1374. }
  1375. int
  1376. gss_svc_init(void)
  1377. {
  1378. return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
  1379. }
  1380. void
  1381. gss_svc_shutdown(void)
  1382. {
  1383. svc_auth_unregister(RPC_AUTH_GSS);
  1384. }