Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Hangbin Liu <hangbin.liu@linux.dev>
To: Minghui Zhang <zhangminghui01@kylinos.cn>
Cc: "David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Shuah Khan <shuah@kernel.org>,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	Simon Horman <horms@kernel.org>, David Ahern <dsahern@kernel.org>
Subject: Re: [PATCH net v2] selftests: nettest: reject negative and out-of-range values
Date: Wed, 2 Dec 2026 17:11:24 +0800	[thread overview]
Message-ID: <aw_gvBjp0d_4zErt@fedora> (raw)
In-Reply-To: <20261201133729.21727-1-zhangminghui01@kylinos.cn>

On Tue, Dec 01, 2026 at 09:37:29PM +0800, Minghui Zhang wrote:
> str_to_uint() narrows the unsigned long returned by strtoul() to int
> before checking the requested range. That conversion is
> implementation-defined for out-of-range values; on 64-bit systems, for
> example, 4294967296 wraps to 0. A malformed or incorrectly generated
> numeric argument can therefore make nettest run with a different DSCP
> field, protocol, port, or timeout instead of failing, potentially
> invalidating a test while it appears successful.
> 
> Keep the conversion result in unsigned long and perform error and range
> checks before narrowing to unsigned int. Reject a leading '-', including
> after leading whitespace, because strtoul() otherwise negates the parsed
> magnitude in unsigned arithmetic. This prevents negative magnitudes from
> wrapping into the accepted range. Also require a trailing newline, if
> present, to be the final character so the entire argument is consumed as
> intended.
> 
> Found while building and auditing the network selftests. On x86_64,
> without the fix, -Q 4294967296 and -Q -18446744073709551361 proceed to
> the missing remote address check. On i386, -Q -4294967295 wraps to 1 and
> does the same. With the fix these inputs report "Invalid DS Field". Valid
> values such as 0, 255, 0xff, and a newline-terminated value still proceed
> to the following validation.
> 
> Testing:
> - Built nettest with USERCFLAGS="-Wextra -Werror" and with Clang.
> - Checked decimal, hexadecimal, negative, whitespace-prefixed, overflow,
>   empty, trailing-data, and newline-terminated inputs.
> - Exercised the rewritten parser through -Q, -p, -P, -t, and MD5 prefix
>   parsing.
> - Runtime-tested the parser and loopback transfers with both x86_64 and
>   ELF32 i386 userspace binaries.
> - Negative and wrapped inputs also passed under ASan and UBSan on both
>   word sizes.
> 
> Reported-by: Sashiko AI review <netdev-bot+sashiko@kernel.org>
> Closes: https://lore.kernel.org/netdev/179612783154.3146.2116613211232217894@kernel.org/
> Fixes: acda655fefae ("selftests: Add nettest")
> Signed-off-by: Minghui Zhang <zhangminghui01@kylinos.cn>
> ---
> v1: https://lore.kernel.org/netdev/20261130115512.57185-1-zhangminghui01@kylinos.cn/
> 
> Changes in v2:
> - Reject negative inputs after optional leading whitespace before the
>   strtoul() conversion.
> - Test negative values, whitespace-prefixed values, and wrapped
>   magnitudes that could otherwise map into the requested range.
> ---
>  tools/testing/selftests/net/nettest.c | 28 ++++++++++++++++++---------
>  1 file changed, 19 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/testing/selftests/net/nettest.c b/tools/testing/selftests/net/nettest.c
> index 1f5227f3d64d..238e52ac32b9 100644
> --- a/tools/testing/selftests/net/nettest.c
> +++ b/tools/testing/selftests/net/nettest.c
> @@ -17,6 +17,7 @@
>  #include <netinet/in.h>
>  #include <netinet/ip.h>
>  #include <netdb.h>
> +#include <ctype.h>
>  #include <fcntl.h>
>  #include <libgen.h>
>  #include <limits.h>
> @@ -625,24 +626,33 @@ static int set_dontroute(int sd)
>  	return 0;
>  }
>  
> -static int str_to_uint(const char *str, int min, int max, unsigned int *value)
> +static int str_to_uint(const char *str, unsigned int min, unsigned int max,
> +		       unsigned int *value)
>  {
> -	int number;
> +	const char *p = str;
> +	unsigned long number;
>  	char *end;
>  
> +	while (isspace((unsigned char)*p))
> +		p++;
> +	if (*p == '-')
> +		return -1;
> +
>  	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;
>  
> -	return -1;
> +	*value = (unsigned int)number;
> +	return 0;
>  }
>  
>  static int resolve_devices(struct sock_args *args)
> -- 
> 2.53.0
> 

FYI, If the issue does not affect the currently running tests, you can target
it to net-next.

Reviewed-by: Hangbin Liu <liuhangbin@kylinos.cn>

      reply	other threads:[~2026-12-02  9:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 13:37 [PATCH net v2] selftests: nettest: reject negative and out-of-range values Minghui Zhang
2026-12-02  9:11 ` Hangbin Liu [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=aw_gvBjp0d_4zErt@fedora \
    --to=hangbin.liu@linux.dev \
    --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