svcauth_gss.c 27 KB

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