Skip to content

Commit dc81b8f

Browse files
tobluxsmfrench
authored andcommitted
ksmbd: Replace strcpy + strcat to improve convert_to_nt_pathname
strcpy() is deprecated [1] and using strcat() is discouraged. Replace them by assigning the prefix directly and by using memcpy() to copy the pathname. Using memcpy() is safe because we already know the length of the source string and that it is guaranteed to be NUL-terminated. Allocate only as many bytes as needed and replace kzalloc() with kmalloc() since memcpy() overwrites the entire buffer anyway. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1] Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Acked-by: Namjae Jeon <linkinjeon@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent c4a2a49 commit dc81b8f

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

fs/smb/server/misc.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ char *convert_to_nt_pathname(struct ksmbd_share_config *share,
164164
{
165165
char *pathname, *ab_pathname, *nt_pathname;
166166
int share_path_len = share->path_sz;
167+
size_t ab_pathname_len;
168+
int prefix;
167169

168170
pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
169171
if (!pathname)
@@ -180,15 +182,18 @@ char *convert_to_nt_pathname(struct ksmbd_share_config *share,
180182
goto free_pathname;
181183
}
182184

183-
nt_pathname = kzalloc(strlen(&ab_pathname[share_path_len]) + 2,
184-
KSMBD_DEFAULT_GFP);
185+
ab_pathname_len = strlen(&ab_pathname[share_path_len]);
186+
prefix = ab_pathname[share_path_len] == '\0' ? 1 : 0;
187+
nt_pathname = kmalloc(prefix + ab_pathname_len + 1, KSMBD_DEFAULT_GFP);
185188
if (!nt_pathname) {
186189
nt_pathname = ERR_PTR(-ENOMEM);
187190
goto free_pathname;
188191
}
189-
if (ab_pathname[share_path_len] == '\0')
190-
strcpy(nt_pathname, "/");
191-
strcat(nt_pathname, &ab_pathname[share_path_len]);
192+
193+
if (prefix)
194+
*nt_pathname = '/';
195+
memcpy(nt_pathname + prefix, &ab_pathname[share_path_len],
196+
ab_pathname_len + 1);
192197

193198
ksmbd_conv_path_to_windows(nt_pathname);
194199

0 commit comments

Comments
 (0)