svcauth_gss.c 48.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * Neil Brown <neilb@cse.unsw.edu.au>
 * J. Bruce Fields <bfields@umich.edu>
 * Andy Adamson <andros@umich.edu>
 * Dug Song <dugsong@monkey.org>
 *
 * RPCSEC_GSS server authentication.
 * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
 * (gssapi)
 *
 * The RPCSEC_GSS involves three stages:
 *  1/ context creation
 *  2/ data exchange
 *  3/ context destruction
 *
 * Context creation is handled largely by upcalls to user-space.
 *  In particular, GSS_Accept_sec_context is handled by an upcall
 * Data exchange is handled entirely within the kernel
 *  In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
 * Context destruction is handled in-kernel
 *  GSS_Delete_sec_context is in-kernel
 *
 * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
 * The context handle and gss_token are used as a key into the rpcsec_init cache.
 * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
 * being major_status, minor_status, context_handle, reply_token.
 * These are sent back to the client.
 * Sequence window management is handled by the kernel.  The window size if currently
 * a compile time constant.
 *
 * When user-space is happy that a context is established, it places an entry
 * in the rpcsec_context cache. The key for this cache is the context_handle.
 * The content includes:
 *   uid/gidlist - for determining access rights
 *   mechanism type
 *   mechanism specific information, such as a key
 *
 */

41
#include <linux/slab.h>
L
Linus Torvalds 已提交
42 43 44
#include <linux/types.h>
#include <linux/module.h>
#include <linux/pagemap.h>
45
#include <linux/user_namespace.h>
L
Linus Torvalds 已提交
46 47 48 49 50 51

#include <linux/sunrpc/auth_gss.h>
#include <linux/sunrpc/gss_err.h>
#include <linux/sunrpc/svcauth.h>
#include <linux/sunrpc/svcauth_gss.h>
#include <linux/sunrpc/cache.h>
52 53 54

#include <trace/events/rpcgss.h>

55
#include "gss_rpc_upcall.h"
L
Linus Torvalds 已提交
56

57

L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
 * into replies.
 *
 * Key is context handle (\x if empty) and gss_token.
 * Content is major_status minor_status (integers) context_handle, reply_token.
 *
 */

static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
{
	return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
}

#define	RSI_HASHBITS	6
#define	RSI_HASHMAX	(1<<RSI_HASHBITS)

struct rsi {
	struct cache_head	h;
	struct xdr_netobj	in_handle, in_token;
	struct xdr_netobj	out_handle, out_token;
	int			major_status, minor_status;
79
	struct rcu_head		rcu_head;
L
Linus Torvalds 已提交
80 81
};

82 83
static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91 92

static void rsi_free(struct rsi *rsii)
{
	kfree(rsii->in_handle.data);
	kfree(rsii->in_token.data);
	kfree(rsii->out_handle.data);
	kfree(rsii->out_token.data);
}

93
static void rsi_free_rcu(struct rcu_head *head)
L
Linus Torvalds 已提交
94
{
95 96
	struct rsi *rsii = container_of(head, struct rsi, rcu_head);

97 98
	rsi_free(rsii);
	kfree(rsii);
L
Linus Torvalds 已提交
99 100
}

101 102 103 104 105 106 107
static void rsi_put(struct kref *ref)
{
	struct rsi *rsii = container_of(ref, struct rsi, h.ref);

	call_rcu(&rsii->rcu_head, rsi_free_rcu);
}

L
Linus Torvalds 已提交
108 109 110 111 112 113
static inline int rsi_hash(struct rsi *item)
{
	return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
	     ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
}

114
static int rsi_match(struct cache_head *a, struct cache_head *b)
L
Linus Torvalds 已提交
115
{
116 117
	struct rsi *item = container_of(a, struct rsi, h);
	struct rsi *tmp = container_of(b, struct rsi, h);
118 119
	return netobj_equal(&item->in_handle, &tmp->in_handle) &&
	       netobj_equal(&item->in_token, &tmp->in_token);
L
Linus Torvalds 已提交
120 121 122 123 124
}

static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
{
	dst->len = len;
125
	dst->data = (len ? kmemdup(src, len, GFP_KERNEL) : NULL);
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135
	if (len && !dst->data)
		return -ENOMEM;
	return 0;
}

static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
{
	return dup_to_netobj(dst, src->data, src->len);
}

136
static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
L
Linus Torvalds 已提交
137
{
138 139 140
	struct rsi *new = container_of(cnew, struct rsi, h);
	struct rsi *item = container_of(citem, struct rsi, h);

L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154
	new->out_handle.data = NULL;
	new->out_handle.len = 0;
	new->out_token.data = NULL;
	new->out_token.len = 0;
	new->in_handle.len = item->in_handle.len;
	item->in_handle.len = 0;
	new->in_token.len = item->in_token.len;
	item->in_token.len = 0;
	new->in_handle.data = item->in_handle.data;
	item->in_handle.data = NULL;
	new->in_token.data = item->in_token.data;
	item->in_token.data = NULL;
}

155
static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
L
Linus Torvalds 已提交
156
{
157 158 159
	struct rsi *new = container_of(cnew, struct rsi, h);
	struct rsi *item = container_of(citem, struct rsi, h);

L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173
	BUG_ON(new->out_handle.data || new->out_token.data);
	new->out_handle.len = item->out_handle.len;
	item->out_handle.len = 0;
	new->out_token.len = item->out_token.len;
	item->out_token.len = 0;
	new->out_handle.data = item->out_handle.data;
	item->out_handle.data = NULL;
	new->out_token.data = item->out_token.data;
	item->out_token.data = NULL;

	new->major_status = item->major_status;
	new->minor_status = item->minor_status;
}

174 175 176 177 178 179 180 181 182
static struct cache_head *rsi_alloc(void)
{
	struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);
	if (rsii)
		return &rsii->h;
	else
		return NULL;
}

183 184 185 186 187
static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
{
	return sunrpc_cache_pipe_upcall_timeout(cd, h);
}

L
Linus Torvalds 已提交
188
static void rsi_request(struct cache_detail *cd,
189 190
		       struct cache_head *h,
		       char **bpp, int *blen)
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199
{
	struct rsi *rsii = container_of(h, struct rsi, h);

	qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
	qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
	(*bpp)[-1] = '\n';
}

static int rsi_parse(struct cache_detail *cd,
200
		    char *mesg, int mlen)
L
Linus Torvalds 已提交
201 202 203 204 205 206
{
	/* context token expiry major minor context token */
	char *buf = mesg;
	char *ep;
	int len;
	struct rsi rsii, *rsip = NULL;
A
Arnd Bergmann 已提交
207
	time64_t expiry;
L
Linus Torvalds 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	int status = -EINVAL;

	memset(&rsii, 0, sizeof(rsii));
	/* handle */
	len = qword_get(&mesg, buf, mlen);
	if (len < 0)
		goto out;
	status = -ENOMEM;
	if (dup_to_netobj(&rsii.in_handle, buf, len))
		goto out;

	/* token */
	len = qword_get(&mesg, buf, mlen);
	status = -EINVAL;
	if (len < 0)
		goto out;
	status = -ENOMEM;
	if (dup_to_netobj(&rsii.in_token, buf, len))
		goto out;

228
	rsip = rsi_lookup(cd, &rsii);
229 230 231
	if (!rsip)
		goto out;

L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240
	rsii.h.flags = 0;
	/* expiry */
	expiry = get_expiry(&mesg);
	status = -EINVAL;
	if (expiry == 0)
		goto out;

	/* major/minor */
	len = qword_get(&mesg, buf, mlen);
241 242 243 244 245 246 247
	if (len <= 0)
		goto out;
	rsii.major_status = simple_strtoul(buf, &ep, 10);
	if (*ep)
		goto out;
	len = qword_get(&mesg, buf, mlen);
	if (len <= 0)
L
Linus Torvalds 已提交
248
		goto out;
249 250
	rsii.minor_status = simple_strtoul(buf, &ep, 10);
	if (*ep)
L
Linus Torvalds 已提交
251 252
		goto out;

253 254 255 256 257 258 259
	/* out_handle */
	len = qword_get(&mesg, buf, mlen);
	if (len < 0)
		goto out;
	status = -ENOMEM;
	if (dup_to_netobj(&rsii.out_handle, buf, len))
		goto out;
L
Linus Torvalds 已提交
260

261 262 263 264 265 266 267 268
	/* out_token */
	len = qword_get(&mesg, buf, mlen);
	status = -EINVAL;
	if (len < 0)
		goto out;
	status = -ENOMEM;
	if (dup_to_netobj(&rsii.out_token, buf, len))
		goto out;
L
Linus Torvalds 已提交
269
	rsii.h.expiry_time = expiry;
270
	rsip = rsi_update(cd, &rsii, rsip);
L
Linus Torvalds 已提交
271 272 273 274
	status = 0;
out:
	rsi_free(&rsii);
	if (rsip)
275
		cache_put(&rsip->h, cd);
276 277
	else
		status = -ENOMEM;
L
Linus Torvalds 已提交
278 279 280
	return status;
}

281
static const struct cache_detail rsi_cache_template = {
282
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
283 284 285
	.hash_size	= RSI_HASHMAX,
	.name           = "auth.rpcsec.init",
	.cache_put      = rsi_put,
286
	.cache_upcall	= rsi_upcall,
287
	.cache_request  = rsi_request,
L
Linus Torvalds 已提交
288
	.cache_parse    = rsi_parse,
289 290 291 292
	.match		= rsi_match,
	.init		= rsi_init,
	.update		= update_rsi,
	.alloc		= rsi_alloc,
L
Linus Torvalds 已提交
293 294
};

295
static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
296 297 298 299
{
	struct cache_head *ch;
	int hash = rsi_hash(item);

300
	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
301 302 303 304 305 306
	if (ch)
		return container_of(ch, struct rsi, h);
	else
		return NULL;
}

307
static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old)
308 309 310 311
{
	struct cache_head *ch;
	int hash = rsi_hash(new);

312
	ch = sunrpc_cache_update(cd, &new->h,
313 314 315 316 317 318 319
				 &old->h, hash);
	if (ch)
		return container_of(ch, struct rsi, h);
	else
		return NULL;
}

L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

/*
 * The rpcsec_context cache is used to store a context that is
 * used in data exchange.
 * The key is a context handle. The content is:
 *  uid, gidlist, mechanism, service-set, mech-specific-data
 */

#define	RSC_HASHBITS	10
#define	RSC_HASHMAX	(1<<RSC_HASHBITS)

#define GSS_SEQ_WIN	128

struct gss_svc_seq_data {
	/* highest seq number seen so far: */
335
	u32			sd_max;
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344 345 346 347
	/* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
	 * sd_win is nonzero iff sequence number i has been seen already: */
	unsigned long		sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
	spinlock_t		sd_lock;
};

struct rsc {
	struct cache_head	h;
	struct xdr_netobj	handle;
	struct svc_cred		cred;
	struct gss_svc_seq_data	seqdata;
	struct gss_ctx		*mechctx;
348
	struct rcu_head		rcu_head;
L
Linus Torvalds 已提交
349 350
};

351 352
static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old);
static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item);
L
Linus Torvalds 已提交
353 354 355 356 357 358

static void rsc_free(struct rsc *rsci)
{
	kfree(rsci->handle.data);
	if (rsci->mechctx)
		gss_delete_sec_context(&rsci->mechctx);
359
	free_svc_cred(&rsci->cred);
L
Linus Torvalds 已提交
360 361
}

362 363 364 365 366 367 368 369
static void rsc_free_rcu(struct rcu_head *head)
{
	struct rsc *rsci = container_of(head, struct rsc, rcu_head);

	kfree(rsci->handle.data);
	kfree(rsci);
}

370
static void rsc_put(struct kref *ref)
L
Linus Torvalds 已提交
371
{
372
	struct rsc *rsci = container_of(ref, struct rsc, h.ref);
L
Linus Torvalds 已提交
373

374 375 376 377
	if (rsci->mechctx)
		gss_delete_sec_context(&rsci->mechctx);
	free_svc_cred(&rsci->cred);
	call_rcu(&rsci->rcu_head, rsc_free_rcu);
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385
}

static inline int
rsc_hash(struct rsc *rsci)
{
	return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
}

386 387
static int
rsc_match(struct cache_head *a, struct cache_head *b)
L
Linus Torvalds 已提交
388
{
389 390 391
	struct rsc *new = container_of(a, struct rsc, h);
	struct rsc *tmp = container_of(b, struct rsc, h);

L
Linus Torvalds 已提交
392 393 394
	return netobj_equal(&new->handle, &tmp->handle);
}

395 396
static void
rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
L
Linus Torvalds 已提交
397
{
398 399 400
	struct rsc *new = container_of(cnew, struct rsc, h);
	struct rsc *tmp = container_of(ctmp, struct rsc, h);

L
Linus Torvalds 已提交
401 402 403 404 405
	new->handle.len = tmp->handle.len;
	tmp->handle.len = 0;
	new->handle.data = tmp->handle.data;
	tmp->handle.data = NULL;
	new->mechctx = NULL;
406
	init_svc_cred(&new->cred);
L
Linus Torvalds 已提交
407 408
}

409 410
static void
update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
L
Linus Torvalds 已提交
411
{
412 413 414
	struct rsc *new = container_of(cnew, struct rsc, h);
	struct rsc *tmp = container_of(ctmp, struct rsc, h);

L
Linus Torvalds 已提交
415 416 417 418 419
	new->mechctx = tmp->mechctx;
	tmp->mechctx = NULL;
	memset(&new->seqdata, 0, sizeof(new->seqdata));
	spin_lock_init(&new->seqdata.sd_lock);
	new->cred = tmp->cred;
420
	init_svc_cred(&tmp->cred);
L
Linus Torvalds 已提交
421 422
}

423 424 425 426 427 428 429 430 431 432
static struct cache_head *
rsc_alloc(void)
{
	struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
	if (rsci)
		return &rsci->h;
	else
		return NULL;
}

433 434 435 436 437
static int rsc_upcall(struct cache_detail *cd, struct cache_head *h)
{
	return -EINVAL;
}

L
Linus Torvalds 已提交
438 439 440 441 442
static int rsc_parse(struct cache_detail *cd,
		     char *mesg, int mlen)
{
	/* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
	char *buf = mesg;
443
	int id;
L
Linus Torvalds 已提交
444 445
	int len, rv;
	struct rsc rsci, *rscp = NULL;
446
	time64_t expiry;
L
Linus Torvalds 已提交
447
	int status = -EINVAL;
448
	struct gss_api_mech *gm = NULL;
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

	memset(&rsci, 0, sizeof(rsci));
	/* context handle */
	len = qword_get(&mesg, buf, mlen);
	if (len < 0) goto out;
	status = -ENOMEM;
	if (dup_to_netobj(&rsci.handle, buf, len))
		goto out;

	rsci.h.flags = 0;
	/* expiry */
	expiry = get_expiry(&mesg);
	status = -EINVAL;
	if (expiry == 0)
		goto out;

465
	rscp = rsc_lookup(cd, &rsci);
466 467 468
	if (!rscp)
		goto out;

L
Linus Torvalds 已提交
469
	/* uid, or NEGATIVE */
470
	rv = get_int(&mesg, &id);
L
Linus Torvalds 已提交
471 472 473 474 475 476 477
	if (rv == -EINVAL)
		goto out;
	if (rv == -ENOENT)
		set_bit(CACHE_NEGATIVE, &rsci.h.flags);
	else {
		int N, i;

478 479 480 481 482 483 484 485
		/*
		 * NOTE: we skip uid_valid()/gid_valid() checks here:
		 * instead, * -1 id's are later mapped to the
		 * (export-specific) anonymous id by nfsd_setuser.
		 *
		 * (But supplementary gid's get no such special
		 * treatment so are checked for validity here.)
		 */
486
		/* uid */
487
		rsci.cred.cr_uid = make_kuid(current_user_ns(), id);
488

L
Linus Torvalds 已提交
489
		/* gid */
490 491
		if (get_int(&mesg, &id))
			goto out;
492
		rsci.cred.cr_gid = make_kgid(current_user_ns(), id);
L
Linus Torvalds 已提交
493 494 495 496

		/* number of additional gid's */
		if (get_int(&mesg, &N))
			goto out;
497 498
		if (N < 0 || N > NGROUPS_MAX)
			goto out;
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506
		status = -ENOMEM;
		rsci.cred.cr_group_info = groups_alloc(N);
		if (rsci.cred.cr_group_info == NULL)
			goto out;

		/* gid's */
		status = -EINVAL;
		for (i=0; i<N; i++) {
507
			kgid_t kgid;
508
			if (get_int(&mesg, &id))
L
Linus Torvalds 已提交
509
				goto out;
510
			kgid = make_kgid(current_user_ns(), id);
511 512
			if (!gid_valid(kgid))
				goto out;
513
			rsci.cred.cr_group_info->gid[i] = kgid;
L
Linus Torvalds 已提交
514
		}
515
		groups_sort(rsci.cred.cr_group_info);
L
Linus Torvalds 已提交
516 517 518 519 520

		/* mech name */
		len = qword_get(&mesg, buf, mlen);
		if (len < 0)
			goto out;
521
		gm = rsci.cred.cr_gss_mech = gss_mech_get_by_name(buf);
L
Linus Torvalds 已提交
522 523 524 525 526 527 528
		status = -EOPNOTSUPP;
		if (!gm)
			goto out;

		status = -EINVAL;
		/* mech-specific data: */
		len = qword_get(&mesg, buf, mlen);
529
		if (len < 0)
L
Linus Torvalds 已提交
530
			goto out;
531 532
		status = gss_import_sec_context(buf, len, gm, &rsci.mechctx,
						NULL, GFP_KERNEL);
533
		if (status)
L
Linus Torvalds 已提交
534
			goto out;
535 536 537 538

		/* get client name */
		len = qword_get(&mesg, buf, mlen);
		if (len > 0) {
539
			rsci.cred.cr_principal = kstrdup(buf, GFP_KERNEL);
540 541
			if (!rsci.cred.cr_principal) {
				status = -ENOMEM;
542
				goto out;
543
			}
544 545
		}

L
Linus Torvalds 已提交
546 547
	}
	rsci.h.expiry_time = expiry;
548
	rscp = rsc_update(cd, &rsci, rscp);
L
Linus Torvalds 已提交
549 550 551 552
	status = 0;
out:
	rsc_free(&rsci);
	if (rscp)
553
		cache_put(&rscp->h, cd);
554 555
	else
		status = -ENOMEM;
L
Linus Torvalds 已提交
556 557 558
	return status;
}

