* [PATCH nf] netfilter: nfnetlink_queue: drain cleanup work on netns exit
@ 2026-11-20 10:57 Junyoung Jang
2026-11-20 13:32 ` Florian Westphal
0 siblings, 1 reply; 2+ messages in thread
From: Junyoung Jang @ 2026-11-20 10:57 UTC (permalink / raw)
To: Pablo Neira Ayuso, Florian Westphal
Cc: Junyoung Jang, Gwanhyun Lee, Phil Sutter, netfilter-devel, netdev
NETLINK_URELEASE unlinks an NFQUEUE instance and schedules its
destruction through queue_rcu_work(). The RCU callback queues
instance_destroy_work() on nfq_cleanup_wq. Queued entries retain
entry->state.net, but nf_queue_entry_get_refs() pins only their
sockets and network devices.
A migratable device can return to init_net as the old namespace exits.
cleanup_net() waits for the RCU callback but not the work it queued.
It can free the namespace before instance_destroy_work() calls
nfqnl_reinject(entry, NF_DROP), which dereferences the old state.net
in nf_hook_entries_head().
A source review assisted by a coding agent identified this race. A
65,536-queue reproducer on unmodified mainline with
KASAN and lockdep enabled reported:
BUG: KASAN: slab-use-after-free in nfqnl_reinject
Workqueue: nfq_workqueue instance_destroy_work
The free stack included cleanup_net(). The trigger used
initial-namespace CAP_NET_ADMIN and a migratable virtio-net device;
it required no kernel modification or fault injection.
Drain NFQUEUE cleanup in the pernet exit batch. rcu_barrier() waits
for pending RCU callbacks to enqueue their work; flush_workqueue()
then completes that work before cleanup_net() frees the namespace.
The batch hook avoids a barrier for each exiting namespace.
The same mainline revision with this patch built without warnings and
completed two 90-second reproducer runs in KVM with no KASAN or
lockdep reports. No netfilter selftests were run.
Fixes: 936206e3f6ff ("netfilter: nfnetlink_queue: make hash table per queue")
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
A custom LLM-based coding assistant helped identify this race and prepare the reproducer
and patch draft; both authors manually verified the source paths,
teardown ordering, and KVM test results. The standalone C reproducer
and complete serial logs are available on request.
net/netfilter/nfnetlink_queue.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c
index a8aa7ad832c3..5eaa314ed59f 100644
--- a/net/netfilter/nfnetlink_queue.c
+++ b/net/netfilter/nfnetlink_queue.c
@@ -2201,9 +2201,17 @@ static void __net_exit nfnl_queue_net_exit(struct net *net)
WARN_ON_ONCE(!hlist_empty(&q->instance_table[i]));
}
+static void __net_exit nfnl_queue_net_exit_batch(struct list_head *net_exit_list)
+{
+ /* Wait for RCU callbacks to queue instance cleanup work. */
+ rcu_barrier();
+ flush_workqueue(nfq_cleanup_wq);
+}
+
static struct pernet_operations nfnl_queue_net_ops = {
.init = nfnl_queue_net_init,
.exit = nfnl_queue_net_exit,
+ .exit_batch = nfnl_queue_net_exit_batch,
.id = &nfnl_queue_net_id,
.size = sizeof(struct nfnl_queue_net),
};
base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH nf] netfilter: nfnetlink_queue: drain cleanup work on netns exit
2026-11-20 10:57 [PATCH nf] netfilter: nfnetlink_queue: drain cleanup work on netns exit Junyoung Jang
@ 2026-11-20 13:32 ` Florian Westphal
0 siblings, 0 replies; 2+ messages in thread
From: Florian Westphal @ 2026-11-20 13:32 UTC (permalink / raw)
To: Junyoung Jang
Cc: Pablo Neira Ayuso, Gwanhyun Lee, Phil Sutter, netfilter-devel,
netdev
Junyoung Jang <graypanda.inzag@gmail.com> wrote:
> NETLINK_URELEASE unlinks an NFQUEUE instance and schedules its
> destruction through queue_rcu_work(). The RCU callback queues
> instance_destroy_work() on nfq_cleanup_wq. Queued entries retain
> entry->state.net, but nf_queue_entry_get_refs() pins only their
> sockets and network devices.
>
> A migratable device can return to init_net as the old namespace exits.
> cleanup_net() waits for the RCU callback but not the work it queued.
> It can free the namespace before instance_destroy_work() calls
> nfqnl_reinject(entry, NF_DROP), which dereferences the old state.net
> in nf_hook_entries_head().
A bit too verbose changelog for my taste but unfortunately with AI
it's becoming the new standard.
> A source review assisted by a coding agent identified this race. A
> 65,536-queue reproducer on unmodified mainline with
> KASAN and lockdep enabled reported:
The above is rendundant.
> BUG: KASAN: slab-use-after-free in nfqnl_reinject
> Workqueue: nfq_workqueue instance_destroy_work
>
> The free stack included cleanup_net(). The trigger used
> initial-namespace CAP_NET_ADMIN and a migratable virtio-net device;
> it required no kernel modification or fault injection.
This is useful, could also have added the trimmed backtrace
instead of the above.
> Drain NFQUEUE cleanup in the pernet exit batch. rcu_barrier() waits
> for pending RCU callbacks to enqueue their work; flush_workqueue()
> then completes that work before cleanup_net() frees the namespace.
> The batch hook avoids a barrier for each exiting namespace.
>
> The same mainline revision with this patch built without warnings and
> completed two 90-second reproducer runs in KVM with no KASAN or
> lockdep reports. No netfilter selftests were run.
This is redundant: its obvious from looking at the diff.
Reviewed-by: Florian Westphal <fw@strlen.de>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 13:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 10:57 [PATCH nf] netfilter: nfnetlink_queue: drain cleanup work on netns exit Junyoung Jang
2026-11-20 13:32 ` Florian Westphal
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.