Skip to contentSkip to conversation

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

PATCH Superseded Pablo Neira Ayuso 3 participants 3 messages Oct 21, 2025 – Oct 24, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

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

This is set from the init_netns via:

net.netfilter.nf_tables_jump_max_netns

which is 65536 by default.

According to Shawn Brady: "The compile time limit of 65536 was chosen to account for any normal use case, and when this value (and associated stressing loop table) was tested against a 1CPU/256MB machine, the system remained functional."

To simplify the logic, this jump limit is global for all tables that are defined in the netns, regardless the family type.

Jump from verdict map is only counted as one single jump because set lookup provides a single exact match, therefore, this is equivalent to an immediate jump.

Tables now provide a new jump_count[2] field which stores the current number of jump chains 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.

This patch includes a documentation update from Shawn Brady.

---
v1: very lightly tested, target nf as a fix for long jump chain calls.

 Documentation/networking/netfilter-sysctl.rst |   9 ++
 include/net/netfilter/nf_tables.h             |   7 +
 net/netfilter/nf_tables_api.c                 | 149 +++++++++++++++++-
 net/netfilter/nft_immediate.c                 |   4 +
 net/netfilter/nft_lookup.c                    |   9 ++
 5 files changed, 174 insertions(+), 4 deletions(-)
Documentation/networking/netfilter-sysctl.rst +9 -0
index beb6d7b275d4..28f193c196ce 100644
@@ -15,3 +15,12 @@ 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_jump_max_netns - INTEGER (count)
+	default 65536
+
+	The maximum numbers of jumps/gotos a netns can have across its tables.
+	This only applies to non-init_net namespaces, and is read only for
+	non-init_user_ns namespaces. Meeting or exceeding this value will
+	cause additional rules to not be added, with EMLINK being return to
+	the user.
include/net/netfilter/nf_tables.h +7 -0
index fab7dc73f738..4886c938bf0e 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: present [0] and future [1] jump to chain counter
  *	@nlpid: netlink port ID
  *	@name: name of the table
  *	@udlen: length of the user data
@@ -1298,6 +1301,7 @@ struct nft_table {
 	u16				family:6,
 					flags:8,
 					genmask:2;
+	int				jump_count[2];
 	u32				nlpid;
 	char				*name;
 	u16				udlen;
@@ -1917,6 +1921,9 @@ struct nftables_pernet {
 	unsigned int		gc_seq;
 	u8			validate_state;
 	struct work_struct	destroy_work;
+#ifdef CONFIG_SYSCTL
+	struct ctl_table_header	*nf_tables_dir_header;
+#endif
 };
 
 extern unsigned int nf_tables_net_id;
net/netfilter/nf_tables_api.c +145 -4

237 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
maintainer
#1 lore raw
Quoted 3 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_api.c +3 -0
+		tbl = kmemdup(tbl, sizeof(nf_tables_sysctl_table), GFP_KERNEL);
+		if (!tbl)
+			return -ENOMEM;

[..]

net/netfilter/nf_tables_api.c static struct notifier_block nft_nl_notifier = { · line 12253

Quoted 5 lines
net/netfilter/nf_tables_api.c +5 -0
+static void nf_tables_sysctl_exit(struct net *net)
+{
+	struct nftables_pernet *nft_net = nft_pernet(net);
+
+	unregister_net_sysctl_table(nft_net->nf_tables_dir_header);

I think this is missing something like:

	const struct ctl_table *table = ft_net->nf_tables_dir_header->ctl_table_arg;

	unregister_net_sysctl_table(nft_net->nf_tables_dir_header);
	if (!net_eq(net, &init_net))
		kfree(table);
}

netfilter_log_sysctl_exit() et. al do this, so I don't think unregister_net_sysctl_table() takes care of the arg.

#2 lore raw
In reply to Pablo Neira Ayuso
Quoted 10 lines · Pablo Neira Ayuso

This is set from the init_netns via:

net.netfilter.nf_tables_jump_max_netns

which is 65536 by default.

According to Shawn Brady: "The compile time limit of 65536 was chosen to account for any normal use case, and when this value (and associated stressing loop table) was tested against a 1CPU/256MB machine, the system remained functional."

Hey, thanks for the call out. Assuming a v2, the spelling is "Shaun Brady".

Quoted 4 lines

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.

Oh, I like this change. While I believe my old patch series behaved correctly, this seems safer/more straight forward.

Would it make sense to name two separate variables something like jump_count_pre/_post for clarity or will that cause packing issues?

I had a heck of a time getting the requested torture test to walk over the abort code path with an existing rule set. I'm interested in learning more about abort states from your tests.

Thanks!

SB