Skip to contentSkip to conversation

params: Replace deprecated strcpy() with strscpy() and memcpy()

PATCH Applied Thorsten Blum 4 participants 4 messages Aug 13, 2025 – Aug 16, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 params: Replace deprecated strcpy() with strscpy() and memcpy()

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

In param_set_copystring(), we can safely use memcpy() because we already know the length of the source string 'val' and that it is guaranteed to be NUL-terminated within the first 'kps->maxlen' bytes.

---
Changes in v2:
- Use memcpy() in param_set_copystring() as suggested by Petr Pavlu
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 kernel/params.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/params.c +4 -3
index b92d64161b75..b96cfd693c99 100644
@@ -513,13 +513,14 @@ EXPORT_SYMBOL(param_array_ops);
 int param_set_copystring(const char *val, const struct kernel_param *kp)
 {
 	const struct kparam_string *kps = kp->str;
+	const size_t len = strnlen(val, kps->maxlen);
 
-	if (strnlen(val, kps->maxlen) == kps->maxlen) {
+	if (len == kps->maxlen) {
 		pr_err("%s: string doesn't fit in %u chars.\n",
 		       kp->name, kps->maxlen-1);
 		return -ENOSPC;
 	}
-	strcpy(kps->string, val);
+	memcpy(kps->string, val, len + 1);
 	return 0;
 }
 EXPORT_SYMBOL(param_set_copystring);
@@ -841,7 +842,7 @@ static void __init param_sysfs_builtin(void)
 		dot = strchr(kp->name, '.');
 		if (!dot) {
 			/* This happens for core_param() */
-			strcpy(modname, "kernel");
+			strscpy(modname, "kernel");
 			name_len = 0;
 		} else {
 			name_len = dot - kp->name + 1;
Signature · 1 line
2.50.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 38 lines · Thorsten Blum

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

In param_set_copystring(), we can safely use memcpy() because we already know the length of the source string 'val' and that it is guaranteed to be NUL-terminated within the first 'kps->maxlen' bytes.

---
Changes in v2:
- Use memcpy() in param_set_copystring() as suggested by Petr Pavlu
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 kernel/params.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/params.c +3 -2
index b92d64161b75..b96cfd693c99 100644
@@ -513,13 +513,14 @@ EXPORT_SYMBOL(param_array_ops);
 int param_set_copystring(const char *val, const struct kernel_param *kp)
 {
 	const struct kparam_string *kps = kp->str;
+	const size_t len = strnlen(val, kps->maxlen);
 
-	if (strnlen(val, kps->maxlen) == kps->maxlen) {
+	if (len == kps->maxlen) {
 		pr_err("%s: string doesn't fit in %u chars.\n",
 		       kp->name, kps->maxlen-1);
 		return -ENOSPC;
 	}
-	strcpy(kps->string, val);
+	memcpy(kps->string, val, len + 1);
 	return 0;
 }
 EXPORT_SYMBOL(param_set_copystring);

kernel/params.c static void __init param_sysfs_builtin(void) · line 848

Quoted 8 lines
kernel/params.c +1 -1
@@ -841,7 +842,7 @@ static void __init param_sysfs_builtin(void)
 		dot = strchr(kp->name, '.');
 		if (!dot) {
 			/* This happens for core_param() */
-			strcpy(modname, "kernel");
+			strscpy(modname, "kernel");
 			name_len = 0;
 		} else {
 			name_len = dot - kp->name + 1;
Signature · 2 lines
Thanks,
Petr
Reviewed-by
#2 lore raw
Quoted 41 lines · Petr Pavlu
Quoted 40 lines · Thorsten Blum

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

In param_set_copystring(), we can safely use memcpy() because we already know the length of the source string 'val' and that it is guaranteed to be NUL-terminated within the first 'kps->maxlen' bytes.

---
Changes in v2:
- Use memcpy() in param_set_copystring() as suggested by Petr Pavlu
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 kernel/params.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/params.c +3 -2
index b92d64161b75..b96cfd693c99 100644
@@ -513,13 +513,14 @@ EXPORT_SYMBOL(param_array_ops);
 int param_set_copystring(const char *val, const struct kernel_param *kp)
 {
 	const struct kparam_string *kps = kp->str;
+	const size_t len = strnlen(val, kps->maxlen);
 
-	if (strnlen(val, kps->maxlen) == kps->maxlen) {
+	if (len == kps->maxlen) {
 		pr_err("%s: string doesn't fit in %u chars.\n",
 		       kp->name, kps->maxlen-1);
 		return -ENOSPC;
 	}
-	strcpy(kps->string, val);
+	memcpy(kps->string, val, len + 1);
 	return 0;
 }
 EXPORT_SYMBOL(param_set_copystring);
@@ -841,7 +842,7 @@ static void __init param_sysfs_builtin(void)
 		dot = strchr(kp->name, '.');
 		if (!dot) {
Quoted 9 lines

kernel/params.c static void __init param_sysfs_builtin(void) · line 848

Quoted 6 lines
kernel/params.c +1 -1
 			/* This happens for core_param() */
-			strcpy(modname, "kernel");
+			strscpy(modname, "kernel");
 			name_len = 0;
 		} else {
 			name_len = dot - kp->name + 1;
Applied
#3 lore raw
In reply to Thorsten Blum
Quoted 8 lines · Thorsten Blum

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

In param_set_copystring(), we can safely use memcpy() because we already know the length of the source string 'val' and that it is guaranteed to be NUL-terminated within the first 'kps->maxlen' bytes.

[...]

Applied, thanks!

[1/1] params: Replace deprecated strcpy() with strscpy() and memcpy()

      commit: 5eb4b9a4cdbb70d70377fe8fb2920b75910e5024

Best regards,

Signature · 1 line
Daniel Gomez <[email protected]>