559
static const struct cache_detail rsc_cache_template = {
560
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
561 562 563
	.hash_size	= RSC_HASHMAX,
	.name		= "auth.rpcsec.context",
	.cache_put	= rsc_put,
564
	.cache_upcall	= rsc_upcall,
L
Linus Torvalds 已提交
565
	.cache_parse	= rsc_parse,
566 567 568 569
	.match		= rsc_match,
	.init		= rsc_init,
	.update		= update_rsc,
	.alloc		= rsc_alloc,
L
Linus Torvalds 已提交
570 571
};

572
static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item)
573 574 575 576
{
	struct cache_head *ch;
	int hash = rsc_hash(item);

577
	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
578 579 580 581 582 583
	if (ch)
		return container_of(ch, struct rsc, h);
	else
		return NULL;
}

584
static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old)
585 586 587 588
{
	struct cache_head *ch;
	int hash = rsc_hash(new);

589
	ch = sunrpc_cache_update(cd, &new->h,
590 591 592 593 594 595 596
				 &old->h, hash);
	if (ch)
		return container_of(ch, struct rsc, h);
	else
		return NULL;
}

L
Linus Torvalds 已提交
597 598

static struct rsc *
599
gss_svc_searchbyctx(struct cache_detail *cd, struct xdr_netobj *handle)
L
Linus Torvalds 已提交
600 601 602 603 604
{
	struct rsc rsci;
	struct rsc *found;

	memset(&rsci, 0, sizeof(rsci));
605 606
	if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
		return NULL;
607
	found = rsc_lookup(cd, &rsci);
608
	rsc_free(&rsci);
L
Linus Torvalds 已提交
609 610
	if (!found)
		return NULL;
611
	if (cache_check(cd, &found->h, NULL))
L
Linus Torvalds 已提交
612 613 614 615
		return NULL;
	return found;
}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
/**
 * gss_check_seq_num - GSS sequence number window check
 * @rqstp: RPC Call to use when reporting errors
 * @rsci: cached GSS context state (updated on return)
 * @seq_num: sequence number to check
 *
 * Implements sequence number algorithm as specified in
 * RFC 2203, Section 5.3.3.1. "Context Management".
 *
 * Return values:
 *   %true: @rqstp's GSS sequence number is inside the window
 *   %false: @rqstp's GSS sequence number is outside the window
 */
static bool gss_check_seq_num(const struct svc_rqst *rqstp, struct rsc *rsci,
			      u32 seq_num)
L
Linus Torvalds 已提交
631 632
{
	struct gss_svc_seq_data *sd = &rsci->seqdata;
633
	bool result = false;
L
Linus Torvalds 已提交
634 635 636 637

	spin_lock(&sd->sd_lock);
	if (seq_num > sd->sd_max) {
		if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
638
			memset(sd->sd_win, 0, sizeof(sd->sd_win));
L
Linus Torvalds 已提交
639 640 641 642 643 644 645 646
			sd->sd_max = seq_num;
		} else while (sd->sd_max < seq_num) {
			sd->sd_max++;
			__clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
		}
		__set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
		goto ok;
	} else if (seq_num <= sd->sd_max - GSS_SEQ_WIN) {
647
		goto toolow;
L
Linus Torvalds 已提交
648 649
	}
	if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
650 651
		goto alreadyseen;

L
Linus Torvalds 已提交
652
ok:
653 654
	result = true;
out:
L
Linus Torvalds 已提交
655
	spin_unlock(&sd->sd_lock);
656 657 658 659 660 661 662 663 664 665
	return result;

toolow:
	trace_rpcgss_svc_seqno_low(rqstp, seq_num,
				   sd->sd_max - GSS_SEQ_WIN,
				   sd->sd_max);
	goto out;
alreadyseen:
	trace_rpcgss_svc_seqno_seen(rqstp, seq_num);
	goto out;
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679
}

static inline u32 round_up_to_quad(u32 i)
{
	return (i + 3 ) & ~3;
}

static inline int
svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
{
	int l;

	if (argv->iov_len < 4)
		return -1;
A
Alexey Dobriyan 已提交
680
	o->len = svc_getnl(argv);
L
Linus Torvalds 已提交
681 682 683 684 685 686 687 688 689 690 691 692
	l = round_up_to_quad(o->len);
	if (argv->iov_len < l)
		return -1;
	o->data = argv->iov_base;
	argv->iov_base += l;
	argv->iov_len -= l;
	return 0;
}

static inline int
svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
{
693
	u8 *p;
L
Linus Torvalds 已提交
694 695 696

	if (resv->iov_len + 4 > PAGE_SIZE)
		return -1;
A
Alexey Dobriyan 已提交
697
	svc_putnl(resv, o->len);
L
Linus Torvalds 已提交
698 699 700 701 702
	p = resv->iov_base + resv->iov_len;
	resv->iov_len += round_up_to_quad(o->len);
	if (resv->iov_len > PAGE_SIZE)
		return -1;
	memcpy(p, o->data, o->len);
703
	memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);
L
Linus Torvalds 已提交
704 705 706
	return 0;
}

707 708
/*
 * Verify the checksum on the header and return SVC_OK on success.
L
Linus Torvalds 已提交
709 710 711 712 713
 * Otherwise, return SVC_DROP (in the case of a bad sequence number)
 * or return SVC_DENIED and indicate error in authp.
 */
static int
gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
714
		  __be32 *rpcstart, struct rpc_gss_wire_cred *gc, __be32 *authp)
L
Linus Torvalds 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
{
	struct gss_ctx		*ctx_id = rsci->mechctx;
	struct xdr_buf		rpchdr;
	struct xdr_netobj	checksum;
	u32			flavor = 0;
	struct kvec		*argv = &rqstp->rq_arg.head[0];
	struct kvec		iov;

	/* data to compute the checksum over: */
	iov.iov_base = rpcstart;
	iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
	xdr_buf_from_iov(&iov, &rpchdr);

	*authp = rpc_autherr_badverf;
	if (argv->iov_len < 4)
		return SVC_DENIED;
A
Alexey Dobriyan 已提交
731
	flavor = svc_getnl(argv);
L
Linus Torvalds 已提交
732 733 734 735 736 737 738
	if (flavor != RPC_AUTH_GSS)
		return SVC_DENIED;
	if (svc_safe_getnetobj(argv, &checksum))
		return SVC_DENIED;

	if (rqstp->rq_deferred) /* skip verification of revisited request */
		return SVC_OK;
739
	if (gss_verify_mic(ctx_id, &rpchdr, &checksum) != GSS_S_COMPLETE) {
L
Linus Torvalds 已提交
740 741 742 743 744
		*authp = rpcsec_gsserr_credproblem;
		return SVC_DENIED;
	}

	if (gc->gc_seq > MAXSEQ) {
745
		trace_rpcgss_svc_seqno_large(rqstp, gc->gc_seq);
L
Linus Torvalds 已提交
746 747 748
		*authp = rpcsec_gsserr_ctxproblem;
		return SVC_DENIED;
	}
749
	if (!gss_check_seq_num(rqstp, rsci, gc->gc_seq))
L
Linus Torvalds 已提交
750 751 752 753
		return SVC_DROP;
	return SVC_OK;
}

754 755 756
static int
gss_write_null_verf(struct svc_rqst *rqstp)
{
757
	__be32     *p;
758

A
Alexey Dobriyan 已提交
759
	svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);
760 761 762 763 764 765 766 767
	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
	/* don't really need to check if head->iov_len > PAGE_SIZE ... */
	*p++ = 0;
	if (!xdr_ressize_check(rqstp, p))
		return -1;
	return 0;
}

