Blame SOURCES/edk2-NetworkPkg-IScsiDxe-fix-potential-integer-overflow-i.patch

b6c182
From 4171bd515a2dcfec59513d3a83adce7ed2903d50 Mon Sep 17 00:00:00 2001
b6c182
From: Laszlo Ersek <lersek@redhat.com>
b6c182
Date: Tue, 8 Jun 2021 14:12:54 +0200
b6c182
Subject: [PATCH 05/10] NetworkPkg/IScsiDxe: fix potential integer overflow in
b6c182
 IScsiBinToHex()
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: [5/10] f52aaaa03b15280eb4a821eeb378d8051ea5ec2a
b6c182
RH-Bugzilla: 1956408
b6c182
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
b6c182
b6c182
Considering IScsiBinToHex():
b6c182
b6c182
>   if (((*HexLength) - 3) < BinLength * 2) {
b6c182
>     *HexLength = BinLength * 2 + 3;
b6c182
>   }
b6c182
b6c182
the following subexpressions are problematic:
b6c182
b6c182
  (*HexLength) - 3
b6c182
  BinLength * 2
b6c182
  BinLength * 2 + 3
b6c182
b6c182
The first one may wrap under zero, the latter two may wrap over
b6c182
MAX_UINT32.
b6c182
b6c182
Rewrite the calculation using SafeIntLib.
b6c182
b6c182
While at it, change the type of the "Index" variable from UINTN to UINT32.
b6c182
The largest "Index"-based value that we calculate is
b6c182
b6c182
  Index * 2 + 2                                (with (Index == BinLength))
b6c182
b6c182
Because the patch makes
b6c182
b6c182
  BinLength * 2 + 3
b6c182
b6c182
safe to calculate in UINT32, using UINT32 for
b6c182
b6c182
  Index * 2 + 2                                (with (Index == BinLength))
b6c182
b6c182
is safe too. Consistently using UINT32 improves readability.
b6c182
b6c182
This patch is best reviewed with "git show -W".
b6c182
b6c182
The integer overflows that this patch fixes are theoretical; a subsequent
b6c182
patch in the series will audit the IScsiBinToHex() call sites, and show
b6c182
that none of them can fail.
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-6-lersek@redhat.com>
b6c182
(cherry picked from commit cf01b2dc8fc3ff9cf49fb891af5703dc03e3193e)
b6c182
---
b6c182
 NetworkPkg/IScsiDxe/IScsiDxe.inf |  1 +
b6c182
 NetworkPkg/IScsiDxe/IScsiImpl.h  |  1 +
b6c182
 NetworkPkg/IScsiDxe/IScsiMisc.c  | 19 +++++++++++++++----
b6c182
 NetworkPkg/IScsiDxe/IScsiMisc.h  |  1 +
b6c182
 4 files changed, 18 insertions(+), 4 deletions(-)
b6c182
b6c182
diff --git a/NetworkPkg/IScsiDxe/IScsiDxe.inf b/NetworkPkg/IScsiDxe/IScsiDxe.inf
b6c182
index 543c408302..1dde56d00c 100644
b6c182
--- a/NetworkPkg/IScsiDxe/IScsiDxe.inf
b6c182
+++ b/NetworkPkg/IScsiDxe/IScsiDxe.inf
b6c182
@@ -74,6 +74,7 @@
b6c182
   MemoryAllocationLib
b6c182
   NetLib
b6c182
   PrintLib
b6c182
+  SafeIntLib
b6c182
   TcpIoLib
b6c182
   UefiBootServicesTableLib
b6c182
   UefiDriverEntryPoint
b6c182
diff --git a/NetworkPkg/IScsiDxe/IScsiImpl.h b/NetworkPkg/IScsiDxe/IScsiImpl.h
b6c182
index d895c7feb9..ac3a25730e 100644
b6c182
--- a/NetworkPkg/IScsiDxe/IScsiImpl.h
b6c182
+++ b/NetworkPkg/IScsiDxe/IScsiImpl.h
b6c182
@@ -44,6 +44,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
b6c182
 #include <Library/MemoryAllocationLib.h>
b6c182
 #include <Library/NetLib.h>
b6c182
 #include <Library/PrintLib.h>
b6c182
+#include <Library/SafeIntLib.h>
b6c182
 #include <Library/TcpIoLib.h>
b6c182
 #include <Library/UefiBootServicesTableLib.h>
b6c182
 #include <Library/UefiHiiServicesLib.h>
b6c182
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.c b/NetworkPkg/IScsiDxe/IScsiMisc.c
b6c182
index b8fef3ff6f..42988e15cb 100644
b6c182
--- a/NetworkPkg/IScsiDxe/IScsiMisc.c
b6c182
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.c
b6c182
@@ -316,6 +316,7 @@ IScsiMacAddrToStr (
b6c182
   @retval EFI_SUCCESS          The binary data is converted to the hexadecimal string
b6c182
                                and the length of the string is updated.
b6c182
   @retval EFI_BUFFER_TOO_SMALL The string is too small.
b6c182
+  @retval EFI_BAD_BUFFER_SIZE  BinLength is too large for hex encoding.
b6c182
   @retval EFI_INVALID_PARAMETER The IP string is malformatted.
b6c182
 
b6c182
 **/
b6c182
@@ -327,18 +328,28 @@ IScsiBinToHex (
b6c182
   IN OUT UINT32 *HexLength
b6c182
   )
b6c182
 {
b6c182
-  UINTN Index;
b6c182
+  UINT32 HexLengthMin;
b6c182
+  UINT32 HexLengthProvided;
b6c182
+  UINT32 Index;
b6c182
 
b6c182
   if ((HexStr == NULL) || (BinBuffer == NULL) || (BinLength == 0)) {
b6c182
     return EFI_INVALID_PARAMETER;
b6c182
   }
b6c182
 
b6c182
-  if (((*HexLength) - 3) < BinLength * 2) {
b6c182
-    *HexLength = BinLength * 2 + 3;
b6c182
+  //
b6c182
+  // Safely calculate: HexLengthMin := BinLength * 2 + 3.
b6c182
+  //
b6c182
+  if (RETURN_ERROR (SafeUint32Mult (BinLength, 2, &HexLengthMin)) ||
b6c182
+      RETURN_ERROR (SafeUint32Add (HexLengthMin, 3, &HexLengthMin))) {
b6c182
+    return EFI_BAD_BUFFER_SIZE;
b6c182
+  }
b6c182
+
b6c182
+  HexLengthProvided = *HexLength;
b6c182
+  *HexLength = HexLengthMin;
b6c182
+  if (HexLengthProvided < HexLengthMin) {
b6c182
     return EFI_BUFFER_TOO_SMALL;
b6c182
   }
b6c182
 
b6c182
-  *HexLength = BinLength * 2 + 3;
b6c182
   //
b6c182
   // Prefix for Hex String.
b6c182
   //
b6c182
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.h b/NetworkPkg/IScsiDxe/IScsiMisc.h
b6c182
index 46c725aab3..231413993b 100644
b6c182
--- a/NetworkPkg/IScsiDxe/IScsiMisc.h
b6c182
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.h
b6c182
@@ -150,6 +150,7 @@ IScsiAsciiStrToIp (
b6c182
   @retval EFI_SUCCESS          The binary data is converted to the hexadecimal string
b6c182
                                and the length of the string is updated.
b6c182
   @retval EFI_BUFFER_TOO_SMALL The string is too small.
b6c182
+  @retval EFI_BAD_BUFFER_SIZE  BinLength is too large for hex encoding.
b6c182
   @retval EFI_INVALID_PARAMETER The IP string is malformatted.
b6c182
 
b6c182
 **/
b6c182
-- 
b6c182
2.27.0
b6c182