Ethernet Bridge development
 help / color / mirror / Atom feed
* [PATCH nf 0/1] netfilter: bridge: detach conntrack template on invalid IP frames
@ 2026-11-23  3:11 Ren Wei
  2026-11-23  3:11 ` [PATCH nf 1/1] " Ren Wei
  0 siblings, 1 reply; 2+ messages in thread
From: Ren Wei @ 2026-11-23  3:11 UTC (permalink / raw)
  To: netfilter-devel, bridge
  Cc: pablo, fw, phil, razor, idosch, davem, vega, sashiko-bot,
	rakukuip, weir

From: Luxiao Xu <rakukuip@gmail.com>

Hi Linux kernel maintainers,

We found and validated an issue in
net/bridge/netfilter/nf_conntrack_bridge.c where malformed IPv4 and IPv6
frames retain an attached conntrack template on early rejection paths in
nf_ct_bridge_pre().

This bug is tracked at: https://bugtracker.nebusec.ai/f/11085

Reachability & impact:
When a bridge policy assigns a conntrack zone before connection tracking
(for example via nftables `ct zone set` at raw priority -300), nftables
attaches a conntrack template with IP_CT_NEW to skb->_nfct. If an
incoming ETH_P_IP or ETH_P_IPV6 frame fails header pull, length
trimming, or header validation in nf_ct_bridge_pre(), nf_ct_bridge_pre()
returns NF_ACCEPT directly without passing the skb to nf_conntrack_in().

Because nft_ct_get_eval() evaluates NFT_CT_STATE before checking
nf_ct_is_template(ct), the retained IP_CT_NEW template causes malformed,
truncated, or wrong-version IP frames to match `ct state new` instead of
`ct state invalid`. Bridge policies that accept NEW traffic and intend to
drop INVALID traffic can consequently forward malformed frames.

Note on related nf-next work:
Florian Westphal's nf-next proposal "netfilter: conntrack: add and use
nf_ct_get_real()" (see link below) would prevent downstream consumers
such as nft_ct_get_eval() from classifying a retained template as NEW,
treating it as INVALID instead. Because nf_ct_get_real() is not in nf or
-stable kernels, and because nf_ct_bridge_pre() should not leave an
unconsumed template attached to skb->_nfct on parse failure (matching
nf_conntrack_in() and the default non-IP branch), releasing the template
in nf_ct_bridge_pre() remains necessary.
https://lore.kernel.org/netfilter-devel/20260923122741.30468-1-fw@strlen.de/

The attached reproducer (poc.sh) uses a netdev ingress rule on vethA to
rewrite the IPv4 version nibble from 0x45 to 0x65 (running as root in
our guest environment); an external or bridge peer can also inject
malformed frames directly over the wire without host privileges.

=== details below ===

Bug details:

In net/bridge/netfilter/nf_conntrack_bridge.c, nf_ct_bridge_pre() has
six early-return paths across the ETH_P_IP and ETH_P_IPV6 cases:
1. ETH_P_IP:
   - !pskb_may_pull(skb, sizeof(struct iphdr))
   - pskb_trim_rcsum(skb, len)
   - nf_ct_br_ip_check(skb)
2. ETH_P_IPV6:
   - !pskb_may_pull(skb, sizeof(struct ipv6hdr))
   - pskb_trim_rcsum(skb, len)
   - nf_ct_br_ipv6_check(skb)

Unlike the default non-IP branch (which calls nf_reset_ct(skb)) and
nf_conntrack_in() (which clears skb->_nfct = 0 and drops the template
reference on parse failure), all six IPv4/IPv6 rejection paths return
NF_ACCEPT with the template still attached to skb->_nfct.

The fix jumps all six paths to a shared `invalid:` label that calls
nf_reset_ct(skb) before returning NF_ACCEPT, dropping the template
reference and restoring skb->_nfct to 0 so that subsequent ct state
evaluation classifies the frame as INVALID.

Testing & Validation:
We validated both the baseline reproduction and the fix in an x86_64
QEMU guest (2 vCPUs, 2GB RAM, CONFIG_KASAN=y, CONFIG_UBSAN=y,
CONFIG_LOCKDEP=y, CONFIG_NF_CONNTRACK_BRIDGE=y) built from nf/main
(734bef548a96).

1. Pre-fix verification (unpatched nf/main):
   Running `bash /home/test_user/poc.sh`:
   - [control-no-template]: seen=1, invalid=1, newpkts=0, mangled=1
   - [template-retained]:   seen=1, invalid=0, newpkts=1, mangled=1

2. Post-fix verification (patched kernel):
   Running `bash /root/poc.sh`:
   - [control-no-template]: seen=1, invalid=1, newpkts=0, mangled=1
   - [template-retained]:   seen=1, invalid=1, newpkts=0, mangled=1

3. Extended regression testing (with `ct zone set 1` active):
   - IPv4 short header (< 20B), inflated tot_len, tot_len < ihl*4,
     low IHL (0x44), wrong version (0x65): all matched invalid=1, new=0.
   - IPv6 short header (< 40B), wrong version (0x40), inflated
     payload_len: all matched invalid=1, new=0.
   - Non-IP ARP frame: matched untracked=1, invalid=0, new=0.
   - Normal IPv4 ping and IPv6 ping6 across bridge (between init_net
     and netns nsB): both succeeded with invalid=0, new=1,
     established=1.
   - 1000-packet malformed IPv4/IPv6 stress test: invalid=1000, new=0,
     with zero KASAN, UBSAN, Lockdep, or refcount warnings in dmesg.

4. Style & patch checks:
   - git diff --check HEAD~1: 0 issues
   - git apply --check on nf/main: clean
   - scripts/checkpatch.pl --strict: 0 errors, 1 warning (consecutive
     Reported-by lines where VEGA has no separate public report URL)

Reproducer:

    bash poc.sh

=== BEGIN poc.sh ===
#!/bin/bash
set -euo pipefail

PATH=/usr/sbin:/sbin:/usr/bin:/bin

TABLE=brtest
BR=br0
PORT_A=vethA
PORT_B=vethB
END_A=peerA
END_B=peerB
TARGET_IP=10.0.0.99
SOURCE_IP=10.0.0.1/24

cleanup() {
	nft flush ruleset >/dev/null 2>&1 || true
	ip route del "$TARGET_IP/32" dev "$END_A" >/dev/null 2>&1 || true
	ip neigh del "$TARGET_IP" dev "$END_A" >/dev/null 2>&1 || true
	ip link del "$PORT_A" >/dev/null 2>&1 || true
	ip link del "$PORT_B" >/dev/null 2>&1 || true
	ip link del "$BR" >/dev/null 2>&1 || true
}

need_root() {
	if [ "$(id -u)" -ne 0 ]; then
		echo "run as root, or use --userns from an unprivileged account" >&2
		exit 1
	fi
}

mac_of() {
	ip -o link show dev "$1" | awk '{for (i = 1; i <= NF; i++) if ($i == "link/ether") print $(i + 1)}'
}

setup_topology() {
	ip link add "$BR" type bridge
	ip link add "$PORT_A" type veth peer name "$END_A"
	ip link add "$PORT_B" type veth peer name "$END_B"
	ip link set "$PORT_A" master "$BR"
	ip link set "$PORT_B" master "$BR"

	for dev in "$BR" "$PORT_A" "$PORT_B" "$END_A" "$END_B"; do
		ip link set "$dev" up
	done

	ip addr add "$SOURCE_IP" dev "$END_A"
	ip route add "$TARGET_IP/32" dev "$END_A"
	ip neigh add "$TARGET_IP" lladdr "$(mac_of "$END_B")" dev "$END_A" nud permanent
}

setup_mangler() {
	nft add table netdev m
	nft add counter netdev m mangled
	nft 'add chain netdev m ingress_vethA { type filter hook ingress device "vethA" priority 0; policy accept; }'
	nft add rule netdev m ingress_vethA meta protocol ip counter name mangled @nh,0,8 set 0x65
}

setup_observer() {
	nft add table bridge "$TABLE"
	for c in seen invalid newpkts established untracked other; do
		nft add counter bridge "$TABLE" "$c"
	done

	nft 'add chain bridge brtest observe { type filter hook prerouting priority 0; policy accept; }'
	nft add rule bridge "$TABLE" observe iifname "$PORT_A" meta protocol ip counter name seen
	nft add rule bridge "$TABLE" observe iifname "$PORT_A" meta protocol ip ct state invalid counter name invalid accept
	nft add rule bridge "$TABLE" observe iifname "$PORT_A" meta protocol ip ct state untracked counter name untracked accept
	nft add rule bridge "$TABLE" observe iifname "$PORT_A" meta protocol ip ct state new counter name newpkts accept
	nft add rule bridge "$TABLE" observe iifname "$PORT_A" meta protocol ip ct state established,related counter name established accept
	nft add rule bridge "$TABLE" observe iifname "$PORT_A" meta protocol ip counter name other
}

add_zone_template_rule() {
	nft 'add chain bridge brtest ctpre { type filter hook prerouting priority -300; policy accept; }'
	nft add rule bridge "$TABLE" ctpre iifname "$PORT_A" meta protocol ip ct zone set 1
}

reset_phase_counters() {
	nft reset counters table bridge "$TABLE"
	nft reset counters table netdev m
}

print_phase_counters() {
	echo "bridge counters:"
	nft list counters table bridge "$TABLE"
	echo "mangle counters:"
	nft list counters table netdev m
}

run_phase() {
	local label=$1

	echo "[$label] sending one ping after mangling the first IPv4 header byte to 0x65"
	ping -I "$END_A" -c 1 -W 1 "$TARGET_IP" >/dev/null || true
	echo "[$label] results"
	print_phase_counters
}

main_inner() {
	need_root
	trap cleanup EXIT
	cleanup

	setup_topology
	setup_observer
	setup_mangler

	run_phase "control-no-template"

	add_zone_template_rule
	reset_phase_counters
	run_phase "template-retained"
}

if [ "${1:-}" = "--userns" ]; then
	exec unshare -Urnm -- "$0" --inner
fi

if [ "${1:-}" = "--inner" ]; then
	shift
fi

main_inner "$@"
=== END poc.sh ===

=== BEGIN pre-fix output ===
[control-no-template] sending one ping after mangling the first IPv4 header byte to 0x65
[control-no-template] results
bridge counters:
table bridge brtest {
	counter seen {
		packets 1 bytes 84
	}
	counter invalid {
		packets 1 bytes 84
	}
	counter newpkts {
		packets 0 bytes 0
	}
	counter established {
		packets 0 bytes 0
	}
	counter untracked {
		packets 0 bytes 0
	}
	counter other {
		packets 0 bytes 0
	}
}
mangle counters:
table netdev m {
	counter mangled {
		packets 1 bytes 84
	}
}
[template-retained] sending one ping after mangling the first IPv4 header byte to 0x65
[template-retained] results
bridge counters:
table bridge brtest {
	counter seen {
		packets 1 bytes 84
	}
	counter invalid {
		packets 0 bytes 0
	}
	counter newpkts {
		packets 1 bytes 84
	}
	counter established {
		packets 0 bytes 0
	}
	counter untracked {
		packets 0 bytes 0
	}
	counter other {
		packets 0 bytes 0
	}
}
mangle counters:
table netdev m {
	counter mangled {
		packets 1 bytes 84
	}
}
=== END pre-fix output ===

=== BEGIN post-fix output ===
[control-no-template] sending one ping after mangling the first IPv4 header byte to 0x65
[control-no-template] results
bridge counters:
table bridge brtest {
	counter seen {
		packets 1 bytes 84
	}
	counter invalid {
		packets 1 bytes 84
	}
	counter newpkts {
		packets 0 bytes 0
	}
	counter established {
		packets 0 bytes 0
	}
	counter untracked {
		packets 0 bytes 0
	}
	counter other {
		packets 0 bytes 0
	}
}
mangle counters:
table netdev m {
	counter mangled {
		packets 1 bytes 84
	}
}
[template-retained] sending one ping after mangling the first IPv4 header byte to 0x65
[template-retained] results
bridge counters:
table bridge brtest {
	counter seen {
		packets 1 bytes 84
	}
	counter invalid {
		packets 1 bytes 84
	}
	counter newpkts {
		packets 0 bytes 0
	}
	counter established {
		packets 0 bytes 0
	}
	counter untracked {
		packets 0 bytes 0
	}
	counter other {
		packets 0 bytes 0
	}
}
mangle counters:
table netdev m {
	counter mangled {
		packets 1 bytes 84
	}
}
=== END post-fix output ===

Best regards,
Luxiao Xu

Luxiao Xu (1):
  netfilter: bridge: detach conntrack template on invalid IP frames

 net/bridge/netfilter/nf_conntrack_bridge.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH nf 1/1] netfilter: bridge: detach conntrack template on invalid IP frames
  2026-11-23  3:11 [PATCH nf 0/1] netfilter: bridge: detach conntrack template on invalid IP frames Ren Wei
@ 2026-11-23  3:11 ` Ren Wei
  0 siblings, 0 replies; 2+ messages in thread