L
Linus Torvalds 已提交
768 769 770
static int
gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
{
771
	__be32			*xdr_seq;
L
Linus Torvalds 已提交
772 773 774
	u32			maj_stat;
	struct xdr_buf		verf_data;
	struct xdr_netobj	mic;
775
	__be32			*p;
L
Linus Torvalds 已提交
776
	struct kvec		iov;
777
	int err = -1;
L
Linus Torvalds 已提交
778

A
Alexey Dobriyan 已提交
779
	svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);
780 781 782 783
	xdr_seq = kmalloc(4, GFP_KERNEL);
	if (!xdr_seq)
		return -1;
	*xdr_seq = htonl(seq);
L
Linus Torvalds 已提交
784

785 786
	iov.iov_base = xdr_seq;
	iov.iov_len = 4;
L
Linus Torvalds 已提交
787 788 789
	xdr_buf_from_iov(&iov, &verf_data);
	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
	mic.data = (u8 *)(p + 1);
790
	maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
L
Linus Torvalds 已提交
791
	if (maj_stat != GSS_S_COMPLETE)
792
		goto out;
L
Linus Torvalds 已提交
793 794 795 796
	*p++ = htonl(mic.len);
	memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
	p += XDR_QUADLEN(mic.len);
	if (!xdr_ressize_check(rqstp, p))
797 798 799 800 801
		goto out;
	err = 0;
out:
	kfree(xdr_seq);
	return err;
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
}

struct gss_domain {
	struct auth_domain	h;
	u32			pseudoflavor;
};

static struct auth_domain *
find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
{
	char *name;

	name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
	if (!name)
		return NULL;
	return auth_domain_find(name);
}

820 821
static struct auth_ops svcauthops_gss;

822 823 824 825 826 827 828
u32 svcauth_gss_flavor(struct auth_domain *dom)
{
	struct gss_domain *gd = container_of(dom, struct gss_domain, h);

	return gd->pseudoflavor;
}

829
EXPORT_SYMBOL_GPL(svcauth_gss_flavor);
830

831
struct auth_domain *
L
Linus Torvalds 已提交
832 833 834 835 836 837 838 839 840
svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
{
	struct gss_domain	*new;
	struct auth_domain	*test;
	int			stat = -ENOMEM;

	new = kmalloc(sizeof(*new), GFP_KERNEL);
	if (!new)
		goto out;
841
	kref_init(&new->h.ref);
842
	new->h.name = kstrdup(name, GFP_KERNEL);
L
Linus Torvalds 已提交
843 844
	if (!new->h.name)
		goto out_free_dom;
845
	new->h.flavour = &svcauthops_gss;
L
Linus Torvalds 已提交
846 847
	new->pseudoflavor = pseudoflavor;

848
	test = auth_domain_lookup(name, &new->h);
849 850 851 852
	if (test != &new->h) {
		pr_warn("svc: duplicate registration of gss pseudo flavour %s.\n",
			name);
		stat = -EADDRINUSE;
853
		auth_domain_put(test);
854
		goto out_free_name;
L
Linus Torvalds 已提交
855
	}
856
	return test;
L
Linus Torvalds 已提交
857

858 859
out_free_name:
	kfree(new->h.name);
L
Linus Torvalds 已提交
860 861 862
out_free_dom:
	kfree(new);
out:
863
	return ERR_PTR(stat);
L
Linus Torvalds 已提交
864
}
865
EXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);
L
Linus Torvalds 已提交
866 867 868 869

static inline int
read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
{
870
	__be32  raw;
L
Linus Torvalds 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
	int     status;

	status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
	if (status)
		return status;
	*obj = ntohl(raw);
	return 0;
}

/* It would be nice if this bit of code could be shared with the client.
 * Obstacles:
 *	The client shouldn't malloc(), would have to pass in own memory.
 *	The server uses base of head iovec as read pointer, while the
 *	client uses separate pointer. */
static int
886
unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
L
Linus Torvalds 已提交
887
{
888
	u32 integ_len, rseqno, maj_stat;
L
Linus Torvalds 已提交
889 890 891 892
	int stat = -EINVAL;
	struct xdr_netobj mic;
	struct xdr_buf integ_buf;

893 894
	mic.data = NULL;

C
Chuck Lever 已提交
895 896 897 898 899 900 901 902
	/* NFS READ normally uses splice to send data in-place. However
	 * the data in cache can change after the reply's MIC is computed
	 * but before the RPC reply is sent. To prevent the client from
	 * rejecting the server-computed MIC in this somewhat rare case,
	 * do not use splice with the GSS integrity service.
	 */
	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);

903 904 905 906
	/* Did we already verify the signature on the original pass through? */
	if (rqstp->rq_deferred)
		return 0;

A
Alexey Dobriyan 已提交
907
	integ_len = svc_getnl(&buf->head[0]);
L
Linus Torvalds 已提交
908
	if (integ_len & 3)
909
		goto unwrap_failed;
L
Linus Torvalds 已提交
910
	if (integ_len > buf->len)
911 912 913 914
		goto unwrap_failed;
	if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
		goto unwrap_failed;

L
Linus Torvalds 已提交
915 916
	/* copy out mic... */
	if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
917
		goto unwrap_failed;
L
Linus Torvalds 已提交
918
	if (mic.len > RPC_MAX_AUTH_SIZE)
919
		goto unwrap_failed;
L
Linus Torvalds 已提交
920 921
	mic.data = kmalloc(mic.len, GFP_KERNEL);
	if (!mic.data)
922
		goto unwrap_failed;
L
Linus Torvalds 已提交
923
	if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
924
		goto unwrap_failed;
925
	maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);
L
Linus Torvalds 已提交
926
	if (maj_stat != GSS_S_COMPLETE)
927 928 929 930
		goto bad_mic;
	rseqno = svc_getnl(&buf->head[0]);
	if (rseqno != seq)
		goto bad_seqno;
931
	/* trim off the mic and padding at the end before returning */
932
	xdr_buf_trim(buf, round_up_to_quad(mic.len) + 4);
L
Linus Torvalds 已提交
933 934
	stat = 0;
out:
935
	kfree(mic.data);
L
Linus Torvalds 已提交
936
	return stat;
937 938 939 940 941 942 943 944 945 946

unwrap_failed:
	trace_rpcgss_svc_unwrap_failed(rqstp);
	goto out;
bad_seqno:
	trace_rpcgss_svc_seqno_bad(rqstp, seq, rseqno);
	goto out;
bad_mic:
	trace_rpcgss_svc_mic(rqstp, maj_stat);
	goto out;
L
Linus Torvalds 已提交
947 948
}

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
static inline int
total_buf_len(struct xdr_buf *buf)
{
	return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;
}

static void
fix_priv_head(struct xdr_buf *buf, int pad)
{
	if (buf->page_len == 0) {
		/* We need to adjust head and buf->len in tandem in this
		 * case to make svc_defer() work--it finds the original
		 * buffer start using buf->len - buf->head[0].iov_len. */
		buf->head[0].iov_len -= pad;
	}
}

static int
unwrap_priv_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
{
	u32 priv_len, maj_stat;
970
	int pad, remaining_len, offset;
971
	u32 rseqno;
972

973
	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
974

A
Alexey Dobriyan 已提交
975
	priv_len = svc_getnl(&buf->head[0]);
976 977 978 979 980 981 982 983 984 985
	if (rqstp->rq_deferred) {
		/* Already decrypted last time through! The sequence number
		 * check at out_seq is unnecessary but harmless: */
		goto out_seq;
	}
	/* buf->len is the number of bytes from the original start of the
	 * request to the end, where head[0].iov_len is just the bytes
	 * not yet read from the head, so these two values are different: */
	remaining_len = total_buf_len(buf);
	if (priv_len > remaining_len)
986
		goto unwrap_failed;
987 988 989 990
	pad = remaining_len - priv_len;
	buf->len -= pad;
	fix_priv_head(buf, pad);

991
	maj_stat = gss_unwrap(ctx, 0, priv_len, buf);
992 993 994 995 996 997 998 999
	pad = priv_len - buf->len;
	buf->len -= pad;
	/* The upper layers assume the buffer is aligned on 4-byte boundaries.
	 * In the krb5p case, at least, the data ends up offset, so we need to
	 * move it around. */
	/* XXX: This is very inefficient.  It would be better to either do
	 * this while we encrypt, or maybe in the receive code, if we can peak
	 * ahead and work out the service and mechanism there. */
C
Chuck Lever 已提交
1000
	offset = xdr_pad_size(buf->head[0].iov_len);
1001 1002 1003 1004 1005 1006
	if (offset) {
		buf->buflen = RPCSVC_MAXPAYLOAD;
		xdr_shift_buf(buf, offset);
		fix_priv_head(buf, pad);
	}
	if (maj_stat != GSS_S_COMPLETE)
1007
		goto bad_unwrap;
1008
out_seq:
1009 1010 1011
	rseqno = svc_getnl(&buf->head[0]);
	if (rseqno != seq)
		goto bad_seqno;
1012
	return 0;
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

unwrap_failed:
	trace_rpcgss_svc_unwrap_failed(rqstp);
	return -EINVAL;
bad_seqno:
	trace_rpcgss_svc_seqno_bad(rqstp, seq, rseqno);
	return -EINVAL;
bad_unwrap:
	trace_rpcgss_svc_unwrap(rqstp, maj_stat);
	return -EINVAL;
1023 1024
}

