Skip to contentSkip to conversation

kconfig/lxdialog: replace strcpy() with strncpy() in inputbox.c

PATCH In review Suchit Karunakaran 4 participants 9 messages Jul 27, 2025 – Jul 29, 2025 1 message not in the archive

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

PATCH v3 1/1 kconfig/lxdialog: replace strcpy() with strncpy() in inputbox.c

strcpy() performs no bounds checking and can lead to buffer overflows if the input string exceeds the destination buffer size. This patch replaces it with strncpy(), and null terminates the input string.

---
 scripts/kconfig/lxdialog/inputbox.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/lxdialog/inputbox.c +4 -2
index 3c6e24b20f5b..5e4a131724f2 100644
@@ -39,8 +39,10 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
 
 	if (!init)
 		instr[0] = '\0';
-	else
-		strcpy(instr, init);
+	else {
+		strncpy(instr, init, sizeof(dialog_input_result) - 1);
+		instr[sizeof(dialog_input_result) - 1] = '\0';
+	}
 
 do_resize:
 	if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGHT_MIN))
Signature · 1 line
2.50.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 8 lines · On Sun, Jul 27, 2025 at 10:14:33PM +0530 Suchit Karunakaran wrote:

strcpy() performs no bounds checking and can lead to buffer overflows if the input string exceeds the destination buffer size. This patch replaces it with strncpy(), and null terminates the input string.

---
 scripts/kconfig/lxdialog/inputbox.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

thanks for your contribution!

If you want to continue contributing, you might want to check-out tools like b4 which simplifies sending and tracking patch-sets.

Kind regards,
Nicolas

author
#2 lore raw
Quoted 11 lines · Nicolas Schier
Quoted 8 lines · On Sun, Jul 27, 2025 at 10:14:33PM +0530 Suchit Karunakaran wrote:

strcpy() performs no bounds checking and can lead to buffer overflows if the input string exceeds the destination buffer size. This patch replaces it with strncpy(), and null terminates the input string.

---
 scripts/kconfig/lxdialog/inputbox.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 7 lines

thanks for your contribution!

If you want to continue contributing, you might want to check-out tools like b4 which simplifies sending and tracking patch-sets.

Thank you very much! Please let me know if there's anything else I should do. Also, I'd appreciate it if you could take a look at my other patch that replaces strcpy with snprintf. Thanks again!

#3 lore raw
In reply to Suchit Karunakaran
Quoted 14 lines · Suchit Karunakaran

strcpy() performs no bounds checking and can lead to buffer overflows if the input string exceeds the destination buffer size. This patch replaces it with strncpy(), and null terminates the input string.

---
 scripts/kconfig/lxdialog/inputbox.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/lxdialog/inputbox.c +0 -0
index 3c6e24b20f5b..5e4a131724f2 100644

