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 out: 1254 cache_put(&rsip->h, sn->rsi_cache); 1255 return ret; 1256 } 1257 1258 static int gss_proxy_save_rsc(struct cache_detail *cd, 1259 struct gssp_upcall_data *ud, 1260 uint64_t *handle) 1261 { 1262 struct rsc rsci, *rscp = NULL; 1263 static atomic64_t ctxhctr; 1264 long long ctxh; 1265 struct gss_api_mech *gm = NULL; 1266 time64_t expiry; 1267 int status; 1268 1269 memset(&rsci, 0, sizeof(rsci)); 1270 /* context handle */ 1271 status = -ENOMEM; 1272 /* the handle needs to be just a unique id, 1273 * use a static counter */ 1274 ctxh = atomic64_inc_return(&ctxhctr); 1275 1276 /* make a copy for the caller */ 1277 *handle = ctxh; 1278 1279 /* make a copy for the rsc cache */ 1280 if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t))) 1281 goto out; 1282 rscp = rsc_lookup(cd, &rsci); 1283 if (!rscp) 1284 goto out; 1285 1286 /* creds */ 1287 if (!ud->found_creds) { 1288 /* userspace seem buggy, we should always get at least a 1289 * mapping to nobody */ 1290 goto out; 1291 } else { 1292 struct timespec64 boot; 1293 1294 /* steal creds */ 1295 rsci.cred = ud->creds; 1296 memset(&ud->creds, 0, sizeof(struct svc_cred)); 1297 1298 status = -EOPNOTSUPP; 1299 /* get mech handle from OID */ 1300 gm = gss_mech_get_by_OID(&ud->mech_oid); 1301 if (!gm) 1302 goto out; 1303 rsci.cred.cr_gss_mech = gm; 1304 1305 status = -EINVAL; 1306 /* mech-specific data: */ 1307 status = gss_import_sec_context(ud->out_handle.data, 1308 ud->out_handle.len, 1309 gm, &rsci.mechctx, 1310 &expiry, GFP_KERNEL); 1311 if (status) 1312 goto out; 1313 1314 getboottime64(&boot); 1315 expiry -= boot.tv_sec; 1316 } 1317 1318 rsci.h.expiry_time = expiry; 1319 rscp = rsc_update(cd, &rsci, rscp); 1320 status = 0; 1321 out: 1322 rsc_free(&rsci); 1323 if (rscp) 1324 cache_put(&rscp->h, cd); 1325 else 1326 status = -ENOMEM; 1327 return status; 1328 } 1329 1330 static int svcauth_gss_proxy_init(struct svc_rqst *rqstp, 1331 struct rpc_gss_wire_cred *gc) 1332 { 1333 struct kvec *resv = &rqstp->rq_res.head[0]; 1334 struct xdr_netobj cli_handle; 1335 struct gssp_upcall_data ud; 1336 uint64_t handle; 1337 int status; 1338 int ret; 1339 struct net *net = SVC_NET(rqstp); 1340 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1341 1342 memset(&ud, 0, sizeof(ud)); 1343 ret = gss_read_proxy_verf(rqstp, gc, &ud.in_handle, &ud.in_token); 1344 if (ret) 1345 return ret; 1346 1347 ret = SVC_CLOSE; 1348 1349 /* Perform synchronous upcall to gss-proxy */ 1350 status = gssp_accept_sec_context_upcall(net, &ud); 1351 if (status) 1352 goto out; 1353 1354 trace_rpcgss_svc_accept_upcall(rqstp, ud.major_status, ud.minor_status); 1355 1356 switch (ud.major_status) { 1357 case GSS_S_CONTINUE_NEEDED: 1358 cli_handle = ud.out_handle; 1359 break; 1360 case GSS_S_COMPLETE: 1361 status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle); 1362 if (status) 1363 goto out; 1364 cli_handle.data = (u8 *)&handle; 1365 cli_handle.len = sizeof(handle); 1366 break; 1367 default: 1368 goto out; 1369 } 1370 1371 /* Got an answer to the upcall; use it: */ 1372 if (gss_write_init_verf(sn->rsc_cache, rqstp, 1373 &cli_handle, &ud.major_status)) 1374 goto out; 1375 if (gss_write_resv(resv, PAGE_SIZE, 1376 &cli_handle, &ud.out_token, 1377 ud.major_status, ud.minor_status)) 1378 goto out; 1379 1380 ret = SVC_COMPLETE; 1381 out: 1382 gss_free_in_token_pages(&ud.in_token); 1383 gssp_free_upcall_data(&ud); 1384 return ret; 1385 } 1386 1387 /* 1388 * Try to set the sn->use_gss_proxy variable to a new value. We only allow 1389 * it to be changed if it's currently undefined (-1). If it's any other value 1390 * then return -EBUSY unless the type wouldn't have changed anyway. 1391 */ 1392 static int set_gss_proxy(struct net *net, int type) 1393 { 1394 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1395 int ret; 1396 1397 WARN_ON_ONCE(type != 0 && type != 1); 1398 ret = cmpxchg(&sn->use_gss_proxy, -1, type); 1399 if (ret != -1 && ret != type) 1400 return -EBUSY; 1401 return 0; 1402 } 1403 1404 static bool use_gss_proxy(struct net *net) 1405 { 1406 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1407 1408 /* If use_gss_proxy is still undefined, then try to disable it */ 1409 if (sn->use_gss_proxy == -1) 1410 set_gss_proxy(net, 0); 1411 return sn->use_gss_proxy; 1412 } 1413 1414 static noinline_for_stack int 1415 svcauth_gss_proc_init(struct svc_rqst *rqstp, struct rpc_gss_wire_cred *gc) 1416 { 1417 struct xdr_stream *xdr = &rqstp->rq_arg_stream; 1418 u32 flavor, len; 1419 void *body; 1420 1421 /* Call's verf field: */ 1422 if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0) 1423 return SVC_GARBAGE; 1424 if (flavor != RPC_AUTH_NULL || len != 0) { 1425 rqstp->rq_auth_stat = rpc_autherr_badverf; 1426 return SVC_DENIED; 1427 } 1428 1429 if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0) { 1430 rqstp->rq_auth_stat = rpc_autherr_badcred; 1431 return SVC_DENIED; 1432 } 1433 1434 if (!use_gss_proxy(SVC_NET(rqstp))) 1435 return svcauth_gss_legacy_init(rqstp, gc); 1436 return svcauth_gss_proxy_init(rqstp, gc); 1437 } 1438 1439 #ifdef CONFIG_PROC_FS 1440 1441 static ssize_t write_gssp(struct file *file, const char __user *buf, 1442 size_t count, loff_t *ppos) 1443 { 1444 struct net *net = pde_data(file_inode(file)); 1445 char tbuf[20]; 1446 unsigned long i; 1447 int res; 1448 1449 if (*ppos || count > sizeof(tbuf)-1) 1450 return -EINVAL; 1451 if (copy_from_user(tbuf, buf, count)) 1452 return -EFAULT; 1453 1454 tbuf[count] = 0; 1455 res = kstrtoul(tbuf, 0, &i); 1456 if (res) 1457 return res; 1458 if (i != 1) 1459 return -EINVAL; 1460 res = set_gssp_clnt(net); 1461 if (res) 1462 return res; 1463 res = set_gss_proxy(net, 1); 1464 if (res) 1465 return res; 1466 return count; 1467 } 1468 1469 static ssize_t read_gssp(struct file *file, char __user *buf, 1470 size_t count, loff_t *ppos) 1471 { 1472 struct net *net = pde_data(file_inode(file)); 1473 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1474 unsigned long p = *ppos; 1475 char tbuf[10]; 1476 size_t len; 1477 1478 snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy); 1479 len = strlen(tbuf); 1480 if (p >= len) 1481 return 0; 1482 len -= p; 1483 if (len > count) 1484 len = count; 1485 if (copy_to_user(buf, (void *)(tbuf+p), len)) 1486 return -EFAULT; 1487 *ppos += len; 1488 return len; 1489 } 1490 1491 static const struct proc_ops use_gss_proxy_proc_ops = { 1492 .proc_open = nonseekable_open, 1493 .proc_write = write_gssp, 1494 .proc_read = read_gssp, 1495 }; 1496 1497 static int create_use_gss_proxy_proc_entry(struct net *net) 1498 { 1499 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1500 struct proc_dir_entry **p = &sn->use_gssp_proc; 1501 1502 sn->use_gss_proxy = -1; 1503 *p = proc_create_data("use-gss-proxy", S_IFREG | 0600, 1504 sn->proc_net_rpc, 1505 &use_gss_proxy_proc_ops, net); 1506 if (!*p) 1507 return -ENOMEM; 1508 init_gssp_clnt(sn); 1509 return 0; 1510 } 1511 1512 static void destroy_use_gss_proxy_proc_entry(struct net *net) 1513 { 1514 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1515 1516 if (sn->use_gssp_proc) { 1517 remove_proc_entry("use-gss-proxy", sn->proc_net_rpc); 1518 clear_gssp_clnt(sn); 1519 } 1520 } 1521 #else /* CONFIG_PROC_FS */ 1522 1523 static int create_use_gss_proxy_proc_entry(struct net *net) 1524 { 1525 return 0; 1526 } 1527 1528 static void destroy_use_gss_proxy_proc_entry(struct net *net) {} 1529 1530 #endif /* CONFIG_PROC_FS */ 1531 1532 /* 1533 * The Call's credential body should contain a struct rpc_gss_cred_t. 1534 * 1535 * RFC 2203 Section 5 1536 * 1537 * struct rpc_gss_cred_t { 1538 * union switch (unsigned int version) { 1539 * case RPCSEC_GSS_VERS_1: 1540 * struct { 1541 * rpc_gss_proc_t gss_proc; 1542 * unsigned int seq_num; 1543 * rpc_gss_service_t service; 1544 * opaque handle<>; 1545 * } rpc_gss_cred_vers_1_t; 1546 * } 1547 * }; 1548 */ 1549 static bool 1550 svcauth_gss_decode_credbody(struct xdr_stream *xdr, 1551 struct rpc_gss_wire_cred *gc, 1552 __be32 **rpcstart) 1553 { 1554 ssize_t handle_len; 1555 u32 body_len; 1556 __be32 *p; 1557 1558 p = xdr_inline_decode(xdr, XDR_UNIT); 1559 if (!p) 1560 return false; 1561 /* 1562 * start of rpc packet is 7 u32's back from here: 1563 * xid direction rpcversion prog vers proc flavour 1564 */ 1565 *rpcstart = p - 7; 1566 body_len = be32_to_cpup(p); 1567 if (body_len > RPC_MAX_AUTH_SIZE) 1568 return false; 1569 1570 /* struct rpc_gss_cred_t */ 1571 if (xdr_stream_decode_u32(xdr, &gc->gc_v) < 0) 1572 return false; 1573 if (xdr_stream_decode_u32(xdr, &gc->gc_proc) < 0) 1574 return false; 1575 if (xdr_stream_decode_u32(xdr, &gc->gc_seq) < 0) 1576 return false; 1577 if (xdr_stream_decode_u32(xdr, &gc->gc_svc) < 0) 1578 return false; 1579 handle_len = xdr_stream_decode_opaque_inline(xdr, 1580 (void **)&gc->gc_ctx.data, 1581 body_len); 1582 if (handle_len < 0) 1583 return false; 1584 if (body_len != XDR_UNIT * 5 + xdr_align_size(handle_len)) 1585 return false; 1586 1587 gc->gc_ctx.len = handle_len; 1588 return true; 1589 } 1590 1591 /** 1592 * svcauth_gss_accept - Decode and validate incoming RPC_AUTH_GSS credential 1593 * @rqstp: RPC transaction 1594 * 1595 * Return values: 1596 * %SVC_OK: Success 1597 * %SVC_COMPLETE: GSS context lifetime event 1598 * %SVC_DENIED: Credential or verifier is not valid 1599 * %SVC_GARBAGE: Failed to decode credential or verifier 1600 * %SVC_CLOSE: Temporary failure 1601 * 1602 * The rqstp->rq_auth_stat field is also set (see RFCs 2203 and 5531). 1603 */ 1604 static int 1605 svcauth_gss_accept(struct svc_rqst *rqstp) 1606 { 1607 struct kvec *resv = &rqstp->rq_res.head[0]; 1608 struct gss_svc_data *svcdata = rqstp->rq_auth_data; 1609 __be32 *rpcstart; 1610 struct rpc_gss_wire_cred *gc; 1611 struct rsc *rsci = NULL; 1612 __be32 *reject_stat = resv->iov_base + resv->iov_len; 1613 int ret; 1614 struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id); 1615 1616 rqstp->rq_auth_stat = rpc_autherr_badcred; 1617 if (!svcdata) 1618 svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL); 1619 if (!svcdata) 1620 goto auth_err; 1621 rqstp->rq_auth_data = svcdata; 1622 svcdata->verf_start = NULL; 1623 svcdata->rsci = NULL; 1624 gc = &svcdata->clcred; 1625 1626 if (!svcauth_gss_decode_credbody(&rqstp->rq_arg_stream, gc, &rpcstart)) 1627 goto auth_err; 1628 if (gc->gc_v != RPC_GSS_VERSION) 1629 goto auth_err; 1630 1631 switch (gc->gc_proc) { 1632 case RPC_GSS_PROC_INIT: 1633 case RPC_GSS_PROC_CONTINUE_INIT: 1634 if (rqstp->rq_proc != 0) 1635 goto auth_err; 1636 return svcauth_gss_proc_init(rqstp, gc); 1637 case RPC_GSS_PROC_DESTROY: 1638 if (rqstp->rq_proc != 0) 1639 goto auth_err; 1640 fallthrough; 1641 case RPC_GSS_PROC_DATA: 1642 rqstp->rq_auth_stat = rpcsec_gsserr_credproblem; 1643 rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx); 1644 if (!rsci) 1645 goto auth_err; 1646 switch (svcauth_gss_verify_header(rqstp, rsci, rpcstart, gc)) { 1647 case SVC_OK: 1648 break; 1649 case SVC_DENIED: 1650 goto auth_err; 1651 case SVC_DROP: 1652 goto drop; 1653 } 1654 break; 1655 default: 1656 if (rqstp->rq_proc != 0) 1657 goto auth_err; 1658 rqstp->rq_auth_stat = rpc_autherr_rejectedcred; 1659 goto auth_err; 1660 } 1661 1662 /* now act upon the command: */ 1663 switch (gc->gc_proc) { 1664 case RPC_GSS_PROC_DESTROY: 1665 if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq)) 1666 goto auth_err; 1667 /* Delete the entry from the cache_list and call cache_put */ 1668 sunrpc_cache_unhash(sn->rsc_cache, &rsci->h); 1669 if (resv->iov_len + 4 > PAGE_SIZE) 1670 goto drop; 1671 svc_putnl(resv, RPC_SUCCESS); 1672 goto complete; 1673 case RPC_GSS_PROC_DATA: 1674 rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem; 1675 svcdata->verf_start = resv->iov_base + resv->iov_len; 1676 if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq)) 1677 goto auth_err; 1678 rqstp->rq_cred = rsci->cred; 1679 get_group_info(rsci->cred.cr_group_info); 1680 rqstp->rq_auth_stat = rpc_autherr_badcred; 1681 switch (gc->gc_svc) { 1682 case RPC_GSS_SVC_NONE: 1683 break; 1684 case RPC_GSS_SVC_INTEGRITY: 1685 /* placeholders for length and seq. number: */ 1686 svc_putnl(resv, 0); 1687 svc_putnl(resv, 0); 1688 if (svcauth_gss_unwrap_integ(rqstp, gc->gc_seq, 1689 rsci->mechctx)) 1690 goto garbage_args; 1691 rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE; 1692 break; 1693 case RPC_GSS_SVC_PRIVACY: 1694 /* placeholders for length and seq. number: */ 1695 svc_putnl(resv, 0); 1696 svc_putnl(resv, 0); 1697 if (svcauth_gss_unwrap_priv(rqstp, gc->gc_seq, 1698 rsci->mechctx)) 1699 goto garbage_args; 1700 rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE * 2; 1701 break; 1702 default: 1703 goto auth_err; 1704 } 1705 svcdata->rsci = rsci; 1706 cache_get(&rsci->h); 1707 rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor( 1708 rsci->mechctx->mech_type, 1709 GSS_C_QOP_DEFAULT, 1710 gc->gc_svc); 1711 ret = SVC_OK; 1712 trace_rpcgss_svc_authenticate(rqstp, gc); 1713 goto out; 1714 } 1715 garbage_args: 1716 ret = SVC_GARBAGE; 1717 goto out; 1718 auth_err: 1719 /* Restore write pointer to its original value: */ 1720 xdr_ressize_check(rqstp, reject_stat); 1721 ret = SVC_DENIED; 1722 goto out; 1723 complete: 1724 ret = SVC_COMPLETE; 1725 goto out; 1726 drop: 1727 ret = SVC_CLOSE; 1728 out: 1729 if (rsci) 1730 cache_put(&rsci->h, sn->rsc_cache); 1731 return ret; 1732 } 1733 1734 static __be32 * 1735 svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd) 1736 { 1737 __be32 *p; 1738 u32 verf_len; 1739 1740 p = gsd->verf_start; 1741 gsd->verf_start = NULL; 1742 1743 /* If the reply stat is nonzero, don't wrap: */ 1744 if (*(p-1) != rpc_success) 1745 return NULL; 1746 /* Skip the verifier: */ 1747 p += 1; 1748 verf_len = ntohl(*p++); 1749 p += XDR_QUADLEN(verf_len); 1750 /* move accept_stat to right place: */ 1751 memcpy(p, p + 2, 4); 1752 /* Also don't wrap if the accept stat is nonzero: */ 1753 if (*p != rpc_success) { 1754 resbuf->head[0].iov_len -= 2 * 4; 1755 return NULL; 1756 } 1757 p++; 1758 return p; 1759 } 1760 1761 /* 1762 * RFC 2203, Section 5.3.2.2 1763 * 1764 * struct rpc_gss_integ_data { 1765 * opaque databody_integ<>; 1766 * opaque checksum<>; 1767 * }; 1768 * 1769 * struct rpc_gss_data_t { 1770 * unsigned int seq_num; 1771 * proc_req_arg_t arg; 1772 * }; 1773 * 1774 * The RPC Reply message has already been XDR-encoded. rq_res_stream 1775 * is now positioned so that the checksum can be written just past 1776 * the RPC Reply message. 1777 */ 1778 static int svcauth_gss_wrap_integ(struct svc_rqst *rqstp) 1779 { 1780 struct gss_svc_data *gsd = rqstp->rq_auth_data; 1781 struct xdr_stream *xdr = &rqstp->rq_res_stream; 1782 struct rpc_gss_wire_cred *gc = &gsd->clcred; 1783 struct xdr_buf *buf = xdr->buf; 1784 struct xdr_buf databody_integ; 1785 struct xdr_netobj checksum; 1786 u32 offset, len, maj_stat; 1787 __be32 *p; 1788 1789 p = svcauth_gss_prepare_to_wrap(buf, gsd); 1790 if (p == NULL) 1791 goto out; 1792 1793 offset = (u8 *)(p + 1) - (u8 *)buf->head[0].iov_base; 1794 len = buf->len - offset; 1795 if (xdr_buf_subsegment(buf, &databody_integ, offset, len)) 1796 goto wrap_failed; 1797 /* Buffer space for these has already been reserved in 1798 * svcauth_gss_accept(). */ 1799 *p++ = cpu_to_be32(len); 1800 *p = cpu_to_be32(gc->gc_seq); 1801 1802 checksum.data = gsd->gsd_scratch; 1803 maj_stat = gss_get_mic(gsd->rsci->mechctx, &databody_integ, &checksum); 1804 if (maj_stat != GSS_S_COMPLETE) 1805 goto bad_mic; 1806 1807 if (xdr_stream_encode_opaque(xdr, checksum.data, checksum.len) < 0) 1808 goto wrap_failed; 1809 xdr_commit_encode(xdr); 1810 1811 out: 1812 return 0; 1813 1814 bad_mic: 1815 trace_rpcgss_svc_get_mic(rqstp, maj_stat); 1816 return -EINVAL; 1817 wrap_failed: 1818 trace_rpcgss_svc_wrap_failed(rqstp); 1819 return -EINVAL; 1820 } 1821 1822 /* 1823 * RFC 2203, Section 5.3.2.3 1824 * 1825 * struct rpc_gss_priv_data { 1826 * opaque databody_priv<> 1827 * }; 1828 * 1829 * struct rpc_gss_data_t { 1830 * unsigned int seq_num; 1831 * proc_req_arg_t arg; 1832 * }; 1833 * 1834 * gss_wrap() expands the size of the RPC message payload in the 1835 * response buffer. The main purpose of svcauth_gss_wrap_priv() 1836 * is to ensure there is adequate space in the response buffer to 1837 * avoid overflow during the wrap. 1838 */ 1839 static int svcauth_gss_wrap_priv(struct svc_rqst *rqstp) 1840 { 1841 struct gss_svc_data *gsd = rqstp->rq_auth_data; 1842 struct rpc_gss_wire_cred *gc = &gsd->clcred; 1843 struct xdr_buf *buf = &rqstp->rq_res; 1844 struct kvec *head = buf->head; 1845 struct kvec *tail = buf->tail; 1846 u32 offset, pad, maj_stat; 1847 __be32 *p, *lenp; 1848 1849 p = svcauth_gss_prepare_to_wrap(buf, gsd); 1850 if (p == NULL) 1851 return 0; 1852 1853 lenp = p++; 1854 offset = (u8 *)p - (u8 *)head->iov_base; 1855 /* Buffer space for this field has already been reserved 1856 * in svcauth_gss_accept(). */ 1857 *p = cpu_to_be32(gc->gc_seq); 1858 1859 /* 1860 * If there is currently tail data, make sure there is 1861 * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in 1862 * the page, and move the current tail data such that 1863 * there is RPC_MAX_AUTH_SIZE slack space available in 1864 * both the head and tail. 1865 */ 1866 if (tail->iov_base) { 1867 if (tail->iov_base >= head->iov_base + PAGE_SIZE) 1868 goto wrap_failed; 1869 if (tail->iov_base < head->iov_base) 1870 goto wrap_failed; 1871 if (tail->iov_len + head->iov_len 1872 + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE) 1873 goto wrap_failed; 1874 memmove(tail->iov_base + RPC_MAX_AUTH_SIZE, tail->iov_base, 1875 tail->iov_len); 1876 tail->iov_base += RPC_MAX_AUTH_SIZE; 1877 } 1878 /* 1879 * If there is no current tail data, make sure there is 1880 * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the 1881 * allotted page, and set up tail information such that there 1882 * is RPC_MAX_AUTH_SIZE slack space available in both the 1883 * head and tail. 1884 */ 1885 if (!tail->iov_base) { 1886 if (head->iov_len + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE) 1887 goto wrap_failed; 1888 tail->iov_base = head->iov_base 1889 + head->iov_len + RPC_MAX_AUTH_SIZE; 1890 tail->iov_len = 0; 1891 } 1892 1893 maj_stat = gss_wrap(gsd->rsci->mechctx, offset, buf, buf->pages); 1894 if (maj_stat != GSS_S_COMPLETE) 1895 goto bad_wrap; 1896 1897 *lenp = cpu_to_be32(buf->len - offset); 1898 pad = xdr_pad_size(buf->len - offset); 1899 p = (__be32 *)(tail->iov_base + tail->iov_len); 1900 memset(p, 0, pad); 1901 tail->iov_len += pad; 1902 buf->len += pad; 1903 1904 return 0; 1905 wrap_failed: 1906 trace_rpcgss_svc_wrap_failed(rqstp); 1907 return -EINVAL; 1908 bad_wrap: 1909 trace_rpcgss_svc_wrap(rqstp, maj_stat); 1910 return -ENOMEM; 1911 } 1912 1913 /** 1914 * svcauth_gss_release - Wrap payload and release resources 1915 * @rqstp: RPC transaction context 1916 * 1917 * Return values: 1918 * %0: the Reply is ready to be sent 1919 * %-ENOMEM: failed to allocate memory 1920 * %-EINVAL: encoding error 1921 * 1922 * XXX: These return values do not match the return values documented 1923 * for the auth_ops ->release method in linux/sunrpc/svcauth.h. 1924 */ 1925 static int 1926 svcauth_gss_release(struct svc_rqst *rqstp) 1927 { 1928 struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id); 1929 struct gss_svc_data *gsd = rqstp->rq_auth_data; 1930 struct rpc_gss_wire_cred *gc; 1931 int stat; 1932 1933 if (!gsd) 1934 goto out; 1935 gc = &gsd->clcred; 1936 if (gc->gc_proc != RPC_GSS_PROC_DATA) 1937 goto out; 1938 /* Release can be called twice, but we only wrap once. */ 1939 if (gsd->verf_start == NULL) 1940 goto out; 1941 1942 switch (gc->gc_svc) { 1943 case RPC_GSS_SVC_NONE: 1944 break; 1945 case RPC_GSS_SVC_INTEGRITY: 1946 stat = svcauth_gss_wrap_integ(rqstp); 1947 if (stat) 1948 goto out_err; 1949 break; 1950 case RPC_GSS_SVC_PRIVACY: 1951 stat = svcauth_gss_wrap_priv(rqstp); 1952 if (stat) 1953 goto out_err; 1954 break; 1955 /* 1956 * For any other gc_svc value, svcauth_gss_accept() already set 1957 * the auth_error appropriately; just fall through: 1958 */ 1959 } 1960 1961 out: 1962 stat = 0; 1963 out_err: 1964 if (rqstp->rq_client) 1965 auth_domain_put(rqstp->rq_client); 1966 rqstp->rq_client = NULL; 1967 if (rqstp->rq_gssclient) 1968 auth_domain_put(rqstp->rq_gssclient); 1969 rqstp->rq_gssclient = NULL; 1970 if (rqstp->rq_cred.cr_group_info) 1971 put_group_info(rqstp->rq_cred.cr_group_info); 1972 rqstp->rq_cred.cr_group_info = NULL; 1973 if (gsd && gsd->rsci) { 1974 cache_put(&gsd->rsci->h, sn->rsc_cache); 1975 gsd->rsci = NULL; 1976 } 1977 return stat; 1978 } 1979 1980 static void 1981 svcauth_gss_domain_release_rcu(struct rcu_head *head) 1982 { 1983 struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head); 1984 struct gss_domain *gd = container_of(dom, struct gss_domain, h); 1985 1986 kfree(dom->name); 1987 kfree(gd); 1988 } 1989 1990 static void 1991 svcauth_gss_domain_release(struct auth_domain *dom) 1992 { 1993 call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu); 1994 } 1995 1996 static struct auth_ops svcauthops_gss = { 1997 .name = "rpcsec_gss", 1998 .owner = THIS_MODULE, 1999 .flavour = RPC_AUTH_GSS, 2000 .accept = svcauth_gss_accept, 2001 .release = svcauth_gss_release, 2002 .domain_release = svcauth_gss_domain_release, 2003 .set_client = svcauth_gss_set_client, 2004 }; 2005 2006 static int rsi_cache_create_net(struct net *net) 2007 { 2008 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 2009 struct cache_detail *cd; 2010 int err; 2011 2012 cd = cache_create_net(&rsi_cache_template, net); 2013 if (IS_ERR(cd)) 2014 return PTR_ERR(cd); 2015 err = cache_register_net(cd, net); 2016 if (err) { 2017 cache_destroy_net(cd, net); 2018 return err; 2019 } 2020 sn->rsi_cache = cd; 2021 return 0; 2022 } 2023 2024 static void rsi_cache_destroy_net(struct net *net) 2025 { 2026 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 2027 struct cache_detail *cd = sn->rsi_cache; 2028 2029 sn->rsi_cache = NULL; 2030 cache_purge(cd); 2031 cache_unregister_net(cd, net); 2032 cache_destroy_net(cd, net); 2033 } 2034 2035 static int rsc_cache_create_net(struct net *net) 2036 { 2037 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 2038 struct cache_detail *cd; 2039 int err; 2040 2041 cd = cache_create_net(&rsc_cache_template, net); 2042 if (IS_ERR(cd)) 2043 return PTR_ERR(cd); 2044 err = cache_register_net(cd, net); 2045 if (err) { 2046 cache_destroy_net(cd, net); 2047 return err; 2048 } 2049 sn->rsc_cache = cd; 2050 return 0; 2051 } 2052 2053 static void rsc_cache_destroy_net(struct net *net) 2054 { 2055 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 2056 struct cache_detail *cd = sn->rsc_cache; 2057 2058 sn->rsc_cache = NULL; 2059 cache_purge(cd); 2060 cache_unregister_net(cd, net); 2061 cache_destroy_net(cd, net); 2062 } 2063 2064 int 2065 gss_svc_init_net(struct net *net) 2066 { 2067 int rv; 2068 2069 rv = rsc_cache_create_net(net); 2070 if (rv) 2071 return rv; 2072 rv = rsi_cache_create_net(net); 2073 if (rv) 2074 goto out1; 2075 rv = create_use_gss_proxy_proc_entry(net); 2076 if (rv) 2077 goto out2; 2078 return 0; 2079 out2: 2080 rsi_cache_destroy_net(net); 2081 out1: 2082 rsc_cache_destroy_net(net); 2083 return rv; 2084 } 2085 2086 void 2087 gss_svc_shutdown_net(struct net *net) 2088 { 2089 destroy_use_gss_proxy_proc_entry(net); 2090 rsi_cache_destroy_net(net); 2091 rsc_cache_destroy_net(net); 2092 } 2093 2094 int 2095 gss_svc_init(void) 2096 { 2097 return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss); 2098 } 2099 2100 void 2101 gss_svc_shutdown(void) 2102 { 2103 svc_auth_unregister(RPC_AUTH_GSS); 2104 } 2105