Skip to contentSkip to conversation

support for afl++ (american fuzzy lop++) fuzzer

PATCH In review Florian Westphal 3 participants 12 messages Oct 17, 2025 – Oct 20, 2025

Subsystems (3)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH v2 1/1 support for afl++ (american fuzzy lop++) fuzzer

afl comes with a compiler frontend that can add instrumentation suitable for running nftables via the "afl-fuzz" fuzzer.

This change adds a "--with-fuzzer" option to configure script and enables specific handling in nftables and libnftables to speed up the fuzzing process. It also adds the "--fuzzer" command line option.

afl-fuzz initialisation gets delayed until after the netlink context is set up and symbol tables such as (e.g. route marks) have been parsed.

When afl-fuzz restarts the process with a new input round, it will resume *after* this point (see __AFL_INIT macro in main.c).

With --fuzzer <stage>, nft will perform multiple fuzzing rounds per
invocation: this increases processing rate by an order of magnitude. The argument to '--fuzzer' specifies the last stage to run:

1: 'parser':
Only run / exercise the flex/bison parser.

2: 'eval': stop after the evaluation phase.
This attempts to build a complete ruleset in memory, does symbol resolution, adds needed shift/masks to payload instructions etc.

3: 'netlink-ro':
'netlink-ro' builds the netlink buffer to send to the kernel, without actually doing so.

4: 'netlink-rw':
Pass generated command/ruleset will be passed to the kernel. You can combine it with the '--check' option to send data to the kernel but without actually committing any changes.
This could still end up triggering a kernel crash if there are bugs in the valiation / transaction / abort phases.

Use 'netlink-ro' if you want to prevent nft from ever submitting any changes to the kernel or if you are only interested in fuzzing nftables and its libraries.

In case a kernel splat is detected, the fuzzing process stops and all further fuzzer attemps are blocked until reboot.

---
Changes since v2:
- get rid of most of v1 preprocessing
- remove all of the input-save mechanism and all of the helper stage scripting that v1 had.

v1 used to save a backlog of fuzzer inputs to disk so that kernel splats could be better understood. This slows down fuzzing a lot, so no longer do that. It could still be added back later on if needed.

This patch could be further simplified by removing 'parser' and 'eval' fuzz modes, but it would not save much code so I kept both.

The netlink-ro one is useful because netlink-rw mode is substantially slower (kernel abort, mutex serialization when running multiple afl instances in same netns).

 .gitignore                       |   5 +
 Makefile.am                      |   5 +
 configure.ac                     |  17 +++
 include/afl++.h                  |  48 +++++++
 include/nftables.h               |   3 +
 src/afl++.c                      | 213 +++++++++++++++++++++++++++++++
 src/libnftables.c                |  24 +++-
 src/main.c                       | 101 +++++++++++++++
 src/preprocess.c                 |   2 +
 tests/afl++/README               | 108 ++++++++++++++++
 tests/afl++/afl-sysctl.conf      |   5 +
 tests/afl++/check_reproducers.sh | 118 +++++++++++++++++
 tests/afl++/run-afl.sh           |  47 +++++++
 13 files changed, 695 insertions(+), 1 deletion(-)
 create mode 100644 include/afl++.h
 create mode 100644 src/afl++.c
 create mode 100644 tests/afl++/README
 create mode 100644 tests/afl++/afl-sysctl.conf
 create mode 100755 tests/afl++/check_reproducers.sh
 create mode 100755 tests/afl++/run-afl.sh
.gitignore +5 -0
index db329eafa529..719829b65d21 100644
@@ -36,6 +36,11 @@ tests/build/tests.log
 /tests/shell/run-tests.sh.log
 /tests/shell/run-tests.sh.trs
 
+# generated by run-afl.sh
+/tests/afl++/in/
+/tests/afl++/nft.dict
+/tests/afl++/out/
+
 # Debian package build temporary files
 build-stamp
 
Makefile.am +5 -0
index fac7ad55cbe7..0a4c213390d5 100644
@@ -64,6 +64,7 @@ noinst_HEADERS = \
 	include/linux/netfilter_ipv6.h \
 	include/linux/netfilter_ipv6/ip6_tables.h \
 	\
+	include/afl++.h	\
 	include/cache.h \
 	include/cli.h \
 	include/cmd.h \
@@ -293,6 +294,10 @@ sbin_PROGRAMS += src/nft
 
 src_nft_SOURCES = src/main.c
 
+if BUILD_AFL
+src_nft_SOURCES += src/afl++.c
+endif
+
 if BUILD_CLI
 src_nft_SOURCES += src/cli.c
 endif
configure.ac +17 -0
index a3ae2956cdf3..9a65fcaf0374 100644
@@ -91,6 +91,18 @@ AC_MSG_ERROR([unexpected CLI value: $with_cli])
 ])
 AM_CONDITIONAL([BUILD_CLI], [test "x$with_cli" != xno])
 
