public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Thorsten Blum <thorsten.blum@linux.dev>
Cc: Namjae Jeon <linkinjeon@kernel.org>,
	Steve French <smfrench@gmail.com>,
	Sergey Senozhatsky <senozhatsky@chromium.org>,
	Tom Talpey <tom@talpey.com>,
	linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] ksmbd: Replace strcpy + strcat with scnprintf in convert_to_nt_pathname
Date: Tue, 18 Nov 2025 22:35:13 +0000	[thread overview]
Message-ID: <20251118223513.241aed65@pumpkin> (raw)
In-Reply-To: <20251118122555.75624-2-thorsten.blum@linux.dev>

On Tue, 18 Nov 2025 13:25:56 +0100
Thorsten Blum <thorsten.blum@linux.dev> wrote:

> strcpy() is deprecated and using strcat() is discouraged; use the safer
> scnprintf() instead.  No functional changes.
> 
> Link: https://github.com/KSPP/linux/issues/88
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  fs/smb/server/misc.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/smb/server/misc.c b/fs/smb/server/misc.c
> index cb2a11ffb23f..86411f947989 100644
> --- a/fs/smb/server/misc.c
> +++ b/fs/smb/server/misc.c
> @@ -164,6 +164,7 @@ char *convert_to_nt_pathname(struct ksmbd_share_config *share,
>  {
>  	char *pathname, *ab_pathname, *nt_pathname;
>  	int share_path_len = share->path_sz;
> +	size_t nt_pathname_len;
>  
>  	pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
>  	if (!pathname)
> @@ -180,15 +181,15 @@ char *convert_to_nt_pathname(struct ksmbd_share_config *share,
>  		goto free_pathname;
>  	}
>  
> -	nt_pathname = kzalloc(strlen(&ab_pathname[share_path_len]) + 2,
> -			      KSMBD_DEFAULT_GFP);
> +	nt_pathname_len = strlen(&ab_pathname[share_path_len]) + 2;
> +	nt_pathname = kzalloc(nt_pathname_len, KSMBD_DEFAULT_GFP);
>  	if (!nt_pathname) {
>  		nt_pathname = ERR_PTR(-ENOMEM);
>  		goto free_pathname;
>  	}
> -	if (ab_pathname[share_path_len] == '\0')
> -		strcpy(nt_pathname, "/");
> -	strcat(nt_pathname, &ab_pathname[share_path_len]);
> +	scnprintf(nt_pathname, nt_pathname_len,
> +		  ab_pathname[share_path_len] == '\0' ? "/%s" : "%s",
> +		  &ab_pathname[share_path_len]);

Ugg...
If nothing else non-constant formats are definitely frowned upon.
Never mind the non-trivial cpu cost of printf.

OTOH once you've got the string length, just use memcpy().
That way you know you won't overflow the malloc buffer even
if someone changes the string on you.

	David



>  
>  	ksmbd_conv_path_to_windows(nt_pathname);
>  


  reply	other threads:[~2025-11-18 22:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-18 12:25 [PATCH] ksmbd: Replace strcpy + strcat with scnprintf in convert_to_nt_pathname Thorsten Blum
2025-11-18 22:35 ` David Laight [this message]
2025-11-19  1:23   ` Thorsten Blum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251118223513.241aed65@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=senozhatsky@chromium.org \
    --cc=smfrench@gmail.com \
    --cc=thorsten.blum@linux.dev \
    --cc=tom@talpey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox