fs: fat: Prevent possible buffer overflow
PATCH 1/1 fs: fat: Prevent possible buffer overflow
Instead of strcpy which is unbounded use strlcpy to ensure that the receiving buffer cannot be overflowed.
This issue found by Smatch.
--- fs/fat/fat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/fat/fat.c +1 -1
@@ -1391,7 +1391,7 @@ int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)return -ENOENT ;memset (dent,0 ,sizeof (*dent)); -strcpy (dent->name, dir->itr.name); +strlcpy (dent->name, dir->itr.name,FS_DIRENT_NAME_LEN );if (CONFIG_IS_ENABLED (EFI_LOADER )) { dent->attr = dir->itr.dent->attr; fat2rtc(le16_to_cpu(dir->itr.dent->cdate),
---
base-commit: 3b4604a40b9fd61b87e9d059fc56f04d36f1a380
change-id: 20250717-fsfat-a77ce8151f74
Best regards,
Signature · 1 line
Andrew Goodbody <[email protected]>
fs/fat/fat.c int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp) · line 1394
Quoted 4 lines · Andrew Goodbody
fs/fat/fat.c +1
-1memset (dent,0 ,sizeof (*dent)); -strcpy (dent->name, dir->itr.name); +strlcpy (dent->name, dir->itr.name,FS_DIRENT_NAME_LEN );
Shouldn't that be strncpy() instead? Using strlcpy() for fixed size records where strings may not be \0 terminated is quirky.
Martin
Quoted 10 lines · Martin Husemann
fs/fat/fat.c int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp) · line 1394
Quoted 4 lines · Andrew Goodbody
memset(dent, 0, sizeof(*dent));fs/fat/fat.c +1
-1-strcpy (dent->name, dir->itr.name); +strlcpy (dent->name, dir->itr.name,FS_DIRENT_NAME_LEN );Shouldn't that be
strncpy()instead? Usingstrlcpy()for fixed size records where strings may not be \0 terminated is quirky.Martin
Hi Martin,
The original use of strcpy suggests that the string must be \0 terminated. I will admit that I do not know the code well, is dir->itr.name guaranteed to be a known fixed size?
Andrew
Quoted 3 lines · Andrew Goodbody
The original use of strcpy suggests that the string must be \0 terminated. I will admit that I do not know the code well, is
dir->itr.nameguaranteed to be a known fixed size?
You are right, the iterator's name pointer always points to a \0 terminated string, but also always points to one shorter than FS_DIRENT_NAME_LEN.
The on-disk fields are not \0 terminated, but the code extracting them deals with it properly. If the length would overflow, a NULL instead of a dir_entry * is returned, so while it is not obvious, AFAICT the original code is fine.
Martin
Quoted 8 lines · Martin Husemann
Quoted 3 lines · Andrew Goodbody
The original use of strcpy suggests that the string must be \0 terminated. I will admit that I do not know the code well, is
dir->itr.nameguaranteed to be a known fixed size?You are right, the iterator's name pointer always points to a \0 terminated string, but also always points to one shorter than
FS_DIRENT_NAME_LEN.
Quoted 6 lines
The on-disk fields are not \0 terminated, but the code extracting them deals with it properly. If the length would overflow, a NULL instead of a
dir_entry* is returned, so while it is not obvious, AFAICT the original code is fine.Martin
Thanks Martin, we can drop this patch.
Andrew