scripts/kconfig/lxdialog/inputbox.c int dialog_inputbox(const char *title, const char *prompt, int height, int width · line 44

Quoted 8 lines
scripts/kconfig/lxdialog/inputbox.c +3 -2
@@ -39,8 +39,10 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
 
 	if (!init)
 		instr[0] = '\0';
-	else
-		strcpy(instr, init);
+	else {
+		strncpy(instr, init, sizeof(dialog_input_result) - 1);
+		instr[sizeof(dialog_input_result) - 1] = '\0';

As this is a userspace tool, why is this change needed at all? How can this overflow and if it does, what happens?

thanks,

greg k-h

author
#4 lore raw
Quoted 17 lines · Greg KH
Quoted 15 lines · Suchit Karunakaran

strcpy() performs no bounds checking and can lead to buffer overflows if the input string exceeds the destination buffer size. This patch replaces it with strncpy(), and null terminates the input string.

---
 scripts/kconfig/lxdialog/inputbox.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/lxdialog/inputbox.c +0 -0
index 3c6e24b20f5b..5e4a131724f2 100644
@@ -39,8 +39,10 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
 
Quoted 11 lines

scripts/kconfig/lxdialog/inputbox.c int dialog_inputbox(const char *title, const char *prompt, int height, int width · line 44

Quoted 7 lines
scripts/kconfig/lxdialog/inputbox.c +3 -2
      if (!init)
              instr[0] = '\0';
-     else
-             strcpy(instr, init);
+     else {
+             strncpy(instr, init, sizeof(dialog_input_result) - 1);
+             instr[sizeof(dialog_input_result) - 1] = '\0';

As this is a userspace tool, why is this change needed at all? How can this overflow and if it does, what happens?

Hi Greg. The primary motivation for this patch was the deprecation of strcpy(). Additionally, I believed there was a possibility of a buffer overflow if the initial string accidentally exceeded the length of instr, although the chances might be low.

#5 lore raw
Quoted 30 lines · Suchit K
Quoted 17 lines · Greg KH
Quoted 15 lines · Suchit Karunakaran

strcpy() performs no bounds checking and can lead to buffer overflows if the input string exceeds the destination buffer size. This patch replaces it with strncpy(), and null terminates the input string.

---
 scripts/kconfig/lxdialog/inputbox.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/lxdialog/inputbox.c +0 -0
index 3c6e24b20f5b..5e4a131724f2 100644
@@ -39,8 +39,10 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
 
Quoted 11 lines

scripts/kconfig/lxdialog/inputbox.c int dialog_inputbox(const char *title, const char *prompt, int height, int width · line 44

Quoted 7 lines
scripts/kconfig/lxdialog/inputbox.c +3 -2
      if (!init)
              instr[0] = '\0';
-     else
-             strcpy(instr, init);
+     else {
+             strncpy(instr, init, sizeof(dialog_input_result) - 1);
+             instr[sizeof(dialog_input_result) - 1] = '\0';

As this is a userspace tool, why is this change needed at all? How can this overflow and if it does, what happens?

Quoted 4 lines

Hi Greg. The primary motivation for this patch was the deprecation of strcpy(). Additionally, I believed there was a possibility of a buffer overflow if the initial string accidentally exceeded the length of instr, although the chances might be low.

Is strcpy() being deprecated in userspace? I think it's a core part of the C language specification :)

Again, how can that buffer be "too large"?

thanks,

greg k-h

A message that is not in the archive (sent off-list or not yet received): CAO9wTFi+atf1vwMrDJBa-X4W5UcQ8K80spgiGhMyhZj4aRJ3Zw@mail.gmail.com

author
#7 lore raw

Resending because I unknowingly disabled plain text mode. Sorry about that.

Quoted 4 lines

Is strcpy() being deprecated in userspace? I think it's a core part of the C language specification :)

My apologies. I was under the assumption that all folders within the kernel repository adhered to the kernel coding guidelines, only to realize that these guidelines primarily apply to kernel space code. You're right, strcpy() isn't deprecated in userspace but as far as I know some compilers emit warnings to replace it with other functions since it's unsafe.

Quoted 2 lines

Again, how can that buffer be "too large"?

Tbh I'm not sure. I was glancing through the code in mconf.c and there were some calls to dialog_inputbox() with file names and a variable length string being passed for the init argument. So, I thought there might be some chance of overflowing.

#8 lore raw
In reply to Greg KH
Quoted 17 lines · Greg KH
Quoted 15 lines · Suchit Karunakaran

strcpy() performs no bounds checking and can lead to buffer overflows if the input string exceeds the destination buffer size. This patch replaces it with strncpy(), and null terminates the input string.

---
 scripts/kconfig/lxdialog/inputbox.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/lxdialog/inputbox.c +0 -0
index 3c6e24b20f5b..5e4a131724f2 100644
@@ -39,8 +39,10 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
 
Quoted 10 lines

scripts/kconfig/lxdialog/inputbox.c int dialog_inputbox(const char *title, const char *prompt, int height, int width · line 44

Quoted 7 lines
scripts/kconfig/lxdialog/inputbox.c +3 -2
      if (!init)
              instr[0] = '\0';
-     else
-             strcpy(instr, init);
+     else {
+             strncpy(instr, init, sizeof(dialog_input_result) - 1);
+             instr[sizeof(dialog_input_result) - 1] = '\0';

As this is a userspace tool, why is this change needed at all? How can this overflow and if it does, what happens?

The buffer size (2049 byte) is large enough, and buffer overflow is unlikely to happen in practical use cases. If it does, I think the program will crash.

Signature · 2 lines
Best Regards
Masahiro Yamada
#9 lore raw
In reply to Suchit Karunakaran
Quoted 15 lines · Suchit Karunakaran

strcpy() performs no bounds checking and can lead to buffer overflows if the input string exceeds the destination buffer size. This patch replaces it with strncpy(), and null terminates the input string.

---
 scripts/kconfig/lxdialog/inputbox.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/lxdialog/inputbox.c +0 -0
index 3c6e24b20f5b..5e4a131724f2 100644

scripts/kconfig/lxdialog/inputbox.c int dialog_inputbox(const char *title, const char *prompt, int height, int width · line 45

Quoted 9 lines
scripts/kconfig/lxdialog/inputbox.c +4 -2
@@ -39,8 +39,10 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
 
        if (!init)
                instr[0] = '\0';
-       else
-               strcpy(instr, init);
+       else {
+               strncpy(instr, init, sizeof(dialog_input_result) - 1);
+               instr[sizeof(dialog_input_result) - 1] = '\0';
+       }

Applied to linux-kbuild. Thanks.

Signature · 2 lines
Best Regards
Masahiro Yamada