netfilter: flowtable: ensure sufficient headroom in xmit path
Subsystems (2)
PATCH 1/1 netfilter: flowtable: ensure sufficient headroom in xmit path
commit ef4f741e8627512cb8c82f59a1fc7aacd854aadf upstream.
Check for headroom and call skb_expand_head() like in the IP output path to ensure there is sufficient headroom for the mac header when forwarding this packet as suggested by sashiko.
[ aq: the upstream commit does not apply to 6.6: 6.6 predates
b5964aac51e0, and FLOW_OFFLOAD_XMIT_NEIGH flows are still sent through neigh_xmit() from nf_flow_offload_ip_hook() and nf_flow_offload_ipv6_hook(). Do the same headroom check there, before skb->dev is switched to the output device, and in nf_flow_queue_xmit().
Without it, a forwarded skb reaches the output device's header_ops->create() with only the headroom it was received with. A UDP socket behind a veth (e.g. a container) leaves 16 bytes; virtio_net leaves 26 for frames larger than GOOD_COPY_LEN. GRE, ip6gre and VLAN have since been hardened individually, 6LoWPAN has not:
lowpan_header_create() stores struct lowpan_addr_info (32 bytes) in front of skb->data after a mere WARN_ON_ONCE(), i.e. an out-of-bounds write before the skb head. Seen on v6.6.162 with IPv6 forwarded by a flowtable from a veth to a 6LoWPAN interface:
WARNING: CPU: 1 PID: 3974 at net/ieee802154/6lowpan/tx.c:21 lowpan_header_create+0x165/0x1b0 [ieee802154_6lowpan]
Call Trace:
neigh_connected_output+0xa6/0xf0
neigh_xmit+0xb4/0x1e0
nf_flow_offload_ipv6_hook+0x544/0x7c0
nf_hook_slow+0x39/0xe0
__netif_receive_skb_core.constprop.0+0xd8a/0xf40
__netif_receive_skb_one_core+0x37/0xa0
process_backlog+0x75/0x130 Before e67c577d8989 ("ipv4: ip_gre: make ipgre_header() robust") the same path panicked with multipoint GRE (DMVPN) output devices:
skb_under_panic in ipgre_header() via nf_flow_offload_ip_hook(). ]
---
Reproducer (run on v6.6.162: WARNING without this patch, clean with it). Needs CONFIG_MAC802154_HWSIM and CONFIG_IEEE802154_6LOWPAN, plus nft, conntrack and python3. The 6LoWPAN neighbour is static and the conntrack entry is inserted with SEEN_REPLY, so the very first packet matches "ct state established" and the flow is offloaded; no 802.15.4 peer is needed. If udev renames wpan0, use the name of the first ARPHRD_IEEE802154 (type 804) interface. Run it in a throwaway VM.
modprobe mac802154_hwsim; modprobe ieee802154_6lowpan
sysctl -w net.ipv6.conf.all.forwarding=1
ip link set wpan0 up
ip link add link wpan0 name lowpan0 type lowpan
ip link set lowpan0 up
ip -6 addr add fd00:b::1/64 dev lowpan0 nodad
ip -6 neigh replace fd00:b::2 lladdr 02:11:22:33:44:55:66:77 dev lowpan0 nud permanent
ip netns add lan
ip link add lan0 type veth peer name lan1 netns lan ip link set lan0 up
ip -6 addr add fd00:a::1/64 dev lan0 nodad ip -n lan link set lan1 up
ip -n lan -6 addr add fd00:a::2/64 dev lan1 nodad ip -n lan -6 route add default via fd00:a::1
nft add table inet t
nft 'add flowtable inet t ft { hook ingress priority filter; devices = { lan0, lowpan0 }; }' nft 'add chain inet t forward { type filter hook forward priority filter; policy accept; }' nft add rule inet t forward ct state established flow add @ft
conntrack -I -p udp -s fd00:a::2 -d fd00:b::2 --sport 40000 --dport 5000 \
-r fd00:b::2 -q fd00:a::2 --reply-port-src 5000 --reply-port-dst 40000 \
-t 120 -u SEEN_REPLY,ASSURED
ip netns exec lan python3 -c 'import socket; s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM); s.bind(("fd00:a::2", 40000)); [s.sendto(b"x" * 64, ("fd00:b::2", 5000)) for _ in range(100)]' dmesg | grep -A12 lowpan_header_create
Without this patch: the WARNING above. With it: none, while the flow is still offloaded ("conntrack -L" shows [OFFLOAD]). Replacing "flow add @ft" with "accept" also avoids it, because ip6_finish_output2() expands the headroom.
net/netfilter/nf_flow_table_ip.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
net/netfilter/nf_flow_table_ip.c +26 -0
@@ -330,6 +330,22 @@ } } +/* Make room for the link layer header of @dev, like ip_finish_output2() does. +* The flowtable fast path skips the IP output path, and a device with +* header_ops (e.g. an NBMA GRE tunnel) may need more headroom than the skb +* was received with. On failure the skb has been freed. +*/ +static struct sk_buff *nf_flow_xmit_headroom (struct sk_buff *skb, +struct net_device *dev) +{ +unsigned int hh_len =LL_RESERVED_SPACE (dev); + +if (unlikely (skb_headroom (skb) < hh_len && dev->header_ops)) + skb =skb_expand_head (skb, hh_len); + +return skb; +} +static unsigned int nf_flow_queue_xmit (struct net *net,struct sk_buff *skb,const struct flow_offload_tuple_rhash *tuplehash,unsigned short type)
@@ -340,6 +356,10 @@if (!outdev)return NF_DROP ; + skb =nf_flow_xmit_headroom (skb, outdev); +if (!skb) +return NF_STOLEN ; + skb->dev = outdev;dev_hard_header (skb, skb->dev, type, tuplehash->tuple.out.h_dest, tuplehash->tuple.out.h_source, skb->len);
@@ -452,6 +472,9 @@case FLOW_OFFLOAD_XMIT_NEIGH : rt = (struct rtable *)tuplehash->tuple.dst_cache; outdev = rt->dst.dev; + skb =nf_flow_xmit_headroom (skb, outdev); +if (!skb) +return NF_STOLEN ; skb->dev = outdev; nexthop =rt_nexthop (rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);skb_dst_drop (skb);
@@ -748,6 +771,9 @@case FLOW_OFFLOAD_XMIT_NEIGH : rt =dst_rt6_info (tuplehash->tuple.dst_cache); outdev = rt->dst.dev; + skb =nf_flow_xmit_headroom (skb, outdev); +if (!skb) +return NF_STOLEN ; skb->dev = outdev; nexthop =rt6_nexthop (rt, &flow->tuplehash[!dir].tuple.src_v6);skb_dst_drop (skb);