Skip to contentSkip to conversation

evaluate: Fix for 'meta hour' ranges spanning date boundaries

PATCH Superseded Phil Sutter 2 participants 3 messages Jul 25, 2025 – Jul 29, 2025 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 evaluate: Fix for 'meta hour' ranges spanning date boundaries

Introduction of EXPR_RANGE_SYMBOL type inadvertently disabled sanitizing of meta hour ranges where the lower boundary has a higher value than the upper boundary. This may happen outside of user control due to the fact that given ranges are converted to UTC which is the kernel's native timezone.

Restore the conditional match and op inversion by matching on the new RHS expression type and also expand it so values are comparable. Since this replaces the whole range expression, make it replace the relational's RHS entirely.

While at it extend testsuites to cover these corner-cases.

Fixes 347039f64509e ("src: add symbol range expression to further compact intervals")
---
 src/evaluate.c                          |  25 +++--
 tests/py/any/meta.t                     |   6 ++
 tests/py/any/meta.t.json                | 123 ++++++++++++++++++++++++
 tests/py/any/meta.t.json.output         |  66 +++++++++++++
 tests/py/any/meta.t.payload             |  38 ++++++++
 tests/shell/testcases/listing/meta_time |  11 +++
 6 files changed, 262 insertions(+), 7 deletions(-)
src/evaluate.c +18 -7
index 9c90590860585..b67c81f01c0e2 100644
@@ -2421,10 +2421,9 @@ static int expr_evaluate_mapping(struct eval_ctx *ctx, struct expr **expr)
 	return 0;
 }
 
