Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Thorsten Blum and committed by
Steve French
dc81b8f4 c4a2a49f

+10 -5
+10 -5
fs/smb/server/misc.c
··· 164 164 { 165 165 char *pathname, *ab_pathname, *nt_pathname; 166 166 int share_path_len = share->path_sz; 167 + size_t ab_pathname_len; 168 + int prefix; 167 169 168 170 pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP); 169 171 if (!pathname) ··· 182 180 goto free_pathname; 183 181 } 184 182 185 - nt_pathname = kzalloc(strlen(&ab_pathname[share_path_len]) + 2, 186 - KSMBD_DEFAULT_GFP); 183 + ab_pathname_len = strlen(&ab_pathname[share_path_len]); 184 + prefix = ab_pathname[share_path_len] == '\0' ? 1 : 0; 185 + nt_pathname = kmalloc(prefix + ab_pathname_len + 1, KSMBD_DEFAULT_GFP); 187 186 if (!nt_pathname) { 188 187 nt_pathname = ERR_PTR(-ENOMEM); 189 188 goto free_pathname; 190 189 } 191 - if (ab_pathname[share_path_len] == '\0') 192 - strcpy(nt_pathname, "/"); 193 - strcat(nt_pathname, &ab_pathname[share_path_len]); 190 + 191 + if (prefix) 192 + *nt_pathname = '/'; 193 + memcpy(nt_pathname + prefix, &ab_pathname[share_path_len], 194 + ab_pathname_len + 1); 194 195 195 196 ksmbd_conv_path_to_windows(nt_pathname); 196 197