Skip to contentSkip to conversation

evaluate: add more checks for '... set 1-3'

PATCH New Florian Westphal 1 participant 3 messages Jan 11, 2024 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover evaluate: add more checks for '... set 1-3'

Only a few statements can accept expressions with ranges, all others will trigger an assertion in the netlink linearization path.

Add missing checks and fix the existing tproxy ones.

Florian Westphal (2):
evaluate: tproxy: move range error checks after arg evaluation
evaluate: add missing range checks for dup,fwd and payload statements

 src/evaluate.c                                | 100 ++++++++++--------
 .../testcases/bogons/nft-f/dup_fwd_ranges     |  14 +++
 .../testcases/bogons/nft-f/tproxy_ranges      |   8 ++
 .../nft-f/unknown_expr_type_range_assert      |   8 +-
 4 files changed, 83 insertions(+), 47 deletions(-)
 create mode 100644 tests/shell/testcases/bogons/nft-f/dup_fwd_ranges
 create mode 100644 tests/shell/testcases/bogons/nft-f/tproxy_ranges
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/2 evaluate: tproxy: move range error checks after arg evaluation

Testing for range before evaluation will still crash us later during netlink linearization, prefixes turn into ranges, symbolic expression might hide a range/prefix.

So move this after the argument has been evaluated.

---
 src/evaluate.c                                   | 12 ++++++------
 tests/shell/testcases/bogons/nft-f/tproxy_ranges |  8 ++++++++
 2 files changed, 14 insertions(+), 6 deletions(-)
 create mode 100644 tests/shell/testcases/bogons/nft-f/tproxy_ranges
src/evaluate.c +6 -6
index d18d65428d51..ff42d97d32e0 100644
@@ -4154,22 +4154,22 @@ static int stmt_evaluate_tproxy(struct eval_ctx *ctx, struct stmt *stmt)
 		return err;
 
 	if (stmt->tproxy.addr != NULL) {
-		if (stmt->tproxy.addr->etype == EXPR_RANGE)
-			return stmt_error(ctx, stmt, "Address ranges are not supported for tproxy.");
-
 		err = stmt_evaluate_addr(ctx, stmt, &stmt->tproxy.family,
 					 &stmt->tproxy.addr);
-
 		if (err < 0)
 			return err;
+
+		if (stmt->tproxy.addr->etype == EXPR_RANGE)
+			return stmt_error(ctx, stmt, "Address ranges are not supported for tproxy.");
 	}
 
 	if (stmt->tproxy.port != NULL) {
-		if (stmt->tproxy.port->etype == EXPR_RANGE)
-			return stmt_error(ctx, stmt, "Port ranges are not supported for tproxy.");
 		err = nat_evaluate_transport(ctx, stmt, &stmt->tproxy.port);
 		if (err < 0)
 			return err;
+
+		if (stmt->tproxy.port->etype == EXPR_RANGE)
+			return stmt_error(ctx, stmt, "Port ranges are not supported for tproxy.");
 	}
 
 	return 0;
tests/shell/testcases/bogons/nft-f/tproxy_ranges new file +8 -0
new file mode 100644
index 000000000000..1230860e3dfe
@@ -0,0 +1,8 @@
+define range = 42-80
+
+table t {
+	chain c {
+		tcp dport 42 tproxy to 192.168.0.1:$range
+		tcp dport 42 tproxy to 192.168.0.0/16
+	}
+}
Signature · 1 line
2.41.0
author maintainer
#2 lore raw

PATCH v2 2/2 evaluate: add missing range checks for dup,fwd and payload statements

In reply to Florian Westphal

Else we assert with:

BUG: unknown expression type range