+AC_ARG_ENABLE([fuzzer],
+	      AS_HELP_STRING([--enable-fuzzer], [Enable fuzzer support.  NEVER use this unless you work on nftables project]),
+	      [enable_fuzzer=yes], [enable_fuzzer=no])
+
+AM_CONDITIONAL([BUILD_AFL], [test "x$enable_fuzzer" = xyes])
+
+HAVE_FUZZER_BUILD=0
+AS_IF([test "x$enable_fuzzer" != xno], [
+	HAVE_FUZZER_BUILD=1
+])
+AC_DEFINE_UNQUOTED([HAVE_FUZZER_BUILD], [$HAVE_FUZZER_BUILD], [Whether to build with fuzzer support])
+
 AC_ARG_WITH([xtables], [AS_HELP_STRING([--with-xtables],
             [Use libxtables for iptables interaction])],
 	    [], [with_xtables=no])
@@ -180,3 +192,8 @@ echo "  systemd unit:                 ${unitdir}"
 else
 echo "  systemd unit:                 no"
 fi
+
+# Do not print "fuzzer support:		no", this is development-only.
+AS_IF([test "x$enable_fuzzer" = xyes ], [
+	echo "  fuzzer support:		yes"
+	], [ ])
include/afl++.h new file +48 -0

48 lines not on this page · raw message

