From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Phil Sutter <phil@nwl.cc>
Cc: Avinash Duduskar <avinash.duduskar@gmail.com>,
netfilter-devel@vger.kernel.org
Subject: Re: [PATCH nft] evaluate: reject negative values for unsigned datatypes
Date: Thu, 13 Aug 2026 14:58:41 +0200 [thread overview]
Message-ID: <an2_gW-ZhrAACQmb@chamomile> (raw)
In-Reply-To: <an2o0fusJ0K-_CbS@orbyte.nwl.cc>
On Thu, Aug 13, 2026 at 01:21:53PM +0200, Phil Sutter wrote:
> On Sat, Aug 08, 2026 at 07:03:18AM +0530, Avinash Duduskar wrote:
> > 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 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 a negative value, unless the evaluation context is a chain
> > priority, which is signed.
> >
> > Fixes: cb7cb885d65e ("evaluate: add expr_evaluate_integer()")
> > Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
> > Signed-off-by: Avinash Duduskar <avinash.duduskar@gmail.com>
> > ---
> > src/evaluate.c | 12 +++++
> > tests/py/any/meta.t | 2 +
> > .../parsing/dumps/negative_values_0.nodump | 0
> > .../shell/testcases/parsing/negative_values_0 | 46 +++++++++++++++++++
> > 4 files changed, 60 insertions(+)
> > create mode 100644 tests/shell/testcases/parsing/dumps/negative_values_0.nodump
> > create mode 100755 tests/shell/testcases/parsing/negative_values_0
> >
> > diff --git a/src/evaluate.c b/src/evaluate.c
> > index 8bb7b609..f5b88c0b 100644
> > --- a/src/evaluate.c
> > +++ b/src/evaluate.c
> > @@ -447,6 +447,18 @@ static int expr_evaluate_integer(struct eval_ctx *ctx, struct expr **exprp)
> > return -1;
> > }
> >
> > + /* chain priorities are signed, everything else is an unsigned key:
> > + * mpz_export() drops the sign, so "-1" would silently become 1.
> > + */
> > + if (mpz_sgn(expr->value) < 0 && ctx->ectx.dtype != &priority_type) {
>
> Although it might remain the only case, I don't like the special-casing
> here. Looking at struct datatype, we have a 32bit field for flags with
> only two bits used yet. Maybe introduce an 'is_signed' bit to check
> here?
Yes, I wonder if we can do this in a more generic way, like specifying
in the datatype itself the min and maximum value expected from the
integer. I don't expect "signed 128-bit only". Maybe add a new
interface to struct datatype to validate the value is within the
boundaries? Maybe something like:
if (ctx->ectx.dtype && ctx->ectx.dtype->validate) {
erec = ctx->ectx.dtype->validate(expr->value));
if (erec)
return -1;
}
prev parent reply other threads:[~2026-08-13 12:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-08 1:33 [PATCH nft] evaluate: reject negative values for unsigned datatypes Avinash Duduskar
2026-08-13 11:21 ` Phil Sutter
2026-08-13 12:58 ` Pablo Neira Ayuso [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=an2_gW-ZhrAACQmb@chamomile \
--to=pablo@netfilter.org \
--cc=avinash.duduskar@gmail.com \
--cc=netfilter-devel@vger.kernel.org \
--cc=phil@nwl.cc \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.