Blame SOURCES/edk2-NetworkPkg-HttpDxe-fix-32-bit-truncation-in-HTTPS-do.patch

6009e6
From 555d93f2daa551dc2311b15210a918aa79ed18ff Mon Sep 17 00:00:00 2001
6009e6
From: Laszlo Ersek <lersek@redhat.com>
6009e6
Date: Tue, 14 Jan 2020 12:39:06 +0100
6009e6
Subject: [PATCH 2/2] NetworkPkg/HttpDxe: fix 32-bit truncation in HTTPS
6009e6
 download
6009e6
MIME-Version: 1.0
6009e6
Content-Type: text/plain; charset=UTF-8
6009e6
Content-Transfer-Encoding: 8bit
6009e6
6009e6
RH-Author: Laszlo Ersek <lersek@redhat.com>
6009e6
Message-id: <20200114123906.8547-3-lersek@redhat.com>
6009e6
Patchwork-id: 93340
6009e6
O-Subject: [RHEL-8.2.0 edk2 PATCH 2/2] NetworkPkg/HttpDxe: fix 32-bit truncation in HTTPS download
6009e6
Bugzilla: 1789797
6009e6
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
6009e6
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
6009e6
6009e6
When downloading over TLS, each TLS message ("APP packet") is returned as
6009e6
a (decrypted) fragment table by EFI_TLS_PROTOCOL.ProcessPacket().
6009e6
6009e6
The TlsProcessMessage() function in "NetworkPkg/HttpDxe/HttpsSupport.c"
6009e6
linearizes the fragment table into a single contiguous data block. The
6009e6
resultant flat data block contains both TLS headers and data.
6009e6
6009e6
The HttpsReceive() function parses the actual application data -- in this
6009e6
case: decrypted HTTP data -- out of the flattened TLS data block, peeling
6009e6
off the TLS headers.
6009e6
6009e6
The HttpResponseWorker() function in "NetworkPkg/HttpDxe/HttpImpl.c"
6009e6
propagates this HTTP data outwards, implementing the
6009e6
EFI_HTTP_PROTOCOL.Response() function.
6009e6
6009e6
Now consider the following documentation for EFI_HTTP_PROTOCOL.Response(),
6009e6
quoted from "MdePkg/Include/Protocol/Http.h":
6009e6
6009e6
> It is the responsibility of the caller to allocate a buffer for Body and
6009e6
> specify the size in BodyLength. If the remote host provides a response
6009e6
> that contains a content body, up to BodyLength bytes will be copied from
6009e6
> the receive buffer into Body and BodyLength will be updated with the
6009e6
> amount of bytes received and copied to Body. This allows the client to
6009e6
> download a large file in chunks instead of into one contiguous block of
6009e6
> memory.
6009e6
6009e6
Note that, if the caller-allocated buffer is larger than the
6009e6
server-provided chunk, then the transfer length is limited by the latter.
6009e6
This is in fact the dominant case when downloading a huge file (for which
6009e6
UefiBootManagerLib allocated a huge contiguous RAM Disk buffer) in small
6009e6
TLS messages.
6009e6
6009e6
For adjusting BodyLength as described above -- i.e., to the application
6009e6
data chunk that has been extracted from the TLS message --, the
6009e6
HttpResponseWorker() function employs the following assignment:
6009e6
6009e6
    HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength);
6009e6
6009e6
The (UINT32) cast is motivated by the MIN() requirement -- in
6009e6
"MdePkg/Include/Base.h" -- that both arguments be of the same type.
6009e6
6009e6
"Fragment.Len" (NET_FRAGMENT.Len) has type UINT32, and
6009e6
"HttpMsg->BodyLength" (EFI_HTTP_MESSAGE.BodyLength) has type UINTN.
6009e6
Therefore a cast is indeed necessary.
6009e6
6009e6
Unfortunately, the cast is done in the wrong direction. Consider the
6009e6
following circumstances:
6009e6
6009e6
- "Fragment.Len" happens to be consistently 16KiB, dictated by the HTTPS
6009e6
  Server's TLS stack,
6009e6
6009e6
- the size of the file to download is 4GiB + N*16KiB, where N is a
6009e6
  positive integer.
6009e6
6009e6
As the download progresses, each received 16KiB application data chunk
6009e6
brings the *next* input value of BodyLength closer down to 4GiB. The cast
6009e6
in MIN() always masks off the high-order bits from the input value of
6009e6
BodyLength, but this is no problem because the low-order bits are nonzero,
6009e6
therefore the MIN() always permits progress.
6009e6
6009e6
However, once BodyLength reaches 4GiB exactly on input, the MIN()
6009e6
invocation produces a zero value. HttpResponseWorker() adjusts the output
6009e6
value of BodyLength to zero, and then passes it to HttpParseMessageBody().
6009e6
6009e6
HttpParseMessageBody() (in "NetworkPkg/Library/DxeHttpLib/DxeHttpLib.c")
6009e6
rejects the zero BodyLength with EFI_INVALID_PARAMETER, which is fully
6009e6
propagated outwards, and aborts the HTTPS download. HttpBootDxe writes the
6009e6
message "Error: Unexpected network error" to the UEFI console.
6009e6
6009e6
For example, a file with size (4GiB + 197MiB) terminates after downloading
6009e6
just 197MiB.
6009e6
6009e6
Invert the direction of the cast: widen "Fragment.Len" to UINTN.
6009e6
6009e6
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
6009e6
Cc: Maciej Rabeda <maciej.rabeda@linux.intel.com>
6009e6
Cc: Siyuan Fu <siyuan.fu@intel.com>
6009e6
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
6009e6
Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>
6009e6
Reviewed-by: Siyuan Fu <siyuan.fu@intel.com>
6009e6
Reviewed-by: Maciej Rabeda <maciej.rabeda@linux.intel.com>
6009e6
(cherry picked from commit 4cca7923992a13f6b753782f469ee944da2db796)
6009e6
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
6009e6
---
6009e6
 NetworkPkg/HttpDxe/HttpImpl.c | 2 +-
6009e6
 1 file changed, 1 insertion(+), 1 deletion(-)
6009e6
6009e6
diff --git a/NetworkPkg/HttpDxe/HttpImpl.c b/NetworkPkg/HttpDxe/HttpImpl.c
6009e6
index 6b87731..1acbb60 100644
6009e6
--- a/NetworkPkg/HttpDxe/HttpImpl.c
6009e6
+++ b/NetworkPkg/HttpDxe/HttpImpl.c
6009e6
@@ -1348,7 +1348,7 @@ HttpResponseWorker (
6009e6
     //
6009e6
     // Process the received the body packet.
6009e6
     //
6009e6
-    HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength);
6009e6
+    HttpMsg->BodyLength = MIN ((UINTN) Fragment.Len, HttpMsg->BodyLength);
6009e6
 
6009e6
     CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);
6009e6
 
6009e6
-- 
6009e6
1.8.3.1
6009e6