Article View: linux.kernel.netdev
Article #24803[PATCH net 06/10] netfilter: conntrack: limit master chain depth
From: Pablo Neira Ayuso
Date: Mon, 30 Nov 2026 21:51
Date: Mon, 30 Nov 2026 21:51
175 lines
14047 bytes
14047 bytes
From: Florian Westphal <fw@strlen.de>
A conntrack can point to a master conntrack, which itself may point to
another master, and so on. There is currently no bound on how deep this
chain can grow.
Force a max depth in nf_ct_expect_alloc(): walk the master chain and
reject expectation allocation if it would exceed 8 levels of nesting.
For too long chains, ctnetlink returns -ENOMEM when creating long master
chains through expectations, this is misleading but adding ERR_PTR()
would make this fix larger at this stage, this can be revisited later on
if there is a use-case, but such scenario is unlikely in production.
Add ctnetlink_find_get_master which validates the new maximum levels of
nesting as well, ctnetlink create now returns -EMLINK in this case.
This prevents unbounded ct->master->master->... chains.
This also restricts the number of call forward/redirects that h323 and sip
helpers will follow.
Fixes: c1d10adb4a52 ("[NETFILTER]: Add ctnetlink port for nf_conntrack")
Reported-by: Daehyeon Ko <4ncienth@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
include/net/netfilter/nf_conntrack_helper.h | 15 +++++++++
net/netfilter/nf_conntrack_expect.c | 15 +++++++--
net/netfilter/nf_conntrack_netlink.c | 36 +++++++++++++++------
3 files changed, 53 insertions(+), 13 deletions(-)
diff --git a/include/net/netfilter/nf_conntrack_helper.h b/include/net/netfilter/nf_conntrack_helper.h
index 335b8c43694f..53a8eda848dc 100644
--- a/include/net/netfilter/nf_conntrack_helper.h
+++ b/include/net/netfilter/nf_conntrack_helper.h
@@ -29,6 +29,8 @@ enum nf_ct_helper_flags {
#define NF_CT_HELPER_NAME_LEN 16
+#define NF_CT_MAX_EXPECT_CHAIN_LEN 8
+
/* Must be kept in sync with the classes defined by helpers */
#define NF_CT_MAX_EXPECT_CLASSES 4
@@ -159,6 +161,19 @@ static inline void nf_ct_help_put(const struct nf_conn *ct)
kfree_rcu(helper, rcu);
}
+static inline bool nf_ct_master_acceptable(const struct nf_conn *m)
+{
+ unsigned int depth = 0;
+
+ while (m->master) {
+ if (++depth >= NF_CT_MAX_EXPECT_CHAIN_LEN)
+ return false;
+ m = m->master;
+ }
+
+ return true;
+}
+
int nf_conntrack_helper_init(void);
void nf_conntrack_helper_fini(void);
diff --git a/net/netfilter/nf_conntrack_expect.c b/net/netfilter/nf_conntrack_expect.c
index 06242c86e1dc..62e434cd57c9 100644
--- a/net/netfilter/nf_conntrack_expect.c
+++ b/net/netfilter/nf_conntrack_expect.c
@@ -299,13 +299,22 @@ void nf_ct_unexpect_related(struct nf_conntrack_expect *exp)
}
EXPORT_SYMBOL_GPL(nf_ct_unexpect_related);
-/* We don't increase the master conntrack refcount for non-fulfilled
- * conntracks. During the conntrack destruction, the expectations are
- * always killed before the conntrack itself */
+/**
+ * nf_ct_expect_alloc - allocate a new conntrack expectation
+ * @me: The conntrack to be used as master.
+ *
+ * The master conntrack refcount is not incremented; expectations
+ * are always killed before the master conntrack is destroyed.
+ *
+ * Return: a new expectation on success or NULL on failure.
+ */
struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me)
{
struct nf_conntrack_expect *new;
+ if (!nf_ct_master_acceptable(me))
+ return NULL;
+
new = kmem_cache_zalloc(nf_ct_expect_cachep, GFP_ATOMIC);
if (!new)
return NULL;
diff --git a/net/netfilter/nf_conntrack_netlink.c b/net/netfilter/nf_conntrack_netlink.c
index 1f8d94a05240..30428d520e43 100644
--- a/net/netfilter/nf_conntrack_netlink.c
+++ b/net/netfilter/nf_conntrack_netlink.c
@@ -2231,6 +2231,26 @@ ctnetlink_change_conntrack(struct nf_conn *ct,
return 0;
}
+static struct nf_conn *
+ctnetlink_find_get_master(struct net *net, const struct nf_conntrack_zone *zone,
+ const struct nf_conntrack_tuple *tuple)
+{
+ struct nf_conntrack_tuple_hash *master_h;
+ struct nf_conn *master_ct;
+
+ master_h = nf_conntrack_find_get(net, zone, tuple);
+ if (!master_h)
+ return ERR_PTR(-ENOENT);
+
+ master_ct = nf_ct_tuplehash_to_ctrack(master_h);
+ if (!nf_ct_master_acceptable(master_ct)) {
+ nf_ct_put(master_ct);
+ return ERR_PTR(-EMLINK);
+ }
+
+ return master_ct;
+}
+
static struct nf_conn *
ctnetlink_create_conntrack(struct net *net,
const struct nf_conntrack_zone *zone,
@@ -2356,7 +2376,6 @@ ctnetlink_create_conntrack(struct net *net,
/* setup master conntrack: this is a confirmed expectation */
if (cda[CTA_TUPLE_MASTER]) {
struct nf_conntrack_tuple master;
- struct nf_conntrack_tuple_hash *master_h;
struct nf_conn *master_ct;
err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
@@ -2364,12 +2383,11 @@ ctnetlink_create_conntrack(struct net *net,
if (err < 0)
goto err2;
- master_h = nf_conntrack_find_get(net, zone, &master);
- if (master_h == NULL) {
- err = -ENOENT;
+ master_ct = ctnetlink_find_get_master(net, zone, &master);
+ if (IS_ERR(master_ct)) {
+ err = PTR_ERR(master_ct);
goto err2;
}
- master_ct = nf_ct_tuplehash_to_ctrack(master_h);
__set_bit(IPS_EXPECTED_BIT, &ct->status);
ct->master = master_ct;
}
@@ -3613,7 +3631,6 @@ ctnetlink_create_expect(struct net *net,
u_int8_t u3, u32 portid, int report)
{
struct nf_conntrack_tuple tuple, mask, master_tuple;
- struct nf_conntrack_tuple_hash *h = NULL;
struct nf_conntrack_expect *exp;
struct nf_conn *ct;
int err;
@@ -3633,10 +3650,9 @@ ctnetlink_create_expect(struct net *net,
return err;
/* Look for master conntrack of this expectation */
- h = nf_conntrack_find_get(net, zone, &master_tuple);
- if (!h)
- return -ENOENT;
- ct = nf_ct_tuplehash_to_ctrack(h);
+ ct = ctnetlink_find_get_master(net, zone, &master_tuple);
+ if (IS_ERR(ct))
+ return PTR_ERR(ct);
rcu_read_lock();
exp = ctnetlink_alloc_expect(cda, ct, NULL, &tuple, &mask);
--
2.47.3
Technical Details
Message-ID:
<20261130215141.1009148-7-pablo@netfilter.org>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374482-linux-kernel-netdev=dewy.ruReferences:
<20261130215141.1009148-1-pablo@netfilter.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374482-linux-kernel-netdev=dewy.ru From: pablo@netfilter.org (Pablo Neira Ayuso) Newsgroups: linux.kernel.netdev Subject: [PATCH net 06/10] netfilter: conntrack: limit master chain depth Date: Mon, 30 Nov 2026 22:51:37 +0100 Organization: Usenet Network Message-ID: <20261130215141.1009148-7-pablo@netfilter.org> References: <20261130215141.1009148-1-pablo@netfilter.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1547411"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:pVU1IBeRk7lWHvorarGz1SRP4lo= sha256:htnNsK9odkrbDqN43lVYEhksj6pbG9yCOe/pk5WaC6g= sha1:Qun+w8UgT9xJBfFQxOa7ZRKPZFc= sha256:BHJLs2MeL04gdaB0znSd+tCa7USs1P75i++XJYfD8O4= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id B40D7126013; Tue, 1 Dec 2026 00:52:16 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.151 X-Spam-Status: No, score=-3.151 tagged_above=-9999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=netfilter.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jCMoplXViua1; Tue, 1 Dec 2026 00:52:13 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-374482-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=netfilter.org X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74]) by mail.dewy.ru (Postfix) with ESMTPS id BFCF4126007; Tue, 1 Dec 2026 00:52:05 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sto.lore.kernel.org (Postfix) with ESMTP id 79173601F6F; Mon, 30 Nov 2026 21:52:04 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=fail; t=1796075518; b=oK3bdXa0Ev1bCjXjn5WGjd1uDHoGIn1XhQWD6ew+w+W/gkxDIahUkeada84K5dAkiTMW 88upvKm9cuWJ1pFv2eB3gz6BU1WDwFOGgrAPJLaH1yrmw5sVv1z3JFGpBvCoecnGGZoa6 oG+I/FXzX47x2tXjI5b8al2EMc6xyh0jg+SoIiEFnR/SvTVUSv4OFsuzlvKa5oeGttyen EJsG4v4nZl2Sx5HHIkoLiVdcQD9AQrmrSQt5JLltwBlkqh/tAnom+HHG8dXR7wydg4U7r UQOewuWr2Nxfy3fZzQP0RnyEiuOxsADHdhgdxCrVlYALpoMx9ZJcIgKXH8X7i3FG+rg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796075518; h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:Received:From:To: Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=Glpc3rXKUHpJj5Ti44MTorQmUxz+swug/JSM6LHpQW0=; b=Zgn8HqrTxIkouJLzV95D6F1sbb2WTYjQPgAo+EpWADHy4u+dG+dYsA1m8osenySky2tO V3wkBQgbm8SmllBCu8Nhi08UYwgBpLuWWvVu6UjlFvVRtaR5yEHg79F/fh09Akmgzxd4X 8NJZixE42x01HqaKrptfC8Zo6CToPCjD85GcA34qdfZxo0i6FQJP6fq7WTUEYxlucdmY2 7gPpdOF2wPsNRV3vjWwIdhzkR5nirgYX5B0+l8lPUQMKqNbhvqpj2FQB081jvA7CLBXbj zsrK+iqRrEFUct883LwKdzd/jNZjZmrn/gfngwZQ1Mrnak1wCLxb1LhEsPSUfq0bRpw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b=PN3hh0Bd; dmarc=none header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; arc=fail smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 47454606A330; Mon, 30 Nov 2026 21:51:58 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b=PN3hh0Bd X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.70.190.124 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796075517; b=X5b6q4bTZSWi6AJGaLlf38IjzY+1Sy+Y4oJ2kCUU4xdJlvLwx79ACxlep1czdONFYpIY u5VM0VceA24d0v4aYDduBs8febTkFCFBhA4NYjqjLGqiP/pRqRYGwhXCkBXvWnX9nbxPC 2hQgik6ldmHNp8/NVGldmvrWmT8BhkDrzk2wQBPgV9Odb6Xa+K/YCBdefuNtH0B3fRcHk cJ97rbu7FutggwC/X9oLzM0AEaZwJGFNLfF8U/4c9VCJAk3TkBZhcZ80q78BH7tQbmZ4H ml3NWQK1/mhBGj8zDRB1qYKapGJg37hTobcuPHpKrxImgTiccHnduL200WQNCkrz1iw== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796075517; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Spam-Status:X-Spam-Checker-Version; bh=Glpc3rXKUHpJj5Ti44MTorQmUxz+swug/JSM6LHpQW0=; b=LsK0PCiH2Rl3K25YHrI0/uWr+HKXCnG7keQJWp/qMajHDS4TyC/4K/R2Dlfxa9Egpwwb qgxpFcIQN7bJ8gE28R3Ry4KJNzfLhNIyy3Kg9n/d+NWDazc1+8UiFRoqQFyOrgmpw0Lt4 HEu33x+pkwHfAQvxmQ0WL0iPZVh+AgpHvCCcHg6muAwRisOkmvXCTAh0MpF0j2jIOXkYC 6DsOLrQEX1cCJRXIlHTOPKAWI0CwNBgSJClFpDyn0bMSXXEy4LP71rtdoZ9D+diALLe65 PJdfeZsAys+tcmsok37HoBTXC5OXr5CGZtPWBGW+DfQuX9rrwozP/2gRBLNSW0YjJOQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; dkim=pass header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b=PN3hh0Bd; arc=none smtp.remote-ip=217.70.190.124 X-Received: from mail.netfilter.org (mail.netfilter.org [217.70.190.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C75A1606A327; Mon, 30 Nov 2026 21:51:56 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C75A1606A327 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netfilter.org X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netfilter.org; s=2025; t=1796075515; bh=Glpc3rXKUHpJj5Ti44MTorQmUxz+swug/JSM6LHpQW0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PN3hh0Bd0iHkPs24ncOz6oSBbv+NacRYViPJVPN/X7EEg1LAMhgfuX9SMwaz0LW0u e/kKk99ZBsCdIRvfQACiJNXrlv4+bOHPXhNATnsfDeCitlNqGB3KIFeSMYI79YRQ53 aS6crrHyFLPyny0CK0ayCblyZW5yfNU1wquEXbSBzOZVZeR9h2jeZ0H1upH8N3DCAo IJ8MmUTZQ0ljGzUFCDewVUBSYSTl/A7zguQH3ODj4l+mYrvY1VOUnDUgqoweSMCU1X hYiTBxYi1dYrGK3NZtVtDKees45RGnpLD8h1yv2WHrnZk++5S0/KCkgPiaD8Oe2obn gulLqpbzQ5cUA== X-Received: from localhost.localdomain (mail-agni [217.70.190.124]) by mail.netfilter.org (Postfix) with ESMTPSA id 641F560082; Mon, 30 Nov 2026 22:51:54 +0100 (CET) X-Cc: davem@davemloft.net, netdev@vger.kernel.org, kuba@kernel.org, pabeni@redhat.com, edumazet@kernel.org, horms@kernel.org, fw@strlen.de, ja@ssi.bg X-Mailer: git-send-email 2.47.3 X-In-Reply-To: <20261130215141.1009148-1-pablo@netfilter.org> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list