From: netdev-bot+sashiko@kernel.org
To: zhangminghui01@kylinos.cn
Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
pabeni@redhat.com, willemb@google.com, shuah@kernel.org,
netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
horms@kernel.org, dsahern@kernel.org
Subject: Re: [PATCH net] selftests: nettest: reject out-of-range integer values
Date: Tue, 01 Dec 2026 12:23:51 +0000 [thread overview]
Message-ID: <179612783154.3146.2116613211232217894@kernel.org> (raw)
In-Reply-To: <20261130115512.57185-1-zhangminghui01@kylinos.cn>
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The rewritten str_to_uint() in
tools/testing/selftests/net/nettest.c still accepts some out-of-range…
- [Low] selftests: nettest: str_to_uint() accepts wrapped negative values
via strtoul()
--
selftests: nettest: reject out-of-range integer values
This patch changes str_to_uint() in nettest.c so the strtoul() result
stays an unsigned long. The errno, trailing-character and range checks
now run before the value is narrowed to unsigned int. The goal is to
reject out-of-range arguments such as 4294967296, which used to wrap to
an in-range value.
> diff --git a/tools/testing/selftests/net/nettest.c b/tools/testing/selftests/net/nettest.c
> index 1f5227f3d64d6..4421beb76a130 100644
> --- a/tools/testing/selftests/net/nettest.c
> +++ b/tools/testing/selftests/net/nettest.c
> @@ -625,24 +625,27 @@ static int set_dontroute(int sd)
[ ... ]
> errno = 0;
> - number = (unsigned int) strtoul(str, &end, 0);
> + number = strtoul(str, &end, 0);
>
> /* entire string should be consumed by conversion
> * and value should be between min and max
> */
> - if (((*end == '\0') || (*end == '\n')) && (end != str) &&
> - (errno != ERANGE) && (min <= number) && (number <= max)) {
> - *value = number;
> - return 0;
> - }
> + if (end == str || errno)
> + return -1;
> + if (*end != '\0' && (*end != '\n' || end[1]))
> + return -1;
> + if (number < min || number > max)
> + return -1;
[Severity: Low]
Does this still let negative arguments wrap into the accepted range?
strtoul() accepts an optional leading '-' and negates the parsed
magnitude in unsigned long arithmetic. As long as the magnitude itself
fits in an unsigned long, errno is left at 0 and end advances past
every digit. None of the new checks catch that case.
On 64-bit, strtoul("-18446744073709551615", &end, 0) returns 1 and
strtoul("-18446744073709551361", &end, 0) returns 255, both with
errno == 0 and *end == '\0'. Both values pass the min/max check, so
nettest -Q -18446744073709551361 runs with a DS field of 255 instead
of reporting "Invalid DS Field".
On 32-bit, any value in [-4294967295, -2147483649] passed where max is
INT_MAX wraps into [1, INT_MAX] and is accepted. That covers the -t
timeout and expected_dev in resolve_devices().
The port, protocol and MD5 prefix length options go through the same
helper. A small negative such as -1 is rejected only because it wraps
to a value far above max. That is presumably what the negative -Q case
in the testing notes exercised.
The commit message says a malformed argument should fail rather than
make nettest run with a different DSCP field, protocol, port or
timeout. Should str_to_uint() also reject a leading '-' before calling
strtoul()? It would need to skip the leading whitespace that strtoul()
itself accepts. Parsing with strtoll() and rejecting negative results
would be another option.
[Severity: High]
Can negative arguments still wrap into range here?
strtoul() accepts a leading minus sign and negates the parsed magnitude in
unsigned long arithmetic. If the magnitude fits in an unsigned long,
errno stays 0 and end moves past every digit.
On 64-bit, strtoul("-18446744073709551361", &end, 0) returns 255 with
errno == 0 and *end == '\0'. That value passes every new check above.
The callers in main() pass optarg straight through:
case 'Q':
if (str_to_uint(optarg, 0, 255, &tmp) != 0) {
fprintf(stderr, "Invalid DS Field\n");
So nettest -Q -18446744073709551361 runs with dsfield 255, and
nettest -p -18446744073709551615 runs with port 1.
The -t and -P options go through the same path. So do the MD5 prefix
length and expected_dev in resolve_devices(). On 32-bit, -4294967295
becomes 1.
A small negative such as -1 is rejected only because it wraps to a value
far above max.
The commit message says a malformed argument should no longer make nettest
"run with a different DSCP field, protocol, port, or timeout instead of
failing".
Could str_to_uint() also reject a leading '-' (after any whitespace)
before it calls strtoul()? Another option is to parse with strtoll() and
reject negative results.
>
> - return -1;
> + *value = (unsigned int)number;
> + return 0;
> }
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130115512.57185-1-zhangminghui01%40kylinos.cn
prev parent reply other threads:[~2026-12-01 12:23 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 11:55 [PATCH net] selftests: nettest: reject out-of-range integer values Minghui Zhang
2026-12-01 12:23 ` netdev-bot+sashiko [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=179612783154.3146.2116613211232217894@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=shuah@kernel.org \
--cc=willemb@google.com \
--cc=zhangminghui01@kylinos.cn \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox