[edk2-platforms][PATCH v1 09/12] Ext4Pkg: Correct integer overflow check on multiplication in DiskUtil


Pedro Falcato
 

On Fri, Dec 9, 2022 at 4:11 PM Savva Mitrofanov <savvamtr@...> wrote:
Multiplication overflow could result into small numbers, so we need also
check it

Can you fix the commit message? Overflow was (in theory) already getting checked, I just did a remarkably poor job open-coding it.
 
Pedro


Savva Mitrofanov
 

Multiplication overflow could result into small numbers, so we need also
check it

Cc: Marvin Häuser <mhaeuser@...>
Cc: Pedro Falcato <pedro.falcato@...>
Cc: Vitaly Cheptsov <vit9696@...>
Signed-off-by: Savva Mitrofanov <savvamtr@...>
---
Features/Ext4Pkg/Ext4Pkg.dsc | 2 +-
Features/Ext4Pkg/Ext4Dxe/DiskUtil.c | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/Features/Ext4Pkg/Ext4Pkg.dsc b/Features/Ext4Pkg/Ext4Pkg.dsc
index 59bc327ebf6e..621c63eaf92d 100644
--- a/Features/Ext4Pkg/Ext4Pkg.dsc
+++ b/Features/Ext4Pkg/Ext4Pkg.dsc
@@ -46,7 +46,7 @@
DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
OrderedCollectionLib|MdePkg/Library/BaseOrderedCollectionRedBlackTreeLib/BaseOrderedCollectionRedBlackTreeLib.inf
BaseUcs2Utf8Lib|RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.inf
-
+
#
# Required for stack protector support
#
diff --git a/Features/Ext4Pkg/Ext4Dxe/DiskUtil.c b/Features/Ext4Pkg/Ext4Dxe/DiskUtil.c
index 32da35f7d9f5..c4af956da926 100644
--- a/Features/Ext4Pkg/Ext4Dxe/DiskUtil.c
+++ b/Features/Ext4Pkg/Ext4Dxe/DiskUtil.c
@@ -60,11 +60,11 @@ Ext4ReadBlocks (
// Check for overflow on the block -> byte conversions.
// Partition->BlockSize is never 0, so we don't need to check for that.

- if (Offset > DivU64x32 ((UINT64)-1, Partition->BlockSize)) {
+ if ((NumberBlocks != 0) && (DivU64x64Remainder (Offset, BlockNumber, NULL) != Partition->BlockSize)) {
return EFI_INVALID_PARAMETER;
}

- if (Length > (UINTN)-1/Partition->BlockSize) {
+ if ((NumberBlocks != 0) && (Length / NumberBlocks != Partition->BlockSize)) {
return EFI_INVALID_PARAMETER;
}

@@ -94,12 +94,12 @@ Ext4AllocAndReadBlocks (

Length = NumberBlocks * Partition->BlockSize;

- if (Length > (UINTN)-1/Partition->BlockSize) {
+ // Check for integer overflow
+ if ((NumberBlocks != 0) && (Length / NumberBlocks != Partition->BlockSize)) {
return NULL;
}

Buf = AllocatePool (Length);
-
if (Buf == NULL) {
return NULL;
}
--
2.38.1