Skip to contentSkip to conversation

kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup()

PATCH Applied Thorsten Blum 2 participants 6 messages Aug 19, 2025 – Sep 26, 2025 KGDB / KDB /debug_core

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup()

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

---
 kernel/debug/kdb/kdb_support.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/debug/kdb/kdb_support.c +4 -2
index 05b137e7dcb9..d36281142fa1 100644
@@ -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;
 }
 
 /*
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 2/4 kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()

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.

Fixes 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
---
 kernel/debug/kdb/kdb_io.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
kernel/debug/kdb/kdb_io.c +8 -6
index 9b11b10b120c..b12b9db75c1d 100644
@@ -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;
 	}
Signature · 1 line
2.50.1
#2 lore raw

PATCH v2 3/4 kdb: Replace deprecated strcpy() with memcpy() in parse_grep()

In reply to Thorsten Blum

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.

---
 kernel/debug/kdb/kdb_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/debug/kdb/kdb_main.c +1 -1
index 7a4d2d4689a5..cdf91976eb7c 100644
@@ -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;
 }
Signature · 1 line
2.50.1
#3 lore raw

PATCH v2 4/4 kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()

In reply to Thorsten Blum

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.

---
Changes in v2:
- Copy 'len - 1' bytes using memcpy() as suggested by Doug
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 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(-)
kernel/debug/kdb/kdb_main.c +2 -10
index cdf91976eb7c..dddf2b5aad57 100644
@@ -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;
kernel/debug/kdb/kdb_private.h +1 -0
index d2520d72b1f5..a2fc7d2bc9fc 100644
@@ -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. */
kernel/debug/kdb/kdb_support.c +29 -0
index d36281142fa1..56f7b906e7cc 100644
@@ -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.
Signature · 1 line
2.50.1
#4 lore raw

Re: [PATCH v2 4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()

Hi Daniel,

Quoted 11 lines · Thorsten Blum

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.

---

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

Thanks,
Thorsten

maintainer Applied
#5 lore raw
In reply to Thorsten Blum
Quoted 3 lines · Thorsten Blum

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,

Signature · 1 line
Daniel Thompson (RISCstar) <[email protected]>