mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] kconfig: randconfig: fix 32-bit KCONFIG_SEED replay and add validation
@ 2026-12-01  6:22 Priyanshu Singh
  2026-12-01  6:22 ` [PATCH v2 1/2] kconfig: randconfig: parse KCONFIG_SEED with strtoull() Priyanshu Singh
  2026-12-01  6:22 ` [PATCH v2 2/2] kconfig: randconfig: validate KCONFIG_SEED and add tests Priyanshu Singh
  0 siblings, 2 replies; 3+ messages in thread
From: Priyanshu Singh @ 2026-12-01  6:22 UTC (permalink / raw)
  To: Nathan Chancellor, Nicolas Schier
  Cc: Julian Braha, Yann E . MORIN, linux-kbuild, linux-kernel,
	Priyanshu Singh

This v2 series splits the KCONFIG_SEED fixes and validation into two
patches per review feedback from Julian Braha:

1. Patch 1/2 is a minimal bugfix switching strtol() to strtoull() so
   32-bit hosts stop clamping seeds >= 0x80000000 to LONG_MAX
   (0x7FFFFFFF).
2. Patch 2/2 adds malformed-format warnings, KCONFIG_WERROR enforcement,
   clock-seed fallback for negative, unparseable, and ERANGE inputs,
   backward-compatible trailing whitespace and non-negative modulo 2^32
   preservation when KCONFIG_WERROR is unset, and regression tests under
   scripts/kconfig/tests/randconfig_seed/.

Changes in v2:
- Split into a 2-patch series.
- Dropped 0b/0B binary prefix rejection and C23 notes.
- Dropped the (unsigned int) cast in `seed = val;`.
- Listed negative values in the malformed input bullet list in
  patch 2/2's commit message.
- Trimmed scripts/kconfig/tests/randconfig_seed/__init__.py from 222
  lines down to 112 lines.

Both patches pass `make testconfig` (34/34 after patch 1/2, 39/39 after
patch 2/2).

Priyanshu Singh (2):
  kconfig: randconfig: parse KCONFIG_SEED with strtoull()
  kconfig: randconfig: validate KCONFIG_SEED and add tests

 scripts/kconfig/conf.c                        |  33 +++++-
 scripts/kconfig/tests/randconfig_seed/Kconfig |  44 +++++++
 .../kconfig/tests/randconfig_seed/__init__.py | 112 ++++++++++++++++++
 3 files changed, 186 insertions(+), 3 deletions(-)
 create mode 100644 scripts/kconfig/tests/randconfig_seed/Kconfig
 create mode 100644 scripts/kconfig/tests/randconfig_seed/__init__.py

-- 
2.53.0

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] kconfig: randconfig: parse KCONFIG_SEED with strtoull()
  2026-12-01  6:22 [PATCH v2 0/2] kconfig: randconfig: fix 32-bit KCONFIG_SEED replay and add validation Priyanshu Singh
@ 2026-12-01  6:22 ` Priyanshu Singh
  2026-12-01  6:22 ` [PATCH v2 2/2] kconfig: randconfig: validate KCONFIG_SEED and add tests Priyanshu Singh
  1 sibling, 0 replies; 3+ messages in thread
From: Priyanshu Singh @ 2026-12-01  6:22 UTC (permalink / raw)
  To: Nathan Chancellor, Nicolas Schier
  Cc: Julian Braha, Yann E . MORIN, linux-kbuild, linux-kernel,
	Priyanshu Singh

set_randconfig_seed() parses KCONFIG_SEED with signed strtol(). On 32-bit
hosts where long is 32 bits, valid unsigned 32-bit seeds in
[0x80000000, 0xFFFFFFFF] exceed LONG_MAX (0x7FFFFFFF), so strtol() clamps
them to 0x7FFFFFFF. Since randconfig itself prints seeds via
"KCONFIG_SEED=0x%X\n", roughly half of all generated seeds fail to replay
accurately on 32-bit hosts.

Switch to strtoull() so the full 32-bit unsigned seed range parses
identically across 32-bit and 64-bit hosts.

