NetworkPkg: Dhcp6Dxe: SECURITY PATCH CVE-2023-45230 Patch

REF:https://bugzilla.tianocore.org/show_bug.cgi?id=4535

Bug Details:
PixieFail Bug #2
CVE-2023-45230
CVSS 8.3 : CVSS:3.1/AV:A/AC:L/PR:N/UI:N/S:U/C:H/I:L/A:H
CWE-119 Improper Restriction of Operations within the Bounds
 of a Memory Buffer

Changes Overview:
> -UINT8 *
> +EFI_STATUS
>  Dhcp6AppendOption (
> -  IN OUT UINT8   *Buf,
> -  IN     UINT16  OptType,
> -  IN     UINT16  OptLen,
> -  IN     UINT8   *Data
> +  IN OUT EFI_DHCP6_PACKET  *Packet,
> +  IN OUT UINT8             **PacketCursor,
> +  IN     UINT16            OptType,
> +  IN     UINT16            OptLen,
> +  IN     UINT8             *Data
>    );

Dhcp6AppendOption() and variants can return errors now.  All callsites
are adapted accordingly.

It gets passed in EFI_DHCP6_PACKET as additional parameter ...

> +  //
> +  // Verify the PacketCursor is within the packet
> +  //
> +  if (  (*PacketCursor < Packet->Dhcp6.Option)
> +     || (*PacketCursor >= Packet->Dhcp6.Option +
 (Packet->Size - sizeof (EFI_DHCP6_HEADER))))
> +  {
> +    return EFI_INVALID_PARAMETER;
> +  }

... so it can look at Packet->Size when checking buffer space.
Also to allow Packet->Length updates.

Lots of checks added.

Cc: Saloni Kasbekar <saloni.kasbekar@intel.com>
Cc: Zachary Clark-williams <zachary.clark-williams@intel.com>

Signed-off-by: Doug Flick [MSFT] <doug.edk2@gmail.com>
Reviewed-by: Saloni Kasbekar <saloni.kasbekar@intel.com>
This commit is contained in:
Doug Flick via groups.io
2024-01-26 05:54:43 +08:00
committed by mergify[bot]
parent 959f71c801
commit f31453e8d6
4 changed files with 666 additions and 237 deletions

View File

