public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Pedro Falcato" <pedro.falcato@gmail.com>
To: devel@edk2.groups.io
Cc: "Pedro Falcato" <pedro.falcato@gmail.com>,
	"Vitaly Cheptsov" <vit9696@protonmail.com>,
	"Marvin Häuser" <mhaeuser@posteo.de>,
	"Michael D Kinney" <michael.d.kinney@intel.com>,
	"Liming Gao" <gaoliming@byosoft.com.cn>,
	"Zhiguang Liu" <zhiguang.liu@intel.com>
Subject: [PATCH 1/1] MdePkg/BaseLib: Fix out-of-bounds reads in SafeString
Date: Mon, 24 Oct 2022 23:11:44 +0100	[thread overview]
Message-ID: <20221024221144.20702-1-pedro.falcato@gmail.com> (raw)

OpenCore folks established an ASAN-equipped project to fuzz Ext4Dxe,
which was able to catch these (mostly harmless) issues.

Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
Cc: Vitaly Cheptsov <vit9696@protonmail.com>
Cc: Marvin Häuser <mhaeuser@posteo.de>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
---
 MdePkg/Library/BaseLib/SafeString.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/MdePkg/Library/BaseLib/SafeString.c b/MdePkg/Library/BaseLib/SafeString.c
index f338a32a3a41..9bf86d32e1d6 100644
--- a/MdePkg/Library/BaseLib/SafeString.c
+++ b/MdePkg/Library/BaseLib/SafeString.c
@@ -863,6 +863,9 @@ StrHexToUintnS (
   OUT       UINTN   *Data
   )
 {
+  CONST CHAR16  *StartString;
+
+  StartString = String;
   ASSERT (((UINTN)String & BIT0) == 0);
 
   //
@@ -897,7 +900,7 @@ StrHexToUintnS (
   }
 
   if (CharToUpper (*String) == L'X') {
-    if (*(String - 1) != L'0') {
+    if ((String != StartString) && (*(String - 1) != L'0')) {
       *Data = 0;
       return RETURN_SUCCESS;
     }
@@ -992,6 +995,9 @@ StrHexToUint64S (
   OUT       UINT64  *Data
   )
 {
+  CONST CHAR16  *StartString;
+
+  StartString = String;
   ASSERT (((UINTN)String & BIT0) == 0);
 
   //
@@ -1026,7 +1032,7 @@ StrHexToUint64S (
   }
 
   if (CharToUpper (*String) == L'X') {
-    if (*(String - 1) != L'0') {
+    if ((String != StartString) && (*(String - 1) != L'0')) {
       *Data = 0;
       return RETURN_SUCCESS;
     }
@@ -2393,6 +2399,9 @@ AsciiStrHexToUintnS (
   OUT       UINTN  *Data
   )
 {
+  CONST CHAR8  *StartString;
+
+  StartString = String;
   //
   // 1. Neither String nor Data shall be a null pointer.
   //
@@ -2425,7 +2434,7 @@ AsciiStrHexToUintnS (
   }
 
   if (AsciiCharToUpper (*String) == 'X') {
-    if (*(String - 1) != '0') {
+    if ((String != StartString) && (*(String - 1) != '0')) {
       *Data = 0;
       return RETURN_SUCCESS;
     }
@@ -2517,6 +2526,9 @@ AsciiStrHexToUint64S (
   OUT       UINT64  *Data
   )
 {
+  CONST CHAR8  *StartString;
+
+  StartString = String;
   //
   // 1. Neither String nor Data shall be a null pointer.
   //
@@ -2549,7 +2561,7 @@ AsciiStrHexToUint64S (
   }
 
   if (AsciiCharToUpper (*String) == 'X') {
-    if (*(String - 1) != '0') {
+    if ((String != StartString) && (*(String - 1) != '0')) {
       *Data = 0;
       return RETURN_SUCCESS;
     }
-- 
2.38.1


                 reply	other threads:[~2022-10-24 22:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221024221144.20702-1-pedro.falcato@gmail.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox