linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()
@ 2025-08-12 13:26 Thorsten Blum
  2025-08-12 16:24 ` Doug Anderson
  0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Blum @ 2025-08-12 13:26 UTC (permalink / raw)
  To: Jason Wessel, Daniel Thompson, Douglas Anderson, Thorsten Blum,
	Justin Stitt
  Cc: linux-hardening, Daniel Thompson, kgdb-bugreport, linux-kernel

strcpy() is deprecated and its behavior is undefined when the source and
destination buffers overlap. Use memmove() instead to avoid any
undefined behavior.

Adjust comments for clarity.

Link: https://github.com/KSPP/linux/issues/88
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
Changes in v2:
- Use memmove() because of strcpy()'s undefined behavior with
  overlapping buffers as suggested by Doug Anderson
- Compile-tested only
- Link to v1: https://lore.kernel.org/lkml/20250811170351.68985-1-thorsten.blum@linux.dev/
---
 kernel/debug/kdb/kdb_io.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
index 9b11b10b120c..b12b9db75c1d 100644
--- a/kernel/debug/kdb/kdb_io.c
+++ b/kernel/debug/kdb/kdb_io.c
@@ -714,8 +714,8 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
 		 * it, depending on the results of the search.
 		 */
 		cp++;	 	     /* to byte after the newline */
-		replaced_byte = *cp; /* remember what/where it was */
-		cphold = cp;
+		replaced_byte = *cp; /* remember what it was */
+		cphold = cp;	     /* remember where it was */
 		*cp = '\0';	     /* end the string for our search */
 
 		/*
@@ -732,8 +732,9 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
 			 * Shift the buffer left.
 			 */
 			*cphold = replaced_byte;
-			strcpy(kdb_buffer, cphold);
-			len = strlen(kdb_buffer);
+			len = strlen(cphold);
+			/* Use memmove() because the buffers overlap */
+			memmove(kdb_buffer, cphold, len + 1);
 			next_avail = kdb_buffer + len;
 			size_avail = sizeof(kdb_buffer) - len;
 			goto kdb_print_out;
@@ -872,8 +873,9 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
 	 */
 	if (kdb_grepping_flag && !suspend_grep) {
 		*cphold = replaced_byte;
-		strcpy(kdb_buffer, cphold);
-		len = strlen(kdb_buffer);
+		len = strlen(cphold);
+		/* Use memmove() because the buffers overlap */
+		memmove(kdb_buffer, cphold, len + 1);
 		next_avail = kdb_buffer + len;
 		size_avail = sizeof(kdb_buffer) - len;
 	}
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()
  2025-08-12 13:26 [PATCH v2] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf() Thorsten Blum
@ 2025-08-12 16:24 ` Doug Anderson
  2025-08-15 14:32   ` Daniel Thompson
  0 siblings, 1 reply; 4+ messages in thread
From: Doug Anderson @ 2025-08-12 16:24 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Jason Wessel, Daniel Thompson, Justin Stitt, linux-hardening,
	Daniel Thompson, kgdb-bugreport, linux-kernel

Hi,

On Tue, Aug 12, 2025 at 6:27 AM Thorsten Blum <thorsten.blum@linux.dev> wrote:
>
> strcpy() is deprecated and its behavior is undefined when the source and
> destination buffers overlap. Use memmove() instead to avoid any
> undefined behavior.
>
> Adjust comments for clarity.
>
> Link: https://github.com/KSPP/linux/issues/88
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> Changes in v2:
> - Use memmove() because of strcpy()'s undefined behavior with
>   overlapping buffers as suggested by Doug Anderson
> - Compile-tested only
> - Link to v1: https://lore.kernel.org/lkml/20250811170351.68985-1-thorsten.blum@linux.dev/
> ---
>  kernel/debug/kdb/kdb_io.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)

Much nicer, thank you!

Given that the old code was officially relying on undefined behavior
of strcpy() before, I'd personally even add:

Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")

In any case:

Reviewed-by: Douglas Anderson <dianders@chromium.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()
  2025-08-12 16:24 ` Doug Anderson
@ 2025-08-15 14:32   ` Daniel Thompson
  2025-08-15 14:56     ` Thorsten Blum
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Thompson @ 2025-08-15 14:32 UTC (permalink / raw)
  To: Doug Anderson
  Cc: Thorsten Blum, Jason Wessel, Daniel Thompson, Justin Stitt,
	linux-hardening, kgdb-bugreport, linux-kernel

On Tue, Aug 12, 2025 at 09:24:55AM -0700, Doug Anderson wrote:
> Hi,
>
> On Tue, Aug 12, 2025 at 6:27 AM Thorsten Blum <thorsten.blum@linux.dev> wrote:
> >
> > strcpy() is deprecated and its behavior is undefined when the source and
> > destination buffers overlap. Use memmove() instead to avoid any
> > undefined behavior.
> >
> > Adjust comments for clarity.
> >
> > Link: https://github.com/KSPP/linux/issues/88
> > Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> > ---
> > Changes in v2:
> > - Use memmove() because of strcpy()'s undefined behavior with
> >   overlapping buffers as suggested by Doug Anderson
> > - Compile-tested only
> > - Link to v1: https://lore.kernel.org/lkml/20250811170351.68985-1-thorsten.blum@linux.dev/
> > ---
> >  kernel/debug/kdb/kdb_io.c | 14 ++++++++------
> >  1 file changed, 8 insertions(+), 6 deletions(-)
>
> Much nicer, thank you!
>
> Given that the old code was officially relying on undefined behavior
> of strcpy() before, I'd personally even add:
>
> Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
>
> In any case:
>
> Reviewed-by: Douglas Anderson <dianders@chromium.org>

LGTM... and I agree that this is bug rather than a clean up so am
waiting to hear back on the Fixes: .


Daniel


PS Feel free to roll all three kdb patches discussed recently into a
   series so I can pull in one go ;-) .

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()
  2025-08-15 14:32   ` Daniel Thompson
@ 2025-08-15 14:56     ` Thorsten Blum
  0 siblings, 0 replies; 4+ messages in thread
From: Thorsten Blum @ 2025-08-15 14:56 UTC (permalink / raw)
  To: Daniel Thompson
  Cc: Doug Anderson, Jason Wessel, Daniel Thompson, Justin Stitt,
	linux-hardening, kgdb-bugreport, linux-kernel

On 15. Aug 2025, at 16:32, Daniel Thompson wrote:
> On Tue, Aug 12, 2025 at 09:24:55AM -0700, Doug Anderson wrote:
>> 
>> On Tue, Aug 12, 2025 at 6:27 AM Thorsten Blum wrote:
>>> 
>>> strcpy() is deprecated and its behavior is undefined when the source and
>>> destination buffers overlap. Use memmove() instead to avoid any
>>> undefined behavior.
>>> 
>>> Adjust comments for clarity.
>>> 
>>> Link: https://github.com/KSPP/linux/issues/88
>>> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
>>> ---
>>> Changes in v2:
>>> - Use memmove() because of strcpy()'s undefined behavior with
>>>  overlapping buffers as suggested by Doug Anderson
>>> - Compile-tested only
>>> - Link to v1: https://lore.kernel.org/lkml/20250811170351.68985-1-thorsten.blum@linux.dev/
>>> ---
>>> kernel/debug/kdb/kdb_io.c | 14 ++++++++------
>>> 1 file changed, 8 insertions(+), 6 deletions(-)
>> 
>> Much nicer, thank you!
>> 
>> Given that the old code was officially relying on undefined behavior
>> of strcpy() before, I'd personally even add:
>> 
>> Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
>> 
>> In any case:
>> 
>> Reviewed-by: Douglas Anderson <dianders@chromium.org>
> 
> LGTM... and I agree that this is bug rather than a clean up so am
> waiting to hear back on the Fixes: .

Yes, the Fixes: tag is fine, of course. Thanks!

How about backporting this to stable?

Best,
Thorsten


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-08-15 14:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-12 13:26 [PATCH v2] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf() Thorsten Blum
2025-08-12 16:24 ` Doug Anderson
2025-08-15 14:32   ` Daniel Thompson
2025-08-15 14:56     ` Thorsten Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).