Skip to contentSkip to conversation

evalute: don't BUG on unexpected base datatype

PATCH In review Florian Westphal 3 participants 5 messages Jun 13, 2025 – Jun 17, 2025 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 evalute: don't BUG on unexpected base datatype

Included bogo will cause a crash but this is the evaluation stage where we can just emit an error instead.

---
I wonder if we should just replace all BUGs in evaluate.c with expr_error() calls, it avoids constant whack-a-mole.

 src/evaluate.c                                        |  3 ++-
 .../bogons/nft-f/invalid_basetype_verdict_assert      | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 tests/shell/testcases/bogons/nft-f/invalid_basetype_verdict_assert
src/evaluate.c +2 -1
index 9c7f23cb080e..de054f82d55f 100644
@@ -482,7 +482,8 @@ static int expr_evaluate_value(struct eval_ctx *ctx, struct expr **expr)
 			return -1;
 		break;
 	default:
-		BUG("invalid basetype %s\n", expr_basetype(*expr)->name);
+		return expr_error(ctx->msgs, *expr, "Unexpected basetype %s",
+				  expr_basetype(*expr)->name);
 	}
 	return 0;
 }
tests/shell/testcases/bogons/nft-f/invalid_basetype_verdict_assert new file +11 -0
new file mode 100644
index 000000000000..f85ce7fe342c
@@ -0,0 +1,11 @@
+table ip t {
+	map m {
+		type ipv4_addr . inet_service : ipv4_addr .  verdict
+		elements = { 10.0.0.1 . 42 : 10.1.1.1 . 0 }
+	}
+
+	chain c {
+		type nat hook prerouting priority dstnat; policy accept;
+		dnat ip to ip saddr . tcp dport map @m
+	}
+}
Signature · 1 line
2.49.0
reviewer
#1 lore raw
Quoted 7 lines · Florian Westphal

Included bogo will cause a crash but this is the evaluation stage where we can just emit an error instead.

---
I wonder if we should just replace all BUGs in evaluate.c with expr_error() calls, it avoids constant whack-a-mole.

I guess the expectation was that bison catches these but I fear JSON parser has weakened that quite a bit.

I wish libnftables to well-behave in error cases unless critical ones like ENOMEM.

Cheers, Phil

#2 lore raw
Quoted 8 lines · Phil Sutter
Quoted 7 lines · Florian Westphal

Included bogo will cause a crash but this is the evaluation stage where we can just emit an error instead.

---
I wonder if we should just replace all BUGs in evaluate.c with expr_error() calls, it avoids constant whack-a-mole.

I think that can help uncover bugs, or are those json induced bugs?

Quoted 2 lines

I guess the expectation was that bison catches these but I fear JSON parser has weakened that quite a bit.

It would be good to harden json parser to reject trivial non-sense, no need to postpone this to the evaluation phase. If fuzzer can help in that regard. I understand some issues can be more easily identified from the evaluation step. I am not telling to only handle this from the parser, I mean "it depends" on the issue.

Quoted 2 lines

I wish libnftables to well-behave in error cases unless critical ones like ENOMEM.

Yes, I guess that's completary to my request above.

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

I wonder if we should just replace all BUGs in evaluate.c with expr_error() calls, it avoids constant whack-a-mole.

I think that can help uncover bugs, or are those json induced bugs?

This one is bison, see included bogon input.
I meant in general.

How do these BUGs help at all?

#4 lore raw

Hi Florian,

Quoted 7 lines · Florian Westphal
Quoted 4 lines · Pablo Neira Ayuso
Quoted 2 lines
Quoted 2 lines

I wonder if we should just replace all BUGs in evaluate.c with expr_error() calls, it avoids constant whack-a-mole.

I think that can help uncover bugs, or are those json induced bugs?

This one is bison, see included bogon input.

For this particular case I think it makes sense to turn it into error.

Nitpick for this patch: I'd suggest this error:

+ return expr_error(ctx->msgs, *expr, "Unexpected datatype %s",
+ (*expr)->dtype->name);

Which results in:

[...]
ruleset:3:61-67: Error: Unexpected datatype verdict
type ipv4_addr . inet_service : ipv4_addr . verdict
^^^^^^^

Quoted 1 line

I meant in general.

It's not so straight forward, I have quickly browsed the existing BUG() calls and, unlike this one, they are mostly trapping nonsense, I guess we can find a way to keep them around while not invoking assert().

Quoted 1 line

How do these BUGs help at all?

Maybe you can turn them into error record adding "BUG:" prefix if you prefer, I guess that will not crash a daemon process which is your concern?

ie. add expr_bug() to highlight an input is triggering a buggy internal state so this can still be reported without crashing the daemon process?