All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup()
@ 2025-08-19  9:59 Thorsten Blum
  2025-08-19  9:59 ` [PATCH v2 2/4] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf() Thorsten Blum
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Thorsten Blum @ 2025-08-19  9:59 UTC (permalink / raw)
  To: Jason Wessel, Daniel Thompson, Douglas Anderson,
	Dr. David Alan Gilbert, Zhang Heng
  Cc: linux-hardening, Thorsten Blum, kgdb-bugreport, linux-kernel

strcpy() is deprecated; use memcpy() instead.

Link: https://github.com/KSPP/linux/issues/88
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 kernel/debug/kdb/kdb_support.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/kernel/debug/kdb/kdb_support.c b/kernel/debug/kdb/kdb_support.c
index 05b137e7dcb9..d36281142fa1 100644
--- a/kernel/debug/kdb/kdb_support.c
+++ b/kernel/debug/kdb/kdb_support.c
@@ -23,6 +23,7 @@
 #include <linux/uaccess.h>
 #include <linux/kdb.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include <linux/ctype.h>
 #include "kdb_private.h"
 
@@ -246,11 +247,12 @@ void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,
  */
 char *kdb_strdup(const char *str, gfp_t type)
 {
-	int n = strlen(str)+1;
+	size_t n = strlen(str) + 1;
 	char *s = kmalloc(n, type);
 	if (!s)
 		return NULL;
-	return strcpy(s, str);
+	memcpy(s, str, n);
+	return s;
 }
 
 /*
-- 
2.50.1


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

* [PATCH v2 2/4] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()
  2025-08-19  9:59 [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() Thorsten Blum
@ 2025-08-19  9:59 ` Thorsten Blum
  2025-08-19  9:59 ` [PATCH v2 3/4] kdb: Replace deprecated strcpy() with memcpy() in parse_grep() Thorsten Blum
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Thorsten Blum @ 2025-08-19  9:59 UTC (permalink / raw)
  To: Jason Wessel, Daniel Thompson, Douglas Anderson, Justin Stitt,
	Thorsten Blum, Martin Hicks
  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
Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Thorsten Blum <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] 6+ messages in thread

* [PATCH v2 3/4] kdb: Replace deprecated strcpy() with memcpy() in parse_grep()
  2025-08-19  9:59 [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() Thorsten Blum
  2025-08-19  9:59 ` [PATCH v2 2/4] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf() Thorsten Blum
