Skip to contentSkip to conversation

Netfilter fixes for net

PATCH In review Pablo Neira Ayuso 2 participants 6 messages Aug 22, 2024

Subsystems (3)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover Netfilter fixes for net

Hi,

The following patchset contains Netfilter fixes for net:

Patch #1 disable BH when collecting stats via hardware offload to ensure
concurrent updates from packet path do not result in losing stats. From Sebastian Andrzej Siewior.

Patch #2 uses write seqcount to reset counters serialize against reader.
Also from Sebastian Andrzej Siewior.

Patch #3 ensures vlan header is in place before accessing its fields,
according to KMSAN splat triggered by syzbot.

Please, pull these changes from:

git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git nf-24-08-22

Thanks.

----------------------------------------------------------------

The following changes since commit 807067bf014d4a3ae2cc55bd3de16f22a01eb580:

kcm: Serialise kcm_sendmsg() for the same socket. (2024-08-19 18:36:12 -0700)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git tags/nf-24-08-22

for you to fetch changes up to 0509ac6c6a9a282ade4ad79b04665395691f73b1:

netfilter: flowtable: validate vlan header (2024-08-21 23:42:49 +0200)

----------------------------------------------------------------
netfilter pull request 24-08-22

----------------------------------------------------------------
Pablo Neira Ayuso (1):
netfilter: flowtable: validate vlan header

Sebastian Andrzej Siewior (2):
netfilter: nft_counter: Disable BH in nft_counter_offload_stats().
netfilter: nft_counter: Synchronize nft_counter_reset() against reader.

 net/netfilter/nf_flow_table_inet.c | 3 +++
 net/netfilter/nf_flow_table_ip.c   | 3 +++
 net/netfilter/nft_counter.c        | 9 +++++++--
 3 files changed, 13 insertions(+), 2 deletions(-)
author maintainer
#1 lore raw

PATCH 1/3 netfilter: nft_counter: Disable BH in nft_counter_offload_stats().

From: Sebastian Andrzej Siewior <[email protected]>

The sequence counter nft_counter_seq is a per-CPU counter. There is no lock associated with it. nft_counter_do_eval() is using the same counter and disables BH which suggest that it can be invoked from a softirq. This in turn means that nft_counter_offload_stats(), which disables only preemption, can be interrupted by nft_counter_do_eval() leading to two writer for one seqcount_t.
This can lead to loosing stats or reading statistics while they are updated.

Disable BH during stats update in nft_counter_offload_stats() to ensure one writer at a time.

Fixes b72920f6e4a9d ("netfilter: nftables: counter hardware offload support")
---
 net/netfilter/nft_counter.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nft_counter.c +2 -2
