Skip to contentSkip to conversation

fix host-endian constant values in set lookup keys

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

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover fix host-endian constant values in set lookup keys

commit b422b07ab2f96436001f33dfdfd937238033c799
("src: permit use of constant values in set lookup keys") allows to do something like this:

set s { type ipv4_addr . ipv4_addr . inet_service .. } something add rule ip saddr . 1.2.3.4 . 80 @s goto c1

However, it turns out that this only works if the constant(s) (1.2.3.4 and 80) have BYTEORDER_BIG_ENDIAN type.

Combining a fixed value with a key portion of HOST type results in a crash (assertion).

First patch is a refactor, second patch adds needed byteorder conversions to deal with this problem.

Florian Westphal (2):
netlink_delinearize: move concat and value postprocessing to helpers
evaluate: permit use of host-endian constant values in set lookup keys

 src/evaluate.c                                | 65 +++++++------
 src/netlink_delinearize.c                     | 97 ++++++++++++-------
 .../packetpath/dumps/set_lookups.nft          | 51 ++++++++++
 tests/shell/testcases/packetpath/set_lookups  | 64 ++++++++++++
 .../sets/dumps/typeof_sets_concat.nft         | 11 +++
 5 files changed, 221 insertions(+), 67 deletions(-)
 create mode 100644 tests/shell/testcases/packetpath/dumps/set_lookups.nft
 create mode 100755 tests/shell/testcases/packetpath/set_lookups
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/2 netlink_delinearize: move concat and value postprocessing to helpers

The new helpers will be used in a followup patch to avoid need to annotate more things in *ctx when doing postprocessing of values embedded in a concatenation.

No functional changes intended.

---
 src/netlink_delinearize.c | 82 ++++++++++++++++++++++-----------------
 1 file changed, 47 insertions(+), 35 deletions(-)
src/netlink_delinearize.c +47 -35
index e21451044451..27630a8a9b34 100644
@@ -2720,6 +2720,50 @@ static struct expr *expr_postprocess_string(struct expr *expr)
 	return out;
 }
 
