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

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