From 837bb626613eb6ae2d9c647b39dc2784a6586c2d Mon Sep 17 00:00:00 2001 From: Oliver Smith-Denny Date: Wed, 28 Aug 2024 09:39:45 -0700 Subject: NetworkPkg: PxeBcDhcp6GoogleTest: Fix Stack Smashing Unit Test PxeBcDhcp6GoogleTest's MultipleDnsEntries test started to fail with stack cookies added for host applications. Debugging this showed that the test was attempting to copy two UINT16s to a UINT8 Data[1] array allocated on the stack. This was moved to a heap based allocation for a UINT32 to accommodate the proper size. After this fix, the unit test passed with stack cookies enabled. Signed-off-by: Oliver Smith-Denny --- .../GoogleTest/PxeBcDhcp6GoogleTest.cpp | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'NetworkPkg') diff --git a/NetworkPkg/UefiPxeBcDxe/GoogleTest/PxeBcDhcp6GoogleTest.cpp b/NetworkPkg/UefiPxeBcDxe/GoogleTest/PxeBcDhcp6GoogleTest.cpp index 61736ff79e..e529bc6daf 100644 --- a/NetworkPkg/UefiPxeBcDxe/GoogleTest/PxeBcDhcp6GoogleTest.cpp +++ b/NetworkPkg/UefiPxeBcDxe/GoogleTest/PxeBcDhcp6GoogleTest.cpp @@ -290,15 +290,9 @@ TEST_F (PxeBcCacheDnsServerAddressesTest, AttemptUnderflowTest) { // Test Description // Test that we can handle recursive dns (multiple dns entries) TEST_F (PxeBcCacheDnsServerAddressesTest, MultipleDnsEntries) { - EFI_DHCP6_PACKET_OPTION Option = { 0 }; + EFI_DHCP6_PACKET_OPTION *Option = NULL; PXEBC_DHCP6_PACKET_CACHE *Cache6 = NULL; - Private.SelectIndex = 1; // SelectIndex is 1-based - Cache6 = &Private.OfferBuffer[Private.SelectIndex - 1].Dhcp6; - Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] = &Option; - // Setup the DHCPv6 offer packet - Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpCode = DHCP6_OPT_SERVER_ID; - EFI_IPv6_ADDRESS addresses[2] = { // 2001:db8:85a3::8a2e:370:7334 { 0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34 }, @@ -306,7 +300,18 @@ TEST_F (PxeBcCacheDnsServerAddressesTest, MultipleDnsEntries) { { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd4, 0x78, 0x91, 0xc3, 0xec, 0xd7, 0x4f, 0xf9 } }; - CopyMem (Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->Data, &addresses, sizeof (addresses)); + Option = (EFI_DHCP6_PACKET_OPTION *)AllocatePool (sizeof (*Option) + sizeof (addresses)); + if (Option == NULL) { + ASSERT_NE (Option, nullptr); + } + + Private.SelectIndex = 1; // SelectIndex is 1-based + Cache6 = &Private.OfferBuffer[Private.SelectIndex - 1].Dhcp6; + Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] = Option; + // Setup the DHCPv6 offer packet + Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpCode = DHCP6_OPT_SERVER_ID; + + CopyMem (Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->Data, addresses, sizeof (addresses)); Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpLen = NTOHS (sizeof (addresses)); @@ -327,6 +332,10 @@ TEST_F (PxeBcCacheDnsServerAddressesTest, MultipleDnsEntries) { if (Private.DnsServer) { FreePool (Private.DnsServer); } + + if (Option) { + FreePool (Option); + } } /////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3