-static int expr_evaluate_symbol_range(struct eval_ctx *ctx, struct expr **exprp)
+static struct expr *symbol_range_expand(struct expr *expr)
 {
-	struct expr *left, *right, *range, *constant_range;
-	struct expr *expr = *exprp;
+	struct expr *left, *right;
 
 	/* expand to symbol and range expressions to consolidate evaluation. */
 	left = symbol_expr_alloc(&expr->location, expr->symtype,
@@ -2433,7 +2432,16 @@ static int expr_evaluate_symbol_range(struct eval_ctx *ctx, struct expr **exprp)
 	right = symbol_expr_alloc(&expr->location, expr->symtype,
 				  (struct scope *)expr->scope,
 				  expr->identifier_range[1]);
-	range = range_expr_alloc(&expr->location, left, right);
+	return range_expr_alloc(&expr->location, left, right);
+}
+
+static int expr_evaluate_symbol_range(struct eval_ctx *ctx, struct expr **exprp)
+{
+	struct expr *left, *right, *range, *constant_range;
+	struct expr *expr = *exprp;
+
+	/* expand to symbol and range expressions to consolidate evaluation. */
+	range = symbol_range_expand(expr);
 
 	if (expr_evaluate(ctx, &range) < 0) {
 		expr_free(range);
@@ -2772,12 +2780,15 @@ static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr)
 
 	pctx = eval_proto_ctx(ctx);
 
-	if (rel->right->etype == EXPR_RANGE && lhs_is_meta_hour(rel->left)) {
-		ret = __expr_evaluate_range(ctx, &rel->right);
+	if (lhs_is_meta_hour(rel->left) &&
+	    rel->right->etype == EXPR_RANGE_SYMBOL) {
+		range = symbol_range_expand(rel->right);
+		ret = __expr_evaluate_range(ctx, &range);
 		if (ret)
 			return ret;
 
-		range = rel->right;
+		expr_free(rel->right);
+		rel->right = range;
 
 		/*
 		 * We may need to do this for proper cross-day ranges,
tests/py/any/meta.t +6 -0
index 3f0ef121a8c03..0d0c268636705 100644
@@ -218,6 +218,12 @@ meta hour "17:00:00" drop;ok;meta hour "17:00" drop
 meta hour "17:00:01" drop;ok
 meta hour "00:00" drop;ok
 meta hour "00:01" drop;ok
+meta hour "01:01" drop;ok
+meta hour "02:02" drop;ok
+meta hour "03:03" drop;ok
+meta hour "00:00"-"02:02" drop;ok
+meta hour "01:01"-"03:03" drop;ok
+meta hour "02:02"-"04:04" drop;ok
 time < "2022-07-01 11:00:00" accept;ok;meta time < "2022-07-01 11:00:00" accept
 time > "2022-07-01 11:00:00" accept;ok;meta time > "2022-07-01 11:00:00" accept
 
tests/py/any/meta.t.json +123 -0

129 lines not on this page · raw message

tests/py/any/meta.t.json.output +66 -0

69 lines not on this page · raw message

tests/py/any/meta.t.payload +38 -0
index 52c3efa84eb5d..2abb44ea2e868 100644
@@ -1052,6 +1052,44 @@ ip meta-test input
   [ cmp eq reg 1 0x0001359c ]
   [ immediate reg 0 drop ]
 
+# meta hour "01:01" drop
+ip test-ip4 input
+  [ meta load hour => reg 1 ]
+  [ cmp eq reg 1 0x000143ac ]
+  [ immediate reg 0 drop ]
+
+# meta hour "02:02" drop
+ip test-ip4 input
+  [ meta load hour => reg 1 ]
+  [ cmp eq reg 1 0x00000078 ]
+  [ immediate reg 0 drop ]
+
+# meta hour "03:03" drop
+ip test-ip4 input
+  [ meta load hour => reg 1 ]
+  [ cmp eq reg 1 0x00000ec4 ]
+  [ immediate reg 0 drop ]
+
+# meta hour "00:00"-"02:02" drop
+  [ meta load hour => reg 1 ]
+  [ byteorder reg 1 = hton(reg 1, 4, 4) ]
+  [ range neq reg 1 0x78000000 0x60350100 ]
+  [ immediate reg 0 drop ]
+
+# meta hour "01:01"-"03:03" drop
+ip test-ip4 input
+  [ meta load hour => reg 1 ]
+  [ byteorder reg 1 = hton(reg 1, 4, 4) ]
+  [ range neq reg 1 0xc40e0000 0xac430100 ]
+  [ immediate reg 0 drop ]
+
+# meta hour "02:02"-"04:04" drop
+ip test-ip4 input
+  [ meta load hour => reg 1 ]
+  [ byteorder reg 1 = hton(reg 1, 4, 4) ]
+  [ range eq reg 1 0x78000000 0x101d0000 ]
+  [ immediate reg 0 drop ]
+
 # time < "2022-07-01 11:00:00" accept
 ip test-ip4 input
   [ meta load time => reg 1 ]
tests/shell/testcases/listing/meta_time +11 -0
index 96a9d5570fd14..61314a99c6498 100755
@@ -65,3 +65,14 @@ printf "\t\tmeta hour \"%02d:%02d\"-\"%02d:%02d\"\n" 5 0 16 0 >> "$TMP1"
 printf "\t\tmeta hour \"%02d:%02d\"-\"%02d:%02d\"\n" 6 0 17 0 >> "$TMP1"
 
 check_decode EADT
+
+$NFT flush chain t c
+TZ=UTC-2 $NFT add rule t c meta hour "00:00"-"01:00"
+TZ=UTC-2 $NFT add rule t c meta hour "00:00"-"03:00"
+TZ=UTC-2 $NFT add rule t c meta hour "01:00"-"04:00"
+
+printf "\t\tmeta hour \"%02d:%02d\"-\"%02d:%02d\"\n" 0 0 1 0 > "$TMP1"
+printf "\t\tmeta hour { \"02:00\"-\"03:00\", \"00:00\"-\"02:00\" }\n" >> "$TMP1"
+printf "\t\tmeta hour { \"02:00\"-\"04:00\", \"01:00\"-\"02:00\" }\n" >> "$TMP1"
+
+check_decode UTC-2
Signature · 1 line
2.49.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Phil Sutter

Introduction of EXPR_RANGE_SYMBOL type inadvertently disabled sanitizing of meta hour ranges where the lower boundary has a higher value than the upper boundary. This may happen outside of user control due to the fact that given ranges are converted to UTC which is the kernel's native timezone.

Restore the conditional match and op inversion by matching on the new RHS expression type and also expand it so values are comparable. Since this replaces the whole range expression, make it replace the relational's RHS entirely.

Thanks, I suspect this bug is related to this recent ticket:

https://bugzilla.netfilter.org/show_bug.cgi?id=1805

Quoted 1 line

While at it extend testsuites to cover these corner-cases.

Thanks for improving coverage for this.

Quoted 2 lines
Fixes 347039f64509e ("src: add symbol range expression to further compact intervals")

src/evaluate.c static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr) · line 2784

Quoted 8 lines
src/evaluate.c +2 -2
@@ -2772,12 +2780,15 @@ static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr)
 
 	pctx = eval_proto_ctx(ctx);
 
-	if (rel->right->etype == EXPR_RANGE && lhs_is_meta_hour(rel->left)) {
-		ret = __expr_evaluate_range(ctx, &rel->right);
+	if (lhs_is_meta_hour(rel->left) &&
+	    rel->right->etype == EXPR_RANGE_SYMBOL) {

Side note, thanks for reversing this check.

author reviewer
#2 lore raw
Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Phil Sutter

Introduction of EXPR_RANGE_SYMBOL type inadvertently disabled sanitizing of meta hour ranges where the lower boundary has a higher value than the upper boundary. This may happen outside of user control due to the fact that given ranges are converted to UTC which is the kernel's native timezone.

Quoted 8 lines
Quoted 4 lines

Restore the conditional match and op inversion by matching on the new RHS expression type and also expand it so values are comparable. Since this replaces the whole range expression, make it replace the relational's RHS entirely.

Thanks, I suspect this bug is related to this recent ticket:

https://bugzilla.netfilter.org/show_bug.cgi?id=1805

Ah yes, I forgot the "Closes:" tag, sorry!

Quoted 3 lines
Quoted 1 line

While at it extend testsuites to cover these corner-cases.

Thanks for improving coverage for this.

Above ticket also mentions how "23:59:60" is accepted when "24:00" is not. I'll send a v2 which describes the expected values in nft.8 to cover for this oddity in strptime() implementation.

Quoted 7 lines
Quoted 2 lines
Fixes 347039f64509e ("src: add symbol range expression to further compact intervals")
Quoted 2 lines
(quoted diff) +0 -0
@@ -2772,12 +2780,15 @@ static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr)
 
Quoted 8 lines

src/evaluate.c static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr) · line 2784

Quoted 6 lines
src/evaluate.c +2 -2
 	pctx = eval_proto_ctx(ctx);
 
-	if (rel->right->etype == EXPR_RANGE && lhs_is_meta_hour(rel->left)) {
-		ret = __expr_evaluate_range(ctx, &rel->right);
+	if (lhs_is_meta_hour(rel->left) &&
+	    rel->right->etype == EXPR_RANGE_SYMBOL) {

Side note, thanks for reversing this check.

I did it merely because consecutive lines being longer than the former ones seem more readable to me. What do you like about it? :)

Thanks, Phil