U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 1/1] fs: fat: validate sector and cluster size
@ 2018-10-31 20:46 Heinrich Schuchardt
  2018-11-20 14:56 ` [U-Boot] [U-Boot, " Tom Rini
  0 siblings, 1 reply; 6+ messages in thread
From: Heinrich Schuchardt @ 2018-10-31 20:46 UTC (permalink / raw)
  To: u-boot

In the rest of the FAT driver we want to be able to rely on the fact that
the cluster size is a power of two. So let's check that both the sector
size and the number of sectors per cluster are valid.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 fs/fat/fat.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index b08949d3705..0f69fa4df78 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -547,6 +547,39 @@ static int get_fs_info(fsdata *mydata)
 		return ret;
 	}
 
+	/* Validate sector and cluster size */
+	switch (bs.sector_size[1]) {
+	case 2:		/*  512 bytes per sector */
+	case 4:		/* 1024 bytes per sector */
+	case 8:		/* 2048 bytes per sector */
+	case 16:	/* 4096 bytes per sector */
+		break;
+	default:
+		ret =  -1;
+	}
+	if (ret || bs.sector_size[0]) {
+		debug("FAT: invalid sector size\n");
+		return -1;
+	}
+	switch (bs.cluster_size) {
+	case 1:
+	case 2:
+	case 4:
+	case 8:
+	case 16:
+	case 32:
+	case 64:
+	case 128:
+		break;
+	default:
+		ret = -1;
+	}
+	/* Check bytes per cluster <= 32768 */
+	if (ret || (u32)bs.sector_size[1] * (u32)bs.cluster_size > 128) {
+		debug("FAT: invalid cluster size\n");
+		return -1;
+	}
+
 	if (mydata->fatsize == 32) {
 		mydata->fatlength = bs.fat32_length;
 		mydata->total_sect = bs.total_sect;
@@ -564,7 +597,7 @@ static int get_fs_info(fsdata *mydata)
 
 	mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
 
-	mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
+	mydata->sect_size = bs.sector_size[1] << 8;
 	mydata->clust_size = bs.cluster_size;
 	if (mydata->sect_size != cur_part_info.blksz) {
 		printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
-- 
2.19.1

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

end of thread, other threads:[~2018-11-21  2:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-31 20:46 [U-Boot] [PATCH 1/1] fs: fat: validate sector and cluster size Heinrich Schuchardt
2018-11-20 14:56 ` [U-Boot] [U-Boot, " Tom Rini
2018-11-20 20:29   ` Heinrich Schuchardt
2018-11-20 20:33     ` Tom Rini
2018-11-20 22:08       ` Heinrich Schuchardt
2018-11-21  2:17         ` Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox