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

b6c182
From 2f0e51dcfea6d9101c4694636a948eb4b6e6d4d4 Mon Sep 17 00:00:00 2001
b6c182
From: Laszlo Ersek <lersek@redhat.com>
b6c182
Date: Tue, 8 Jun 2021 14:12:57 +0200
b6c182
Subject: [PATCH 08/10] NetworkPkg/IScsiDxe: fix IScsiHexToBin() hex parsing
b6c182
MIME-Version: 1.0
b6c182
Content-Type: text/plain; charset=UTF-8
b6c182
Content-Transfer-Encoding: 8bit
b6c182
b6c182
RH-Author: Laszlo Ersek <lersek@redhat.com>
b6c182
RH-MergeRequest: 5: NetworkPkg/IScsiDxe: fix IScsiHexToBin() security and functionality bugs [rhel-8.5.0, post-rebase]
b6c182
RH-Commit: [8/10] febb96c07dbd0e4a191e855742cb47fc6e39dfba
b6c182
RH-Bugzilla: 1956408
b6c182
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
b6c182
b6c182
The IScsiHexToBin() function has the following parser issues:
b6c182
b6c182
(1) If the *subject sequence* in "HexStr" is empty, the function returns
b6c182
    EFI_SUCCESS (with "BinLength" set to 0 on output). Such inputs should
b6c182
    be rejected.
b6c182
b6c182
(2) The function mis-handles a "HexStr" that ends with a stray nibble. For
b6c182
    example, if "HexStr" is "0xABC", the function decodes it to the bytes
b6c182
    {0xAB, 0x0C}, sets "BinLength" to 2 on output, and returns
b6c182
    EFI_SUCCESS. Such inputs should be rejected.
b6c182
b6c182
(3) If an invalid hex char is found in "HexStr", the function treats it as
b6c182
    end-of-hex-string, and returns EFI_SUCCESS. Such inputs should be
b6c182
    rejected.
b6c182
b6c182
All of the above cases are remotely triggerable, as shown in a subsequent
b6c182
patch, which adds error checking to the IScsiHexToBin() call sites. While
b6c182
the initiator is not immediately compromised, incorrectly parsing CHAP_R
b6c182
from the target, in case of mutual authentication, is not great.
b6c182
b6c182
Extend the interface contract of IScsiHexToBin() with
b6c182
EFI_INVALID_PARAMETER, for reporting issues (1) through (3), and implement
b6c182
the new checks.
b6c182
b6c182
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
b6c182
Cc: Maciej Rabeda <maciej.rabeda@linux.intel.com>
b6c182
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
b6c182
Cc: Siyuan Fu <siyuan.fu@intel.com>
b6c182
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3356
b6c182
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
b6c182
Reviewed-by: Maciej Rabeda <maciej.rabeda@linux.intel.com>
b6c182
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
b6c182
Message-Id: <20210608121259.32451-9-lersek@redhat.com>
b6c182
(cherry picked from commit 47b76780b487dbfde4efb6843b16064c4a97e94d)
b6c182
---
b6c182
 NetworkPkg/IScsiDxe/IScsiMisc.c | 12 ++++++++++--
b6c182
 NetworkPkg/IScsiDxe/IScsiMisc.h |  1 +
b6c182
 2 files changed, 11 insertions(+), 2 deletions(-)
b6c182
b6c182
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.c b/NetworkPkg/IScsiDxe/IScsiMisc.c
b6c182
index 014700e87a..f0f4992b07 100644
b6c182
--- a/NetworkPkg/IScsiDxe/IScsiMisc.c
b6c182
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.c
b6c182
@@ -376,6 +376,7 @@ IScsiBinToHex (
b6c182
 
b6c182
   @retval EFI_SUCCESS           The hexadecimal string is converted into a
b6c182
                                 binary encoded buffer.
b6c182
+  @retval EFI_INVALID_PARAMETER Invalid hex encoding found in HexStr.
b6c182
   @retval EFI_BUFFER_TOO_SMALL  The binary buffer is too small to hold the
b6c182
                                 converted data.
b6c182
 **/
b6c182
@@ -402,14 +403,21 @@ IScsiHexToBin (
b6c182
 
b6c182
   Length = AsciiStrLen (HexStr);
b6c182
 
b6c182
+  //
b6c182
+  // Reject an empty hex string; reject a stray nibble.
b6c182
+  //
b6c182
+  if (Length == 0 || Length % 2 != 0) {
b6c182
+    return EFI_INVALID_PARAMETER;
b6c182
+  }
b6c182
+
b6c182
   for (Index = 0; Index < Length; Index ++) {
b6c182
     TemStr[0] = HexStr[Index];
b6c182
     Digit = (UINT8) AsciiStrHexToUint64 (TemStr);
b6c182
     if (Digit == 0 && TemStr[0] != '0') {
b6c182
       //
b6c182
-      // Invalid Lun Char.
b6c182
+      // Invalid Hex Char.
b6c182
       //
b6c182
-      break;
b6c182
+      return EFI_INVALID_PARAMETER;
b6c182
     }
b6c182
     if ((Index & 1) == 0) {
b6c182
       BinBuffer [Index/2] = Digit;
b6c182
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.h b/NetworkPkg/IScsiDxe/IScsiMisc.h
b6c182
index 28cf408cd5..404a482e57 100644
b6c182
--- a/NetworkPkg/IScsiDxe/IScsiMisc.h
b6c182
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.h
b6c182
@@ -171,6 +171,7 @@ IScsiBinToHex (
b6c182
 
b6c182
   @retval EFI_SUCCESS           The hexadecimal string is converted into a
b6c182
                                 binary encoded buffer.
b6c182
+  @retval EFI_INVALID_PARAMETER Invalid hex encoding found in HexStr.
b6c182
   @retval EFI_BUFFER_TOO_SMALL  The binary buffer is too small to hold the
b6c182
                                 converted data.
b6c182
 **/
b6c182
-- 
b6c182
2.27.0
b6c182