From 3ef6a71ed186d1e8e44c26c24a1cbea5533e3554 Mon Sep 17 00:00:00 2001 From: Oliver Smith-Denny Date: Mon, 5 Aug 2024 14:54:27 -0700 Subject: FatPkg: Check BlockIo Device Has Supported BlockSize Per the FAT spec, FAT32 supports block sizes of 512B, 1KB, 2KB, or 4KB. This patch adds a check to the FAT driver initialization to ensure that the underlying BlockIo device supports one of those block sizes and fails initialization otherwise. The underlying BlockIo blocksize is used when we flush the FatDiskCache back to disk and if the block size is an unsupported size, we could cause file corruption. Signed-off-by: Oliver Smith-Denny --- FatPkg/EnhancedFatDxe/Init.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/FatPkg/EnhancedFatDxe/Init.c b/FatPkg/EnhancedFatDxe/Init.c index 3a946199e7..8563b950a4 100644 --- a/FatPkg/EnhancedFatDxe/Init.c +++ b/FatPkg/EnhancedFatDxe/Init.c @@ -61,6 +61,30 @@ FatAllocateVolume ( // Volume->RootDirEnt.FileString = Volume->RootFileString; Volume->RootDirEnt.Entry.Attributes = FAT_ATTRIBUTE_DIRECTORY; + + // + // Check to see if the underlying block device's BlockSize meets what the FAT spec requires + // + if ((BlockIo == NULL) || (BlockIo->Media == NULL)) { + DEBUG ((DEBUG_ERROR, "%a BlockIo or BlockIo is NULL!\n", __func__)); + Status = EFI_INVALID_PARAMETER; + goto Done; + } + + if ((BlockIo->Media->BlockSize > (1 << MAX_BLOCK_ALIGNMENT)) || + (BlockIo->Media->BlockSize < (1 << MIN_BLOCK_ALIGNMENT))) + { + Status = EFI_UNSUPPORTED; + DEBUG (( + DEBUG_ERROR, + "%a invalid BlockIo BlockSize %u for FAT filesystem on MediaId %u. Min 512b, max 4kb\n", + __func__, + BlockIo->Media->BlockSize, + BlockIo->Media->MediaId + )); + goto Done; + } + // // Check to see if there's a file system on the volume // -- cgit