@@ -3,9 +3,9 @@
(C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "Dhcp6Impl.h"
@@ -930,7 +930,8 @@ Dhcp6SendSolicitMsg (
//
Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
if (Packet == NULL) {
return EFI_OUT_OF_RESOURCES;
Status = EFI_OUT_OF_RESOURCES;
goto ON_ERROR;
}
Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
@@ -944,54 +945,64 @@ Dhcp6SendSolicitMsg (
Cursor = Packet->Dhcp6.Option;
Length = HTONS (ClientId->Length);
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptClientId),
Length,
ClientId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendETOption (
Cursor,
Status = Dhcp6AppendETOption (
Packet,
&Cursor,
Instance,
&Elapsed
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendIaOption (
Cursor,
Status = Dhcp6AppendIaOption (
Packet,
&Cursor,
Instance->IaCb.Ia,
Instance->IaCb.T1,
Instance->IaCb.T2,
Packet->Dhcp6.Header.MessageType
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
//
// Append user-defined when configurate Dhcp6 service.
//
for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
UserOpt = Instance->Config->OptionList[Index];
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
UserOpt->OpCode,
UserOpt->OpLen,
UserOpt->Data
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
}
//
// Determine the size/length of packet.
//
Packet->Length += (UINT32)(Cursor - Packet->Dhcp6.Option);
ASSERT (Packet->Size > Packet->Length + 8);
//
// Callback to user with the packet to be sent and check the user's feedback.
//
Status = Dhcp6CallbackUser (Instance, Dhcp6SendSolicit, &Packet);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
@@ -1005,10 +1016,8 @@ Dhcp6SendSolicitMsg (
Instance->StartTime = 0;
Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
@@ -1020,6 +1029,14 @@ Dhcp6SendSolicitMsg (
Elapsed,
Instance->Config->SolicitRetransmission
);
ON_ERROR:
if (Packet) {
FreePool (Packet);
}
return Status;
}
/**
@@ -1110,7 +1127,8 @@ Dhcp6SendRequestMsg (
//
Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
if (Packet == NULL) {
return EFI_OUT_OF_RESOURCES;
Status = EFI_OUT_OF_RESOURCES;
goto ON_ERROR;
}
Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
@@ -1124,51 +1142,67 @@ Dhcp6SendRequestMsg (
Cursor = Packet->Dhcp6.Option;
Length = HTONS (ClientId->Length);
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptClientId),
Length,
ClientId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendETOption (
Cursor,
Status = Dhcp6AppendETOption (
Packet,
&Cursor,
Instance,
&Elapsed
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptServerId),
ServerId->Length,
ServerId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendIaOption (
Cursor,
Status = Dhcp6AppendIaOption (
Packet,
&Cursor,
Instance->IaCb.Ia,
Instance->IaCb.T1,
Instance->IaCb.T2,
Packet->Dhcp6.Header.MessageType
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
//
// Append user-defined when configurate Dhcp6 service.
//
for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
UserOpt = Instance->Config->OptionList[Index];
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
UserOpt->OpCode,
UserOpt->OpLen,
UserOpt->Data
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
}
//
// Determine the size/length of packet.
//
Packet->Length += (UINT32)(Cursor - Packet->Dhcp6.Option);
ASSERT (Packet->Size > Packet->Length + 8);
//
@@ -1177,8 +1211,7 @@ Dhcp6SendRequestMsg (
Status = Dhcp6CallbackUser (Instance, Dhcp6SendRequest, &Packet);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
@@ -1194,14 +1227,21 @@ Dhcp6SendRequestMsg (
Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
// Enqueue the sent packet for the retransmission in case reply timeout.
//
return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
ON_ERROR:
if (Packet) {
FreePool (Packet);
}
return Status;
}
/**
@@ -1266,7 +1306,8 @@ Dhcp6SendDeclineMsg (
//
Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE);
if (Packet == NULL) {
return EFI_OUT_OF_RESOURCES;
Status = EFI_OUT_OF_RESOURCES;
goto ON_ERROR;
}
Packet->Size = DHCP6_BASE_PACKET_SIZE;
@@ -1280,42 +1321,58 @@ Dhcp6SendDeclineMsg (
Cursor = Packet->Dhcp6.Option;
Length = HTONS (ClientId->Length);
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptClientId),
Length,
ClientId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendETOption (
Cursor,
Status = Dhcp6AppendETOption (
Packet,
&Cursor,
Instance,
&Elapsed
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptServerId),
ServerId->Length,
ServerId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendIaOption (Cursor, DecIa, 0, 0, Packet->Dhcp6.Header.MessageType);
Status = Dhcp6AppendIaOption (
Packet,
&Cursor,
DecIa,
0,
0,
Packet->Dhcp6.Header.MessageType
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
//
// Determine the size/length of packet.
//
Packet->Length += (UINT32)(Cursor - Packet->Dhcp6.Option);
ASSERT (Packet->Size > Packet->Length + 8);
//
// Callback to user with the packet to be sent and check the user's feedback.
//
Status = Dhcp6CallbackUser (Instance, Dhcp6SendDecline, &Packet);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
@@ -1329,16 +1386,22 @@ Dhcp6SendDeclineMsg (
Instance->StartTime = 0;
Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
// Enqueue the sent packet for the retransmission in case reply timeout.
//
return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
ON_ERROR:
if (Packet) {
FreePool (Packet);
}
return Status;
}
/**
@@ -1399,7 +1462,8 @@ Dhcp6SendReleaseMsg (
//
Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE);
if (Packet == NULL) {
return EFI_OUT_OF_RESOURCES;
Status = EFI_OUT_OF_RESOURCES;
goto ON_ERROR;
}
Packet->Size = DHCP6_BASE_PACKET_SIZE;
@@ -1413,45 +1477,61 @@ Dhcp6SendReleaseMsg (
Cursor = Packet->Dhcp6.Option;
Length = HTONS (ClientId->Length);
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptClientId),
Length,
ClientId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
//
// ServerId is extracted from packet, it's network order.
//
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptServerId),
ServerId->Length,
ServerId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendETOption (
Cursor,
Status = Dhcp6AppendETOption (
Packet,
&Cursor,
Instance,
&Elapsed
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendIaOption (Cursor, RelIa, 0, 0, Packet->Dhcp6.Header.MessageType);
Status = Dhcp6AppendIaOption (
Packet,
&Cursor,
RelIa,
0,
0,
Packet->Dhcp6.Header.MessageType
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
//
// Determine the size/length of packet
//
Packet->Length += (UINT32)(Cursor - Packet->Dhcp6.Option);
ASSERT (Packet->Size > Packet->Length + 8);
//
// Callback to user with the packet to be sent and check the user's feedback.
//
Status = Dhcp6CallbackUser (Instance, Dhcp6SendRelease, &Packet);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
@@ -1461,16 +1541,22 @@ Dhcp6SendReleaseMsg (
Instance->IaCb.Ia->State = Dhcp6Releasing;
Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
// Enqueue the sent packet for the retransmission in case reply timeout.
//
return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
ON_ERROR:
if (Packet) {
FreePool (Packet);
}
return Status;
}
/**
@@ -1529,7 +1615,8 @@ Dhcp6SendRenewRebindMsg (
//
Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
if (Packet == NULL) {
return EFI_OUT_OF_RESOURCES;
Status = EFI_OUT_OF_RESOURCES;
goto ON_ERROR;
}
Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
@@ -1543,26 +1630,38 @@ Dhcp6SendRenewRebindMsg (
Cursor = Packet->Dhcp6.Option;
Length = HTONS (ClientId->Length);
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptClientId),
Length,
ClientId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendETOption (
Cursor,
Status = Dhcp6AppendETOption (
Packet,
&Cursor,
Instance,
&Elapsed
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendIaOption (
Cursor,
Status = Dhcp6AppendIaOption (
Packet,
&Cursor,
Instance->IaCb.Ia,
Instance->IaCb.T1,
Instance->IaCb.T2,
Packet->Dhcp6.Header.MessageType
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
if (!RebindRequest) {
//
@@ -1578,18 +1677,22 @@ Dhcp6SendRenewRebindMsg (
Dhcp6OptServerId
);
if (Option == NULL) {
FreePool (Packet);
return EFI_DEVICE_ERROR;
Status = EFI_DEVICE_ERROR;
goto ON_ERROR;
}
ServerId = (EFI_DHCP6_DUID *)(Option + 2);
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptServerId),
ServerId->Length,
ServerId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
}
//
@@ -1597,18 +1700,18 @@ Dhcp6SendRenewRebindMsg (
//
for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
UserOpt = Instance->Config->OptionList[Index];
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
UserOpt->OpCode,
UserOpt->OpLen,
UserOpt->Data
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
}
//
// Determine the size/length of packet.
//
Packet->Length += (UINT32)(Cursor - Packet->Dhcp6.Option);
ASSERT (Packet->Size > Packet->Length + 8);
//
@@ -1618,10 +1721,8 @@ Dhcp6SendRenewRebindMsg (
Event = (RebindRequest) ? Dhcp6EnterRebinding : Dhcp6EnterRenewing;
Status = Dhcp6CallbackUser (Instance, Event, &Packet);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
@@ -1638,16 +1739,22 @@ Dhcp6SendRenewRebindMsg (
Instance->StartTime = 0;
Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
// Enqueue the sent packet for the retransmission in case reply timeout.
//
return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
ON_ERROR:
if (Packet) {
FreePool (Packet);
}
return Status;
}
/**
@@ -1811,7 +1918,8 @@ Dhcp6SendInfoRequestMsg (
//
Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
if (Packet == NULL) {
return EFI_OUT_OF_RESOURCES;
Status = EFI_OUT_OF_RESOURCES;
goto ON_ERROR;
}
Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
@@ -1828,44 +1936,56 @@ Dhcp6SendInfoRequestMsg (
if (SendClientId) {
Length = HTONS (ClientId->Length);
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptClientId),
Length,
ClientId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
}
Cursor = Dhcp6AppendETOption (
Cursor,
Status = Dhcp6AppendETOption (
Packet,
&Cursor,
Instance,
&Elapsed
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
OptionRequest->OpCode,
OptionRequest->OpLen,
OptionRequest->Data
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
//
// Append user-defined when configurate Dhcp6 service.
//
for (Index = 0; Index < OptionCount; Index++) {
UserOpt = OptionList[Index];
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
UserOpt->OpCode,
UserOpt->OpLen,
UserOpt->Data
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
}
//
// Determine the size/length of packet.
//
Packet->Length += (UINT32)(Cursor - Packet->Dhcp6.Option);
ASSERT (Packet->Size > Packet->Length + 8);
//
@@ -1877,16 +1997,22 @@ Dhcp6SendInfoRequestMsg (
// Send info-request packet with no state.
//
Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
// Enqueue the sent packet for the retransmission in case reply timeout.
//
return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, Retransmission);
ON_ERROR:
if (Packet) {
FreePool (Packet);
}
return Status;
}
/**
@@ -1937,7 +2063,8 @@ Dhcp6SendConfirmMsg (
//
Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
if (Packet == NULL) {
return EFI_OUT_OF_RESOURCES;
Status = EFI_OUT_OF_RESOURCES;
goto ON_ERROR;
}
Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
@@ -1951,54 +2078,64 @@ Dhcp6SendConfirmMsg (
Cursor = Packet->Dhcp6.Option;
Length = HTONS (ClientId->Length);
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
HTONS (Dhcp6OptClientId),
Length,
ClientId->Duid
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendETOption (
Cursor,
Status = Dhcp6AppendETOption (
Packet,
&Cursor,
Instance,
&Elapsed
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
Cursor = Dhcp6AppendIaOption (
Cursor,
Status = Dhcp6AppendIaOption (
Packet,
&Cursor,
Instance->IaCb.Ia,
Instance->IaCb.T1,
Instance->IaCb.T2,
Packet->Dhcp6.Header.MessageType
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
//
// Append user-defined when configurate Dhcp6 service.
//
for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
UserOpt = Instance->Config->OptionList[Index];
Cursor = Dhcp6AppendOption (
Cursor,
Status = Dhcp6AppendOption (
Packet,
&Cursor,
UserOpt->OpCode,
UserOpt->OpLen,
UserOpt->Data
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
}
//
// Determine the size/length of packet.
//
Packet->Length += (UINT32)(Cursor - Packet->Dhcp6.Option);
ASSERT (Packet->Size > Packet->Length + 8);
//
// Callback to user with the packet to be sent and check the user's feedback.
//
Status = Dhcp6CallbackUser (Instance, Dhcp6SendConfirm, &Packet);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
@@ -2012,16 +2149,22 @@ Dhcp6SendConfirmMsg (
Instance->StartTime = 0;
Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
if (EFI_ERROR (Status)) {
FreePool (Packet);
return Status;
goto ON_ERROR;
}
//
// Enqueue the sent packet for the retransmission in case reply timeout.
//
return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
ON_ERROR:
if (Packet) {
FreePool (Packet);
}
return Status;
}
/**