Skip to contentSkip to conversation

kconfig: fix use-after-free when a variable refers to itself

PATCH In review Leizhen Zhang 4 participants 4 messages Oct 8 – Nov 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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

---
v2:
- Use my real name in the From and Signed-off-by lines. No code changes.

v1: https://lore.kernel.org/r/[email protected]

 scripts/kconfig/preprocess.c                  | 21 ++++++++++++-------
 .../kconfig/tests/preprocess/variable/Kconfig |  5 +++++
 .../tests/preprocess/variable/expected_stderr |  1 +
 3 files changed, 20 insertions(+), 7 deletions(-)
scripts/kconfig/preprocess.c +14 -7
index 783abcaa5c..db226898c9 100644
@@ -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;
 	}
 }
scripts/kconfig/tests/preprocess/variable/Kconfig +5 -0
index 9ce2f95cbd..226114e86a 100644
@@ -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))
scripts/kconfig/tests/preprocess/variable/expected_stderr +1 -0
index a4841c3fdf..7008e3b177 100644
@@ -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
Signature · 1 line
2.34.1
reviewer Tested-by Reviewed-by on v2 1/1
#1 lore raw
Quoted 17 lines · Leizhen Zhang

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.

Quoted 5 lines

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
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Leizhen Zhang
Quoted 17 lines

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.

Quoted 5 lines

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

Just for the future, you can omit Assisted-by per

1e035297ffda ("docs: remove Assisted-by references")

although I would still like to see how AI was used in the creation of a patch below the fold '---' like the v2 text.

Signature · 2 lines
Cheers,
Nathan
maintainer
#3 lore raw
In reply to Leizhen Zhang
Quoted 10 lines · Leizhen Zhang

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

[...]

Applied to kbuild/linux.git (kbuild-next-unstable), thanks!

[1/1] kconfig: fix use-after-free when a variable refers to itself

      https://git.kernel.org/kbuild/c/3910d22d

Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped, reverted or modified (e.g. trailers).

Patches applied to the kbuild-next-unstable branch are accepted pending wider testing in linux-next and any post-commit review; they will generally be moved to the kbuild-next branch in about a week if no issues are found.

Best regards,

Signature · 1 line
Nicolas