+static void expr_postprocess_value(struct rule_pp_ctx *ctx, struct expr **exprp)
+{
+	struct expr *expr = *exprp;
+
+	// FIXME
+	if (expr->byteorder == BYTEORDER_HOST_ENDIAN)
+		mpz_switch_byteorder(expr->value, expr->len / BITS_PER_BYTE);
+
+	if (expr_basetype(expr)->type == TYPE_STRING)
+		*exprp = expr_postprocess_string(expr);
+
+	expr = *exprp;
+	if (expr->dtype->basetype != NULL &&
+	    expr->dtype->basetype->type == TYPE_BITMASK)
+		*exprp = bitmask_expr_to_binops(expr);
+}
+
+static void expr_postprocess_concat(struct rule_pp_ctx *ctx, struct expr **exprp)
+{
+	struct expr *i, *n, *expr = *exprp;
+	unsigned int type = expr->dtype->type, ntype = 0;
+	int off = expr->dtype->subtypes;
+	const struct datatype *dtype;
+	LIST_HEAD(tmp);
+
+	assert(expr->etype == EXPR_CONCAT);
+
+	ctx->flags |= RULE_PP_IN_CONCATENATION;
+	list_for_each_entry_safe(i, n, &expr->expressions, list) {
+		if (type) {
+			dtype = concat_subtype_lookup(type, --off);
+			expr_set_type(i, dtype, dtype->byteorder);
+		}
+		list_del(&i->list);
+		expr_postprocess(ctx, &i);
+		list_add_tail(&i->list, &tmp);
+
+		ntype = concat_subtype_add(ntype, i->dtype->type);
+	}
+	ctx->flags &= ~RULE_PP_IN_CONCATENATION;
+	list_splice(&tmp, &expr->expressions);
+	__datatype_set(expr, concat_type_alloc(ntype));
+}
+
 static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
 {
 	struct dl_proto_ctx *dl = dl_proto_ctx(ctx);
@@ -2746,30 +2790,9 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
 		list_for_each_entry(i, &expr->expressions, list)
 			expr_postprocess(ctx, &i);
 		break;
-	case EXPR_CONCAT: {
-		unsigned int type = expr->dtype->type, ntype = 0;
-		int off = expr->dtype->subtypes;
-		const struct datatype *dtype;
-		LIST_HEAD(tmp);
-		struct expr *n;
-
-		ctx->flags |= RULE_PP_IN_CONCATENATION;
-		list_for_each_entry_safe(i, n, &expr->expressions, list) {
-			if (type) {
-				dtype = concat_subtype_lookup(type, --off);
-				expr_set_type(i, dtype, dtype->byteorder);
-			}
-			list_del(&i->list);
-			expr_postprocess(ctx, &i);
-			list_add_tail(&i->list, &tmp);
-
-			ntype = concat_subtype_add(ntype, i->dtype->type);
-		}
-		ctx->flags &= ~RULE_PP_IN_CONCATENATION;
-		list_splice(&tmp, &expr->expressions);
-		__datatype_set(expr, concat_type_alloc(ntype));
+	case EXPR_CONCAT:
+		expr_postprocess_concat(ctx, exprp);
 		break;
-	}
 	case EXPR_UNARY:
 		expr_postprocess(ctx, &expr->arg);
 		expr_set_type(expr, expr->arg->dtype, !expr->arg->byteorder);
@@ -2882,18 +2905,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
 		payload_dependency_kill(&dl->pdctx, expr, dl->pctx.family);
 		break;
 	case EXPR_VALUE:
-		// FIXME
-		if (expr->byteorder == BYTEORDER_HOST_ENDIAN)
-			mpz_switch_byteorder(expr->value, expr->len / BITS_PER_BYTE);
-
-		if (expr_basetype(expr)->type == TYPE_STRING)
-			*exprp = expr_postprocess_string(expr);
-
-		expr = *exprp;
-		if (expr->dtype->basetype != NULL &&
-		    expr->dtype->basetype->type == TYPE_BITMASK)
-			*exprp = bitmask_expr_to_binops(expr);
-
+		expr_postprocess_value(ctx, exprp);
 		break;
 	case EXPR_RANGE:
 		expr_postprocess(ctx, &expr->left);
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/2 evaluate: permit use of host-endian constant values in set lookup keys

In reply to Florian Westphal

AFL found following crash:

table ip filter {
	map ipsec_in {
		typeof ipsec in reqid . iif : verdict
		flags interval
	}

	chain INPUT {
		type filter hook input priority filter; policy drop;
		ipsec in reqid . 0 @ipsec_in
	}
}

Which yields:
nft: evaluate.c:1213: expr_evaluate_unary: Assertion `!expr_is_constant(arg)' failed.

All existing test cases with constant values use big endian values, but "iif" expects host endian values.

As raw values were not supported before, concat byteorder conversion doesn't handle constants.

Fix this:

1. Add constant handling so that the number is converted in-place, without unary expression.

2. Add the inverse handling on delinearization for non-interval set types.
When dissecting the concat data soup, watch for integer constants where the datatype indicates host endian integer.

Last, extend an existing test case with the afl input to cover in/output.

A new test case is added to test linearization, delinearization and matching.

Fixes b422b07ab2f9 ("src: permit use of constant values in set lookup keys")
---
 src/evaluate.c                                | 65 ++++++++++---------
 src/netlink_delinearize.c                     | 27 ++++++--
 .../packetpath/dumps/set_lookups.nft          | 51 +++++++++++++++
 tests/shell/testcases/packetpath/set_lookups  | 64 ++++++++++++++++++
 .../sets/dumps/typeof_sets_concat.nft         | 11 ++++
 5 files changed, 180 insertions(+), 38 deletions(-)
 create mode 100644 tests/shell/testcases/packetpath/dumps/set_lookups.nft
 create mode 100755 tests/shell/testcases/packetpath/set_lookups
src/evaluate.c +33 -32
index 5a25916506fc..3cfbb6c10767 100644
@@ -174,42 +174,12 @@ static enum ops byteorder_conversion_op(struct expr *expr,
 	    expr->byteorder, byteorder);
 }
 
-static int byteorder_conversion(struct eval_ctx *ctx, struct expr **expr,
-				enum byteorder byteorder)
+static int byteorder_convert_expr(struct eval_ctx *ctx, struct expr **expr,
+				  enum byteorder byteorder)
 {
 	enum datatypes basetype;
 	enum ops op;
 
-	assert(!expr_is_constant(*expr) || expr_is_singleton(*expr));
-
-	if ((*expr)->byteorder == byteorder)
-		return 0;
-
-	/* Conversion for EXPR_CONCAT is handled for single composing ranges */
-	if ((*expr)->etype == EXPR_CONCAT) {
-		struct expr *i, *next, *unary;
-
-		list_for_each_entry_safe(i, next, &(*expr)->expressions, list) {
-			if (i->byteorder == BYTEORDER_BIG_ENDIAN)
-				continue;
-
-			basetype = expr_basetype(i)->type;
-			if (basetype == TYPE_STRING)
-				continue;
-
-			assert(basetype == TYPE_INTEGER);
-
-			op = byteorder_conversion_op(i, byteorder);
-			unary = unary_expr_alloc(&i->location, op, i);
-			if (expr_evaluate(ctx, &unary) < 0)
-				return -1;
-
-			list_replace(&i->list, &unary->list);
-		}
-
-		return 0;
-	}
-
 	basetype = expr_basetype(*expr)->type;
 	switch (basetype) {
 	case TYPE_INTEGER:
@@ -235,6 +205,37 @@ static int byteorder_conversion(struct eval_ctx *ctx, struct expr **expr,
 	return 0;
 }
 
+static int byteorder_conversion(struct eval_ctx *ctx, struct expr **expr,
+				enum byteorder byteorder)
+{
+	assert(!expr_is_constant(*expr) || expr_is_singleton(*expr));
+
+	if ((*expr)->byteorder == byteorder)
+		return 0;
+
+	/* Conversion for EXPR_CONCAT is handled for single composing ranges */
+	if ((*expr)->etype == EXPR_CONCAT) {
+		struct expr *i, *next;
+
+		list_for_each_entry_safe(i, next, &(*expr)->expressions, list) {
+			struct expr *unary = i;
+
+			if (i->byteorder == BYTEORDER_BIG_ENDIAN)
+				continue;
+
+			if (byteorder_convert_expr(ctx, &unary, byteorder) < 0)
+				return -1;
+
+			if (i != unary)
+				list_replace(&i->list, &unary->list);
+		}
+
+		return 0;
+	}
+
+	return byteorder_convert_expr(ctx, expr, byteorder);
+}
+
 static int table_not_found(struct eval_ctx *ctx)
 {
 	struct table *table;
src/netlink_delinearize.c +21 -6
index 27630a8a9b34..fe0bbc49e0d3 100644
@@ -2720,12 +2720,14 @@ static struct expr *expr_postprocess_string(struct expr *expr)
 	return out;
 }
 
-static void expr_postprocess_value(struct rule_pp_ctx *ctx, struct expr **exprp)
+static void expr_postprocess_value(struct rule_pp_ctx *ctx, struct expr **exprp,
+				   const struct set *set)
 {
+	bool interval = set && set->flags & NFT_SET_INTERVAL;
 	struct expr *expr = *exprp;
 
 	// FIXME
-	if (expr->byteorder == BYTEORDER_HOST_ENDIAN)
+	if (expr->byteorder == BYTEORDER_HOST_ENDIAN && !interval)
 		mpz_switch_byteorder(expr->value, expr->len / BITS_PER_BYTE);
 
 	if (expr_basetype(expr)->type == TYPE_STRING)
@@ -2737,7 +2739,8 @@ static void expr_postprocess_value(struct rule_pp_ctx *ctx, struct expr **exprp)
 		*exprp = bitmask_expr_to_binops(expr);
 }
 
-static void expr_postprocess_concat(struct rule_pp_ctx *ctx, struct expr **exprp)
+static void expr_postprocess_concat(struct rule_pp_ctx *ctx, struct expr **exprp,
+				    const struct set *set)
 {
 	struct expr *i, *n, *expr = *exprp;
 	unsigned int type = expr->dtype->type, ntype = 0;
@@ -2754,7 +2757,15 @@ static void expr_postprocess_concat(struct rule_pp_ctx *ctx, struct expr **exprp
 			expr_set_type(i, dtype, dtype->byteorder);
 		}
 		list_del(&i->list);
-		expr_postprocess(ctx, &i);
+
+		switch (i->etype) {
+		case EXPR_VALUE:
+			expr_postprocess_value(ctx, &i, set);
+			break;
+		default:
+			expr_postprocess(ctx, &i);
+			break;
+		}
 		list_add_tail(&i->list, &tmp);
 
 		ntype = concat_subtype_add(ntype, i->dtype->type);
@@ -2791,7 +2802,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
 			expr_postprocess(ctx, &i);
 		break;
 	case EXPR_CONCAT:
-		expr_postprocess_concat(ctx, exprp);
+		expr_postprocess_concat(ctx, exprp, NULL);
 		break;
 	case EXPR_UNARY:
 		expr_postprocess(ctx, &expr->arg);
@@ -2864,10 +2875,14 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
 			return;
 		case EXPR_CONCAT:
 			if (expr->right->etype == EXPR_SET_REF) {
+				const struct set *set = expr->right->set;
+
 				assert(expr->left->dtype == &invalid_type);
 				assert(expr->right->dtype != &invalid_type);
 
 				datatype_set(expr->left, expr->right->dtype);
+				expr_postprocess_concat(ctx, &expr->left, set);
+				break;
 			}
 			expr_postprocess(ctx, &expr->left);
 			break;
@@ -2905,7 +2920,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
 		payload_dependency_kill(&dl->pdctx, expr, dl->pctx.family);
 		break;
 	case EXPR_VALUE:
-		expr_postprocess_value(ctx, exprp);
+		expr_postprocess_value(ctx, exprp, NULL);
 		break;
 	case EXPR_RANGE:
 		expr_postprocess(ctx, &expr->left);
tests/shell/testcases/packetpath/dumps/set_lookups.nft new file +51 -0

51 lines not on this page · raw message

tests/shell/testcases/packetpath/set_lookups new file +64 -0

64 lines not on this page · raw message

tests/shell/testcases/sets/dumps/typeof_sets_concat.nft +11 -0
index dbaf7cdc2d7d..348b58487d5c 100644
@@ -10,3 +10,14 @@ table netdev t {
 		ether type != 8021q update @s { ether daddr . 123 timeout 1m } counter packets 0 bytes 0 return
 	}
 }
+table ip t {
+	set s {
+		typeof ipsec in reqid . iif
+		size 16
+		flags interval
+	}
+
+	chain c2 {
+		ipsec in reqid . "lo" @s
+	}
+}
Signature · 1 line
2.43.0