Blame SOURCES/edk2-NetworkPkg-IScsiDxe-fix-IScsiHexToBin-hex-parsing.patch

d15d15
From fd7e1858bc0e538e9af42b9f0514553da9533553 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 08/10] NetworkPkg/IScsiDxe: fix IScsiHexToBin() hex parsing
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: [8/10] f77fc01700564c5e15027bd902a846102d488bf6
d15d15
RH-Bugzilla: 1956676
d15d15
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
d15d15
d15d15
The IScsiHexToBin() function has the following parser issues:
d15d15
d15d15
(1) If the *subject sequence* in "HexStr" is empty, the function returns
d15d15
    EFI_SUCCESS (with "BinLength" set to 0 on output). Such inputs should
d15d15
    be rejected.
d15d15
d15d15
(2) The function mis-handles a "HexStr" that ends with a stray nibble. For
d15d15
    example, if "HexStr" is "0xABC", the function decodes it to the bytes
d15d15
    {0xAB, 0x0C}, sets "BinLength" to 2 on output, and returns
d15d15
    EFI_SUCCESS. Such inputs should be rejected.
d15d15
d15d15
(3) If an invalid hex char is found in "HexStr", the function treats it as
d15d15
    end-of-hex-string, and returns EFI_SUCCESS. Such inputs should be
d15d15
    rejected.
d15d15
d15d15
All of the above cases are remotely triggerable, as shown in a subsequent
d15d15
patch, which adds error checking to the IScsiHexToBin() call sites. While
d15d15
the initiator is not immediately compromised, incorrectly parsing CHAP_R
d15d15
from the target, in case of mutual authentication, is not great.
d15d15
d15d15
Extend the interface contract of IScsiHexToBin() with
d15d15
EFI_INVALID_PARAMETER, for reporting issues (1) through (3), and implement
d15d15
the new checks.
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 | 12 ++++++++++--
d15d15
 NetworkPkg/IScsiDxe/IScsiMisc.h |  1 +
d15d15
 2 files changed, 11 insertions(+), 2 deletions(-)
d15d15
d15d15
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.c b/NetworkPkg/IScsiDxe/IScsiMisc.c
d15d15
index 014700e87a..f0f4992b07 100644
d15d15
--- a/NetworkPkg/IScsiDxe/IScsiMisc.c
d15d15
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.c
d15d15
@@ -376,6 +376,7 @@ IScsiBinToHex (
d15d15
 
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_BUFFER_TOO_SMALL  The binary buffer is too small to hold the
d15d15
                                 converted data.
d15d15
 **/
d15d15
@@ -402,14 +403,21 @@ IScsiHexToBin (
d15d15
 
d15d15
   Length = AsciiStrLen (HexStr);
d15d15
 
d15d15
+  //
d15d15
+  // Reject an empty hex string; reject a stray nibble.
d15d15
+  //
d15d15
+  if (Length == 0 || Length % 2 != 0) {
d15d15
+    return EFI_INVALID_PARAMETER;
d15d15
+  }
d15d15
+
d15d15
   for (Index = 0; Index < Length; Index ++) {
d15d15
     TemStr[0] = HexStr[Index];
d15d15
     Digit = (UINT8) AsciiStrHexToUint64 (TemStr);
d15d15
     if (Digit == 0 && TemStr[0] != '0') {
d15d15
       //
d15d15
-      // Invalid Lun Char.
d15d15
+      // Invalid Hex Char.
d15d15
       //
d15d15
-      break;
d15d15
+      return EFI_INVALID_PARAMETER;
d15d15
     }
d15d15
     if ((Index & 1) == 0) {
d15d15
       BinBuffer [Index/2] = Digit;
d15d15
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.h b/NetworkPkg/IScsiDxe/IScsiMisc.h
d15d15
index 28cf408cd5..404a482e57 100644
d15d15
--- a/NetworkPkg/IScsiDxe/IScsiMisc.h
d15d15
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.h
d15d15
@@ -171,6 +171,7 @@ IScsiBinToHex (
d15d15
 
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_BUFFER_TOO_SMALL  The binary buffer is too small to hold the
d15d15
                                 converted data.
d15d15
 **/
d15d15
-- 
d15d15
2.27.0
d15d15