render / rpms / edk2

Forked from rpms/edk2 3 months ago
Clone

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

c4e3b2
From 67474c22010ba8c7c240d8e02b2151c7d796171d Mon Sep 17 00:00:00 2001
c4e3b2
From: Laszlo Ersek <lersek@redhat.com>
c4e3b2
Date: Tue, 8 Jun 2021 14:12:54 +0200
c4e3b2
Subject: [PATCH 05/11] NetworkPkg/IScsiDxe: fix potential integer overflow in
c4e3b2
 IScsiBinToHex()
c4e3b2
MIME-Version: 1.0
c4e3b2
Content-Type: text/plain; charset=UTF-8
c4e3b2
Content-Transfer-Encoding: 8bit
c4e3b2
c4e3b2
RH-Author: Laszlo Ersek <lersek@redhat.com>
c4e3b2
RH-MergeRequest: 1: NetworkPkg/IScsiDxe: fix IScsiHexToBin() security and functionality bugs [RHEL-9, c9s]
c4e3b2
RH-Commit: [5/10] 3d7a886c1f73d811ef47381e4d6a82683ab0900e
c4e3b2
RH-Bugzilla: 1961100
c4e3b2
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
c4e3b2
c4e3b2
Considering IScsiBinToHex():
c4e3b2
c4e3b2
>   if (((*HexLength) - 3) < BinLength * 2) {
c4e3b2
>     *HexLength = BinLength * 2 + 3;
c4e3b2
>   }
c4e3b2
c4e3b2
the following subexpressions are problematic:
c4e3b2
c4e3b2
  (*HexLength) - 3
c4e3b2
  BinLength * 2
c4e3b2
  BinLength * 2 + 3
c4e3b2
c4e3b2
The first one may wrap under zero, the latter two may wrap over
c4e3b2
MAX_UINT32.
c4e3b2
c4e3b2
Rewrite the calculation using SafeIntLib.
c4e3b2
c4e3b2
While at it, change the type of the "Index" variable from UINTN to UINT32.
c4e3b2
The largest "Index"-based value that we calculate is
c4e3b2
c4e3b2
  Index * 2 + 2                                (with (Index == BinLength))
c4e3b2
c4e3b2
Because the patch makes
c4e3b2
c4e3b2
  BinLength * 2 + 3
c4e3b2
c4e3b2
safe to calculate in UINT32, using UINT32 for
c4e3b2
c4e3b2
  Index * 2 + 2                                (with (Index == BinLength))
c4e3b2
c4e3b2
is safe too. Consistently using UINT32 improves readability.
c4e3b2
c4e3b2
This patch is best reviewed with "git show -W".
c4e3b2
c4e3b2
The integer overflows that this patch fixes are theoretical; a subsequent
c4e3b2
patch in the series will audit the IScsiBinToHex() call sites, and show
c4e3b2
that none of them can fail.
c4e3b2
c4e3b2
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
c4e3b2
Cc: Maciej Rabeda <maciej.rabeda@linux.intel.com>
c4e3b2
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
c4e3b2
Cc: Siyuan Fu <siyuan.fu@intel.com>
c4e3b2
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3356
c4e3b2
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
c4e3b2
Reviewed-by: Maciej Rabeda <maciej.rabeda@linux.intel.com>
c4e3b2
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
c4e3b2
Message-Id: <20210608121259.32451-6-lersek@redhat.com>
c4e3b2
(cherry picked from commit cf01b2dc8fc3ff9cf49fb891af5703dc03e3193e)
c4e3b2
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
c4e3b2
---
c4e3b2
 NetworkPkg/IScsiDxe/IScsiDxe.inf |  1 +
c4e3b2
 NetworkPkg/IScsiDxe/IScsiImpl.h  |  1 +
c4e3b2
 NetworkPkg/IScsiDxe/IScsiMisc.c  | 19 +++++++++++++++----
c4e3b2
 NetworkPkg/IScsiDxe/IScsiMisc.h  |  1 +
c4e3b2
 4 files changed, 18 insertions(+), 4 deletions(-)
c4e3b2
c4e3b2
diff --git a/NetworkPkg/IScsiDxe/IScsiDxe.inf b/NetworkPkg/IScsiDxe/IScsiDxe.inf
c4e3b2
index 543c408302..1dde56d00c 100644
c4e3b2
--- a/NetworkPkg/IScsiDxe/IScsiDxe.inf
c4e3b2
+++ b/NetworkPkg/IScsiDxe/IScsiDxe.inf
c4e3b2
@@ -74,6 +74,7 @@
c4e3b2
   MemoryAllocationLib
c4e3b2
   NetLib
c4e3b2
   PrintLib
c4e3b2
+  SafeIntLib
c4e3b2
   TcpIoLib
