[PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()

Thorsten Blum posted 1 patch 1 month, 3 weeks ago
kernel/params.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
[PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()
Posted by Thorsten Blum 1 month, 3 weeks ago
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.

Link: https://github.com/KSPP/linux/issues/88
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
Changes in v2:
- Use memcpy() in param_set_copystring() as suggested by Petr Pavlu
- Link to v1: https://lore.kernel.org/lkml/20250810214456.2236-1-thorsten.blum@linux.dev/
---
 kernel/params.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/kernel/params.c b/kernel/params.c
index b92d64161b75..b96cfd693c99 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -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;
-- 
2.50.1
Re: [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()
Posted by Daniel Gomez 1 month, 2 weeks ago
On Wed, 13 Aug 2025 15:21:59 +0200, Thorsten Blum wrote:
> 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,
-- 
Daniel Gomez <da.gomez@samsung.com>
Re: [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()
Posted by Petr Pavlu 1 month, 3 weeks ago
On 8/13/25 3:21 PM, Thorsten Blum wrote:
> 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.
> 
> Link: https://github.com/KSPP/linux/issues/88
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> Changes in v2:
> - Use memcpy() in param_set_copystring() as suggested by Petr Pavlu
> - Link to v1: https://lore.kernel.org/lkml/20250810214456.2236-1-thorsten.blum@linux.dev/
> ---
>  kernel/params.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/params.c b/kernel/params.c
> index b92d64161b75..b96cfd693c99 100644
> --- a/kernel/params.c
> +++ b/kernel/params.c
> @@ -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;

Reviewed-by: Petr Pavlu <petr.pavlu@suse.com>

-- 
Thanks,
Petr
Re: [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()
Posted by Daniel Gomez 1 month, 2 weeks ago
On 13/08/2025 07.14, Petr Pavlu wrote:
> On 8/13/25 3:21 PM, Thorsten Blum wrote:
>> 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.
>>
>> Link: https://github.com/KSPP/linux/issues/88
>> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
>> ---
>> Changes in v2:
>> - Use memcpy() in param_set_copystring() as suggested by Petr Pavlu
>> - Link to v1: https://lore.kernel.org/lkml/20250810214456.2236-1-thorsten.blum@linux.dev/
>> ---
>>  kernel/params.c | 7 ++++---
>>  1 file changed, 4 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel/params.c b/kernel/params.c
>> index b92d64161b75..b96cfd693c99 100644
>> --- a/kernel/params.c
>> +++ b/kernel/params.c
>> @@ -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;
> 
> Reviewed-by: Petr Pavlu <petr.pavlu@suse.com>
> 

Reviewed-by: Daniel Gomez <da.gomez@samsung.com>