From: Ren Wei @ 2026-11-23  3:11 UTC (permalink / raw)
  To: netfilter-devel, bridge
  Cc: pablo, fw, phil, razor, idosch, davem, vega, sashiko-bot,
	rakukuip, weir

From: Luxiao Xu <rakukuip@gmail.com>

A bridge rule such as nftables `ct zone set` can attach a conntrack
template with IP_CT_NEW to an skb before nf_ct_bridge_pre() runs. When
a frame has an ETH_P_IP or ETH_P_IPV6 protocol, nf_ct_bridge_pre()
performs header pull, length trimming, and nf_ct_br_ip_check() /
nf_ct_br_ipv6_check() validation before invoking defragmentation and
nf_conntrack_in().

If any of those six checks fails, nf_ct_bridge_pre() returns NF_ACCEPT
immediately without passing the skb to nf_conntrack_in(), which would
normally clear skb->_nfct and drop the template reference on parse
failure. Because nft_ct_get_eval() evaluates NFT_CT_STATE from ct and
ctinfo before rejecting template objects, the retained template causes
malformed, truncated, or wrong-version IP frames to match `ct state new`
instead of `ct state invalid`.

Jump to a shared error path that calls nf_reset_ct(skb) before returning
NF_ACCEPT on all IPv4 and IPv6 header/length rejection paths so the
template reference is released and the frame is treated as invalid,
even on kernels without downstream template filtering in ct readers.

