volume_id/fat: add validity checks for FAT12/16 (rootdir can't have 0 sectors)
function old new delta volume_id_probe_vfat 815 852 +37 mkfs_vfat_main 1523 1518 -5 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 37/-5) Total: 32 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
1 parent
484e887ce6
commit
0c1060dee1
3 files changed
+68
-42
No files matched your search
@@ -110,8 +110,8 @@ struct msdos_boot_sector {
|
||||
uint8_t fat32_version[2]; // 02a major, minor filesystem version (I see 0,0)
|
||||
uint32_t fat32_root_cluster; // 02c first cluster in root directory
|
||||
uint16_t fat32_info_sector; // 030 filesystem info sector (usually 1)
|
||||
uint16_t fat32_backup_boot; // 032 backup boot sector (usually 6)
|
||||
uint32_t reserved2[3]; // 034 'unused'. 12 bytes of 'boot file name'?
|
||||
uint16_t fat32_backup_boot; // 032 backup boot sector (usually 6, 0: none)
|
||||
uint8_t boot_file[12]; // 034 Boot file name (boot code may search root dir for this name)
|
||||
struct msdos_volume_info vi; // 040
|
||||
char boot_code[0x200 - 0x5a - 2]; // 05a
|
||||
#define BOOT_SIGN 0xAA55
|
||||
|
||||
@@ -152,8 +152,8 @@ struct msdos_boot_sector {
|
||||
uint8_t fat32_version[2]; /* 02a major, minor filesystem version (I see 0,0) */
|
||||
uint32_t fat32_root_cluster; /* 02c first cluster in root directory */
|
||||
uint16_t fat32_info_sector; /* 030 filesystem info sector (usually 1) */
|
||||
uint16_t fat32_backup_boot; /* 032 backup boot sector (usually 6) */
|
||||
uint32_t reserved2[3]; /* 034 unused */
|
||||
uint16_t fat32_backup_boot; /* 032 backup boot sector (usually 6, 0: none) */
|
||||
uint8_t boot_file[12]; /* 034 Boot file name (boot code may search root dir for this name) */
|
||||
struct msdos_volume_info vi; /* 040 */
|
||||
char boot_code[0x200 - 0x5a - 2]; /* 05a */
|
||||
#define BOOT_SIGN 0xAA55
|
||||
@@ -235,7 +235,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
|
||||
unsigned opts;
|
||||
uint16_t sect_per_track;
|
||||
uint8_t media_byte;
|
||||
uint8_t sect_per_clust;
|
||||
uint8_t sect_per_clust; // must be 8-bit (code expects that 256 won't fit)
|
||||
uint8_t heads;
|
||||
enum {
|
||||
OPT_A = 1 << 0, // [IGNORED] atari format
|
||||
@@ -443,9 +443,9 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
|
||||
sect_per_fat += ((unsigned)spf_adj / 2) | 1;
|
||||
}
|
||||
next:
|
||||
if (sect_per_clust == 128)
|
||||
bb_simple_error_msg_and_die("can't make FAT32 with >128 sectors/cluster");
|
||||
sect_per_clust *= 2;
|
||||
if (sect_per_clust == 0) // uint8_t overflowed: 128*2 = 0
|
||||
bb_simple_error_msg_and_die("can't make FAT32 with >128 sectors/cluster");
|
||||
sect_per_fat = (sect_per_fat / 2) | 1;
|
||||
}
|
||||
found_total_clust:
|
||||
|
||||
+61
-35
@@ -30,6 +30,17 @@
|
||||
#define FAT12_MAX 0xff4
|
||||
#define FAT16_MAX 0xfff4
|
||||
#define FAT32_MAX 0x0ffffff6
|
||||
// These are the maximum allowed counts of clusters, *not* the maximum cluster#!
|
||||
// Example: the cluster numbers on largest possible FAT12 fs are 2..FAT12_MAX+1
|
||||
// (clusters 0 and 1 do not exist).
|
||||
// General rules for cluster numbers in FAT table:
|
||||
// 0x0: is "free clister"
|
||||
// 0x1: reserved
|
||||
// from 0x2 to 0xF..F5 are valid cluster numbers (compare with the above constants).
|
||||
// 0xF..F6 reserved (but probably can be a valid cluster# on a large FAT32 volume?)
|
||||
// 0xF..F7 is "has bad sector(s)" (but probably can be a valid cluster# on a large FAT32 volume?)
|
||||
// from 0xF..F8 to 0xF..FF are "no next cluster in this chain".
|
||||
// FAT32 ignores upper 4 bits of 32-bit cluster number.
|
||||
|
||||
#define FAT_ATTR_VOLUME_ID 0x08
|
||||
#define FAT_ATTR_DIR 0x10
|
||||
@@ -44,14 +55,14 @@ struct vfat_super_block {
|
||||
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 volume_size_sect16; /* 013 volume size in sectors */
|
||||
uint16_t dir_entries; /* 011 root directory entries, must be 0 for FAT32 */
|
||||
uint16_t volume_size_sct16; /* 013 volume size in sectors, must be 0 for FAT32 */
|
||||
uint8_t media; /* 015 media code */
|
||||
uint16_t fat_length; /* 016 sectors/FAT, must be 0 for FAT32 */
|
||||
uint16_t fat_sct16; /* 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 volume_size_sect32; /* 020 volume size in sectors (if volume_size_sect16 == 0) */
|
||||
uint32_t volume_size_sct32; /* 020 volume size in sectors (if volume_size_sct16 == 0) */
|
||||
union {
|
||||
struct fat_super_block {
|
||||
uint8_t drive_number; /* 024 */
|
||||
@@ -64,13 +75,13 @@ struct vfat_super_block {
|
||||
uint8_t pmagic[2];
|
||||
} PACKED fat;
|
||||
struct fat32_super_block {
|
||||
uint32_t fat32_length; /* 024 sectors/FAT */
|
||||
uint32_t fat_sct32; /* 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 */
|
||||
uint16_t fsinfo_sct; /* 030 filesystem info sector (usually 1) */
|
||||
uint16_t backup_boot_sct; /* 032 backup boot sector (usually 6, 0: none) */
|
||||
uint8_t boot_file[12]; /* 034 Boot file name (boot code may search root dir for this name) */
|
||||
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+) */
|
||||
@@ -141,19 +152,18 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
|
||||
struct vfat_super_block *vs;
|
||||
struct vfat_dir_entry *dir;
|
||||
uint16_t sector_size_bytes;
|
||||
uint16_t dir_entries;
|
||||
uint32_t rootdir_entries; /* 16 bits really, but wider type works better */
|
||||
uint32_t rootdir_size_sct;
|
||||
uint32_t sect_count;
|
||||
uint32_t reserved_sct; /* 16 bits really, but wider type works better */
|
||||
uint32_t fat_size_sct;
|
||||
uint32_t root_cluster;
|
||||
uint32_t dir_size_sct;
|
||||
uint32_t cluster_count;
|
||||
uint64_t root_start_off;
|
||||
uint32_t after_fat_sct;
|
||||
uint32_t next_cluster;
|
||||
uint8_t *buf;
|
||||
uint32_t buf_size;
|
||||
uint8_t *label = NULL;
|
||||
uint32_t next_cluster;
|
||||
int maxloop;
|
||||
|
||||
dbg("probing at offset 0x%llx", (unsigned long long) fat_partition_off);
|
||||
@@ -219,28 +229,30 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
|
||||
if (reserved_sct == 0) /* FAT start sector can't be 0 */
|
||||
return -1;
|
||||
|
||||
fat_size_sct = le16_to_cpu(vs->fat_length); /* not unaligned */
|
||||
fat_size_sct = le16_to_cpu(vs->fat_sct16); /* not unaligned */
|
||||
if (fat_size_sct == 0)
|
||||
fat_size_sct = le32_to_cpu(vs->type.fat32.fat32_length); /* not unaligned */
|
||||
/* Zero here means it's not a FAT volume (FAT table can't have zero sectors). > 0x3fffffff bogus too */
|
||||
fat_size_sct = le32_to_cpu(vs->type.fat32.fat_sct32); /* not unaligned */
|
||||
/* Zero here means it's not a FAT volume (FAT table can't have zero sectors).
|
||||
* 0x40000000+ is bogus too: data area size is fat_size_sct*0x10000 (for 512-byte sectors and 1-sector clusters),
|
||||
* fat_size_sct=0x40000000 corresponds to at least 64TB filesystem, which needs 64-bit total sector count.
|
||||
*/
|
||||
if (fat_size_sct == 0 || (fat_size_sct & 0xC0000000u))
|
||||
return -1;
|
||||
fat_size_sct *= vs->fats;
|
||||
dbg("fat_size_sct 0x%x", fat_size_sct);
|
||||
|
||||
dir_entries = get_unaligned_le16(&vs->dir_entries);
|
||||
dir_size_sct = ((dir_entries * sizeof(struct vfat_dir_entry)) +
|
||||
(sector_size_bytes-1)) / sector_size_bytes;
|
||||
dbg("dir_size_sct 0x%x", dir_size_sct); /* zero on FAT32 */
|
||||
rootdir_entries = get_unaligned_le16(&vs->dir_entries);
|
||||
rootdir_size_sct = (rootdir_entries * sizeof(struct vfat_dir_entry) + (sector_size_bytes-1)) / sector_size_bytes;
|
||||
dbg("dir_size_sct 0x%x", rootdir_size_sct); /* zero on FAT32 */
|
||||
|
||||
sect_count = get_unaligned_le16(&vs->volume_size_sect16);
|
||||
sect_count = get_unaligned_le16(&vs->volume_size_sct16);
|
||||
if (sect_count == 0)
|
||||
sect_count = le32_to_cpu(vs->volume_size_sect32); /* not unaligned */
|
||||
sect_count = le32_to_cpu(vs->volume_size_sct32); /* not unaligned */
|
||||
dbg("sect_count 0x%x", sect_count);
|
||||
|
||||
after_fat_sct = reserved_sct + fat_size_sct;
|
||||
|
||||
cluster_count = sect_count - (after_fat_sct + dir_size_sct);
|
||||
cluster_count = sect_count - (after_fat_sct + rootdir_size_sct);
|
||||
if (cluster_count > sect_count) /* subtraction underflowed? */
|
||||
return -1; /* data area can't be missing */
|
||||
cluster_count /= vs->sectors_per_cluster;
|
||||
@@ -248,9 +260,12 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
|
||||
return -1; /* data area can't be missing */
|
||||
dbg("cluster_count 0x%x", cluster_count);
|
||||
|
||||
// if (cluster_count < FAT12_MAX) {
|
||||
// Microsoft insists that FAT type (12/16/32) is determined solely by cluster_count.
|
||||
// It's funny, because one of the steps to calculate it uses dir_entries (offset 0x11)
|
||||
// which _just by itself_ already determines whether FS is FAT12/16 or FAT32.
|
||||
// if (cluster_count <= FAT12_MAX) {
|
||||
// strcpy(id->type_version, "FAT12");
|
||||
// } else if (cluster_count < FAT16_MAX) {
|
||||
// } else if (cluster_count <= FAT16_MAX) {
|
||||
// strcpy(id->type_version, "FAT16");
|
||||
// } else {
|
||||
// strcpy(id->type_version, "FAT32");
|
||||
@@ -259,8 +274,10 @@ 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
|
||||
if (rootdir_entries == 0)
|
||||
return -1; /* can't be zero for FAT12/16 */
|
||||
if (vs->fat_sct16 == 0) /* not unaligned */
|
||||
return -1; /* FAT12/16 uses 16-bit field for FAT sector count, FAT32 uses vs->type.fat32.fat_sct32 */
|
||||
|
||||
/* String "NO NAME" has a special meaning (empty label) only for label stored in boot sector */
|
||||
if (memcmp(vs->type.fat.label, "NO NAME ", 11) != 0) {
|
||||
@@ -272,17 +289,26 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
|
||||
/* the label may be an attribute in the root directory */
|
||||
root_start_off = after_fat_sct * sector_size_bytes;
|
||||
dbg("root dir start 0x%llx", (unsigned long long) root_start_off);
|
||||
dbg("expected entries 0x%x", dir_entries);
|
||||
dbg("expected entries 0x%x", rootdir_entries);
|
||||
|
||||
buf_size = dir_entries * sizeof(struct vfat_dir_entry);
|
||||
buf_size = rootdir_entries * sizeof(struct vfat_dir_entry);
|
||||
buf = volume_id_get_buffer(id, fat_partition_off + root_start_off, buf_size);
|
||||
if (buf == NULL)
|
||||
goto ret;
|
||||
|
||||
label = get_attr_volume_id((struct vfat_dir_entry*) buf, dir_entries);
|
||||
label = get_attr_volume_id((struct vfat_dir_entry*) buf, rootdir_entries);
|
||||
goto ret;
|
||||
|
||||
fat32:
|
||||
// fatgen103.doc, Microsoft's FAT32 File System Specification version 1.03:
|
||||
// if (rootdir_entries != 0) // "For FAT32 volumes, this field must be set to 0"
|
||||
// return -1;
|
||||
// if (get_unaligned16(&vs->volume_size_sct16) != 0) // "On FAT32 volumes this field must be 0"
|
||||
// return -1;
|
||||
//^^^ can be combined as get_unaligned32(&vs->dir_entries), they are adjacent
|
||||
// if (vs->fat_sct16 != 0) // "On FAT32 volumes this field must be 0"
|
||||
// return -1;
|
||||
|
||||
/* String "NO NAME" has a special meaning (empty label) only for label stored in boot sector */
|
||||
if (memcmp(vs->type.fat32.label, "NO NAME ", 11) != 0) {
|
||||
// volume_id_set_label_raw(id, vs->type.fat32.label, 11);
|
||||
@@ -292,10 +318,8 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
|
||||
|
||||
/* FAT32 root dir is a cluster chain like any other directory */
|
||||
buf_size = vs->sectors_per_cluster * sector_size_bytes;
|
||||
root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
|
||||
|
||||
next_cluster = root_cluster;
|
||||
maxloop = 100;
|
||||
next_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
|
||||
maxloop = 100; /* in case rootdir cluster chain is in fact a loop */
|
||||
while (--maxloop) {
|
||||
uint64_t next_off_sct;
|
||||
uint64_t next_off;
|
||||
@@ -303,6 +327,10 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
|
||||
int count;
|
||||
|
||||
dbg("next_cluster 0x%x", (unsigned)next_cluster);
|
||||
if (next_cluster < 2 || next_cluster > FAT32_MAX)
|
||||
//FIXME: should FAT32_MAX be FAT32_MAX+1 instead?
|
||||
break;
|
||||
|
||||
next_off_sct = (uint64_t)(next_cluster - 2) * vs->sectors_per_cluster;
|
||||
next_off = (after_fat_sct + next_off_sct) * sector_size_bytes;
|
||||
dbg("cluster offset 0x%llx", (unsigned long long) next_off);
|
||||
@@ -330,8 +358,6 @@ int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partitio
|
||||
|
||||
/* set next cluster */
|
||||
next_cluster = le32_to_cpu(*(uint32_t*)buf) & 0x0fffffff;
|
||||
if (next_cluster < 2 || next_cluster > FAT32_MAX)
|
||||
break;
|
||||
}
|
||||
if (maxloop == 0)
|
||||
dbg("reached maximum follow count of root cluster chain, give up");
|
||||
|
||||
Reference in new issue
Block a user