linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()
@ 2025-08-13 13:21 Thorsten Blum
  2025-08-13 14:14 ` Petr Pavlu
  2025-08-16 19:51 ` Daniel Gomez
  0 siblings, 2 replies; 4+ messages in thread
From: Thorsten Blum @ 2025-08-13 13:21 UTC (permalink / raw)
  To: Petr Pavlu, Thomas Weißschuh, Shyam Saini, Luis Chamberlain,
	Dmitry Antipov, Thorsten Blum
  Cc: linux-kernel

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


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

* Re: [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()
  2025-08-13 13:21 [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy() Thorsten Blum
@ 2025-08-13 14:14 ` Petr Pavlu
  2025-08-15 19:19   ` Daniel Gomez
  2025-08-16 19:51 ` Daniel Gomez
  1 sibling, 1 reply; 4+ messages in thread
From: Petr Pavlu @ 2025-08-13 14:14 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Thomas Weißschuh, Shyam Saini, Luis Chamberlain,
	Dmitry Antipov, linux-kernel, linux-modules, Daniel Gomez

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

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

* Re: [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()
  2025-08-13 14:14 ` Petr Pavlu
@ 2025-08-15 19:19   ` Daniel Gomez
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Gomez @ 2025-08-15 19:19 UTC (permalink / raw)
  To: Petr Pavlu, Thorsten Blum
  Cc: Thomas Weißschuh, Shyam Saini, Luis Chamberlain,
	Dmitry Antipov, linux-kernel, linux-modules, Kees Cook

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>


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

* Re: [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy()
  2025-08-13 13:21 [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy() Thorsten Blum
  2025-08-13 14:14 ` Petr Pavlu
@ 2025-08-16 19:51 ` Daniel Gomez
  1 sibling, 0 replies; 4+ messages in thread
From: Daniel Gomez @ 2025-08-16 19:51 UTC (permalink / raw)
  To: Petr Pavlu, Thomas Weißschuh, Shyam Saini, Luis Chamberlain,
	Dmitry Antipov, Thorsten Blum
  Cc: linux-kernel


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>


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

end of thread, other threads:[~2025-08-16 19:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-13 13:21 [PATCH v2] params: Replace deprecated strcpy() with strscpy() and memcpy() Thorsten Blum
2025-08-13 14:14 ` Petr Pavlu
2025-08-15 19:19   ` Daniel Gomez
2025-08-16 19:51 ` Daniel Gomez

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).