🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25688

Re: [PATCH net v2] selftests: nettest: reject negative and out-of-range values

#25688
From: Hangbin Liu
Date: Wed, 02 Dec 2026 09:11
115 lines
12826 bytes
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>
Technical Details
Message-ID: <aw_gvBjp0d_4zErt@fedora>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375665-linux-kernel-netdev=dewy.ru
References: <20261201133729.21727-1-zhangminghui01@kylinos.cn>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375665-linux-kernel-netdev=dewy.ru
From: hangbin.liu@linux.dev (Hangbin Liu)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net v2] selftests: nettest: reject negative and
 out-of-range values
Date: Wed, 2 Dec 2026 17:11:24 +0800
Organization: Usenet Network
Message-ID: <aw_gvBjp0d_4zErt@fedora>
References: <20261201133729.21727-1-zhangminghui01@kylinos.cn>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: usenet.network;
	logging-data="1932115"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:DRzYuvbD0xWFtzOwGLXSAsnG+Ng= sha256:PsuNdo2ICAenfobNCWth4gY2FtTDr+a3iY7YNom/O0g=
	sha1:Z0eghB4gZkYdtynVD3QdDDCHAZA= sha256:v8cJmAdMuJe8tT3QQJ9MNHOd08p0fQ76zb+rEobNWtU=
Content-Disposition: inline
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 9EAF01175C4; Wed,  2 Dec 2026 12:12:04 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.05
X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=linux.dev
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id JFApiqKHdnRv;
	Wed,  2 Dec 2026 12:11:56 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375665-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 3B6EC117590; Wed,  2 Dec 2026 12:11:47 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 8C98F2BFE8; Wed,  2 Dec 2026 09:11:44 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796202703;
	b=YEWryfQGKEBnlzxCHGIQ5TiyupzWo5w3s6av5qC0vDk0X3CnVEWa6aVIGpX4Vw0GScO9
	 WhJxrSX6YYq12UnGUuM+S+JRlHhsnnIO6Zh27WBAVS9qOnjZhrVGKG38bAC+hc3KHfw2Z
	 lOQb5bG2R6qY2ei3dj53NaDW762s3NrF4DOt5+CtKwdEKPpoJ4q960iUJfe2KIsGDA69v
	 8l2f68b8yG+WQhhVhrD26Ji4ZLh6Mp4o03MhFAARezoPjTky6KrwN2tpEupcgZLc6h8kj
	 4ef1AEPdKehtp2rTgA20Cb1BQpG60jOKy2w3wewIWVTmNgu77QSSZyT0dfjM0zkjQkQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796202703;
	h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature:
	 X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:Date:From:To:Cc:
	 Subject:Message-ID:References:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Type:Content-Disposition:In-Reply-To;
	bh=tweWR2jU82KSiWw8aGXWo+wu6d6S53j7gVQLsy5NVSo=;
	b=oSFqZTUUdx+3X/PW521Gy+pst55dT2W10TkOU/qUeefo9AbLVrZsMlpxG4p59VzV4+zA
	 8wdsQ+BTFrX17gUyiBsN5l50henp8pFe1khUjeGNL9WUr6jad5DF71d+JS2rwX53as/pz
	 UtrVybuioUgDFe6LZGqVq3TQvWFXh5Gl+vFNanH7/TLd7u2ZF3Ni+r36tiqU6pcAH71Oi
	 JCBcBYkxejOkMC1KE2JyKNQBjXXy9r+mcqX4nIfU+bfii/84mwpLoES67FRH00E7rpWOq
	 qHzVyAel7WFeu0b2WN9z9lV3JS7UhRfZjuJ6//F0E7NHz1vTM6D4A5j/HRIvALLKPmA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=VkkgiopX;
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id B758C606A32E;
	Wed,  2 Dec 2026 09:11:43 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=VkkgiopX
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.41
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796202702;
	b=Zb9N1aPgJDm812jrK8gux5xwxxE3K9i4eNt5xKfj1H1rft/l8y19GlSVqpMkUysyBCfL
	 iAa0whMwHaazLS1KRTvrCWtbfdLPnzjcD0+cX7cd8nY+gfeLAowqspQQFrh+9Di7QW6Ho
	 6vumBUKbf/hhp6baluDYKVJNvZhQMtAqFQmE8uMcnCSjp9RdOH75+/ZoScQFOZ4al70if
	 wHBjepuvhE08TFwi+/DtYCxkWQsPy/2LMlbebt+QkuJTKlbvzLpohrVN/hJoNK6DkFTLt
	 OOdr/83mSJLnSe+7Gb90FCrD5PvmFGAIqE8w/9tYMOYBAJ2G744TFS3RflUTtL4HBKg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796202702;
	h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received:
	 X-Mizu-Trace-ID:X-Migadu-Flow:Date:From:To:Cc:Subject:Message-ID:
	 References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To;
	bh=tweWR2jU82KSiWw8aGXWo+wu6d6S53j7gVQLsy5NVSo=;
	b=H4md6XauHG02eZb2fN9MHkb1Uxtl8a2cSK2JxGZyk99FGlLEGE7R+YphcSBQTfeuqH3P
	 Q+LGcSYjfB/sjsgSkxkDsUqDtmDxp+fNXFVqyE0nL8QZeR05wm3hAx6VEmcI8GgOFfbhk
	 RmabHyWZg26aIZ4XeR8tiYmUgMf+PlF3H5hK/yRESyetL56UMeChE5PD9ztfe+dvB0IZP
	 CgoUqzdGjg3K2jMWB1M1WdEROqyRix6SRZtAs3SM6BHo5J7kx5lOMex45lJKmed+kNztd
	 DnQIVJ1t4OweSPpCd7hd8TW2H/2W5ls5JjxRZ5xq89isrlBb78yj60xDVjbEUNevcRw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=VkkgiopX;
	arc=none smtp.remote-ip=91.218.175.41
X-Received: from mta0.migadu.com (out-41.mta0.migadu.com [91.218.175.41])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB52F606A330
	for <netdev@vger.kernel.org>; Wed,  2 Dec 2026 09:11:40 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AB52F606A330
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev
X-Envelope-To: netdev@vger.kernel.org
X-DKIM-Signature: a=rsa-sha256; bh=hQYb7Are9MpOPtwKaLHqll33YwlX6qME/8c++vE1LaI=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1796202699; v=1; x=1796807499;
 b=VkkgiopXHGwR3rgjiRNDT7aB5uQqVwahObanr9VUK4hqCAfgSQAh1JTVGAAFFb3TIb3drQDz
 OvnbygOx3OSaON5ZUry2BOlhFsnc/RzfNK2ShngG/h9vY/fqbUWUxthZN392DUEXl3Y8cf3IIDN
 nOffwknOdgbV6HuhMCVGh9cY=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by mta10.migadu.com with ESMTPS id 79d3e35ac2c642b0;
	Wed, 02 Dec 2026 09:11:39 +0000
X-Mizu-Trace-ID: 79d3e35ac2c642b0
X-Migadu-Flow: FLOW_OUT
X-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>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <20261201133729.21727-1-zhangminghui01@kylinos.cn>