All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fat: check whether fs size exceeds device size
@ 2016-06-24  8:58 Zheng Lv
  2016-06-24  9:34 ` OGAWA Hirofumi
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Zheng Lv @ 2016-06-24  8:58 UTC (permalink / raw)
  To: hirofumi; +Cc: linux-fsdevel

The original code did not check for size of device. A truncated device
would mount, I/O failures would occur when "attempt to access beyond end
of device", leading to data lost.

Fix this by comparing total-sectors field in BPB with size of device.

This commit also prints a KERN_INFO message if there are extra sectors
at end of device (ie. total sectors < device sectors).

Signed-off-by: Zheng Lv <lv.zheng.2015@gmail.com>
---
 fs/fat/inode.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/fs/fat/inode.c b/fs/fat/inode.c
index 3bcf579..211f7bb 100644
--- a/fs/fat/inode.c
+++ b/fs/fat/inode.c
@@ -1583,6 +1583,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
 	struct msdos_sb_info *sbi;
 	u16 logical_sector_size;
 	u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
+	u64 device_sectors;
 	int debug;
 	long error;
 	char buf[50];
@@ -1738,6 +1739,17 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
 	if (total_sectors == 0)
 		total_sectors = bpb.fat_total_sect;
 
+	device_sectors = sb->s_bdev->bd_inode->i_size / logical_sector_size;
+	if (device_sectors && total_sectors > device_sectors) {
+		fat_msg(sb, KERN_ERR, "total sectors %u "
+			"exceeds size of device (%llu sectors)",
+			total_sectors, device_sectors);
+		goto out_invalid;
+	} else if (device_sectors && total_sectors < device_sectors) {
+		fat_msg(sb, KERN_INFO, "%llu unused sectors at end of device",
+			device_sectors - total_sectors);
+	}
+
 	total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
 
 	if (sbi->fat_bits != 32)
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-06-25 10:08 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-24  8:58 [PATCH] fat: check whether fs size exceeds device size Zheng Lv
2016-06-24  9:34 ` OGAWA Hirofumi
2016-06-24 13:56   ` Zheng Lv
2016-06-25 10:08     ` OGAWA Hirofumi
2016-06-24 14:02   ` Zheng Lv
2016-06-24 15:10     ` Zheng Lv
2016-06-24  9:38 ` kbuild test robot
2016-06-24 10:39 ` kbuild test robot
2016-06-24 10:53 ` kbuild test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.