@ 2025-08-19  9:59 ` Thorsten Blum
  2025-08-19  9:59 ` [PATCH v2 4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd() Thorsten Blum
  2025-09-26 14:59 ` [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() Daniel Thompson
  3 siblings, 0 replies; 6+ messages in thread
From: Thorsten Blum @ 2025-08-19  9:59 UTC (permalink / raw)
  To: Jason Wessel, Daniel Thompson, Douglas Anderson, Nir Lichtman,
	Greg Kroah-Hartman, Yuran Pereira
  Cc: linux-hardening, Thorsten Blum, Daniel Thompson, kgdb-bugreport,
	linux-kernel

strcpy() is deprecated; use memcpy() instead.

We can safely use memcpy() because we already know the length of the
source string 'cp' and that it is guaranteed to be NUL-terminated within
the first KDB_GREP_STRLEN bytes.

Link: https://github.com/KSPP/linux/issues/88
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 kernel/debug/kdb/kdb_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index 7a4d2d4689a5..cdf91976eb7c 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -860,7 +860,7 @@ static void parse_grep(const char *str)
 		kdb_printf("search string too long\n");
 		return;
 	}
-	strcpy(kdb_grep_string, cp);
+	memcpy(kdb_grep_string, cp, len + 1);
 	kdb_grepping_flag++;
 	return;
 }
-- 
2.50.1


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

* [PATCH v2 4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()
  2025-08-19  9:59 [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() Thorsten Blum
  2025-08-19  9:59 ` [PATCH v2 2/4] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf() Thorsten Blum
  2025-08-19  9:59 ` [PATCH v2 3/4] kdb: Replace deprecated strcpy() with memcpy() in parse_grep() Thorsten Blum
@ 2025-08-19  9:59 ` Thorsten Blum
  2025-09-08 10:28   ` Thorsten Blum
  2025-09-26 14:59 ` [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() Daniel Thompson
  3 siblings, 1 reply; 6+ messages in thread
From: Thorsten Blum @ 2025-08-19  9:59 UTC (permalink / raw)
  To: Jason Wessel, Daniel Thompson, Douglas Anderson, Nir Lichtman,
	Yuran Pereira, Greg Kroah-Hartman, Zhang Heng,
	Dr. David Alan Gilbert
  Cc: linux-hardening, Thorsten Blum, Daniel Thompson, kgdb-bugreport,
	linux-kernel

strcpy() is deprecated; use the new helper function kdb_strdup_dequote()
instead. In addition to string duplication similar to kdb_strdup(), it
also trims surrounding quotes from the input string if present.

kdb_strdup_dequote() also checks for a trailing quote in the input
string which was previously not checked.

Link: https://github.com/KSPP/linux/issues/88
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
Changes in v2:
- Copy 'len - 1' bytes using memcpy() as suggested by Doug
- Link to v1: https://lore.kernel.org/lkml/20250818181153.661431-4-thorsten.blum@linux.dev/
---
 kernel/debug/kdb/kdb_main.c    | 12 ++----------
 kernel/debug/kdb/kdb_private.h |  1 +
 kernel/debug/kdb/kdb_support.c | 29 +++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index cdf91976eb7c..dddf2b5aad57 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -721,20 +721,12 @@ static int kdb_defcmd(int argc, const char **argv)
 	mp->name = kdb_strdup(argv[1], GFP_KDB);
 	if (!mp->name)
 		goto fail_name;
-	mp->usage = kdb_strdup(argv[2], GFP_KDB);
+	mp->usage = kdb_strdup_dequote(argv[2], GFP_KDB);
 	if (!mp->usage)
 		goto fail_usage;
-	mp->help = kdb_strdup(argv[3], GFP_KDB);
+	mp->help = kdb_strdup_dequote(argv[3], GFP_KDB);
 	if (!mp->help)
 		goto fail_help;
-	if (mp->usage[0] == '"') {
-		strcpy(mp->usage, argv[2]+1);
-		mp->usage[strlen(mp->usage)-1] = '\0';
-	}
-	if (mp->help[0] == '"') {
-		strcpy(mp->help, argv[3]+1);
-		mp->help[strlen(mp->help)-1] = '\0';
-	}
 
 	INIT_LIST_HEAD(&kdb_macro->statements);
 	defcmd_in_progress = true;
diff --git a/kernel/debug/kdb/kdb_private.h b/kernel/debug/kdb/kdb_private.h
index d2520d72b1f5..a2fc7d2bc9fc 100644
--- a/kernel/debug/kdb/kdb_private.h
+++ b/kernel/debug/kdb/kdb_private.h
@@ -110,6 +110,7 @@ extern int kdbgetaddrarg(int, const char **, int*, unsigned long *,
 extern int kdbgetsymval(const char *, kdb_symtab_t *);
 extern int kdbnearsym(unsigned long, kdb_symtab_t *);
 extern char *kdb_strdup(const char *str, gfp_t type);
+extern char *kdb_strdup_dequote(const char *str, gfp_t type);
 extern void kdb_symbol_print(unsigned long, const kdb_symtab_t *, unsigned int);
 
 /* Routine for debugging the debugger state. */
diff --git a/kernel/debug/kdb/kdb_support.c b/kernel/debug/kdb/kdb_support.c
index d36281142fa1..56f7b906e7cc 100644
--- a/kernel/debug/kdb/kdb_support.c
+++ b/kernel/debug/kdb/kdb_support.c
@@ -255,6 +255,35 @@ char *kdb_strdup(const char *str, gfp_t type)
 	return s;
 }
 
+/*
+ * kdb_strdup_dequote - same as kdb_strdup(), but trims surrounding quotes from
+ *			the input string if present.
+ * Remarks:
+ *	Quotes are only removed if there is both a leading and a trailing quote.
+ */
+char *kdb_strdup_dequote(const char *str, gfp_t type)
+{
+	size_t len = strlen(str);
+	char *s;
+
+	if (str[0] == '"' && len > 1 && str[len - 1] == '"') {
+		/* trim both leading and trailing quotes */
+		str++;
+		len -= 2;
+	}
+
+	len++; /* add space for NUL terminator */
+
+	s = kmalloc(len, type);
+	if (!s)
+		return NULL;
+
+	memcpy(s, str, len - 1);
+	s[len - 1] = '\0';
+
+	return s;
+}
+
 /*
  * kdb_getarea_size - Read an area of data.  The kdb equivalent of
  *	copy_from_user, with kdb messages for invalid addresses.
-- 
2.50.1


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

* Re: [PATCH v2 4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()
  2025-08-19  9:59 ` [PATCH v2 4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd() Thorsten Blum
@ 2025-09-08 10:28   ` Thorsten Blum
  0 siblings, 0 replies; 6+ messages in thread
From: Thorsten Blum @ 2025-09-08 10:28 UTC (permalink / raw)
  To: Jason Wessel, Daniel Thompson, Douglas Anderson, Nir Lichtman,
	Yuran Pereira, Greg Kroah-Hartman, Zhang Heng,
	Dr. David Alan Gilbert
  Cc: linux-hardening, Daniel Thompson, kgdb-bugreport, linux-kernel

Hi Daniel,

On 19. Aug 2025, at 11:59, Thorsten Blum wrote:
> strcpy() is deprecated; use the new helper function kdb_strdup_dequote()
> instead. In addition to string duplication similar to kdb_strdup(), it
> also trims surrounding quotes from the input string if present.
> 
> kdb_strdup_dequote() also checks for a trailing quote in the input
> string which was previously not checked.
> 
> Link: https://github.com/KSPP/linux/issues/88
> Reviewed-by: Douglas Anderson <dianders@chromium.org>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---

Could you give this series another look? All four patches have been
reviewed by Doug and it still applies to -next.

Thanks,
Thorsten


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

* Re: [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup()
  2025-08-19  9:59 [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() Thorsten Blum
                   ` (2 preceding siblings ...)
  2025-08-19  9:59 ` [PATCH v2 4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd() Thorsten Blum
@ 2025-09-26 14:59 ` Daniel Thompson
  3 siblings, 0 replies; 6+ messages in thread
From: Daniel Thompson @ 2025-09-26 14:59 UTC (permalink / raw)
  To: Jason Wessel, Douglas Anderson, Dr. David Alan Gilbert,
	Zhang Heng, Thorsten Blum
  Cc: linux-hardening, kgdb-bugreport, linux-kernel


On Tue, 19 Aug 2025 11:59:03 +0200, Thorsten Blum wrote:
> strcpy() is deprecated; use memcpy() instead.
> 
> 

Applied, thanks!

[1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup()
      commit: d4be3238d9e5f4841e5385cba3d81268c00d9e7d
[2/4] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()
      commit: 8790cc2940bf9f5ec4d7458b0ea7f94a8acb094f
[3/4] kdb: Replace deprecated strcpy() with memcpy() in parse_grep()
      commit: 5b26f1a3146454a24dbcb8b1cdae5d507f7432e6
[4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()
      commit: 0c28a23722e03695ae1cbbadde3dd32d75c1cb7f

Best regards,
-- 
Daniel Thompson (RISCstar) <danielt@kernel.org>


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

end of thread, other threads:[~2025-09-26 14:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-19  9:59 [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() Thorsten Blum
2025-08-19  9:59 ` [PATCH v2 2/4] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf() Thorsten Blum
2025-08-19  9:59 ` [PATCH v2 3/4] kdb: Replace deprecated strcpy() with memcpy() in parse_grep() Thorsten Blum
2025-08-19  9:59 ` [PATCH v2 4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd() Thorsten Blum
2025-09-08 10:28   ` Thorsten Blum
2025-09-26 14:59 ` [PATCH v2 1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() Daniel Thompson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.