L
Linus Torvalds 已提交
1025 1026 1027
struct gss_svc_data {
	/* decoded gss client cred: */
	struct rpc_gss_wire_cred	clcred;
1028 1029 1030
	/* save a pointer to the beginning of the encoded verifier,
	 * for use in encryption/checksumming in svcauth_gss_release: */
	__be32				*verf_start;
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039
	struct rsc			*rsci;
};

static int
svcauth_gss_set_client(struct svc_rqst *rqstp)
{
	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
	struct rsc *rsci = svcdata->rsci;
	struct rpc_gss_wire_cred *gc = &svcdata->clcred;
1040
	int stat;
L
Linus Torvalds 已提交
1041

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
	/*
	 * A gss export can be specified either by:
	 * 	export	*(sec=krb5,rw)
	 * or by
	 * 	export gss/krb5(rw)
	 * The latter is deprecated; but for backwards compatibility reasons
	 * the nfsd code will still fall back on trying it if the former
	 * doesn't work; so we try to make both available to nfsd, below.
	 */
	rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
	if (rqstp->rq_gssclient == NULL)
L
Linus Torvalds 已提交
1053
		return SVC_DENIED;
1054
	stat = svcauth_unix_set_client(rqstp);
1055
	if (stat == SVC_DROP || stat == SVC_CLOSE)
1056
		return stat;
L
Linus Torvalds 已提交
1057 1058 1059
	return SVC_OK;
}

1060
static inline int
1061 1062
gss_write_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
		struct xdr_netobj *out_handle, int *major_status)
1063 1064
{
	struct rsc *rsci;
1065
	int        rc;
1066

1067
	if (*major_status != GSS_S_COMPLETE)
1068
		return gss_write_null_verf(rqstp);
1069
	rsci = gss_svc_searchbyctx(cd, out_handle);
1070
	if (rsci == NULL) {
1071
		*major_status = GSS_S_NO_CONTEXT;
1072 1073
		return gss_write_null_verf(rqstp);
	}
1074
	rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
1075
	cache_put(&rsci->h, cd);
1076
	return rc;
1077 1078
}

1079
static inline int
1080 1081 1082
gss_read_common_verf(struct rpc_gss_wire_cred *gc,
		     struct kvec *argv, __be32 *authp,
		     struct xdr_netobj *in_handle)
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
{
	/* Read the verifier; should be NULL: */
	*authp = rpc_autherr_badverf;
	if (argv->iov_len < 2 * 4)
		return SVC_DENIED;
	if (svc_getnl(argv) != RPC_AUTH_NULL)
		return SVC_DENIED;
	if (svc_getnl(argv) != 0)
		return SVC_DENIED;
	/* Martial context handle and token for upcall: */
	*authp = rpc_autherr_badcred;
	if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
		return SVC_DENIED;
1096
	if (dup_netobj(in_handle, &gc->gc_ctx))
1097
		return SVC_CLOSE;
1098
	*authp = rpc_autherr_badverf;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

	return 0;
}

static inline int
gss_read_verf(struct rpc_gss_wire_cred *gc,
	      struct kvec *argv, __be32 *authp,
	      struct xdr_netobj *in_handle,
	      struct xdr_netobj *in_token)
{
	struct xdr_netobj tmpobj;
	int res;

	res = gss_read_common_verf(gc, argv, authp, in_handle);
	if (res)
		return res;

1116
	if (svc_safe_getnetobj(argv, &tmpobj)) {
1117
		kfree(in_handle->data);
1118 1119
		return SVC_DENIED;
	}
1120 1121
	if (dup_netobj(in_token, &tmpobj)) {
		kfree(in_handle->data);
1122
		return SVC_CLOSE;
1123 1124
	}

1125 1126 1127
	return 0;
}

C
Chuck Lever 已提交
1128
static void gss_free_in_token_pages(struct gssp_in_token *in_token)
1129 1130
{
	u32 inlen;
C
Chuck Lever 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	int i;

	i = 0;
	inlen = in_token->page_len;
	while (inlen) {
		if (in_token->pages[i])
			put_page(in_token->pages[i]);
		inlen -= inlen > PAGE_SIZE ? PAGE_SIZE : inlen;
	}

	kfree(in_token->pages);
	in_token->pages = NULL;
}

static int gss_read_proxy_verf(struct svc_rqst *rqstp,
			       struct rpc_gss_wire_cred *gc, __be32 *authp,
			       struct xdr_netobj *in_handle,
			       struct gssp_in_token *in_token)
{
	struct kvec *argv = &rqstp->rq_arg.head[0];
	unsigned int page_base, length;
	int pages, i, res;
	size_t inlen;
1154 1155 1156 1157 1158 1159 1160 1161 1162

	res = gss_read_common_verf(gc, argv, authp, in_handle);
	if (res)
		return res;

	inlen = svc_getnl(argv);
	if (inlen > (argv->iov_len + rqstp->rq_arg.page_len))
		return SVC_DENIED;

C
Chuck Lever 已提交
1163 1164 1165 1166 1167
	pages = DIV_ROUND_UP(inlen, PAGE_SIZE);
	in_token->pages = kcalloc(pages, sizeof(struct page *), GFP_KERNEL);
	if (!in_token->pages)
		return SVC_DENIED;
	in_token->page_base = 0;
1168
	in_token->page_len = inlen;
C
Chuck Lever 已提交
1169 1170 1171 1172 1173 1174 1175
	for (i = 0; i < pages; i++) {
		in_token->pages[i] = alloc_page(GFP_KERNEL);
		if (!in_token->pages[i]) {
			gss_free_in_token_pages(in_token);
			return SVC_DENIED;
		}
	}
1176

C
Chuck Lever 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
	length = min_t(unsigned int, inlen, argv->iov_len);
	memcpy(page_address(in_token->pages[0]), argv->iov_base, length);
	inlen -= length;

	i = 1;
	page_base = rqstp->rq_arg.page_base;
	while (inlen) {
		length = min_t(unsigned int, inlen, PAGE_SIZE);
		memcpy(page_address(in_token->pages[i]),
		       page_address(rqstp->rq_arg.pages[i]) + page_base,
		       length);

		inlen -= length;
		page_base = 0;
		i++;
	}
1193 1194 1195
	return 0;
}

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
static inline int
gss_write_resv(struct kvec *resv, size_t size_limit,
	       struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
	       int major_status, int minor_status)
{
	if (resv->iov_len + 4 > size_limit)
		return -1;
	svc_putnl(resv, RPC_SUCCESS);
	if (svc_safe_putnetobj(resv, out_handle))
		return -1;
	if (resv->iov_len + 3 * 4 > size_limit)
		return -1;
	svc_putnl(resv, major_status);
	svc_putnl(resv, minor_status);
	svc_putnl(resv, GSS_SEQ_WIN);
	if (svc_safe_putnetobj(resv, out_token))
		return -1;
	return 0;
}

/*
 * Having read the cred already and found we're in the context
 * initiation case, read the verifier and initiate (or check the results
 * of) upcalls to userspace for help with context initiation.  If
 * the upcall results are available, write the verifier and result.
 * Otherwise, drop the request pending an answer to the upcall.
 */
1223
static int svcauth_gss_legacy_init(struct svc_rqst *rqstp,
1224 1225 1226 1227 1228 1229
			struct rpc_gss_wire_cred *gc, __be32 *authp)
{
	struct kvec *argv = &rqstp->rq_arg.head[0];
	struct kvec *resv = &rqstp->rq_res.head[0];
	struct rsi *rsip, rsikey;
	int ret;
1230
	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1231 1232 1233 1234 1235 1236 1237

	memset(&rsikey, 0, sizeof(rsikey));
	ret = gss_read_verf(gc, argv, authp,
			    &rsikey.in_handle, &rsikey.in_token);
	if (ret)
		return ret;

1238
	/* Perform upcall, or find upcall result: */
1239
	rsip = rsi_lookup(sn->rsi_cache, &rsikey);
1240 1241
	rsi_free(&rsikey);
	if (!rsip)
1242
		return SVC_CLOSE;
1243
	if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
1244
		/* No upcall result: */
1245
		return SVC_CLOSE;
1246 1247 1248

	ret = SVC_CLOSE;
	/* Got an answer to the upcall; use it: */
1249 1250
	if (gss_write_init_verf(sn->rsc_cache, rqstp,
				&rsip->out_handle, &rsip->major_status))
1251
		goto out;
1252 1253 1254
	if (gss_write_resv(resv, PAGE_SIZE,
			   &rsip->out_handle, &rsip->out_token,
			   rsip->major_status, rsip->minor_status))
1255 1256
		goto out;

1257 1258
	ret = SVC_COMPLETE;
out:
1259
	cache_put(&rsip->h, sn->rsi_cache);
1260
	return ret;
1261 1262
}

