[PATCH] libblkid: Erroneous detection of FAT partition

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I recently came across a problem with an ext2 grub boot partition that wasn't 
being recognised by blkid.  Turns out that libblkid was detecting both FAT 
and ext2 file systems.  The partition was originally formatted as FAT (by 
WinNT) and then reformatted as ext2 by gparted-livecd-0.3.4-11.

The problem is with shlibs/blkid/src/probers/vfat.c.  At the end of this file 
the struct blkid_idinfo is declared with some magic search strings.  In 
particular, 2 patterns are defined to match the single byte jmp/jxx opcodes 
at the start of a DOS boot sector.  Because the partition was once formatted 
as a bootable DOS partition it matches these patterns and consequently the 
function probe_vfat is called.  For these simple pattern matches the function 
probe_fat_nomagic is called to filter out false positives.  In normal 
circumstances only MSDOS 2 and earlier floppies should be detected by this 
function.  A very important feature of these disks is the boot signature - 
bytes 0x55, 0xaa at the end of the 1st sector which indicate to the BIOS that 
the disk is bootable.  All MSDOS floppies have these bytes but the function 
probe_fat_nomagic doesn't check for them.

When I used gparted to reformat this DOS partition to ext2 it overwrote the 
MSDOS system name (offset 3), the FAT16 magic signature (offset 0x36) and the 
boot signature at offset 0x1fe but left the initial jmp opcode and BPB 
intact.  This causes the simple pattern match and BPB check to falsely detect 
a FAT filesystem.

To fix this I wrote this small patch to check for the 0x55, 0xaa bytes at the 
end of the sector:

--- util-linux-ng-2.16.1/shlibs/blkid/src/probers/vfat.c	2009-08-21 
15:59:30.000000000 +0200
+++ util-linux-ng-2.16.1-new/shlibs/blkid/src/probers/vfat.c	2009-09-30 
14:12:40.000000000 +0200
@@ -58,14 +58,16 @@ struct msdos_super_block {
 /* 0e*/	uint16_t	ms_reserved;
 /* 10*/	uint8_t		ms_fats;
 /* 11*/	unsigned char	ms_dir_entries[2];
-/* 13*/	unsigned char	ms_sectors[2];
+/* 13*/	unsigned char	ms_sectors[2]; /* =0 iff V3 or later */
 /* 15*/	unsigned char	ms_media;
-/* 16*/	uint16_t	ms_fat_length;
+/* 16*/	uint16_t	ms_fat_length; /* Sectors per FAT */
 /* 18*/	uint16_t	ms_secs_track;
 /* 1a*/	uint16_t	ms_heads;
 /* 1c*/	uint32_t	ms_hidden;
-/* 20*/	uint32_t	ms_total_sect;
-/* 24*/	unsigned char	ms_unknown[3];
+/* V3 BPB */
+/* 20*/	uint32_t	ms_total_sect; /* iff ms_sectors == 0 */
+/* V4 BPB */
+/* 24*/	unsigned char	ms_unknown[3]; /* Phys drive no., resvd, V4 sig (0x29) 
*/
 /* 27*/	unsigned char	ms_serno[4];
 /* 2b*/	unsigned char	ms_label[11];
 /* 36*/	unsigned char   ms_magic[8];
@@ -143,6 +145,10 @@ static int probe_fat_nomagic(blkid_probe
 	if (!ms)
 		return -1;
 
+	/* Old floppies have a valid MBR signature */
+	if (ms->ms_pmagic[0] != 0x55 || ms->ms_pmagic[1] != 0xAA)
+		return 1;
+
 	/* heads check */
 	if (ms->ms_heads == 0)
 		return 1;

Signed-off-by: Lawrence Rust

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux