evaluate: reject negative values for unsigned datatypes
PATCH v2 1/1 evaluate: reject negative values for unsigned datatypes
expr_evaluate_integer() only tests the upper bound, so a negative value passes the range check 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 }
}
}The json frontend has it twice over: "elem": ["-1"] and the bare number "elem": [-1] are both accepted and land as element 1 the same way.
The error string has read "Value %s exceeds valid range 0-%s" since the check was added, so the contract was already unsigned; only the upper half of it was enforced. The result is not a wrap either: "-1" gives 1 while "-4294967295" gives 0xffffffff.
Reject any negative value. Chain and flowtable priorities are signed but never reach this check as a negative mpz: bare and json numeric priorities are built as raw C ints by both frontends, symbols are rebuilt from atoi() in priority_type_parse(), and the name-plus-offset forms are computed as C ints in evaluate_priority().
---
v2: drop the priority_type special case, it can never see a negative
mpz (Phil, Pablo); name the json string and number forms in the message and cover them plus flowtable priority in the test
src/evaluate.c | 10 +++ tests/py/any/meta.t | 2 + .../parsing/dumps/negative_values_0.nodump | 0 .../shell/testcases/parsing/negative_values_0 | 70 +++++++++++++++++++ 4 files changed, 82 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
@@ -447,6 +447,16 @@ static int expr_evaluate_integer(struct eval_ctx *ctx, struct expr **exprp)return -1 ; } +/* 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->stmt_len > ctx->ectx.len) masklen = ctx->stmt_len;else
tests/py/any/meta.t +2 -0
@@ -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
tests/shell/testcases/parsing/negative_values_0 new file +70 -0
70 lines not on this page · raw message
base-commit: 4f54425ea59250dcd02518ef4a50e6c0b7c162bf
Signature · 1 line
2.55.0