Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: gro: fix misuse of CB in udp socket lookup

This patch fixes a misuse of IP{6}CB(skb) in GRO, while calling to
`udp6_lib_lookup2` when handling udp tunnels. `udp6_lib_lookup2` fetch the
device from CB. The fix changes it to fetch the device from `skb->dev`.
l3mdev case requires special attention since it has a master and a slave
device.

Fixes: a6024562ffd7 ("udp: Add GRO functions to UDP socket")
Reported-by: Gal Pressman <gal@nvidia.com>
Signed-off-by: Richard Gobert <richardbgobert@gmail.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Richard Gobert and committed by
David S. Miller
7938cd15 e346e231

+65 -8
+43
include/net/gro.h
··· 452 452 gro_normal_list(napi); 453 453 } 454 454 455 + /* This function is the alternative of 'inet_iif' and 'inet_sdif' 456 + * functions in case we can not rely on fields of IPCB. 457 + * 458 + * The caller must verify skb_valid_dst(skb) is false and skb->dev is initialized. 459 + * The caller must hold the RCU read lock. 460 + */ 461 + static inline void inet_get_iif_sdif(const struct sk_buff *skb, int *iif, int *sdif) 462 + { 463 + *iif = inet_iif(skb) ?: skb->dev->ifindex; 464 + *sdif = 0; 465 + 466 + #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 467 + if (netif_is_l3_slave(skb->dev)) { 468 + struct net_device *master = netdev_master_upper_dev_get_rcu(skb->dev); 469 + 470 + *sdif = *iif; 471 + *iif = master ? master->ifindex : 0; 472 + } 473 + #endif 474 + } 475 + 476 + /* This function is the alternative of 'inet6_iif' and 'inet6_sdif' 477 + * functions in case we can not rely on fields of IP6CB. 478 + * 479 + * The caller must verify skb_valid_dst(skb) is false and skb->dev is initialized. 480 + * The caller must hold the RCU read lock. 481 + */ 482 + static inline void inet6_get_iif_sdif(const struct sk_buff *skb, int *iif, int *sdif) 483 + { 484 + /* using skb->dev->ifindex because skb_dst(skb) is not initialized */ 485 + *iif = skb->dev->ifindex; 486 + *sdif = 0; 487 + 488 + #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 489 + if (netif_is_l3_slave(skb->dev)) { 490 + struct net_device *master = netdev_master_upper_dev_get_rcu(skb->dev); 491 + 492 + *sdif = *iif; 493 + *iif = master ? master->ifindex : 0; 494 + } 495 + #endif 496 + } 497 + 455 498 extern struct list_head offload_base; 456 499 457 500 #endif /* _NET_IPV6_GRO_H */
+6 -2
net/ipv4/udp.c
··· 114 114 #include <net/sock_reuseport.h> 115 115 #include <net/addrconf.h> 116 116 #include <net/udp_tunnel.h> 117 + #include <net/gro.h> 117 118 #if IS_ENABLED(CONFIG_IPV6) 118 119 #include <net/ipv6_stubs.h> 119 120 #endif ··· 556 555 { 557 556 const struct iphdr *iph = ip_hdr(skb); 558 557 struct net *net = dev_net(skb->dev); 558 + int iif, sdif; 559 + 560 + inet_get_iif_sdif(skb, &iif, &sdif); 559 561 560 562 return __udp4_lib_lookup(net, iph->saddr, sport, 561 - iph->daddr, dport, inet_iif(skb), 562 - inet_sdif(skb), net->ipv4.udp_table, NULL); 563 + iph->daddr, dport, iif, 564 + sdif, net->ipv4.udp_table, NULL); 563 565 } 564 566 565 567 /* Must be called under rcu_read_lock().
+5 -2
net/ipv4/udp_offload.c
··· 609 609 { 610 610 const struct iphdr *iph = skb_gro_network_header(skb); 611 611 struct net *net = dev_net(skb->dev); 612 + int iif, sdif; 613 + 614 + inet_get_iif_sdif(skb, &iif, &sdif); 612 615 613 616 return __udp4_lib_lookup(net, iph->saddr, sport, 614 - iph->daddr, dport, inet_iif(skb), 615 - inet_sdif(skb), net->ipv4.udp_table, NULL); 617 + iph->daddr, dport, iif, 618 + sdif, net->ipv4.udp_table, NULL); 616 619 } 617 620 618 621 INDIRECT_CALLABLE_SCOPE
+6 -2
net/ipv6/udp.c
··· 51 51 #include <net/inet6_hashtables.h> 52 52 #include <net/busy_poll.h> 53 53 #include <net/sock_reuseport.h> 54 + #include <net/gro.h> 54 55 55 56 #include <linux/proc_fs.h> 56 57 #include <linux/seq_file.h> ··· 301 300 { 302 301 const struct ipv6hdr *iph = ipv6_hdr(skb); 303 302 struct net *net = dev_net(skb->dev); 303 + int iif, sdif; 304 + 305 + inet6_get_iif_sdif(skb, &iif, &sdif); 304 306 305 307 return __udp6_lib_lookup(net, &iph->saddr, sport, 306 - &iph->daddr, dport, inet6_iif(skb), 307 - inet6_sdif(skb), net->ipv4.udp_table, NULL); 308 + &iph->daddr, dport, iif, 309 + sdif, net->ipv4.udp_table, NULL); 308 310 } 309 311 310 312 /* Must be called under rcu_read_lock().
+5 -2
net/ipv6/udp_offload.c
··· 118 118 { 119 119 const struct ipv6hdr *iph = skb_gro_network_header(skb); 120 120 struct net *net = dev_net(skb->dev); 121 + int iif, sdif; 122 + 123 + inet6_get_iif_sdif(skb, &iif, &sdif); 121 124 122 125 return __udp6_lib_lookup(net, &iph->saddr, sport, 123 - &iph->daddr, dport, inet6_iif(skb), 124 - inet6_sdif(skb), net->ipv4.udp_table, NULL); 126 + &iph->daddr, dport, iif, 127 + sdif, net->ipv4.udp_table, NULL); 125 128 } 126 129 127 130 INDIRECT_CALLABLE_SCOPE