From: Dmitrii Tulnov <tulnov.dl@gmail.com>
To: Nathan Chancellor <nathan@kernel.org>, Nicolas Schier <nsc@kernel.org>
Cc: Julian Braha <julianbraha@gmail.com>,
Peter Korsgaard <jacmet@uclibc.org>,
"Yann E. MORIN" <yann.morin.1998@free.fr>,
linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] kconfig: reject malformed KCONFIG_PROBABILITY values
Date: Mon, 7 Sep 2026 22:45:36 +0300 [thread overview]
Message-ID: <20260907194536.37-1-tulnov.dl@gmail.com> (raw)
randconfig checks the numeric range of each probability but does not
validate where strtol() stops. For example, KCONFIG_PROBABILITY=50% is
accepted as 50:0:0: the '%' is parsed twice as zero. This silently sets
both tristate y/m probabilities to zero, reducing the coverage of random
configuration builds. Empty fields and extra fields are also accepted.
Require each field to contain an integer followed by the end of the
string or a colon introducing another field, with at most three fields.
Preserve the leading whitespace and optional sign accepted by strtol().
Keep the strtol() result as long until the range check so that narrowing
to int cannot turn an out-of-range value into a valid probability.
Add regression tests for malformed and out-of-range values, the supported
probability formats, and the documented empty-value default.
Fixes: e43956e60769 ("kconfig: implement KCONFIG_PROBABILITY for randconfig")
Assisted-by: LLM
Signed-off-by: Dmitrii Tulnov <tulnov.dl@gmail.com>
---
Validation on kbuild-next, x86_64, GCC 13.3.0:
- make testconfig with HOSTCFLAGS=-Werror: 58 passed. With the original
conf binary: 18 failed, 40 passed; all failures are new regression tests.
- ASan/UBSan at -O1, with leak detection disabled: the same 58 tests passed.
- Both builds passed 1,635 input cases and 440 comparisons with the original
conf using valid inputs and fixed seeds. Malformed inputs preserved an
existing .config, including when KCONFIG_ALLCONFIG was set.
- make defconfig, allnoconfig, allmodconfig and valid randconfig passed.
KCONFIG_PROBABILITY=50% changed from success to the expected error.
- No vmlinux build or boot test; this changes the host configuration tool.
A 32-bit host build was unavailable because multilib headers were missing.
An AI coding assistant helped find the issue, prepare the fix, description
and tests, and run validation. The requested task was to find and fix a
useful, reproducible Linux bug suitable for a first contribution.
scripts/kconfig/conf.c | 9 +++-
.../tests/randconfig_probability/Kconfig | 12 +++++
.../tests/randconfig_probability/__init__.py | 54 +++++++++++++++++++
3 files changed, 74 insertions(+), 1 deletion(-)
create mode 100644 scripts/kconfig/tests/randconfig_probability/Kconfig
create mode 100644 scripts/kconfig/tests/randconfig_probability/__init__.py
diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
index fe8ba09b0..fa5dae74e 100644
--- a/scripts/kconfig/conf.c
+++ b/scripts/kconfig/conf.c
@@ -191,7 +191,14 @@ static void conf_set_all_new_symbols(enum conf_def_mode mode)
n = 0;
while (env && *env) {
char *endp;
- int tmp = strtol(env, &endp, 10);
+ long tmp = strtol(env, &endp, 10);
+
+ if (endp == env || (*endp && *endp != ':') ||
+ (*endp == ':' && (!endp[1] || n == 2))) {
+ errno = EINVAL;
+ perror("KCONFIG_PROBABILITY");
+ exit(1);
+ }
if (tmp >= 0 && tmp <= 100) {
p[n++] = tmp;
diff --git a/scripts/kconfig/tests/randconfig_probability/Kconfig b/scripts/kconfig/tests/randconfig_probability/Kconfig
new file mode 100644
index 000000000..84f4e5fcc
--- /dev/null
+++ b/scripts/kconfig/tests/randconfig_probability/Kconfig
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config MODULES
+ bool
+ default y
+ modules
+
+config BOOL
+ bool "Bool"
+
+config TRI
+ tristate "Tristate"
diff --git a/scripts/kconfig/tests/randconfig_probability/__init__.py b/scripts/kconfig/tests/randconfig_probability/__init__.py
new file mode 100644
index 000000000..022c7eaf2
--- /dev/null
+++ b/scripts/kconfig/tests/randconfig_probability/__init__.py
@@ -0,0 +1,54 @@
+# SPDX-License-Identifier: GPL-2.0-only
+"""Validate KCONFIG_PROBABILITY without changing the supported distributions."""
+
+import pytest
+
+
+@pytest.mark.parametrize('probability', [
+ 'invalid', ' ', '50%', '50 ', '0x32', '10 20', '10:20x',
+ '10:20:invalid', '10:20:30x',
+ ':50', '50:', '10::20', '10:20:', '10:20:30:', '10:20:30:40',
+ '-1', '101', '0:101', '0:0:101', '60:41', '0:60:41',
+ '4294967296', '-4294967296',
+ '999999999999999999999999', '-999999999999999999999999',
+])
+def test_invalid(conf, monkeypatch, probability):
+ monkeypatch.setenv('KCONFIG_PROBABILITY', probability)
+
+ assert conf.randconfig(seed=0) == 1
+ assert 'KCONFIG_PROBABILITY:' in conf.stderr
+
+
+@pytest.mark.parametrize('probability, boolean, tristate', [
+ ('0', 'n', 'n'),
+ ('0:0', 'n', 'n'),
+ ('100:0', 'y', 'y'),
+ ('0:100', 'y', 'm'),
+ ('100:0:0', 'y', 'n'),
+ ('0:100:0', 'n', 'y'),
+ ('0:0:100', 'n', 'm'),
+ ('000:000:100', 'n', 'm'),
+ ('+100:0', 'y', 'y'),
+ (' \t100:0', 'y', 'y'),
+ ('0: \t100:0', 'n', 'y'),
+])
+def test_valid(conf, monkeypatch, probability, boolean, tristate):
+ monkeypatch.setenv('KCONFIG_PROBABILITY', probability)
+
+ assert conf.randconfig(seed=0) == 0
+ for symbol, value in [('BOOL', boolean), ('TRI', tristate)]:
+ if value == 'n':
+ expected = '# CONFIG_{} is not set'.format(symbol)
+ else:
+ expected = 'CONFIG_{}={}'.format(symbol, value)
+ assert expected in conf.config.splitlines()
+
+
+def test_empty(conf, monkeypatch):
+ monkeypatch.delenv('KCONFIG_PROBABILITY', raising=False)
+ assert conf.randconfig(seed=0) == 0
+ default_config = conf.config
+
+ monkeypatch.setenv('KCONFIG_PROBABILITY', '')
+ assert conf.randconfig(seed=0) == 0
+ assert conf.config == default_config
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
next reply other threads:[~2026-09-07 19:45 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-07 19:45 Dmitrii Tulnov [this message]
2026-09-08 19:24 ` Julian Braha
2026-09-08 20:35 ` Dmitrii Tulnov
2026-09-08 20:36 ` [PATCH v2] kconfig: warn about " Dmitrii Tulnov
2026-09-10 0:05 ` Julian Braha
2026-09-10 9:13 ` Dmitrii Tulnov
2026-09-10 11:25 ` Julian Braha
2026-09-10 12:04 ` [PATCH v3] " Dmitrii Tulnov
2026-09-10 21:17 ` Julian Braha
2026-09-10 21:52 ` [PATCH v4] " Dmitrii Tulnov
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=20260907194536.37-1-tulnov.dl@gmail.com \
--to=tulnov.dl@gmail.com \
--cc=jacmet@uclibc.org \
--cc=julianbraha@gmail.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=yann.morin.1998@free.fr \
/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
all inboxes | Powered by JetHome®