| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | #include <linux/types.h> |
|---|
| 2 | 3 | #include <linux/sched.h> |
|---|
| 3 | 4 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 37 | 38 | extern struct auth_ops svcauth_null; |
|---|
| 38 | 39 | extern struct auth_ops svcauth_unix; |
|---|
| 39 | 40 | |
|---|
| 40 | | -static void svcauth_unix_domain_release(struct auth_domain *dom) |
|---|
| 41 | +static void svcauth_unix_domain_release_rcu(struct rcu_head *head) |
|---|
| 41 | 42 | { |
|---|
| 43 | + struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head); |
|---|
| 42 | 44 | struct unix_domain *ud = container_of(dom, struct unix_domain, h); |
|---|
| 43 | 45 | |
|---|
| 44 | 46 | kfree(dom->name); |
|---|
| 45 | 47 | kfree(ud); |
|---|
| 48 | +} |
|---|
| 49 | + |
|---|
| 50 | +static void svcauth_unix_domain_release(struct auth_domain *dom) |
|---|
| 51 | +{ |
|---|
| 52 | + call_rcu(&dom->rcu_head, svcauth_unix_domain_release_rcu); |
|---|
| 46 | 53 | } |
|---|
| 47 | 54 | |
|---|
| 48 | 55 | struct auth_domain *unix_domain_find(char *name) |
|---|
| .. | .. |
|---|
| 50 | 57 | struct auth_domain *rv; |
|---|
| 51 | 58 | struct unix_domain *new = NULL; |
|---|
| 52 | 59 | |
|---|
| 53 | | - rv = auth_domain_lookup(name, NULL); |
|---|
| 60 | + rv = auth_domain_find(name); |
|---|
| 54 | 61 | while(1) { |
|---|
| 55 | 62 | if (rv) { |
|---|
| 56 | 63 | if (new && rv != &new->h) |
|---|
| .. | .. |
|---|
| 91 | 98 | char m_class[8]; /* e.g. "nfsd" */ |
|---|
| 92 | 99 | struct in6_addr m_addr; |
|---|
| 93 | 100 | struct unix_domain *m_client; |
|---|
| 101 | + struct rcu_head m_rcu; |
|---|
| 94 | 102 | }; |
|---|
| 95 | 103 | |
|---|
| 96 | 104 | static void ip_map_put(struct kref *kref) |
|---|
| .. | .. |
|---|
| 101 | 109 | if (test_bit(CACHE_VALID, &item->flags) && |
|---|
| 102 | 110 | !test_bit(CACHE_NEGATIVE, &item->flags)) |
|---|
| 103 | 111 | auth_domain_put(&im->m_client->h); |
|---|
| 104 | | - kfree(im); |
|---|
| 112 | + kfree_rcu(im, m_rcu); |
|---|
| 105 | 113 | } |
|---|
| 106 | 114 | |
|---|
| 107 | 115 | static inline int hash_ip6(const struct in6_addr *ip) |
|---|
| .. | .. |
|---|
| 140 | 148 | return NULL; |
|---|
| 141 | 149 | } |
|---|
| 142 | 150 | |
|---|
| 151 | +static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h) |
|---|
| 152 | +{ |
|---|
| 153 | + return sunrpc_cache_pipe_upcall(cd, h); |
|---|
| 154 | +} |
|---|
| 155 | + |
|---|
| 143 | 156 | static void ip_map_request(struct cache_detail *cd, |
|---|
| 144 | 157 | struct cache_head *h, |
|---|
| 145 | 158 | char **bpp, int *blen) |
|---|
| .. | .. |
|---|
| 158 | 171 | } |
|---|
| 159 | 172 | |
|---|
| 160 | 173 | static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr); |
|---|
| 161 | | -static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry); |
|---|
| 174 | +static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time64_t expiry); |
|---|
| 162 | 175 | |
|---|
| 163 | 176 | static int ip_map_parse(struct cache_detail *cd, |
|---|
| 164 | 177 | char *mesg, int mlen) |
|---|
| .. | .. |
|---|
| 179 | 192 | |
|---|
| 180 | 193 | struct ip_map *ipmp; |
|---|
| 181 | 194 | struct auth_domain *dom; |
|---|
| 182 | | - time_t expiry; |
|---|
| 195 | + time64_t expiry; |
|---|
| 183 | 196 | |
|---|
| 184 | 197 | if (mesg[mlen-1] != '\n') |
|---|
| 185 | 198 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 280 | 293 | |
|---|
| 281 | 294 | strcpy(ip.m_class, class); |
|---|
| 282 | 295 | ip.m_addr = *addr; |
|---|
| 283 | | - ch = sunrpc_cache_lookup(cd, &ip.h, |
|---|
| 284 | | - hash_str(class, IP_HASHBITS) ^ |
|---|
| 285 | | - hash_ip6(addr)); |
|---|
| 296 | + ch = sunrpc_cache_lookup_rcu(cd, &ip.h, |
|---|
| 297 | + hash_str(class, IP_HASHBITS) ^ |
|---|
| 298 | + hash_ip6(addr)); |
|---|
| 286 | 299 | |
|---|
| 287 | 300 | if (ch) |
|---|
| 288 | 301 | return container_of(ch, struct ip_map, h); |
|---|
| .. | .. |
|---|
| 300 | 313 | } |
|---|
| 301 | 314 | |
|---|
| 302 | 315 | static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, |
|---|
| 303 | | - struct unix_domain *udom, time_t expiry) |
|---|
| 316 | + struct unix_domain *udom, time64_t expiry) |
|---|
| 304 | 317 | { |
|---|
| 305 | 318 | struct ip_map ip; |
|---|
| 306 | 319 | struct cache_head *ch; |
|---|
| .. | .. |
|---|
| 317 | 330 | return -ENOMEM; |
|---|
| 318 | 331 | cache_put(ch, cd); |
|---|
| 319 | 332 | return 0; |
|---|
| 320 | | -} |
|---|
| 321 | | - |
|---|
| 322 | | -static inline int ip_map_update(struct net *net, struct ip_map *ipm, |
|---|
| 323 | | - struct unix_domain *udom, time_t expiry) |
|---|
| 324 | | -{ |
|---|
| 325 | | - struct sunrpc_net *sn; |
|---|
| 326 | | - |
|---|
| 327 | | - sn = net_generic(net, sunrpc_net_id); |
|---|
| 328 | | - return __ip_map_update(sn->ip_map_cache, ipm, udom, expiry); |
|---|
| 329 | 333 | } |
|---|
| 330 | 334 | |
|---|
| 331 | 335 | void svcauth_unix_purge(struct net *net) |
|---|
| .. | .. |
|---|
| 412 | 416 | struct cache_head h; |
|---|
| 413 | 417 | kuid_t uid; |
|---|
| 414 | 418 | struct group_info *gi; |
|---|
| 419 | + struct rcu_head rcu; |
|---|
| 415 | 420 | }; |
|---|
| 416 | 421 | |
|---|
| 417 | 422 | static int unix_gid_hash(kuid_t uid) |
|---|
| .. | .. |
|---|
| 426 | 431 | if (test_bit(CACHE_VALID, &item->flags) && |
|---|
| 427 | 432 | !test_bit(CACHE_NEGATIVE, &item->flags)) |
|---|
| 428 | 433 | put_group_info(ug->gi); |
|---|
| 429 | | - kfree(ug); |
|---|
| 434 | + kfree_rcu(ug, rcu); |
|---|
| 430 | 435 | } |
|---|
| 431 | 436 | |
|---|
| 432 | 437 | static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew) |
|---|
| .. | .. |
|---|
| 458 | 463 | return NULL; |
|---|
| 459 | 464 | } |
|---|
| 460 | 465 | |
|---|
| 466 | +static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h) |
|---|
| 467 | +{ |
|---|
| 468 | + return sunrpc_cache_pipe_upcall_timeout(cd, h); |
|---|
| 469 | +} |
|---|
| 470 | + |
|---|
| 461 | 471 | static void unix_gid_request(struct cache_detail *cd, |
|---|
| 462 | 472 | struct cache_head *h, |
|---|
| 463 | 473 | char **bpp, int *blen) |
|---|
| .. | .. |
|---|
| 482 | 492 | int rv; |
|---|
| 483 | 493 | int i; |
|---|
| 484 | 494 | int err; |
|---|
| 485 | | - time_t expiry; |
|---|
| 495 | + time64_t expiry; |
|---|
| 486 | 496 | struct unix_gid ug, *ugp; |
|---|
| 487 | 497 | |
|---|
| 488 | 498 | if (mesg[mlen - 1] != '\n') |
|---|
| .. | .. |
|---|
| 492 | 502 | rv = get_int(&mesg, &id); |
|---|
| 493 | 503 | if (rv) |
|---|
| 494 | 504 | return -EINVAL; |
|---|
| 495 | | - uid = make_kuid(&init_user_ns, id); |
|---|
| 505 | + uid = make_kuid(current_user_ns(), id); |
|---|
| 496 | 506 | ug.uid = uid; |
|---|
| 497 | 507 | |
|---|
| 498 | 508 | expiry = get_expiry(&mesg); |
|---|
| .. | .. |
|---|
| 514 | 524 | err = -EINVAL; |
|---|
| 515 | 525 | if (rv) |
|---|
| 516 | 526 | goto out; |
|---|
| 517 | | - kgid = make_kgid(&init_user_ns, gid); |
|---|
| 527 | + kgid = make_kgid(current_user_ns(), gid); |
|---|
| 518 | 528 | if (!gid_valid(kgid)) |
|---|
| 519 | 529 | goto out; |
|---|
| 520 | 530 | ug.gi->gid[i] = kgid; |
|---|
| .. | .. |
|---|
| 547 | 557 | struct cache_detail *cd, |
|---|
| 548 | 558 | struct cache_head *h) |
|---|
| 549 | 559 | { |
|---|
| 550 | | - struct user_namespace *user_ns = &init_user_ns; |
|---|
| 560 | + struct user_namespace *user_ns = m->file->f_cred->user_ns; |
|---|
| 551 | 561 | struct unix_gid *ug; |
|---|
| 552 | 562 | int i; |
|---|
| 553 | 563 | int glen; |
|---|
| .. | .. |
|---|
| 575 | 585 | .hash_size = GID_HASHMAX, |
|---|
| 576 | 586 | .name = "auth.unix.gid", |
|---|
| 577 | 587 | .cache_put = unix_gid_put, |
|---|
| 588 | + .cache_upcall = unix_gid_upcall, |
|---|
| 578 | 589 | .cache_request = unix_gid_request, |
|---|
| 579 | 590 | .cache_parse = unix_gid_parse, |
|---|
| 580 | 591 | .cache_show = unix_gid_show, |
|---|
| .. | .. |
|---|
| 619 | 630 | struct cache_head *ch; |
|---|
| 620 | 631 | |
|---|
| 621 | 632 | ug.uid = uid; |
|---|
| 622 | | - ch = sunrpc_cache_lookup(cd, &ug.h, unix_gid_hash(uid)); |
|---|
| 633 | + ch = sunrpc_cache_lookup_rcu(cd, &ug.h, unix_gid_hash(uid)); |
|---|
| 623 | 634 | if (ch) |
|---|
| 624 | 635 | return container_of(ch, struct unix_gid, h); |
|---|
| 625 | 636 | else |
|---|
| .. | .. |
|---|
| 788 | 799 | struct kvec *argv = &rqstp->rq_arg.head[0]; |
|---|
| 789 | 800 | struct kvec *resv = &rqstp->rq_res.head[0]; |
|---|
| 790 | 801 | struct svc_cred *cred = &rqstp->rq_cred; |
|---|
| 802 | + struct user_namespace *userns; |
|---|
| 791 | 803 | u32 slen, i; |
|---|
| 792 | 804 | int len = argv->iov_len; |
|---|
| 793 | 805 | |
|---|
| .. | .. |
|---|
| 808 | 820 | * (export-specific) anonymous id by nfsd_setuser. |
|---|
| 809 | 821 | * Supplementary gid's will be left alone. |
|---|
| 810 | 822 | */ |
|---|
| 811 | | - cred->cr_uid = make_kuid(&init_user_ns, svc_getnl(argv)); /* uid */ |
|---|
| 812 | | - cred->cr_gid = make_kgid(&init_user_ns, svc_getnl(argv)); /* gid */ |
|---|
| 823 | + userns = (rqstp->rq_xprt && rqstp->rq_xprt->xpt_cred) ? |
|---|
| 824 | + rqstp->rq_xprt->xpt_cred->user_ns : &init_user_ns; |
|---|
| 825 | + cred->cr_uid = make_kuid(userns, svc_getnl(argv)); /* uid */ |
|---|
| 826 | + cred->cr_gid = make_kgid(userns, svc_getnl(argv)); /* gid */ |
|---|
| 813 | 827 | slen = svc_getnl(argv); /* gids length */ |
|---|
| 814 | 828 | if (slen > UNX_NGROUPS || (len -= (slen + 2)*4) < 0) |
|---|
| 815 | 829 | goto badcred; |
|---|
| .. | .. |
|---|
| 817 | 831 | if (cred->cr_group_info == NULL) |
|---|
| 818 | 832 | return SVC_CLOSE; |
|---|
| 819 | 833 | for (i = 0; i < slen; i++) { |
|---|
| 820 | | - kgid_t kgid = make_kgid(&init_user_ns, svc_getnl(argv)); |
|---|
| 834 | + kgid_t kgid = make_kgid(userns, svc_getnl(argv)); |
|---|
| 821 | 835 | cred->cr_group_info->gid[i] = kgid; |
|---|
| 822 | 836 | } |
|---|
| 823 | 837 | groups_sort(cred->cr_group_info); |
|---|
| .. | .. |
|---|
| 869 | 883 | .hash_size = IP_HASHMAX, |
|---|
| 870 | 884 | .name = "auth.unix.ip", |
|---|
| 871 | 885 | .cache_put = ip_map_put, |
|---|
| 886 | + .cache_upcall = ip_map_upcall, |
|---|
| 872 | 887 | .cache_request = ip_map_request, |
|---|
| 873 | 888 | .cache_parse = ip_map_parse, |
|---|
| 874 | 889 | .cache_show = ip_map_show, |
|---|