1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/types.h> 3 #include <linux/sched.h> 4 #include <linux/module.h> 5 #include <linux/sunrpc/types.h> 6 #include <linux/sunrpc/xdr.h> 7 #include <linux/sunrpc/svcsock.h> 8 #include <linux/sunrpc/svcauth.h> 9 #include <linux/sunrpc/gss_api.h> 10 #include <linux/sunrpc/addr.h> 11 #include <linux/err.h> 12 #include <linux/seq_file.h> 13 #include <linux/hash.h> 14 #include <linux/string.h> 15 #include <linux/slab.h> 16 #include <net/sock.h> 17 #include <net/ipv6.h> 18 #include <linux/kernel.h> 19 #include <linux/user_namespace.h> 20 #include <net/genetlink.h> 21 #include <uapi/linux/sunrpc_netlink.h> 22 #include <trace/events/sunrpc.h> 23 24 #define RPCDBG_FACILITY RPCDBG_AUTH 25 26 #include "netns.h" 27 #include "netlink.h" 28 29 /* 30 * AUTHUNIX and AUTHNULL credentials are both handled here. 31 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid 32 * are always nobody (-2). i.e. we do the same IP address checks for 33 * AUTHNULL as for AUTHUNIX, and that is done here. 34 */ 35 36 37 struct unix_domain { 38 struct auth_domain h; 39 /* other stuff later */ 40 }; 41 42 extern struct auth_ops svcauth_null; 43 extern struct auth_ops svcauth_unix; 44 extern struct auth_ops svcauth_tls; 45 46 static void svcauth_unix_domain_release_rcu(struct rcu_head *head) 47 { 48 struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head); 49 struct unix_domain *ud = container_of(dom, struct unix_domain, h); 50 51 kfree(dom->name); 52 kfree(ud); 53 } 54 55 static void svcauth_unix_domain_release(struct auth_domain *dom) 56 { 57 call_rcu(&dom->rcu_head, svcauth_unix_domain_release_rcu); 58 } 59 60 struct auth_domain *unix_domain_find(char *name) 61 { 62 struct auth_domain *rv; 63 struct unix_domain *new = NULL; 64 65 rv = auth_domain_find(name); 66 while(1) { 67 if (rv) { 68 if (new && rv != &new->h) 69 svcauth_unix_domain_release(&new->h); 70 71 if (rv->flavour != &svcauth_unix) { 72 auth_domain_put(rv); 73 return NULL; 74 } 75 return rv; 76 } 77 78 new = kmalloc_obj(*new); 79 if (new == NULL) 80 return NULL; 81 kref_init(&new->h.ref); 82 new->h.name = kstrdup(name, GFP_KERNEL); 83 if (new->h.name == NULL) { 84 kfree(new); 85 return NULL; 86 } 87 new->h.flavour = &svcauth_unix; 88 rv = auth_domain_lookup(name, &new->h); 89 } 90 } 91 EXPORT_SYMBOL_GPL(unix_domain_find); 92 93 94 /************************************************** 95 * cache for IP address to unix_domain 96 * as needed by AUTH_UNIX 97 */ 98 #define IP_HASHBITS 8 99 #define IP_HASHMAX (1<<IP_HASHBITS) 100 101 struct ip_map { 102 struct cache_head h; 103 char m_class[8]; /* e.g. "nfsd" */ 104 struct in6_addr m_addr; 105 struct unix_domain *m_client; 106 struct rcu_head m_rcu; 107 }; 108 109 static void ip_map_put(struct kref *kref) 110 { 111 struct cache_head *item = container_of(kref, struct cache_head, ref); 112 struct ip_map *im = container_of(item, struct ip_map,h); 113 114 if (test_bit(CACHE_VALID, &item->flags) && 115 !test_bit(CACHE_NEGATIVE, &item->flags)) 116 auth_domain_put(&im->m_client->h); 117 kfree_rcu(im, m_rcu); 118 } 119 120 static inline int hash_ip6(const struct in6_addr *ip) 121 { 122 return hash_32(ipv6_addr_hash(ip), IP_HASHBITS); 123 } 124 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew) 125 { 126 struct ip_map *orig = container_of(corig, struct ip_map, h); 127 struct ip_map *new = container_of(cnew, struct ip_map, h); 128 return strcmp(orig->m_class, new->m_class) == 0 && 129 ipv6_addr_equal(&orig->m_addr, &new->m_addr); 130 } 131 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem) 132 { 133 struct ip_map *new = container_of(cnew, struct ip_map, h); 134 struct ip_map *item = container_of(citem, struct ip_map, h); 135 136 strscpy(new->m_class, item->m_class); 137 new->m_addr = item->m_addr; 138 } 139 static void update(struct cache_head *cnew, struct cache_head *citem) 140 { 141 struct ip_map *new = container_of(cnew, struct ip_map, h); 142 struct ip_map *item = container_of(citem, struct ip_map, h); 143 144 kref_get(&item->m_client->h.ref); 145 new->m_client = item->m_client; 146 } 147 static struct cache_head *ip_map_alloc(void) 148 { 149 struct ip_map *i = kmalloc_obj(*i); 150 if (i) 151 return &i->h; 152 else 153 return NULL; 154 } 155 156 static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h) 157 { 158 return sunrpc_cache_upcall(cd, h); 159 } 160 161 static void ip_map_request(struct cache_detail *cd, 162 struct cache_head *h, 163 char **bpp, int *blen) 164 { 165 char text_addr[40]; 166 struct ip_map *im = container_of(h, struct ip_map, h); 167 168 if (ipv6_addr_v4mapped(&(im->m_addr))) { 169 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]); 170 } else { 171 snprintf(text_addr, 40, "%pI6", &im->m_addr); 172 } 173 qword_add(bpp, blen, im->m_class); 174 qword_add(bpp, blen, text_addr); 175 (*bpp)[-1] = '\n'; 176 } 177 178 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr); 179 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time64_t expiry); 180 181 static int ip_map_parse(struct cache_detail *cd, 182 char *mesg, int mlen) 183 { 184 /* class ipaddress [domainname] */ 185 /* should be safe just to use the start of the input buffer 186 * for scratch: */ 187 char *buf = mesg; 188 int len; 189 char class[8]; 190 union { 191 struct sockaddr sa; 192 struct sockaddr_in s4; 193 struct sockaddr_in6 s6; 194 } address; 195 struct sockaddr_in6 sin6; 196 int err; 197 198 struct ip_map *ipmp; 199 struct auth_domain *dom; 200 time64_t expiry; 201 202 if (mesg[mlen-1] != '\n') 203 return -EINVAL; 204 mesg[mlen-1] = 0; 205 206 /* class */ 207 len = qword_get(&mesg, class, sizeof(class)); 208 if (len <= 0) return -EINVAL; 209 210 /* ip address */ 211 len = qword_get(&mesg, buf, mlen); 212 if (len <= 0) return -EINVAL; 213 214 if (rpc_pton(cd->net, buf, len, &address.sa, sizeof(address)) == 0) 215 return -EINVAL; 216 switch (address.sa.sa_family) { 217 case AF_INET: 218 /* Form a mapped IPv4 address in sin6 */ 219 sin6.sin6_family = AF_INET6; 220 ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr, 221 &sin6.sin6_addr); 222 break; 223 #if IS_ENABLED(CONFIG_IPV6) 224 case AF_INET6: 225 memcpy(&sin6, &address.s6, sizeof(sin6)); 226 break; 227 #endif 228 default: 229 return -EINVAL; 230 } 231 232 err = get_expiry(&mesg, &expiry); 233 if (err) 234 return err; 235 236 /* domainname, or empty for NEGATIVE */ 237 len = qword_get(&mesg, buf, mlen); 238 if (len < 0) return -EINVAL; 239 240 if (len) { 241 dom = unix_domain_find(buf); 242 if (dom == NULL) 243 return -ENOENT; 244 } else 245 dom = NULL; 246 247 /* IPv6 scope IDs are ignored for now */ 248 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr); 249 if (ipmp) { 250 err = __ip_map_update(cd, ipmp, 251 container_of(dom, struct unix_domain, h), 252 expiry); 253 } else 254 err = -ENOMEM; 255 256 if (dom) 257 auth_domain_put(dom); 258 259 cache_flush(); 260 return err; 261 } 262 263 static int ip_map_show(struct seq_file *m, 264 struct cache_detail *cd, 265 struct cache_head *h) 266 { 267 struct ip_map *im; 268 struct in6_addr addr; 269 char *dom = "-no-domain-"; 270 271 if (h == NULL) { 272 seq_puts(m, "#class IP domain\n"); 273 return 0; 274 } 275 im = container_of(h, struct ip_map, h); 276 /* class addr domain */ 277 addr = im->m_addr; 278 279 if (test_bit(CACHE_VALID, &h->flags) && 280 !test_bit(CACHE_NEGATIVE, &h->flags)) 281 dom = im->m_client->h.name; 282 283 if (ipv6_addr_v4mapped(&addr)) { 284 seq_printf(m, "%s %pI4 %s\n", 285 im->m_class, &addr.s6_addr32[3], dom); 286 } else { 287 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom); 288 } 289 return 0; 290 } 291 292 293 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, 294 struct in6_addr *addr) 295 { 296 struct ip_map ip; 297 struct cache_head *ch; 298 299 strscpy(ip.m_class, class); 300 ip.m_addr = *addr; 301 ch = sunrpc_cache_lookup_rcu(cd, &ip.h, 302 hash_str(class, IP_HASHBITS) ^ 303 hash_ip6(addr)); 304 305 if (ch) 306 return container_of(ch, struct ip_map, h); 307 else 308 return NULL; 309 } 310 311 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, 312 struct unix_domain *udom, time64_t expiry) 313 { 314 struct ip_map ip; 315 struct cache_head *ch; 316 317 ip.m_client = udom; 318 ip.h.flags = 0; 319 if (!udom) 320 set_bit(CACHE_NEGATIVE, &ip.h.flags); 321 ip.h.expiry_time = expiry; 322 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h, 323 hash_str(ipm->m_class, IP_HASHBITS) ^ 324 hash_ip6(&ipm->m_addr)); 325 if (!ch) 326 return -ENOMEM; 327 cache_put(ch, cd); 328 return 0; 329 } 330 331 void svcauth_unix_purge(struct net *net) 332 { 333 struct sunrpc_net *sn; 334 335 sn = net_generic(net, sunrpc_net_id); 336 cache_purge(sn->ip_map_cache); 337 } 338 EXPORT_SYMBOL_GPL(svcauth_unix_purge); 339 340 static inline struct ip_map * 341 ip_map_cached_get(struct svc_xprt *xprt) 342 { 343 struct ip_map *ipm = NULL; 344 struct sunrpc_net *sn; 345 346 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) { 347 spin_lock(&xprt->xpt_lock); 348 ipm = xprt->xpt_auth_cache; 349 if (ipm != NULL) { 350 sn = net_generic(xprt->xpt_net, sunrpc_net_id); 351 if (cache_is_expired(sn->ip_map_cache, &ipm->h)) { 352 /* 353 * The entry has been invalidated since it was 354 * remembered, e.g. by a second mount from the 355 * same IP address. 356 */ 357 xprt->xpt_auth_cache = NULL; 358 spin_unlock(&xprt->xpt_lock); 359 cache_put(&ipm->h, sn->ip_map_cache); 360 return NULL; 361 } 362 cache_get(&ipm->h); 363 } 364 spin_unlock(&xprt->xpt_lock); 365 } 366 return ipm; 367 } 368 369 static inline void 370 ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm) 371 { 372 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) { 373 spin_lock(&xprt->xpt_lock); 374 if (xprt->xpt_auth_cache == NULL) { 375 /* newly cached, keep the reference */ 376 xprt->xpt_auth_cache = ipm; 377 ipm = NULL; 378 } 379 spin_unlock(&xprt->xpt_lock); 380 } 381 if (ipm) { 382 struct sunrpc_net *sn; 383 384 sn = net_generic(xprt->xpt_net, sunrpc_net_id); 385 cache_put(&ipm->h, sn->ip_map_cache); 386 } 387 } 388 389 void 390 svcauth_unix_info_release(struct svc_xprt *xpt) 391 { 392 struct ip_map *ipm; 393 394 ipm = xpt->xpt_auth_cache; 395 if (ipm != NULL) { 396 struct sunrpc_net *sn; 397 398 sn = net_generic(xpt->xpt_net, sunrpc_net_id); 399 cache_put(&ipm->h, sn->ip_map_cache); 400 } 401 } 402 403 /**************************************************************************** 404 * auth.unix.gid cache 405 * simple cache to map a UID to a list of GIDs 406 * because AUTH_UNIX aka AUTH_SYS has a max of UNX_NGROUPS 407 */ 408 #define GID_HASHBITS 8 409 #define GID_HASHMAX (1<<GID_HASHBITS) 410 411 struct unix_gid { 412 struct cache_head h; 413 kuid_t uid; 414 struct group_info *gi; 415 struct rcu_head rcu; 416 }; 417 418 static int unix_gid_hash(kuid_t uid) 419 { 420 return hash_long(from_kuid(&init_user_ns, uid), GID_HASHBITS); 421 } 422 423 static void unix_gid_free(struct rcu_head *rcu) 424 { 425 struct unix_gid *ug = container_of(rcu, struct unix_gid, rcu); 426 struct cache_head *item = &ug->h; 427 428 if (test_bit(CACHE_VALID, &item->flags) && 429 !test_bit(CACHE_NEGATIVE, &item->flags)) 430 put_group_info(ug->gi); 431 kfree(ug); 432 } 433 434 static void unix_gid_put(struct kref *kref) 435 { 436 struct cache_head *item = container_of(kref, struct cache_head, ref); 437 struct unix_gid *ug = container_of(item, struct unix_gid, h); 438 439 call_rcu(&ug->rcu, unix_gid_free); 440 } 441 442 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew) 443 { 444 struct unix_gid *orig = container_of(corig, struct unix_gid, h); 445 struct unix_gid *new = container_of(cnew, struct unix_gid, h); 446 return uid_eq(orig->uid, new->uid); 447 } 448 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem) 449 { 450 struct unix_gid *new = container_of(cnew, struct unix_gid, h); 451 struct unix_gid *item = container_of(citem, struct unix_gid, h); 452 new->uid = item->uid; 453 } 454 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem) 455 { 456 struct unix_gid *new = container_of(cnew, struct unix_gid, h); 457 struct unix_gid *item = container_of(citem, struct unix_gid, h); 458 459 get_group_info(item->gi); 460 new->gi = item->gi; 461 } 462 static struct cache_head *unix_gid_alloc(void) 463 { 464 struct unix_gid *g = kmalloc_obj(*g); 465 if (g) 466 return &g->h; 467 else 468 return NULL; 469 } 470 471 static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h) 472 { 473 return sunrpc_cache_upcall_warn(cd, h); 474 } 475 476 static void unix_gid_request(struct cache_detail *cd, 477 struct cache_head *h, 478 char **bpp, int *blen) 479 { 480 char tuid[20]; 481 struct unix_gid *ug = container_of(h, struct unix_gid, h); 482 483 snprintf(tuid, 20, "%u", from_kuid(&init_user_ns, ug->uid)); 484 qword_add(bpp, blen, tuid); 485 (*bpp)[-1] = '\n'; 486 } 487 488 static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid); 489 490 static int unix_gid_parse(struct cache_detail *cd, 491 char *mesg, int mlen) 492 { 493 /* uid expiry Ngid gid0 gid1 ... gidN-1 */ 494 int id; 495 kuid_t uid; 496 int gids; 497 int rv; 498 int i; 499 int err; 500 time64_t expiry; 501 struct unix_gid ug, *ugp; 502 503 if (mesg[mlen - 1] != '\n') 504 return -EINVAL; 505 mesg[mlen-1] = 0; 506 507 rv = get_int(&mesg, &id); 508 if (rv) 509 return -EINVAL; 510 uid = make_kuid(current_user_ns(), id); 511 ug.uid = uid; 512 513 err = get_expiry(&mesg, &expiry); 514 if (err) 515 return err; 516 517 rv = get_int(&mesg, &gids); 518 if (rv || gids < 0 || gids > 8192) 519 return -EINVAL; 520 521 ug.gi = groups_alloc(gids); 522 if (!ug.gi) 523 return -ENOMEM; 524 525 for (i = 0 ; i < gids ; i++) { 526 int gid; 527 kgid_t kgid; 528 rv = get_int(&mesg, &gid); 529 err = -EINVAL; 530 if (rv) 531 goto out; 532 kgid = make_kgid(current_user_ns(), gid); 533 if (!gid_valid(kgid)) 534 goto out; 535 ug.gi->gid[i] = kgid; 536 } 537 538 groups_sort(ug.gi); 539 ugp = unix_gid_lookup(cd, uid); 540 if (ugp) { 541 struct cache_head *ch; 542 ug.h.flags = 0; 543 /* 544 * mountd sends at least the user's primary group on 545 * success, so an empty list can only mean the lookup 546 * failed. Keep the credential's own groups instead. 547 */ 548 if (gids == 0) 549 set_bit(CACHE_NEGATIVE, &ug.h.flags); 550 ug.h.expiry_time = expiry; 551 ch = sunrpc_cache_update(cd, 552 &ug.h, &ugp->h, 553 unix_gid_hash(uid)); 554 if (!ch) 555 err = -ENOMEM; 556 else { 557 err = 0; 558 cache_put(ch, cd); 559 } 560 } else 561 err = -ENOMEM; 562 out: 563 if (ug.gi) 564 put_group_info(ug.gi); 565 return err; 566 } 567 568 static int unix_gid_show(struct seq_file *m, 569 struct cache_detail *cd, 570 struct cache_head *h) 571 { 572 struct user_namespace *user_ns = m->file->f_cred->user_ns; 573 struct unix_gid *ug; 574 int i; 575 int glen; 576 577 if (h == NULL) { 578 seq_puts(m, "#uid cnt: gids...\n"); 579 return 0; 580 } 581 ug = container_of(h, struct unix_gid, h); 582 if (test_bit(CACHE_VALID, &h->flags) && 583 !test_bit(CACHE_NEGATIVE, &h->flags)) 584 glen = ug->gi->ngroups; 585 else 586 glen = 0; 587 588 seq_printf(m, "%u %d:", from_kuid_munged(user_ns, ug->uid), glen); 589 for (i = 0; i < glen; i++) 590 seq_printf(m, " %d", from_kgid_munged(user_ns, ug->gi->gid[i])); 591 seq_printf(m, "\n"); 592 return 0; 593 } 594 595 static int unix_gid_notify(struct cache_detail *cd, struct cache_head *h) 596 { 597 return sunrpc_cache_notify(cd, h, SUNRPC_CACHE_TYPE_UNIX_GID); 598 } 599 600 /** 601 * sunrpc_nl_unix_gid_get_reqs_dumpit - dump pending unix_gid requests 602 * @skb: reply buffer 603 * @cb: netlink metadata and command arguments 604 * 605 * Walk the unix_gid cache's pending request list and create a netlink 606 * message with a nested entry for each cache_request, containing the 607 * seqno and uid. 608 * 609 * Uses cb->args[0] as a seqno cursor for dump continuation across 610 * multiple netlink messages. 611 * 612 * Returns the size of the reply or a negative errno. 613 */ 614 int sunrpc_nl_unix_gid_get_reqs_dumpit(struct sk_buff *skb, 615 struct netlink_callback *cb) 616 { 617 struct sunrpc_net *sn; 618 struct cache_detail *cd; 619 struct cache_head **items; 620 u64 *seqnos; 621 int cnt, i, emitted; 622 void *hdr; 623 int ret; 624 625 sn = net_generic(sock_net(skb->sk), sunrpc_net_id); 626 627 cd = sn->unix_gid_cache; 628 if (!cd) 629 return -ENODEV; 630 631 cnt = sunrpc_cache_requests_count(cd); 632 if (!cnt) 633 return 0; 634 635 items = kzalloc_objs(*items, cnt); 636 seqnos = kcalloc(cnt, sizeof(*seqnos), GFP_KERNEL); 637 if (!items || !seqnos) { 638 ret = -ENOMEM; 639 goto out_alloc; 640 } 641 642 cnt = sunrpc_cache_requests_snapshot(cd, items, seqnos, cnt, 643 cb->args[0]); 644 if (!cnt) { 645 ret = 0; 646 goto out_alloc; 647 } 648 649 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 650 cb->nlh->nlmsg_seq, &sunrpc_nl_family, 651 NLM_F_MULTI, SUNRPC_CMD_UNIX_GID_GET_REQS); 652 if (!hdr) { 653 ret = -ENOBUFS; 654 goto out_put; 655 } 656 657 emitted = 0; 658 for (i = 0; i < cnt; i++) { 659 struct unix_gid *ug; 660 struct nlattr *nest; 661 662 ug = container_of(items[i], struct unix_gid, h); 663 664 nest = nla_nest_start(skb, 665 SUNRPC_A_UNIX_GID_REQS_REQUESTS); 666 if (!nest) 667 break; 668 669 if (nla_put_u64_64bit(skb, SUNRPC_A_UNIX_GID_SEQNO, 670 seqnos[i], 0) || 671 nla_put_u32(skb, SUNRPC_A_UNIX_GID_UID, 672 from_kuid(&init_user_ns, ug->uid))) { 673 nla_nest_cancel(skb, nest); 674 break; 675 } 676 677 nla_nest_end(skb, nest); 678 cb->args[0] = seqnos[i]; 679 emitted++; 680 } 681 682 if (!emitted) { 683 genlmsg_cancel(skb, hdr); 684 ret = -EMSGSIZE; 685 goto out_put; 686 } 687 688 genlmsg_end(skb, hdr); 689 ret = skb->len; 690 out_put: 691 for (i = 0; i < cnt; i++) 692 cache_put(items[i], cd); 693 out_alloc: 694 kfree(seqnos); 695 kfree(items); 696 return ret; 697 } 698 699 /** 700 * sunrpc_nl_parse_one_unix_gid - parse one unix_gid entry from netlink 701 * @cd: cache_detail for the unix_gid cache 702 * @attr: nested attribute containing unix_gid fields 703 * 704 * Parses one unix_gid entry from a netlink message and updates the 705 * cache. Mirrors the logic in unix_gid_parse(). 706 * 707 * Returns 0 on success or a negative errno. 708 */ 709 static int sunrpc_nl_parse_one_unix_gid(struct cache_detail *cd, 710 struct nlattr *attr) 711 { 712 struct nlattr *tb[SUNRPC_A_UNIX_GID_EXPIRY + 1]; 713 struct unix_gid ug, *ugp; 714 struct timespec64 boot; 715 struct nlattr *gid_attr; 716 int err, rem, gids = 0; 717 kuid_t uid; 718 719 err = nla_parse_nested(tb, SUNRPC_A_UNIX_GID_EXPIRY, attr, 720 sunrpc_unix_gid_nl_policy, NULL); 721 if (err) 722 return err; 723 724 /* uid (required) */ 725 if (!tb[SUNRPC_A_UNIX_GID_UID]) 726 return -EINVAL; 727 uid = make_kuid(current_user_ns(), 728 nla_get_u32(tb[SUNRPC_A_UNIX_GID_UID])); 729 ug.uid = uid; 730 731 /* expiry (required, wallclock seconds) */ 732 if (!tb[SUNRPC_A_UNIX_GID_EXPIRY]) 733 return -EINVAL; 734 getboottime64(&boot); 735 ug.h.flags = 0; 736 ug.h.expiry_time = nla_get_u64(tb[SUNRPC_A_UNIX_GID_EXPIRY]) - 737 boot.tv_sec; 738 739 if (tb[SUNRPC_A_UNIX_GID_NEGATIVE]) { 740 /* failed lookup: keep the credential's own groups */ 741 set_bit(CACHE_NEGATIVE, &ug.h.flags); 742 ug.gi = groups_alloc(0); 743 if (!ug.gi) 744 return -ENOMEM; 745 } else { 746 /* Count gids */ 747 nla_for_each_nested_type(gid_attr, SUNRPC_A_UNIX_GID_GIDS, 748 attr, rem) 749 gids++; 750 751 if (gids > 8192) 752 return -EINVAL; 753 754 ug.gi = groups_alloc(gids); 755 if (!ug.gi) 756 return -ENOMEM; 757 758 gids = 0; 759 nla_for_each_nested_type(gid_attr, SUNRPC_A_UNIX_GID_GIDS, 760 attr, rem) { 761 kgid_t kgid; 762 763 kgid = make_kgid(current_user_ns(), 764 nla_get_u32(gid_attr)); 765 if (!gid_valid(kgid)) { 766 err = -EINVAL; 767 goto out; 768 } 769 ug.gi->gid[gids++] = kgid; 770 } 771 groups_sort(ug.gi); 772 } 773 774 ugp = unix_gid_lookup(cd, uid); 775 if (ugp) { 776 struct cache_head *ch; 777 778 ch = sunrpc_cache_update(cd, &ug.h, &ugp->h, 779 unix_gid_hash(uid)); 780 if (!ch) { 781 err = -ENOMEM; 782 } else { 783 err = 0; 784 cache_put(ch, cd); 785 } 786 } else { 787 err = -ENOMEM; 788 } 789 out: 790 if (ug.gi) 791 put_group_info(ug.gi); 792 return err; 793 } 794 795 /** 796 * sunrpc_nl_unix_gid_set_reqs_doit - respond to unix_gid requests 797 * @skb: reply buffer 798 * @info: netlink metadata and command arguments 799 * 800 * Parse one or more unix_gid cache responses from userspace and 801 * update the unix_gid cache accordingly. 802 * 803 * Returns 0 on success or a negative errno. 804 */ 805 int sunrpc_nl_unix_gid_set_reqs_doit(struct sk_buff *skb, 806 struct genl_info *info) 807 { 808 struct sunrpc_net *sn; 809 struct cache_detail *cd; 810 const struct nlattr *attr; 811 int rem, ret = 0; 812 813 sn = net_generic(genl_info_net(info), sunrpc_net_id); 814 815 cd = sn->unix_gid_cache; 816 if (!cd) 817 return -ENODEV; 818 819 nlmsg_for_each_attr_type(attr, SUNRPC_A_UNIX_GID_REQS_REQUESTS, 820 info->nlhdr, GENL_HDRLEN, rem) { 821 ret = sunrpc_nl_parse_one_unix_gid(cd, 822 (struct nlattr *)attr); 823 if (ret) 824 break; 825 } 826 827 return ret; 828 } 829 830 /** 831 * sunrpc_nl_cache_flush_doit - flush sunrpc caches via netlink 832 * @skb: reply buffer 833 * @info: netlink metadata and command arguments 834 * 835 * Flush the ip_map and/or unix_gid caches. If SUNRPC_A_CACHE_FLUSH_MASK 836 * is provided, only flush the caches indicated by the bitmask (bit 1 = 837 * ip_map, bit 2 = unix_gid). If omitted, flush both. 838 * 839 * Return 0 on success or a negative errno. 840 */ 841 int sunrpc_nl_cache_flush_doit(struct sk_buff *skb, struct genl_info *info) 842 { 843 struct sunrpc_net *sn; 844 u32 mask = ~0U; 845 846 sn = net_generic(genl_info_net(info), sunrpc_net_id); 847 848 if (info->attrs[SUNRPC_A_CACHE_FLUSH_MASK]) 849 mask = nla_get_u32(info->attrs[SUNRPC_A_CACHE_FLUSH_MASK]); 850 851 if ((mask & SUNRPC_CACHE_TYPE_IP_MAP) && 852 sn->ip_map_cache) 853 cache_purge(sn->ip_map_cache); 854 855 if ((mask & SUNRPC_CACHE_TYPE_UNIX_GID) && 856 sn->unix_gid_cache) 857 cache_purge(sn->unix_gid_cache); 858 859 return 0; 860 } 861 862 static const struct cache_detail unix_gid_cache_template = { 863 .owner = THIS_MODULE, 864 .hash_size = GID_HASHMAX, 865 .name = "auth.unix.gid", 866 .cache_put = unix_gid_put, 867 .cache_upcall = unix_gid_upcall, 868 .cache_notify = unix_gid_notify, 869 .cache_request = unix_gid_request, 870 .cache_parse = unix_gid_parse, 871 .cache_show = unix_gid_show, 872 .match = unix_gid_match, 873 .init = unix_gid_init, 874 .update = unix_gid_update, 875 .alloc = unix_gid_alloc, 876 }; 877 878 int unix_gid_cache_create(struct net *net) 879 { 880 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 881 struct cache_detail *cd; 882 int err; 883 884 cd = cache_create_net(&unix_gid_cache_template, net); 885 if (IS_ERR(cd)) 886 return PTR_ERR(cd); 887 err = cache_register_net(cd, net); 888 if (err) { 889 cache_destroy_net(cd, net); 890 return err; 891 } 892 sn->unix_gid_cache = cd; 893 return 0; 894 } 895 896 void unix_gid_cache_destroy(struct net *net) 897 { 898 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 899 struct cache_detail *cd = sn->unix_gid_cache; 900 901 sn->unix_gid_cache = NULL; 902 cache_purge(cd); 903 cache_unregister_net(cd, net); 904 cache_destroy_net(cd, net); 905 } 906 907 static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid) 908 { 909 struct unix_gid ug; 910 struct cache_head *ch; 911 912 ug.uid = uid; 913 ch = sunrpc_cache_lookup_rcu(cd, &ug.h, unix_gid_hash(uid)); 914 if (ch) 915 return container_of(ch, struct unix_gid, h); 916 else 917 return NULL; 918 } 919 920 static struct group_info *unix_gid_find(kuid_t uid, struct svc_rqst *rqstp) 921 { 922 struct unix_gid *ug; 923 struct group_info *gi; 924 int ret; 925 struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, 926 sunrpc_net_id); 927 928 ug = unix_gid_lookup(sn->unix_gid_cache, uid); 929 if (!ug) 930 return ERR_PTR(-EAGAIN); 931 ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle); 932 switch (ret) { 933 case -ENOENT: 934 return ERR_PTR(-ENOENT); 935 case -ETIMEDOUT: 936 return ERR_PTR(-ESHUTDOWN); 937 case 0: 938 gi = get_group_info(ug->gi); 939 cache_put(&ug->h, sn->unix_gid_cache); 940 return gi; 941 default: 942 return ERR_PTR(-EAGAIN); 943 } 944 } 945 946 enum svc_auth_status 947 svcauth_unix_set_client(struct svc_rqst *rqstp) 948 { 949 struct sockaddr_in *sin; 950 struct sockaddr_in6 *sin6, sin6_storage; 951 struct ip_map *ipm; 952 struct group_info *gi; 953 struct svc_cred *cred = &rqstp->rq_cred; 954 struct svc_xprt *xprt = rqstp->rq_xprt; 955 struct net *net = xprt->xpt_net; 956 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 957 958 switch (rqstp->rq_addr.ss_family) { 959 case AF_INET: 960 sin = svc_addr_in(rqstp); 961 sin6 = &sin6_storage; 962 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr); 963 break; 964 case AF_INET6: 965 sin6 = svc_addr_in6(rqstp); 966 break; 967 default: 968 BUG(); 969 } 970 971 rqstp->rq_client = NULL; 972 if (rqstp->rq_proc == 0) 973 goto out; 974 975 rqstp->rq_auth_stat = rpc_autherr_badcred; 976 ipm = ip_map_cached_get(xprt); 977 if (ipm == NULL) 978 ipm = __ip_map_lookup(sn->ip_map_cache, 979 rqstp->rq_server->sv_programs->pg_class, 980 &sin6->sin6_addr); 981 982 if (ipm == NULL) 983 return SVC_DENIED; 984 985 switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) { 986 default: 987 BUG(); 988 case -ETIMEDOUT: 989 return SVC_CLOSE; 990 case -EAGAIN: 991 return SVC_DROP; 992 case -ENOENT: 993 return SVC_DENIED; 994 case 0: 995 rqstp->rq_client = &ipm->m_client->h; 996 kref_get(&rqstp->rq_client->ref); 997 ip_map_cached_put(xprt, ipm); 998 break; 999 } 1000 1001 gi = unix_gid_find(cred->cr_uid, rqstp); 1002 switch (PTR_ERR(gi)) { 1003 case -EAGAIN: 1004 return SVC_DROP; 1005 case -ESHUTDOWN: 1006 return SVC_CLOSE; 1007 case -ENOENT: 1008 break; 1009 default: 1010 put_group_info(cred->cr_group_info); 1011 cred->cr_group_info = gi; 1012 } 1013 1014 out: 1015 rqstp->rq_auth_stat = rpc_auth_ok; 1016 return SVC_OK; 1017 } 1018 EXPORT_SYMBOL_GPL(svcauth_unix_set_client); 1019 1020 /** 1021 * svcauth_null_accept - Decode and validate incoming RPC_AUTH_NULL credential 1022 * @rqstp: RPC transaction 1023 * 1024 * Return values: 1025 * %SVC_OK: Both credential and verifier are valid 1026 * %SVC_DENIED: Credential or verifier is not valid 1027 * %SVC_GARBAGE: Failed to decode credential or verifier 1028 * %SVC_CLOSE: Temporary failure 1029 * 1030 * rqstp->rq_auth_stat is set as mandated by RFC 5531. 1031 */ 1032 static enum svc_auth_status 1033 svcauth_null_accept(struct svc_rqst *rqstp) 1034 { 1035 struct xdr_stream *xdr = &rqstp->rq_arg_stream; 1036 struct svc_cred *cred = &rqstp->rq_cred; 1037 u32 flavor, len; 1038 void *body; 1039 1040 /* Length of Call's credential body field: */ 1041 if (xdr_stream_decode_u32(xdr, &len) < 0) 1042 return SVC_GARBAGE; 1043 if (len != 0) { 1044 rqstp->rq_auth_stat = rpc_autherr_badcred; 1045 return SVC_DENIED; 1046 } 1047 1048 /* Call's verf field: */ 1049 if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0) 1050 return SVC_GARBAGE; 1051 if (flavor != RPC_AUTH_NULL || len != 0) { 1052 rqstp->rq_auth_stat = rpc_autherr_badverf; 1053 return SVC_DENIED; 1054 } 1055 1056 /* Signal that mapping to nobody uid/gid is required */ 1057 cred->cr_uid = INVALID_UID; 1058 cred->cr_gid = INVALID_GID; 1059 cred->cr_group_info = groups_alloc(0); 1060 if (cred->cr_group_info == NULL) 1061 return SVC_CLOSE; /* kmalloc failure - client must retry */ 1062 1063 if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream, 1064 RPC_AUTH_NULL, NULL, 0) < 0) 1065 return SVC_CLOSE; 1066 if (!svcxdr_set_accept_stat(rqstp)) 1067 return SVC_CLOSE; 1068 1069 rqstp->rq_cred.cr_flavor = RPC_AUTH_NULL; 1070 return SVC_OK; 1071 } 1072 1073 static int 1074 svcauth_null_release(struct svc_rqst *rqstp) 1075 { 1076 if (rqstp->rq_client) 1077 auth_domain_put(rqstp->rq_client); 1078 rqstp->rq_client = NULL; 1079 if (rqstp->rq_cred.cr_group_info) 1080 put_group_info(rqstp->rq_cred.cr_group_info); 1081 rqstp->rq_cred.cr_group_info = NULL; 1082 1083 return 0; /* don't drop */ 1084 } 1085 1086 1087 struct auth_ops svcauth_null = { 1088 .name = "null", 1089 .owner = THIS_MODULE, 1090 .flavour = RPC_AUTH_NULL, 1091 .accept = svcauth_null_accept, 1092 .release = svcauth_null_release, 1093 .set_client = svcauth_unix_set_client, 1094 }; 1095 1096 1097 /** 1098 * svcauth_tls_accept - Decode and validate incoming RPC_AUTH_TLS credential 1099 * @rqstp: RPC transaction 1100 * 1101 * Return values: 1102 * %SVC_OK: Both credential and verifier are valid 1103 * %SVC_DENIED: Credential or verifier is not valid 1104 * %SVC_GARBAGE: Failed to decode credential or verifier 1105 * %SVC_CLOSE: Temporary failure 1106 * 1107 * rqstp->rq_auth_stat is set as mandated by RFC 5531. 1108 */ 1109 static enum svc_auth_status 1110 svcauth_tls_accept(struct svc_rqst *rqstp) 1111 { 1112 struct xdr_stream *xdr = &rqstp->rq_arg_stream; 1113 struct svc_cred *cred = &rqstp->rq_cred; 1114 struct svc_xprt *xprt = rqstp->rq_xprt; 1115 u32 flavor, len; 1116 void *body; 1117 __be32 *p; 1118 1119 /* Length of Call's credential body field: */ 1120 if (xdr_stream_decode_u32(xdr, &len) < 0) 1121 return SVC_GARBAGE; 1122 if (len != 0) { 1123 rqstp->rq_auth_stat = rpc_autherr_badcred; 1124 return SVC_DENIED; 1125 } 1126 1127 /* Call's verf field: */ 1128 if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0) 1129 return SVC_GARBAGE; 1130 if (flavor != RPC_AUTH_NULL || len != 0) { 1131 rqstp->rq_auth_stat = rpc_autherr_badverf; 1132 return SVC_DENIED; 1133 } 1134 1135 /* AUTH_TLS is not valid on non-NULL procedures */ 1136 if (rqstp->rq_proc != 0) { 1137 rqstp->rq_auth_stat = rpc_autherr_badcred; 1138 return SVC_DENIED; 1139 } 1140 1141 /* Signal that mapping to nobody uid/gid is required */ 1142 cred->cr_uid = INVALID_UID; 1143 cred->cr_gid = INVALID_GID; 1144 cred->cr_group_info = groups_alloc(0); 1145 if (cred->cr_group_info == NULL) 1146 return SVC_CLOSE; 1147 1148 if (xprt->xpt_ops->xpo_handshake) { 1149 p = xdr_reserve_space(&rqstp->rq_res_stream, XDR_UNIT * 2 + 8); 1150 if (!p) 1151 return SVC_CLOSE; 1152 trace_svc_tls_start(xprt); 1153 *p++ = rpc_auth_null; 1154 *p++ = cpu_to_be32(8); 1155 memcpy(p, "STARTTLS", 8); 1156 1157 set_bit(XPT_HANDSHAKE, &xprt->xpt_flags); 1158 svc_xprt_enqueue(xprt); 1159 } else { 1160 trace_svc_tls_unavailable(xprt); 1161 if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream, 1162 RPC_AUTH_NULL, NULL, 0) < 0) 1163 return SVC_CLOSE; 1164 } 1165 if (!svcxdr_set_accept_stat(rqstp)) 1166 return SVC_CLOSE; 1167 1168 rqstp->rq_cred.cr_flavor = RPC_AUTH_TLS; 1169 return SVC_OK; 1170 } 1171 1172 struct auth_ops svcauth_tls = { 1173 .name = "tls", 1174 .owner = THIS_MODULE, 1175 .flavour = RPC_AUTH_TLS, 1176 .accept = svcauth_tls_accept, 1177 .release = svcauth_null_release, 1178 .set_client = svcauth_unix_set_client, 1179 }; 1180 1181 1182 /** 1183 * svcauth_unix_accept - Decode and validate incoming RPC_AUTH_SYS credential 1184 * @rqstp: RPC transaction 1185 * 1186 * Return values: 1187 * %SVC_OK: Both credential and verifier are valid 1188 * %SVC_DENIED: Credential or verifier is not valid 1189 * %SVC_GARBAGE: Failed to decode credential or verifier 1190 * %SVC_CLOSE: Temporary failure 1191 * 1192 * rqstp->rq_auth_stat is set as mandated by RFC 5531. 1193 */ 1194 static enum svc_auth_status 1195 svcauth_unix_accept(struct svc_rqst *rqstp) 1196 { 1197 struct xdr_stream *xdr = &rqstp->rq_arg_stream; 1198 struct svc_cred *cred = &rqstp->rq_cred; 1199 struct user_namespace *userns; 1200 u32 flavor, len, i; 1201 void *body; 1202 __be32 *p; 1203 1204 /* 1205 * This implementation ignores the length of the Call's 1206 * credential body field and the timestamp and machinename 1207 * fields. 1208 */ 1209 p = xdr_inline_decode(xdr, XDR_UNIT * 3); 1210 if (!p) 1211 return SVC_GARBAGE; 1212 len = be32_to_cpup(p + 2); 1213 if (len > RPC_MAX_MACHINENAME) 1214 return SVC_GARBAGE; 1215 if (!xdr_inline_decode(xdr, len)) 1216 return SVC_GARBAGE; 1217 1218 /* 1219 * Note: we skip uid_valid()/gid_valid() checks here for 1220 * backwards compatibility with clients that use -1 id's. 1221 * Instead, -1 uid or gid is later mapped to the 1222 * (export-specific) anonymous id by nfsd_setuser. 1223 * Supplementary gid's will be left alone. 1224 */ 1225 userns = (rqstp->rq_xprt && rqstp->rq_xprt->xpt_cred) ? 1226 rqstp->rq_xprt->xpt_cred->user_ns : &init_user_ns; 1227 if (xdr_stream_decode_u32(xdr, &i) < 0) 1228 return SVC_GARBAGE; 1229 cred->cr_uid = make_kuid(userns, i); 1230 if (xdr_stream_decode_u32(xdr, &i) < 0) 1231 return SVC_GARBAGE; 1232 cred->cr_gid = make_kgid(userns, i); 1233 1234 if (xdr_stream_decode_u32(xdr, &len) < 0) 1235 return SVC_GARBAGE; 1236 if (len > UNX_NGROUPS) 1237 goto badcred; 1238 p = xdr_inline_decode(xdr, XDR_UNIT * len); 1239 if (!p) 1240 return SVC_GARBAGE; 1241 cred->cr_group_info = groups_alloc(len); 1242 if (cred->cr_group_info == NULL) 1243 return SVC_CLOSE; 1244 for (i = 0; i < len; i++) { 1245 kgid_t kgid = make_kgid(userns, be32_to_cpup(p++)); 1246 cred->cr_group_info->gid[i] = kgid; 1247 } 1248 groups_sort(cred->cr_group_info); 1249 1250 /* Call's verf field: */ 1251 if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0) 1252 return SVC_GARBAGE; 1253 if (flavor != RPC_AUTH_NULL || len != 0) { 1254 rqstp->rq_auth_stat = rpc_autherr_badverf; 1255 return SVC_DENIED; 1256 } 1257 1258 if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream, 1259 RPC_AUTH_NULL, NULL, 0) < 0) 1260 return SVC_CLOSE; 1261 if (!svcxdr_set_accept_stat(rqstp)) 1262 return SVC_CLOSE; 1263 1264 rqstp->rq_cred.cr_flavor = RPC_AUTH_UNIX; 1265 return SVC_OK; 1266 1267 badcred: 1268 rqstp->rq_auth_stat = rpc_autherr_badcred; 1269 return SVC_DENIED; 1270 } 1271 1272 static int 1273 svcauth_unix_release(struct svc_rqst *rqstp) 1274 { 1275 /* Verifier (such as it is) is already in place. 1276 */ 1277 if (rqstp->rq_client) 1278 auth_domain_put(rqstp->rq_client); 1279 rqstp->rq_client = NULL; 1280 if (rqstp->rq_cred.cr_group_info) 1281 put_group_info(rqstp->rq_cred.cr_group_info); 1282 rqstp->rq_cred.cr_group_info = NULL; 1283 1284 return 0; 1285 } 1286 1287 1288 struct auth_ops svcauth_unix = { 1289 .name = "unix", 1290 .owner = THIS_MODULE, 1291 .flavour = RPC_AUTH_UNIX, 1292 .accept = svcauth_unix_accept, 1293 .release = svcauth_unix_release, 1294 .domain_release = svcauth_unix_domain_release, 1295 .set_client = svcauth_unix_set_client, 1296 }; 1297 1298 static int ip_map_notify(struct cache_detail *cd, struct cache_head *h) 1299 { 1300 return sunrpc_cache_notify(cd, h, SUNRPC_CACHE_TYPE_IP_MAP); 1301 } 1302 1303 /** 1304 * sunrpc_nl_ip_map_get_reqs_dumpit - dump pending ip_map requests 1305 * @skb: reply buffer 1306 * @cb: netlink metadata and command arguments 1307 * 1308 * Walk the ip_map cache's pending request list and create a netlink 1309 * message with a nested entry for each cache_request, containing the 1310 * seqno, class and addr. 1311 * 1312 * Uses cb->args[0] as a seqno cursor for dump continuation across 1313 * multiple netlink messages. 1314 * 1315 * Returns the size of the reply or a negative errno. 1316 */ 1317 int sunrpc_nl_ip_map_get_reqs_dumpit(struct sk_buff *skb, 1318 struct netlink_callback *cb) 1319 { 1320 struct sunrpc_net *sn; 1321 struct cache_detail *cd; 1322 struct cache_head **items; 1323 u64 *seqnos; 1324 int cnt, i, emitted; 1325 void *hdr; 1326 int ret; 1327 1328 sn = net_generic(sock_net(skb->sk), sunrpc_net_id); 1329 1330 cd = sn->ip_map_cache; 1331 if (!cd) 1332 return -ENODEV; 1333 1334 cnt = sunrpc_cache_requests_count(cd); 1335 if (!cnt) 1336 return 0; 1337 1338 items = kzalloc_objs(*items, cnt); 1339 seqnos = kcalloc(cnt, sizeof(*seqnos), GFP_KERNEL); 1340 if (!items || !seqnos) { 1341 ret = -ENOMEM; 1342 goto out_alloc; 1343 } 1344 1345 cnt = sunrpc_cache_requests_snapshot(cd, items, seqnos, cnt, 1346 cb->args[0]); 1347 if (!cnt) { 1348 ret = 0; 1349 goto out_alloc; 1350 } 1351 1352 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 1353 cb->nlh->nlmsg_seq, &sunrpc_nl_family, 1354 NLM_F_MULTI, SUNRPC_CMD_IP_MAP_GET_REQS); 1355 if (!hdr) { 1356 ret = -ENOBUFS; 1357 goto out_put; 1358 } 1359 1360 emitted = 0; 1361 for (i = 0; i < cnt; i++) { 1362 struct ip_map *im; 1363 struct nlattr *nest; 1364 char text_addr[40]; 1365 1366 im = container_of(items[i], struct ip_map, h); 1367 1368 if (ipv6_addr_v4mapped(&im->m_addr)) 1369 snprintf(text_addr, 20, "%pI4", 1370 &im->m_addr.s6_addr32[3]); 1371 else 1372 snprintf(text_addr, 40, "%pI6", &im->m_addr); 1373 1374 nest = nla_nest_start(skb, SUNRPC_A_IP_MAP_REQS_REQUESTS); 1375 if (!nest) 1376 break; 1377 1378 if (nla_put_u64_64bit(skb, SUNRPC_A_IP_MAP_SEQNO, 1379 seqnos[i], 0) || 1380 nla_put_string(skb, SUNRPC_A_IP_MAP_CLASS, 1381 im->m_class) || 1382 nla_put_string(skb, SUNRPC_A_IP_MAP_ADDR, text_addr)) { 1383 nla_nest_cancel(skb, nest); 1384 break; 1385 } 1386 1387 nla_nest_end(skb, nest); 1388 cb->args[0] = seqnos[i]; 1389 emitted++; 1390 } 1391 1392 if (!emitted) { 1393 genlmsg_cancel(skb, hdr); 1394 ret = -EMSGSIZE; 1395 goto out_put; 1396 } 1397 1398 genlmsg_end(skb, hdr); 1399 ret = skb->len; 1400 out_put: 1401 for (i = 0; i < cnt; i++) 1402 cache_put(items[i], cd); 1403 out_alloc: 1404 kfree(seqnos); 1405 kfree(items); 1406 return ret; 1407 } 1408 1409 /** 1410 * sunrpc_nl_parse_one_ip_map - parse one ip_map entry from netlink 1411 * @cd: cache_detail for the ip_map cache 1412 * @attr: nested attribute containing ip_map fields 1413 * 1414 * Parses one ip_map entry from a netlink message and updates the 1415 * cache. Mirrors the logic in ip_map_parse(). 1416 * 1417 * Returns 0 on success or a negative errno. 1418 */ 1419 static int sunrpc_nl_parse_one_ip_map(struct cache_detail *cd, 1420 struct nlattr *attr) 1421 { 1422 struct nlattr *tb[SUNRPC_A_IP_MAP_EXPIRY + 1]; 1423 union { 1424 struct sockaddr sa; 1425 struct sockaddr_in s4; 1426 struct sockaddr_in6 s6; 1427 } address; 1428 struct sockaddr_in6 sin6; 1429 struct ip_map *ipmp; 1430 struct auth_domain *dom = NULL; 1431 struct unix_domain *udom = NULL; 1432 struct timespec64 boot; 1433 time64_t expiry; 1434 char class[8]; 1435 int err; 1436 int len; 1437 1438 err = nla_parse_nested(tb, SUNRPC_A_IP_MAP_EXPIRY, attr, 1439 sunrpc_ip_map_nl_policy, NULL); 1440 if (err) 1441 return err; 1442 1443 /* class (required) */ 1444 if (!tb[SUNRPC_A_IP_MAP_CLASS]) 1445 return -EINVAL; 1446 len = nla_len(tb[SUNRPC_A_IP_MAP_CLASS]); 1447 if (len <= 0 || len > sizeof(class)) 1448 return -EINVAL; 1449 nla_strscpy(class, tb[SUNRPC_A_IP_MAP_CLASS], sizeof(class)); 1450 1451 /* addr (required) */ 1452 if (!tb[SUNRPC_A_IP_MAP_ADDR]) 1453 return -EINVAL; 1454 if (rpc_pton(cd->net, nla_data(tb[SUNRPC_A_IP_MAP_ADDR]), 1455 nla_len(tb[SUNRPC_A_IP_MAP_ADDR]) - 1, 1456 &address.sa, sizeof(address)) == 0) 1457 return -EINVAL; 1458 1459 switch (address.sa.sa_family) { 1460 case AF_INET: 1461 sin6.sin6_family = AF_INET6; 1462 ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr, 1463 &sin6.sin6_addr); 1464 break; 1465 #if IS_ENABLED(CONFIG_IPV6) 1466 case AF_INET6: 1467 memcpy(&sin6, &address.s6, sizeof(sin6)); 1468 break; 1469 #endif 1470 default: 1471 return -EINVAL; 1472 } 1473 1474 /* expiry (required, wallclock seconds) */ 1475 if (!tb[SUNRPC_A_IP_MAP_EXPIRY]) 1476 return -EINVAL; 1477 getboottime64(&boot); 1478 expiry = nla_get_u64(tb[SUNRPC_A_IP_MAP_EXPIRY]) - boot.tv_sec; 1479 1480 /* domain name or negative */ 1481 if (tb[SUNRPC_A_IP_MAP_NEGATIVE]) { 1482 udom = NULL; 1483 } else if (tb[SUNRPC_A_IP_MAP_DOMAIN]) { 1484 dom = unix_domain_find(nla_data(tb[SUNRPC_A_IP_MAP_DOMAIN])); 1485 if (!dom) 1486 return -ENOENT; 1487 udom = container_of(dom, struct unix_domain, h); 1488 } else { 1489 return -EINVAL; 1490 } 1491 1492 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr); 1493 if (ipmp) 1494 err = __ip_map_update(cd, ipmp, udom, expiry); 1495 else 1496 err = -ENOMEM; 1497 1498 if (dom) 1499 auth_domain_put(dom); 1500 1501 cache_flush(); 1502 return err; 1503 } 1504 1505 /** 1506 * sunrpc_nl_ip_map_set_reqs_doit - respond to ip_map requests 1507 * @skb: reply buffer 1508 * @info: netlink metadata and command arguments 1509 * 1510 * Parse one or more ip_map cache responses from userspace and 1511 * update the ip_map cache accordingly. 1512 * 1513 * Returns 0 on success or a negative errno. 1514 */ 1515 int sunrpc_nl_ip_map_set_reqs_doit(struct sk_buff *skb, 1516 struct genl_info *info) 1517 { 1518 struct sunrpc_net *sn; 1519 struct cache_detail *cd; 1520 const struct nlattr *attr; 1521 int rem, ret = 0; 1522 1523 sn = net_generic(genl_info_net(info), sunrpc_net_id); 1524 1525 cd = sn->ip_map_cache; 1526 if (!cd) 1527 return -ENODEV; 1528 1529 nlmsg_for_each_attr_type(attr, SUNRPC_A_IP_MAP_REQS_REQUESTS, 1530 info->nlhdr, GENL_HDRLEN, rem) { 1531 ret = sunrpc_nl_parse_one_ip_map(cd, 1532 (struct nlattr *)attr); 1533 if (ret) 1534 break; 1535 } 1536 1537 return ret; 1538 } 1539 1540 static const struct cache_detail ip_map_cache_template = { 1541 .owner = THIS_MODULE, 1542 .hash_size = IP_HASHMAX, 1543 .name = "auth.unix.ip", 1544 .cache_put = ip_map_put, 1545 .cache_upcall = ip_map_upcall, 1546 .cache_notify = ip_map_notify, 1547 .cache_request = ip_map_request, 1548 .cache_parse = ip_map_parse, 1549 .cache_show = ip_map_show, 1550 .match = ip_map_match, 1551 .init = ip_map_init, 1552 .update = update, 1553 .alloc = ip_map_alloc, 1554 }; 1555 1556 int ip_map_cache_create(struct net *net) 1557 { 1558 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1559 struct cache_detail *cd; 1560 int err; 1561 1562 cd = cache_create_net(&ip_map_cache_template, net); 1563 if (IS_ERR(cd)) 1564 return PTR_ERR(cd); 1565 err = cache_register_net(cd, net); 1566 if (err) { 1567 cache_destroy_net(cd, net); 1568 return err; 1569 } 1570 sn->ip_map_cache = cd; 1571 return 0; 1572 } 1573 1574 void ip_map_cache_destroy(struct net *net) 1575 { 1576 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1577 struct cache_detail *cd = sn->ip_map_cache; 1578 1579 sn->ip_map_cache = NULL; 1580 cache_purge(cd); 1581 cache_unregister_net(cd, net); 1582 cache_destroy_net(cd, net); 1583 } 1584