Skip to content

Commit 5883437

Browse files
FlickdmFlickdm
authored and
Flickdm
committed
NetworkPkg:: SECURITY PATCH CVE-2023-45237
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=4542 Bug Overview: PixieFail Bug tianocore#9 CVE-2023-45237 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N CWE-338 Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG) Use of a Weak PseudoRandom Number Generator Change Overview: Updates all Instances of NET_RANDOM (NetRandomInitSeed ()) to either > > EFI_STATUS > EFIAPI > PseudoRandomU32 ( > OUT UINT32 *Output > ); > or (depending on the use case) > > EFI_STATUS > EFIAPI > PseudoRandom ( > OUT VOID *Output, > IN UINTN OutputLength > ); > This is because the use of Example: The following code snippet PseudoRandomU32 () function is used: > > UINT32 Random; > > Status = PseudoRandomU32 (&Random); > if (EFI_ERROR (Status)) { > DEBUG ((DEBUG_ERROR, "%a failed to generate random number: %r\n", __func__, Status)); > return Status; > } > This also introduces a new PCD to enable/disable the use of the secure implementation of algorithms for PseudoRandom () and instead depend on the default implementation. This may be required for some platforms where the UEFI Spec defined algorithms are not available. > > PcdEnforceSecureRngAlgorithms > If the platform does not have any one of the UEFI defined secure RNG algorithms then the driver will assert. Cc: Saloni Kasbekar <[email protected]> Cc: Zachary Clark-williams <[email protected]> Signed-off-by: Doug Flick [MSFT] <[email protected]>
1 parent d83d342 commit 5883437

27 files changed

+410
-83
lines changed

