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 <[email protected]>
> ---
> 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(-)
> 
> 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 <[email protected]>

-- 
Thanks,
Petr

Reply via email to