Article View: linux.kernel.netdev
Article #25301[PATCH net v2] selftests: nettest: reject negative and out-of-range values
From: Minghui Zhang
Date: Tue, 01 Dec 2026 13:37
Date: Tue, 01 Dec 2026 13:37
108 lines
11965 bytes
11965 bytes
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
Technical Details
Message-ID:
<20261201133729.21727-1-zhangminghui01@kylinos.cn>Path:
mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375118-linux-kernel-netdev=dewy.ruShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375118-linux-kernel-netdev=dewy.ru From: zhangminghui01@kylinos.cn (Minghui Zhang) Newsgroups: linux.kernel.netdev Subject: [PATCH net v2] selftests: nettest: reject negative and out-of-range values Date: Tue, 1 Dec 2026 21:37:29 +0800 Organization: Usenet Network Message-ID: <20261201133729.21727-1-zhangminghui01@kylinos.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1723916"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:3rHcYr1wPCrJWTT01IS+0Q6eAp8= sha256:wKjrLOZ9X/1DX6g5VIkP3rbEmYb4riiHBnCk8vjoRpQ= sha1:7a4Ua5Q5aN7Zv6MgcbS+8YRK/Kk= sha256:ezNDIEdNbDU7Hc5aw3Gz02mE9Upq0gralRyZcBNyKGA= 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 F24DE1173C1; Tue, 1 Dec 2026 16:37:57 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.95 X-Spam-Status: No, score=-2.95 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=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, UNPARSEABLE_RELAY=0.001] autolearn=ham autolearn_force=no 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 Hd9qP5JGk6eL; Tue, 1 Dec 2026 16:37:52 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375118-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=kylinos.cn X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 86B501173BA; Tue, 1 Dec 2026 16:37:49 +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 ED4CB32DA1; Tue, 1 Dec 2026 13:37:47 +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=1796132267; b=YHBiIp54JJy3SXr7di8G4rNZtvJ8wMvxK7Eu8mmMt7F/7zbr1jXQnrr33VBdRkAz3MEp a28zUkW+M6eoWZW+EQyFkGFA7dtOcAZom0N6nAmK/OE7A7UxdwD8W/h2nCTt+eVJDypZz zEybxEPwOQrF16pp5UPphNFojjKcTi49ieQYQnFKRs6dlzn5lQOLmTohVCka6D9LUULIk B/iV5+otXQ5VLJ1OzVQYwS6DjltuizvTRl8H4R78Qn7ym02TSmApnseTr6hWkrjWMd7k8 IwVTcDaYOV6hr8wt48E5w8xfJk4amgONNTEuAj3S98SbPUCidBOPkvMzDDZiqtUE5Mw== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796132267; h=X-Original-To:Received:DMARC-Filter:X-UUID:X-CID-P-RULE:X-CID-O-INFO: X-CID-INFO:X-CID-META:X-CID-BVR:X-CID-BAS:X-CID-FACTOR:X-CID-RHF: X-UUID:X-User:Received:From:To:Cc:Subject:Date:Message-ID: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=dwRCQRnjelRtS+0oiZ4uzhOPX51OAqhT2W5bE1352RU=; b=4MApm5wKDOGH9dIn5niuxMLCZIzUZWEJyJlVMK7rZZQcDVC5RMe16JnxWBF7yoEqyahJ RPjiUTn1UaEGSgP/xiLlJIXC3XhR98SxxA+HKeA9sWn4jFVl6nqktfkPMzCv4PgldYVHo qhWkZqO+J5A2WwXqVFtkxIdw7RHbZc1dvsN6be/dTfgHEgvhMFaD1XSuGFJfG2gZHdT58 NfsawwsnsxYdo31R00SdJe0VhRV2nb1PJLTyCRS9i4aBw6nj78GVPx5hHofWJ1Nz5iBw1 stuStDnDnIOOTdgx8gQwlNI0390oK2AkftHwnuFo5NUNFcs0kr3hRPYth1/OASyZ9WQ== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; 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 8D917606A31A; Tue, 1 Dec 2026 13:37:47 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=124.126.103.232 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796132266; b=HgNZdffhJ71GhB5sbabCAUFX1rG2VTBJ92xxplJVBxEXCAQ89OyfZQelKO//vQLO9rNQ yxnWMQeX/YcUq3jeBQ8yKA7ylbjhaUbIuWl6Q6l5lX8vJuFjA+Hb3NTjChzAt6hzbivGo 0PKg6OIBEzmEpyjYY2swSK+4y7IfhJ02jRyw6JofyVqdpUaL/x9G/Vb1saBOO0JV9AiSn B77GaVnrWbhNhY10GEOiF9E7F+7jYPLOTlRCwNcHsXgUQbigRMy3rG6fn8KS12FcHVpDs Epq4to4VrsvWvLvh4yV1gw3mMJGm4pfaYGPhxetRlb14cn9uyHKniUOSxXFc49W/z8w== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796132266; h=DMARC-Filter:X-UUID:X-CID-P-RULE:X-CID-O-INFO:X-CID-INFO:X-CID-META: X-CID-BVR:X-CID-BAS:X-CID-FACTOR:X-CID-RHF:X-UUID:X-User:Received: From:To:Cc:Subject:Date:Message-ID:MIME-Version: Content-Transfer-Encoding; bh=dwRCQRnjelRtS+0oiZ4uzhOPX51OAqhT2W5bE1352RU=; b=Ili3vL+h0JXrT4pTIaekOsHjtCV3G52ihF3CmvXy15phsytW/TQyOns2OwOi/5t0VQjI mWAKYtRrIm5XlzpINExBBHhQiWGvq1HIo1TwrVtWkA1dg/a8XT5yUkTQdlHNVdv88Fd38 FsDSPjSeJFmlSJs7hXXUua6N4AcH0s6yjJnt0xUNyy78Db1iuXM3m8ivN9EGgNr+p+EPM FoAYXz+ZVnEFqBIsBzTu73lAtO8mSmUyD1PA8J8NRafR/rYoJX9StLC1k+2jxX5g3bknW dxHqv5LnQOqAz+SmKC64Zjq3kTHG0hlHir2tm+PGYd6jx+7QErObPLMI7voHquGQlmQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.remote-ip=124.126.103.232 X-Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E48B6606A339; Tue, 1 Dec 2026 13:37:41 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E48B6606A339 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 5d906fe4ed8c11f19a56ed5b684f684d-20261201 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.19,REQID:ecdc531b-7712-40e8-bee1-3b3a0d63dbdc,IP:10, URL:0,TC:0,Content:-25,EDM:25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACT ION:release,TS:10 X-CID-INFO: VERSION:1.3.19,REQID:ecdc531b-7712-40e8-bee1-3b3a0d63dbdc,IP:10,UR L:0,TC:0,Content:-25,EDM:25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:10 X-CID-META: VersionHash:7db8b62,CLOUDID:e2b7241f35969b0137f43eedbde4ada7,BulkI D:2612012137389F37JDZ5,BulkQuantity:0,SF:17|19|38|66|78|102|127|153|865|89 8,TC:nil,Content:0|15|50|99,EDM:5,IP:-2,URL:99|1,File:nil,RT:nil,Bulk:nil, QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 ,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD,TF_CID_SPAM_ULS X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 5d906fe4ed8c11f19a56ed5b684f684d-20261201 X-User: zhangminghui01@kylinos.cn X-Received: from zmh-y9000p [(123.118.210.172)] by mailgw.kylinos.cn (envelope-from <zhangminghui01@kylinos.cn>) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 1862822701; Tue, 01 Dec 2026 21:37:34 +0800 X-Cc: 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