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