NetworkPkg/Dhcp4Dxe/Dhcp4Driver.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** @file
22
33
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
4+
Copyright (c) Microsoft Corporation
45
SPDX-License-Identifier: BSD-2-Clause-Patent
56
67
**/
@@ -189,6 +190,13 @@ Dhcp4CreateService (
189190
{
190191
DHCP_SERVICE *DhcpSb;
191192
EFI_STATUS Status;
193+
UINT32 Random;
194+
195+
Status = PseudoRandomU32 (&Random);
196+
if (EFI_ERROR (Status)) {
197+
DEBUG ((DEBUG_ERROR, "%a failed to generate random number: %r\n", __func__, Status));
198+
return Status;
199+
}
192200

193201
*Service = NULL;
194202
DhcpSb = AllocateZeroPool (sizeof (DHCP_SERVICE));
@@ -203,7 +211,7 @@ Dhcp4CreateService (
203211
DhcpSb->Image = ImageHandle;
204212
InitializeListHead (&DhcpSb->Children);
205213
DhcpSb->DhcpState = Dhcp4Stopped;
206-
DhcpSb->Xid = NET_RANDOM (NetRandomInitSeed ());
214+
DhcpSb->Xid = Random;
207215
CopyMem (
208216
&DhcpSb->ServiceBinding,
209217
&mDhcp4ServiceBindingTemplate,

NetworkPkg/Dhcp6Dxe/Dhcp6Driver.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
implementation for Dhcp6 Driver.
44
55
Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6-
6+
Copyright (c) Microsoft Corporation
77
SPDX-License-Identifier: BSD-2-Clause-Patent
88
99
**/
@@ -123,6 +123,13 @@ Dhcp6CreateService (
123123
{
124124
DHCP6_SERVICE *Dhcp6Srv;
125125
EFI_STATUS Status;
126+
UINT32 Random;
127+
128+
Status = PseudoRandomU32 (&Random);
129+
if (EFI_ERROR (Status)) {
130+
DEBUG ((DEBUG_ERROR, "%a failed to generate random number: %r\n", __func__, Status));
131+
return Status;
132+
}
126133

127134
*Service = NULL;
128135
Dhcp6Srv = AllocateZeroPool (sizeof (DHCP6_SERVICE));
@@ -147,7 +154,7 @@ Dhcp6CreateService (
147154
Dhcp6Srv->Signature = DHCP6_SERVICE_SIGNATURE;
148155
Dhcp6Srv->Controller = Controller;
149156
Dhcp6Srv->Image = ImageHandle;
150-
Dhcp6Srv->Xid = (0xffffff & NET_RANDOM (NetRandomInitSeed ()));
157+
Dhcp6Srv->Xid = (0xffffff & Random);
151158

152159
CopyMem (
153160
&Dhcp6Srv->ServiceBinding,

NetworkPkg/DnsDxe/DnsDhcp.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Functions implementation related with DHCPv4/v6 for DNS driver.
33
44
Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5+
Copyright (c) Microsoft Corporation
56
SPDX-License-Identifier: BSD-2-Clause-Patent
67
78
**/
@@ -277,6 +278,7 @@ GetDns4ServerFromDhcp4 (
277278
EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token;
278279
BOOLEAN IsDone;
279280
UINTN Index;
281+
UINT32 Random;
280282

281283
Image = Instance->Service->ImageHandle;
282284
Controller = Instance->Service->ControllerHandle;
@@ -292,6 +294,12 @@ GetDns4ServerFromDhcp4 (
292294
Data = NULL;
293295
InterfaceInfo = NULL;
294296

297+
Status = PseudoRandomU32 (&Random);
298+
if (EFI_ERROR (Status)) {
299+
DEBUG ((DEBUG_ERROR, "%a failed to generate random number: %r\n", __func__, Status));
300+
return Status;
301+
}
302+
295303
ZeroMem ((UINT8 *)ParaList, sizeof (ParaList));
296304

297305
ZeroMem (&MnpConfigData, sizeof (EFI_MANAGED_NETWORK_CONFIG_DATA));
@@ -467,7 +475,7 @@ GetDns4ServerFromDhcp4 (
467475

468476
Status = Dhcp4->Build (Dhcp4, &SeedPacket, 0, NULL, 2, ParaList, &Token.Packet);
469477

470-
Token.Packet->Dhcp4.Header.Xid = HTONL (NET_RANDOM (NetRandomInitSeed ()));
478+
Token.Packet->Dhcp4.Header.Xid = Random;
471479

472480
Token.Packet->Dhcp4.Header.Reserved = HTONS ((UINT16)0x8000);
473481

NetworkPkg/DnsDxe/DnsImpl.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
DnsDxe support functions implementation.
33
44
Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5+
Copyright (c) Microsoft Corporation
56
SPDX-License-Identifier: BSD-2-Clause-Patent
67
78
**/
@@ -1963,6 +1964,14 @@ ConstructDNSQuery (
19631964
NET_FRAGMENT Frag;
19641965
DNS_HEADER *DnsHeader;
19651966
DNS_QUERY_SECTION *DnsQuery;
1967+
EFI_STATUS Status;
1968+
UINT32 Random;
1969+
1970+
Status = PseudoRandomU32 (&Random);
1971+
if (EFI_ERROR (Status)) {
1972+
DEBUG ((DEBUG_ERROR, "%a failed to generate random number: %r\n", __func__, Status));
1973+
return Status;
1974+
}
19661975

19671976
//
19681977
// Messages carried by UDP are restricted to 512 bytes (not counting the IP
@@ -1977,7 +1986,7 @@ ConstructDNSQuery (
19771986
// Fill header
19781987
//
19791988
DnsHeader = (DNS_HEADER *)Frag.Bulk;
1980-
DnsHeader->Identification = (UINT16)NET_RANDOM (NetRandomInitSeed ());
1989+
DnsHeader->Identification = (UINT16)Random;
19811990
DnsHeader->Flags.Uint16 = 0x0000;
19821991
DnsHeader->Flags.Bits.RD = 1;
19831992
DnsHeader->Flags.Bits.OpCode = DNS_FLAGS_OPCODE_STANDARD;

NetworkPkg/HttpBootDxe/HttpBootDhcp6.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Functions implementation related with DHCPv6 for HTTP boot driver.
33
44
Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5+
Copyright (c) Microsoft Corporation
56
SPDX-License-Identifier: BSD-2-Clause-Patent
67
78
**/
@@ -951,6 +952,7 @@ HttpBootDhcp6Sarr (
951952
UINT32 OptCount;
952953
UINT8 Buffer[HTTP_BOOT_DHCP6_OPTION_MAX_SIZE];
953954
EFI_STATUS Status;
955+
UINT32 Random;
954956

955957
Dhcp6 = Private->Dhcp6;
956958
ASSERT (Dhcp6 != NULL);
@@ -961,6 +963,12 @@ HttpBootDhcp6Sarr (
961963
OptCount = HttpBootBuildDhcp6Options (Private, OptList, Buffer);
962964
ASSERT (OptCount > 0);
963965

966+
Status = PseudoRandomU32 (&Random);
967+
if (EFI_ERROR (Status)) {
968+
DEBUG ((DEBUG_ERROR, "%a failed to generate random number: %r\n", __func__, Status));
969+
return Status;
970+
}
971+
964972
Retransmit = AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION));
965973
if (Retransmit == NULL) {
966974
return EFI_OUT_OF_RESOURCES;
@@ -976,7 +984,7 @@ HttpBootDhcp6Sarr (
976984
Config.IaInfoEvent = NULL;
977985
Config.RapidCommit = FALSE;
978986
Config.ReconfigureAccept = FALSE;
979-
Config.IaDescriptor.IaId = NET_RANDOM (NetRandomInitSeed ());
987+
Config.IaDescriptor.IaId = Random;
980988
Config.IaDescriptor.Type = EFI_DHCP6_IA_TYPE_NA;
981989
Config.SolicitRetransmission = Retransmit;
982990
Retransmit->Irt = 4;

NetworkPkg/IScsiDxe/IScsiCHAP.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Configuration.
44
55
Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
6+
Copyright (c) Microsoft Corporation
67
SPDX-License-Identifier: BSD-2-Clause-Patent
78
89
**/
@@ -576,16 +577,24 @@ IScsiCHAPToSendReq (
576577
//
577578
// CHAP_I=<I>
578579
//
579-
IScsiGenRandom ((UINT8 *)&AuthData->OutIdentifier, 1);
580+
Status = IScsiGenRandom ((UINT8 *)&AuthData->OutIdentifier, 1);
581+
if (EFI_ERROR (Status)) {
582+
break;
583+
}
584+
580585
AsciiSPrint (ValueStr, sizeof (ValueStr), "%d", AuthData->OutIdentifier);
581586
IScsiAddKeyValuePair (Pdu, ISCSI_KEY_CHAP_IDENTIFIER, ValueStr);
582587
//
583588
// CHAP_C=<C>
584589
//
585-
IScsiGenRandom (
586-
(UINT8 *)AuthData->OutChallenge,
587-
AuthData->Hash->DigestSize
588-
);
590+
Status = IScsiGenRandom (
591+
(UINT8 *)AuthData->OutChallenge,
592+
AuthData->Hash->DigestSize
593+
);
594+
if (EFI_ERROR (Status)) {
595+
break;
596+
}
597+
589598
BinToHexStatus = IScsiBinToHex (
590599
(UINT8 *)AuthData->OutChallenge,
591600
AuthData->Hash->DigestSize,

NetworkPkg/IScsiDxe/IScsiMisc.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Miscellaneous routines for iSCSI driver.
33
44
Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5+
Copyright (c) Microsoft Corporation
56
SPDX-License-Identifier: BSD-2-Clause-Patent
67
78
**/
@@ -474,20 +475,17 @@ IScsiNetNtoi (
474475
@param[in, out] Rand The buffer to contain random numbers.
475476
@param[in] RandLength The length of the Rand buffer.
476477
478+
@retval EFI_SUCCESS on success
479+
@retval others on error
480+
477481
**/
478-
VOID
482+
EFI_STATUS
479483
IScsiGenRandom (
480484
IN OUT UINT8 *Rand,
481485
IN UINTN RandLength
482486
)
483487
{
484-
UINT32 Random;
485-
486-
while (RandLength > 0) {
487-
Random = NET_RANDOM (NetRandomInitSeed ());
488-
*Rand++ = (UINT8)(Random);
489-
RandLength--;
490-
}
488+
return PseudoRandom (Rand, RandLength);
491489
}
492490

493491
/**

NetworkPkg/IScsiDxe/IScsiMisc.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Miscellaneous definitions for iSCSI driver.
33
44
Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5+
Copyright (c) Microsoft Corporation
56
SPDX-License-Identifier: BSD-2-Clause-Patent
67
78
**/
@@ -202,8 +203,11 @@ IScsiNetNtoi (
202203
@param[in, out] Rand The buffer to contain random numbers.
203204
@param[in] RandLength The length of the Rand buffer.
204205
206+
@retval EFI_SUCCESS on success
207+
@retval others on error
208+
205209
**/
206-
VOID
210+
EFI_STATUS
207211
IScsiGenRandom (
208212
IN OUT UINT8 *Rand,
209213
IN UINTN RandLength

NetworkPkg/Include/Library/NetLib.h

+30-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
It provides basic functions for the UEFI network stack.
44
55
Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
6+
Copyright (c) Microsoft Corporation
67
SPDX-License-Identifier: BSD-2-Clause-Patent
78
89
**/
@@ -539,8 +540,6 @@ extern EFI_IPv4_ADDRESS mZeroIp4Addr;
539540
#define TICKS_PER_MS 10000U
540541
#define TICKS_PER_SECOND 10000000U
541542

542-
#define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)
543-
544543
/**
545544
Extract a UINT32 from a byte stream.
546545
@@ -580,19 +579,40 @@ NetPutUint32 (
580579
);
581580

582581
/**
583-
Initialize a random seed using current time and monotonic count.
582+
Generate a Random output data given a length.
584583
585-
Get current time and monotonic count first. Then initialize a random seed
586-
based on some basic mathematics operation on the hour, day, minute, second,
587-
nanosecond and year of the current time and the monotonic count value.
584+
@param[out] Output - The buffer to store the generated random data.
585+
@param[in] OutputLength - The length of the output buffer.
588586
589-
@return The random seed initialized with current time.
587+
@retval EFI_SUCCESS On Success
588+
@retval EFI_INVALID_PARAMETER Pointer is null or size is zero
589+
@retval EFI_NOT_FOUND RNG protocol not found
590+
@retval Others Error from RngProtocol->GetRNG()
590591
592+
@return Status code
591593
**/
592-
UINT32
594+
EFI_STATUS
593595
EFIAPI
594-
NetRandomInitSeed (
595-
VOID
596+
PseudoRandom (
597+
OUT VOID *Output,
598+
IN UINTN OutputLength
599+
);
600+
601+
/**
602+
Generate a 32-bit pseudo-random number.
603+
604+
@param[out] Output - The buffer to store the generated random number.
605+
606+
@retval EFI_SUCCESS On Success
607+
@retval EFI_NOT_FOUND RNG protocol not found
608+
@retval Others Error from RngProtocol->GetRNG()
609+
610+
@return Status code
611+
**/
612+
EFI_STATUS
613+
EFIAPI
614+
PseudoRandomU32 (
615+
OUT UINT32 *Output
596616
);
597617

598618
#define NET_LIST_USER_STRUCT(Entry, Type, Field) \

NetworkPkg/Ip4Dxe/Ip4Driver.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
The driver binding and service binding protocol for IP4 driver.
33
44
Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.<BR>
5+
Copyright (c) Microsoft Corporation
56
(C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
67
78
SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -549,11 +550,18 @@ Ip4DriverBindingStart (
549550
EFI_IP4_CONFIG2_PROTOCOL *Ip4Cfg2;
550551
UINTN Index;
551552
IP4_CONFIG2_DATA_ITEM *DataItem;
553+
UINT32 Random;
552554

553555
IpSb = NULL;
554556
Ip4Cfg2 = NULL;
555557
DataItem = NULL;
556558

559+
Status = PseudoRandomU32 (&Random);
560+
if (EFI_ERROR (Status)) {
561+
DEBUG ((DEBUG_ERROR, "%a failed to generate random number: %r\n", __func__, Status));
562+
return Status;
563+
}
564+
557565
//
558566
// Test for the Ip4 service binding protocol
559567
//
@@ -653,7 +661,7 @@ Ip4DriverBindingStart (
653661
//
654662
// Initialize the IP4 ID
655663
//
656-
mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());
664+
mIp4Id = (UINT16)Random;
657665

658666
return Status;
659667

NetworkPkg/Ip6Dxe/Ip6ConfigImpl.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,13 @@ Ip6ConfigInitInstance (
22762276
UINTN Index;
22772277
UINT16 IfIndex;
22782278
IP6_CONFIG_DATA_ITEM *DataItem;
2279+
UINT32 Random;
2280+
2281+
Status = PseudoRandomU32 (&Random);
2282+
if (EFI_ERROR (Status)) {
2283+
DEBUG ((DEBUG_ERROR, "%a failed to generate random number: %r\n", __func__, Status));
2284+
return Status;
2285+
}
22792286

22802287
IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance);
22812288

@@ -2381,7 +2388,7 @@ Ip6ConfigInitInstance (
23812388
// The NV variable is not set, so generate a random IAID, and write down the
23822389
// fresh new configuration as the NV variable now.
23832390
//
2384-
Instance->IaId = NET_RANDOM (NetRandomInitSeed ());
2391+
Instance->IaId = Random;
23852392

23862393
for (Index = 0; Index < IpSb->SnpMode.HwAddressSize; Index++) {
23872394
Instance->IaId |= (IpSb->SnpMode.CurrentAddress.Addr[Index] << ((Index << 3) & 31));

0 commit comments

Comments
 (0)