Skip to contentSkip to conversation

vfat: don't read garbage after last dirent

PATCH Superseded Matteo Croce 2 participants 3 messages Dec 7, 2018 – Dec 14, 2018 VFAT/FAT/msdos filesystem

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfat: don't read garbage after last dirent

The FAT32 File System Specification[1] states:

If DIR_Name[0] == 0x00, then the directory entry is free, and there are no allocated directory entries after this one.

The special 0 value, indicates to FAT file system driver code that the rest of the entries in this directory do notneed to be examined because they are all free.

This is not enforced by Linux, and is possible to read garbage if not all the dirents after the last one are filled with zeroes.

[1] http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/fatgen103.doc

---
 fs/fat/dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/fat/dir.c +1 -1
index c8366cb8eccd..0cec89271349 100644
@@ -588,7 +588,7 @@ static int __fat_readdir(struct inode *inode, struct file *file,
 
 	bh = NULL;
 get_new:
-	if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
+	if (fat_get_entry(inode, &cpos, &bh, &de) == -1 || !de->name[0])
 		goto end_of_dir;
 parse_record:
 	nr_slots = 0;
Signature · 1 line
2.19.2
#1 lore raw
Quoted 1 line · Matteo Croce

the rest of the entries in this directory do notneed to be examined

I made a small typo, it should be "do not need"

Regards,

Signature · 2 lines
Matteo Croce
per aspera ad upstream
maintainer
#2 lore raw
In reply to Matteo Croce
Quoted 9 lines · Matteo Croce

The FAT32 File System Specification[1] states:

If DIR_Name[0] == 0x00, then the directory entry is free, and there are no allocated directory entries after this one.

The special 0 value, indicates to FAT file system driver code that the rest of the entries in this directory do notneed to be examined because they are all free.

Quoted 7 lines

This is not enforced by Linux, and is possible to read garbage if not all the dirents after the last one are filled with zeroes.

[1] http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/fatgen103.doc

I know this spec. But name[0] == 0 means - the rest of the entries in this directory must be 0. Otherwise, overwriting zeroed entry by adding new dir entry, then shows garbage as dir entry. So "stop at zero" is just a optimization.

On other hand, I know there is buggy formatters don't clear that garbage, and uses it on read-only storage area.

So I will agree to supporting "stop at zero" though (and keep assuming dir is initialized with zero clear. i.e. don't add tricky workaround to support buggy formatters.), the implementation should handle it for whole path, not only readdir().

E.g. I recall lookup, dir empty check path, and maybe there are others I can't recall immediately.

fs/fat/dir.c static int __fat_readdir(struct inode *inode, struct file *file, · line 594

Quoted 6 lines

get_new:

fs/fat/dir.c +1 -1
-	if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
+	if (fat_get_entry(inode, &cpos, &bh, &de) == -1 || !de->name[0])
 		goto end_of_dir;
 parse_record:
 	nr_slots = 0;

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>