svcauth_gss.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * Neil Brown <neilb@cse.unsw.edu.au>
  3. * J. Bruce Fields <bfields@umich.edu>
  4. * Andy Adamson <andros@umich.edu>
  5. * Dug Song <dugsong@monkey.org>
  6. *
  7. * RPCSEC_GSS server authentication.
  8. * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
  9. * (gssapi)
  10. *
  11. * The RPCSEC_GSS involves three stages:
  12. * 1/ context creation
  13. * 2/ data exchange
  14. * 3/ context destruction
  15. *
  16. * Context creation is handled largely by upcalls to user-space.
  17. * In particular, GSS_Accept_sec_context is handled by an upcall
  18. * Data exchange is handled entirely within the kernel
  19. * In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
  20. * Context destruction is handled in-kernel
  21. * GSS_Delete_sec_context is in-kernel
  22. *
  23. * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
  24. * The context handle and gss_token are used as a key into the rpcsec_init cache.
  25. * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
  26. * being major_status, minor_status, context_handle, reply_token.
  27. * These are sent back to the client.
  28. * Sequence window management is handled by the kernel. The window size if currently
  29. * a compile time constant.
  30. *
  31. * When user-space is happy that a context is established, it places an entry
  32. * in the rpcsec_context cache. The key for this cache is the context_handle.
  33. * The content includes:
  34. * uid/gidlist - for determining access rights
  35. * mechanism type
  36. * mechanism specific information, such as a key
  37. *
  38. */
  39. #include <linux/types.h>
  40. #include <linux/module.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/sunrpc/auth_gss.h>
  43. #include <linux/sunrpc/svcauth.h>
  44. #include <linux/sunrpc/gss_err.h>
  45. #include <linux/sunrpc/svcauth.h>
  46. #include <linux/sunrpc/svcauth_gss.h>
  47. #include <linux/sunrpc/cache.h>
  48. #ifdef RPC_DEBUG
  49. # define RPCDBG_FACILITY RPCDBG_AUTH
  50. #endif
  51. /* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
  52. * into replies.
  53. *
  54. * Key is context handle (\x if empty) and gss_token.
  55. * Content is major_status minor_status (integers) context_handle, reply_token.
  56. *
  57. */
  58. static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
  59. {
  60. return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
  61. }
  62. #define RSI_HASHBITS 6
  63. #define RSI_HASHMAX (1<<RSI_HASHBITS)
  64. #define RSI_HASHMASK (RSI_HASHMAX-1)
  65. struct rsi {
  66. struct cache_head h;
  67. struct xdr_netobj in_handle, in_token;
  68. struct xdr_netobj out_handle, out_token;
  69. int major_status, minor_status;
  70. };
  71. static struct cache_head *rsi_table[RSI_HASHMAX];
  72. static struct cache_detail rsi_cache;
  73. static struct rsi *rsi_lookup(struct rsi *item, int set);
  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 cache_head *item, struct cache_detail *cd)
  82. {
  83. struct rsi *rsii = container_of(item, struct rsi, h);
  84. if (cache_put(item, cd)) {
  85. rsi_free(rsii);
  86. kfree(rsii);
  87. }
  88. }
  89. static inline int rsi_hash(struct rsi *item)
  90. {
  91. return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
  92. ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
  93. }
  94. static inline int rsi_match(struct rsi *item, struct rsi *tmp)
  95. {
  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 ? kmalloc(len, GFP_KERNEL) : NULL);
  103. if (dst->data)
  104. memcpy(dst->data, src, len);
  105. if (len && !dst->data)
  106. return -ENOMEM;
  107. return 0;
  108. }
  109. static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
  110. {
  111. return dup_to_netobj(dst, src->data, src->len);
  112. }
  113. static inline void rsi_init(struct rsi *new, struct rsi *item)
  114. {
  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 inline void rsi_update(struct rsi *new, struct rsi *item)
  129. {
  130. BUG_ON(new->out_handle.data || new->out_token.data);
  131. new->out_handle.len = item->out_handle.len;
  132. item->out_handle.len = 0;
  133. new->out_token.len = item->out_token.len;
  134. item->out_token.len = 0;
  135. new->out_handle.data = item->out_handle.data;
  136. item->out_handle.data = NULL;
  137. new->out_token.data = item->out_token.data;
  138. item->out_token.data = NULL;
  139. new->major_status = item->major_status;
  140. new->minor_status = item->minor_status;
  141. }
  142. static void rsi_request(struct cache_detail *cd,
  143. struct cache_head *h,
  144. char **bpp, int *blen)
  145. {
  146. struct rsi *rsii = container_of(h, struct rsi, h);
  147. qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
  148. qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
  149. (*bpp)[-1] = '\n';
  150. }
  151. static int rsi_parse(struct cache_detail *cd,
  152. char *mesg, int mlen)
  153. {
  154. /* context token expiry major minor context token */
  155. char *buf = mesg;
  156. char *ep;
  157. int len;
  158. struct rsi rsii, *rsip = NULL;
  159. time_t expiry;
  160. int status = -EINVAL;
  161. memset(&rsii, 0, sizeof(rsii));
  162. /* handle */
  163. len = qword_get(&mesg, buf, mlen);
  164. if (len < 0)
  165. goto out;
  166. status = -ENOMEM;
  167. if (dup_to_netobj(&rsii.in_handle, buf, len))
  168. goto out;
  169. /* token */
  170. len = qword_get(&mesg, buf, mlen);
  171. status = -EINVAL;
  172. if (len < 0)
  173. goto out;
  174. status = -ENOMEM;
  175. if (dup_to_netobj(&rsii.in_token, buf, len))
  176. goto out;
  177. rsii.h.flags = 0;
  178. /* expiry */
  179. expiry = get_expiry(&mesg);
  180. status = -EINVAL;
  181. if (expiry == 0)
  182. goto out;
  183. /* major/minor */
  184. len = qword_get(&mesg, buf, mlen);
  185. if (len < 0)
  186. goto out;
  187. if (len == 0) {
  188. goto out;
  189. } else {
  190. rsii.major_status = simple_strtoul(buf, &ep, 10);
  191. if (*ep)
  192. goto out;
  193. len = qword_get(&mesg, buf, mlen);
  194. if (len <= 0)
  195. goto out;
  196. rsii.minor_status = simple_strtoul(buf, &ep, 10);
  197. if (*ep)
  198. goto out;
  199. /* out_handle */
  200. len = qword_get(&mesg, buf, mlen);
  201. if (len < 0)
  202. goto out;
  203. status = -ENOMEM;
  204. if (dup_to_netobj(&rsii.out_handle, buf, len))
  205. goto out;
  206. /* out_token */
  207. len = qword_get(&mesg, buf, mlen);
  208. status = -EINVAL;
  209. if (len < 0)
  210. goto out;
  211. status = -ENOMEM;
  212. if (dup_to_netobj(&rsii.out_token, buf, len))
  213. goto out;
  214. }
  215. rsii.h.expiry_time = expiry;
  216. rsip = rsi_lookup(&rsii, 1);
  217. status = 0;
  218. out:
  219. rsi_free(&rsii);
  220. if (rsip)
  221. rsi_put(&rsip->h, &rsi_cache);
  222. return status;
  223. }
  224. static struct cache_detail rsi_cache = {
  225. .hash_size = RSI_HASHMAX,
  226. .hash_table = rsi_table,
  227. .name = "auth.rpcsec.init",
  228. .cache_put = rsi_put,
  229. .cache_request = rsi_request,
  230. .cache_parse = rsi_parse,
  231. };
  232. static DefineSimpleCacheLookup(rsi, 0)
  233. /*
  234. * The rpcsec_context cache is used to store a context that is
  235. * used in data exchange.
  236. * The key is a context handle. The content is:
  237. * uid, gidlist, mechanism, service-set, mech-specific-data
  238. */
  239. #define RSC_HASHBITS 10
  240. #define RSC_HASHMAX (1<<RSC_HASHBITS)
  241. #define RSC_HASHMASK (RSC_HASHMAX-1)
  242. #define GSS_SEQ_WIN 128
  243. struct gss_svc_seq_data {
  244. /* highest seq number seen so far: */
  245. int sd_max;
  246. /* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
  247. * sd_win is nonzero iff sequence number i has been seen already: */
  248. unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
  249. spinlock_t sd_lock;
  250. };
  251. struct rsc {
  252. struct cache_head h;
  253. struct xdr_netobj handle;
  254. struct svc_cred cred;
  255. struct gss_svc_seq_data seqdata;
  256. struct gss_ctx *mechctx;
  257. };
  258. static struct cache_head *rsc_table[RSC_HASHMAX];
  259. static struct cache_detail rsc_cache;
  260. static struct rsc *rsc_lookup(struct rsc *item, int set);
  261. static void rsc_free(struct rsc *rsci)
  262. {
  263. kfree(rsci->handle.data);
  264. if (rsci->mechctx)
  265. gss_delete_sec_context(&rsci->mechctx);
  266. if (rsci->cred.cr_group_info)
  267. put_group_info(rsci->cred.cr_group_info);
  268. }
  269. static void rsc_put(struct cache_head *item, struct cache_detail *cd)
  270. {
  271. struct rsc *rsci = container_of(item, struct rsc, h);
  272. if (cache_put(item, cd)) {
  273. rsc_free(rsci);
  274. kfree(rsci);
  275. }
  276. }
  277. static inline int
  278. rsc_hash(struct rsc *rsci)
  279. {
  280. return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
  281. }
  282. static inline int
  283. rsc_match(struct rsc *new, struct rsc *tmp)
  284. {
  285. return netobj_equal(&new->handle, &tmp->handle);
  286. }
  287. static inline void
  288. rsc_init(struct rsc *new, struct rsc *tmp)
  289. {
  290. new->handle.len = tmp->handle.len;
  291. tmp->handle.len = 0;
  292. new->handle.data = tmp->handle.data;
  293. tmp->handle.data = NULL;
  294. new->mechctx = NULL;
  295. new->cred.cr_group_info = NULL;
  296. }
  297. static inline void
  298. rsc_update(struct rsc *new, struct rsc *tmp)
  299. {
  300. new->mechctx = tmp->mechctx;
  301. tmp->mechctx = NULL;
  302. memset(&new->seqdata, 0, sizeof(new->seqdata));
  303. spin_lock_init(&new->seqdata.sd_lock);
  304. new->cred = tmp->cred;
  305. tmp->cred.cr_group_info = NULL;
  306. }
  307. static int rsc_parse(struct cache_detail *cd,
  308. char *mesg, int mlen)
  309. {
  310. /* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
  311. char *buf = mesg;
  312. int len, rv;
  313. struct rsc rsci, *rscp = NULL;
  314. time_t expiry;
  315. int status = -EINVAL;
  316. memset(&rsci, 0, sizeof(rsci));
  317. /* context handle */
  318. len = qword_get(&mesg, buf, mlen);
  319. if (len < 0) goto out;
  320. status = -ENOMEM;
  321. if (dup_to_netobj(&rsci.handle, buf, len))
  322. goto out;
  323. rsci.h.flags = 0;
  324. /* expiry */
  325. expiry = get_expiry(&mesg);
  326. status = -EINVAL;
  327. if (expiry == 0)
  328. goto out;
  329. /* uid, or NEGATIVE */
  330. rv = get_int(&mesg, &rsci.cred.cr_uid);
  331. if (rv == -EINVAL)
  332. goto out;
  333. if (rv == -ENOENT)
  334. set_bit(CACHE_NEGATIVE, &rsci.h.flags);
  335. else {
  336. int N, i;
  337. struct gss_api_mech *gm;
  338. /* gid */
  339. if (get_int(&mesg, &rsci.cred.cr_gid))
  340. goto out;
  341. /* number of additional gid's */
  342. if (get_int(&mesg, &N))
  343. goto out;
  344. status = -ENOMEM;
  345. rsci.cred.cr_group_info = groups_alloc(N);
  346. if (rsci.cred.cr_group_info == NULL)
  347. goto out;
  348. /* gid's */
  349. status = -EINVAL;
  350. for (i=0; i<N; i++) {
  351. gid_t gid;
  352. if (get_int(&mesg, &gid))
  353. goto out;
  354. GROUP_AT(rsci.cred.cr_group_info, i) = gid;
  355. }
  356. /* mech name */
  357. len = qword_get(&mesg, buf, mlen);
  358. if (len < 0)
  359. goto out;
  360. gm = gss_mech_get_by_name(buf);
  361. status = -EOPNOTSUPP;
  362. if (!gm)
  363. goto out;
  364. status = -EINVAL;
  365. /* mech-specific data: */
  366. len = qword_get(&mesg, buf, mlen);
  367. if (len < 0) {
  368. gss_mech_put(gm);
  369. goto out;
  370. }
  371. if (gss_import_sec_context(buf, len, gm, &rsci.mechctx)) {
  372. gss_mech_put(gm);
  373. goto out;
  374. }
  375. gss_mech_put(gm);
  376. }
  377. rsci.h.expiry_time = expiry;
  378. rscp = rsc_lookup(&rsci, 1);
  379. status = 0;
  380. out:
  381. rsc_free(&rsci);
  382. if (rscp)
  383. rsc_put(&rscp->h, &rsc_cache);
  384. return status;
  385. }
  386. static struct cache_detail rsc_cache = {
  387. .hash_size = RSC_HASHMAX,
  388. .hash_table = rsc_table,
  389. .name = "auth.rpcsec.context",
  390. .cache_put = rsc_put,
  391. .cache_parse = rsc_parse,
  392. };
  393. static DefineSimpleCacheLookup(rsc, 0);
  394. static struct rsc *
  395. gss_svc_searchbyctx(struct xdr_netobj *handle)
  396. {
  397. struct rsc rsci;
  398. struct rsc *found;
  399. memset(&rsci, 0, sizeof(rsci));
  400. if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
  401. return NULL;
  402. found = rsc_lookup(&rsci, 0);
  403. rsc_free(&rsci);
  404. if (!found)
  405. return NULL;
  406. if (cache_check(&rsc_cache, &found->h, NULL))
  407. return NULL;
  408. return found;
  409. }
  410. /* Implements sequence number algorithm as specified in RFC 2203. */
  411. static int
  412. gss_check_seq_num(struct rsc *rsci, int seq_num)
  413. {
  414. struct gss_svc_seq_data *sd = &rsci->seqdata;
  415. spin_lock(&sd->sd_lock);
  416. if (seq_num > sd->sd_max) {
  417. if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
  418. memset(sd->sd_win,0,sizeof(sd->sd_win));
  419. sd->sd_max = seq_num;
  420. } else while (sd->sd_max < seq_num) {
  421. sd->sd_max++;
  422. __clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
  423. }
  424. __set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
  425. goto ok;
  426. } else if (seq_num <= sd->sd_max - GSS_SEQ_WIN) {
  427. goto drop;
  428. }
  429. /* sd_max - GSS_SEQ_WIN < seq_num <= sd_max */
  430. if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
  431. goto drop;
  432. ok:
  433. spin_unlock(&sd->sd_lock);
  434. return 1;
  435. drop:
  436. spin_unlock(&sd->sd_lock);
  437. return 0;
  438. }
  439. static inline u32 round_up_to_quad(u32 i)
  440. {
  441. return (i + 3 ) & ~3;
  442. }
  443. static inline int
  444. svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
  445. {
  446. int l;
  447. if (argv->iov_len < 4)
  448. return -1;
  449. o->len = ntohl(svc_getu32(argv));
  450. l = round_up_to_quad(o->len);
  451. if (argv->iov_len < l)
  452. return -1;
  453. o->data = argv->iov_base;
  454. argv->iov_base += l;
  455. argv->iov_len -= l;
  456. return 0;
  457. }
  458. static inline int
  459. svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
  460. {
  461. u32 *p;
  462. if (resv->iov_len + 4 > PAGE_SIZE)
  463. return -1;
  464. svc_putu32(resv, htonl(o->len));
  465. p = resv->iov_base + resv->iov_len;
  466. resv->iov_len += round_up_to_quad(o->len);
  467. if (resv->iov_len > PAGE_SIZE)
  468. return -1;
  469. memcpy(p, o->data, o->len);
  470. memset((u8 *)p + o->len, 0, round_up_to_quad(o->len) - o->len);
  471. return 0;
  472. }
  473. /* Verify the checksum on the header and return SVC_OK on success.
  474. * Otherwise, return SVC_DROP (in the case of a bad sequence number)
  475. * or return SVC_DENIED and indicate error in authp.
  476. */
  477. static int
  478. gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
  479. u32 *rpcstart, struct rpc_gss_wire_cred *gc, u32 *authp)
  480. {
  481. struct gss_ctx *ctx_id = rsci->mechctx;
  482. struct xdr_buf rpchdr;
  483. struct xdr_netobj checksum;
  484. u32 flavor = 0;
  485. struct kvec *argv = &rqstp->rq_arg.head[0];
  486. struct kvec iov;
  487. /* data to compute the checksum over: */
  488. iov.iov_base = rpcstart;
  489. iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
  490. xdr_buf_from_iov(&iov, &rpchdr);
  491. *authp = rpc_autherr_badverf;
  492. if (argv->iov_len < 4)
  493. return SVC_DENIED;
  494. flavor = ntohl(svc_getu32(argv));
  495. if (flavor != RPC_AUTH_GSS)
  496. return SVC_DENIED;
  497. if (svc_safe_getnetobj(argv, &checksum))
  498. return SVC_DENIED;
  499. if (rqstp->rq_deferred) /* skip verification of revisited request */
  500. return SVC_OK;
  501. if (gss_verify_mic(ctx_id, &rpchdr, &checksum, NULL)
  502. != GSS_S_COMPLETE) {
  503. *authp = rpcsec_gsserr_credproblem;
  504. return SVC_DENIED;
  505. }
  506. if (gc->gc_seq > MAXSEQ) {
  507. dprintk("RPC: svcauth_gss: discarding request with large sequence number %d\n",
  508. gc->gc_seq);
  509. *authp = rpcsec_gsserr_ctxproblem;
  510. return SVC_DENIED;
  511. }
  512. if (!gss_check_seq_num(rsci, gc->gc_seq)) {
  513. dprintk("RPC: svcauth_gss: discarding request with old sequence number %d\n",
  514. gc->gc_seq);
  515. return SVC_DROP;
  516. }
  517. return SVC_OK;
  518. }
  519. static int
  520. gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
  521. {
  522. u32 xdr_seq;
  523. u32 maj_stat;
  524. struct xdr_buf verf_data;
  525. struct xdr_netobj mic;
  526. u32 *p;
  527. struct kvec iov;
  528. svc_putu32(rqstp->rq_res.head, htonl(RPC_AUTH_GSS));
  529. xdr_seq = htonl(seq);
  530. iov.iov_base = &xdr_seq;
  531. iov.iov_len = sizeof(xdr_seq);
  532. xdr_buf_from_iov(&iov, &verf_data);
  533. p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
  534. mic.data = (u8 *)(p + 1);
  535. maj_stat = gss_get_mic(ctx_id, 0, &verf_data, &mic);
  536. if (maj_stat != GSS_S_COMPLETE)
  537. return -1;
  538. *p++ = htonl(mic.len);
  539. memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
  540. p += XDR_QUADLEN(mic.len);
  541. if (!xdr_ressize_check(rqstp, p))
  542. return -1;
  543. return 0;
  544. }
  545. struct gss_domain {
  546. struct auth_domain h;
  547. u32 pseudoflavor;
  548. };
  549. static struct auth_domain *
  550. find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
  551. {
  552. char *name;
  553. name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
  554. if (!name)
  555. return NULL;
  556. return auth_domain_find(name);
  557. }
  558. int
  559. svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
  560. {
  561. struct gss_domain *new;
  562. struct auth_domain *test;
  563. int stat = -ENOMEM;
  564. new = kmalloc(sizeof(*new), GFP_KERNEL);
  565. if (!new)
  566. goto out;
  567. cache_init(&new->h.h);
  568. new->h.name = kmalloc(strlen(name) + 1, GFP_KERNEL);
  569. if (!new->h.name)
  570. goto out_free_dom;
  571. strcpy(new->h.name, name);
  572. new->h.flavour = RPC_AUTH_GSS;
  573. new->pseudoflavor = pseudoflavor;
  574. new->h.h.expiry_time = NEVER;
  575. test = auth_domain_lookup(&new->h, 1);
  576. if (test == &new->h) {
  577. BUG_ON(atomic_dec_and_test(&new->h.h.refcnt));
  578. } else { /* XXX Duplicate registration? */
  579. auth_domain_put(&new->h);
  580. goto out;
  581. }
  582. return 0;
  583. out_free_dom:
  584. kfree(new);
  585. out:
  586. return stat;
  587. }
  588. EXPORT_SYMBOL(svcauth_gss_register_pseudoflavor);
  589. static inline int
  590. read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
  591. {
  592. u32 raw;
  593. int status;
  594. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  595. if (status)
  596. return status;
  597. *obj = ntohl(raw);
  598. return 0;
  599. }
  600. /* It would be nice if this bit of code could be shared with the client.
  601. * Obstacles:
  602. * The client shouldn't malloc(), would have to pass in own memory.
  603. * The server uses base of head iovec as read pointer, while the
  604. * client uses separate pointer. */
  605. static int
  606. unwrap_integ_data(struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  607. {
  608. int stat = -EINVAL;
  609. u32 integ_len, maj_stat;
  610. struct xdr_netobj mic;
  611. struct xdr_buf integ_buf;
  612. integ_len = ntohl(svc_getu32(&buf->head[0]));
  613. if (integ_len & 3)
  614. goto out;
  615. if (integ_len > buf->len)
  616. goto out;
  617. if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
  618. BUG();
  619. /* copy out mic... */
  620. if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
  621. BUG();
  622. if (mic.len > RPC_MAX_AUTH_SIZE)
  623. goto out;
  624. mic.data = kmalloc(mic.len, GFP_KERNEL);
  625. if (!mic.data)
  626. goto out;
  627. if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
  628. goto out;
  629. maj_stat = gss_verify_mic(ctx, &integ_buf, &mic, NULL);
  630. if (maj_stat != GSS_S_COMPLETE)
  631. goto out;
  632. if (ntohl(svc_getu32(&buf->head[0])) != seq)
  633. goto out;
  634. stat = 0;
  635. out:
  636. return stat;
  637. }
  638. struct gss_svc_data {
  639. /* decoded gss client cred: */
  640. struct rpc_gss_wire_cred clcred;
  641. /* pointer to the beginning of the procedure-specific results,
  642. * which may be encrypted/checksummed in svcauth_gss_release: */
  643. u32 *body_start;
  644. struct rsc *rsci;
  645. };
  646. static int
  647. svcauth_gss_set_client(struct svc_rqst *rqstp)
  648. {
  649. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  650. struct rsc *rsci = svcdata->rsci;
  651. struct rpc_gss_wire_cred *gc = &svcdata->clcred;
  652. rqstp->rq_client = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
  653. if (rqstp->rq_client == NULL)
  654. return SVC_DENIED;
  655. return SVC_OK;
  656. }
  657. /*
  658. * Accept an rpcsec packet.
  659. * If context establishment, punt to user space
  660. * If data exchange, verify/decrypt
  661. * If context destruction, handle here
  662. * In the context establishment and destruction case we encode
  663. * response here and return SVC_COMPLETE.
  664. */
  665. static int
  666. svcauth_gss_accept(struct svc_rqst *rqstp, u32 *authp)
  667. {
  668. struct kvec *argv = &rqstp->rq_arg.head[0];
  669. struct kvec *resv = &rqstp->rq_res.head[0];
  670. u32 crlen;
  671. struct xdr_netobj tmpobj;
  672. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  673. struct rpc_gss_wire_cred *gc;
  674. struct rsc *rsci = NULL;
  675. struct rsi *rsip, rsikey;
  676. u32 *rpcstart;
  677. u32 *reject_stat = resv->iov_base + resv->iov_len;
  678. int ret;
  679. dprintk("RPC: svcauth_gss: argv->iov_len = %zd\n",argv->iov_len);
  680. *authp = rpc_autherr_badcred;
  681. if (!svcdata)
  682. svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
  683. if (!svcdata)
  684. goto auth_err;
  685. rqstp->rq_auth_data = svcdata;
  686. svcdata->body_start = NULL;
  687. svcdata->rsci = NULL;
  688. gc = &svcdata->clcred;
  689. /* start of rpc packet is 7 u32's back from here:
  690. * xid direction rpcversion prog vers proc flavour
  691. */
  692. rpcstart = argv->iov_base;
  693. rpcstart -= 7;
  694. /* credential is:
  695. * version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
  696. * at least 5 u32s, and is preceeded by length, so that makes 6.
  697. */
  698. if (argv->iov_len < 5 * 4)
  699. goto auth_err;
  700. crlen = ntohl(svc_getu32(argv));
  701. if (ntohl(svc_getu32(argv)) != RPC_GSS_VERSION)
  702. goto auth_err;
  703. gc->gc_proc = ntohl(svc_getu32(argv));
  704. gc->gc_seq = ntohl(svc_getu32(argv));
  705. gc->gc_svc = ntohl(svc_getu32(argv));
  706. if (svc_safe_getnetobj(argv, &gc->gc_ctx))
  707. goto auth_err;
  708. if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
  709. goto auth_err;
  710. if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
  711. goto auth_err;
  712. /*
  713. * We've successfully parsed the credential. Let's check out the
  714. * verifier. An AUTH_NULL verifier is allowed (and required) for
  715. * INIT and CONTINUE_INIT requests. AUTH_RPCSEC_GSS is required for
  716. * PROC_DATA and PROC_DESTROY.
  717. *
  718. * AUTH_NULL verifier is 0 (AUTH_NULL), 0 (length).
  719. * AUTH_RPCSEC_GSS verifier is:
  720. * 6 (AUTH_RPCSEC_GSS), length, checksum.
  721. * checksum is calculated over rpcheader from xid up to here.
  722. */
  723. *authp = rpc_autherr_badverf;
  724. switch (gc->gc_proc) {
  725. case RPC_GSS_PROC_INIT:
  726. case RPC_GSS_PROC_CONTINUE_INIT:
  727. if (argv->iov_len < 2 * 4)
  728. goto auth_err;
  729. if (ntohl(svc_getu32(argv)) != RPC_AUTH_NULL)
  730. goto auth_err;
  731. if (ntohl(svc_getu32(argv)) != 0)
  732. goto auth_err;
  733. break;
  734. case RPC_GSS_PROC_DATA:
  735. case RPC_GSS_PROC_DESTROY:
  736. *authp = rpcsec_gsserr_credproblem;
  737. rsci = gss_svc_searchbyctx(&gc->gc_ctx);
  738. if (!rsci)
  739. goto auth_err;
  740. switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
  741. case SVC_OK:
  742. break;
  743. case SVC_DENIED:
  744. goto auth_err;
  745. case SVC_DROP:
  746. goto drop;
  747. }
  748. break;
  749. default:
  750. *authp = rpc_autherr_rejectedcred;
  751. goto auth_err;
  752. }
  753. /* now act upon the command: */
  754. switch (gc->gc_proc) {
  755. case RPC_GSS_PROC_INIT:
  756. case RPC_GSS_PROC_CONTINUE_INIT:
  757. *authp = rpc_autherr_badcred;
  758. if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
  759. goto auth_err;
  760. memset(&rsikey, 0, sizeof(rsikey));
  761. if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))
  762. goto drop;
  763. *authp = rpc_autherr_badverf;
  764. if (svc_safe_getnetobj(argv, &tmpobj)) {
  765. kfree(rsikey.in_handle.data);
  766. goto auth_err;
  767. }
  768. if (dup_netobj(&rsikey.in_token, &tmpobj)) {
  769. kfree(rsikey.in_handle.data);
  770. goto drop;
  771. }
  772. rsip = rsi_lookup(&rsikey, 0);
  773. rsi_free(&rsikey);
  774. if (!rsip) {
  775. goto drop;
  776. }
  777. switch(cache_check(&rsi_cache, &rsip->h, &rqstp->rq_chandle)) {
  778. case -EAGAIN:
  779. goto drop;
  780. case -ENOENT:
  781. goto drop;
  782. case 0:
  783. rsci = gss_svc_searchbyctx(&rsip->out_handle);
  784. if (!rsci) {
  785. goto drop;
  786. }
  787. if (gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN))
  788. goto drop;
  789. if (resv->iov_len + 4 > PAGE_SIZE)
  790. goto drop;
  791. svc_putu32(resv, rpc_success);
  792. if (svc_safe_putnetobj(resv, &rsip->out_handle))
  793. goto drop;
  794. if (resv->iov_len + 3 * 4 > PAGE_SIZE)
  795. goto drop;
  796. svc_putu32(resv, htonl(rsip->major_status));
  797. svc_putu32(resv, htonl(rsip->minor_status));
  798. svc_putu32(resv, htonl(GSS_SEQ_WIN));
  799. if (svc_safe_putnetobj(resv, &rsip->out_token))
  800. goto drop;
  801. rqstp->rq_client = NULL;
  802. }
  803. goto complete;
  804. case RPC_GSS_PROC_DESTROY:
  805. set_bit(CACHE_NEGATIVE, &rsci->h.flags);
  806. if (resv->iov_len + 4 > PAGE_SIZE)
  807. goto drop;
  808. svc_putu32(resv, rpc_success);
  809. goto complete;
  810. case RPC_GSS_PROC_DATA:
  811. *authp = rpcsec_gsserr_ctxproblem;
  812. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  813. goto auth_err;
  814. rqstp->rq_cred = rsci->cred;
  815. get_group_info(rsci->cred.cr_group_info);
  816. *authp = rpc_autherr_badcred;
  817. switch (gc->gc_svc) {
  818. case RPC_GSS_SVC_NONE:
  819. break;
  820. case RPC_GSS_SVC_INTEGRITY:
  821. if (unwrap_integ_data(&rqstp->rq_arg,
  822. gc->gc_seq, rsci->mechctx))
  823. goto auth_err;
  824. /* placeholders for length and seq. number: */
  825. svcdata->body_start = resv->iov_base + resv->iov_len;
  826. svc_putu32(resv, 0);
  827. svc_putu32(resv, 0);
  828. break;
  829. case RPC_GSS_SVC_PRIVACY:
  830. /* currently unsupported */
  831. default:
  832. goto auth_err;
  833. }
  834. svcdata->rsci = rsci;
  835. cache_get(&rsci->h);
  836. ret = SVC_OK;
  837. goto out;
  838. }
  839. auth_err:
  840. /* Restore write pointer to original value: */
  841. xdr_ressize_check(rqstp, reject_stat);
  842. ret = SVC_DENIED;
  843. goto out;
  844. complete:
  845. ret = SVC_COMPLETE;
  846. goto out;
  847. drop:
  848. ret = SVC_DROP;
  849. out:
  850. if (rsci)
  851. rsc_put(&rsci->h, &rsc_cache);
  852. return ret;
  853. }
  854. static int
  855. svcauth_gss_release(struct svc_rqst *rqstp)
  856. {
  857. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  858. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  859. struct xdr_buf *resbuf = &rqstp->rq_res;
  860. struct xdr_buf integ_buf;
  861. struct xdr_netobj mic;
  862. struct kvec *resv;
  863. u32 *p;
  864. int integ_offset, integ_len;
  865. int stat = -EINVAL;
  866. if (gc->gc_proc != RPC_GSS_PROC_DATA)
  867. goto out;
  868. /* Release can be called twice, but we only wrap once. */
  869. if (gsd->body_start == NULL)
  870. goto out;
  871. /* normally not set till svc_send, but we need it here: */
  872. resbuf->len = resbuf->head[0].iov_len
  873. + resbuf->page_len + resbuf->tail[0].iov_len;
  874. switch (gc->gc_svc) {
  875. case RPC_GSS_SVC_NONE:
  876. break;
  877. case RPC_GSS_SVC_INTEGRITY:
  878. p = gsd->body_start;
  879. gsd->body_start = NULL;
  880. /* move accept_stat to right place: */
  881. memcpy(p, p + 2, 4);
  882. /* don't wrap in failure case: */
  883. /* Note: counting on not getting here if call was not even
  884. * accepted! */
  885. if (*p != rpc_success) {
  886. resbuf->head[0].iov_len -= 2 * 4;
  887. goto out;
  888. }
  889. p++;
  890. integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
  891. integ_len = resbuf->len - integ_offset;
  892. BUG_ON(integ_len % 4);
  893. *p++ = htonl(integ_len);
  894. *p++ = htonl(gc->gc_seq);
  895. if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset,
  896. integ_len))
  897. BUG();
  898. if (resbuf->page_len == 0
  899. && resbuf->tail[0].iov_len + RPC_MAX_AUTH_SIZE
  900. < PAGE_SIZE) {
  901. BUG_ON(resbuf->tail[0].iov_len);
  902. /* Use head for everything */
  903. resv = &resbuf->head[0];
  904. } else if (resbuf->tail[0].iov_base == NULL) {
  905. /* copied from nfsd4_encode_read */
  906. svc_take_page(rqstp);
  907. resbuf->tail[0].iov_base = page_address(rqstp
  908. ->rq_respages[rqstp->rq_resused-1]);
  909. rqstp->rq_restailpage = rqstp->rq_resused-1;
  910. resbuf->tail[0].iov_len = 0;
  911. resv = &resbuf->tail[0];
  912. } else {
  913. resv = &resbuf->tail[0];
  914. }
  915. mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
  916. if (gss_get_mic(gsd->rsci->mechctx, 0, &integ_buf, &mic))
  917. goto out_err;
  918. svc_putu32(resv, htonl(mic.len));
  919. memset(mic.data + mic.len, 0,
  920. round_up_to_quad(mic.len) - mic.len);
  921. resv->iov_len += XDR_QUADLEN(mic.len) << 2;
  922. /* not strictly required: */
  923. resbuf->len += XDR_QUADLEN(mic.len) << 2;
  924. BUG_ON(resv->iov_len > PAGE_SIZE);
  925. break;
  926. case RPC_GSS_SVC_PRIVACY:
  927. default:
  928. goto out_err;
  929. }
  930. out:
  931. stat = 0;
  932. out_err:
  933. if (rqstp->rq_client)
  934. auth_domain_put(rqstp->rq_client);
  935. rqstp->rq_client = NULL;
  936. if (rqstp->rq_cred.cr_group_info)
  937. put_group_info(rqstp->rq_cred.cr_group_info);
  938. rqstp->rq_cred.cr_group_info = NULL;
  939. if (gsd->rsci)
  940. rsc_put(&gsd->rsci->h, &rsc_cache);
  941. gsd->rsci = NULL;
  942. return stat;
  943. }
  944. static void
  945. svcauth_gss_domain_release(struct auth_domain *dom)
  946. {
  947. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  948. kfree(dom->name);
  949. kfree(gd);
  950. }
  951. static struct auth_ops svcauthops_gss = {
  952. .name = "rpcsec_gss",
  953. .owner = THIS_MODULE,
  954. .flavour = RPC_AUTH_GSS,
  955. .accept = svcauth_gss_accept,
  956. .release = svcauth_gss_release,
  957. .domain_release = svcauth_gss_domain_release,
  958. .set_client = svcauth_gss_set_client,
  959. };
  960. int
  961. gss_svc_init(void)
  962. {
  963. int rv = svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
  964. if (rv == 0) {
  965. cache_register(&rsc_cache);
  966. cache_register(&rsi_cache);
  967. }
  968. return rv;
  969. }
  970. void
  971. gss_svc_shutdown(void)
  972. {
  973. cache_unregister(&rsc_cache);
  974. cache_unregister(&rsi_cache);
  975. svc_auth_unregister(RPC_AUTH_GSS);
  976. }