Blame SOURCES/edk2-NetworkPkg-IScsiDxe-check-IScsiHexToBin-return-value.patch

c4e3b2
From de86f03cd7ed849ff62b1591c5fd34aeb1792887 Mon Sep 17 00:00:00 2001
c4e3b2
From: Laszlo Ersek <lersek@redhat.com>
c4e3b2
Date: Tue, 8 Jun 2021 14:12:59 +0200
c4e3b2
Subject: [PATCH 10/11] NetworkPkg/IScsiDxe: check IScsiHexToBin() return
c4e3b2
 values
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: [10/10] 840f483839ce598396bb6db8ec1f0f50689b8215
c4e3b2
RH-Bugzilla: 1961100
c4e3b2
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
c4e3b2
c4e3b2
IScsiDxe (that is, the initiator) receives two hex-encoded strings from
c4e3b2
the iSCSI target:
c4e3b2
c4e3b2
- CHAP_C, where the target challenges the initiator,
c4e3b2
c4e3b2
- CHAP_R, where the target answers the challenge from the initiator (in
c4e3b2
  case the initiator wants mutual authentication).
c4e3b2
c4e3b2
Accordingly, we have two IScsiHexToBin() call sites:
c4e3b2
c4e3b2
- At the CHAP_C decoding site, check whether the decoding succeeds. The
c4e3b2
  decoded buffer ("AuthData->InChallenge") can accommodate 1024 bytes,
c4e3b2
  which is a permissible restriction on the target, per
c4e3b2
  <https://tools.ietf.org/html/rfc7143#section-12.1.3>. Shorter challenges
c4e3b2
  from the target are acceptable.
c4e3b2
c4e3b2
- At the CHAP_R decoding site, enforce that the decoding both succeed, and
c4e3b2
  provide exactly ISCSI_CHAP_RSP_LEN bytes. CHAP_R contains the digest
c4e3b2
  calculated by the target, therefore it must be of fixed size. We may
c4e3b2
  only call IScsiCHAPAuthTarget() if "TargetRsp" has been fully populated.
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: Philippe Mathieu-Daudé <philmd@redhat.com>
c4e3b2
Reviewed-by: Maciej Rabeda <maciej.rabeda@linux.intel.com>
c4e3b2
Message-Id: <20210608121259.32451-11-lersek@redhat.com>
c4e3b2
(cherry picked from commit b8649cf2a3e673a4a8cb6c255e394b354b771550)
c4e3b2
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
c4e3b2
---
c4e3b2
 NetworkPkg/IScsiDxe/IScsiCHAP.c | 20 ++++++++++++++------
c4e3b2
 1 file changed, 14 insertions(+), 6 deletions(-)
c4e3b2
c4e3b2
diff --git a/NetworkPkg/IScsiDxe/IScsiCHAP.c b/NetworkPkg/IScsiDxe/IScsiCHAP.c
c4e3b2
index dbe3c8ef46..7e930c0d1e 100644
c4e3b2
--- a/NetworkPkg/IScsiDxe/IScsiCHAP.c
c4e3b2
+++ b/NetworkPkg/IScsiDxe/IScsiCHAP.c
c4e3b2
@@ -290,11 +290,15 @@ IScsiCHAPOnRspReceived (
c4e3b2
 
c4e3b2
     AuthData->InIdentifier      = (UINT32) Result;
c4e3b2
     AuthData->InChallengeLength = (UINT32) sizeof (AuthData->InChallenge);
c4e3b2
-    IScsiHexToBin (
c4e3b2
-      (UINT8 *) AuthData->InChallenge,
c4e3b2
-      &AuthData->InChallengeLength,
c4e3b2
-      Challenge
c4e3b2
-      );
c4e3b2
+    Status = IScsiHexToBin (
c4e3b2
+               (UINT8 *) AuthData->InChallenge,
c4e3b2
+               &AuthData->InChallengeLength,
c4e3b2
+               Challenge
c4e3b2
+               );
c4e3b2
+    if (EFI_ERROR (Status)) {
c4e3b2
+      Status = EFI_PROTOCOL_ERROR;
c4e3b2
+      goto ON_EXIT;
c4e3b2
+    }
c4e3b2
     Status = IScsiCHAPCalculateResponse (
c4e3b2
                AuthData->InIdentifier,
c4e3b2
                AuthData->AuthConfig->CHAPSecret,
c4e3b2
@@ -337,7 +341,11 @@ IScsiCHAPOnRspReceived (
c4e3b2
     }
c4e3b2
 
c4e3b2
     RspLen = ISCSI_CHAP_RSP_LEN;
c4e3b2
-    IScsiHexToBin (TargetRsp, &RspLen, Response);
c4e3b2
+    Status = IScsiHexToBin (TargetRsp, &RspLen, Response);
c4e3b2
+    if (EFI_ERROR (Status) || RspLen != ISCSI_CHAP_RSP_LEN) {
c4e3b2
+      Status = EFI_PROTOCOL_ERROR;
c4e3b2
+      goto ON_EXIT;
c4e3b2
+    }
c4e3b2
 
c4e3b2
     //
c4e3b2
     // Check the CHAP Name and Response replied by Target.
c4e3b2
-- 
c4e3b2
2.27.0
c4e3b2