mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yiran Qiu <eritque-arcus@ikuyo.dev>
To: pablo@netfilter.org, fw@strlen.de
Cc: phil@nwl.cc, netfilter-devel@vger.kernel.org,
	coreteam@netfilter.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, Yiran Qiu <eritque-arcus@ikuyo.dev>,
	stable@vger.kernel.org
Subject: [PATCH nf] netfilter: conntrack: skip unconfirmed entries in /proc/net/nf_conntrack
Date: Tue, 17 Nov 2026 13:11:40 -0500	[thread overview]
Message-ID: <20261117181140.133758-1-eritque-arcus@ikuyo.dev> (raw)

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

                 reply	other threads:[~2026-11-17 18:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117181140.133758-1-eritque-arcus@ikuyo.dev \
    --to=eritque-arcus@ikuyo.dev \
    --cc=coreteam@netfilter.org \
    --cc=fw@strlen.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®