nft: src/netlink_linearize.c:912: netlink_gen_expr: Assertion `0' failed.

While at it, condense meta and exthdr to reuse the same helper.

---
This supersedes:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

 src/evaluate.c                                | 88 +++++++++++--------
 .../testcases/bogons/nft-f/dup_fwd_ranges     | 14 +++
 .../nft-f/unknown_expr_type_range_assert      |  8 +-
 3 files changed, 69 insertions(+), 41 deletions(-)
 create mode 100644 tests/shell/testcases/bogons/nft-f/dup_fwd_ranges
src/evaluate.c +50 -38
index ff42d97d32e0..319efd21d8a0 100644
@@ -74,6 +74,33 @@ static int __fmtstring(3, 4) set_error(struct eval_ctx *ctx,
 	return -1;
 }
 
+static const char *stmt_name(const struct stmt *stmt)
+{
+	switch (stmt->ops->type) {
+	case STMT_NAT:
+		switch (stmt->nat.type) {
+		case NFT_NAT_SNAT:
+			return "snat";
+		case NFT_NAT_DNAT:
+			return "dnat";
+		case NFT_NAT_REDIR:
+			return "redirect";
+		case NFT_NAT_MASQ:
+			return "masquerade";
+		}
+		break;
+	default:
+		break;
+	}
+
+	return stmt->ops->name;
+}
+
+static int stmt_error_range(struct eval_ctx *ctx, const struct stmt *stmt, const struct expr *e)
+{
+	return expr_error(ctx->msgs, e, "%s: range argument not supported", stmt_name(stmt));
+}
+
 static void key_fix_dtype_byteorder(struct expr *key)
 {
 	const struct datatype *dtype = key->dtype;
@@ -3105,13 +3132,8 @@ static int stmt_evaluate_exthdr(struct eval_ctx *ctx, struct stmt *stmt)
 	if (ret < 0)
 		return ret;
 
-	switch (stmt->exthdr.val->etype) {
-	case EXPR_RANGE:
-		return expr_error(ctx->msgs, stmt->exthdr.val,
-				   "cannot be a range");
-	default:
-		break;
-	}
+	if (stmt->exthdr.val->etype == EXPR_RANGE)
+		return stmt_error_range(ctx, stmt, stmt->exthdr.val);
 
 	return 0;
 }
@@ -3144,6 +3166,9 @@ static int stmt_evaluate_payload(struct eval_ctx *ctx, struct stmt *stmt)
 				 payload->byteorder) < 0)
 		return -1;
 
+	if (stmt->payload.val->etype == EXPR_RANGE)
+		return stmt_error_range(ctx, stmt, stmt->payload.val);
+
 	need_csum = stmt_evaluate_payload_need_csum(payload);
 
 	if (!payload_needs_adjustment(payload)) {
@@ -3305,15 +3330,8 @@ static int stmt_evaluate_meta(struct eval_ctx *ctx, struct stmt *stmt)
 	if (ret < 0)
 		return ret;
 
-	switch (stmt->meta.expr->etype) {
-	case EXPR_RANGE:
-		ret = expr_error(ctx->msgs, stmt->meta.expr,
-				 "Meta expression cannot be a range");
-		break;
-	default:
-		break;
-
-	}
+	if (stmt->meta.expr->etype == EXPR_RANGE)
+		return stmt_error_range(ctx, stmt, stmt->meta.expr);
 
 	return ret;
 }
@@ -3336,6 +3354,9 @@ static int stmt_evaluate_ct(struct eval_ctx *ctx, struct stmt *stmt)
 		return stmt_error(ctx, stmt,
 				  "ct secmark must not be set to constant value");
 
+	if (stmt->ct.expr->etype == EXPR_RANGE)
+		return stmt_error_range(ctx, stmt, stmt->ct.expr);
+
 	return 0;
 }
 
@@ -3853,28 +3874,6 @@ static int nat_evaluate_transport(struct eval_ctx *ctx, struct stmt *stmt,
 	return 0;
 }
 
-static const char *stmt_name(const struct stmt *stmt)
-{
-	switch (stmt->ops->type) {
-	case STMT_NAT:
-		switch (stmt->nat.type) {
-		case NFT_NAT_SNAT:
-			return "snat";
-		case NFT_NAT_DNAT:
-			return "dnat";
-		case NFT_NAT_REDIR:
-			return "redirect";
-		case NFT_NAT_MASQ:
-			return "masquerade";
-		}
-		break;
-	default:
-		break;
-	}
-
-	return stmt->ops->name;
-}
-
 static int stmt_evaluate_l3proto(struct eval_ctx *ctx,
 				 struct stmt *stmt, uint8_t family)
 {
@@ -4272,6 +4271,9 @@ static int stmt_evaluate_dup(struct eval_ctx *ctx, struct stmt *stmt)
 						&stmt->dup.dev);
 			if (err < 0)
 				return err;
+
+			if (stmt->dup.dev->etype == EXPR_RANGE)
+				return stmt_error_range(ctx, stmt, stmt->dup.dev);
 		}
 		break;
 	case NFPROTO_NETDEV:
@@ -4290,6 +4292,10 @@ static int stmt_evaluate_dup(struct eval_ctx *ctx, struct stmt *stmt)
 	default:
 		return stmt_error(ctx, stmt, "unsupported family");
 	}
+
+	if (stmt->dup.to->etype == EXPR_RANGE)
+		return stmt_error_range(ctx, stmt, stmt->dup.to);
+
 	return 0;
 }
 
@@ -4311,6 +4317,9 @@ static int stmt_evaluate_fwd(struct eval_ctx *ctx, struct stmt *stmt)
 		if (err < 0)
 			return err;
 
+		if (stmt->fwd.dev->etype == EXPR_RANGE)
+			return stmt_error_range(ctx, stmt, stmt->fwd.dev);
+
 		if (stmt->fwd.addr != NULL) {
 			switch (stmt->fwd.family) {
 			case NFPROTO_IPV4:
@@ -4329,6 +4338,9 @@ static int stmt_evaluate_fwd(struct eval_ctx *ctx, struct stmt *stmt)
 						&stmt->fwd.addr);
 			if (err < 0)
 				return err;
+
+			if (stmt->fwd.addr->etype == EXPR_RANGE)
+				return stmt_error_range(ctx, stmt, stmt->fwd.addr);
 		}
 		break;
 	default:
tests/shell/testcases/bogons/nft-f/dup_fwd_ranges new file +14 -0
new file mode 100644
index 000000000000..efaff9e542c0
@@ -0,0 +1,14 @@
+define dev = "1"-"2"
+
+table netdev t {
+	chain c {
+		fwd to 1-2
+		dup to 1-2
+	}
+}
+
+table ip t {
+	chain c {
+		dup to 1-2 device $dev
+	}
+}
tests/shell/testcases/bogons/nft-f/unknown_expr_type_range_assert +5 -3
index 234dd623167d..e6206736f120 100644
@@ -1,5 +1,7 @@
 table ip x {
-        chain k {
-                meta mark set 0x001-3434
-        }
+	chain k {
+		meta mark set 0x001-3434
+		ct mark set 0x001-3434
+		tcp dport set 1-3
+	}
 }
Signature · 1 line
2.41.0