Skip to contentSkip to conversation

kconfig: fix potential NULL pointer dereference in conf_askvalue

PATCH Superseded Xingjing Deng 2 participants 5 messages Feb 25 – Mar 1

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kconfig: fix potential NULL pointer dereference in conf_askvalue

In conf_askvalue(), the 'def' argument (retrieved via sym_get_string_value) can be NULL. When the symbol is not changeable, the code calls printf("%s\n", def), which leads to a segmentation fault on certain systems/libc implementations when passing a NULL pointer to %s.

This patch adds a check to ensure 'def' is not NULL before printing. Additionally, it removes the redundant re-initialization of the 'line' buffer inside the !sym_is_changeable(sym) block, as it is already initialized at the beginning of the function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 scripts/kconfig/conf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
scripts/kconfig/conf.c +2 -4
index a7b44cd8a..2771bc84e 100644
@@ -297,9 +297,7 @@ static int conf_askvalue(struct symbol *sym, const char *def)
 	line[1] = 0;
 
 	if (!sym_is_changeable(sym)) {
-		printf("%s\n", def);
-		line[0] = '\n';
-		line[1] = 0;
+		printf("%s\n", def ? def : "");
 		return 0;
 	}
 
@@ -307,7 +305,7 @@ static int conf_askvalue(struct symbol *sym, const char *def)
 	case oldconfig:
 	case syncconfig:
 		if (sym_has_value(sym)) {
-			printf("%s\n", def);
+			printf("%s\n", def ? def : "");
 			return 0;
 		}
 		/* fall through */
Signature · 1 line
2.25.1
#1 lore raw
Quoted 4 lines · Xingjing Deng

In conf_askvalue(), the 'def' argument (retrieved via sym_get_string_value) can be NULL. When the symbol is not changeable, the code calls printf("%s\n", def), which leads to a segmentation fault on certain systems/libc implementations when passing a NULL pointer to %s.

How do you reproduce this segmentation fault? Surely someone would have hit this if it were a real problem given the Fixes tag? Or is this a corner case?

Quoted 38 lines

This patch adds a check to ensure 'def' is not NULL before printing. Additionally, it removes the redundant re-initialization of the 'line' buffer inside the !sym_is_changeable(sym) block, as it is already initialized at the beginning of the function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 scripts/kconfig/conf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
scripts/kconfig/conf.c +2 -4
index a7b44cd8a..2771bc84e 100644
@@ -297,9 +297,7 @@ static int conf_askvalue(struct symbol *sym, const char *def)
 	line[1] = 0;
 
 	if (!sym_is_changeable(sym)) {
-		printf("%s\n", def);
-		line[0] = '\n';
-		line[1] = 0;
+		printf("%s\n", def ? def : "");
 		return 0;
 	}
 
@@ -307,7 +305,7 @@ static int conf_askvalue(struct symbol *sym, const char *def)
 	case oldconfig:
 	case syncconfig:
 		if (sym_has_value(sym)) {
-			printf("%s\n", def);
+			printf("%s\n", def ? def : "");
 			return 0;
 		}
 		/* fall through */
Signature · 1 line
2.25.1
#2 lore raw
Quoted 10 lines · Nathan Chancellor <[email protected]> 于2026年2月26日周四 03:44写道:
Quoted 4 lines · Xingjing Deng

In conf_askvalue(), the 'def' argument (retrieved via sym_get_string_value) can be NULL. When the symbol is not changeable, the code calls printf("%s\n", def), which leads to a segmentation fault on certain systems/libc implementations when passing a NULL pointer to %s.

How do you reproduce this segmentation fault? Surely someone would have hit this if it were a real problem given the Fixes tag? Or is this a corner case?

I tested printing NULL with printf locally and confirmed that it does cause issues. In my opinion, this problem is more of a corner case—I identified it through static program analysis and have not yet reproduced it in practice.

Quoted 39 lines
Quoted 39 lines

This patch adds a check to ensure 'def' is not NULL before printing. Additionally, it removes the redundant re-initialization of the 'line' buffer inside the !sym_is_changeable(sym) block, as it is already initialized at the beginning of the function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 scripts/kconfig/conf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
scripts/kconfig/conf.c +2 -5
index a7b44cd8a..2771bc84e 100644
@@ -297,9 +297,7 @@ static int conf_askvalue(struct symbol *sym, const char *def)
      line[1] = 0;
 
      if (!sym_is_changeable(sym)) {
-             printf("%s\n", def);
-             line[0] = '\n';
-             line[1] = 0;
+             printf("%s\n", def ? def : "");
              return 0;
      }
 
@@ -307,7 +305,7 @@ static int conf_askvalue(struct symbol *sym, const char *def)
      case oldconfig:
      case syncconfig:
              if (sym_has_value(sym)) {
-                     printf("%s\n", def);
+                     printf("%s\n", def ? def : "");
                      return 0;
              }
              /* fall through */
--

2.25.1

#3 lore raw
Quoted 8 lines · Xingjing Deng
Quoted 7 lines · Nathan Chancellor <[email protected]> 于2026年2月26日周四 03:44写道:
Quoted 4 lines · Xingjing Deng

In conf_askvalue(), the 'def' argument (retrieved via sym_get_string_value) can be NULL. When the symbol is not changeable, the code calls printf("%s\n", def), which leads to a segmentation fault on certain systems/libc implementations when passing a NULL pointer to %s.

Quoted 8 lines
Quoted 3 lines

How do you reproduce this segmentation fault? Surely someone would have hit this if it were a real problem given the Fixes tag? Or is this a corner case?

I tested printing NULL with printf locally and confirmed that it does cause issues. In my opinion, this problem is more of a corner case—I identified it through static program analysis and have not yet reproduced it in practice.

Thanks for confirming. I think it would be better to make this clearer in the commit message because it reads as though the problem is reproducible in practice. Also, 'def ?: ""' would do the same thing with fewer characters.

Cheers,
Nathan

#4 lore raw
Quoted 19 lines · Nathan Chancellor <[email protected]> 于2026年2月27日周五 04:35写道:
Quoted 8 lines · Xingjing Deng
Quoted 7 lines · Nathan Chancellor <[email protected]> 于2026年2月26日周四 03:44写道:
Quoted 4 lines · Xingjing Deng

In conf_askvalue(), the 'def' argument (retrieved via sym_get_string_value) can be NULL. When the symbol is not changeable, the code calls printf("%s\n", def), which leads to a segmentation fault on certain systems/libc implementations when passing a NULL pointer to %s.

Quoted 8 lines
Quoted 3 lines

How do you reproduce this segmentation fault? Surely someone would have hit this if it were a real problem given the Fixes tag? Or is this a corner case?

I tested printing NULL with printf locally and confirmed that it does cause issues. In my opinion, this problem is more of a corner case—I identified it through static program analysis and have not yet reproduced it in practice.

Quoted 7 lines

Thanks for confirming. I think it would be better to make this clearer in the commit message because it reads as though the problem is reproducible in practice. Also, 'def ?: ""' would do the same thing with fewer characters.

Cheers,
Nathan

OK, I have published v2 now.
Thanks for your reply.