Fixes: 3c171f496ef5 ("netfilter: bridge: add connection tracking system")
Cc: stable@vger.kernel.org
Reported-by: VEGA <vega@nebusec.ai>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/3cf187b612fd78f8835fa9dad870326ce380f82f.1785479514.git.zhilinz@nebusec.ai
Signed-off-by: Luxiao Xu <rakukuip@gmail.com>
Signed-off-by: Ren Wei <weir@nebusec.ai>
---
Note on related upstream work:
Florian Westphal's series "[v2,nf-next] netfilter: conntrack: add and
use nf_ct_get_real()" (linked below) mitigates the downstream symptom
by making ct state readers such as nft_ct_get_eval() treat retained
templates as NULL (INVALID rather than NEW). However, nf_ct_get_real()
targets nf-next and is not present in nf or -stable kernels, and
nf_ct_bridge_pre() still leaves an unconsumed template attached to
skb->_nfct on malformed IP frames until freed. Resetting ct state at
the rejection site in nf_ct_bridge_pre() aligns it with
nf_conntrack_in() and the existing non-IP default branch.
Link: https://lore.kernel.org/netfilter-devel/20260923122741.30468-1-fw@strlen.de/

 net/bridge/netfilter/nf_conntrack_bridge.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/net/bridge/netfilter/nf_conntrack_bridge.c b/net/bridge/netfilter/nf_conntrack_bridge.c
