netfilter: flowtable: interlock NF_FLOW_TEARDOWN with NF_FLOW_PENDING
Subsystems (3)
PATCH v3 1/1 netfilter: flowtable: interlock NF_FLOW_TEARDOWN with NF_FLOW_PENDING
nf_flow_offload_gc_step() checks NF_FLOW_PENDING once at entry before setting NF_FLOW_TEARDOWN and potentially freeing the flow via flow_offload_del(). Concurrently, flow_offload_add() or a datapath caller that looked up the flow prior to teardown can call nf_flow_offload_work_alloc(), set NF_FLOW_PENDING, and queue a FLOW_CLS_REPLACE work item holding a raw pointer to the flow without rechecking NF_FLOW_TEARDOWN. If the pending bit is set after the GC check, flow_offload_del() frees the flow via RCU while the queued work item can still dereference and clear NF_FLOW_PENDING on offload->flow.
Interlock the two flags: in nf_flow_offload_gc_step(), check NF_FLOW_PENDING after NF_FLOW_TEARDOWN has been observed or set, paired with a full memory barrier; in nf_flow_offload_work_alloc(), recheck NF_FLOW_TEARDOWN after test_and_set_bit(NF_FLOW_PENDING) for FLOW_CLS_REPLACE and bail out if set. Restrict the teardown check to FLOW_CLS_REPLACE because GC itself queues FLOW_CLS_DESTROY and FLOW_CLS_STATS work on flows already marked for teardown.
This complements commit 0abb54363974 ("netfilter: flowtable: do not hw offload flows in teardown state"), which checks the teardown bit in the nf_flow_offload_refresh() caller: checking inside nf_flow_offload_work_alloc() covers all FLOW_CLS_REPLACE callers, including flow_offload_add(), and the GC-side barrier closes the remaining window where GC reads NF_FLOW_PENDING at entry before NF_FLOW_TEARDOWN is set.
Found with an LLM-assisted code review tool and verified by inspection on v7.4-rc4 (b6a9fa34ce4). Build-tested only; reproducing the race at runtime requires a hardware-offloaded flowtable device.
---
v3: rebase onto net-next with 0abb54363974 included; note how the check
inside work_alloc() complements the refresh-side check and why the GC-side barrier is still needed. No code changes.
v2: resend via direct SMTP without line-wrapping corruption in the diff.
net/netfilter/nf_flow_table_core.c | 8 +++++--- net/netfilter/nf_flow_table_offload.c | 7 +++++++ 2 files changed, 12 insertions(+), 3 deletions(-)
net/netfilter/nf_flow_table_core.c +5 -3
@@ -578,9 +578,6 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table, {bool teardown; -if (test_bit (NF_FLOW_PENDING , &flow->flags)) -return ; - teardown =test_bit (NF_FLOW_TEARDOWN , &flow->flags); if (nf_flow_has_expired(flow) ||
@@ -594,6 +591,11 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table,nf_flow_table_extend_ct_timeout (flow->ct); } +/* Pairs with test_and_set_bit(NF_FLOW_PENDING) in work_alloc(). */ +smp_mb (); +if (test_bit (NF_FLOW_PENDING , &flow->flags)) +return ; +if (teardown) {if (test_bit (NF_FLOW_HW , &flow->flags)) {if (!test_bit (NF_FLOW_HW_DYING , &flow->flags))
net/netfilter/nf_flow_table_offload.c +7 -0
@@ -1088,6 +1088,13 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,if (test_and_set_bit (NF_FLOW_PENDING , &flow->flags))return NULL ; +/* Pairs with smp_mb() in nf_flow_offload_gc_step(). */ +if (cmd ==FLOW_CLS_REPLACE && +test_bit (NF_FLOW_TEARDOWN , &flow->flags)) { +clear_bit (NF_FLOW_PENDING , &flow->flags); +return NULL ; + } + offload = kmalloc_obj(struct flow_offload_work, GFP_ATOMIC);if (!offload) {clear_bit (NF_FLOW_PENDING , &flow->flags);
Signature · 1 line
2.50.1 (Apple Git-155)
Pablo Neira Ayuso
maintainerQuoted 74 lines · Qican Ma
nf_flow_offload_gc_step()checksNF_FLOW_PENDINGonce at entry before settingNF_FLOW_TEARDOWNand potentially freeing the flow viaflow_offload_del(). Concurrently,flow_offload_add()or a datapath caller that looked up the flow prior to teardown can callnf_flow_offload_work_alloc(), setNF_FLOW_PENDING, and queue aFLOW_CLS_REPLACEwork item holding a raw pointer to the flow without recheckingNF_FLOW_TEARDOWN. If the pending bit is set after the GC check,flow_offload_del()frees the flow via RCU while the queued work item can still dereference and clearNF_FLOW_PENDINGonoffload->flow.Interlock the two flags: in
nf_flow_offload_gc_step(), checkNF_FLOW_PENDINGafterNF_FLOW_TEARDOWNhas been observed or set, paired with a full memory barrier; innf_flow_offload_work_alloc(), recheckNF_FLOW_TEARDOWNaftertest_and_set_bit(NF_FLOW_PENDING)forFLOW_CLS_REPLACEand bail out if set. Restrict the teardown check toFLOW_CLS_REPLACEbecause GC itself queuesFLOW_CLS_DESTROYandFLOW_CLS_STATSwork on flows already marked for teardown.This complements commit
0abb54363974("netfilter: flowtable: do not hw offload flows in teardown state"), which checks the teardown bit in thenf_flow_offload_refresh()caller: checking insidenf_flow_offload_work_alloc()covers allFLOW_CLS_REPLACEcallers, includingflow_offload_add(), and the GC-side barrier closes the remaining window where GC readsNF_FLOW_PENDINGat entry beforeNF_FLOW_TEARDOWNis set.Found with an LLM-assisted code review tool and verified by inspection on v7.4-rc4 (
b6a9fa34ce4). Build-tested only; reproducing the race at runtime requires a hardware-offloaded flowtable device.Fixes 7c549fb7eecd ("netfilter: flowtable: generalize pending status bit")Signed-off-by Qican Ma <[email protected]>---
v3: rebase onto net-next with0abb54363974included; note how the check
insidework_alloc()complements the refresh-side check and why the GC-side barrier is still needed. No code changes.
v2: resend via direct SMTP without line-wrapping corruption in the diff.net/netfilter/nf_flow_table_core.c | 8 +++++--- net/netfilter/nf_flow_table_offload.c | 7 +++++++ 2 files changed, 12 insertions(+), 3 deletions(-)net/netfilter/nf_flow_table_core.c +5
-3@@ -578,9 +578,6 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table, {bool teardown; -if (test_bit (NF_FLOW_PENDING , &flow->flags)) -return ; - teardown =test_bit (NF_FLOW_TEARDOWN , &flow->flags); if (nf_flow_has_expired(flow) ||@@ -594,6 +591,11 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table,nf_flow_table_extend_ct_timeout (flow->ct); } +/* Pairs with test_and_set_bit(NF_FLOW_PENDING) in work_alloc(). */ +smp_mb (); +if (test_bit (NF_FLOW_PENDING , &flow->flags)) +return ; +if (teardown) {if (test_bit (NF_FLOW_HW , &flow->flags)) {if (!test_bit (NF_FLOW_HW_DYING , &flow->flags))net/netfilter/nf_flow_table_offload.c +0
-0
net/netfilter/nf_flow_table_offload.c nf_flow_offload_work_alloc(struct nf_flowtable *flowtable, · line 1096
Quoted 9 lines
net/netfilter/nf_flow_table_offload.c +6
-0@@ -1088,6 +1088,13 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,if (test_and_set_bit (NF_FLOW_PENDING , &flow->flags))return NULL ; +/* Pairs with smp_mb() in nf_flow_offload_gc_step(). */ +if (cmd ==FLOW_CLS_REPLACE && +test_bit (NF_FLOW_TEARDOWN , &flow->flags)) { +clear_bit (NF_FLOW_PENDING , &flow->flags); +return NULL ; + }
Not correct, this is duplicating an already existing check in net-next.
Quoted 7 lines
net/netfilter/nf_flow_table_offload.c +1
-0+ offload = kmalloc_obj(struct flow_offload_work, GFP_ATOMIC);if (!offload) {clear_bit (NF_FLOW_PENDING , &flow->flags);Signature · 1 line
2.50.1 (Apple Git-155)
Hi Pablo,
Right - with 0abb54363974 providing the caller-side teardown check, the work_alloc()-side check is redundant. I have sent v4 with that hunk dropped, keeping only the GC-side half of the interlock, which is the piece still missing in net-next: gc_step() reads NF_FLOW_PENDING at entry before the teardown decision, so the work item can still be queued and the flow RCU-freed underneath it.
Thanks,
Qican Ma