c4e3b2
   UefiBootServicesTableLib
c4e3b2
   UefiDriverEntryPoint
c4e3b2
diff --git a/NetworkPkg/IScsiDxe/IScsiImpl.h b/NetworkPkg/IScsiDxe/IScsiImpl.h
c4e3b2
index d895c7feb9..ac3a25730e 100644
c4e3b2
--- a/NetworkPkg/IScsiDxe/IScsiImpl.h
c4e3b2
+++ b/NetworkPkg/IScsiDxe/IScsiImpl.h
c4e3b2
@@ -44,6 +44,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
c4e3b2
 #include <Library/MemoryAllocationLib.h>
c4e3b2
 #include <Library/NetLib.h>
c4e3b2
 #include <Library/PrintLib.h>
c4e3b2
+#include <Library/SafeIntLib.h>
c4e3b2
 #include <Library/TcpIoLib.h>
c4e3b2
 #include <Library/UefiBootServicesTableLib.h>
c4e3b2
 #include <Library/UefiHiiServicesLib.h>
c4e3b2
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.c b/NetworkPkg/IScsiDxe/IScsiMisc.c
c4e3b2
index b8fef3ff6f..42988e15cb 100644
c4e3b2
--- a/NetworkPkg/IScsiDxe/IScsiMisc.c
c4e3b2
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.c
c4e3b2
@@ -316,6 +316,7 @@ IScsiMacAddrToStr (
c4e3b2
   @retval EFI_SUCCESS          The binary data is converted to the hexadecimal string
c4e3b2
                                and the length of the string is updated.
c4e3b2
   @retval EFI_BUFFER_TOO_SMALL The string is too small.
c4e3b2
+  @retval EFI_BAD_BUFFER_SIZE  BinLength is too large for hex encoding.
c4e3b2
   @retval EFI_INVALID_PARAMETER The IP string is malformatted.
c4e3b2
 
c4e3b2
 **/
c4e3b2
@@ -327,18 +328,28 @@ IScsiBinToHex (
c4e3b2
   IN OUT UINT32 *HexLength
c4e3b2
   )
c4e3b2
 {
c4e3b2
-  UINTN Index;
c4e3b2
+  UINT32 HexLengthMin;
c4e3b2
+  UINT32 HexLengthProvided;
c4e3b2
+  UINT32 Index;
c4e3b2
 
c4e3b2
   if ((HexStr == NULL) || (BinBuffer == NULL) || (BinLength == 0)) {
c4e3b2
     return EFI_INVALID_PARAMETER;
c4e3b2
   }
c4e3b2
 
c4e3b2
-  if (((*HexLength) - 3) < BinLength * 2) {
c4e3b2
-    *HexLength = BinLength * 2 + 3;
c4e3b2
+  //
c4e3b2
+  // Safely calculate: HexLengthMin := BinLength * 2 + 3.
c4e3b2
+  //
c4e3b2
+  if (RETURN_ERROR (SafeUint32Mult (BinLength, 2, &HexLengthMin)) ||
c4e3b2
+      RETURN_ERROR (SafeUint32Add (HexLengthMin, 3, &HexLengthMin))) {
c4e3b2
+    return EFI_BAD_BUFFER_SIZE;
c4e3b2
+  }
c4e3b2
+
c4e3b2
+  HexLengthProvided = *HexLength;
c4e3b2
+  *HexLength = HexLengthMin;
c4e3b2
+  if (HexLengthProvided < HexLengthMin) {
c4e3b2
     return EFI_BUFFER_TOO_SMALL;
c4e3b2
   }
c4e3b2
 
c4e3b2
-  *HexLength = BinLength * 2 + 3;
c4e3b2
   //
c4e3b2
   // Prefix for Hex String.
c4e3b2
   //
c4e3b2
diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.h b/NetworkPkg/IScsiDxe/IScsiMisc.h
c4e3b2
index 46c725aab3..231413993b 100644
c4e3b2
--- a/NetworkPkg/IScsiDxe/IScsiMisc.h
c4e3b2
+++ b/NetworkPkg/IScsiDxe/IScsiMisc.h
c4e3b2
@@ -150,6 +150,7 @@ IScsiAsciiStrToIp (
c4e3b2
   @retval EFI_SUCCESS          The binary data is converted to the hexadecimal string
c4e3b2
                                and the length of the string is updated.
c4e3b2
   @retval EFI_BUFFER_TOO_SMALL The string is too small.
c4e3b2
+  @retval EFI_BAD_BUFFER_SIZE  BinLength is too large for hex encoding.
c4e3b2
   @retval EFI_INVALID_PARAMETER The IP string is malformatted.
c4e3b2
 
c4e3b2
 **/
c4e3b2
-- 
c4e3b2
2.27.0
c4e3b2