Re: [PATCH net 1/4] netfilter: flowtable: do not queue offload work after DESTROY

From: netdev-bot+sashiko

Date: Sun Nov 22 2026 - 16:17:30 EST


Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The new NF_FLOW_HW_DYING guard in nf_flow_offload_work_alloc()
(net/netfilter/nf_flow_table_offload.c) does not close the bug class…
- [Low] The commit message describes the race mechanism and the affected
work types imprecisely.

--

netfilter: flowtable: do not queue offload work after DESTROY

This patch makes nf_flow_offload_work_alloc() refuse to queue any work
other than FLOW_CLS_DESTROY once NF_FLOW_HW_DYING is set. The goal is to
stop a packet path reader from queueing REPLACE work for a flow that GC
is already removing, because the offload worker could then use the flow
after it has been freed.

> once the DESTROY work has run and cleared the bit, a reader can take it
> again and queue a REPLACE (or STATS) work for a flow which is already
> being removed.

[Severity: Low]
Is the STATS part accurate? The packet path only reaches
nf_flow_offload_refresh() through flow_offload_refresh(), and that queues
FLOW_CLS_REPLACE.

nf_flow_offload_stats() has a single caller, nf_flow_offload_gc_step(),
and GC skips any flow that has NF_FLOW_PENDING set:

if (test_bit(NF_FLOW_PENDING, &flow->flags))
return;

GC also never runs concurrently with itself. So STATS work that GC queues
cannot race with GC freeing the same flow.

For STATS, the only effect of this patch is that GC stops queueing STATS
for CLOSING && HW && DYING flows whose hardware rule DESTROY has already
removed.

> GC removes a hardware flow after NF_FLOW_HW_DEAD is set, and frees it after
> an RCU grace period, which the reader can outlive in a preemptible RCU
> read-side section.

Could this be worded more precisely? A reader cannot outlive a grace
period that started while it was inside its read-side section, because
the call_rcu() in flow_offload_free() waits for it.

What outlives the grace period is the REPLACE work that the reader queued
on nf_flow_offload_add_wq before rcu_read_unlock().

> diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
> index 4365859220e6a..62f460d38efa2 100644
> --- a/net/netfilter/nf_flow_table_offload.c
> +++ b/net/netfilter/nf_flow_table_offload.c
> @@ -1088,6 +1088,19 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,
> if (test_and_set_bit(NF_FLOW_PENDING, &flow->flags))
> return NULL;
>
> + /* GC only removes a hardware flow after its DESTROY work has run, and
> + * NF_FLOW_PENDING keeps another one from being queued, so nothing can
> + * free the flow while this work is pending, unless DESTROY was queued
> + * before we took the pending bit. A reader which found the flow before
> + * it was torn down must not queue more work then, as the flow could be
> + * freed before that work runs.
> + */
> + if (cmd != FLOW_CLS_DESTROY &&
> + test_bit(NF_FLOW_HW_DYING, &flow->flags)) {
> + clear_bit(NF_FLOW_PENDING, &flow->flags);
> + return NULL;
> + }

[Severity: High]
Does the claim that "nothing can free the flow while this work is pending"
hold for flows that GC frees through its software-only branch?

flow_offload_add() publishes both tuples in the rhashtable before
nf_flow_offload_add() sets NF_FLOW_HW:

net/netfilter/nf_flow_table_core.c:flow_offload_add() {
...
err = rhashtable_insert_fast(&flow_table->rhashtable,
&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
nf_flow_offload_rhash_params);
...
nf_ct_refresh(flow->ct, NF_CT_DAY);

if (nf_flowtable_hw_offload(flow_table))
nf_flow_offload_add(flow_table, flow);
...
}

In that window, nf_flow_offload_gc_step() can decide to tear the flow
down while NF_FLOW_HW is still clear. The teardown can come from a dying
ct, a stale dst, or NF_FLOW_TEARDOWN set by another CPU after a FIN/RST
arrives on the already-published REPLY tuple. GC then takes this branch:

if (teardown) {
if (test_bit(NF_FLOW_HW, &flow->flags)) {
...
} else {
flow_offload_del(flow_table, flow);
}

That path removes the rhashtable entries, releases the dsts and calls
call_rcu() on the flow. It never takes NF_FLOW_PENDING and never sets
NF_FLOW_HW_DYING.

The creating CPU then carries on:

CPU1 (creator) CPU2 (GC)
flow_offload_add()
rhashtable_insert_fast(REPLY)
rhashtable_insert_fast(ORIGINAL)
nf_flow_offload_gc_step()
teardown, NF_FLOW_HW clear
flow_offload_del()
flow_offload_free()
call_rcu()
nf_flow_offload_add()
set_bit(NF_FLOW_HW)
nf_flow_offload_refresh()
nf_flow_offload_work_alloc()
test_and_set_bit(NF_FLOW_PENDING) succeeds
NF_FLOW_HW_DYING clear, new check passes
queue FLOW_CLS_REPLACE

Suppose the work runs after CPU1 leaves its RCU read-side section and the
grace period ends. Would flow_offload_work_handler() then use the freed
flow in flow_offload_work_add(), in nf_flow_offload_alloc(), in
set_bit(IPS_HW_OFFLOAD_BIT, &flow->ct->status), and in
clear_bit(NF_FLOW_PENDING, &offload->flow->flags)?

If the work runs earlier, it seems it would still install a hardware rule
for a flow that is no longer in the table. GC would never DESTROY that
rule.

That is the same use-after-free and stale rule outcome the commit message
describes. As far as I can tell, no later change in this series touches
this path.

Would it make sense for GC's non-HW branch to claim NF_FLOW_PENDING, or
set a dying marker, before freeing the flow? Another option would be to
set NF_FLOW_HW before the flow is published.

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118201250.2075007-1-florian.fainelli%40broadcom.com