Skip to contentSkip to conversation

evaluate: reject negative values for unsigned datatypes

PATCH New Avinash Duduskar 1 participant 1 message Sep 11 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 evaluate: reject negative values for unsigned datatypes

expr_evaluate_integer() only checks the upper bound, so a negative value passes and mpz_export() then drops the sign:

  # nft add element ip t m { "-1" }
  # nft list set ip t m
  table ip t {
          set m {
                  type mark
                  elements = { 0x00000001 }
          }
  }

Same through json, both as "elem": ["-1"] and as "elem": [-1].

Reject a negative value. Priorities are unaffected: integer_type_parse() is the only caller of mpz_set_str() outside mini-gmp, and priority_type_parse() discards its result for an atoi() int before evaluation.

Values from a define are not covered. initializer_expr's DASH NUM rule loses the sign before evaluation, so "define x = -1" still lands as 0xffffffff on an unsigned type. Separate bug, left alone.

---
v3: sign check moved ahead of the maxval test. No other code change.
Commit message now records that a define loses the sign before evaluation, so those are out of scope.

v2 dropped the priority special case Phil objected to and had no reply. Pablo, this still does not add the ->validate interface you asked for. I did not want a new datatype hook and its per-type min/max in the same patch as the fix, but I will do that first as a prerequisite if it is the shape you want.

v2: https://lore.kernel.org/netfilter-devel/[email protected]/
Tested on fcef13a3: shell 507 ok / 6 skipped / 0 failed, py 2083
tests 0 error.

 src/evaluate.c                                | 10 +++
 tests/py/any/meta.t                           |  2 +
 .../parsing/dumps/negative_values_0.nodump    |  0
 .../shell/testcases/parsing/negative_values_0 | 71 +++++++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 tests/shell/testcases/parsing/dumps/negative_values_0.nodump
 create mode 100755 tests/shell/testcases/parsing/negative_values_0
src/evaluate.c +10 -0
index 8115867f..7af800d3 100644
@@ -437,6 +437,16 @@ static int expr_evaluate_integer(struct eval_ctx *ctx, struct expr **exprp)
 	uint32_t masklen;
 	mpz_t mask;
 
+	/* mpz_export() ignores the sign, so "-1" would silently become 1. */
+	if (mpz_sgn(expr->value) < 0) {
+		valstr = mpz_get_str(NULL, 10, expr->value);
+		expr_error(ctx->msgs, expr,
+			   "Value %s is negative, expecting an unsigned value",
+			   valstr);
+		nft_gmp_free(valstr);
+		return -1;
+	}
+
 	if (ctx->ectx.maxval > 0 &&
 	    mpz_cmp_ui(expr->value, ctx->ectx.maxval) > 0) {
 		valstr = mpz_get_str(NULL, 10, expr->value);
tests/py/any/meta.t +2 -0
index c5ab2ad9..4f486307 100644
@@ -56,6 +56,8 @@ meta mark and 0x03 == 0x01;ok;meta mark & 0x00000003 == 0x00000001
 meta mark and 0x03 != 0x01;ok;meta mark & 0x00000003 != 0x00000001
 meta mark 0x10;ok;meta mark 0x00000010
 meta mark != 0x10;ok;meta mark != 0x00000010
+meta mark "-1";fail
+meta mark "-4294967295";fail
 meta mark 0xffffff00/24;ok;meta mark & 0xffffff00 == 0xffffff00
 
 meta mark or 0x03 == 0x01;ok;meta mark | 0x00000003 == 0x00000001
tests/shell/testcases/parsing/dumps/negative_values_0.nodump new file +0 -0
new file mode 100644
index 00000000..e69de29b
tests/shell/testcases/parsing/negative_values_0 new file +71 -0

71 lines not on this page · raw message

base-commit: fcef13a358a0755a0bd980910d47989b064a756d

Signature · 1 line
2.55.0