1263 1264 1265 1266 1267 1268 1269 1270
static int gss_proxy_save_rsc(struct cache_detail *cd,
				struct gssp_upcall_data *ud,
				uint64_t *handle)
{
	struct rsc rsci, *rscp = NULL;
	static atomic64_t ctxhctr;
	long long ctxh;
	struct gss_api_mech *gm = NULL;
1271
	time64_t expiry;
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	int status = -EINVAL;

	memset(&rsci, 0, sizeof(rsci));
	/* context handle */
	status = -ENOMEM;
	/* the handle needs to be just a unique id,
	 * use a static counter */
	ctxh = atomic64_inc_return(&ctxhctr);

	/* make a copy for the caller */
	*handle = ctxh;

	/* make a copy for the rsc cache */
	if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
		goto out;
	rscp = rsc_lookup(cd, &rsci);
	if (!rscp)
		goto out;

	/* creds */
	if (!ud->found_creds) {
		/* userspace seem buggy, we should always get at least a
		 * mapping to nobody */
1295
		goto out;
1296
	} else {
1297
		struct timespec64 boot;
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

		/* steal creds */
		rsci.cred = ud->creds;
		memset(&ud->creds, 0, sizeof(struct svc_cred));

		status = -EOPNOTSUPP;
		/* get mech handle from OID */
		gm = gss_mech_get_by_OID(&ud->mech_oid);
		if (!gm)
			goto out;
1308
		rsci.cred.cr_gss_mech = gm;
1309 1310 1311 1312 1313 1314 1315 1316 1317

		status = -EINVAL;
		/* mech-specific data: */
		status = gss_import_sec_context(ud->out_handle.data,
						ud->out_handle.len,
						gm, &rsci.mechctx,
						&expiry, GFP_KERNEL);
		if (status)
			goto out;
1318 1319 1320

		getboottime64(&boot);
		expiry -= boot.tv_sec;
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
	}

	rsci.h.expiry_time = expiry;
	rscp = rsc_update(cd, &rsci, rscp);
	status = 0;
out:
	rsc_free(&rsci);
	if (rscp)
		cache_put(&rscp->h, cd);
	else
		status = -ENOMEM;
	return status;
}

static int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
			struct rpc_gss_wire_cred *gc, __be32 *authp)
{
	struct kvec *resv = &rqstp->rq_res.head[0];
	struct xdr_netobj cli_handle;
	struct gssp_upcall_data ud;
	uint64_t handle;
	int status;
	int ret;
1344
	struct net *net = SVC_NET(rqstp);
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);

	memset(&ud, 0, sizeof(ud));
	ret = gss_read_proxy_verf(rqstp, gc, authp,
				  &ud.in_handle, &ud.in_token);
	if (ret)
		return ret;

	ret = SVC_CLOSE;

	/* Perform synchronous upcall to gss-proxy */
	status = gssp_accept_sec_context_upcall(net, &ud);
	if (status)
		goto out;

1360
	trace_rpcgss_svc_accept_upcall(rqstp, ud.major_status, ud.minor_status);
1361 1362 1363 1364 1365 1366 1367

	switch (ud.major_status) {
	case GSS_S_CONTINUE_NEEDED:
		cli_handle = ud.out_handle;
		break;
	case GSS_S_COMPLETE:
		status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
1368
		if (status)
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
			goto out;
		cli_handle.data = (u8 *)&handle;
		cli_handle.len = sizeof(handle);
		break;
	default:
		goto out;
	}

	/* Got an answer to the upcall; use it: */
	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1379
				&cli_handle, &ud.major_status))
1380 1381 1382
		goto out;
	if (gss_write_resv(resv, PAGE_SIZE,
			   &cli_handle, &ud.out_token,
1383
			   ud.major_status, ud.minor_status))
1384 1385 1386 1387
		goto out;

	ret = SVC_COMPLETE;
out:
C
Chuck Lever 已提交
1388
	gss_free_in_token_pages(&ud.in_token);
1389 1390 1391 1392
	gssp_free_upcall_data(&ud);
	return ret;
}

J
Jeff Layton 已提交
1393 1394 1395 1396 1397 1398
/*
 * Try to set the sn->use_gss_proxy variable to a new value. We only allow
 * it to be changed if it's currently undefined (-1). If it's any other value
 * then return -EBUSY unless the type wouldn't have changed anyway.
 */
static int set_gss_proxy(struct net *net, int type)
1399 1400
{
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
J
Jeff Layton 已提交
1401
	int ret;
1402

J
Jeff Layton 已提交
1403 1404 1405 1406
	WARN_ON_ONCE(type != 0 && type != 1);
	ret = cmpxchg(&sn->use_gss_proxy, -1, type);
	if (ret != -1 && ret != type)
		return -EBUSY;
1407 1408 1409
	return 0;
}

J
Jeff Layton 已提交
1410
static bool use_gss_proxy(struct net *net)
1411 1412 1413
{
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);

J
Jeff Layton 已提交
1414 1415 1416 1417
	/* If use_gss_proxy is still undefined, then try to disable it */
	if (sn->use_gss_proxy == -1)
		set_gss_proxy(net, 0);
	return sn->use_gss_proxy;
1418 1419
}

J
Jeff Layton 已提交
1420 1421
#ifdef CONFIG_PROC_FS

1422 1423 1424
static ssize_t write_gssp(struct file *file, const char __user *buf,
			 size_t count, loff_t *ppos)
{
A
Al Viro 已提交
1425
	struct net *net = PDE_DATA(file_inode(file));
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
	char tbuf[20];
	unsigned long i;
	int res;

	if (*ppos || count > sizeof(tbuf)-1)
		return -EINVAL;
	if (copy_from_user(tbuf, buf, count))
		return -EFAULT;

	tbuf[count] = 0;
	res = kstrtoul(tbuf, 0, &i);
	if (res)
		return res;
	if (i != 1)
		return -EINVAL;
1441
	res = set_gssp_clnt(net);
1442 1443
	if (res)
		return res;
1444
	res = set_gss_proxy(net, 1);
1445 1446 1447 1448 1449 1450 1451 1452
	if (res)
		return res;
	return count;
}

static ssize_t read_gssp(struct file *file, char __user *buf,
			 size_t count, loff_t *ppos)
{
A
Al Viro 已提交
1453
	struct net *net = PDE_DATA(file_inode(file));
1454
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1455 1456 1457 1458
	unsigned long p = *ppos;
	char tbuf[10];
	size_t len;

1459
	snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	len = strlen(tbuf);
	if (p >= len)
		return 0;
	len -= p;
	if (len > count)
		len = count;
	if (copy_to_user(buf, (void *)(tbuf+p), len))
		return -EFAULT;
	*ppos += len;
	return len;
}

1472 1473 1474 1475
static const struct proc_ops use_gss_proxy_proc_ops = {
	.proc_open	= nonseekable_open,
	.proc_write	= write_gssp,
	.proc_read	= read_gssp,
1476 1477 1478 1479 1480 1481 1482 1483
};

static int create_use_gss_proxy_proc_entry(struct net *net)
{
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
	struct proc_dir_entry **p = &sn->use_gssp_proc;

	sn->use_gss_proxy = -1;
1484
	*p = proc_create_data("use-gss-proxy", S_IFREG | 0600,
1485
			      sn->proc_net_rpc,
1486
			      &use_gss_proxy_proc_ops, net);
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
	if (!*p)
		return -ENOMEM;
	init_gssp_clnt(sn);
	return 0;
}

static void destroy_use_gss_proxy_proc_entry(struct net *net)
{
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);

	if (sn->use_gssp_proc) {
S
Stephen Hemminger 已提交
1498
		remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
1499 1500 1501
		clear_gssp_clnt(sn);
	}
}
1502 1503 1504 1505 1506 1507 1508 1509
#else /* CONFIG_PROC_FS */

static int create_use_gss_proxy_proc_entry(struct net *net)
{
	return 0;
}

static void destroy_use_gss_proxy_proc_entry(struct net *net) {}
1510 1511 1512

#endif /* CONFIG_PROC_FS */

L
Linus Torvalds 已提交
1513 1514 1515 1516 1517 1518 1519 1520 1521
/*
 * Accept an rpcsec packet.
 * If context establishment, punt to user space
 * If data exchange, verify/decrypt
 * If context destruction, handle here
 * In the context establishment and destruction case we encode
 * response here and return SVC_COMPLETE.
 */
