Skip to contentSkip to conversation

netfilter: nf_tables: limit maximum number of jumps/gotos per netns

PATCH New Pablo Neira Ayuso 1 participant 2 messages Nov 3, 2025

Subsystems (4)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 netfilter: nf_tables: limit maximum number of jumps/gotos per netns

Add a new sysctl:

net.netfilter.nf_tables_jump_max_netns

which is 256000 by default, because old iptables-nft rulesets are more likely to have more jumps/gotos compared to native nftables rulesets.

This limit prevents soft lockups on the packet caused by crafted rulesets with too many jumps.

This jump/goto limit is global for all tables that are defined in the netns, regardless the family type, but IPv4 and IPv6 are mutually excluded in the jump counting.

Note that verdict maps count as a single jump/goto because a map lookup provides a single exact match, therefore, this is equivalent to an immediate jump.

This limit is not the net count of jumps in your ruleset, but the number of jumps can be visited traversing the acyclic directed graph with depth-first search. This is done from control plane where the evaluation of the selectors is not possible, therefore, this represents the hypothetical worst case.

This patch adds a jump_count[2] field per table which stores the current number of jumps/gotos in the present [0] and the future [1]. During the preparation phase, jump_count[1] is updated to store the number of jumps after the last table validation while processing the batch.

If this batch does not update the number of jumps in this table, then jump_count[0] provides the current number of jumps and jump_count[1] is set to -1. When checking if the number of jumps go over the limit, check if jump_count[1] is >= 0, meaning the number of jumps for this table has been modified by this batch, otherwise use jump_count[0], meaning this table has not been modified in terms of new jumps.

After the commit phase, jump_count[0] is set to jump_count[1] if it is

Quoted 1 line

= 0. Otherwise, in case of abort, jump_count[1] is reset to -1 to

prepare for handling the next batch.

---
v1: - target nf-next instead nf.
- rise default to 256000, requested by Florian.
- use check_add_overflow() to deal with jump count overflows.
- mutually exclude IPv4 and IPv6.
- use:

struct {
int jumps;
} count[2];

to prepare for sysctl to limit maximum number of rules in netns.

 Documentation/networking/netfilter-sysctl.rst |  19 ++
 include/net/netfilter/nf_tables.h             |   9 +
 include/net/netns/netfilter.h                 |   6 +
 net/netfilter/Makefile                        |   2 +-
 net/netfilter/core.c                          |   9 +
 net/netfilter/nf_tables_api.c                 | 166 +++++++++++++++++-
 net/netfilter/nf_tables_sysctl.c              |  91 ++++++++++
 net/netfilter/nft_immediate.c                 |   4 +
 net/netfilter/nft_lookup.c                    |   9 +
 9 files changed, 310 insertions(+), 5 deletions(-)
 create mode 100644 net/netfilter/nf_tables_sysctl.c
Documentation/networking/netfilter-sysctl.rst +19 -0
index beb6d7b275d4..83942c394284 100644
@@ -15,3 +15,22 @@ nf_log_all_netns - BOOLEAN
 	with LOG target; this aims to prevent containers from flooding host
 	kernel log. If enabled, this target also works in other network
 	namespaces. This variable is only accessible from init_net.
+
+nf_tables_jumps_max_netns - INTEGER (count)
+	default 256000
+
+	This is the maximum number of jumps/gotos that a netns can have
+	across its tables. This limit prevents packet path soft lockups
+	caused by rulesets with too many jumps. This limit does not
+	represent the net count of jumps in your ruleset; rather, it
+	represents the number of jumps that can be reached when traversing
+	the ruleset via a depth-first search (DFS). Note that IPv4 and IPv6
+	tables are mutually excluded in this jump count. This limit is
+	determined in the control plane, where evaluating the rule
+	selectors is not possible; therefore, it represents the
+	hypothetical worst case. This limit only applies to non-init_net
+	namespaces and can be read for non-init_user_ns namespaces. Meeting
+	or exceeding this value will prevent additional rules from being
+	added and will return an EMLINK error to the user. Verdict map
+	lookups only count as a single jump, therefore, assuming you do not
+	use iptables-nft your system, then a good alternative default is 256.
include/net/netfilter/nf_tables.h +9 -0
index fab7dc73f738..65ae265428eb 100644
@@ -209,6 +209,7 @@ static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
  *	@family: protocol family
  *	@level: depth of the chains
  *	@report: notify via unicast netlink message
+ * 	@jump_count: jump to chain counter
  *	@reg_inited: bitmap of initialised registers
  */
 struct nft_ctx {
@@ -222,6 +223,7 @@ struct nft_ctx {
 	u8				family;
 	u8				level;
 	bool				report;
+	int				jump_count;
 	DECLARE_BITMAP(reg_inited, NFT_REG32_NUM);
 };
 
@@ -1279,6 +1281,7 @@ static inline void nft_use_inc_restore(u32 *use)
  *	@family:address family
  *	@flags: table flag (see enum nft_table_flags)
  *	@genmask: generation mask
+ * 	@jump_count: current [0] and next [1] jump to chain counter
  *	@nlpid: netlink port ID
  *	@name: name of the table
  *	@udlen: length of the user data
@@ -1303,6 +1306,9 @@ struct nft_table {
 	u16				udlen;
 	u8				*udata;
 	u8				validate_state;
+	struct {
+		int			jumps;
+	} count[2];
 };
 
 static inline bool nft_table_has_owner(const struct nft_table *table)
@@ -1903,6 +1909,9 @@ __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
 #endif
 
+int netfilter_nf_tables_sysctl_init(void);
+void netfilter_nf_tables_sysctl_fini(void);
+
 struct nftables_pernet {
 	struct list_head	tables;
 	struct list_head	commit_list;
include/net/netns/netfilter.h +6 -0
index a6a0bf4a247e..6199f00fa2cb 100644
@@ -18,6 +18,9 @@ struct netns_nf {
 #ifdef CONFIG_LWTUNNEL
 	struct ctl_table_header *nf_lwtnl_dir_header;
 #endif
+#if IS_ENABLED(CONFIG_NF_TABLES)
+	struct ctl_table_header *nf_tables_dir_header;
+#endif
 #endif
 	struct nf_hook_entries __rcu *hooks_ipv4[NF_INET_NUMHOOKS];
 	struct nf_hook_entries __rcu *hooks_ipv6[NF_INET_NUMHOOKS];
@@ -33,5 +36,8 @@ struct netns_nf {
 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
 	unsigned int defrag_ipv6_users;
 #endif
+#if IS_ENABLED(CONFIG_NF_TABLES)
+	unsigned int nf_tables_jumps_max_netns;
+#endif
 };
 #endif
net/netfilter/Makefile +1 -1
index 6bfc250e474f..cdd9cadbd76c 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
-netfilter-objs := core.o nf_log.o nf_queue.o nf_sockopt.o utils.o
+netfilter-objs := core.o nf_log.o nf_tables_sysctl.o nf_queue.o nf_sockopt.o utils.o
 
 nf_conntrack-y	:= nf_conntrack_core.o nf_conntrack_standalone.o nf_conntrack_expect.o nf_conntrack_helper.o \
 		   nf_conntrack_proto.o nf_conntrack_proto_generic.o nf_conntrack_proto_tcp.o nf_conntrack_proto_udp.o \
net/netfilter/core.c +9 -0
index 11a702065bab..2753e8aa3f1f 100644
@@ -24,6 +24,7 @@
 #include <linux/rcupdate.h>
 #include <net/net_namespace.h>
 #include <net/netfilter/nf_queue.h>
+#include <net/netfilter/nf_tables.h>
 #include <net/sock.h>
 
 #include "nf_internals.h"
@@ -814,13 +815,21 @@ int __init netfilter_init(void)
 	ret = netfilter_lwtunnel_init();
 	if (ret < 0)
 		goto err_lwtunnel_pernet;
+#endif
+#if IS_ENABLED(CONFIG_NF_TABLES)
+	ret = netfilter_nf_tables_sysctl_init();
+	if (ret < 0)
+		goto err_nft_pernet;
 #endif
 	ret = netfilter_log_init();
 	if (ret < 0)
 		goto err_log_pernet;
 
 	return 0;
+
 err_log_pernet:
+	netfilter_nf_tables_sysctl_fini();
+err_nft_pernet:
 #ifdef CONFIG_LWTUNNEL
 	netfilter_lwtunnel_fini();
 err_lwtunnel_pernet:
net/netfilter/nf_tables_api.c +162 -4

240 lines not on this page · raw message

net/netfilter/nf_tables_sysctl.c new file +91 -0

91 lines not on this page · raw message

net/netfilter/nft_immediate.c +4 -0
index 02ee5fb69871..43f81c81d179 100644
@@ -259,6 +259,10 @@ static int nft_immediate_validate(const struct nft_ctx *ctx,
 	switch (data->verdict.code) {
 	case NFT_JUMP:
 	case NFT_GOTO:
+		if (pctx->jump_count >= INT_MAX)
+			return -EMLINK;
+
+		pctx->jump_count++;
 		pctx->level++;
 		err = nft_chain_validate(ctx, data->verdict.chain);
 		if (err < 0)
net/netfilter/nft_lookup.c +9 -0
index 58c5b14889c4..0051aa5574e6 100644
@@ -246,12 +246,16 @@ static int nft_lookup_validate(const struct nft_ctx *ctx,
 			       const struct nft_expr *expr)
 {
 	const struct nft_lookup *priv = nft_expr_priv(expr);
+	struct nft_ctx *pctx = (struct nft_ctx *)ctx;
 	struct nft_set_iter iter;
 
 	if (!(priv->set->flags & NFT_SET_MAP) ||
 	    priv->set->dtype != NFT_DATA_VERDICT)
 		return 0;
 
+	if (pctx->jump_count >= INT_MAX)
+		return -EMLINK;
+
 	iter.genmask	= nft_genmask_next(ctx->net);
 	iter.type	= NFT_ITER_UPDATE;
 	iter.skip	= 0;
@@ -266,6 +270,11 @@ static int nft_lookup_validate(const struct nft_ctx *ctx,
 	if (iter.err < 0)
 		return iter.err;
 
+	/* Verdict maps always have one exact match per lookup at least, count
+	 * only one jump per set reference.
+	 */
+	pctx->jump_count++;
+
 	return 0;
 }
 
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 selftests: netfilter: add test for nf_tables_jumps_max_netns sysctl

This patch adds gen_ruleset_many_jumps.c which is a program that generates a random ruleset with many jumps and it estimates the number of jumps that results from its evaluation.

nft_ruleset_many_jumps.sh creates the ruleset and tests if it loads or fail as expected according to the estimated number of jumps.

---
v1: cosmetic changes.

 .../testing/selftests/net/netfilter/Makefile  |   2 +
 .../net/netfilter/gen_ruleset_many_jumps.c    | 146 ++++++++++++++++++
 .../net/netfilter/nft_ruleset_many_jumps.sh   | 118 ++++++++++++++
 3 files changed, 266 insertions(+)
 create mode 100644 tools/testing/selftests/net/netfilter/gen_ruleset_many_jumps.c
 create mode 100755 tools/testing/selftests/net/netfilter/nft_ruleset_many_jumps.sh
tools/testing/selftests/net/netfilter/Makefile +2 -0
index ee2d1a5254f8..dc1b328be31d 100644
@@ -31,6 +31,7 @@ TEST_PROGS := \
 	nft_meta.sh \
 	nft_nat.sh \
 	nft_nat_zones.sh \
+	nft_ruleset_many_jumps.sh \
 	nft_queue.sh \
 	nft_synproxy.sh \
 	nft_tproxy_tcp.sh \
@@ -48,6 +49,7 @@ TEST_GEN_FILES = \
 	connect_close \
 	conntrack_dump_flush \
 	conntrack_reverse_clash \
+	gen_ruleset_many_jumps \
 	nf_queue \
 	sctp_collision \
 	udpclash \
tools/testing/selftests/net/netfilter/gen_ruleset_many_jumps.c new file +146 -0

146 lines not on this page · raw message

tools/testing/selftests/net/netfilter/nft_ruleset_many_jumps.sh new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.30.2