audk/NetworkPkg
Laszlo Ersek 4cca792399 NetworkPkg/HttpDxe: fix 32-bit truncation in HTTPS download
When downloading over TLS, each TLS message ("APP packet") is returned as
a (decrypted) fragment table by EFI_TLS_PROTOCOL.ProcessPacket().

The TlsProcessMessage() function in "NetworkPkg/HttpDxe/HttpsSupport.c"
linearizes the fragment table into a single contiguous data block. The
resultant flat data block contains both TLS headers and data.

The HttpsReceive() function parses the actual application data -- in this
case: decrypted HTTP data -- out of the flattened TLS data block, peeling
off the TLS headers.

The HttpResponseWorker() function in "NetworkPkg/HttpDxe/HttpImpl.c"
propagates this HTTP data outwards, implementing the
EFI_HTTP_PROTOCOL.Response() function.

Now consider the following documentation for EFI_HTTP_PROTOCOL.Response(),
quoted from "MdePkg/Include/Protocol/Http.h":

> It is the responsibility of the caller to allocate a buffer for Body and
> specify the size in BodyLength. If the remote host provides a response
> that contains a content body, up to BodyLength bytes will be copied from
> the receive buffer into Body and BodyLength will be updated with the
> amount of bytes received and copied to Body. This allows the client to
> download a large file in chunks instead of into one contiguous block of
> memory.

Note that, if the caller-allocated buffer is larger than the
server-provided chunk, then the transfer length is limited by the latter.
This is in fact the dominant case when downloading a huge file (for which
UefiBootManagerLib allocated a huge contiguous RAM Disk buffer) in small
TLS messages.

For adjusting BodyLength as described above -- i.e., to the application
data chunk that has been extracted from the TLS message --, the
HttpResponseWorker() function employs the following assignment:

    HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength);

The (UINT32) cast is motivated by the MIN() requirement -- in
"MdePkg/Include/Base.h" -- that both arguments be of the same type.

"Fragment.Len" (NET_FRAGMENT.Len) has type UINT32, and
"HttpMsg->BodyLength" (EFI_HTTP_MESSAGE.BodyLength) has type UINTN.
Therefore a cast is indeed necessary.

Unfortunately, the cast is done in the wrong direction. Consider the
following circumstances:

- "Fragment.Len" happens to be consistently 16KiB, dictated by the HTTPS
  Server's TLS stack,

- the size of the file to download is 4GiB + N*16KiB, where N is a
  positive integer.

As the download progresses, each received 16KiB application data chunk
brings the *next* input value of BodyLength closer down to 4GiB. The cast
in MIN() always masks off the high-order bits from the input value of
BodyLength, but this is no problem because the low-order bits are nonzero,
therefore the MIN() always permits progress.

However, once BodyLength reaches 4GiB exactly on input, the MIN()
invocation produces a zero value. HttpResponseWorker() adjusts the output
value of BodyLength to zero, and then passes it to HttpParseMessageBody().

HttpParseMessageBody() (in "NetworkPkg/Library/DxeHttpLib/DxeHttpLib.c")
rejects the zero BodyLength with EFI_INVALID_PARAMETER, which is fully
propagated outwards, and aborts the HTTPS download. HttpBootDxe writes the
message "Error: Unexpected network error" to the UEFI console.

For example, a file with size (4GiB + 197MiB) terminates after downloading
just 197MiB.

Invert the direction of the cast: widen "Fragment.Len" to UINTN.

Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Maciej Rabeda <maciej.rabeda@linux.intel.com>
Cc: Siyuan Fu <siyuan.fu@intel.com>
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>
Reviewed-by: Siyuan Fu <siyuan.fu@intel.com>
Reviewed-by: Maciej Rabeda <maciej.rabeda@linux.intel.com>
2020-01-14 10:53:42 +00:00
..
Application/VConfig NetworkPkg: Move Network library header file from MdeModulePkg to NetworkPkg 2019-05-27 09:25:18 +08:00
ArpDxe NetworkPkg: Remove unnecessary MdeModulePkg/MdeModulePkg.dec dependency in INF 2019-10-08 09:20:34 +08:00
Dhcp4Dxe NetworkPkg: Remove unnecessary MdeModulePkg/MdeModulePkg.dec dependency in INF 2019-10-08 09:20:34 +08:00
Dhcp6Dxe NetworkPkg: Fixes to static code analysis hits 2019-12-04 01:16:45 +00:00
DnsDxe NetworkPkg: fix CloseProtocol & UninstallMultipleProtocolInterfaces calls 2019-10-09 09:40:10 +02:00
DpcDxe NetworkPkg: Move Dpc.h from MdeModulePkg to NetworkPkg 2019-08-15 16:42:43 +08:00
HttpBootDxe
HttpDxe NetworkPkg/HttpDxe: fix 32-bit truncation in HTTPS download 2020-01-14 10:53:42 +00:00
HttpUtilitiesDxe NetworkPkg: Remove unnecessary MdeModulePkg/MdeModulePkg.dec dependency in INF 2019-10-08 09:20:34 +08:00
IScsiDxe NetworkPkg: fix CloseProtocol & UninstallMultipleProtocolInterfaces calls 2019-10-09 09:40:10 +02:00
Include NetworkPkg: Move network related header files from MdeModulePkg to NetworkPkg 2019-10-08 09:20:32 +08:00
Ip4Dxe NetworkPkg: Fixes to static code analysis hits 2019-12-04 01:16:45 +00:00
Ip6Dxe NetworkPkg/Ip6Dxe: Fix typo in comment 2019-10-17 09:09:26 +08:00
Library NetworkPkg/DxeNetLib: fix type typo in NetLibGetMacAddress() 2019-10-09 09:40:10 +02:00
MnpDxe NetworkPkg: Remove unnecessary MdeModulePkg/MdeModulePkg.dec dependency in INF 2019-10-08 09:20:34 +08:00
Mtftp4Dxe NetworkPkg: fix CloseProtocol & UninstallMultipleProtocolInterfaces calls 2019-10-09 09:40:10 +02:00
Mtftp6Dxe NetworkPkg: Remove unnecessary MdeModulePkg/MdeModulePkg.dec dependency in INF 2019-10-08 09:20:34 +08:00
SnpDxe NetworkPkg/SnpDxe: Use PcdGetBool() instead of FixedPcdGetBool in Snp.c 2019-10-29 17:39:10 +08:00
TcpDxe NetworkPkg: Fixes to static code analysis hits 2019-12-04 01:16:45 +00:00
TlsAuthConfigDxe NetworkPkg: fix CloseProtocol & UninstallMultipleProtocolInterfaces calls 2019-10-09 09:40:10 +02:00
TlsDxe NetworkPkg/TlsDxe: Add the support of host validation to TlsDxe driver (CVE-2019-14553) 2019-11-02 12:08:19 +01:00
Udp4Dxe NetworkPkg: Remove unnecessary MdeModulePkg/MdeModulePkg.dec dependency in INF 2019-10-08 09:20:34 +08:00
Udp6Dxe NetworkPkg: Remove unnecessary MdeModulePkg/MdeModulePkg.dec dependency in INF 2019-10-08 09:20:34 +08:00
UefiPxeBcDxe NetworkPkg: Remove unnecessary MdeModulePkg/MdeModulePkg.dec dependency in INF 2019-10-08 09:20:34 +08:00
VlanConfigDxe NetworkPkg: Move Network library header file from MdeModulePkg to NetworkPkg 2019-05-27 09:25:18 +08:00
WifiConnectionManagerDxe NetworkPkg: add missing newline at end of file 2019-10-04 11:18:22 +01:00
Network.dsc.inc NetworkPkg: Add package level include DSC file 2019-05-08 16:10:39 +08:00
Network.fdf.inc NetworkPkg: Move Network library and drivers from MdeModulePkg to NetworkPkg 2019-05-27 09:25:18 +08:00
NetworkComponents.dsc.inc NetworkPkg: Move Network library and drivers from MdeModulePkg to NetworkPkg 2019-05-27 09:25:18 +08:00
NetworkDefines.dsc.inc NetworkPkg: Add DSC/FDF include segment files to NetworkPkg. 2019-05-08 16:10:39 +08:00
NetworkLibs.dsc.inc NetworkPkg: Move Network library and drivers from MdeModulePkg to NetworkPkg 2019-05-27 09:25:18 +08:00
NetworkPcds.dsc.inc NetworkPkg: Add DSC/FDF include segment files to NetworkPkg. 2019-05-08 16:10:39 +08:00
NetworkPkg.ci.yaml NetworkPkg: Add YAML file for CI builds 2019-11-11 13:02:24 -08:00
NetworkPkg.dec NetworkPkg/SnpDxe: Add PCD to remove ExitBootServices event from SNP driver. 2019-10-21 14:50:51 +08:00
NetworkPkg.dsc NetworkPkg: Add YAML file for CI builds 2019-11-11 13:02:24 -08:00
NetworkPkg.uni NetworkPkg/NetworkPkg.uni: Add missing strings for PCD 2019-12-04 05:05:57 +00:00
NetworkPkgExtra.uni