render / rpms / edk2

Forked from rpms/edk2 2 months ago
Clone

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

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