Blame SOURCES/edk2-NetworkPkg-IScsiDxe-fix-IScsiHexToBin-buffer-overflo.patch

d15d15
From 47668ca7bca333ef223b5897fb044b6760f215f5 Mon Sep 17 00:00:00 2001
d15d15
From: Laszlo Ersek <lersek@redhat.com>
d15d15
Date: Tue, 27 Apr 2021 11:02:51 +0200
d15d15
Subject: [PATCH 09/10] NetworkPkg/IScsiDxe: fix IScsiHexToBin() buffer
d15d15
 overflow
d15d15
MIME-Version: 1.0
d15d15
Content-Type: text/plain; charset=UTF-8
d15d15
Content-Transfer-Encoding: 8bit
d15d15
d15d15
RH-Author: Laszlo Ersek <lersek@redhat.com>
d15d15
RH-MergeRequest: 3: NetworkPkg/IScsiDxe: fix IScsiHexToBin() security and functionality bugs [rhel-8.4.0.z]
d15d15
RH-Commit: [9/10] 9230129f3b079e61a53d39b81072c7884c991e49
d15d15
RH-Bugzilla: 1956676
d15d15
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
d15d15
d15d15
The IScsiHexToBin() function documents the EFI_BUFFER_TOO_SMALL return
d15d15
condition, but never actually checks whether the decoded buffer fits into
d15d15
the caller-provided room (i.e., the input value of "BinLength"), and
d15d15
EFI_BUFFER_TOO_SMALL is never returned. The decoding of "HexStr" can
d15d15
overflow "BinBuffer".
d15d15
d15d15
This is remotely exploitable, as shown in a subsequent patch, which adds
d15d15
error checking to the IScsiHexToBin() call sites. This issue allows the
d15d15
target to compromise the initiator.
d15d15
d15d15
Introduce EFI_BAD_BUFFER_SIZE, in addition to the existent
d15d15
EFI_BUFFER_TOO_SMALL, for reporting a special case of the buffer overflow,
d15d15
plus actually catch the buffer overflow.
d15d15
d15d15
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
d15d15
Cc: Maciej Rabeda <maciej.rabeda@linux.intel.com>
d15d15
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
d15d15
Cc: Siyuan Fu <siyuan.fu@intel.com>
d15d15
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3356
d15d15
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
d15d15
Reviewed-by: Maciej Rabeda <maciej.rabeda@linux.intel.com>
d15d15
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
d15d15
Upstream: https://bugzilla.tianocore.org/show_bug.cgi?id=3356, c#17...c#22
d15d15
---
d15d15
 NetworkPkg/IScsiDxe/IScsiMisc.c | 20 +++++++++++++++++---
d15d15
 NetworkPkg/IScsiDxe/IScsiMisc.h |  3 +++
d15d15
 2 files changed, 20 insertions(+), 3 deletions(-)
d15d15
d15d15
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.c b/NetworkPkg/IScsiDxe/IScsiMisc.c
d15d15
index f0f4992b07..4069547867 100644
d15d15
--- a/NetworkPkg/IScsiDxe/IScsiMisc.c
d15d15
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.c
d15d15
@@ -377,6 +377,9 @@ IScsiBinToHex (
d15d15
   @retval EFI_SUCCESS           The hexadecimal string is converted into a
d15d15
                                 binary encoded buffer.
d15d15
   @retval EFI_INVALID_PARAMETER Invalid hex encoding found in HexStr.
d15d15
+  @retval EFI_BAD_BUFFER_SIZE   The length of HexStr is too large for decoding:
d15d15
+                                the decoded size cannot be expressed in
d15d15
+                                BinLength on output.
d15d15
   @retval EFI_BUFFER_TOO_SMALL  The binary buffer is too small to hold the
d15d15
                                 converted data.
d15d15
 **/
d15d15
@@ -387,6 +390,8 @@ IScsiHexToBin (
d15d15
   IN     CHAR8  *HexStr
d15d15
   )
d15d15
 {
d15d15
+  UINTN   BinLengthMin;
d15d15
+  UINT32  BinLengthProvided;
d15d15
   UINTN   Index;
d15d15
   UINTN   Length;
d15d15
   UINT8   Digit;
d15d15
@@ -409,6 +414,18 @@ IScsiHexToBin (
d15d15
   if (Length == 0 || Length % 2 != 0) {
d15d15
     return EFI_INVALID_PARAMETER;
d15d15
   }
d15d15
+  //
d15d15
+  // Check if the caller provides enough room for the decoded blob.
d15d15
+  //
d15d15
+  BinLengthMin = Length / 2;
d15d15
+  if (BinLengthMin > MAX_UINT32) {
d15d15
+    return EFI_BAD_BUFFER_SIZE;
d15d15
+  }
d15d15
+  BinLengthProvided = *BinLength;
d15d15
+  *BinLength = (UINT32)BinLengthMin;
d15d15
+  if (BinLengthProvided < BinLengthMin) {
d15d15
+    return EFI_BUFFER_TOO_SMALL;
d15d15
+  }
d15d15
 
d15d15
   for (Index = 0; Index < Length; Index ++) {
d15d15
     TemStr[0] = HexStr[Index];
d15d15
@@ -425,9 +442,6 @@ IScsiHexToBin (
d15d15
       BinBuffer [Index/2] = (UINT8) ((BinBuffer [Index/2] << 4) + Digit);
d15d15
     }
d15d15
   }
d15d15
-
d15d15
-  *BinLength = (UINT32) ((Index + 1)/2);
d15d15
-
d15d15
   return EFI_SUCCESS;
d15d15
 }
d15d15
 
d15d15
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.h b/NetworkPkg/IScsiDxe/IScsiMisc.h
d15d15
index 404a482e57..fddef4f466 100644
d15d15
--- a/NetworkPkg/IScsiDxe/IScsiMisc.h
d15d15
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.h
d15d15
@@ -172,6 +172,9 @@ IScsiBinToHex (
d15d15
   @retval EFI_SUCCESS           The hexadecimal string is converted into a
d15d15
                                 binary encoded buffer.
d15d15
   @retval EFI_INVALID_PARAMETER Invalid hex encoding found in HexStr.
d15d15
+  @retval EFI_BAD_BUFFER_SIZE   The length of HexStr is too large for decoding:
d15d15
+                                the decoded size cannot be expressed in
d15d15
+                                BinLength on output.
d15d15
   @retval EFI_BUFFER_TOO_SMALL  The binary buffer is too small to hold the
d15d15
                                 converted data.
d15d15
 **/
d15d15
-- 
d15d15
2.27.0
d15d15