static int
1522
svcauth_gss_accept(struct svc_rqst *rqstp, __be32 *authp)
L
Linus Torvalds 已提交
1523 1524 1525 1526 1527 1528 1529
{
	struct kvec	*argv = &rqstp->rq_arg.head[0];
	struct kvec	*resv = &rqstp->rq_res.head[0];
	u32		crlen;
	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
	struct rpc_gss_wire_cred *gc;
	struct rsc	*rsci = NULL;
1530 1531
	__be32		*rpcstart;
	__be32		*reject_stat = resv->iov_base + resv->iov_len;
L
Linus Torvalds 已提交
1532
	int		ret;
1533
	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
L
Linus Torvalds 已提交
1534 1535 1536 1537 1538 1539 1540

	*authp = rpc_autherr_badcred;
	if (!svcdata)
		svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
	if (!svcdata)
		goto auth_err;
	rqstp->rq_auth_data = svcdata;
1541
	svcdata->verf_start = NULL;
L
Linus Torvalds 已提交
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
	svcdata->rsci = NULL;
	gc = &svcdata->clcred;

	/* start of rpc packet is 7 u32's back from here:
	 * xid direction rpcversion prog vers proc flavour
	 */
	rpcstart = argv->iov_base;
	rpcstart -= 7;

	/* credential is:
	 *   version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
L
Lucas De Marchi 已提交
1553
	 * at least 5 u32s, and is preceded by length, so that makes 6.
L
Linus Torvalds 已提交
1554 1555 1556 1557
	 */

	if (argv->iov_len < 5 * 4)
		goto auth_err;
A
Alexey Dobriyan 已提交
1558 1559
	crlen = svc_getnl(argv);
	if (svc_getnl(argv) != RPC_GSS_VERSION)
L
Linus Torvalds 已提交
1560
		goto auth_err;
A
Alexey Dobriyan 已提交
1561 1562 1563
	gc->gc_proc = svc_getnl(argv);
	gc->gc_seq = svc_getnl(argv);
	gc->gc_svc = svc_getnl(argv);
L
Linus Torvalds 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
	if (svc_safe_getnetobj(argv, &gc->gc_ctx))
		goto auth_err;
	if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
		goto auth_err;

	if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
		goto auth_err;

	*authp = rpc_autherr_badverf;
	switch (gc->gc_proc) {
	case RPC_GSS_PROC_INIT:
	case RPC_GSS_PROC_CONTINUE_INIT:
1576 1577 1578 1579
		if (use_gss_proxy(SVC_NET(rqstp)))
			return svcauth_gss_proxy_init(rqstp, gc, authp);
		else
			return svcauth_gss_legacy_init(rqstp, gc, authp);
L
Linus Torvalds 已提交
1580 1581
	case RPC_GSS_PROC_DATA:
	case RPC_GSS_PROC_DESTROY:
1582
		/* Look up the context, and check the verifier: */
L
Linus Torvalds 已提交
1583
		*authp = rpcsec_gsserr_credproblem;
1584
		rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
L
Linus Torvalds 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
		if (!rsci)
			goto auth_err;
		switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
		case SVC_OK:
			break;
		case SVC_DENIED:
			goto auth_err;
		case SVC_DROP:
			goto drop;
		}
		break;
	default:
		*authp = rpc_autherr_rejectedcred;
		goto auth_err;
	}

	/* now act upon the command: */
	switch (gc->gc_proc) {
	case RPC_GSS_PROC_DESTROY:
1604 1605
		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
			goto auth_err;
1606 1607
		/* Delete the entry from the cache_list and call cache_put */
		sunrpc_cache_unhash(sn->rsc_cache, &rsci->h);
L
Linus Torvalds 已提交
1608 1609
		if (resv->iov_len + 4 > PAGE_SIZE)
			goto drop;
A
Alexey Dobriyan 已提交
1610
		svc_putnl(resv, RPC_SUCCESS);
L
Linus Torvalds 已提交
1611 1612 1613
		goto complete;
	case RPC_GSS_PROC_DATA:
		*authp = rpcsec_gsserr_ctxproblem;
1614
		svcdata->verf_start = resv->iov_base + resv->iov_len;
L
Linus Torvalds 已提交
1615 1616 1617 1618 1619 1620 1621 1622 1623
		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
			goto auth_err;
		rqstp->rq_cred = rsci->cred;
		get_group_info(rsci->cred.cr_group_info);
		*authp = rpc_autherr_badcred;
		switch (gc->gc_svc) {
		case RPC_GSS_SVC_NONE:
			break;
		case RPC_GSS_SVC_INTEGRITY:
1624 1625 1626
			/* placeholders for length and seq. number: */
			svc_putnl(resv, 0);
			svc_putnl(resv, 0);
1627
			if (unwrap_integ_data(rqstp, &rqstp->rq_arg,
L
Linus Torvalds 已提交
1628
					gc->gc_seq, rsci->mechctx))
1629
				goto garbage_args;
1630
			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE;
1631 1632
			break;
		case RPC_GSS_SVC_PRIVACY:
L
Linus Torvalds 已提交
1633
			/* placeholders for length and seq. number: */
A
Alexey Dobriyan 已提交
1634 1635
			svc_putnl(resv, 0);
			svc_putnl(resv, 0);
1636 1637
			if (unwrap_priv_data(rqstp, &rqstp->rq_arg,
					gc->gc_seq, rsci->mechctx))
1638
				goto garbage_args;
1639
			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE * 2;
1640
			break;
L
Linus Torvalds 已提交
1641 1642 1643 1644 1645
		default:
			goto auth_err;
		}
		svcdata->rsci = rsci;
		cache_get(&rsci->h);
1646
		rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
1647 1648 1649
					rsci->mechctx->mech_type,
					GSS_C_QOP_DEFAULT,
					gc->gc_svc);
L
Linus Torvalds 已提交
1650
		ret = SVC_OK;
1651
		trace_rpcgss_svc_authenticate(rqstp, gc);
L
Linus Torvalds 已提交
1652 1653
		goto out;
	}
1654 1655 1656
garbage_args:
	ret = SVC_GARBAGE;
	goto out;
L
Linus Torvalds 已提交
1657
auth_err:
1658
	/* Restore write pointer to its original value: */
L
Linus Torvalds 已提交
1659 1660 1661 1662 1663 1664 1665
	xdr_ressize_check(rqstp, reject_stat);
	ret = SVC_DENIED;
	goto out;
complete:
	ret = SVC_COMPLETE;
	goto out;
drop:
1666
	ret = SVC_CLOSE;
L
Linus Torvalds 已提交
1667 1668
out:
	if (rsci)
1669
		cache_put(&rsci->h, sn->rsc_cache);
L
Linus Torvalds 已提交
1670 1671 1672
	return ret;
}

1673
static __be32 *
1674 1675
svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
{
1676 1677
	__be32 *p;
	u32 verf_len;
1678

1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
	p = gsd->verf_start;
	gsd->verf_start = NULL;

	/* If the reply stat is nonzero, don't wrap: */
	if (*(p-1) != rpc_success)
		return NULL;
	/* Skip the verifier: */
	p += 1;
	verf_len = ntohl(*p++);
	p += XDR_QUADLEN(verf_len);
1689 1690
	/* move accept_stat to right place: */
	memcpy(p, p + 2, 4);
1691
	/* Also don't wrap if the accept stat is nonzero: */
1692 1693 1694 1695 1696 1697 1698 1699
	if (*p != rpc_success) {
		resbuf->head[0].iov_len -= 2 * 4;
		return NULL;
	}
	p++;
	return p;
}

1700 1701
static inline int
svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
L
Linus Torvalds 已提交
1702 1703 1704 1705 1706 1707 1708
{
	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
	struct rpc_gss_wire_cred *gc = &gsd->clcred;
	struct xdr_buf *resbuf = &rqstp->rq_res;
	struct xdr_buf integ_buf;
	struct xdr_netobj mic;
	struct kvec *resv;
1709
	__be32 *p;
L
Linus Torvalds 已提交
1710 1711 1712
	int integ_offset, integ_len;
	int stat = -EINVAL;

1713 1714
	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
	if (p == NULL)
1715 1716 1717
		goto out;
	integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
	integ_len = resbuf->len - integ_offset;
C
Chuck Lever 已提交
1718 1719
	if (integ_len & 3)
		goto out;
1720 1721
	*p++ = htonl(integ_len);
	*p++ = htonl(gc->gc_seq);
J
J. Bruce Fields 已提交
1722 1723 1724 1725
	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len)) {
		WARN_ON_ONCE(1);
		goto out_err;
	}
1726
	if (resbuf->tail[0].iov_base == NULL) {
1727 1728 1729 1730 1731 1732
		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
			goto out_err;
		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
						+ resbuf->head[0].iov_len;
		resbuf->tail[0].iov_len = 0;
	}
J
J. Bruce Fields 已提交
1733
	resv = &resbuf->tail[0];
1734 1735 1736
	mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
	if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
		goto out_err;
A
Alexey Dobriyan 已提交
1737
	svc_putnl(resv, mic.len);
1738 1739 1740 1741 1742
	memset(mic.data + mic.len, 0,
			round_up_to_quad(mic.len) - mic.len);
	resv->iov_len += XDR_QUADLEN(mic.len) << 2;
	/* not strictly required: */
	resbuf->len += XDR_QUADLEN(mic.len) << 2;
1743 1744
	if (resv->iov_len > PAGE_SIZE)
		goto out_err;
1745 1746 1747 1748 1749 1750
out:
	stat = 0;
out_err:
	return stat;
}

1751 1752 1753 1754 1755 1756 1757
static inline int
svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
{
	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
	struct rpc_gss_wire_cred *gc = &gsd->clcred;
	struct xdr_buf *resbuf = &rqstp->rq_res;
	struct page **inpages = NULL;
1758 1759
	__be32 *p, *len;
	int offset;
1760 1761
	int pad;

1762 1763
	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
	if (p == NULL)
1764 1765 1766 1767 1768 1769 1770
		return 0;
	len = p++;
	offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
	*p++ = htonl(gc->gc_seq);
	inpages = resbuf->pages;
	/* XXX: Would be better to write some xdr helper functions for
	 * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
1771 1772 1773 1774 1775 1776 1777 1778

	/*
	 * If there is currently tail data, make sure there is
	 * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
	 * the page, and move the current tail data such that
	 * there is RPC_MAX_AUTH_SIZE slack space available in
	 * both the head and tail.
	 */
1779
	if (resbuf->tail[0].iov_base) {
1780 1781 1782 1783 1784
		if (resbuf->tail[0].iov_base >=
			resbuf->head[0].iov_base + PAGE_SIZE)
			return -EINVAL;
		if (resbuf->tail[0].iov_base < resbuf->head[0].iov_base)
			return -EINVAL;
1785 1786 1787 1788 1789 1790 1791 1792
		if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
				+ 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
			return -ENOMEM;
		memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
			resbuf->tail[0].iov_base,
			resbuf->tail[0].iov_len);
		resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
	}
1793 1794 1795 1796 1797 1798 1799
	/*
	 * If there is no current tail data, make sure there is
	 * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
	 * allotted page, and set up tail information such that there
	 * is RPC_MAX_AUTH_SIZE slack space available in both the
	 * head and tail.
	 */
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
	if (resbuf->tail[0].iov_base == NULL) {
		if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
			return -ENOMEM;
		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
			+ resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
		resbuf->tail[0].iov_len = 0;
	}
	if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
		return -ENOMEM;
	*len = htonl(resbuf->len - offset);
	pad = 3 - ((resbuf->len - offset - 1)&3);
1811
	p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
1812 1813 1814 1815 1816 1817
	memset(p, 0, pad);
	resbuf->tail[0].iov_len += pad;
	resbuf->len += pad;
	return 0;
}

1818 1819 1820 1821 1822 1823 1824
static int
svcauth_gss_release(struct svc_rqst *rqstp)
{
	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
	struct rpc_gss_wire_cred *gc = &gsd->clcred;
	struct xdr_buf *resbuf = &rqstp->rq_res;
	int stat = -EINVAL;
1825
	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1826

L
Linus Torvalds 已提交
1827 1828 1829
	if (gc->gc_proc != RPC_GSS_PROC_DATA)
		goto out;
	/* Release can be called twice, but we only wrap once. */
1830
	if (gsd->verf_start == NULL)
L
Linus Torvalds 已提交
1831 1832
		goto out;
	/* normally not set till svc_send, but we need it here: */
1833 1834 1835
	/* XXX: what for?  Do we mess it up the moment we call svc_putu32
	 * or whatever? */
	resbuf->len = total_buf_len(resbuf);
L
Linus Torvalds 已提交
1836 1837 1838 1839
	switch (gc->gc_svc) {
	case RPC_GSS_SVC_NONE:
		break;
	case RPC_GSS_SVC_INTEGRITY:
1840 1841 1842
		stat = svcauth_gss_wrap_resp_integ(rqstp);
		if (stat)
			goto out_err;
L
Linus Torvalds 已提交
1843 1844
		break;
	case RPC_GSS_SVC_PRIVACY:
1845 1846 1847 1848
		stat = svcauth_gss_wrap_resp_priv(rqstp);
		if (stat)
			goto out_err;
		break;
1849 1850 1851 1852
	/*
	 * For any other gc_svc value, svcauth_gss_accept() already set
	 * the auth_error appropriately; just fall through:
	 */
L
Linus Torvalds 已提交
1853 1854 1855 1856 1857 1858 1859 1860
	}

out:
	stat = 0;
out_err:
	if (rqstp->rq_client)
		auth_domain_put(rqstp->rq_client);
	rqstp->rq_client = NULL;
1861 1862 1863
	if (rqstp->rq_gssclient)
		auth_domain_put(rqstp->rq_gssclient);
	rqstp->rq_gssclient = NULL;
L
Linus Torvalds 已提交
1864 1865 1866 1867
	if (rqstp->rq_cred.cr_group_info)
		put_group_info(rqstp->rq_cred.cr_group_info);
	rqstp->rq_cred.cr_group_info = NULL;
	if (gsd->rsci)
1868
		cache_put(&gsd->rsci->h, sn->rsc_cache);
L
Linus Torvalds 已提交
1869 1870 1871 1872 1873 1874
	gsd->rsci = NULL;

	return stat;
}

static void
1875
svcauth_gss_domain_release_rcu(struct rcu_head *head)
L
Linus Torvalds 已提交
1876
{
1877
	struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
L
Linus Torvalds 已提交
1878 1879 1880 1881 1882 1883
	struct gss_domain *gd = container_of(dom, struct gss_domain, h);

	kfree(dom->name);
	kfree(gd);
}

1884 1885 1886 1887 1888 1889
static void
svcauth_gss_domain_release(struct auth_domain *dom)
{
	call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu);
}

L
Linus Torvalds 已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
static struct auth_ops svcauthops_gss = {
	.name		= "rpcsec_gss",
	.owner		= THIS_MODULE,
	.flavour	= RPC_AUTH_GSS,
	.accept		= svcauth_gss_accept,
	.release	= svcauth_gss_release,
	.domain_release = svcauth_gss_domain_release,
	.set_client	= svcauth_gss_set_client,
};

1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
static int rsi_cache_create_net(struct net *net)
{
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
	struct cache_detail *cd;
	int err;

	cd = cache_create_net(&rsi_cache_template, net);
	if (IS_ERR(cd))
		return PTR_ERR(cd);
	err = cache_register_net(cd, net);
	if (err) {
		cache_destroy_net(cd, net);
		return err;
	}
	sn->rsi_cache = cd;
	return 0;
}

static void rsi_cache_destroy_net(struct net *net)
{
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
	struct cache_detail *cd = sn->rsi_cache;

	sn->rsi_cache = NULL;
	cache_purge(cd);
	cache_unregister_net(cd, net);
	cache_destroy_net(cd, net);
}

static int rsc_cache_create_net(struct net *net)
{
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
	struct cache_detail *cd;
	int err;

	cd = cache_create_net(&rsc_cache_template, net);
	if (IS_ERR(cd))
		return PTR_ERR(cd);
	err = cache_register_net(cd, net);
	if (err) {
		cache_destroy_net(cd, net);
		return err;
	}
	sn->rsc_cache = cd;
	return 0;
}

static void rsc_cache_destroy_net(struct net *net)
{
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
	struct cache_detail *cd = sn->rsc_cache;

	sn->rsc_cache = NULL;
	cache_purge(cd);
	cache_unregister_net(cd, net);
	cache_destroy_net(cd, net);
}

L
Linus Torvalds 已提交
1958
int
1959
gss_svc_init_net(struct net *net)
L
Linus Torvalds 已提交
1960
{
1961 1962 1963
	int rv;

	rv = rsc_cache_create_net(net);
1964 1965
	if (rv)
		return rv;
1966
	rv = rsi_cache_create_net(net);
1967 1968
	if (rv)
		goto out1;
1969 1970 1971
	rv = create_use_gss_proxy_proc_entry(net);
	if (rv)
		goto out2;
1972
	return 0;
1973 1974
out2:
	destroy_use_gss_proxy_proc_entry(net);
1975
out1:
1976
	rsc_cache_destroy_net(net);
L
Linus Torvalds 已提交
1977 1978 1979
	return rv;
}

1980 1981 1982
void
gss_svc_shutdown_net(struct net *net)
{
1983
	destroy_use_gss_proxy_proc_entry(net);
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
	rsi_cache_destroy_net(net);
	rsc_cache_destroy_net(net);
}

int
gss_svc_init(void)
{
	return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
}

L
Linus Torvalds 已提交
1994 1995 1996 1997 1998
void
gss_svc_shutdown(void)
{
	svc_auth_unregister(RPC_AUTH_GSS);
}