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

netfilter: nf_reject: don't leak dst refcount for loopback packets

recent patches to add a WARN() when replacing skb dst entry found an
old bug:

WARNING: include/linux/skbuff.h:1165 skb_dst_check_unset include/linux/skbuff.h:1164 [inline]
WARNING: include/linux/skbuff.h:1165 skb_dst_set include/linux/skbuff.h:1210 [inline]
WARNING: include/linux/skbuff.h:1165 nf_reject_fill_skb_dst+0x2a4/0x330 net/ipv4/netfilter/nf_reject_ipv4.c:234
[..]
Call Trace:
nf_send_unreach+0x17b/0x6e0 net/ipv4/netfilter/nf_reject_ipv4.c:325
nft_reject_inet_eval+0x4bc/0x690 net/netfilter/nft_reject_inet.c:27
expr_call_ops_eval net/netfilter/nf_tables_core.c:237 [inline]
..

This is because blamed commit forgot about loopback packets.
Such packets already have a dst_entry attached, even at PRE_ROUTING stage.

Instead of checking hook just check if the skb already has a route
attached to it.

Fixes: f53b9b0bdc59 ("netfilter: introduce support for reject at prerouting stage")
Signed-off-by: Florian Westphal <fw@strlen.de>
Link: https://patch.msgid.link/20250820123707.10671-1-fw@strlen.de
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Florian Westphal and committed by
Jakub Kicinski
91a79b79 1b78236a

+4 -7
+2 -4
net/ipv4/netfilter/nf_reject_ipv4.c
··· 247 247 if (!oth) 248 248 return; 249 249 250 - if ((hook == NF_INET_PRE_ROUTING || hook == NF_INET_INGRESS) && 251 - nf_reject_fill_skb_dst(oldskb) < 0) 250 + if (!skb_dst(oldskb) && nf_reject_fill_skb_dst(oldskb) < 0) 252 251 return; 253 252 254 253 if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) ··· 320 321 if (iph->frag_off & htons(IP_OFFSET)) 321 322 return; 322 323 323 - if ((hook == NF_INET_PRE_ROUTING || hook == NF_INET_INGRESS) && 324 - nf_reject_fill_skb_dst(skb_in) < 0) 324 + if (!skb_dst(skb_in) && nf_reject_fill_skb_dst(skb_in) < 0) 325 325 return; 326 326 327 327 if (skb_csum_unnecessary(skb_in) ||
+2 -3
net/ipv6/netfilter/nf_reject_ipv6.c
··· 293 293 fl6.fl6_sport = otcph->dest; 294 294 fl6.fl6_dport = otcph->source; 295 295 296 - if (hook == NF_INET_PRE_ROUTING || hook == NF_INET_INGRESS) { 296 + if (!skb_dst(oldskb)) { 297 297 nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false); 298 298 if (!dst) 299 299 return; ··· 397 397 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL) 398 398 skb_in->dev = net->loopback_dev; 399 399 400 - if ((hooknum == NF_INET_PRE_ROUTING || hooknum == NF_INET_INGRESS) && 401 - nf_reject6_fill_skb_dst(skb_in) < 0) 400 + if (!skb_dst(skb_in) && nf_reject6_fill_skb_dst(skb_in) < 0) 402 401 return; 403 402 404 403 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);