1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Neil Brown <neilb@cse.unsw.edu.au>
4  * J. Bruce Fields <bfields@umich.edu>
5  * Andy Adamson <andros@umich.edu>
6  * Dug Song <dugsong@monkey.org>
7  *
8  * RPCSEC_GSS server authentication.
9  * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
10  * (gssapi)
11  *
12  * The RPCSEC_GSS involves three stages:
13  *  1/ context creation
14  *  2/ data exchange
15  *  3/ context destruction
16  *
17  * Context creation is handled largely by upcalls to user-space.
18  *  In particular, GSS_Accept_sec_context is handled by an upcall
19  * Data exchange is handled entirely within the kernel
20  *  In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
21  * Context destruction is handled in-kernel
22  *  GSS_Delete_sec_context is in-kernel
23  *
24  * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
25  * The context handle and gss_token are used as a key into the rpcsec_init cache.
26  * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
27  * being major_status, minor_status, context_handle, reply_token.
28  * These are sent back to the client.
29  * Sequence window management is handled by the kernel.  The window size if currently
30  * a compile time constant.
31  *
32  * When user-space is happy that a context is established, it places an entry
33  * in the rpcsec_context cache. The key for this cache is the context_handle.
34  * The content includes:
35  *   uid/gidlist - for determining access rights
36  *   mechanism type
37  *   mechanism specific information, such as a key
38  *
39  */
40 
41 #include <linux/slab.h>
42 #include <linux/types.h>
43 #include <linux/module.h>
44 #include <linux/pagemap.h>
45 #include <linux/user_namespace.h>
46 
47 #include <linux/sunrpc/auth_gss.h>
48 #include <linux/sunrpc/gss_err.h>
49 #include <linux/sunrpc/svcauth.h>
50 #include <linux/sunrpc/svcauth_gss.h>
51 #include <linux/sunrpc/cache.h>
52 #include <linux/sunrpc/gss_krb5.h>
53 
54 #include <trace/events/rpcgss.h>
55 
56 #include "gss_rpc_upcall.h"
57 
58 /*
59  * Unfortunately there isn't a maximum checksum size exported via the
60  * GSS API. Manufacture one based on GSS mechanisms supported by this
61  * implementation.
62  */
63 #define GSS_MAX_CKSUMSIZE (GSS_KRB5_TOK_HDR_LEN + GSS_KRB5_MAX_CKSUM_LEN)
64 
65 /*
66  * This value may be increased in the future to accommodate other
67  * usage of the scratch buffer.
68  */
69 #define GSS_SCRATCH_SIZE GSS_MAX_CKSUMSIZE
70 
71 struct gss_svc_data {
72 	/* decoded gss client cred: */
73 	struct rpc_gss_wire_cred	clcred;
74 	/* save a pointer to the beginning of the encoded verifier,
75 	 * for use in encryption/checksumming in svcauth_gss_release: */
76 	__be32				*verf_start;
77 	struct rsc			*rsci;
78 
79 	/* for temporary results */
80 	u8				gsd_scratch[GSS_SCRATCH_SIZE];
81 };
82 
83 /* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
84  * into replies.
85  *
86  * Key is context handle (\x if empty) and gss_token.
87  * Content is major_status minor_status (integers) context_handle, reply_token.
88  *
89  */
90 
91 static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
92 {
93 	return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
94 }
95 
96 #define	RSI_HASHBITS	6
97 #define	RSI_HASHMAX	(1<<RSI_HASHBITS)
98 
99 struct rsi {
100 	struct cache_head	h;
101 	struct xdr_netobj	in_handle, in_token;
102 	struct xdr_netobj	out_handle, out_token;
103 	int			major_status, minor_status;
104 	struct rcu_head		rcu_head;
105 };
106 
107 static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
108 static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
109 
110 static void rsi_free(struct rsi *rsii)
111 {
112 	kfree(rsii->in_handle.data);
113 	kfree(rsii->in_token.data);
114 	kfree(rsii->out_handle.data);
115 	kfree(rsii->out_token.data);
116 }
117 
118 static void rsi_free_rcu(struct rcu_head *head)
119 {
120 	struct rsi *rsii = container_of(head, struct rsi, rcu_head);
121 
122 	rsi_free(rsii);
123 	kfree(rsii);
124 }
125 
126 static void rsi_put(struct kref *ref)
127 {
128 	struct rsi *rsii = container_of(ref, struct rsi, h.ref);
129 
130 	call_rcu(&rsii->rcu_head, rsi_free_rcu);
131 }
132 
133 static inline int rsi_hash(struct rsi *item)
134 {
135 	return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
136 	     ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
137 }
138 
139 static int rsi_match(struct cache_head *a, struct cache_head *b)
140 {
141 	struct rsi *item = container_of(a, struct rsi, h);
142 	struct rsi *tmp = container_of(b, struct rsi, h);
143 	return netobj_equal(&item->in_handle, &tmp->in_handle) &&
144 	       netobj_equal(&item->in_token, &tmp->in_token);
145 }
146 
147 static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
148 {
149 	dst->len = len;
150 	dst->data = (len ? kmemdup(src, len, GFP_KERNEL) : NULL);
151 	if (len && !dst->data)
152 		return -ENOMEM;
153 	return 0;
154 }
155 
156 static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
157 {
158 	return dup_to_netobj(dst, src->data, src->len);
159 }
160 
161 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
162 {
163 	struct rsi *new = container_of(cnew, struct rsi, h);
164 	struct rsi *item = container_of(citem, struct rsi, h);
165 
166 	new->out_handle.data = NULL;
167 	new->out_handle.len = 0;
168 	new->out_token.data = NULL;
169 	new->out_token.len = 0;
170 	new->in_handle.len = item->in_handle.len;
171 	item->in_handle.len = 0;
172 	new->in_token.len = item->in_token.len;
173 	item->in_token.len = 0;
174 	new->in_handle.data = item->in_handle.data;
175 	item->in_handle.data = NULL;
176 	new->in_token.data = item->in_token.data;
177 	item->in_token.data = NULL;
178 }
179 
180 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
181 {
182 	struct rsi *new = container_of(cnew, struct rsi, h);
183 	struct rsi *item = container_of(citem, struct rsi, h);
184 
185 	BUG_ON(new->out_handle.data || new->out_token.data);
186 	new->out_handle.len = item->out_handle.len;
187 	item->out_handle.len = 0;
188 	new->out_token.len = item->out_token.len;
189 	item->out_token.len = 0;
190 	new->out_handle.data = item->out_handle.data;
191 	item->out_handle.data = NULL;
192 	new->out_token.data = item->out_token.data;
193 	item->out_token.data = NULL;
194 
195 	new->major_status = item->major_status;
196 	new->minor_status = item->minor_status;
197 }
198 
199 static struct cache_head *rsi_alloc(void)
200 {
201 	struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);
202 	if (rsii)
203 		return &rsii->h;
204 	else
205 		return NULL;
206 }
207 
208 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
209 {
210 	return sunrpc_cache_pipe_upcall_timeout(cd, h);
211 }
212 
213 static void rsi_request(struct cache_detail *cd,
214 		       struct cache_head *h,
215 		       char **bpp, int *blen)
216 {
217 	struct rsi *rsii = container_of(h, struct rsi, h);
218 
219 	qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
220 	qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
221 	(*bpp)[-1] = '\n';
222 	WARN_ONCE(*blen < 0,
223 		  "RPCSEC/GSS credential too large - please use gssproxy\n");
224 }
225 
226 static int rsi_parse(struct cache_detail *cd,
227 		    char *mesg, int mlen)
228 {
229 	/* context token expiry major minor context token */
230 	char *buf = mesg;
231 	char *ep;
232 	int len;
233 	struct rsi rsii, *rsip = NULL;
234 	time64_t expiry;
235 	int status = -EINVAL;
236 
237 	memset(&rsii, 0, sizeof(rsii));
238 	/* handle */
239 	len = qword_get(&mesg, buf, mlen);
240 	if (len < 0)
241 		goto out;
242 	status = -ENOMEM;
243 	if (dup_to_netobj(&rsii.in_handle, buf, len))
244 		goto out;
245 
246 	/* token */
247 	len = qword_get(&mesg, buf, mlen);
248 	status = -EINVAL;
249 	if (len < 0)
250 		goto out;
251 	status = -ENOMEM;
252 	if (dup_to_netobj(&rsii.in_token, buf, len))
253 		goto out;
254 
255 	rsip = rsi_lookup(cd, &rsii);
256 	if (!rsip)
257 		goto out;
258 
259 	rsii.h.flags = 0;
260 	/* expiry */
261 	expiry = get_expiry(&mesg);
262 	status = -EINVAL;
263 	if (expiry == 0)
264 		goto out;
265 
266 	/* major/minor */
267 	len = qword_get(&mesg, buf, mlen);
268 	if (len <= 0)
269 		goto out;
270 	rsii.major_status = simple_strtoul(buf, &ep, 10);
271 	if (*ep)
272 		goto out;
273 	len = qword_get(&mesg, buf, mlen);
274 	if (len <= 0)
275 		goto out;
276 	rsii.minor_status = simple_strtoul(buf, &ep, 10);
277 	if (*ep)
278 		goto out;
279 
280 	/* out_handle */
281 	len = qword_get(&mesg, buf, mlen);
282 	if (len < 0)
283 		goto out;
284 	status = -ENOMEM;
285 	if (dup_to_netobj(&rsii.out_handle, buf, len))
286 		goto out;
287 
288 	/* out_token */
289 	len = qword_get(&mesg, buf, mlen);
290 	status = -EINVAL;
291 	if (len < 0)
292 		goto out;
293 	status = -ENOMEM;
294 	if (dup_to_netobj(&rsii.out_token, buf, len))
295 		goto out;
296 	rsii.h.expiry_time = expiry;
297 	rsip = rsi_update(cd, &rsii, rsip);
298 	status = 0;
299 out:
300 	rsi_free(&rsii);
301 	if (rsip)
302 		cache_put(&rsip->h, cd);
303 	else
304 		status = -ENOMEM;
305 	return status;
306 }
307 
308 static const struct cache_detail rsi_cache_template = {
309 	.owner		= THIS_MODULE,
310 	.hash_size	= RSI_HASHMAX,
311 	.name           = "auth.rpcsec.init",
312 	.cache_put      = rsi_put,
313 	.cache_upcall	= rsi_upcall,
314 	.cache_request  = rsi_request,
315 	.cache_parse    = rsi_parse,
316 	.match		= rsi_match,
317 	.init		= rsi_init,
318 	.update		= update_rsi,
319 	.alloc		= rsi_alloc,
320 };
321 
322 static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
323 {
324 	struct cache_head *ch;
325 	int hash = rsi_hash(item);
326 
327 	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
328 	if (ch)
329 		return container_of(ch, struct rsi, h);
330 	else
331 		return NULL;
332 }
333 
334 static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old)
335 {
336 	struct cache_head *ch;
337 	int hash = rsi_hash(new);
338 
339 	ch = sunrpc_cache_update(cd, &new->h,
340 				 &old->h, hash);
341 	if (ch)
342 		return container_of(ch, struct rsi, h);
343 	else
344 		return NULL;
345 }
346 
347 
348 /*
349  * The rpcsec_context cache is used to store a context that is
350  * used in data exchange.
351  * The key is a context handle. The content is:
352  *  uid, gidlist, mechanism, service-set, mech-specific-data
353  */
354 
355 #define	RSC_HASHBITS	10
356 #define	RSC_HASHMAX	(1<<RSC_HASHBITS)
357 
358 #define GSS_SEQ_WIN	128
359 
360 struct gss_svc_seq_data {
361 	/* highest seq number seen so far: */
362 	u32			sd_max;
363 	/* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
364 	 * sd_win is nonzero iff sequence number i has been seen already: */
365 	unsigned long		sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
366 	spinlock_t		sd_lock;
367 };
368 
369 struct rsc {
370 	struct cache_head	h;
371 	struct xdr_netobj	handle;
372 	struct svc_cred		cred;
373 	struct gss_svc_seq_data	seqdata;
374 	struct gss_ctx		*mechctx;
375 	struct rcu_head		rcu_head;
376 };
377 
378 static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old);
379 static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item);
380 
381 static void rsc_free(struct rsc *rsci)
382 {
383 	kfree(rsci->handle.data);
384 	if (rsci->mechctx)
385 		gss_delete_sec_context(&rsci->mechctx);
386 	free_svc_cred(&rsci->cred);
387 }
388 
389 static void rsc_free_rcu(struct rcu_head *head)
390 {
391 	struct rsc *rsci = container_of(head, struct rsc, rcu_head);
392 
393 	kfree(rsci->handle.data);
394 	kfree(rsci);
395 }
396 
397 static void rsc_put(struct kref *ref)
398 {
399 	struct rsc *rsci = container_of(ref, struct rsc, h.ref);
400 
401 	if (rsci->mechctx)
402 		gss_delete_sec_context(&rsci->mechctx);
403 	free_svc_cred(&rsci->cred);
404 	call_rcu(&rsci->rcu_head, rsc_free_rcu);
405 }
406 
407 static inline int
408 rsc_hash(struct rsc *rsci)
409 {
410 	return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
411 }
412 
413 static int
414 rsc_match(struct cache_head *a, struct cache_head *b)
415 {
416 	struct rsc *new = container_of(a, struct rsc, h);
417 	struct rsc *tmp = container_of(b, struct rsc, h);
418 
419 	return netobj_equal(&new->handle, &tmp->handle);
420 }
421 
422 static void
423 rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
424 {
425 	struct rsc *new = container_of(cnew, struct rsc, h);
426 	struct rsc *tmp = container_of(ctmp, struct rsc, h);
427 
428 	new->handle.len = tmp->handle.len;
429 	tmp->handle.len = 0;
430 	new->handle.data = tmp->handle.data;
431 	tmp->handle.data = NULL;
432 	new->mechctx = NULL;
433 	init_svc_cred(&new->cred);
434 }
435 
436 static void
437 update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
438 {
439 	struct rsc *new = container_of(cnew, struct rsc, h);
440 	struct rsc *tmp = container_of(ctmp, struct rsc, h);
441 
442 	new->mechctx = tmp->mechctx;
443 	tmp->mechctx = NULL;
444 	memset(&new->seqdata, 0, sizeof(new->seqdata));
445 	spin_lock_init(&new->seqdata.sd_lock);
446 	new->cred = tmp->cred;
447 	init_svc_cred(&tmp->cred);
448 }
449 
450 static struct cache_head *
451 rsc_alloc(void)
452 {
453 	struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
454 	if (rsci)
455 		return &rsci->h;
456 	else
457 		return NULL;
458 }
459 
460 static int rsc_upcall(struct cache_detail *cd, struct cache_head *h)
461 {
462 	return -EINVAL;
463 }
464 
465 static int rsc_parse(struct cache_detail *cd,
466 		     char *mesg, int mlen)
467 {
468 	/* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
469 	char *buf = mesg;
470 	int id;
471 	int len, rv;
472 	struct rsc rsci, *rscp = NULL;
473 	time64_t expiry;
474 	int status = -EINVAL;
475 	struct gss_api_mech *gm = NULL;
476 
477 	memset(&rsci, 0, sizeof(rsci));
478 	/* context handle */
479 	len = qword_get(&mesg, buf, mlen);
480 	if (len < 0) goto out;
481 	status = -ENOMEM;
482 	if (dup_to_netobj(&rsci.handle, buf, len))
483 		goto out;
484 
485 	rsci.h.flags = 0;
486 	/* expiry */
487 	expiry = get_expiry(&mesg);
488 	status = -EINVAL;
489 	if (expiry == 0)
490 		goto out;
491 
492 	rscp = rsc_lookup(cd, &rsci);
493 	if (!rscp)
494 		goto out;
495 
496 	/* uid, or NEGATIVE */
497 	rv = get_int(&mesg, &id);
498 	if (rv == -EINVAL)
499 		goto out;
500 	if (rv == -ENOENT)
501 		set_bit(CACHE_NEGATIVE, &rsci.h.flags);
502 	else {
503 		int N, i;
504 
505 		/*
506 		 * NOTE: we skip uid_valid()/gid_valid() checks here:
507 		 * instead, * -1 id's are later mapped to the
508 		 * (export-specific) anonymous id by nfsd_setuser.
509 		 *
510 		 * (But supplementary gid's get no such special
511 		 * treatment so are checked for validity here.)
512 		 */
513 		/* uid */
514 		rsci.cred.cr_uid = make_kuid(current_user_ns(), id);
515 
516 		/* gid */
517 		if (get_int(&mesg, &id))
518 			goto out;
519 		rsci.cred.cr_gid = make_kgid(current_user_ns(), id);
520 
521 		/* number of additional gid's */
522 		if (get_int(&mesg, &N))
523 			goto out;
524 		if (N < 0 || N > NGROUPS_MAX)
525 			goto out;
526 		status = -ENOMEM;
527 		rsci.cred.cr_group_info = groups_alloc(N);
528 		if (rsci.cred.cr_group_info == NULL)
529 			goto out;
530 
531 		/* gid's */
532 		status = -EINVAL;
533 		for (i=0; i<N; i++) {
534 			kgid_t kgid;
535 			if (get_int(&mesg, &id))
536 				goto out;
537 			kgid = make_kgid(current_user_ns(), id);
538 			if (!gid_valid(kgid))
539 				goto out;
540 			rsci.cred.cr_group_info->gid[i] = kgid;
541 		}
542 		groups_sort(rsci.cred.cr_group_info);
543 
544 		/* mech name */
545 		len = qword_get(&mesg, buf, mlen);
546 		if (len < 0)
547 			goto out;
548 		gm = rsci.cred.cr_gss_mech = gss_mech_get_by_name(buf);
549 		status = -EOPNOTSUPP;
550 		if (!gm)
551 			goto out;
552 
553 		status = -EINVAL;
554 		/* mech-specific data: */
555 		len = qword_get(&mesg, buf, mlen);
556 		if (len < 0)
557 			goto out;
558 		status = gss_import_sec_context(buf, len, gm, &rsci.mechctx,
559 						NULL, GFP_KERNEL);
560 		if (status)
561 			goto out;
562 
563 		/* get client name */
564 		len = qword_get(&mesg, buf, mlen);
565 		if (len > 0) {
566 			rsci.cred.cr_principal = kstrdup(buf, GFP_KERNEL);
567 			if (!rsci.cred.cr_principal) {
568 				status = -ENOMEM;
569 				goto out;
570 			}
571 		}
572 
573 	}
574 	rsci.h.expiry_time = expiry;
575 	rscp = rsc_update(cd, &rsci, rscp);
576 	status = 0;
577 out:
578 	rsc_free(&rsci);
579 	if (rscp)
580 		cache_put(&rscp->h, cd);
581 	else
582 		status = -ENOMEM;
583 	return status;
584 }
585 
586 static const struct cache_detail rsc_cache_template = {
587 	.owner		= THIS_MODULE,
588 	.hash_size	= RSC_HASHMAX,
589 	.name		= "auth.rpcsec.context",
590 	.cache_put	= rsc_put,
591 	.cache_upcall	= rsc_upcall,
592 	.cache_parse	= rsc_parse,
593 	.match		= rsc_match,
594 	.init		= rsc_init,
595 	.update		= update_rsc,
596 	.alloc		= rsc_alloc,
597 };
598 
599 static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item)
600 {
601 	struct cache_head *ch;
602 	int hash = rsc_hash(item);
603 
604 	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
605 	if (ch)
606 		return container_of(ch, struct rsc, h);
607 	else
608 		return NULL;
609 }
610 
611 static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old)
612 {
613 	struct cache_head *ch;
614 	int hash = rsc_hash(new);
615 
616 	ch = sunrpc_cache_update(cd, &new->h,
617 				 &old->h, hash);
618 	if (ch)
619 		return container_of(ch, struct rsc, h);
620 	else
621 		return NULL;
622 }
623 
624 
625 static struct rsc *
626 gss_svc_searchbyctx(struct cache_detail *cd, struct xdr_netobj *handle)
627 {
628 	struct rsc rsci;
629 	struct rsc *found;
630 
631 	memset(&rsci, 0, sizeof(rsci));
632 	if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
633 		return NULL;
634 	found = rsc_lookup(cd, &rsci);
635 	rsc_free(&rsci);
636 	if (!found)
637 		return NULL;
638 	if (cache_check(cd, &found->h, NULL))
639 		return NULL;
640 	return found;
641 }
642 
643 /**
644  * gss_check_seq_num - GSS sequence number window check
645  * @rqstp: RPC Call to use when reporting errors
646  * @rsci: cached GSS context state (updated on return)
647  * @seq_num: sequence number to check
648  *
649  * Implements sequence number algorithm as specified in
650  * RFC 2203, Section 5.3.3.1. "Context Management".
651  *
652  * Return values:
653  *   %true: @rqstp's GSS sequence number is inside the window
654  *   %false: @rqstp's GSS sequence number is outside the window
655  */
656 static bool gss_check_seq_num(const struct svc_rqst *rqstp, struct rsc *rsci,
657 			      u32 seq_num)
658 {
659 	struct gss_svc_seq_data *sd = &rsci->seqdata;
660 	bool result = false;
661 
662 	spin_lock(&sd->sd_lock);
663 	if (seq_num > sd->sd_max) {
664 		if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
665 			memset(sd->sd_win, 0, sizeof(sd->sd_win));
666 			sd->sd_max = seq_num;
667 		} else while (sd->sd_max < seq_num) {
668 			sd->sd_max++;
669 			__clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
670 		}
671 		__set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
672 		goto ok;
673 	} else if (seq_num + GSS_SEQ_WIN <= sd->sd_max) {
674 		goto toolow;
675 	}
676 	if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
677 		goto alreadyseen;
678 
679 ok:
680 	result = true;
681 out:
682 	spin_unlock(&sd->sd_lock);
683 	return result;
684 
685 toolow:
686 	trace_rpcgss_svc_seqno_low(rqstp, seq_num,
687 				   sd->sd_max - GSS_SEQ_WIN,
688 				   sd->sd_max);
689 	goto out;
690 alreadyseen:
691 	trace_rpcgss_svc_seqno_seen(rqstp, seq_num);
692 	goto out;
693 }
694 
695 static inline u32 round_up_to_quad(u32 i)
696 {
697 	return (i + 3 ) & ~3;
698 }
699 
700 static inline int
701 svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
702 {
703 	u8 *p;
704 
705 	if (resv->iov_len + 4 > PAGE_SIZE)
706 		return -1;
707 	svc_putnl(resv, o->len);
708 	p = resv->iov_base + resv->iov_len;
709 	resv->iov_len += round_up_to_quad(o->len);
710 	if (resv->iov_len > PAGE_SIZE)
711 		return -1;
712 	memcpy(p, o->data, o->len);
713 	memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);
714 	return 0;
715 }
716 
717 /*
718  * Decode and verify a Call's verifier field. For RPC_AUTH_GSS Calls,
719  * the body of this field contains a variable length checksum.
720  *
721  * GSS-specific auth_stat values are mandated by RFC 2203 Section
722  * 5.3.3.3.
723  */
724 static int
725 svcauth_gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
726 			  __be32 *rpcstart, struct rpc_gss_wire_cred *gc)
727 {
728 	struct xdr_stream	*xdr = &rqstp->rq_arg_stream;
729 	struct gss_ctx		*ctx_id = rsci->mechctx;
730 	u32			flavor, maj_stat;
731 	struct xdr_buf		rpchdr;
732 	struct xdr_netobj	checksum;
733 	struct kvec		iov;
734 
735 	/*
736 	 * Compute the checksum of the incoming Call from the
737 	 * XID field to credential field:
738 	 */
739 	iov.iov_base = rpcstart;
740 	iov.iov_len = (u8 *)xdr->p - (u8 *)rpcstart;
741 	xdr_buf_from_iov(&iov, &rpchdr);
742 
743 	/* Call's verf field: */
744 	if (xdr_stream_decode_opaque_auth(xdr, &flavor,
745 					  (void **)&checksum.data,
746 					  &checksum.len) < 0) {
747 		rqstp->rq_auth_stat = rpc_autherr_badverf;
748 		return SVC_DENIED;
749 	}
750 	if (flavor != RPC_AUTH_GSS) {
751 		rqstp->rq_auth_stat = rpc_autherr_badverf;
752 		return SVC_DENIED;
753 	}
754 
755 	if (rqstp->rq_deferred)
756 		return SVC_OK;
757 	maj_stat = gss_verify_mic(ctx_id, &rpchdr, &checksum);
758 	if (maj_stat != GSS_S_COMPLETE) {
759 		trace_rpcgss_svc_mic(rqstp, maj_stat);
760 		rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
761 		return SVC_DENIED;
762 	}
763 
764 	if (gc->gc_seq > MAXSEQ) {
765 		trace_rpcgss_svc_seqno_large(rqstp, gc->gc_seq);
766 		rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
767 		return SVC_DENIED;
768 	}
769 	if (!gss_check_seq_num(rqstp, rsci, gc->gc_seq))
770 		return SVC_DROP;
771 	return SVC_OK;
772 }
773 
774 static int
775 gss_write_null_verf(struct svc_rqst *rqstp)
776 {
777 	__be32     *p;
778 
779 	svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);
780 	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
781 	/* don't really need to check if head->iov_len > PAGE_SIZE ... */
782 	*p++ = 0;
783 	if (!xdr_ressize_check(rqstp, p))
784 		return -1;
785 	return 0;
786 }
787 
788 static int
789 gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
790 {
791 	__be32			*xdr_seq;
792 	u32			maj_stat;
793 	struct xdr_buf		verf_data;
794 	struct xdr_netobj	mic;
795 	__be32			*p;
796 	struct kvec		iov;
797 	int err = -1;
798 
799 	svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);
800 	xdr_seq = kmalloc(4, GFP_KERNEL);
801 	if (!xdr_seq)
802 		return -ENOMEM;
803 	*xdr_seq = htonl(seq);
804 
805 	iov.iov_base = xdr_seq;
806 	iov.iov_len = 4;
807 	xdr_buf_from_iov(&iov, &verf_data);
808 	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
809 	mic.data = (u8 *)(p + 1);
810 	maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
811 	if (maj_stat != GSS_S_COMPLETE)
812 		goto out;
813 	*p++ = htonl(mic.len);
814 	memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
815 	p += XDR_QUADLEN(mic.len);
816 	if (!xdr_ressize_check(rqstp, p))
817 		goto out;
818 	err = 0;
819 out:
820 	kfree(xdr_seq);
821 	return err;
822 }
823 
824 struct gss_domain {
825 	struct auth_domain	h;
826 	u32			pseudoflavor;
827 };
828 
829 static struct auth_domain *
830 find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
831 {
832 	char *name;
833 
834 	name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
835 	if (!name)
836 		return NULL;
837 	return auth_domain_find(name);
838 }
839 
840 static struct auth_ops svcauthops_gss;
841 
842 u32 svcauth_gss_flavor(struct auth_domain *dom)
843 {
844 	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
845 
846 	return gd->pseudoflavor;
847 }
848 
849 EXPORT_SYMBOL_GPL(svcauth_gss_flavor);
850 
851 struct auth_domain *
852 svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
853 {
854 	struct gss_domain	*new;
855 	struct auth_domain	*test;
856 	int			stat = -ENOMEM;
857 
858 	new = kmalloc(sizeof(*new), GFP_KERNEL);
859 	if (!new)
860 		goto out;
861 	kref_init(&new->h.ref);
862 	new->h.name = kstrdup(name, GFP_KERNEL);
863 	if (!new->h.name)
864 		goto out_free_dom;
865 	new->h.flavour = &svcauthops_gss;
866 	new->pseudoflavor = pseudoflavor;
867 
868 	test = auth_domain_lookup(name, &new->h);
869 	if (test != &new->h) {
870 		pr_warn("svc: duplicate registration of gss pseudo flavour %s.\n",
871 			name);
872 		stat = -EADDRINUSE;
873 		auth_domain_put(test);
874 		goto out_free_name;
875 	}
876 	return test;
877 
878 out_free_name:
879 	kfree(new->h.name);
880 out_free_dom:
881 	kfree(new);
882 out:
883 	return ERR_PTR(stat);
884 }
885 EXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);
886 
887 /*
888  * RFC 2203, Section 5.3.2.2
889  *
890  *	struct rpc_gss_integ_data {
891  *		opaque databody_integ<>;
892  *		opaque checksum<>;
893  *	};
894  *
895  *	struct rpc_gss_data_t {
896  *		unsigned int seq_num;
897  *		proc_req_arg_t arg;
898  *	};
899  */
900 static noinline_for_stack int
901 svcauth_gss_unwrap_integ(struct svc_rqst *rqstp, u32 seq, struct gss_ctx *ctx)
902 {
903 	struct gss_svc_data *gsd = rqstp->rq_auth_data;
904 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
905 	u32 len, offset, seq_num, maj_stat;
906 	struct xdr_buf *buf = xdr->buf;
907 	struct xdr_buf databody_integ;
908 	struct xdr_netobj checksum;
909 
910 	/* NFS READ normally uses splice to send data in-place. However
911 	 * the data in cache can change after the reply's MIC is computed
912 	 * but before the RPC reply is sent. To prevent the client from
913 	 * rejecting the server-computed MIC in this somewhat rare case,
914 	 * do not use splice with the GSS integrity service.
915 	 */
916 	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
917 
918 	/* Did we already verify the signature on the original pass through? */
919 	if (rqstp->rq_deferred)
920 		return 0;
921 
922 	if (xdr_stream_decode_u32(xdr, &len) < 0)
923 		goto unwrap_failed;
924 	if (len & 3)
925 		goto unwrap_failed;
926 	offset = xdr_stream_pos(xdr);
927 	if (xdr_buf_subsegment(buf, &databody_integ, offset, len))
928 		goto unwrap_failed;
929 
930 	/*
931 	 * The xdr_stream now points to the @seq_num field. The next
932 	 * XDR data item is the @arg field, which contains the clear
933 	 * text RPC program payload. The checksum, which follows the
934 	 * @arg field, is located and decoded without updating the
935 	 * xdr_stream.
936 	 */
937 
938 	offset += len;
939 	if (xdr_decode_word(buf, offset, &checksum.len))
940 		goto unwrap_failed;
941 	if (checksum.len > sizeof(gsd->gsd_scratch))
942 		goto unwrap_failed;
943 	checksum.data = gsd->gsd_scratch;
944 	if (read_bytes_from_xdr_buf(buf, offset + XDR_UNIT, checksum.data,
945 				    checksum.len))
946 		goto unwrap_failed;
947 
948 	maj_stat = gss_verify_mic(ctx, &databody_integ, &checksum);
949 	if (maj_stat != GSS_S_COMPLETE)
950 		goto bad_mic;
951 
952 	/* The received seqno is protected by the checksum. */
953 	if (xdr_stream_decode_u32(xdr, &seq_num) < 0)
954 		goto unwrap_failed;
955 	if (seq_num != seq)
956 		goto bad_seqno;
957 
958 	xdr_truncate_decode(xdr, XDR_UNIT + checksum.len);
959 	return 0;
960 
961 unwrap_failed:
962 	trace_rpcgss_svc_unwrap_failed(rqstp);
963 	return -EINVAL;
964 bad_seqno:
965 	trace_rpcgss_svc_seqno_bad(rqstp, seq, seq_num);
966 	return -EINVAL;
967 bad_mic:
968 	trace_rpcgss_svc_mic(rqstp, maj_stat);
969 	return -EINVAL;
970 }
971 
972 /*
973  * RFC 2203, Section 5.3.2.3
974  *
975  *	struct rpc_gss_priv_data {
976  *		opaque databody_priv<>
977  *	};
978  *
979  *	struct rpc_gss_data_t {
980  *		unsigned int seq_num;
981  *		proc_req_arg_t arg;
982  *	};
983  */
984 static noinline_for_stack int
985 svcauth_gss_unwrap_priv(struct svc_rqst *rqstp, u32 seq, struct gss_ctx *ctx)
986 {
987 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
988 	u32 len, maj_stat, seq_num, offset;
989 	struct xdr_buf *buf = xdr->buf;
990 	unsigned int saved_len;
991 
992 	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
993 
994 	if (xdr_stream_decode_u32(xdr, &len) < 0)
995 		goto unwrap_failed;
996 	if (rqstp->rq_deferred) {
997 		/* Already decrypted last time through! The sequence number
998 		 * check at out_seq is unnecessary but harmless: */
999 		goto out_seq;
1000 	}
1001 	if (len > xdr_stream_remaining(xdr))
1002 		goto unwrap_failed;
1003 	offset = xdr_stream_pos(xdr);
1004 
1005 	saved_len = buf->len;
1006 	maj_stat = gss_unwrap(ctx, offset, offset + len, buf);
1007 	if (maj_stat != GSS_S_COMPLETE)
1008 		goto bad_unwrap;
1009 	xdr->nwords -= XDR_QUADLEN(saved_len - buf->len);
1010 
1011 out_seq:
1012 	/* gss_unwrap() decrypted the sequence number. */
1013 	if (xdr_stream_decode_u32(xdr, &seq_num) < 0)
1014 		goto unwrap_failed;
1015 	if (seq_num != seq)
1016 		goto bad_seqno;
1017 	return 0;
1018 
1019 unwrap_failed:
1020 	trace_rpcgss_svc_unwrap_failed(rqstp);
1021 	return -EINVAL;
1022 bad_seqno:
1023 	trace_rpcgss_svc_seqno_bad(rqstp, seq, seq_num);
1024 	return -EINVAL;
1025 bad_unwrap:
1026 	trace_rpcgss_svc_unwrap(rqstp, maj_stat);
1027 	return -EINVAL;
1028 }
1029 
1030 static int
1031 svcauth_gss_set_client(struct svc_rqst *rqstp)
1032 {
1033 	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
1034 	struct rsc *rsci = svcdata->rsci;
1035 	struct rpc_gss_wire_cred *gc = &svcdata->clcred;
1036 	int stat;
1037 
1038 	rqstp->rq_auth_stat = rpc_autherr_badcred;
1039 
1040 	/*
1041 	 * A gss export can be specified either by:
1042 	 * 	export	*(sec=krb5,rw)
1043 	 * or by
1044 	 * 	export gss/krb5(rw)
1045 	 * The latter is deprecated; but for backwards compatibility reasons
1046 	 * the nfsd code will still fall back on trying it if the former
1047 	 * doesn't work; so we try to make both available to nfsd, below.
1048 	 */
1049 	rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
1050 	if (rqstp->rq_gssclient == NULL)
1051 		return SVC_DENIED;
1052 	stat = svcauth_unix_set_client(rqstp);
1053 	if (stat == SVC_DROP || stat == SVC_CLOSE)
1054 		return stat;
1055 
1056 	rqstp->rq_auth_stat = rpc_auth_ok;
1057 	return SVC_OK;
1058 }
1059 
1060 static inline int
1061 gss_write_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
1062 		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 		return gss_write_null_verf(rqstp);
1073 	}
1074 	rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
1075 	cache_put(&rsci->h, cd);
1076 	return rc;
1077 }
1078 
1079 static void gss_free_in_token_pages(struct gssp_in_token *in_token)
1080 {
1081 	u32 inlen;
1082 	int i;
1083 
1084 	i = 0;
1085 	inlen = in_token->page_len;
1086 	while (inlen) {
1087 		if (in_token->pages[i])
1088 			put_page(in_token->pages[i]);
1089 		inlen -= inlen > PAGE_SIZE ? PAGE_SIZE : inlen;
1090 	}
1091 
1092 	kfree(in_token->pages);
1093 	in_token->pages = NULL;
1094 }
1095 
1096 static int gss_read_proxy_verf(struct svc_rqst *rqstp,
1097 			       struct rpc_gss_wire_cred *gc,
1098 			       struct xdr_netobj *in_handle,
1099 			       struct gssp_in_token *in_token)
1100 {
1101 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1102 	unsigned int length, pgto_offs, pgfrom_offs;
1103 	int pages, i, pgto, pgfrom;
1104 	size_t to_offs, from_offs;
1105 	u32 inlen;
1106 
1107 	if (dup_netobj(in_handle, &gc->gc_ctx))
1108 		return SVC_CLOSE;
1109 
1110 	/*
1111 	 *  RFC 2203 Section 5.2.2
1112 	 *
1113 	 *	struct rpc_gss_init_arg {
1114 	 *		opaque gss_token<>;
1115 	 *	};
1116 	 */
1117 	if (xdr_stream_decode_u32(xdr, &inlen) < 0)
1118 		goto out_denied_free;
1119 	if (inlen > xdr_stream_remaining(xdr))
1120 		goto out_denied_free;
1121 
1122 	pages = DIV_ROUND_UP(inlen, PAGE_SIZE);
1123 	in_token->pages = kcalloc(pages, sizeof(struct page *), GFP_KERNEL);
1124 	if (!in_token->pages)
1125 		goto out_denied_free;
1126 	in_token->page_base = 0;
1127 	in_token->page_len = inlen;
1128 	for (i = 0; i < pages; i++) {
1129 		in_token->pages[i] = alloc_page(GFP_KERNEL);
1130 		if (!in_token->pages[i]) {
1131 			gss_free_in_token_pages(in_token);
1132 			goto out_denied_free;
1133 		}
1134 	}
1135 
1136 	length = min_t(unsigned int, inlen, (char *)xdr->end - (char *)xdr->p);
1137 	memcpy(page_address(in_token->pages[0]), xdr->p, length);
1138 	inlen -= length;
1139 
1140 	to_offs = length;
1141 	from_offs = rqstp->rq_arg.page_base;
1142 	while (inlen) {
1143 		pgto = to_offs >> PAGE_SHIFT;
1144 		pgfrom = from_offs >> PAGE_SHIFT;
1145 		pgto_offs = to_offs & ~PAGE_MASK;
1146 		pgfrom_offs = from_offs & ~PAGE_MASK;
1147 
1148 		length = min_t(unsigned int, inlen,
1149 			 min_t(unsigned int, PAGE_SIZE - pgto_offs,
1150 			       PAGE_SIZE - pgfrom_offs));
1151 		memcpy(page_address(in_token->pages[pgto]) + pgto_offs,
1152 		       page_address(rqstp->rq_arg.pages[pgfrom]) + pgfrom_offs,
1153 		       length);
1154 
1155 		to_offs += length;
1156 		from_offs += length;
1157 		inlen -= length;
1158 	}
1159 	return 0;
1160 
1161 out_denied_free:
1162 	kfree(in_handle->data);
1163 	return SVC_DENIED;
1164 }
1165 
1166 static inline int
1167 gss_write_resv(struct kvec *resv, size_t size_limit,
1168 	       struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
1169 	       int major_status, int minor_status)
1170 {
1171 	if (resv->iov_len + 4 > size_limit)
1172 		return -1;
1173 	svc_putnl(resv, RPC_SUCCESS);
1174 	if (svc_safe_putnetobj(resv, out_handle))
1175 		return -1;
1176 	if (resv->iov_len + 3 * 4 > size_limit)
1177 		return -1;
1178 	svc_putnl(resv, major_status);
1179 	svc_putnl(resv, minor_status);
1180 	svc_putnl(resv, GSS_SEQ_WIN);
1181 	if (svc_safe_putnetobj(resv, out_token))
1182 		return -1;
1183 	return 0;
1184 }
1185 
1186 /*
1187  * Having read the cred already and found we're in the context
1188  * initiation case, read the verifier and initiate (or check the results
1189  * of) upcalls to userspace for help with context initiation.  If
1190  * the upcall results are available, write the verifier and result.
1191  * Otherwise, drop the request pending an answer to the upcall.
1192  */
1193 static int
1194 svcauth_gss_legacy_init(struct svc_rqst *rqstp,
1195 			struct rpc_gss_wire_cred *gc)
1196 {
1197 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1198 	struct kvec *resv = &rqstp->rq_res.head[0];
1199 	struct rsi *rsip, rsikey;
1200 	__be32 *p;
1201 	u32 len;
1202 	int ret;
1203 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1204 
1205 	memset(&rsikey, 0, sizeof(rsikey));
1206 	if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))
1207 		return SVC_CLOSE;
1208 
1209 	/*
1210 	 *  RFC 2203 Section 5.2.2
1211 	 *
1212 	 *	struct rpc_gss_init_arg {
1213 	 *		opaque gss_token<>;
1214 	 *	};
1215 	 */
1216 	if (xdr_stream_decode_u32(xdr, &len) < 0) {
1217 		kfree(rsikey.in_handle.data);
1218 		return SVC_DENIED;
1219 	}
1220 	p = xdr_inline_decode(xdr, len);
1221 	if (!p) {
1222 		kfree(rsikey.in_handle.data);
1223 		return SVC_DENIED;
1224 	}
1225 	rsikey.in_token.data = kmalloc(len, GFP_KERNEL);
1226 	if (ZERO_OR_NULL_PTR(rsikey.in_token.data)) {
1227 		kfree(rsikey.in_handle.data);
1228 		return SVC_CLOSE;
1229 	}
1230 	memcpy(rsikey.in_token.data, p, len);
1231 	rsikey.in_token.len = len;
1232 
1233 	/* Perform upcall, or find upcall result: */
1234 	rsip = rsi_lookup(sn->rsi_cache, &rsikey);
1235 	rsi_free(&rsikey);
1236 	if (!rsip)
1237 		return SVC_CLOSE;
1238 	if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
1239 		/* No upcall result: */
1240 		return SVC_CLOSE;
1241 
1242 	ret = SVC_CLOSE;
1243 	/* Got an answer to the upcall; use it: */
1244 	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1245 				&rsip->out_handle, &rsip->major_status))
1246 		goto out;
1247 	if (gss_write_resv(resv, PAGE_SIZE,
1248 			   &rsip->out_handle, &rsip->out_token,
1249 			   rsip->major_status, rsip->minor_status))
1250 		goto out;
1251 
1252 	ret = SVC_COMPLETE;
1253 	svcxdr_init_encode(rqstp);
1254 out:
1255 	cache_put(&rsip->h, sn->rsi_cache);
1256 	return ret;
1257 }
1258 
1259 static int gss_proxy_save_rsc(struct cache_detail *cd,
1260 				struct gssp_upcall_data *ud,
1261 				uint64_t *handle)
1262 {
1263 	struct rsc rsci, *rscp = NULL;
1264 	static atomic64_t ctxhctr;
1265 	long long ctxh;
1266 	struct gss_api_mech *gm = NULL;
1267 	time64_t expiry;
1268 	int status;
1269 
1270 	memset(&rsci, 0, sizeof(rsci));
1271 	/* context handle */
1272 	status = -ENOMEM;
1273 	/* the handle needs to be just a unique id,
1274 	 * use a static counter */
1275 	ctxh = atomic64_inc_return(&ctxhctr);
1276 
1277 	/* make a copy for the caller */
1278 	*handle = ctxh;
1279 
1280 	/* make a copy for the rsc cache */
1281 	if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
1282 		goto out;
1283 	rscp = rsc_lookup(cd, &rsci);
1284 	if (!rscp)
1285 		goto out;
1286 
1287 	/* creds */
1288 	if (!ud->found_creds) {
1289 		/* userspace seem buggy, we should always get at least a
1290 		 * mapping to nobody */
1291 		goto out;
1292 	} else {
1293 		struct timespec64 boot;
1294 
1295 		/* steal creds */
1296 		rsci.cred = ud->creds;
1297 		memset(&ud->creds, 0, sizeof(struct svc_cred));
1298 
1299 		status = -EOPNOTSUPP;
1300 		/* get mech handle from OID */
1301 		gm = gss_mech_get_by_OID(&ud->mech_oid);
1302 		if (!gm)
1303 			goto out;
1304 		rsci.cred.cr_gss_mech = gm;
1305 
1306 		status = -EINVAL;
1307 		/* mech-specific data: */
1308 		status = gss_import_sec_context(ud->out_handle.data,
1309 						ud->out_handle.len,
1310 						gm, &rsci.mechctx,
1311 						&expiry, GFP_KERNEL);
1312 		if (status)
1313 			goto out;
1314 
1315 		getboottime64(&boot);
1316 		expiry -= boot.tv_sec;
1317 	}
1318 
1319 	rsci.h.expiry_time = expiry;
1320 	rscp = rsc_update(cd, &rsci, rscp);
1321 	status = 0;
1322 out:
1323 	rsc_free(&rsci);
1324 	if (rscp)
1325 		cache_put(&rscp->h, cd);
1326 	else
1327 		status = -ENOMEM;
1328 	return status;
1329 }
1330 
1331 static int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
1332 				  struct rpc_gss_wire_cred *gc)
1333 {
1334 	struct kvec *resv = &rqstp->rq_res.head[0];
1335 	struct xdr_netobj cli_handle;
1336 	struct gssp_upcall_data ud;
1337 	uint64_t handle;
1338 	int status;
1339 	int ret;
1340 	struct net *net = SVC_NET(rqstp);
1341 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1342 
1343 	memset(&ud, 0, sizeof(ud));
1344 	ret = gss_read_proxy_verf(rqstp, gc, &ud.in_handle, &ud.in_token);
1345 	if (ret)
1346 		return ret;
1347 
1348 	ret = SVC_CLOSE;
1349 
1350 	/* Perform synchronous upcall to gss-proxy */
1351 	status = gssp_accept_sec_context_upcall(net, &ud);
1352 	if (status)
1353 		goto out;
1354 
1355 	trace_rpcgss_svc_accept_upcall(rqstp, ud.major_status, ud.minor_status);
1356 
1357 	switch (ud.major_status) {
1358 	case GSS_S_CONTINUE_NEEDED:
1359 		cli_handle = ud.out_handle;
1360 		break;
1361 	case GSS_S_COMPLETE:
1362 		status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
1363 		if (status)
1364 			goto out;
1365 		cli_handle.data = (u8 *)&handle;
1366 		cli_handle.len = sizeof(handle);
1367 		break;
1368 	default:
1369 		goto out;
1370 	}
1371 
1372 	/* Got an answer to the upcall; use it: */
1373 	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1374 				&cli_handle, &ud.major_status))
1375 		goto out;
1376 	if (gss_write_resv(resv, PAGE_SIZE,
1377 			   &cli_handle, &ud.out_token,
1378 			   ud.major_status, ud.minor_status))
1379 		goto out;
1380 
1381 	ret = SVC_COMPLETE;
1382 	svcxdr_init_encode(rqstp);
1383 out:
1384 	gss_free_in_token_pages(&ud.in_token);
1385 	gssp_free_upcall_data(&ud);
1386 	return ret;
1387 }
1388 
1389 /*
1390  * Try to set the sn->use_gss_proxy variable to a new value. We only allow
1391  * it to be changed if it's currently undefined (-1). If it's any other value
1392  * then return -EBUSY unless the type wouldn't have changed anyway.
1393  */
1394 static int set_gss_proxy(struct net *net, int type)
1395 {
1396 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1397 	int ret;
1398 
1399 	WARN_ON_ONCE(type != 0 && type != 1);
1400 	ret = cmpxchg(&sn->use_gss_proxy, -1, type);
1401 	if (ret != -1 && ret != type)
1402 		return -EBUSY;
1403 	return 0;
1404 }
1405 
1406 static bool use_gss_proxy(struct net *net)
1407 {
1408 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1409 
1410 	/* If use_gss_proxy is still undefined, then try to disable it */
1411 	if (sn->use_gss_proxy == -1)
1412 		set_gss_proxy(net, 0);
1413 	return sn->use_gss_proxy;
1414 }
1415 
1416 static noinline_for_stack int
1417 svcauth_gss_proc_init(struct svc_rqst *rqstp, struct rpc_gss_wire_cred *gc)
1418 {
1419 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1420 	u32 flavor, len;
1421 	void *body;
1422 
1423 	/* Call's verf field: */
1424 	if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
1425 		return SVC_GARBAGE;
1426 	if (flavor != RPC_AUTH_NULL || len != 0) {
1427 		rqstp->rq_auth_stat = rpc_autherr_badverf;
1428 		return SVC_DENIED;
1429 	}
1430 
1431 	if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0) {
1432 		rqstp->rq_auth_stat = rpc_autherr_badcred;
1433 		return SVC_DENIED;
1434 	}
1435 
1436 	if (!use_gss_proxy(SVC_NET(rqstp)))
1437 		return svcauth_gss_legacy_init(rqstp, gc);
1438 	return svcauth_gss_proxy_init(rqstp, gc);
1439 }
1440 
1441 #ifdef CONFIG_PROC_FS
1442 
1443 static ssize_t write_gssp(struct file *file, const char __user *buf,
1444 			 size_t count, loff_t *ppos)
1445 {
1446 	struct net *net = pde_data(file_inode(file));
1447 	char tbuf[20];
1448 	unsigned long i;
1449 	int res;
1450 
1451 	if (*ppos || count > sizeof(tbuf)-1)
1452 		return -EINVAL;
1453 	if (copy_from_user(tbuf, buf, count))
1454 		return -EFAULT;
1455 
1456 	tbuf[count] = 0;
1457 	res = kstrtoul(tbuf, 0, &i);
1458 	if (res)
1459 		return res;
1460 	if (i != 1)
1461 		return -EINVAL;
1462 	res = set_gssp_clnt(net);
1463 	if (res)
1464 		return res;
1465 	res = set_gss_proxy(net, 1);
1466 	if (res)
1467 		return res;
1468 	return count;
1469 }
1470 
1471 static ssize_t read_gssp(struct file *file, char __user *buf,
1472 			 size_t count, loff_t *ppos)
1473 {
1474 	struct net *net = pde_data(file_inode(file));
1475 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1476 	unsigned long p = *ppos;
1477 	char tbuf[10];
1478 	size_t len;
1479 
1480 	snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
1481 	len = strlen(tbuf);
1482 	if (p >= len)
1483 		return 0;
1484 	len -= p;
1485 	if (len > count)
1486 		len = count;
1487 	if (copy_to_user(buf, (void *)(tbuf+p), len))
1488 		return -EFAULT;
1489 	*ppos += len;
1490 	return len;
1491 }
1492 
1493 static const struct proc_ops use_gss_proxy_proc_ops = {
1494 	.proc_open	= nonseekable_open,
1495 	.proc_write	= write_gssp,
1496 	.proc_read	= read_gssp,
1497 };
1498 
1499 static int create_use_gss_proxy_proc_entry(struct net *net)
1500 {
1501 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1502 	struct proc_dir_entry **p = &sn->use_gssp_proc;
1503 
1504 	sn->use_gss_proxy = -1;
1505 	*p = proc_create_data("use-gss-proxy", S_IFREG | 0600,
1506 			      sn->proc_net_rpc,
1507 			      &use_gss_proxy_proc_ops, net);
1508 	if (!*p)
1509 		return -ENOMEM;
1510 	init_gssp_clnt(sn);
1511 	return 0;
1512 }
1513 
1514 static void destroy_use_gss_proxy_proc_entry(struct net *net)
1515 {
1516 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1517 
1518 	if (sn->use_gssp_proc) {
1519 		remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
1520 		clear_gssp_clnt(sn);
1521 	}
1522 }
1523 #else /* CONFIG_PROC_FS */
1524 
1525 static int create_use_gss_proxy_proc_entry(struct net *net)
1526 {
1527 	return 0;
1528 }
1529 
1530 static void destroy_use_gss_proxy_proc_entry(struct net *net) {}
1531 
1532 #endif /* CONFIG_PROC_FS */
1533 
1534 /*
1535  * The Call's credential body should contain a struct rpc_gss_cred_t.
1536  *
1537  * RFC 2203 Section 5
1538  *
1539  *	struct rpc_gss_cred_t {
1540  *		union switch (unsigned int version) {
1541  *		case RPCSEC_GSS_VERS_1:
1542  *			struct {
1543  *				rpc_gss_proc_t gss_proc;
1544  *				unsigned int seq_num;
1545  *				rpc_gss_service_t service;
1546  *				opaque handle<>;
1547  *			} rpc_gss_cred_vers_1_t;
1548  *		}
1549  *	};
1550  */
1551 static bool
1552 svcauth_gss_decode_credbody(struct xdr_stream *xdr,
1553 			    struct rpc_gss_wire_cred *gc,
1554 			    __be32 **rpcstart)
1555 {
1556 	ssize_t handle_len;
1557 	u32 body_len;
1558 	__be32 *p;
1559 
1560 	p = xdr_inline_decode(xdr, XDR_UNIT);
1561 	if (!p)
1562 		return false;
1563 	/*
1564 	 * start of rpc packet is 7 u32's back from here:
1565 	 * xid direction rpcversion prog vers proc flavour
1566 	 */
1567 	*rpcstart = p - 7;
1568 	body_len = be32_to_cpup(p);
1569 	if (body_len > RPC_MAX_AUTH_SIZE)
1570 		return false;
1571 
1572 	/* struct rpc_gss_cred_t */
1573 	if (xdr_stream_decode_u32(xdr, &gc->gc_v) < 0)
1574 		return false;
1575 	if (xdr_stream_decode_u32(xdr, &gc->gc_proc) < 0)
1576 		return false;
1577 	if (xdr_stream_decode_u32(xdr, &gc->gc_seq) < 0)
1578 		return false;
1579 	if (xdr_stream_decode_u32(xdr, &gc->gc_svc) < 0)
1580 		return false;
1581 	handle_len = xdr_stream_decode_opaque_inline(xdr,
1582 						     (void **)&gc->gc_ctx.data,
1583 						     body_len);
1584 	if (handle_len < 0)
1585 		return false;
1586 	if (body_len != XDR_UNIT * 5 + xdr_align_size(handle_len))
1587 		return false;
1588 
1589 	gc->gc_ctx.len = handle_len;
1590 	return true;
1591 }
1592 
1593 /**
1594  * svcauth_gss_accept - Decode and validate incoming RPC_AUTH_GSS credential
1595  * @rqstp: RPC transaction
1596  *
1597  * Return values:
1598  *   %SVC_OK: Success
1599  *   %SVC_COMPLETE: GSS context lifetime event
1600  *   %SVC_DENIED: Credential or verifier is not valid
1601  *   %SVC_GARBAGE: Failed to decode credential or verifier
1602  *   %SVC_CLOSE: Temporary failure
1603  *
1604  * The rqstp->rq_auth_stat field is also set (see RFCs 2203 and 5531).
1605  */
1606 static int
1607 svcauth_gss_accept(struct svc_rqst *rqstp)
1608 {
1609 	struct kvec	*resv = &rqstp->rq_res.head[0];
1610 	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
1611 	__be32		*rpcstart;
1612 	struct rpc_gss_wire_cred *gc;
1613 	struct rsc	*rsci = NULL;
1614 	__be32		*reject_stat = resv->iov_base + resv->iov_len;
1615 	int		ret;
1616 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1617 
1618 	rqstp->rq_auth_stat = rpc_autherr_badcred;
1619 	if (!svcdata)
1620 		svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
1621 	if (!svcdata)
1622 		goto auth_err;
1623 	rqstp->rq_auth_data = svcdata;
1624 	svcdata->verf_start = NULL;
1625 	svcdata->rsci = NULL;
1626 	gc = &svcdata->clcred;
1627 
1628 	if (!svcauth_gss_decode_credbody(&rqstp->rq_arg_stream, gc, &rpcstart))
1629 		goto auth_err;
1630 	if (gc->gc_v != RPC_GSS_VERSION)
1631 		goto auth_err;
1632 
1633 	switch (gc->gc_proc) {
1634 	case RPC_GSS_PROC_INIT:
1635 	case RPC_GSS_PROC_CONTINUE_INIT:
1636 		if (rqstp->rq_proc != 0)
1637 			goto auth_err;
1638 		return svcauth_gss_proc_init(rqstp, gc);
1639 	case RPC_GSS_PROC_DESTROY:
1640 		if (rqstp->rq_proc != 0)
1641 			goto auth_err;
1642 		fallthrough;
1643 	case RPC_GSS_PROC_DATA:
1644 		rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
1645 		rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
1646 		if (!rsci)
1647 			goto auth_err;
1648 		switch (svcauth_gss_verify_header(rqstp, rsci, rpcstart, gc)) {
1649 		case SVC_OK:
1650 			break;
1651 		case SVC_DENIED:
1652 			goto auth_err;
1653 		case SVC_DROP:
1654 			goto drop;
1655 		}
1656 		break;
1657 	default:
1658 		if (rqstp->rq_proc != 0)
1659 			goto auth_err;
1660 		rqstp->rq_auth_stat = rpc_autherr_rejectedcred;
1661 		goto auth_err;
1662 	}
1663 
1664 	/* now act upon the command: */
1665 	switch (gc->gc_proc) {
1666 	case RPC_GSS_PROC_DESTROY:
1667 		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1668 			goto auth_err;
1669 		/* Delete the entry from the cache_list and call cache_put */
1670 		sunrpc_cache_unhash(sn->rsc_cache, &rsci->h);
1671 		if (resv->iov_len + 4 > PAGE_SIZE)
1672 			goto drop;
1673 		svc_putnl(resv, RPC_SUCCESS);
1674 		goto complete;
1675 	case RPC_GSS_PROC_DATA:
1676 		rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
1677 		svcdata->verf_start = resv->iov_base + resv->iov_len;
1678 		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1679 			goto auth_err;
1680 		rqstp->rq_cred = rsci->cred;
1681 		get_group_info(rsci->cred.cr_group_info);
1682 		rqstp->rq_auth_stat = rpc_autherr_badcred;
1683 		switch (gc->gc_svc) {
1684 		case RPC_GSS_SVC_NONE:
1685 			svcxdr_init_encode(rqstp);
1686 			break;
1687 		case RPC_GSS_SVC_INTEGRITY:
1688 			svcxdr_init_encode(rqstp);
1689 			/* placeholders for body length and seq. number: */
1690 			xdr_reserve_space(&rqstp->rq_res_stream, XDR_UNIT * 2);
1691 			if (svcauth_gss_unwrap_integ(rqstp, gc->gc_seq,
1692 						     rsci->mechctx))
1693 				goto garbage_args;
1694 			svcxdr_set_auth_slack(rqstp, RPC_MAX_AUTH_SIZE);
1695 			break;
1696 		case RPC_GSS_SVC_PRIVACY:
1697 			svcxdr_init_encode(rqstp);
1698 			/* placeholders for body length and seq. number: */
1699 			xdr_reserve_space(&rqstp->rq_res_stream, XDR_UNIT * 2);
1700 			if (svcauth_gss_unwrap_priv(rqstp, gc->gc_seq,
1701 						    rsci->mechctx))
1702 				goto garbage_args;
1703 			svcxdr_set_auth_slack(rqstp, RPC_MAX_AUTH_SIZE * 2);
1704 			break;
1705 		default:
1706 			goto auth_err;
1707 		}
1708 		svcdata->rsci = rsci;
1709 		cache_get(&rsci->h);
1710 		rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
1711 					rsci->mechctx->mech_type,
1712 					GSS_C_QOP_DEFAULT,
1713 					gc->gc_svc);
1714 		ret = SVC_OK;
1715 		trace_rpcgss_svc_authenticate(rqstp, gc);
1716 		goto out;
1717 	}
1718 garbage_args:
1719 	ret = SVC_GARBAGE;
1720 	goto out;
1721 auth_err:
1722 	/* Restore write pointer to its original value: */
1723 	xdr_ressize_check(rqstp, reject_stat);
1724 	ret = SVC_DENIED;
1725 	goto out;
1726 complete:
1727 	ret = SVC_COMPLETE;
1728 	goto out;
1729 drop:
1730 	ret = SVC_CLOSE;
1731 out:
1732 	if (rsci)
1733 		cache_put(&rsci->h, sn->rsc_cache);
1734 	return ret;
1735 }
1736 
1737 static __be32 *
1738 svcauth_gss_prepare_to_wrap(struct svc_rqst *rqstp, struct gss_svc_data *gsd)
1739 {
1740 	struct xdr_buf *resbuf = &rqstp->rq_res;
1741 	__be32 *p;
1742 	u32 verf_len;
1743 
1744 	p = gsd->verf_start;
1745 	gsd->verf_start = NULL;
1746 
1747 	/* AUTH_ERROR replies are not wrapped. */
1748 	if (rqstp->rq_auth_stat != rpc_auth_ok)
1749 		return NULL;
1750 
1751 	/* Skip the verifier: */
1752 	p += 1;
1753 	verf_len = ntohl(*p++);
1754 	p += XDR_QUADLEN(verf_len);
1755 	/* move accept_stat to right place: */
1756 	memcpy(p, p + 2, 4);
1757 	/* Also don't wrap if the accept stat is nonzero: */
1758 	if (*p != rpc_success) {
1759 		resbuf->head[0].iov_len -= 2 * 4;
1760 		return NULL;
1761 	}
1762 	p++;
1763 	return p;
1764 }
1765 
1766 /*
1767  * RFC 2203, Section 5.3.2.2
1768  *
1769  *	struct rpc_gss_integ_data {
1770  *		opaque databody_integ<>;
1771  *		opaque checksum<>;
1772  *	};
1773  *
1774  *	struct rpc_gss_data_t {
1775  *		unsigned int seq_num;
1776  *		proc_req_arg_t arg;
1777  *	};
1778  *
1779  * The RPC Reply message has already been XDR-encoded. rq_res_stream
1780  * is now positioned so that the checksum can be written just past
1781  * the RPC Reply message.
1782  */
1783 static int svcauth_gss_wrap_integ(struct svc_rqst *rqstp)
1784 {
1785 	struct gss_svc_data *gsd = rqstp->rq_auth_data;
1786 	struct xdr_stream *xdr = &rqstp->rq_res_stream;
1787 	struct rpc_gss_wire_cred *gc = &gsd->clcred;
1788 	struct xdr_buf *buf = xdr->buf;
1789 	struct xdr_buf databody_integ;
1790 	struct xdr_netobj checksum;
1791 	u32 offset, len, maj_stat;
1792 	__be32 *p;
1793 
1794 	p = svcauth_gss_prepare_to_wrap(rqstp, gsd);
1795 	if (p == NULL)
1796 		goto out;
1797 
1798 	offset = (u8 *)(p + 1) - (u8 *)buf->head[0].iov_base;
1799 	len = buf->len - offset;
1800 	if (xdr_buf_subsegment(buf, &databody_integ, offset, len))
1801 		goto wrap_failed;
1802 	/* Buffer space for these has already been reserved in
1803 	 * svcauth_gss_accept(). */
1804 	*p++ = cpu_to_be32(len);
1805 	*p = cpu_to_be32(gc->gc_seq);
1806 
1807 	checksum.data = gsd->gsd_scratch;
1808 	maj_stat = gss_get_mic(gsd->rsci->mechctx, &databody_integ, &checksum);
1809 	if (maj_stat != GSS_S_COMPLETE)
1810 		goto bad_mic;
1811 
1812 	if (xdr_stream_encode_opaque(xdr, checksum.data, checksum.len) < 0)
1813 		goto wrap_failed;
1814 	xdr_commit_encode(xdr);
1815 
1816 out:
1817 	return 0;
1818 
1819 bad_mic:
1820 	trace_rpcgss_svc_get_mic(rqstp, maj_stat);
1821 	return -EINVAL;
1822 wrap_failed:
1823 	trace_rpcgss_svc_wrap_failed(rqstp);
1824 	return -EINVAL;
1825 }
1826 
1827 /*
1828  * RFC 2203, Section 5.3.2.3
1829  *
1830  *	struct rpc_gss_priv_data {
1831  *		opaque databody_priv<>
1832  *	};
1833  *
1834  *	struct rpc_gss_data_t {
1835  *		unsigned int seq_num;
1836  *		proc_req_arg_t arg;
1837  *	};
1838  *
1839  * gss_wrap() expands the size of the RPC message payload in the
1840  * response buffer. The main purpose of svcauth_gss_wrap_priv()
1841  * is to ensure there is adequate space in the response buffer to
1842  * avoid overflow during the wrap.
1843  */
1844 static int svcauth_gss_wrap_priv(struct svc_rqst *rqstp)
1845 {
1846 	struct gss_svc_data *gsd = rqstp->rq_auth_data;
1847 	struct rpc_gss_wire_cred *gc = &gsd->clcred;
1848 	struct xdr_buf *buf = &rqstp->rq_res;
1849 	struct kvec *head = buf->head;
1850 	struct kvec *tail = buf->tail;
1851 	u32 offset, pad, maj_stat;
1852 	__be32 *p, *lenp;
1853 
1854 	p = svcauth_gss_prepare_to_wrap(rqstp, gsd);
1855 	if (p == NULL)
1856 		return 0;
1857 
1858 	lenp = p++;
1859 	offset = (u8 *)p - (u8 *)head->iov_base;
1860 	/* Buffer space for this field has already been reserved
1861 	 * in svcauth_gss_accept(). */
1862 	*p = cpu_to_be32(gc->gc_seq);
1863 
1864 	/*
1865 	 * If there is currently tail data, make sure there is
1866 	 * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
1867 	 * the page, and move the current tail data such that
1868 	 * there is RPC_MAX_AUTH_SIZE slack space available in
1869 	 * both the head and tail.
1870 	 */
1871 	if (tail->iov_base) {
1872 		if (tail->iov_base >= head->iov_base + PAGE_SIZE)
1873 			goto wrap_failed;
1874 		if (tail->iov_base < head->iov_base)
1875 			goto wrap_failed;
1876 		if (tail->iov_len + head->iov_len
1877 				+ 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1878 			goto wrap_failed;
1879 		memmove(tail->iov_base + RPC_MAX_AUTH_SIZE, tail->iov_base,
1880 			tail->iov_len);
1881 		tail->iov_base += RPC_MAX_AUTH_SIZE;
1882 	}
1883 	/*
1884 	 * If there is no current tail data, make sure there is
1885 	 * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
1886 	 * allotted page, and set up tail information such that there
1887 	 * is RPC_MAX_AUTH_SIZE slack space available in both the
1888 	 * head and tail.
1889 	 */
1890 	if (!tail->iov_base) {
1891 		if (head->iov_len + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1892 			goto wrap_failed;
1893 		tail->iov_base = head->iov_base
1894 			+ head->iov_len + RPC_MAX_AUTH_SIZE;
1895 		tail->iov_len = 0;
1896 	}
1897 
1898 	maj_stat = gss_wrap(gsd->rsci->mechctx, offset, buf, buf->pages);
1899 	if (maj_stat != GSS_S_COMPLETE)
1900 		goto bad_wrap;
1901 
1902 	*lenp = cpu_to_be32(buf->len - offset);
1903 	pad = xdr_pad_size(buf->len - offset);
1904 	p = (__be32 *)(tail->iov_base + tail->iov_len);
1905 	memset(p, 0, pad);
1906 	tail->iov_len += pad;
1907 	buf->len += pad;
1908 
1909 	return 0;
1910 wrap_failed:
1911 	trace_rpcgss_svc_wrap_failed(rqstp);
1912 	return -EINVAL;
1913 bad_wrap:
1914 	trace_rpcgss_svc_wrap(rqstp, maj_stat);
1915 	return -ENOMEM;
1916 }
1917 
1918 /**
1919  * svcauth_gss_release - Wrap payload and release resources
1920  * @rqstp: RPC transaction context
1921  *
1922  * Return values:
1923  *    %0: the Reply is ready to be sent
1924  *    %-ENOMEM: failed to allocate memory
1925  *    %-EINVAL: encoding error
1926  *
1927  * XXX: These return values do not match the return values documented
1928  *      for the auth_ops ->release method in linux/sunrpc/svcauth.h.
1929  */
1930 static int
1931 svcauth_gss_release(struct svc_rqst *rqstp)
1932 {
1933 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1934 	struct gss_svc_data *gsd = rqstp->rq_auth_data;
1935 	struct rpc_gss_wire_cred *gc;
1936 	int stat;
1937 
1938 	if (!gsd)
1939 		goto out;
1940 	gc = &gsd->clcred;
1941 	if (gc->gc_proc != RPC_GSS_PROC_DATA)
1942 		goto out;
1943 	/* Release can be called twice, but we only wrap once. */
1944 	if (gsd->verf_start == NULL)
1945 		goto out;
1946 
1947 	switch (gc->gc_svc) {
1948 	case RPC_GSS_SVC_NONE:
1949 		break;
1950 	case RPC_GSS_SVC_INTEGRITY:
1951 		stat = svcauth_gss_wrap_integ(rqstp);
1952 		if (stat)
1953 			goto out_err;
1954 		break;
1955 	case RPC_GSS_SVC_PRIVACY:
1956 		stat = svcauth_gss_wrap_priv(rqstp);
1957 		if (stat)
1958 			goto out_err;
1959 		break;
1960 	/*
1961 	 * For any other gc_svc value, svcauth_gss_accept() already set
1962 	 * the auth_error appropriately; just fall through:
1963 	 */
1964 	}
1965 
1966 out:
1967 	stat = 0;
1968 out_err:
1969 	if (rqstp->rq_client)
1970 		auth_domain_put(rqstp->rq_client);
1971 	rqstp->rq_client = NULL;
1972 	if (rqstp->rq_gssclient)
1973 		auth_domain_put(rqstp->rq_gssclient);
1974 	rqstp->rq_gssclient = NULL;
1975 	if (rqstp->rq_cred.cr_group_info)
1976 		put_group_info(rqstp->rq_cred.cr_group_info);
1977 	rqstp->rq_cred.cr_group_info = NULL;
1978 	if (gsd && gsd->rsci) {
1979 		cache_put(&gsd->rsci->h, sn->rsc_cache);
1980 		gsd->rsci = NULL;
1981 	}
1982 	return stat;
1983 }
1984 
1985 static void
1986 svcauth_gss_domain_release_rcu(struct rcu_head *head)
1987 {
1988 	struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
1989 	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
1990 
1991 	kfree(dom->name);
1992 	kfree(gd);
1993 }
1994 
1995 static void
1996 svcauth_gss_domain_release(struct auth_domain *dom)
1997 {
1998 	call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu);
1999 }
2000 
2001 static struct auth_ops svcauthops_gss = {
2002 	.name		= "rpcsec_gss",
2003 	.owner		= THIS_MODULE,
2004 	.flavour	= RPC_AUTH_GSS,
2005 	.accept		= svcauth_gss_accept,
2006 	.release	= svcauth_gss_release,
2007 	.domain_release = svcauth_gss_domain_release,
2008 	.set_client	= svcauth_gss_set_client,
2009 };
2010 
2011 static int rsi_cache_create_net(struct net *net)
2012 {
2013 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2014 	struct cache_detail *cd;
2015 	int err;
2016 
2017 	cd = cache_create_net(&rsi_cache_template, net);
2018 	if (IS_ERR(cd))
2019 		return PTR_ERR(cd);
2020 	err = cache_register_net(cd, net);
2021 	if (err) {
2022 		cache_destroy_net(cd, net);
2023 		return err;
2024 	}
2025 	sn->rsi_cache = cd;
2026 	return 0;
2027 }
2028 
2029 static void rsi_cache_destroy_net(struct net *net)
2030 {
2031 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2032 	struct cache_detail *cd = sn->rsi_cache;
2033 
2034 	sn->rsi_cache = NULL;
2035 	cache_purge(cd);
2036 	cache_unregister_net(cd, net);
2037 	cache_destroy_net(cd, net);
2038 }
2039 
2040 static int rsc_cache_create_net(struct net *net)
2041 {
2042 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2043 	struct cache_detail *cd;
2044 	int err;
2045 
2046 	cd = cache_create_net(&rsc_cache_template, net);
2047 	if (IS_ERR(cd))
2048 		return PTR_ERR(cd);
2049 	err = cache_register_net(cd, net);
2050 	if (err) {
2051 		cache_destroy_net(cd, net);
2052 		return err;
2053 	}
2054 	sn->rsc_cache = cd;
2055 	return 0;
2056 }
2057 
2058 static void rsc_cache_destroy_net(struct net *net)
2059 {
2060 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2061 	struct cache_detail *cd = sn->rsc_cache;
2062 
2063 	sn->rsc_cache = NULL;
2064 	cache_purge(cd);
2065 	cache_unregister_net(cd, net);
2066 	cache_destroy_net(cd, net);
2067 }
2068 
2069 int
2070 gss_svc_init_net(struct net *net)
2071 {
2072 	int rv;
2073 
2074 	rv = rsc_cache_create_net(net);
2075 	if (rv)
2076 		return rv;
2077 	rv = rsi_cache_create_net(net);
2078 	if (rv)
2079 		goto out1;
2080 	rv = create_use_gss_proxy_proc_entry(net);
2081 	if (rv)
2082 		goto out2;
2083 	return 0;
2084 out2:
2085 	rsi_cache_destroy_net(net);
2086 out1:
2087 	rsc_cache_destroy_net(net);
2088 	return rv;
2089 }
2090 
2091 void
2092 gss_svc_shutdown_net(struct net *net)
2093 {
2094 	destroy_use_gss_proxy_proc_entry(net);
2095 	rsi_cache_destroy_net(net);
2096 	rsc_cache_destroy_net(net);
2097 }
2098 
2099 int
2100 gss_svc_init(void)
2101 {
2102 	return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
2103 }
2104 
2105 void
2106 gss_svc_shutdown(void)
2107 {
2108 	svc_auth_unregister(RPC_AUTH_GSS);
2109 }
2110