ksmbd: Replace strcpy + strcat with scnprintf in convert_to_nt_pathname
Subsystems (2)
PATCH 1/1 ksmbd: Replace strcpy + strcat with scnprintf in convert_to_nt_pathname
strcpy() is deprecated and using strcat() is discouraged; use the safer scnprintf() instead. No functional changes.
--- fs/smb/server/misc.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
fs/smb/server/misc.c +6 -5
@@ -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]);ksmbd_conv_path_to_windows (nt_pathname);
Signature · 1 line
2.51.1
Quoted 29 lines · On Tue, 18 Nov 2025 13:25:56 +0100 Thorsten Blum <[email protected]>…
strcpy()is deprecated and usingstrcat()is discouraged; use the saferscnprintf()instead. No functional changes.Signed-off-by Thorsten Blum <[email protected]>--- fs/smb/server/misc.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)fs/smb/server/misc.c +3
-2@@ -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 );
fs/smb/server/misc.c char *convert_to_nt_pathname(struct ksmbd_share_config *share, · line 192
Quoted 10 lines
fs/smb/server/misc.c +3
-3if (!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
Quoted 3 lines
fs/smb/server/misc.c +0
-0ksmbd_conv_path_to_windows (nt_pathname);
Quoted 11 lines · David Laight
Quoted 7 lines · On Tue, 18 Nov 2025 13:25:56 +0100 Thorsten Blum <[email protected]>…
strcpy()is deprecated and usingstrcat()is discouraged; use the saferscnprintf()instead. No functional changes.Signed-off-by Thorsten Blum <[email protected]>---
[...]
Quoted 7 lines
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.
Ok, I'll submit a v2.
Thanks,
Thorsten