index 291ed2026367..16f40b503d37 100644
@@ -265,7 +265,7 @@ static void nft_counter_offload_stats(struct nft_expr *expr,
 	struct nft_counter *this_cpu;
 	seqcount_t *myseq;
 
-	preempt_disable();
+	local_bh_disable();
 	this_cpu = this_cpu_ptr(priv->counter);
 	myseq = this_cpu_ptr(&nft_counter_seq);
 
@@ -273,7 +273,7 @@ static void nft_counter_offload_stats(struct nft_expr *expr,
 	this_cpu->packets += stats->pkts;
 	this_cpu->bytes += stats->bytes;
 	write_seqcount_end(myseq);
-	preempt_enable();
+	local_bh_enable();
 }
 
 void nft_counter_init_seqcount(void)
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 2/3 netfilter: nft_counter: Synchronize nft_counter_reset() against reader.

In reply to Pablo Neira Ayuso

From: Sebastian Andrzej Siewior <[email protected]>

nft_counter_reset() resets the counter by subtracting the previously retrieved value from the counter. This is a write operation on the counter and as such it requires to be performed with a write sequence of nft_counter_seq to serialize against its possible reader.

Update the packets/ bytes within write-sequence of nft_counter_seq.

Fixes d84701ecbcd6a ("netfilter: nft_counter: rework atomic dump and reset")
---
 net/netfilter/nft_counter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/netfilter/nft_counter.c +5 -0
index 16f40b503d37..eab0dc66bee6 100644
@@ -107,11 +107,16 @@ static void nft_counter_reset(struct nft_counter_percpu_priv *priv,
 			      struct nft_counter *total)
 {
 	struct nft_counter *this_cpu;
+	seqcount_t *myseq;
 
 	local_bh_disable();
 	this_cpu = this_cpu_ptr(priv->counter);
+	myseq = this_cpu_ptr(&nft_counter_seq);
+
+	write_seqcount_begin(myseq);
 	this_cpu->packets -= total->packets;
 	this_cpu->bytes -= total->bytes;
+	write_seqcount_end(myseq);
 	local_bh_enable();
 }
 
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

PATCH 3/3 netfilter: flowtable: validate vlan header

In reply to Pablo Neira Ayuso

Ensure there is sufficient room to access the protocol field of the VLAN header, validate it once before the flowtable lookup.

=====================================================

BUG: KMSAN: uninit-value in nf_flow_offload_inet_hook+0x45a/0x5f0 net/netfilter/nf_flow_table_inet.c:32
 nf_flow_offload_inet_hook+0x45a/0x5f0 net/netfilter/nf_flow_table_inet.c:32
 nf_hook_entry_hookfn include/linux/netfilter.h:154 [inline]
 nf_hook_slow+0xf4/0x400 net/netfilter/core.c:626
 nf_hook_ingress include/linux/netfilter_netdev.h:34 [inline]
 nf_ingress net/core/dev.c:5440 [inline]
Fixes 4cd91f7c290f ("netfilter: flowtable: add vlan support")
Reported-by [email protected]
---
 net/netfilter/nf_flow_table_inet.c | 3 +++
 net/netfilter/nf_flow_table_ip.c   | 3 +++
 2 files changed, 6 insertions(+)
net/netfilter/nf_flow_table_inet.c +3 -0
index 88787b45e30d..dd9a392052ee 100644
@@ -17,6 +17,9 @@ nf_flow_offload_inet_hook(void *priv, struct sk_buff *skb,
 
 	switch (skb->protocol) {
 	case htons(ETH_P_8021Q):
+		if (!pskb_may_pull(skb, VLAN_HLEN))
+			return NF_ACCEPT;
+
 		veth = (struct vlan_ethhdr *)skb_mac_header(skb);
 		proto = veth->h_vlan_encapsulated_proto;
 		break;
net/netfilter/nf_flow_table_ip.c +3 -0
index c2c005234dcd..9a9031e9ea1c 100644
@@ -281,6 +281,9 @@ static bool nf_flow_skb_encap_protocol(struct sk_buff *skb, __be16 proto,
 
 	switch (skb->protocol) {
 	case htons(ETH_P_8021Q):
+		if (!pskb_may_pull(skb, VLAN_HLEN))
+			return false;
+
 		veth = (struct vlan_ethhdr *)skb_mac_header(skb);
 		if (veth->h_vlan_encapsulated_proto == proto) {
 			*offset += VLAN_HLEN;
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH net 3/3] netfilter: flowtable: validate vlan header

Quoted 25 lines · Pablo Neira Ayuso

Ensure there is sufficient room to access the protocol field of the VLAN header, validate it once before the flowtable lookup.

=====================================================

BUG: KMSAN: uninit-value in nf_flow_offload_inet_hook+0x45a/0x5f0 net/netfilter/nf_flow_table_inet.c:32
 nf_flow_offload_inet_hook+0x45a/0x5f0 net/netfilter/nf_flow_table_inet.c:32
 nf_hook_entry_hookfn include/linux/netfilter.h:154 [inline]
 nf_hook_slow+0xf4/0x400 net/netfilter/core.c:626
 nf_hook_ingress include/linux/netfilter_netdev.h:34 [inline]
 nf_ingress net/core/dev.c:5440 [inline]
Fixes 4cd91f7c290f ("netfilter: flowtable: add vlan support")
Reported-by [email protected]
---
 net/netfilter/nf_flow_table_inet.c | 3 +++
 net/netfilter/nf_flow_table_ip.c   | 3 +++
 2 files changed, 6 insertions(+)
net/netfilter/nf_flow_table_inet.c +0 -0
index 88787b45e30d..dd9a392052ee 100644

net/netfilter/nf_flow_table_inet.c nf_flow_offload_inet_hook(void *priv, struct sk_buff *skb, · line 23

Quoted 7 lines
net/netfilter/nf_flow_table_inet.c +3 -0
@@ -17,6 +17,9 @@ nf_flow_offload_inet_hook(void *priv, struct sk_buff *skb,
 
        switch (skb->protocol) {
        case htons(ETH_P_8021Q):
+               if (!pskb_may_pull(skb, VLAN_HLEN))
+                       return NF_ACCEPT;
+
                veth = (struct vlan_ethhdr *)skb_mac_header(skb);

Is skb_mac_header(skb) always pointing at skb->data - 14 at this stage ?

Otherwise, using

if (!pskb_may_pull(skb, skb_mac_offset(skb) + sizeof(*veth)) would be safer.

author maintainer
#5 lore raw

Re: [PATCH net 3/3] netfilter: flowtable: validate vlan header

Quoted 36 lines · Eric Dumazet
Quoted 25 lines · Pablo Neira Ayuso

Ensure there is sufficient room to access the protocol field of the VLAN header, validate it once before the flowtable lookup.

=====================================================

BUG: KMSAN: uninit-value in nf_flow_offload_inet_hook+0x45a/0x5f0 net/netfilter/nf_flow_table_inet.c:32
 nf_flow_offload_inet_hook+0x45a/0x5f0 net/netfilter/nf_flow_table_inet.c:32
 nf_hook_entry_hookfn include/linux/netfilter.h:154 [inline]
 nf_hook_slow+0xf4/0x400 net/netfilter/core.c:626
 nf_hook_ingress include/linux/netfilter_netdev.h:34 [inline]
 nf_ingress net/core/dev.c:5440 [inline]
Fixes 4cd91f7c290f ("netfilter: flowtable: add vlan support")
Reported-by [email protected]
---
 net/netfilter/nf_flow_table_inet.c | 3 +++
 net/netfilter/nf_flow_table_ip.c   | 3 +++
 2 files changed, 6 insertions(+)
net/netfilter/nf_flow_table_inet.c +0 -0
index 88787b45e30d..dd9a392052ee 100644

net/netfilter/nf_flow_table_inet.c nf_flow_offload_inet_hook(void *priv, struct sk_buff *skb, · line 23

Quoted 7 lines
net/netfilter/nf_flow_table_inet.c +3 -0
@@ -17,6 +17,9 @@ nf_flow_offload_inet_hook(void *priv, struct sk_buff *skb,
 
        switch (skb->protocol) {
        case htons(ETH_P_8021Q):
+               if (!pskb_may_pull(skb, VLAN_HLEN))
+                       return NF_ACCEPT;
+
                veth = (struct vlan_ethhdr *)skb_mac_header(skb);

Is skb_mac_header(skb) always pointing at skb->data - 14 at this stage ?

Quoted 3 lines

Otherwise, using

if (!pskb_may_pull(skb, skb_mac_offset(skb) + sizeof(*veth)) would be safer.

That looks consistent. Done and I just sent a new PR. Thanks.