public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-libc Patch] StdLib/LibC/StdLib: Handle possible math overflow in malloc()
@ 2021-08-18 22:03 Michael D Kinney
  2021-08-18 22:08 ` [edk2-devel] " Rebecca Cran
  0 siblings, 1 reply; 3+ messages in thread
From: Michael D Kinney @ 2021-08-18 22:03 UTC (permalink / raw)
  To: devel; +Cc: Rebecca Cran, Yitzhak Briskman, Jian J Wang, Yonghong Zhu

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1510

Check for addition overflow in malloc() when computing NodeSize
and return error if overflow is detected.

Cc: Rebecca Cran <rebecca@nuviainc.com>
Cc: Yitzhak Briskman <yitzhak.briskman@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
---
 StdLib/LibC/StdLib/Malloc.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/StdLib/LibC/StdLib/Malloc.c b/StdLib/LibC/StdLib/Malloc.c
index c131b9e..7bf8827 100644
--- a/StdLib/LibC/StdLib/Malloc.c
+++ b/StdLib/LibC/StdLib/Malloc.c
@@ -94,6 +94,12 @@ malloc(size_t Size)
     return NULL;
   }
 
+  if ((Size + sizeof(CPOOL_HEAD)) < Size) {
+    RetVal  = NULL;
+    errno   = ENOMEM;
+    DEBUG((DEBUG_ERROR, "\nERROR malloc: Size overflow\n"));
+  }
+
   NodeSize = (UINTN)(Size + sizeof(CPOOL_HEAD));
 
   DEBUG((DEBUG_POOL, "malloc(%d): NodeSz: %d", Size, NodeSize));
-- 
2.32.0.windows.1


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

end of thread, other threads:[~2021-08-18 22:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-08-18 22:03 [edk2-libc Patch] StdLib/LibC/StdLib: Handle possible math overflow in malloc() Michael D Kinney
2021-08-18 22:08 ` [edk2-devel] " Rebecca Cran
2021-08-18 22:18   ` Michael D Kinney

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