include/nftables.h +3 -0
index c058667c03fa..538150126816 100644
@@ -145,6 +145,9 @@ struct nft_ctx {
 	void			*json_root;
 	json_t			*json_echo;
 	const char		*stdin_buf;
+#if HAVE_FUZZER_BUILD
+	int			afl_ctx_stage;
+#endif
 };
 
 enum nftables_exit_codes {
src/afl++.c new file +213 -0

213 lines not on this page · raw message

src/libnftables.c +23 -1
index 9f6a1bc33539..66b03a1170bb 100644
@@ -9,6 +9,7 @@
 #include <nft.h>
 
 #include <nftables/libnftables.h>
+#include <afl++.h>
 #include <erec.h>
 #include <mnl.h>
 #include <parser.h>
@@ -19,6 +20,17 @@
 #include <sys/stat.h>
 #include <libgen.h>
 
+static int do_mnl_batch_talk(struct netlink_ctx *ctx, struct list_head *err_list,
+			     uint32_t num_cmds)
+{
+#if HAVE_FUZZER_BUILD
+	if (ctx->nft->afl_ctx_stage &&
+	    ctx->nft->afl_ctx_stage < NFT_AFL_FUZZER_NETLINK_RW)
+		return 0;
+#endif
+	return mnl_batch_talk(ctx, err_list, num_cmds);
+}
+
 static int nft_netlink(struct nft_ctx *nft,
 		       struct list_head *cmds, struct list_head *msgs)
 {
@@ -37,7 +49,13 @@ static int nft_netlink(struct nft_ctx *nft,
 	if (list_empty(cmds))
 		goto out;
 
+#if HAVE_FUZZER_BUILD
+	if (nft->afl_ctx_stage &&
+	    nft->afl_ctx_stage <= NFT_AFL_FUZZER_EVALUATION)
+		goto out;
+#endif
 	batch_seqnum = mnl_batch_begin(ctx.batch, mnl_seqnum_inc(&seqnum));
+
 	list_for_each_entry(cmd, cmds, list) {
 		ctx.seqnum = cmd->seqnum_from = mnl_seqnum_inc(&seqnum);
 		ret = do_command(&ctx, cmd);
@@ -57,7 +75,7 @@ static int nft_netlink(struct nft_ctx *nft,
 	if (!mnl_batch_ready(ctx.batch))
 		goto out;
 
-	ret = mnl_batch_talk(&ctx, &err_list, num_cmds);
+	ret = do_mnl_batch_talk(&ctx, &err_list, num_cmds);
 	if (ret < 0) {
 		if (ctx.maybe_emsgsize && errno == EMSGSIZE) {
 			netlink_io_error(&ctx, NULL,
@@ -605,6 +623,10 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
 		rc = nft_parse_bison_buffer(nft, nlbuf, &msgs, &cmds,
 					    &indesc_cmdline);
 
+#if HAVE_FUZZER_BUILD
+	if (nft->afl_ctx_stage == NFT_AFL_FUZZER_PARSER)
+		goto err;
+#endif
 	parser_rc = rc;
 
 	rc = nft_evaluate(nft, &msgs, &cmds);
src/main.c +101 -0

161 lines not on this page · raw message

src/preprocess.c +2 -0
index 619f67a15049..640ffad34a62 100644
@@ -6,6 +6,8 @@
  * later) as published by the Free Software Foundation.
  */
 
+#include <nft.h>
+
 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
tests/afl++/README new file +108 -0

108 lines not on this page · raw message

tests/afl++/afl-sysctl.conf new file +5 -0
new file mode 100644
index 000000000000..490e62b00094
@@ -0,0 +1,5 @@
+kernel.core_pattern=core
+fs.suid_dumpable=1
+kernel.core_uses_pid=0
+kernel.randomize_va_space=0
+kernel.sched_autogroup_enabled=1
tests/afl++/check_reproducers.sh new file +118 -0

118 lines not on this page · raw message

tests/afl++/run-afl.sh new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.51.0
#1 lore raw
Quoted 7 lines · Florian Westphal

afl comes with a compiler frontend that can add instrumentation suitable for running nftables via the "afl-fuzz" fuzzer.

This change adds a "--with-fuzzer" option to configure script and enables specific handling in nftables and libnftables to speed up the fuzzing process. It also adds the "--fuzzer" command line option.

Hi Florian, I think this is awesome. I have been playing with it since you posted this patch.. and found a couple of things already!

Quoted 29 lines

afl-fuzz initialisation gets delayed until after the netlink context is set up and symbol tables such as (e.g. route marks) have been parsed.

When afl-fuzz restarts the process with a new input round, it will resume *after* this point (see __AFL_INIT macro in main.c).

With --fuzzer <stage>, nft will perform multiple fuzzing rounds per
invocation: this increases processing rate by an order of magnitude. The argument to '--fuzzer' specifies the last stage to run:

1: 'parser':
Only run / exercise the flex/bison parser.

2: 'eval': stop after the evaluation phase.
This attempts to build a complete ruleset in memory, does symbol resolution, adds needed shift/masks to payload instructions etc.

3: 'netlink-ro':
'netlink-ro' builds the netlink buffer to send to the kernel, without actually doing so.

4: 'netlink-rw':
Pass generated command/ruleset will be passed to the kernel. You can combine it with the '--check' option to send data to the kernel but without actually committing any changes.
This could still end up triggering a kernel crash if there are bugs in the valiation / transaction / abort phases.

Quoted 7 lines

Use 'netlink-ro' if you want to prevent nft from ever submitting any changes to the kernel or if you are only interested in fuzzing nftables and its libraries.

In case a kernel splat is detected, the fuzzing process stops and all further fuzzer attemps are blocked until reboot.

I do not think this is what happens or I am maybe misunderstanding something.I got a kernel splat - soft lockup as the CPU was stuck for 40s (!). Anyway, kernel was then tainted but the fuzzer didn't stop it continued running but not executing the commands as kernel was tainted.. see the comments below.

Quoted 64 lines

[...]

src/afl++.c new file +63 -0
+/* this lets the source compile without afl-clang-fast/lto */
+static unsigned char fuzz_buf[4096];
+static ssize_t fuzz_len;
+
+#define __AFL_FUZZ_TESTCASE_LEN fuzz_len
+#define __AFL_FUZZ_TESTCASE_BUF fuzz_buf
+#define __AFL_FUZZ_INIT() do { } while (0)
+#define __AFL_LOOP(x) \
+   ((fuzz_len = read(0, fuzz_buf, sizeof(fuzz_buf))) > 0 ? 1 : 0)
+#endif
+
+struct nft_afl_state {
+	FILE *make_it_fail_fp;
+};
+
+static struct nft_afl_state state;
+
+static char *preprocess(unsigned char *input, ssize_t len)
+{
+	ssize_t real_len = strnlen((char *)input, len);
+
+	if (real_len == 0)
+		return NULL;
+
+	if (real_len >= len)
+		input[len - 1] = 0;
+
+	return (char *)input;
+}
+
+static bool kernel_is_tainted(void)
+{
+	FILE *fp = fopen("/proc/sys/kernel/tainted", "r");
+	unsigned int taint;
+	bool ret = false;
+
+	if (fp) {
+		if (fscanf(fp, "%u", &taint) == 1 && taint) {
+			fprintf(stderr, "Kernel is tainted: 0x%x\n", taint);
+			sleep(3);	/* in case we run under fuzzer, don't restart right away */
+			ret = true;
+		}
+
+		fclose(fp);
+	}
+
+	return ret;
+}
+
+static void fault_inject_write(FILE *fp, unsigned int v)
+{
+	rewind(fp);
+	fprintf(fp, "%u\n", v);
+	fflush(fp);
+}
+
+static void fault_inject_enable(const struct nft_afl_state *state)
+{
+	if (state->make_it_fail_fp)
+		fault_inject_write(state->make_it_fail_fp, 1);
+}
+
+static void fault_inject_disable(const struct nft_afl_state *state)

src/afl++.c line 105

Quoted 10 lines
src/afl++.c new file +10 -0
+{
+	if (state->make_it_fail_fp)
+		fault_inject_write(state->make_it_fail_fp, 0);
+}
+
+static bool nft_afl_run_cmd(struct nft_ctx *ctx, const char *input_cmd)
+{
+	if (kernel_is_tainted())
+		return false;
+

While this prevents the execution of the command, the input is already generated. See comments in main function.

Quoted 93 lines
src/afl++.c new file +93 -0
+	switch (ctx->afl_ctx_stage) {
+	case NFT_AFL_FUZZER_PARSER:
+	case NFT_AFL_FUZZER_EVALUATION:
+	case NFT_AFL_FUZZER_NETLINK_RO:
+		nft_run_cmd_from_buffer(ctx, input_cmd);
+		return true;
+	case NFT_AFL_FUZZER_NETLINK_RW:
+		break;
+	}
+
+	fault_inject_enable(&state);
+	nft_run_cmd_from_buffer(ctx, input_cmd);
+	fault_inject_disable(&state);
+
+	return kernel_is_tainted();
+}
+
+static FILE *fault_inject_open(void)
+{
+	return fopen(self_fault_inject_file, "r+");
+}
+
+static bool nft_afl_state_init(struct nft_afl_state *state)
+{
+	state->make_it_fail_fp = fault_inject_open();
+	return true;
+}
+
+int nft_afl_init(struct nft_ctx *ctx, enum nft_afl_fuzzer_stage stage)
+{
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+	const char instrumented[] = "afl instrumented";
+#else
+	const char instrumented[] = "no afl instrumentation";
+#endif
+	nft_afl_print_build_info(stderr);
+
+	if (!nft_afl_state_init(&state))
+		return -1;
+
+	ctx->afl_ctx_stage = stage;
+
+	if (state.make_it_fail_fp) {
+		unsigned int value;
+		int ret;
+
+		rewind(state.make_it_fail_fp);
+		ret = fscanf(state.make_it_fail_fp, "%u", &value);
+		if (ret != 1 || value != 1) {
+			fclose(state.make_it_fail_fp);
+			state.make_it_fail_fp = NULL;
+		}
+
+		/* if its enabled, disable and then re-enable ONLY
+		 * when submitting data to the kernel.
+		 *
+		 * Otherwise even libnftables memory allocations could fail
+		 * which is not what we want.
+		 */
+		fault_inject_disable(&state);
+	}
+
+	fprintf(stderr, "starting (%s, %s fault injection)", instrumented, state.make_it_fail_fp ? "with" : "no");
+	return 0;
+}
+
+int nft_afl_main(struct nft_ctx *ctx)
+{
+	unsigned char *buf;
+	ssize_t len;
+
+	if (kernel_is_tainted())
+		return -1;
+
+	if (state.make_it_fail_fp) {
+		FILE *fp = fault_inject_open();
+
+		/* reopen is needed because /proc/self is a symlink, i.e.
+		 * fp refers to parent process, not "us".
+		 */
+		if (!fp) {
+			fprintf(stderr, "Could not reopen %s: %s", self_fault_inject_file, strerror(errno));
+			return -1;
+		}
+
+		fclose(state.make_it_fail_fp);
+		state.make_it_fail_fp = fp;
+	}
+
+	buf = __AFL_FUZZ_TESTCASE_BUF;
+
+	while (__AFL_LOOP(UINT_MAX)) {
+		char *input;

src/afl++.c line 208

Quoted 10 lines
src/afl++.c new file +10 -0
+
+		len = __AFL_FUZZ_TESTCASE_LEN;  // do not use the macro directly in a call!
+
+		input = preprocess(buf, len);
+		if (!input)
+			continue;
+
+		/* buf is null terminated at this point */
+		if (!nft_afl_run_cmd(ctx, input))
+			continue;

If we get a kernel splat and kernel is tainted but continue running (soft lockup, warning..) the fuzzer won't stop, it won't execute more commands but it will generate inputs for hours.

In addition I noticed that when a kernel splat happens the ruleset that triggered it isn't saved anywhere, it would be nice to save them so we have a reproducer right away.. For one of the kernel splats I got, I was able to generate a reproducer by looking at the trace.. but for the other I couldn't.

I have a server at home that I am not using.. I would love to automate a script to run this in multiple VMs and generate reports :)

author maintainer
#2 lore raw
Quoted 11 lines · Fernando Fernandez Mancera
Quoted 7 lines · Florian Westphal

afl comes with a compiler frontend that can add instrumentation suitable for running nftables via the "afl-fuzz" fuzzer.

This change adds a "--with-fuzzer" option to configure script and enables specific handling in nftables and libnftables to speed up the fuzzing process. It also adds the "--fuzzer" command line option.

Hi Florian, I think this is awesome. I have been playing with it since you posted this patch.. and found a couple of things already!

Great :)

Quoted 8 lines
Quoted 3 lines

In case a kernel splat is detected, the fuzzing process stops and all further fuzzer attemps are blocked until reboot.

I do not think this is what happens or I am maybe misunderstanding something.I got a kernel splat - soft lockup as the CPU was stuck for 40s (!). Anyway, kernel was then tainted but the fuzzer didn't stop it continued running but not executing the commands as kernel was tainted.. see the comments below.

Yes, I don't know how to stop it. IIRC exit() just makes afl-fuzz resume with next cycle.

Quoted 2 lines

While this prevents the execution of the command, the input is already generated. See comments in main function.

Yes. I just want it to stop burning cpu cycles if kernel is already tainted.

Quoted 10 lines
Quoted 10 lines
src/afl++.c new file +10 -0
+	buf = __AFL_FUZZ_TESTCASE_BUF;
+
+	while (__AFL_LOOP(UINT_MAX)) {
+		char *input;
+
+		len = __AFL_FUZZ_TESTCASE_LEN;  // do not use the macro directly in a call!
+
+		input = preprocess(buf, len);
+		if (!input)
+			continue;
Quoted 8 lines

src/afl++.c line 208

Quoted 4 lines
src/afl++.c new file +4 -0
+
+		/* buf is null terminated at this point */
+		if (!nft_afl_run_cmd(ctx, input))
+			continue;

If we get a kernel splat and kernel is tainted but continue running (soft lockup, warning..) the fuzzer won't stop, it won't execute more commands but it will generate inputs for hours.

Yes, it should not continue to burn cpu cycles.

Quoted 3 lines

In addition I noticed that when a kernel splat happens the ruleset that triggered it isn't saved anywhere, it would be nice to save them so we have a reproducer right away.

I had such code but removed it for this version.

I can send a followup patch to re-add it but I think that it is better for kernel fuzzing to extend knft acordingly, as nft is restricted by the input grammar wrt. the nonsense that it can create.

Quoted 2 lines

I have a server at home that I am not using.. I would love to automate a script to run this in multiple VMs and generate reports :)

Yes, that would be good. Note that I still primarily use it with netlink-ro mode to not exercise he kernel, its easy to make graph validation (or abort path) take very long to finish.

There is still a patch series in the queue to limit jumps in nftables and I did not yet have time in looking at the abort path, Its simply not an issue for normal cases (you assume the input is going to be committed...). But for faster netlink-rw/knft fuzzing it would make sense to look into async abort (like we do for commit cleanup).

#3 lore raw
Quoted 10 lines · Florian Westphal
Quoted 3 lines

In addition I noticed that when a kernel splat happens the ruleset that triggered it isn't saved anywhere, it would be nice to save them so we have a reproducer right away.

I had such code but removed it for this version.

I can send a followup patch to re-add it but I think that it is better for kernel fuzzing to extend knft acordingly, as nft is restricted by the input grammar wrt. the nonsense that it can create.

That is fine for me, I still have pending to try knft which I might do this week if I have time. If we do not want to save which ruleset generated the kernel splat I would drop netlink-rw mode completely..

Quoted 7 lines
Quoted 2 lines

I have a server at home that I am not using.. I would love to automate a script to run this in multiple VMs and generate reports :)

Yes, that would be good. Note that I still primarily use it with netlink-ro mode to not exercise he kernel, its easy to make graph validation (or abort path) take very long to finish.

Quoted 6 lines

There is still a patch series in the queue to limit jumps in nftables and I did not yet have time in looking at the abort path, Its simply not an issue for normal cases (you assume the input is going to be committed...). But for faster netlink-rw/knft fuzzing it would make sense to look into async abort (like we do for commit cleanup).

Yes, it seems we found the same issue. I do not have a solution on the control plane although I was about to send this patch for data plane.

net/netfilter/nf_tables_core.c +4 -1
index 6557a4018c09..ddc4943d082c 100644
@@ -251,10 +251,10 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
 {
        const struct nft_chain *chain = priv, *basechain = chain;
        const struct net *net = nft_net(pkt);
+       unsigned int stackptr = 0, jumps = 0;
        const struct nft_expr *expr, *last;
        const struct nft_rule_dp *rule;
        struct nft_regs regs;
-       unsigned int stackptr = 0;
        struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
        bool genbit = READ_ONCE(net->nft.gencursor);
        struct nft_rule_blob *blob;
@@ -314,6 +314,9 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
 
        switch (regs.verdict.code) {
        case NFT_JUMP:
+               jumps++;
+               if (WARN_ON_ONCE(jumps > 256))
+                       return NF_DROP;
                if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
                        return NF_DROP;
                jumpstack[stackptr].rule = nft_rule_next(rule);

Currently with enough jumps chained together and traffic generated, CPU can get stuck on nft_do_chain() triggering a kernel splat. If there is a solution on data plane it would be much better than this of course.

author maintainer
#4 lore raw
Quoted 7 lines · Fernando Fernandez Mancera
Quoted 6 lines · Florian Westphal
Quoted 3 lines

In addition I noticed that when a kernel splat happens the ruleset that triggered it isn't saved anywhere, it would be nice to save them so we have a reproducer right away.

I had such code but removed it for this version.

Quoted 8 lines
Quoted 4 lines

I can send a followup patch to re-add it but I think that it is better for kernel fuzzing to extend knft acordingly, as nft is restricted by the input grammar wrt. the nonsense that it can create.

That is fine for me, I still have pending to try knft which I might do this week if I have time. If we do not want to save which ruleset generated the kernel splat I would drop netlink-rw mode completely..

Hmmm.... I'm not sure on this. It would be a bit of a silly limitation. Its not like -rw adds a huge chunk of code.

The store code wasn't too bad, back then I added some scripting for allow to e.g. call nft flush ruleset periodically and that was more code than strictly needed for pure nft (-ro mode) fuzzing.

Quoted 30 lines

Yes, it seems we found the same issue. I do not have a solution on the control plane although I was about to send this patch for data plane.

net/netfilter/nf_tables_core.c +4 -1
index 6557a4018c09..ddc4943d082c 100644
@@ -251,10 +251,10 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
  {
         const struct nft_chain *chain = priv, *basechain = chain;
         const struct net *net = nft_net(pkt);
+       unsigned int stackptr = 0, jumps = 0;
         const struct nft_expr *expr, *last;
         const struct nft_rule_dp *rule;
         struct nft_regs regs;
-       unsigned int stackptr = 0;
         struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
         bool genbit = READ_ONCE(net->nft.gencursor);
         struct nft_rule_blob *blob;
@@ -314,6 +314,9 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
 
         switch (regs.verdict.code) {
         case NFT_JUMP:
+               jumps++;
+               if (WARN_ON_ONCE(jumps > 256))
+                       return NF_DROP;
                 if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
                         return NF_DROP;
                 jumpstack[stackptr].rule = nft_rule_next(rule);
Quoted 3 lines

Currently with enough jumps chained together and traffic generated, CPU can get stuck on nft_do_chain() triggering a kernel splat. If there is a solution on data plane it would be much better than this of course.

There is this patch:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

I planned to push it upstream in this merge window.

#5 lore raw
Quoted 54 lines · Florian Westphal
Quoted 7 lines · Fernando Fernandez Mancera
Quoted 6 lines · Florian Westphal
Quoted 3 lines

In addition I noticed that when a kernel splat happens the ruleset that triggered it isn't saved anywhere, it would be nice to save them so we have a reproducer right away.

I had such code but removed it for this version.

Quoted 8 lines
Quoted 4 lines

I can send a followup patch to re-add it but I think that it is better for kernel fuzzing to extend knft acordingly, as nft is restricted by the input grammar wrt. the nonsense that it can create.

That is fine for me, I still have pending to try knft which I might do this week if I have time. If we do not want to save which ruleset generated the kernel splat I would drop netlink-rw mode completely..

Hmmm.... I'm not sure on this. It would be a bit of a silly limitation. Its not like -rw adds a huge chunk of code.

The store code wasn't too bad, back then I added some scripting for allow to e.g. call nft flush ruleset periodically and that was more code than strictly needed for pure nft (-ro mode) fuzzing.

Quoted 30 lines

Yes, it seems we found the same issue. I do not have a solution on the control plane although I was about to send this patch for data plane.

net/netfilter/nf_tables_core.c +4 -1
index 6557a4018c09..ddc4943d082c 100644
@@ -251,10 +251,10 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
   {
          const struct nft_chain *chain = priv, *basechain = chain;
          const struct net *net = nft_net(pkt);
+       unsigned int stackptr = 0, jumps = 0;
          const struct nft_expr *expr, *last;
          const struct nft_rule_dp *rule;
          struct nft_regs regs;
-       unsigned int stackptr = 0;
          struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
          bool genbit = READ_ONCE(net->nft.gencursor);
          struct nft_rule_blob *blob;
@@ -314,6 +314,9 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
 
          switch (regs.verdict.code) {
          case NFT_JUMP:
+               jumps++;
+               if (WARN_ON_ONCE(jumps > 256))
+                       return NF_DROP;
                  if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
                          return NF_DROP;
                  jumpstack[stackptr].rule = nft_rule_next(rule);
Quoted 9 lines
Quoted 3 lines

Currently with enough jumps chained together and traffic generated, CPU can get stuck on nft_do_chain() triggering a kernel splat. If there is a solution on data plane it would be much better than this of course.

There is this patch:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

I planned to push it upstream in this merge window.

This looks quite good. I tested it and seems to solve the problem, great!

#6 lore raw
Quoted 64 lines · Fernando Fernandez Mancera
Quoted 61 lines · Florian Westphal
Quoted 7 lines · Fernando Fernandez Mancera
Quoted 6 lines · Florian Westphal
Quoted 3 lines

In addition I noticed that when a kernel splat happens the ruleset that triggered it isn't saved anywhere, it would be nice to save them so we have a reproducer right away.

I had such code but removed it for this version.

Quoted 8 lines
Quoted 4 lines

I can send a followup patch to re-add it but I think that it is better for kernel fuzzing to extend knft acordingly, as nft is restricted by the input grammar wrt. the nonsense that it can create.

That is fine for me, I still have pending to try knft which I might do this week if I have time. If we do not want to save which ruleset generated the kernel splat I would drop netlink-rw mode completely..

Hmmm.... I'm not sure on this. It would be a bit of a silly limitation. Its not like -rw adds a huge chunk of code.

The store code wasn't too bad, back then I added some scripting for allow to e.g. call nft flush ruleset periodically and that was more code than strictly needed for pure nft (-ro mode) fuzzing.

Quoted 30 lines

Yes, it seems we found the same issue. I do not have a solution on the control plane although I was about to send this patch for data plane.

net/netfilter/nf_tables_core.c +4 -1
index 6557a4018c09..ddc4943d082c 100644
@@ -251,10 +251,10 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
   {
          const struct nft_chain *chain = priv, *basechain = chain;
          const struct net *net = nft_net(pkt);
+       unsigned int stackptr = 0, jumps = 0;
          const struct nft_expr *expr, *last;
          const struct nft_rule_dp *rule;
          struct nft_regs regs;
-       unsigned int stackptr = 0;
          struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
          bool genbit = READ_ONCE(net->nft.gencursor);
          struct nft_rule_blob *blob;
@@ -314,6 +314,9 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
 
          switch (regs.verdict.code) {
          case NFT_JUMP:
+               jumps++;
+               if (WARN_ON_ONCE(jumps > 256))
+                       return NF_DROP;
                  if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
                          return NF_DROP;
                  jumpstack[stackptr].rule = nft_rule_next(rule);
Quoted 3 lines

Currently with enough jumps chained together and traffic generated, CPU can get stuck on nft_do_chain() triggering a kernel splat. If there is a solution on data plane it would be much better than this of course.

There is this patch:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

Quoted 3 lines
Quoted 1 line

I planned to push it upstream in this merge window.

This looks quite good. I tested it and seems to solve the problem, great!

This patch emits a WARN_ON_ONCE.

Can this be controlled from control plane instead?

#7 lore raw
Quoted 65 lines · Pablo Neira Ayuso
Quoted 64 lines · Fernando Fernandez Mancera
Quoted 61 lines · Florian Westphal
Quoted 7 lines · Fernando Fernandez Mancera
Quoted 6 lines · Florian Westphal
Quoted 3 lines

In addition I noticed that when a kernel splat happens the ruleset that triggered it isn't saved anywhere, it would be nice to save them so we have a reproducer right away.

I had such code but removed it for this version.

Quoted 8 lines
Quoted 4 lines

I can send a followup patch to re-add it but I think that it is better for kernel fuzzing to extend knft acordingly, as nft is restricted by the input grammar wrt. the nonsense that it can create.

That is fine for me, I still have pending to try knft which I might do this week if I have time. If we do not want to save which ruleset generated the kernel splat I would drop netlink-rw mode completely..

Hmmm.... I'm not sure on this. It would be a bit of a silly limitation. Its not like -rw adds a huge chunk of code.

The store code wasn't too bad, back then I added some scripting for allow to e.g. call nft flush ruleset periodically and that was more code than strictly needed for pure nft (-ro mode) fuzzing.

Quoted 30 lines

Yes, it seems we found the same issue. I do not have a solution on the control plane although I was about to send this patch for data plane.

net/netfilter/nf_tables_core.c +4 -1
index 6557a4018c09..ddc4943d082c 100644
@@ -251,10 +251,10 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
   {
          const struct nft_chain *chain = priv, *basechain = chain;
          const struct net *net = nft_net(pkt);
+       unsigned int stackptr = 0, jumps = 0;
          const struct nft_expr *expr, *last;
          const struct nft_rule_dp *rule;
          struct nft_regs regs;
-       unsigned int stackptr = 0;
          struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
          bool genbit = READ_ONCE(net->nft.gencursor);
          struct nft_rule_blob *blob;
@@ -314,6 +314,9 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
 
          switch (regs.verdict.code) {
          case NFT_JUMP:
+               jumps++;
+               if (WARN_ON_ONCE(jumps > 256))
+                       return NF_DROP;
                  if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
                          return NF_DROP;
                  jumpstack[stackptr].rule = nft_rule_next(rule);
Quoted 3 lines

Currently with enough jumps chained together and traffic generated, CPU can get stuck on nft_do_chain() triggering a kernel splat. If there is a solution on data plane it would be much better than this of course.

There is this patch:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I planned to push it upstream in this merge window.

This looks quite good. I tested it and seems to solve the problem, great!

This patch emits a WARN_ON_ONCE.

Can this be controlled from control plane instead?

Oh well, you refer to the patch that checks this from control plane.

I remember an issue with abort path, has this been addressed?

#8 lore raw
Quoted 70 lines · Pablo Neira Ayuso
Quoted 69 lines · Pablo Neira Ayuso
Quoted 67 lines · Fernando Fernandez Mancera
Quoted 54 lines · Florian Westphal
Quoted 7 lines · Fernando Fernandez Mancera
Quoted 6 lines · Florian Westphal
Quoted 3 lines

In addition I noticed that when a kernel splat happens the ruleset that triggered it isn't saved anywhere, it would be nice to save them so we have a reproducer right away.

I had such code but removed it for this version.

Quoted 8 lines
Quoted 4 lines

I can send a followup patch to re-add it but I think that it is better for kernel fuzzing to extend knft acordingly, as nft is restricted by the input grammar wrt. the nonsense that it can create.

That is fine for me, I still have pending to try knft which I might do this week if I have time. If we do not want to save which ruleset generated the kernel splat I would drop netlink-rw mode completely..

Hmmm.... I'm not sure on this. It would be a bit of a silly limitation. Its not like -rw adds a huge chunk of code.

The store code wasn't too bad, back then I added some scripting for allow to e.g. call nft flush ruleset periodically and that was more code than strictly needed for pure nft (-ro mode) fuzzing.

Quoted 30 lines

Yes, it seems we found the same issue. I do not have a solution on the control plane although I was about to send this patch for data plane.

net/netfilter/nf_tables_core.c +4 -1
index 6557a4018c09..ddc4943d082c 100644
@@ -251,10 +251,10 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
   {
          const struct nft_chain *chain = priv, *basechain = chain;
          const struct net *net = nft_net(pkt);
+       unsigned int stackptr = 0, jumps = 0;
          const struct nft_expr *expr, *last;
          const struct nft_rule_dp *rule;
          struct nft_regs regs;
-       unsigned int stackptr = 0;
          struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
          bool genbit = READ_ONCE(net->nft.gencursor);
          struct nft_rule_blob *blob;
@@ -314,6 +314,9 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
 
          switch (regs.verdict.code) {
          case NFT_JUMP:
+               jumps++;
+               if (WARN_ON_ONCE(jumps > 256))
+                       return NF_DROP;
                  if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
                          return NF_DROP;
                  jumpstack[stackptr].rule = nft_rule_next(rule);
Quoted 8 lines
Quoted 3 lines

Currently with enough jumps chained together and traffic generated, CPU can get stuck on nft_do_chain() triggering a kernel splat. If there is a solution on data plane it would be much better than this of course.

There is this patch:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

I planned to push it upstream in this merge window.

This looks quite good. I tested it and seems to solve the problem, great!

Quoted 7 lines
Quoted 3 lines

This patch emits a WARN_ON_ONCE.

Can this be controlled from control plane instead?

Oh well, you refer to the patch that checks this from control plane.

I remember an issue with abort path, has this been addressed?

I think this does not handle rule/set element removal with jump/goto correctly.

author maintainer
#9 lore raw
Quoted 8 lines · Pablo Neira Ayuso
Quoted 5 lines
Quoted 1 line

Can this be controlled from control plane instead?

Oh well, you refer to the patch that checks this from control plane.

I remember an issue with abort path, has this been addressed?

I think this does not handle rule/set element removal with jump/goto correctly.

I haven't reviewed it yet. There are buildbot warning reports and submitter seems to have abandoned this patch set.

I will review and toss it if needed, this bug exists forever so its not like we must apply this right away.

If absolutely needed i can scrape time next month to work on this.

#10 lore raw
Quoted 10 lines · Florian Westphal
Quoted 8 lines · Pablo Neira Ayuso
Quoted 5 lines
Quoted 1 line

Can this be controlled from control plane instead?

Oh well, you refer to the patch that checks this from control plane.

I remember an issue with abort path, has this been addressed?

I think this does not handle rule/set element removal with jump/goto correctly.

Quoted 7 lines

I haven't reviewed it yet. There are buildbot warning reports and submitter seems to have abandoned this patch set.

I will review and toss it if needed, this bug exists forever so its not like we must apply this right away.

If absolutely needed i can scrape time next month to work on this.

I think this is also broken from commit path, when two tables are validated, first succeeds then the second table fails. Leaving the first table with incorrect jump count.

author maintainer
#11 lore raw
Quoted 3 lines · Pablo Neira Ayuso

I think this is also broken from commit path, when two tables are validated, first succeeds then the second table fails. Leaving the first table with incorrect jump count.

OK, I marked it as changes-requested in patchwork.