mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrey Misiurov <amisiurov@cloudlinux.com>
To: Pablo Neira Ayuso <pablo@netfilter.org>, Florian Westphal <fw@strlen.de>
Cc: Jozsef Kadlecsik <kadlec@netfilter.org>,
	Phil Sutter <phil@nwl.cc>,
	Fernando Fernandez Mancera <fmancera@suse.de>,
	netfilter-devel@vger.kernel.org, coreteam@netfilter.org,
	linux-kernel@vger.kernel.org, amisiurov@cloudlinux.com
Subject: [PATCH nf] netfilter: bpf: reject a repeated bpf_ct_set_nat_info()
Date: Fri, 20 Nov 2026 18:13:25 +0100	[thread overview]
Message-ID: <20261120171325.2721546-1-amisiurov@cloudlinux.com> (raw)

bpf_ct_set_nat_info() checks the l3 protocol and the manip type and then
calls nf_nat_setup_info(). It does not check whether NAT for that manip was
already set up.

A BPF program holding an unconfirmed nf_conn___init can call it twice with
the same manip. The first call sets IPS_SRC_NAT_DONE (or IPS_DST_NAT_DONE).
On the second call nf_nat_setup_info() hits WARN_ON(nf_nat_initialized()):

  WARNING: CPU: 1 PID: 21673 at net/netfilter/nf_nat_core.c:674 nf_nat_setup_info+0x251/0x2b0 [nf_nat]
  CPU: 1 PID: 21673 Comm: loader
  RIP: 0010:nf_nat_setup_info+0x251/0x2b0 [nf_nat]
  Call Trace:
   bpf_ct_set_nat_info+0x81/0xb0 [nf_nat]
   bpf_prog_e4b00ba35d2f7a6b_nf_skb_ct_nat_twice+0x110/0x13f
   bpf_test_run+0x172/0x2e0
   bpf_prog_test_run_skb+0x2e5/0x4f0
   __sys_bpf+0x7f8/0xe90
   __x64_sys_bpf+0x1a/0x30
   do_syscall_64+0x5f/0xe0
   entry_SYSCALL_64_after_hwframe+0x76/0x7e

With panic_on_warn this is a DoS.

Reject the call with -EEXIST when NAT for that manip is already initialised,
before nf_nat_setup_info() is reached.

Fixes: 0fabd2aa199f ("net: netfilter: add bpf_ct_set_nat_info kfunc helper")
Signed-off-by: Andrey Misiurov <amisiurov@cloudlinux.com>
---
v2:
- Correct the Fixes tag to 0fabd2aa199f (the helper that added the kfunc),
  not the later manip-type check (Fernando Fernandez Mancera).
- Drop the inline comment; the check is self-documenting (Fernando).
- Reword the commit message (no "confirmed check" in this path).

Found during a backport review.

Reproduced on almalinux9 5.14.0-687.61.1 with a BPF_PROG_TEST_RUN SCHED_CLS
prog that calls bpf_ct_set_nat_info() with NF_NAT_MANIP_SRC twice: without
the fix the second call returns -ENOMEM and WARNs as above; with the fix it
returns -EEXIST and there is no WARN.

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

diff --git a/net/netfilter/nf_nat_bpf.c b/net/netfilter/nf_nat_bpf.c
--- a/net/netfilter/nf_nat_bpf.c
+++ b/net/netfilter/nf_nat_bpf.c
@@ -42,6 +42,9 @@
 	if (manip != NF_NAT_MANIP_SRC && manip != NF_NAT_MANIP_DST)
 		return -EINVAL;

+	if (nf_nat_initialized(ct, manip))
+		return -EEXIST;
+
 	memset(&range, 0, sizeof(struct nf_nat_range2));
 	range.flags = NF_NAT_RANGE_MAP_IPS;
 	range.min_addr = *addr;

                 reply	other threads:[~2026-11-20 17:13 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=20261120171325.2721546-1-amisiurov@cloudlinux.com \
    --to=amisiurov@cloudlinux.com \
    --cc=coreteam@netfilter.org \
    --cc=fmancera@suse.de \
    --cc=fw@strlen.de \
    --cc=kadlec@netfilter.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    /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®