Skip to contentSkip to conversation

kconfig: fix dangling check_top after an unexpected recursive dependency

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 dangling check_top after an unexpected recursive dependency

If the last symbol of a recursive dependency is a choice value, sym_check_print_recursive() pushes the on-stack cv_stack onto the dependency stack. If the choice is then not found on the stack, the function prints "unexpected recursive dependency error" and returns without removing cv_stack again. check_top is left pointing into the stack frame of the returned function, and the next dep_stack_remove() dereferences it:

AddressSanitizer: stack-use-after-return ... in dep_stack_remove

This can be triggered by an (invalid) Kconfig file in which a symbol is a choice value of two choices and the second choice depends on it:

choice
prompt "c1"
config A
bool "a"
endchoice

choice
prompt "c2"
depends on A
config A
bool "a"
endchoice

Jump to the existing cleanup at the end of the function instead of returning directly.

Found by fuzzing Kconfig input with ASan/UBSan.

Fixes d595cea62403 ("kconfig: print more info when we see a recursive dependency")
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/symbol.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/kconfig/symbol.c +2 -1
index dcb4b45e67..2e87af0985 100644
@@ -1181,7 +1181,7 @@ static void sym_check_print_recursive(struct symbol *last_sym)
 			break;
 	if (!stack) {
 		fprintf(stderr, "unexpected recursive dependency error\n");
-		return;
+		goto out;
 	}
 
 	for (; stack; stack = stack->next) {
@@ -1226,6 +1226,7 @@ static void sym_check_print_recursive(struct symbol *last_sym)
 		"subsection \"Kconfig recursive dependency limitations\"\n"
 		"\n");
 
+out:
 	if (check_top == &cv_stack)
 		dep_stack_remove();
 }
Signature · 1 line
2.34.1
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 26 lines · Leizhen Zhang

If the last symbol of a recursive dependency is a choice value, sym_check_print_recursive() pushes the on-stack cv_stack onto the dependency stack. If the choice is then not found on the stack, the function prints "unexpected recursive dependency error" and returns without removing cv_stack again. check_top is left pointing into the stack frame of the returned function, and the next dep_stack_remove() dereferences it:

AddressSanitizer: stack-use-after-return ... in dep_stack_remove

This can be triggered by an (invalid) Kconfig file in which a symbol is a choice value of two choices and the second choice depends on it:

choice
prompt "c1"
config A
bool "a"
endchoice

choice
prompt "c2"
depends on A
config A
bool "a"
endchoice

Quoted 8 lines

Jump to the existing cleanup at the end of the function instead of returning directly.

Found by fuzzing Kconfig input with ASan/UBSan.

Fixes d595cea62403 ("kconfig: print more info when we see a recursive dependency")
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 26 lines

If the last symbol of a recursive dependency is a choice value, sym_check_print_recursive() pushes the on-stack cv_stack onto the dependency stack. If the choice is then not found on the stack, the function prints "unexpected recursive dependency error" and returns without removing cv_stack again. check_top is left pointing into the stack frame of the returned function, and the next dep_stack_remove() dereferences it:

AddressSanitizer: stack-use-after-return ... in dep_stack_remove

This can be triggered by an (invalid) Kconfig file in which a symbol is a choice value of two choices and the second choice depends on it:

choice
prompt "c1"
config A
bool "a"
endchoice

choice
prompt "c2"
depends on A
config A
bool "a"
endchoice

Quoted 8 lines

Jump to the existing cleanup at the end of the function instead of returning directly.

Found by fuzzing Kconfig input with ASan/UBSan.

Fixes d595cea62403 ("kconfig: print more info when we see a recursive dependency")
Assisted-by Claude:claude-opus-5-5 ASan UBSan libFuzzer
Signature · 2 lines
Cheers,
Nathan
maintainer
#3 lore raw
In reply to Leizhen Zhang
Quoted 9 lines · Leizhen Zhang

If the last symbol of a recursive dependency is a choice value, sym_check_print_recursive() pushes the on-stack cv_stack onto the dependency stack. If the choice is then not found on the stack, the function prints "unexpected recursive dependency error" and returns without removing cv_stack again. check_top is left pointing into the stack frame of the returned function, and the next dep_stack_remove() dereferences it:

[...]

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

[1/1] kconfig: fix dangling check_top after an unexpected recursive dependency

      https://git.kernel.org/kbuild/c/fa887263

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