summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorLeizhen Zhang <lzsx618@gmail.com>2026-10-08 11:45:04 -0500
committerNicolas Schier <nsc@kernel.org>2026-11-05 07:14:18 +0100
commit3910d22df6845ed40aa01f4869655e0442ca2e5b (patch)
tree9680ae5c13ddd3ed557bd094651f5f7df8d902a6 /scripts
parentf57c1b71bea3ec79bc2499563ac8a2cf758cc669 (diff)
downloadlinux-next-3910d22df6845ed40aa01f4869655e0442ca2e5b.tar.gz
linux-next-3910d22df6845ed40aa01f4869655e0442ca2e5b.zip
kconfig: fix use-after-free when a variable refers to itself
For a simply expanded variable, variable_add() frees the old value of an existing variable (or allocates a new variable with an uninitialized value) before expanding the right-hand side. If the right-hand side refers to the variable itself, which is a common idiom in Makefiles, e.g. X := 1 X := $(X) 2 the expansion reads the freed (or uninitialized) value. With ASan this is reported as a heap-use-after-free, or a SEGV if X was undefined. Without ASan, X silently ends up as " 2" instead of "1 2". Expand the right-hand side before updating the variable, initialize the value of a newly created variable, and only free the old value when it is actually replaced. Add a test case for this. Found by fuzzing Kconfig input with ASan/UBSan. Fixes: 1175c02506ff ("kconfig: support simply expanded variable") Assisted-by: Claude:claude-opus-5-5 ASan UBSan libFuzzer Signed-off-by: Leizhen Zhang <lzsx618@gmail.com> Tested-by: Julian Braha <julianbraha@gmail.com> Reviewed-by: Julian Braha <julianbraha@gmail.com> Reviewed-by: Nathan Chancellor <nathan@kernel.org> Link: https://patch.msgid.link/20261008164504.3683811-1-lzsx618@gmail.com Signed-off-by: Nicolas Schier <nsc@kernel.org>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/kconfig/preprocess.c21
-rw-r--r--scripts/kconfig/tests/preprocess/variable/Kconfig5
-rw-r--r--scripts/kconfig/tests/preprocess/variable/expected_stderr1
3 files changed, 20 insertions, 7 deletions
diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c
index 783abcaa5cc5..db226898c988 100644
--- a/scripts/kconfig/preprocess.c
+++ b/scripts/kconfig/preprocess.c
@@ -293,27 +293,33 @@ void variable_add(const char *name, const char *value,
if (flavor == VAR_APPEND) {
flavor = v->flavor;
append = true;
- } else {
- free(v->value);
}
} else {
/* For undefined variables, += assumes the recursive flavor */
if (flavor == VAR_APPEND)
flavor = VAR_RECURSIVE;
+ }
+ /*
+ * Expand the value before touching the variable because the
+ * right-hand side may refer to the variable itself, like
+ * "X := $(X) foo".
+ */
+ if (flavor == VAR_SIMPLE)
+ new_value = expand_string(value);
+ else
+ new_value = xstrdup(value);
+
+ if (!v) {
v = xmalloc(sizeof(*v));
v->name = xstrdup(name);
+ v->value = NULL;
v->exp_count = 0;
list_add_tail(&v->node, &variable_list);
}
v->flavor = flavor;
- if (flavor == VAR_SIMPLE)
- new_value = expand_string(value);
- else
- new_value = xstrdup(value);
-
if (append) {
v->value = xrealloc(v->value,
strlen(v->value) + strlen(new_value) + 2);
@@ -321,6 +327,7 @@ void variable_add(const char *name, const char *value,
strcat(v->value, new_value);
free(new_value);
} else {
+ free(v->value);
v->value = new_value;
}
}
diff --git a/scripts/kconfig/tests/preprocess/variable/Kconfig b/scripts/kconfig/tests/preprocess/variable/Kconfig
index 9ce2f95cbd24..226114e86a28 100644
--- a/scripts/kconfig/tests/preprocess/variable/Kconfig
+++ b/scripts/kconfig/tests/preprocess/variable/Kconfig
@@ -51,3 +51,8 @@ $(warning,$(greeting,Hello))
# Unreferenced parameters are just ignored.
$(warning,$(greeting,Hello,John,ignored,ignored))
+
+# Simply expanded variable referring to itself.
+X := 1
+X := $(X) 2
+$(warning,X = $(X))
diff --git a/scripts/kconfig/tests/preprocess/variable/expected_stderr b/scripts/kconfig/tests/preprocess/variable/expected_stderr
index a4841c3fdff5..7008e3b1771c 100644
--- a/scripts/kconfig/tests/preprocess/variable/expected_stderr
+++ b/scripts/kconfig/tests/preprocess/variable/expected_stderr
@@ -7,3 +7,4 @@ Kconfig:41: AB = 5
Kconfig:45: Hello, my name is John.
Kconfig:50: Hello, my name is .
Kconfig:53: Hello, my name is John.
+Kconfig:58: X = 1 2