kconfig/lxdialog: replace strcpy() with strncpy() in inputbox.c
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
@@ -39,8 +39,10 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int widthif (!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
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 withstrncpy(), and null terminates the input string.Signed-off-by Suchit Karunakaran <[email protected]>--- 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
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 withstrncpy(), and null terminates the input string.Signed-off-by Suchit Karunakaran <[email protected]>--- scripts/kconfig/lxdialog/inputbox.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 7 lines
Reviewed-by Nicolas Schier <[email protected]>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!
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 withstrncpy(), and null terminates the input string.Signed-off-by Suchit Karunakaran <[email protected]>--- scripts/kconfig/lxdialog/inputbox.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/kconfig/lxdialog/inputbox.c +0
-0
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 widthif (!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
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 withstrncpy(), and null terminates the input string.Signed-off-by Suchit Karunakaran <[email protected]>--- scripts/kconfig/lxdialog/inputbox.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/kconfig/lxdialog/inputbox.c +0
-0@@ -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
-2if (!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.
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 withstrncpy(), and null terminates the input string.Signed-off-by Suchit Karunakaran <[email protected]>--- scripts/kconfig/lxdialog/inputbox.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/kconfig/lxdialog/inputbox.c +0
-0@@ -39,8 +39,10 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int widthQuoted 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
-2if (!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
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.
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 withstrncpy(), and null terminates the input string.Signed-off-by Suchit Karunakaran <[email protected]>--- scripts/kconfig/lxdialog/inputbox.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/kconfig/lxdialog/inputbox.c +0
-0@@ -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
-2if (!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
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 withstrncpy(), and null terminates the input string.Signed-off-by Suchit Karunakaran <[email protected]>--- scripts/kconfig/lxdialog/inputbox.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/kconfig/lxdialog/inputbox.c +0
-0
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 widthif (!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