volume_id/fat: prevent possible division by zero and crash

function                                             old     new   delta
volume_id_probe_vfat                                 830     841     +11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko committed 2026-11-08 18:25:22 +01:00
1 parent d68f55fc29
commit b7c89c493a
1 file changed
+46 -45
+46 -45
View File
@@ -39,41 +39,43 @@
struct vfat_super_block {
uint8_t boot_jump[3];
uint8_t sysid[8];
uint16_t sector_size_bytes;
uint8_t sectors_per_cluster;
uint16_t reserved_sct;
uint8_t fats;
uint16_t dir_entries;
uint16_t sectors;
uint8_t media;
uint16_t fat_length;
uint16_t secs_track;
uint16_t heads;
uint32_t hidden;
uint32_t total_sect;
uint8_t sysid[8]; /* 003 name - can be used to special case partition manager volumes */
uint16_t sector_size_bytes; /* 00b bytes per logical sector */
uint8_t sectors_per_cluster; /* 00d sectors/cluster */
uint16_t reserved_sct; /* 00e reserved sectors (sector offset of 1st FAT relative to volume start) */
uint8_t fats; /* 010 number of FATs */
uint16_t dir_entries; /* 011 root directory entries */
uint16_t sectors; /* 013 volume size in sectors */
uint8_t media; /* 015 media code */
uint16_t fat_length; /* 016 sectors/FAT, must be 0 for FAT32 */
uint16_t secs_track; /* 018 sectors per track */
uint16_t heads; /* 01a number of heads */
uint32_t hidden; /* 01c hidden sectors (sector offset of volume within physical disk) */
uint32_t total_sect; /* 020 volume size in sectors (if sectors == 0 at offset 013) */
union {
struct fat_super_block {
uint8_t unknown[3];
uint8_t serno[4];
uint8_t label[11];
uint8_t magic[8];
uint8_t unknown[3]; /* 024 drive number,flags,ext_boot_sign(0x28/0x29)*/
uint8_t serno[4]; /* 027 volume serial number */
uint8_t label[11]; /* 02b volume label (exists if ext_boot_sign=0x29) */
uint8_t magic[8]; /* 036 typically "FATnn" (exists if ext_boot_sign=0x29) */
uint8_t dummy2[192];
uint8_t pmagic[2];
} PACKED fat;
struct fat32_super_block {
uint32_t fat32_length;
uint16_t flags;
uint8_t version[2];
uint32_t root_cluster;
uint16_t insfo_sector;
uint16_t backup_boot;
uint16_t reserved2[6];
uint8_t unknown[3];
uint8_t serno[4];
uint8_t label[11];
uint8_t magic[8];
uint8_t dummy2[164];
uint32_t fat32_length; /* 024 sectors/FAT */
uint16_t flags; /* 028 bit 8: fat mirroring, low 4: active fat */
uint8_t version[2]; /* 02a major, minor filesystem version (I see 0,0) */
uint32_t root_cluster; /* 02c first cluster in root directory */
uint16_t insfo_sector; /* 030 filesystem info sector (usually 1) */
uint16_t backup_boot; /* 032 backup boot sector (usually 6) */
uint16_t reserved2[6]; /* 034 unused */
uint8_t drive_number; /* 040 BIOS drive number */
uint8_t reserved3; /* 041 unused */
uint8_t ext_boot_sign; /* 042 0x29 if fields below exist (DOS 3.3+) */
uint8_t serno[4]; /* 043 volume serial number */
uint8_t label[11]; /* 047 volume label */
uint8_t magic[8]; /* 052 typically "FATnn" */
uint8_t dummy2[164]; /* 05a */
uint8_t pmagic[2];
} PACKED fat32;
} PACKED type;
@@ -152,24 +154,27 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
if (vs == NULL)
return -1;
/* cluster size check */
if (vs->sectors_per_cluster == 0
|| (vs->sectors_per_cluster & (vs->sectors_per_cluster-1))
) {
return -1;
}
/* ^^^ ensuring nonzero _before_ "goto valid"s below, or else DIV/0 later */
/* believe only that's fat, don't trust the version
* the cluster_count will tell us
*/
if (memcmp(vs->sysid, "NTFS", 4) == 0)
return -1;
if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
goto valid;
if (memcmp(vs->type.fat32.magic, "FAT32 ", 8) == 0)
goto valid;
if (memcmp(vs->type.fat.magic, "FAT16 ", 8) == 0)
goto valid;
if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
goto valid;
if (memcmp(vs->type.fat.magic, "FAT12 ", 8) == 0)
goto valid;
@@ -184,23 +189,15 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
) {
return -1;
}
//TODO: the above check is bogus, FAT partitions are not required to contain boot code
/* heads check */
if (vs->heads == 0)
return -1;
/* cluster size check */
if (vs->sectors_per_cluster == 0
|| (vs->sectors_per_cluster & (vs->sectors_per_cluster-1))
) {
return -1;
}
//TODO: the heads value is meaningless (and unused) on most modern storage
/* media check */
if (vs->media < 0xf8 && vs->media != 0xf0)
return -1;
/* fat count*/
/* fat count */
if (vs->fats != 2)
return -1;
@@ -227,6 +224,7 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
fat_size_sct = le16_to_cpu(vs->fat_length);
if (fat_size_sct == 0)
fat_size_sct = le32_to_cpu(vs->type.fat32.fat32_length);
//TODO: zero here means it's not a FAT volume (FAT table can't have zero sectors). > 0x7fffffff bogus too
fat_size_sct *= vs->fats;
dbg("fat_size_sct 0x%x", fat_size_sct);
@@ -250,6 +248,9 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
if (cluster_count > FAT16_MAX)
goto fat32;
//TODO: check "dir_entries != 0"? It can't be zero for FAT12/16
//TODO: check "vs->fat_length != 0"? FAT12/16 must use this 16-bit field for FAT sector count, only FAT32 can fall back to vs->type.fat32.fat32_length
/* the label may be an attribute in the root directory */
root_start_off = (reserved_sct + fat_size_sct) * sector_size_bytes;
dbg("root dir start 0x%llx", (unsigned long long) root_start_off);