* [edk2-devel] [PATCH v1 1/2] FatPkg/EnhancedFatDxe: Fix SIGN_EXTENSION Coverity issues
2023-09-28 7:46 [edk2-devel] [PATCH v1 0/2] BZ 4249: Fix FatPkg/EnhancedFatDxe issues pointed by Coverity Ranbir Singh
@ 2023-09-28 7:46 ` Ranbir Singh
2023-09-28 7:46 ` [edk2-devel] [PATCH v1 2/2] FatPkg/EnhancedFatDxe: Fix OVERFLOW_BEFORE_WIDEN Coverity issue Ranbir Singh
1 sibling, 0 replies; 3+ messages in thread
From: Ranbir Singh @ 2023-09-28 7:46 UTC (permalink / raw)
To: devel, rsingh; +Cc: Ray Ni, Veeresh Sangolli
From: Ranbir Singh <Ranbir.Singh3@Dell.com>
The functions FatGetDirEntInfo and FatOpenDirEnt contains the code
statements
Cluster = (Entry->FileClusterHigh << 16) | Entry->FileCluster;
and
OFile->FileCluster = ((DirEnt->Entry.FileClusterHigh) << 16) | (DirEnt->Entry.FileCluster);
respectively. As per Coverity report, in both these statements, there is
an "Operand1" with type "UINT16" (16 bits, unsigned) which is promoted in
"(Operand1 << 16) | Operand2" to type "int" (32 bits, signed), then sign-
extended to type "unsigned long long" (64 bits, unsigned). If the result
of "(Operand1 << 16) | Operand2" is greater than 0x7FFFFFFF, the upper
bits of the result will all be 1.
So to avoid sign-extension, typecast the Operand1 and then the inter-
-mediate result after << 16 operation with UINTN. Note - UINTN is the
data type of the variable on the LHS of the assignment.
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4249
Cc: Ray Ni <ray.ni@intel.com>
Co-authored-by: Veeresh Sangolli <veeresh.sangolli@dellteam.com>
Signed-off-by: Ranbir Singh <Ranbir.Singh3@Dell.com>
Signed-off-by: Ranbir Singh <rsingh@ventanamicro.com>
---
FatPkg/EnhancedFatDxe/DirectoryManage.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/FatPkg/EnhancedFatDxe/DirectoryManage.c b/FatPkg/EnhancedFatDxe/DirectoryManage.c
index 723fc35f38db..a21b7973cd21 100644
--- a/FatPkg/EnhancedFatDxe/DirectoryManage.c
+++ b/FatPkg/EnhancedFatDxe/DirectoryManage.c
@@ -474,7 +474,7 @@ FatGetDirEntInfo (
Info = Buffer;
Info->Size = ResultSize;
if ((Entry->Attributes & FAT_ATTRIBUTE_DIRECTORY) != 0) {
- Cluster = (Entry->FileClusterHigh << 16) | Entry->FileCluster;
+ Cluster = (UINTN)((UINTN)(Entry->FileClusterHigh) << 16) | Entry->FileCluster;
Info->PhysicalSize = FatPhysicalDirSize (Volume, Cluster);
Info->FileSize = Info->PhysicalSize;
} else {
@@ -1167,7 +1167,7 @@ FatOpenDirEnt (
//
Volume = Parent->Volume;
OFile->FullPathLen = Parent->FullPathLen + 1 + StrLen (DirEnt->FileString);
- OFile->FileCluster = ((DirEnt->Entry.FileClusterHigh) << 16) | (DirEnt->Entry.FileCluster);
+ OFile->FileCluster = (UINTN)((UINTN)(DirEnt->Entry.FileClusterHigh) << 16) | (DirEnt->Entry.FileCluster);
InsertTailList (&Parent->ChildHead, &OFile->ChildLink);
} else {
//
--
2.34.1
-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109139): https://edk2.groups.io/g/devel/message/109139
Mute This Topic: https://groups.io/mt/101633788/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [edk2-devel] [PATCH v1 2/2] FatPkg/EnhancedFatDxe: Fix OVERFLOW_BEFORE_WIDEN Coverity issue
2023-09-28 7:46 [edk2-devel] [PATCH v1 0/2] BZ 4249: Fix FatPkg/EnhancedFatDxe issues pointed by Coverity Ranbir Singh
2023-09-28 7:46 ` [edk2-devel] [PATCH v1 1/2] FatPkg/EnhancedFatDxe: Fix SIGN_EXTENSION Coverity issues Ranbir Singh
@ 2023-09-28 7:46 ` Ranbir Singh
1 sibling, 0 replies; 3+ messages in thread
From: Ranbir Singh @ 2023-09-28 7:46 UTC (permalink / raw)
To: devel, rsingh; +Cc: Ray Ni, Veeresh Sangolli
From: Ranbir Singh <Ranbir.Singh3@Dell.com>
The function FatInitializeDiskCache evaluates an expression
FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment
and assigns it to DataCacheSize which is of type UINTN.
As per Coverity report,
FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment is a
potentially overflowing expression with type "int" (32 bits, signed)
evaluated using 32-bit arithmetic, and then used in a context that
expects an expression of type "UINTN" (64 bits, unsigned).
To avoid overflow, cast "FAT_DATACACHE_GROUP_COUNT" to type "UINTN".
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4249
Cc: Ray Ni <ray.ni@intel.com>
Co-authored-by: Veeresh Sangolli <veeresh.sangolli@dellteam.com>
Signed-off-by: Ranbir Singh <Ranbir.Singh3@Dell.com>
Signed-off-by: Ranbir Singh <rsingh@ventanamicro.com>
---
FatPkg/EnhancedFatDxe/DiskCache.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/FatPkg/EnhancedFatDxe/DiskCache.c b/FatPkg/EnhancedFatDxe/DiskCache.c
index d1a34a6a646f..d56e338586d8 100644
--- a/FatPkg/EnhancedFatDxe/DiskCache.c
+++ b/FatPkg/EnhancedFatDxe/DiskCache.c
@@ -477,7 +477,7 @@ FatInitializeDiskCache (
DiskCache[CacheFat].BaseAddress = Volume->FatPos;
DiskCache[CacheFat].LimitAddress = Volume->FatPos + Volume->FatSize;
FatCacheSize = FatCacheGroupCount << DiskCache[CacheFat].PageAlignment;
- DataCacheSize = FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment;
+ DataCacheSize = (UINTN)FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment;
//
// Allocate the Fat Cache buffer
//
--
2.34.1
-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109140): https://edk2.groups.io/g/devel/message/109140
Mute This Topic: https://groups.io/mt/101633789/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-
^ permalink raw reply related [flat|nested] 3+ messages in thread