Fixes: 0d8024c6ebad ("kconfig: allow specifying the seed for randconfig")
Signed-off-by: Priyanshu Singh <singhpriyanshu9838@gmail.com>
---
 scripts/kconfig/conf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
index 31c6fd25e6ec..11cf158c8b4e 100644
--- a/scripts/kconfig/conf.c
+++ b/scripts/kconfig/conf.c
@@ -94,7 +94,7 @@ static void set_randconfig_seed(void)
 	if (env && *env) {
 		char *endp;
 
-		seed = strtol(env, &endp, 0);
+		seed = strtoull(env, &endp, 0);
 		if (*endp == '\0')
 			seed_set = true;
 	}
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] kconfig: randconfig: validate KCONFIG_SEED and add tests
  2026-12-01  6:22 [PATCH v2 0/2] kconfig: randconfig: fix 32-bit KCONFIG_SEED replay and add validation Priyanshu Singh
  2026-12-01  6:22 ` [PATCH v2 1/2] kconfig: randconfig: parse KCONFIG_SEED with strtoull() Priyanshu Singh
@ 2026-12-01  6:22 ` Priyanshu Singh
  1 sibling, 0 replies; 3+ messages in thread
From: Priyanshu Singh @ 2026-12-01  6:22 UTC (permalink / raw)
  To: Nathan Chancellor, Nicolas Schier
  Cc: Julian Braha, Yann E . MORIN, linux-kbuild, linux-kernel,
	Priyanshu Singh

set_randconfig_seed() uses strtoull() without validating where parsing
stops or checking errno, so non-canonical or invalid inputs such as
trailing whitespace or garbage strings silently fall back to a
time-based seed with exit status 0, bypassing KCONFIG_WERROR, while
leading whitespace, signs, and > UINT_MAX values pass silently.

Emit "warning: KCONFIG_SEED has malformed format" when KCONFIG_SEED
deviates from a canonical unsigned 32-bit integer:
- leading or trailing whitespace
- negative values or an explicit '+' sign
- values exceeding UINT_MAX or overflowing unsigned long long (ERANGE)
- unparseable input or trailing non-whitespace characters

When KCONFIG_WERROR is set, exit with status 1 before writing .config.
When KCONFIG_WERROR is unset, consume trailing whitespace and preserve
modulo 2^32 conversion for non-negative parseable 64-bit integers
(including leading/trailing whitespace, explicit '+', and values
exceeding UINT_MAX), while falling back to gettimeofday() on negative
values, unparseable input, or ERANGE overflow.

Add regression tests under scripts/kconfig/tests/randconfig_seed/.

Signed-off-by: Priyanshu Singh <singhpriyanshu9838@gmail.com>
---
 scripts/kconfig/conf.c                        |  33 +++++-
 scripts/kconfig/tests/randconfig_seed/Kconfig |  44 +++++++
 .../kconfig/tests/randconfig_seed/__init__.py | 112 ++++++++++++++++++
 3 files changed, 186 insertions(+), 3 deletions(-)
 create mode 100644 scripts/kconfig/tests/randconfig_seed/Kconfig
 create mode 100644 scripts/kconfig/tests/randconfig_seed/__init__.py

diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
index 11cf158c8b4e..d80a24e76f67 100644
--- a/scripts/kconfig/conf.c
+++ b/scripts/kconfig/conf.c
@@ -92,11 +92,38 @@ static void set_randconfig_seed(void)
 
 	env = getenv("KCONFIG_SEED");
 	if (env && *env) {
+		const char *p = env;
 		char *endp;
+		unsigned long long val;
+		bool erange;
+		bool warned = false;
+
+		while (isspace((unsigned char)*p))
+			p++;
+
+		errno = 0;
+		val = strtoull(p, &endp, 0);
+		erange = errno == ERANGE;
+
+		if (p != env || !isdigit((unsigned char)*p) || *endp != '\0' ||
+		    erange || val > UINT_MAX) {
+			fprintf(stderr,
+				"warning: KCONFIG_SEED has malformed format\n");
+			warned = true;
+		}
 
-		seed = strtoull(env, &endp, 0);
-		if (*endp == '\0')
-			seed_set = true;
+		if (*p != '-' && endp != p && !erange) {
+			while (isspace((unsigned char)*endp))
+				endp++;
+
+			if (*endp == '\0') {
+				seed = val;
+				seed_set = true;
+			}
+		}
+
+		if (warned && getenv("KCONFIG_WERROR"))
+			exit(1);
 	}
 
 	if (!seed_set) {
diff --git a/scripts/kconfig/tests/randconfig_seed/Kconfig b/scripts/kconfig/tests/randconfig_seed/Kconfig
new file mode 100644
index 000000000000..28d5cb225b04
--- /dev/null
+++ b/scripts/kconfig/tests/randconfig_seed/Kconfig
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config MODULES
+	bool
+	default y
+	modules
+
+config BOOL1
+	bool "Bool 1"
+
+config BOOL2
+	bool "Bool 2"
+
+config BOOL3
+	bool "Bool 3"
+
+config BOOL4
+	bool "Bool 4"
+
+config TRI1
+	tristate "Tristate 1"
+
+config TRI2
+	tristate "Tristate 2"
+
+config TRI3
+	tristate "Tristate 3"
+
+config TRI4
+	tristate "Tristate 4"
+
+choice
+	prompt "Choice"
+
+config CHOICE_A
+	bool "Choice A"
+
+config CHOICE_B
+	bool "Choice B"
+
+config CHOICE_C
+	bool "Choice C"
+
+endchoice
diff --git a/scripts/kconfig/tests/randconfig_seed/__init__.py b/scripts/kconfig/tests/randconfig_seed/__init__.py
new file mode 100644
index 000000000000..0008e4fef7b1
--- /dev/null
+++ b/scripts/kconfig/tests/randconfig_seed/__init__.py
@@ -0,0 +1,112 @@
+# SPDX-License-Identifier: GPL-2.0-only
+"""Validate KCONFIG_SEED handling in randconfig."""
+
+import time
+
+
+def test_valid(conf):
+    groups = [
+        ('0x0', ['0', '00', '0x0', '0X0']),
+        ('0x2A', ['42', '052', '0x2a', '0X2A']),
+        ('0x80000000', ['2147483648', '020000000000', '0x80000000']),
+        ('0xFFFFFFFF',
+         ['4294967295', '037777777777', '0xffffffff', '0XFFFFFFFF']),
+    ]
+    for expected_hex, representations in groups:
+        expected_cfg = None
+        for seed in representations:
+            ctx = 'seed={!r}, expected={!r}'.format(seed, expected_hex)
+            env = {'KCONFIG_SEED': seed}
+            assert conf._run_conf('--randconfig', extra_env=env) == 0, ctx
+            assert conf.stderr == '', ctx
+            assert 'KCONFIG_SEED={}\n'.format(expected_hex) in conf.stdout, ctx
+            assert conf.config is not None, ctx
+            if expected_cfg is None:
+                expected_cfg = conf.config
+            else:
+                assert conf.config == expected_cfg, ctx
+
+
+def test_malformed_preserves_config(conf):
+    pairs = [
+        (' 42', '42'), ('42 ', '42'), (' \t0x2a ', '0x2a'),
+        ('+0', '0'), ('+42', '42'), (' +42 ', '42'),
+        ('4294967296', '0x0'), ('0x10000002a', '0x2a'),
+        ('18446744073709551615', '0xffffffff'),
+    ]
+    for bad, good in pairs:
+        ctx = 'bad={!r}, good={!r}'.format(bad, good)
+        assert conf._run_conf('--randconfig',
+                              extra_env={'KCONFIG_SEED': bad}) == 0, ctx
+        assert 'warning: KCONFIG_SEED has malformed format' in conf.stderr, ctx
+        assert conf.stderr.count('warning:') == 1, ctx
+        bad_out, bad_cfg = conf.stdout, conf.config
+
+        assert conf._run_conf('--randconfig',
+                              extra_env={'KCONFIG_SEED': good}) == 0, ctx
+        assert 'warning:' not in conf.stderr, ctx
+        assert conf.stdout == bad_out, ctx
+        assert conf.config == bad_cfg, ctx
+
+
+def test_malformed_falls_back_to_clock(conf):
+    targets = [
+        ('-0', '0x0'), ('-1', '0xFFFFFFFF'), (' -1 ', '0xFFFFFFFF'),
+        ('-42', '0xFFFFFFD6'), ('-0x1', '0xFFFFFFFF'),
+        ('-4294967296', '0x0'), ('-18446744073709551615', '0x1'),
+        ('invalid', '0x0'), (' ', '0x0'), ('+', '0x0'), ('-', '0x0'),
+        ('0x', '0x0'), ('08', '0x0'), ('42foo', '0x2A'), ('10 20', '0xA'),
+        ('18446744073709551616', '0xFFFFFFFF'),
+        ('-18446744073709551616', '0xFFFFFFFF'),
+    ]
+    for seed, forbidden in targets:
+        forbidden_line = 'KCONFIG_SEED={}\n'.format(forbidden)
+        matched = True
+        ctx = 'seed={!r}'.format(seed)
+        for _ in range(3):
+            env = {'KCONFIG_SEED': seed}
+            assert conf._run_conf('--randconfig', extra_env=env) == 0, ctx
+            assert ('warning: KCONFIG_SEED has malformed format' in
+                    conf.stderr), ctx
+            assert conf.stderr.count('warning:') == 1, ctx
+            assert 'KCONFIG_SEED=0x' in conf.stdout, ctx
+            assert conf.config is not None, ctx
+            if forbidden_line not in conf.stdout:
+                matched = False
+                break
+            time.sleep(0.002)
+        assert not matched, 'seed={!r} stuck at {!r}'.format(seed, forbidden)
+
+
+def test_werror(conf):
+    seeds = [
+        ('42', 0), ('0xffffffff', 0),
+        ('invalid', 1), ('42 ', 1), ('+42', 1), ('-1', 1),
+        ('4294967296', 1), ('18446744073709551616', 1),
+    ]
+    for seed, status in seeds:
+        for werror in ['', '0', '1']:
+            ctx = 'seed={!r}, werror={!r}'.format(seed, werror)
+            env = {'KCONFIG_SEED': seed, 'KCONFIG_WERROR': werror}
+            assert conf._run_conf('--randconfig', extra_env=env) == status, ctx
+            if status:
+                assert ('warning: KCONFIG_SEED has malformed format' in
+                        conf.stderr), ctx
+                assert conf.stderr.count('warning:') == 1, ctx
+                assert conf.config is None, ctx
+            else:
+                assert 'warning:' not in conf.stderr, ctx
+                assert conf.config is not None, ctx
+
+
+def test_unset_and_empty(conf):
+    for base in [{}, {'KCONFIG_SEED': ''}]:
+        for werror in [None, '', '0', '1']:
+            env = dict(base)
+            if werror is not None:
+                env['KCONFIG_WERROR'] = werror
+            ctx = 'env={!r}'.format(env)
+            assert conf._run_conf('--randconfig', extra_env=env) == 0, ctx
+            assert 'warning:' not in conf.stderr, ctx
+            assert 'KCONFIG_SEED=0x' in conf.stdout, ctx
+            assert conf.config is not None, ctx
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-01  6:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01  6:22 [PATCH v2 0/2] kconfig: randconfig: fix 32-bit KCONFIG_SEED replay and add validation Priyanshu Singh
2026-12-01  6:22 ` [PATCH v2 1/2] kconfig: randconfig: parse KCONFIG_SEED with strtoull() Priyanshu Singh
2026-12-01  6:22 ` [PATCH v2 2/2] kconfig: randconfig: validate KCONFIG_SEED and add tests Priyanshu Singh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®