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_getnetobj(struct kvec *argv, struct xdr_netobj *o)
702 {
703 	int l;
704 
705 	if (argv->iov_len < 4)
706 		return -1;
707 	o->len = svc_getnl(argv);
708 	l = round_up_to_quad(o->len);
709 	if (argv->iov_len < l)
710 		return -1;
711 	o->data = argv->iov_base;
712 	argv->iov_base += l;
713 	argv->iov_len -= l;
714 	return 0;
715 }
716 
717 static inline int
718 svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
719 {
720 	u8 *p;
721 
722 	if (resv->iov_len + 4 > PAGE_SIZE)
723 		return -1;
724 	svc_putnl(resv, o->len);
725 	p = resv->iov_base + resv->iov_len;
726 	resv->iov_len += round_up_to_quad(o->len);
727 	if (resv->iov_len > PAGE_SIZE)
728 		return -1;
729 	memcpy(p, o->data, o->len);
730 	memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);
731 	return 0;
732 }
733 
734 /*
735  * Decode and verify a Call's verifier field. For RPC_AUTH_GSS Calls,
736  * the body of this field contains a variable length checksum.
737  *
738  * GSS-specific auth_stat values are mandated by RFC 2203 Section
739  * 5.3.3.3.
740  */
741 static int
742 svcauth_gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
743 			  __be32 *rpcstart, struct rpc_gss_wire_cred *gc)
744 {
745 	struct xdr_stream	*xdr = &rqstp->rq_arg_stream;
746 	struct gss_ctx		*ctx_id = rsci->mechctx;
747 	u32			flavor, maj_stat;
748 	struct xdr_buf		rpchdr;
749 	struct xdr_netobj	checksum;
750 	struct kvec		iov;
751 
752 	/*
753 	 * Compute the checksum of the incoming Call from the
754 	 * XID field to credential field:
755 	 */
756 	iov.iov_base = rpcstart;
757 	iov.iov_len = (u8 *)xdr->p - (u8 *)rpcstart;
758 	xdr_buf_from_iov(&iov, &rpchdr);
759 
760 	/* Call's verf field: */
761 	if (xdr_stream_decode_opaque_auth(xdr, &flavor,
762 					  (void **)&checksum.data,
763 					  &checksum.len) < 0) {
764 		rqstp->rq_auth_stat = rpc_autherr_badverf;
765 		return SVC_DENIED;
766 	}
767 	if (flavor != RPC_AUTH_GSS) {
768 		rqstp->rq_auth_stat = rpc_autherr_badverf;
769 		return SVC_DENIED;
770 	}
771 
772 	if (rqstp->rq_deferred)
773 		return SVC_OK;
774 	maj_stat = gss_verify_mic(ctx_id, &rpchdr, &checksum);
775 	if (maj_stat != GSS_S_COMPLETE) {
776 		trace_rpcgss_svc_mic(rqstp, maj_stat);
777 		rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
778 		return SVC_DENIED;
779 	}
780 
781 	if (gc->gc_seq > MAXSEQ) {
782 		trace_rpcgss_svc_seqno_large(rqstp, gc->gc_seq);
783 		rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
784 		return SVC_DENIED;
785 	}
786 	if (!gss_check_seq_num(rqstp, rsci, gc->gc_seq))
787 		return SVC_DROP;
788 	return SVC_OK;
789 }
790 
791 static int
792 gss_write_null_verf(struct svc_rqst *rqstp)
793 {
794 	__be32     *p;
795 
796 	svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);
797 	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
798 	/* don't really need to check if head->iov_len > PAGE_SIZE ... */
799 	*p++ = 0;
800 	if (!xdr_ressize_check(rqstp, p))
801 		return -1;
802 	return 0;
803 }
804 
805 static int
806 gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
807 {
808 	__be32			*xdr_seq;
809 	u32			maj_stat;
810 	struct xdr_buf		verf_data;
811 	struct xdr_netobj	mic;
812 	__be32			*p;
813 	struct kvec		iov;
814 	int err = -1;
815 
816 	svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);
817 	xdr_seq = kmalloc(4, GFP_KERNEL);
818 	if (!xdr_seq)
819 		return -ENOMEM;
820 	*xdr_seq = htonl(seq);
821 
822 	iov.iov_base = xdr_seq;
823 	iov.iov_len = 4;
824 	xdr_buf_from_iov(&iov, &verf_data);
825 	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
826 	mic.data = (u8 *)(p + 1);
827 	maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
828 	if (maj_stat != GSS_S_COMPLETE)
829 		goto out;
830 	*p++ = htonl(mic.len);
831 	memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
832 	p += XDR_QUADLEN(mic.len);
833 	if (!xdr_ressize_check(rqstp, p))
834 		goto out;
835 	err = 0;
836 out:
837 	kfree(xdr_seq);
838 	return err;
839 }
840 
841 struct gss_domain {
842 	struct auth_domain	h;
843 	u32			pseudoflavor;
844 };
845 
846 static struct auth_domain *
847 find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
848 {
849 	char *name;
850 
851 	name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
852 	if (!name)
853 		return NULL;
854 	return auth_domain_find(name);
855 }
856 
857 static struct auth_ops svcauthops_gss;
858 
859 u32 svcauth_gss_flavor(struct auth_domain *dom)
860 {
861 	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
862 
863 	return gd->pseudoflavor;
864 }
865 
866 EXPORT_SYMBOL_GPL(svcauth_gss_flavor);
867 
868 struct auth_domain *
869 svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
870 {
871 	struct gss_domain	*new;
872 	struct auth_domain	*test;
873 	int			stat = -ENOMEM;
874 
875 	new = kmalloc(sizeof(*new), GFP_KERNEL);
876 	if (!new)
877 		goto out;
878 	kref_init(&new->h.ref);
879 	new->h.name = kstrdup(name, GFP_KERNEL);
880 	if (!new->h.name)
881 		goto out_free_dom;
882 	new->h.flavour = &svcauthops_gss;
883 	new->pseudoflavor = pseudoflavor;
884 
885 	test = auth_domain_lookup(name, &new->h);
886 	if (test != &new->h) {
887 		pr_warn("svc: duplicate registration of gss pseudo flavour %s.\n",
888 			name);
889 		stat = -EADDRINUSE;
890 		auth_domain_put(test);
891 		goto out_free_name;
892 	}
893 	return test;
894 
895 out_free_name:
896 	kfree(new->h.name);
897 out_free_dom:
898 	kfree(new);
899 out:
900 	return ERR_PTR(stat);
901 }
902 EXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);
903 
904 /*
905  * RFC 2203, Section 5.3.2.2
906  *
907  *	struct rpc_gss_integ_data {
908  *		opaque databody_integ<>;
909  *		opaque checksum<>;
910  *	};
911  *
912  *	struct rpc_gss_data_t {
913  *		unsigned int seq_num;
914  *		proc_req_arg_t arg;
915  *	};
916  */
917 static noinline_for_stack int
918 svcauth_gss_unwrap_integ(struct svc_rqst *rqstp, u32 seq, struct gss_ctx *ctx)
919 {
920 	struct gss_svc_data *gsd = rqstp->rq_auth_data;
921 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
922 	u32 len, offset, seq_num, maj_stat;
923 	struct xdr_buf *buf = xdr->buf;
924 	struct xdr_buf databody_integ;
925 	struct xdr_netobj checksum;
926 
927 	/* NFS READ normally uses splice to send data in-place. However
928 	 * the data in cache can change after the reply's MIC is computed
929 	 * but before the RPC reply is sent. To prevent the client from
930 	 * rejecting the server-computed MIC in this somewhat rare case,
931 	 * do not use splice with the GSS integrity service.
932 	 */
933 	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
934 
935 	/* Did we already verify the signature on the original pass through? */
936 	if (rqstp->rq_deferred)
937 		return 0;
938 
939 	if (xdr_stream_decode_u32(xdr, &len) < 0)
940 		goto unwrap_failed;
941 	if (len & 3)
942 		goto unwrap_failed;
943 	offset = xdr_stream_pos(xdr);
944 	if (xdr_buf_subsegment(buf, &databody_integ, offset, len))
945 		goto unwrap_failed;
946 
947 	/*
948 	 * The xdr_stream now points to the @seq_num field. The next
949 	 * XDR data item is the @arg field, which contains the clear
950 	 * text RPC program payload. The checksum, which follows the
951 	 * @arg field, is located and decoded without updating the
952 	 * xdr_stream.
953 	 */
954 
955 	offset += len;
956 	if (xdr_decode_word(buf, offset, &checksum.len))
957 		goto unwrap_failed;
958 	if (checksum.len > sizeof(gsd->gsd_scratch))
959 		goto unwrap_failed;
960 	checksum.data = gsd->gsd_scratch;
961 	if (read_bytes_from_xdr_buf(buf, offset + XDR_UNIT, checksum.data,
962 				    checksum.len))
963 		goto unwrap_failed;
964 
965 	maj_stat = gss_verify_mic(ctx, &databody_integ, &checksum);
966 	if (maj_stat != GSS_S_COMPLETE)
967 		goto bad_mic;
968 
969 	/* The received seqno is protected by the checksum. */
970 	if (xdr_stream_decode_u32(xdr, &seq_num) < 0)
971 		goto unwrap_failed;
972 	if (seq_num != seq)
973 		goto bad_seqno;
974 
975 	xdr_truncate_decode(xdr, XDR_UNIT + checksum.len);
976 	return 0;
977 
978 unwrap_failed:
979 	trace_rpcgss_svc_unwrap_failed(rqstp);
980 	return -EINVAL;
981 bad_seqno:
982 	trace_rpcgss_svc_seqno_bad(rqstp, seq, seq_num);
983 	return -EINVAL;
984 bad_mic:
985 	trace_rpcgss_svc_mic(rqstp, maj_stat);
986 	return -EINVAL;
987 }
988 
989 static inline int
990 total_buf_len(struct xdr_buf *buf)
991 {
992 	return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;
993 }
994 
995 /*
996  * RFC 2203, Section 5.3.2.3
997  *
998  *	struct rpc_gss_priv_data {
999  *		opaque databody_priv<>
1000  *	};
1001  *
1002  *	struct rpc_gss_data_t {
1003  *		unsigned int seq_num;
1004  *		proc_req_arg_t arg;
1005  *	};
1006  */
1007 static noinline_for_stack int
1008 svcauth_gss_unwrap_priv(struct svc_rqst *rqstp, u32 seq, struct gss_ctx *ctx)
1009 {
1010 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1011 	u32 len, maj_stat, seq_num, offset;
1012 	struct xdr_buf *buf = xdr->buf;
1013 	unsigned int saved_len;
1014 
1015 	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
1016 
1017 	if (xdr_stream_decode_u32(xdr, &len) < 0)
1018 		goto unwrap_failed;
1019 	if (rqstp->rq_deferred) {
1020 		/* Already decrypted last time through! The sequence number
1021 		 * check at out_seq is unnecessary but harmless: */
1022 		goto out_seq;
1023 	}
1024 	if (len > xdr_stream_remaining(xdr))
1025 		goto unwrap_failed;
1026 	offset = xdr_stream_pos(xdr);
1027 
1028 	saved_len = buf->len;
1029 	maj_stat = gss_unwrap(ctx, offset, offset + len, buf);
1030 	if (maj_stat != GSS_S_COMPLETE)
1031 		goto bad_unwrap;
1032 	xdr->nwords -= XDR_QUADLEN(saved_len - buf->len);
1033 
1034 out_seq:
1035 	/* gss_unwrap() decrypted the sequence number. */
1036 	if (xdr_stream_decode_u32(xdr, &seq_num) < 0)
1037 		goto unwrap_failed;
1038 	if (seq_num != seq)
1039 		goto bad_seqno;
1040 	return 0;
1041 
1042 unwrap_failed:
1043 	trace_rpcgss_svc_unwrap_failed(rqstp);
1044 	return -EINVAL;
1045 bad_seqno:
1046 	trace_rpcgss_svc_seqno_bad(rqstp, seq, seq_num);
1047 	return -EINVAL;
1048 bad_unwrap:
1049 	trace_rpcgss_svc_unwrap(rqstp, maj_stat);
1050 	return -EINVAL;
1051 }
1052 
1053 static int
1054 svcauth_gss_set_client(struct svc_rqst *rqstp)
1055 {
1056 	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
1057 	struct rsc *rsci = svcdata->rsci;
1058 	struct rpc_gss_wire_cred *gc = &svcdata->clcred;
1059 	int stat;
1060 
1061 	rqstp->rq_auth_stat = rpc_autherr_badcred;
1062 
1063 	/*
1064 	 * A gss export can be specified either by:
1065 	 * 	export	*(sec=krb5,rw)
1066 	 * or by
1067 	 * 	export gss/krb5(rw)
1068 	 * The latter is deprecated; but for backwards compatibility reasons
1069 	 * the nfsd code will still fall back on trying it if the former
1070 	 * doesn't work; so we try to make both available to nfsd, below.
1071 	 */
1072 	rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
1073 	if (rqstp->rq_gssclient == NULL)
1074 		return SVC_DENIED;
1075 	stat = svcauth_unix_set_client(rqstp);
1076 	if (stat == SVC_DROP || stat == SVC_CLOSE)
1077 		return stat;
1078 
1079 	rqstp->rq_auth_stat = rpc_auth_ok;
1080 	return SVC_OK;
1081 }
1082 
1083 static inline int
1084 gss_write_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
1085 		struct xdr_netobj *out_handle, int *major_status)
1086 {
1087 	struct rsc *rsci;
1088 	int        rc;
1089 
1090 	if (*major_status != GSS_S_COMPLETE)
1091 		return gss_write_null_verf(rqstp);
1092 	rsci = gss_svc_searchbyctx(cd, out_handle);
1093 	if (rsci == NULL) {
1094 		*major_status = GSS_S_NO_CONTEXT;
1095 		return gss_write_null_verf(rqstp);
1096 	}
1097 	rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
1098 	cache_put(&rsci->h, cd);
1099 	return rc;
1100 }
1101 
1102 static void gss_free_in_token_pages(struct gssp_in_token *in_token)
1103 {
1104 	u32 inlen;
1105 	int i;
1106 
1107 	i = 0;
1108 	inlen = in_token->page_len;
1109 	while (inlen) {
1110 		if (in_token->pages[i])
1111 			put_page(in_token->pages[i]);
1112 		inlen -= inlen > PAGE_SIZE ? PAGE_SIZE : inlen;
1113 	}
1114 
1115 	kfree(in_token->pages);
1116 	in_token->pages = NULL;
1117 }
1118 
1119 static int gss_read_proxy_verf(struct svc_rqst *rqstp,
1120 			       struct rpc_gss_wire_cred *gc,
1121 			       struct xdr_netobj *in_handle,
1122 			       struct gssp_in_token *in_token)
1123 {
1124 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1125 	unsigned int length, pgto_offs, pgfrom_offs;
1126 	int pages, i, pgto, pgfrom;
1127 	size_t to_offs, from_offs;
1128 	u32 inlen;
1129 
1130 	if (dup_netobj(in_handle, &gc->gc_ctx))
1131 		return SVC_CLOSE;
1132 
1133 	/*
1134 	 *  RFC 2203 Section 5.2.2
1135 	 *
1136 	 *	struct rpc_gss_init_arg {
1137 	 *		opaque gss_token<>;
1138 	 *	};
1139 	 */
1140 	if (xdr_stream_decode_u32(xdr, &inlen) < 0)
1141 		goto out_denied_free;
1142 	if (inlen > xdr_stream_remaining(xdr))
1143 		goto out_denied_free;
1144 
1145 	pages = DIV_ROUND_UP(inlen, PAGE_SIZE);
1146 	in_token->pages = kcalloc(pages, sizeof(struct page *), GFP_KERNEL);
1147 	if (!in_token->pages)
1148 		goto out_denied_free;
1149 	in_token->page_base = 0;
1150 	in_token->page_len = inlen;
1151 	for (i = 0; i < pages; i++) {
1152 		in_token->pages[i] = alloc_page(GFP_KERNEL);
1153 		if (!in_token->pages[i]) {
1154 			gss_free_in_token_pages(in_token);
1155 			goto out_denied_free;
1156 		}
1157 	}
1158 
1159 	length = min_t(unsigned int, inlen, (char *)xdr->end - (char *)xdr->p);
1160 	memcpy(page_address(in_token->pages[0]), xdr->p, length);
1161 	inlen -= length;
1162 
1163 	to_offs = length;
1164 	from_offs = rqstp->rq_arg.page_base;
1165 	while (inlen) {
1166 		pgto = to_offs >> PAGE_SHIFT;
1167 		pgfrom = from_offs >> PAGE_SHIFT;
1168 		pgto_offs = to_offs & ~PAGE_MASK;
1169 		pgfrom_offs = from_offs & ~PAGE_MASK;
1170 
1171 		length = min_t(unsigned int, inlen,
1172 			 min_t(unsigned int, PAGE_SIZE - pgto_offs,
1173 			       PAGE_SIZE - pgfrom_offs));
1174 		memcpy(page_address(in_token->pages[pgto]) + pgto_offs,
1175 		       page_address(rqstp->rq_arg.pages[pgfrom]) + pgfrom_offs,
1176 		       length);
1177 
1178 		to_offs += length;
1179 		from_offs += length;
1180 		inlen -= length;
1181 	}
1182 	return 0;
1183 
1184 out_denied_free:
1185 	kfree(in_handle->data);
1186 	return SVC_DENIED;
1187 }
1188 
1189 static inline int
1190 gss_write_resv(struct kvec *resv, size_t size_limit,
1191 	       struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
1192 	       int major_status, int minor_status)
1193 {
1194 	if (resv->iov_len + 4 > size_limit)
1195 		return -1;
1196 	svc_putnl(resv, RPC_SUCCESS);
1197 	if (svc_safe_putnetobj(resv, out_handle))
1198 		return -1;
1199 	if (resv->iov_len + 3 * 4 > size_limit)
1200 		return -1;
1201 	svc_putnl(resv, major_status);
1202 	svc_putnl(resv, minor_status);
1203 	svc_putnl(resv, GSS_SEQ_WIN);
1204 	if (svc_safe_putnetobj(resv, out_token))
1205 		return -1;
1206 	return 0;
1207 }
1208 
1209 /*
1210  * Having read the cred already and found we're in the context
1211  * initiation case, read the verifier and initiate (or check the results
1212  * of) upcalls to userspace for help with context initiation.  If
1213  * the upcall results are available, write the verifier and result.
1214  * Otherwise, drop the request pending an answer to the upcall.
1215  */
1216 static int
1217 svcauth_gss_legacy_init(struct svc_rqst *rqstp,
1218 			struct rpc_gss_wire_cred *gc)
1219 {
1220 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1221 	struct kvec *resv = &rqstp->rq_res.head[0];
1222 	struct rsi *rsip, rsikey;
1223 	__be32 *p;
1224 	u32 len;
1225 	int ret;
1226 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1227 
1228 	memset(&rsikey, 0, sizeof(rsikey));
1229 	if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))
1230 		return SVC_CLOSE;
1231 
1232 	/*
1233 	 *  RFC 2203 Section 5.2.2
1234 	 *
1235 	 *	struct rpc_gss_init_arg {
1236 	 *		opaque gss_token<>;
1237 	 *	};
1238 	 */
1239 	if (xdr_stream_decode_u32(xdr, &len) < 0) {
1240 		kfree(rsikey.in_handle.data);
1241 		return SVC_DENIED;
1242 	}
1243 	p = xdr_inline_decode(xdr, len);
1244 	if (!p) {
1245 		kfree(rsikey.in_handle.data);
1246 		return SVC_DENIED;
1247 	}
1248 	rsikey.in_token.data = kmalloc(len, GFP_KERNEL);
1249 	if (ZERO_OR_NULL_PTR(rsikey.in_token.data)) {
1250 		kfree(rsikey.in_handle.data);
1251 		return SVC_CLOSE;
1252 	}
1253 	memcpy(rsikey.in_token.data, p, len);
1254 	rsikey.in_token.len = len;
1255 
1256 	/* Perform upcall, or find upcall result: */
1257 	rsip = rsi_lookup(sn->rsi_cache, &rsikey);
1258 	rsi_free(&rsikey);
1259 	if (!rsip)
1260 		return SVC_CLOSE;
1261 	if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
1262 		/* No upcall result: */
1263 		return SVC_CLOSE;
1264 
1265 	ret = SVC_CLOSE;
1266 	/* Got an answer to the upcall; use it: */
1267 	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1268 				&rsip->out_handle, &rsip->major_status))
1269 		goto out;
1270 	if (gss_write_resv(resv, PAGE_SIZE,
1271 			   &rsip->out_handle, &rsip->out_token,
1272 			   rsip->major_status, rsip->minor_status))
1273 		goto out;
1274 
1275 	ret = SVC_COMPLETE;
1276 out:
1277 	cache_put(&rsip->h, sn->rsi_cache);
1278 	return ret;
1279 }
1280 
1281 static int gss_proxy_save_rsc(struct cache_detail *cd,
1282 				struct gssp_upcall_data *ud,
1283 				uint64_t *handle)
1284 {
1285 	struct rsc rsci, *rscp = NULL;
1286 	static atomic64_t ctxhctr;
1287 	long long ctxh;
1288 	struct gss_api_mech *gm = NULL;
1289 	time64_t expiry;
1290 	int status;
1291 
1292 	memset(&rsci, 0, sizeof(rsci));
1293 	/* context handle */
1294 	status = -ENOMEM;
1295 	/* the handle needs to be just a unique id,
1296 	 * use a static counter */
1297 	ctxh = atomic64_inc_return(&ctxhctr);
1298 
1299 	/* make a copy for the caller */
1300 	*handle = ctxh;
1301 
1302 	/* make a copy for the rsc cache */
1303 	if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
1304 		goto out;
1305 	rscp = rsc_lookup(cd, &rsci);
1306 	if (!rscp)
1307 		goto out;
1308 
1309 	/* creds */
1310 	if (!ud->found_creds) {
1311 		/* userspace seem buggy, we should always get at least a
1312 		 * mapping to nobody */
1313 		goto out;
1314 	} else {
1315 		struct timespec64 boot;
1316 
1317 		/* steal creds */
1318 		rsci.cred = ud->creds;
1319 		memset(&ud->creds, 0, sizeof(struct svc_cred));
1320 
1321 		status = -EOPNOTSUPP;
1322 		/* get mech handle from OID */
1323 		gm = gss_mech_get_by_OID(&ud->mech_oid);
1324 		if (!gm)
1325 			goto out;
1326 		rsci.cred.cr_gss_mech = gm;
1327 
1328 		status = -EINVAL;
1329 		/* mech-specific data: */
1330 		status = gss_import_sec_context(ud->out_handle.data,
1331 						ud->out_handle.len,
1332 						gm, &rsci.mechctx,
1333 						&expiry, GFP_KERNEL);
1334 		if (status)
1335 			goto out;
1336 
1337 		getboottime64(&boot);
1338 		expiry -= boot.tv_sec;
1339 	}
1340 
1341 	rsci.h.expiry_time = expiry;
1342 	rscp = rsc_update(cd, &rsci, rscp);
1343 	status = 0;
1344 out:
1345 	rsc_free(&rsci);
1346 	if (rscp)
1347 		cache_put(&rscp->h, cd);
1348 	else
1349 		status = -ENOMEM;
1350 	return status;
1351 }
1352 
1353 static int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
1354 				  struct rpc_gss_wire_cred *gc)
1355 {
1356 	struct kvec *resv = &rqstp->rq_res.head[0];
1357 	struct xdr_netobj cli_handle;
1358 	struct gssp_upcall_data ud;
1359 	uint64_t handle;
1360 	int status;
1361 	int ret;
1362 	struct net *net = SVC_NET(rqstp);
1363 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1364 
1365 	memset(&ud, 0, sizeof(ud));
1366 	ret = gss_read_proxy_verf(rqstp, gc, &ud.in_handle, &ud.in_token);
1367 	if (ret)
1368 		return ret;
1369 
1370 	ret = SVC_CLOSE;
1371 
1372 	/* Perform synchronous upcall to gss-proxy */
1373 	status = gssp_accept_sec_context_upcall(net, &ud);
1374 	if (status)
1375 		goto out;
1376 
1377 	trace_rpcgss_svc_accept_upcall(rqstp, ud.major_status, ud.minor_status);
1378 
1379 	switch (ud.major_status) {
1380 	case GSS_S_CONTINUE_NEEDED:
1381 		cli_handle = ud.out_handle;
1382 		break;
1383 	case GSS_S_COMPLETE:
1384 		status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
1385 		if (status)
1386 			goto out;
1387 		cli_handle.data = (u8 *)&handle;
1388 		cli_handle.len = sizeof(handle);
1389 		break;
1390 	default:
1391 		goto out;
1392 	}
1393 
1394 	/* Got an answer to the upcall; use it: */
1395 	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1396 				&cli_handle, &ud.major_status))
1397 		goto out;
1398 	if (gss_write_resv(resv, PAGE_SIZE,
1399 			   &cli_handle, &ud.out_token,
1400 			   ud.major_status, ud.minor_status))
1401 		goto out;
1402 
1403 	ret = SVC_COMPLETE;
1404 out:
1405 	gss_free_in_token_pages(&ud.in_token);
1406 	gssp_free_upcall_data(&ud);
1407 	return ret;
1408 }
1409 
1410 /*
1411  * Try to set the sn->use_gss_proxy variable to a new value. We only allow
1412  * it to be changed if it's currently undefined (-1). If it's any other value
1413  * then return -EBUSY unless the type wouldn't have changed anyway.
1414  */
1415 static int set_gss_proxy(struct net *net, int type)
1416 {
1417 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1418 	int ret;
1419 
1420 	WARN_ON_ONCE(type != 0 && type != 1);
1421 	ret = cmpxchg(&sn->use_gss_proxy, -1, type);
1422 	if (ret != -1 && ret != type)
1423 		return -EBUSY;
1424 	return 0;
1425 }
1426 
1427 static bool use_gss_proxy(struct net *net)
1428 {
1429 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1430 
1431 	/* If use_gss_proxy is still undefined, then try to disable it */
1432 	if (sn->use_gss_proxy == -1)
1433 		set_gss_proxy(net, 0);
1434 	return sn->use_gss_proxy;
1435 }
1436 
1437 static noinline_for_stack int
1438 svcauth_gss_proc_init(struct svc_rqst *rqstp, struct rpc_gss_wire_cred *gc)
1439 {
1440 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1441 	u32 flavor, len;
1442 	void *body;
1443 
1444 	/* Call's verf field: */
1445 	if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
1446 		return SVC_GARBAGE;
1447 	if (flavor != RPC_AUTH_NULL || len != 0) {
1448 		rqstp->rq_auth_stat = rpc_autherr_badverf;
1449 		return SVC_DENIED;
1450 	}
1451 
1452 	if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0) {
1453 		rqstp->rq_auth_stat = rpc_autherr_badcred;
1454 		return SVC_DENIED;
1455 	}
1456 
1457 	if (!use_gss_proxy(SVC_NET(rqstp)))
1458 		return svcauth_gss_legacy_init(rqstp, gc);
1459 	return svcauth_gss_proxy_init(rqstp, gc);
1460 }
1461 
1462 #ifdef CONFIG_PROC_FS
1463 
1464 static ssize_t write_gssp(struct file *file, const char __user *buf,
1465 			 size_t count, loff_t *ppos)
1466 {
1467 	struct net *net = pde_data(file_inode(file));
1468 	char tbuf[20];
1469 	unsigned long i;
1470 	int res;
1471 
1472 	if (*ppos || count > sizeof(tbuf)-1)
1473 		return -EINVAL;
1474 	if (copy_from_user(tbuf, buf, count))
1475 		return -EFAULT;
1476 
1477 	tbuf[count] = 0;
1478 	res = kstrtoul(tbuf, 0, &i);
1479 	if (res)
1480 		return res;
1481 	if (i != 1)
1482 		return -EINVAL;
1483 	res = set_gssp_clnt(net);
1484 	if (res)
1485 		return res;
1486 	res = set_gss_proxy(net, 1);
1487 	if (res)
1488 		return res;
1489 	return count;
1490 }
1491 
1492 static ssize_t read_gssp(struct file *file, char __user *buf,
1493 			 size_t count, loff_t *ppos)
1494 {
1495 	struct net *net = pde_data(file_inode(file));
1496 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1497 	unsigned long p = *ppos;
1498 	char tbuf[10];
1499 	size_t len;
1500 
1501 	snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
1502 	len = strlen(tbuf);
1503 	if (p >= len)
1504 		return 0;
1505 	len -= p;
1506 	if (len > count)
1507 		len = count;
1508 	if (copy_to_user(buf, (void *)(tbuf+p), len))
1509 		return -EFAULT;
1510 	*ppos += len;
1511 	return len;
1512 }
1513 
1514 static const struct proc_ops use_gss_proxy_proc_ops = {
1515 	.proc_open	= nonseekable_open,
1516 	.proc_write	= write_gssp,
1517 	.proc_read	= read_gssp,
1518 };
1519 
1520 static int create_use_gss_proxy_proc_entry(struct net *net)
1521 {
1522 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1523 	struct proc_dir_entry **p = &sn->use_gssp_proc;
1524 
1525 	sn->use_gss_proxy = -1;
1526 	*p = proc_create_data("use-gss-proxy", S_IFREG | 0600,
1527 			      sn->proc_net_rpc,
1528 			      &use_gss_proxy_proc_ops, net);
1529 	if (!*p)
1530 		return -ENOMEM;
1531 	init_gssp_clnt(sn);
1532 	return 0;
1533 }
1534 
1535 static void destroy_use_gss_proxy_proc_entry(struct net *net)
1536 {
1537 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1538 
1539 	if (sn->use_gssp_proc) {
1540 		remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
1541 		clear_gssp_clnt(sn);
1542 	}
1543 }
1544 #else /* CONFIG_PROC_FS */
1545 
1546 static int create_use_gss_proxy_proc_entry(struct net *net)
1547 {
1548 	return 0;
1549 }
1550 
1551 static void destroy_use_gss_proxy_proc_entry(struct net *net) {}
1552 
1553 #endif /* CONFIG_PROC_FS */
1554 
1555 /*
1556  * Accept an rpcsec packet.
1557  * If context establishment, punt to user space
1558  * If data exchange, verify/decrypt
1559  * If context destruction, handle here
1560  * In the context establishment and destruction case we encode
1561  * response here and return SVC_COMPLETE.
1562  */
1563 static int
1564 svcauth_gss_accept(struct svc_rqst *rqstp)
1565 {
1566 	struct kvec	*argv = &rqstp->rq_arg.head[0];
1567 	struct kvec	*resv = &rqstp->rq_res.head[0];
1568 	u32		crlen;
1569 	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
1570 	struct rpc_gss_wire_cred *gc;
1571 	struct rsc	*rsci = NULL;
1572 	__be32		*rpcstart;
1573 	__be32		*reject_stat = resv->iov_base + resv->iov_len;
1574 	int		ret;
1575 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1576 
1577 	rqstp->rq_auth_stat = rpc_autherr_badcred;
1578 	if (!svcdata)
1579 		svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
1580 	if (!svcdata)
1581 		goto auth_err;
1582 	rqstp->rq_auth_data = svcdata;
1583 	svcdata->verf_start = NULL;
1584 	svcdata->rsci = NULL;
1585 	gc = &svcdata->clcred;
1586 
1587 	/* start of rpc packet is 7 u32's back from here:
1588 	 * xid direction rpcversion prog vers proc flavour
1589 	 */
1590 	rpcstart = argv->iov_base;
1591 	rpcstart -= 7;
1592 
1593 	/* credential is:
1594 	 *   version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
1595 	 * at least 5 u32s, and is preceded by length, so that makes 6.
1596 	 */
1597 
1598 	if (argv->iov_len < 5 * 4)
1599 		goto auth_err;
1600 	crlen = svc_getnl(argv);
1601 	if (svc_getnl(argv) != RPC_GSS_VERSION)
1602 		goto auth_err;
1603 	gc->gc_proc = svc_getnl(argv);
1604 	gc->gc_seq = svc_getnl(argv);
1605 	gc->gc_svc = svc_getnl(argv);
1606 	if (svc_safe_getnetobj(argv, &gc->gc_ctx))
1607 		goto auth_err;
1608 	if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
1609 		goto auth_err;
1610 
1611 	if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
1612 		goto auth_err;
1613 
1614 	svcxdr_init_decode(rqstp);
1615 	rqstp->rq_auth_stat = rpc_autherr_badverf;
1616 	switch (gc->gc_proc) {
1617 	case RPC_GSS_PROC_INIT:
1618 	case RPC_GSS_PROC_CONTINUE_INIT:
1619 		return svcauth_gss_proc_init(rqstp, gc);
1620 	case RPC_GSS_PROC_DATA:
1621 	case RPC_GSS_PROC_DESTROY:
1622 		/* Look up the context, and check the verifier: */
1623 		rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
1624 		rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
1625 		if (!rsci)
1626 			goto auth_err;
1627 		switch (svcauth_gss_verify_header(rqstp, rsci, rpcstart, gc)) {
1628 		case SVC_OK:
1629 			break;
1630 		case SVC_DENIED:
1631 			goto auth_err;
1632 		case SVC_DROP:
1633 			goto drop;
1634 		}
1635 		break;
1636 	default:
1637 		rqstp->rq_auth_stat = rpc_autherr_rejectedcred;
1638 		goto auth_err;
1639 	}
1640 
1641 	/* now act upon the command: */
1642 	switch (gc->gc_proc) {
1643 	case RPC_GSS_PROC_DESTROY:
1644 		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1645 			goto auth_err;
1646 		/* Delete the entry from the cache_list and call cache_put */
1647 		sunrpc_cache_unhash(sn->rsc_cache, &rsci->h);
1648 		if (resv->iov_len + 4 > PAGE_SIZE)
1649 			goto drop;
1650 		svc_putnl(resv, RPC_SUCCESS);
1651 		goto complete;
1652 	case RPC_GSS_PROC_DATA:
1653 		rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
1654 		svcdata->verf_start = resv->iov_base + resv->iov_len;
1655 		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1656 			goto auth_err;
1657 		rqstp->rq_cred = rsci->cred;
1658 		get_group_info(rsci->cred.cr_group_info);
1659 		rqstp->rq_auth_stat = rpc_autherr_badcred;
1660 		switch (gc->gc_svc) {
1661 		case RPC_GSS_SVC_NONE:
1662 			break;
1663 		case RPC_GSS_SVC_INTEGRITY:
1664 			/* placeholders for length and seq. number: */
1665 			svc_putnl(resv, 0);
1666 			svc_putnl(resv, 0);
1667 			if (svcauth_gss_unwrap_integ(rqstp, gc->gc_seq,
1668 						     rsci->mechctx))
1669 				goto garbage_args;
1670 			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE;
1671 			break;
1672 		case RPC_GSS_SVC_PRIVACY:
1673 			/* placeholders for length and seq. number: */
1674 			svc_putnl(resv, 0);
1675 			svc_putnl(resv, 0);
1676 			if (svcauth_gss_unwrap_priv(rqstp, gc->gc_seq,
1677 						    rsci->mechctx))
1678 				goto garbage_args;
1679 			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE * 2;
1680 			break;
1681 		default:
1682 			goto auth_err;
1683 		}
1684 		svcdata->rsci = rsci;
1685 		cache_get(&rsci->h);
1686 		rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
1687 					rsci->mechctx->mech_type,
1688 					GSS_C_QOP_DEFAULT,
1689 					gc->gc_svc);
1690 		ret = SVC_OK;
1691 		trace_rpcgss_svc_authenticate(rqstp, gc);
1692 		goto out;
1693 	}
1694 garbage_args:
1695 	ret = SVC_GARBAGE;
1696 	goto out;
1697 auth_err:
1698 	/* Restore write pointer to its original value: */
1699 	xdr_ressize_check(rqstp, reject_stat);
1700 	ret = SVC_DENIED;
1701 	goto out;
1702 complete:
1703 	ret = SVC_COMPLETE;
1704 	goto out;
1705 drop:
1706 	ret = SVC_CLOSE;
1707 out:
1708 	if (rsci)
1709 		cache_put(&rsci->h, sn->rsc_cache);
1710 	return ret;
1711 }
1712 
1713 static __be32 *
1714 svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
1715 {
1716 	__be32 *p;
1717 	u32 verf_len;
1718 
1719 	p = gsd->verf_start;
1720 	gsd->verf_start = NULL;
1721 
1722 	/* If the reply stat is nonzero, don't wrap: */
1723 	if (*(p-1) != rpc_success)
1724 		return NULL;
1725 	/* Skip the verifier: */
1726 	p += 1;
1727 	verf_len = ntohl(*p++);
1728 	p += XDR_QUADLEN(verf_len);
1729 	/* move accept_stat to right place: */
1730 	memcpy(p, p + 2, 4);
1731 	/* Also don't wrap if the accept stat is nonzero: */
1732 	if (*p != rpc_success) {
1733 		resbuf->head[0].iov_len -= 2 * 4;
1734 		return NULL;
1735 	}
1736 	p++;
1737 	return p;
1738 }
1739 
1740 static inline int
1741 svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
1742 {
1743 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1744 	struct rpc_gss_wire_cred *gc = &gsd->clcred;
1745 	struct xdr_buf *resbuf = &rqstp->rq_res;
1746 	struct xdr_buf integ_buf;
1747 	struct xdr_netobj mic;
1748 	struct kvec *resv;
1749 	__be32 *p;
1750 	int integ_offset, integ_len;
1751 	int stat = -EINVAL;
1752 
1753 	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
1754 	if (p == NULL)
1755 		goto out;
1756 	integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
1757 	integ_len = resbuf->len - integ_offset;
1758 	if (integ_len & 3)
1759 		goto out;
1760 	*p++ = htonl(integ_len);
1761 	*p++ = htonl(gc->gc_seq);
1762 	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len)) {
1763 		WARN_ON_ONCE(1);
1764 		goto out_err;
1765 	}
1766 	if (resbuf->tail[0].iov_base == NULL) {
1767 		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1768 			goto out_err;
1769 		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
1770 						+ resbuf->head[0].iov_len;
1771 		resbuf->tail[0].iov_len = 0;
1772 	}
1773 	resv = &resbuf->tail[0];
1774 	mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
1775 	if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
1776 		goto out_err;
1777 	svc_putnl(resv, mic.len);
1778 	memset(mic.data + mic.len, 0,
1779 			round_up_to_quad(mic.len) - mic.len);
1780 	resv->iov_len += XDR_QUADLEN(mic.len) << 2;
1781 	/* not strictly required: */
1782 	resbuf->len += XDR_QUADLEN(mic.len) << 2;
1783 	if (resv->iov_len > PAGE_SIZE)
1784 		goto out_err;
1785 out:
1786 	stat = 0;
1787 out_err:
1788 	return stat;
1789 }
1790 
1791 static inline int
1792 svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
1793 {
1794 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1795 	struct rpc_gss_wire_cred *gc = &gsd->clcred;
1796 	struct xdr_buf *resbuf = &rqstp->rq_res;
1797 	struct page **inpages = NULL;
1798 	__be32 *p, *len;
1799 	int offset;
1800 	int pad;
1801 
1802 	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
1803 	if (p == NULL)
1804 		return 0;
1805 	len = p++;
1806 	offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
1807 	*p++ = htonl(gc->gc_seq);
1808 	inpages = resbuf->pages;
1809 	/* XXX: Would be better to write some xdr helper functions for
1810 	 * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
1811 
1812 	/*
1813 	 * If there is currently tail data, make sure there is
1814 	 * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
1815 	 * the page, and move the current tail data such that
1816 	 * there is RPC_MAX_AUTH_SIZE slack space available in
1817 	 * both the head and tail.
1818 	 */
1819 	if (resbuf->tail[0].iov_base) {
1820 		if (resbuf->tail[0].iov_base >=
1821 			resbuf->head[0].iov_base + PAGE_SIZE)
1822 			return -EINVAL;
1823 		if (resbuf->tail[0].iov_base < resbuf->head[0].iov_base)
1824 			return -EINVAL;
1825 		if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
1826 				+ 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1827 			return -ENOMEM;
1828 		memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
1829 			resbuf->tail[0].iov_base,
1830 			resbuf->tail[0].iov_len);
1831 		resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
1832 	}
1833 	/*
1834 	 * If there is no current tail data, make sure there is
1835 	 * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
1836 	 * allotted page, and set up tail information such that there
1837 	 * is RPC_MAX_AUTH_SIZE slack space available in both the
1838 	 * head and tail.
1839 	 */
1840 	if (resbuf->tail[0].iov_base == NULL) {
1841 		if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1842 			return -ENOMEM;
1843 		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
1844 			+ resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
1845 		resbuf->tail[0].iov_len = 0;
1846 	}
1847 	if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
1848 		return -ENOMEM;
1849 	*len = htonl(resbuf->len - offset);
1850 	pad = 3 - ((resbuf->len - offset - 1)&3);
1851 	p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
1852 	memset(p, 0, pad);
1853 	resbuf->tail[0].iov_len += pad;
1854 	resbuf->len += pad;
1855 	return 0;
1856 }
1857 
1858 static int
1859 svcauth_gss_release(struct svc_rqst *rqstp)
1860 {
1861 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1862 	struct rpc_gss_wire_cred *gc;
1863 	struct xdr_buf *resbuf = &rqstp->rq_res;
1864 	int stat = -EINVAL;
1865 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1866 
1867 	if (!gsd)
1868 		goto out;
1869 	gc = &gsd->clcred;
1870 	if (gc->gc_proc != RPC_GSS_PROC_DATA)
1871 		goto out;
1872 	/* Release can be called twice, but we only wrap once. */
1873 	if (gsd->verf_start == NULL)
1874 		goto out;
1875 	/* normally not set till svc_send, but we need it here: */
1876 	/* XXX: what for?  Do we mess it up the moment we call svc_putu32
1877 	 * or whatever? */
1878 	resbuf->len = total_buf_len(resbuf);
1879 	switch (gc->gc_svc) {
1880 	case RPC_GSS_SVC_NONE:
1881 		break;
1882 	case RPC_GSS_SVC_INTEGRITY:
1883 		stat = svcauth_gss_wrap_resp_integ(rqstp);
1884 		if (stat)
1885 			goto out_err;
1886 		break;
1887 	case RPC_GSS_SVC_PRIVACY:
1888 		stat = svcauth_gss_wrap_resp_priv(rqstp);
1889 		if (stat)
1890 			goto out_err;
1891 		break;
1892 	/*
1893 	 * For any other gc_svc value, svcauth_gss_accept() already set
1894 	 * the auth_error appropriately; just fall through:
1895 	 */
1896 	}
1897 
1898 out:
1899 	stat = 0;
1900 out_err:
1901 	if (rqstp->rq_client)
1902 		auth_domain_put(rqstp->rq_client);
1903 	rqstp->rq_client = NULL;
1904 	if (rqstp->rq_gssclient)
1905 		auth_domain_put(rqstp->rq_gssclient);
1906 	rqstp->rq_gssclient = NULL;
1907 	if (rqstp->rq_cred.cr_group_info)
1908 		put_group_info(rqstp->rq_cred.cr_group_info);
1909 	rqstp->rq_cred.cr_group_info = NULL;
1910 	if (gsd && gsd->rsci) {
1911 		cache_put(&gsd->rsci->h, sn->rsc_cache);
1912 		gsd->rsci = NULL;
1913 	}
1914 	return stat;
1915 }
1916 
1917 static void
1918 svcauth_gss_domain_release_rcu(struct rcu_head *head)
1919 {
1920 	struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
1921 	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
1922 
1923 	kfree(dom->name);
1924 	kfree(gd);
1925 }
1926 
1927 static void
1928 svcauth_gss_domain_release(struct auth_domain *dom)
1929 {
1930 	call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu);
1931 }
1932 
1933 static struct auth_ops svcauthops_gss = {
1934 	.name		= "rpcsec_gss",
1935 	.owner		= THIS_MODULE,
1936 	.flavour	= RPC_AUTH_GSS,
1937 	.accept		= svcauth_gss_accept,
1938 	.release	= svcauth_gss_release,
1939 	.domain_release = svcauth_gss_domain_release,
1940 	.set_client	= svcauth_gss_set_client,
1941 };
1942 
1943 static int rsi_cache_create_net(struct net *net)
1944 {
1945 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1946 	struct cache_detail *cd;
1947 	int err;
1948 
1949 	cd = cache_create_net(&rsi_cache_template, net);
1950 	if (IS_ERR(cd))
1951 		return PTR_ERR(cd);
1952 	err = cache_register_net(cd, net);
1953 	if (err) {
1954 		cache_destroy_net(cd, net);
1955 		return err;
1956 	}
1957 	sn->rsi_cache = cd;
1958 	return 0;
1959 }
1960 
1961 static void rsi_cache_destroy_net(struct net *net)
1962 {
1963 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1964 	struct cache_detail *cd = sn->rsi_cache;
1965 
1966 	sn->rsi_cache = NULL;
1967 	cache_purge(cd);
1968 	cache_unregister_net(cd, net);
1969 	cache_destroy_net(cd, net);
1970 }
1971 
1972 static int rsc_cache_create_net(struct net *net)
1973 {
1974 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1975 	struct cache_detail *cd;
1976 	int err;
1977 
1978 	cd = cache_create_net(&rsc_cache_template, net);
1979 	if (IS_ERR(cd))
1980 		return PTR_ERR(cd);
1981 	err = cache_register_net(cd, net);
1982 	if (err) {
1983 		cache_destroy_net(cd, net);
1984 		return err;
1985 	}
1986 	sn->rsc_cache = cd;
1987 	return 0;
1988 }
1989 
1990 static void rsc_cache_destroy_net(struct net *net)
1991 {
1992 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1993 	struct cache_detail *cd = sn->rsc_cache;
1994 
1995 	sn->rsc_cache = NULL;
1996 	cache_purge(cd);
1997 	cache_unregister_net(cd, net);
1998 	cache_destroy_net(cd, net);
1999 }
2000 
2001 int
2002 gss_svc_init_net(struct net *net)
2003 {
2004 	int rv;
2005 
2006 	rv = rsc_cache_create_net(net);
2007 	if (rv)
2008 		return rv;
2009 	rv = rsi_cache_create_net(net);
2010 	if (rv)
2011 		goto out1;
2012 	rv = create_use_gss_proxy_proc_entry(net);
2013 	if (rv)
2014 		goto out2;
2015 	return 0;
2016 out2:
2017 	rsi_cache_destroy_net(net);
2018 out1:
2019 	rsc_cache_destroy_net(net);
2020 	return rv;
2021 }
2022 
2023 void
2024 gss_svc_shutdown_net(struct net *net)
2025 {
2026 	destroy_use_gss_proxy_proc_entry(net);
2027 	rsi_cache_destroy_net(net);
2028 	rsc_cache_destroy_net(net);
2029 }
2030 
2031 int
2032 gss_svc_init(void)
2033 {
2034 	return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
2035 }
2036 
2037 void
2038 gss_svc_shutdown(void)
2039 {
2040 	svc_auth_unregister(RPC_AUTH_GSS);
2041 }
2042