mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH nf] netfilter: conntrack: skip unconfirmed entries in /proc/net/nf_conntrack
@ 2026-11-17 18:11 Yiran Qiu
  0 siblings, 0 replies; only message in thread
From: Yiran Qiu @ 2026-11-17 18:11 UTC (permalink / raw)
  To: pablo, fw
  Cc: phil, netfilter-devel, coreteam, netdev, linux-kernel, Yiran Qiu, stable

ct_seq_show() takes a reference on a conntrack it found by a lockless
walk of the hash table, but never checks that the conntrack is
confirmed.  nf_conntrack is SLAB_TYPESAFE_BY_RCU, so between
ct_get_next() and refcount_inc_not_zero() the entry can be freed and
its slot reused by init_conntrack() for a new, unconfirmed conntrack.
The increment then succeeds and the dump prints a conntrack that is not
in any table.  nf_ct_key_equal(), gc_worker_skip_ct() and early_drop()
reject unconfirmed entries for this reason.

Since commit 2ad9d7747c10 ("netfilter: conntrack: free extension area
immediately") this is also a use-after-free.  An unconfirmed conntrack
can still grow its extension area, for example when an nft ct helper
object attaches a helper, and nf_ct_ext_add() then frees the old
ct->ext through krealloc().  seq_print_acct() and ct_show_delta_time()
read ct->ext without a lock, so the dump can read the freed buffer and
print what it finds there as counters or a time delta.

Skip entries that are not confirmed.

Found with afb-afl, an LLM-driven kernel audit/repro pipeline; the fix
and this changelog were also drafted with LLM assistance.  Reproduced
on a KASAN build of mainline (v7.4-rc3+) that carried timing counters
in ct_seq_show() (two ktime_get() per entry) but no added delay: an
unprivileged user in its own user and network namespace churns UDP
flows through an nft ct helper object while reading
/proc/net/nf_conntrack, and KASAN reported a slab-use-after-free in
ct_seq_show() after 9 and 41 minutes in two runs.  The same build with
this patch ran the reproducer for 60 minutes with no report.  With only
this patch applied to a KASAN build, the conntrack_resize.sh selftest,
which compares /proc/net/nf_conntrack with ctnetlink dumps, passed in
three runs.

Fixes: ea781f197d6a ("netfilter: nf_conntrack: use SLAB_DESTROY_BY_RCU and get rid of call_rcu()")
Cc: stable@vger.kernel.org
Signed-off-by: Yiran Qiu <eritque-arcus@ikuyo.dev>
---
Tested tree: mainline 141b164bbe18 (v7.4-rc3-133-g141b164bbe18).

The reproducer fills the namespace's conntrack table so that
early_drop() recycles slots, and puts 300 non-matching rules in front
of the helper rule, which delays the helper's krealloc() by about 16us
after the new conntrack's refcount_set(); four threads read
/proc/net/nf_conntrack.  The guest had 8 vCPUs.  The build needs
CONFIG_SLUB_RCU_DEBUG=n: with it, KASAN defers the reuse of the
conntrack slot by a grace period and the race cannot happen.  The
counters saw 136 and 88 recycled unconfirmed conntracks pinned by
ct_seq_show() in the two 60-minute runs; with this patch 98 were
pinned and none reached the printer.  From the first run:

  BUG: KASAN: slab-use-after-free in ct_seq_show+0x5ea/0x830
  Read of size 1 at addr ffff8880108f0a05 by task payload/218
  CPU: 7 UID: 1000 PID: 218 Comm: payload Not tainted 7.4.0-rc3-kasan-hunt+ #16 PREEMPTLAZY
  Call Trace:
  [...]
   ct_seq_show+0x5ea/0x830
   seq_read_iter+0x600/0x7e0
   seq_read+0x203/0x2e0
   proc_reg_read+0xec/0x150
   vfs_read+0x144/0x610
  [...]
  Allocated by task 213:
  [...]
   krealloc_node_align_noprof+0x131/0x3c0
   nf_ct_ext_add+0x110/0x380
   nf_ct_ecache_ext_add+0xa9/0x150
   init_conntrack.isra.0+0x444/0xb90
   nf_conntrack_in+0x652/0xac0
  [...]
  Freed by task 213:
  [...]
   kfree+0x113/0x480
   krealloc_node_align_noprof+0x158/0x3c0
   nf_ct_ext_add+0x110/0x380
   nf_ct_helper_ext_add+0x17/0x50
   nft_ct_helper_obj_eval+0x1e5/0x3a0
  [...]
  The buggy address is located 5 bytes inside of
   freed 128-byte region [ffff8880108f0a00, ffff8880108f0a80)

Offset 5 is ext->offset[NF_CT_EXT_TSTAMP], which ct_show_delta_time()
reads even when the timestamp extension is not present.  An earlier
build with a udelay() added in seq_print_acct() hit the same free
there.

6.12.y has the same code (read in v6.12.118, not run there); the hunk
needs its context adjusted, as c5ba345b2d35 is not in that branch.
Reproducer available on request.

 net/netfilter/nf_conntrack_standalone.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/netfilter/nf_conntrack_standalone.c b/net/netfilter/nf_conntrack_standalone.c
index d88a10230862..299691d7ac1e 100644
--- a/net/netfilter/nf_conntrack_standalone.c
+++ b/net/netfilter/nf_conntrack_standalone.c
@@ -314,6 +314,9 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	/* load ->status after refcount increase */
 	smp_acquire__after_ctrl_dep();
 
+	if (!nf_ct_is_confirmed(ct))
+		goto release;
+
 	if (nf_ct_should_gc(ct)) {
 		struct ct_iter_state *st = s->private;
 
-- 
2.56.1

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-17 18:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 18:11 [PATCH nf] netfilter: conntrack: skip unconfirmed entries in /proc/net/nf_conntrack Yiran Qiu

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

all inboxes | Powered by JetHome®