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. 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. Without the fix, ./nettest -Q 4294967296 proceeds to the missing remote address check. With the fix it reports "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". - Checked valid and invalid decimal, hexadecimal, overflow, negative, empty, and trailing-data inputs for -Q. - A loopback server/client transfer completed successfully. Fixes: acda655fefae ("selftests: Add nettest") Signed-off-by: Minghui Zhang --- tools/testing/selftests/net/nettest.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/net/nettest.c b/tools/testing/selftests/net/nettest.c index 1f5227f3d64d..4421beb76a13 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) 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; + unsigned long number; char *end; 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