index b5444335b86f..0da0f899edb8 100644
--- a/net/bridge/netfilter/nf_conntrack_bridge.c
+++ b/net/bridge/netfilter/nf_conntrack_bridge.c
@@ -257,28 +257,28 @@ static unsigned int nf_ct_bridge_pre(void *priv, struct sk_buff *skb,
 	switch (skb->protocol) {
 	case htons(ETH_P_IP):
 		if (!pskb_may_pull(skb, sizeof(struct iphdr)))
-			return NF_ACCEPT;
+			goto invalid;
 
 		len = skb_ip_totlen(skb);
 		if (pskb_trim_rcsum(skb, len))
-			return NF_ACCEPT;
+			goto invalid;
 
 		if (nf_ct_br_ip_check(skb))
-			return NF_ACCEPT;
+			goto invalid;
 
 		bridge_state.pf = NFPROTO_IPV4;
 		ret = nf_ct_br_defrag4(skb, &bridge_state);
 		break;
 	case htons(ETH_P_IPV6):
 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
-			return NF_ACCEPT;
+			goto invalid;
 
 		len = sizeof(struct ipv6hdr) + skb_ipv6_payload_len(skb);
 		if (pskb_trim_rcsum(skb, len))
-			return NF_ACCEPT;
+			goto invalid;
 
 		if (nf_ct_br_ipv6_check(skb))
-			return NF_ACCEPT;
+			goto invalid;
 
 		bridge_state.pf = NFPROTO_IPV6;
 		ret = nf_ct_br_defrag6(skb, &bridge_state);
@@ -293,6 +293,10 @@ static unsigned int nf_ct_bridge_pre(void *priv, struct sk_buff *skb,
 		return ret;
 
 	return nf_conntrack_in(skb, &bridge_state);
+
+invalid:
+	nf_reset_ct(skb);
+	return NF_ACCEPT;
 }
 
 static unsigned int nf_ct_bridge_in(void *priv, struct sk_buff *skb,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23  3:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  3:11 [PATCH nf 0/1] netfilter: bridge: detach conntrack template on invalid IP frames Ren Wei
2026-11-23  3:11 ` [PATCH nf 1/1] " Ren Wei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox