From nobody Thu Nov 14 07:00:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110661+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110661+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031848; cv=none; d=zohomail.com; s=zohoarc; b=XBWDvKvsOkg4Fo2/XAr1TcSof+eSOwvOOUW7DKl+81GpLXTYDS1Rybh6GyjzvuW0LL5Bl0bCn+hiaVRkNzKqkEzmzEYRieHuroN9SeGbWmZonSaAkHzNt8sAntvnsnjTUCoaoKvtB6K9pez6nJxPzk4s8osVwj9mgmW/KMg8Ykg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031848; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=NuNVJkuycq4Us51NdnMJ9c4svL6Rwv1neBKjcOYEesQ=; b=oBfRwwU5I+YpycXrLtEJ8hnqTgI7i/sjQ5K0lw8ilhd8sTeNO8A6zniCGhaGlLRO4g6z5wrb0FSTjGiw7DrGFX9xVb/7lktNw8Xu5fKILGz8+gqEifGg84274p8O/K5aCd8CKNSNScHPYCkD2aQ1D9ik2zYwWIK8kqE77QNP2S8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110661+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031848543561.2777530671618; Fri, 3 Nov 2023 10:17:28 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=tzIURsTHnbYyE97BiA0kO187gX/Y23N/S+76lgPW0N4=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1699031848; v=1; b=dmUq/V4McASRky2DYxcxwryF8ta9tqcGuwjStiFNq4/InbKDne9F35Z+e8ssGIptxLxmCeEg IJ39rDMjNgfsoEct3Wdj7b5m7JQwWmzKAl0+3VrUF5nQslPqS9ltxMvWSqARF7F211SLKGrRDSC LGVjQllMM5ZOiUPXlIZx+Et4= X-Received: by 127.0.0.2 with SMTP id Kec4YY1788612xqZAphZy00F; Fri, 03 Nov 2023 10:17:28 -0700 X-Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by mx.groups.io with SMTP id smtpd.web10.59758.1699031846705387868 for ; Fri, 03 Nov 2023 10:17:26 -0700 X-Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1cc5b7057d5so20875815ad.2 for ; Fri, 03 Nov 2023 10:17:26 -0700 (PDT) X-Gm-Message-State: ZtM2LqxlBbn1CBfKVbK0ZiMyx1787277AA= X-Google-Smtp-Source: AGHT+IGdj5heZqjqkQv0/SctsXAceVxlIDQeYyGh89cnOJg1JwxryRKHhcCBvByPgXO3y9auP0lcbQ== X-Received: by 2002:a17:903:1ca:b0:1cc:3bd3:73d8 with SMTP id e10-20020a17090301ca00b001cc3bd373d8mr20675434plh.59.1699031845937; Fri, 03 Nov 2023 10:17:25 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:25 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi , Jiaxin Wu , Ray Ni Subject: [edk2-devel] [PATCH v4 13/14] MdeModulePkg: Add Logic to Create/Delete Image Properties Records Date: Fri, 3 Nov 2023 10:17:05 -0700 Message-ID: <20231103171706.148-14-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031848974100041 Content-Type: text/plain; charset="utf-8" Add logic to create and delete image properties records. Where applicable, redirect existing code to use the new library. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Cc: Jiaxin Wu Cc: Ray Ni Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c = | 184 +++---------------- MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c = | 166 +++-------------- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c = | 186 ++++++++++++++++++++ MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h = | 39 ++++ MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf= | 1 + 5 files changed, 281 insertions(+), 295 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index af6c26244cc0..993db281062a 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -557,25 +557,6 @@ CoreGetMemoryMapWithSeparatedImageSection ( // Below functions are for ImageRecord // =20 -/** - Set MemoryAttributesTable according to PE/COFF image section alignment. - - @param SectionAlignment PE/COFF section alignment -**/ -STATIC -VOID -SetMemoryAttributesTableSectionAlignment ( - IN UINT32 SectionAlignment - ) -{ - if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D= 0) && - mMemoryAttributesTableEnable) - { - DEBUG ((DEBUG_VERBOSE, "SetMemoryAttributesTableSectionAlignment - Cle= ar\n")); - mMemoryAttributesTableEnable =3D FALSE; - } -} - /** Insert image record. =20 @@ -586,20 +567,12 @@ InsertImageRecord ( IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage ) { - VOID *ImageAddress; - EFI_IMAGE_DOS_HEADER *DosHdr; - UINT32 PeCoffHeaderOffset; - UINT32 SectionAlignment; - EFI_IMAGE_SECTION_HEADER *Section; - EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; - UINT8 *Name; - UINTN Index; - IMAGE_PROPERTIES_RECORD *ImageRecord; - CHAR8 *PdbPointer; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + EFI_STATUS Status; + IMAGE_PROPERTIES_RECORD *ImageRecord; + CHAR8 *PdbPointer; + UINT32 RequiredAlignment; =20 DEBUG ((DEBUG_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage)); - DEBUG ((DEBUG_VERBOSE, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); =20 if (mMemoryAttributesTableEndOfDxe) { DEBUG ((DEBUG_INFO, "Do not insert runtime image record after EndOfDxe= \n")); @@ -611,139 +584,48 @@ InsertImageRecord ( return; } =20 - ImageRecord->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + InitializeListHead (&ImageRecord->Link); + InitializeListHead (&ImageRecord->CodeSegmentList); =20 - DEBUG ((DEBUG_VERBOSE, "ImageRecordCount - 0x%x\n", mImagePropertiesPriv= ateData.ImageRecordCount)); - - // - // Step 1: record whole region - // - ImageRecord->ImageBase =3D (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->Im= ageBase; - ImageRecord->ImageSize =3D RuntimeImage->ImageSize; - - ImageAddress =3D RuntimeImage->ImageBase; - - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)RuntimeImage->I= mageBase); if (PdbPointer !=3D NULL) { DEBUG ((DEBUG_VERBOSE, " Image - %a\n", PdbPointer)); } =20 - // - // Check PE/COFF image - // - DosHdr =3D (EFI_IMAGE_DOS_HEADER *)(UINTN)ImageAddress; - PeCoffHeaderOffset =3D 0; - if (DosHdr->e_magic =3D=3D EFI_IMAGE_DOS_SIGNATURE) { - PeCoffHeaderOffset =3D DosHdr->e_lfanew; - } - - Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *)(UINTN)ImageAddress + P= eCoffHeaderOffset); - if (Hdr.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE) { - DEBUG ((DEBUG_VERBOSE, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr.Pe3= 2->Signature)); - // It might be image in SMM. - goto Finish; - } - - // - // Get SectionAlignment - // - if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MA= GIC) { - SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionAlignment; - } else { - SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.SectionAlignment; - } - - SetMemoryAttributesTableSectionAlignment (SectionAlignment); - if ((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D = 0) { - DEBUG (( - DEBUG_WARN, - "!!!!!!!! InsertImageRecord - Section Alignment(0x%x) is not %dK != !!!!!!!\n", - SectionAlignment, - RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10 - )); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); - if (PdbPointer !=3D NULL) { - DEBUG ((DEBUG_WARN, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer)); + RequiredAlignment =3D RUNTIME_PAGE_ALLOCATION_GRANULARITY; + Status =3D CreateImagePropertiesRecord ( + RuntimeImage->ImageBase, + RuntimeImage->ImageSize, + &RequiredAlignment, + ImageRecord + ); + + if (EFI_ERROR (Status)) { + if (Status =3D=3D EFI_ABORTED) { + mMemoryAttributesTableEnable =3D FALSE; } =20 + Status =3D EFI_ABORTED; goto Finish; } =20 - Section =3D (EFI_IMAGE_SECTION_HEADER *)( - (UINT8 *)(UINTN)ImageAddress + - PeCoffHeaderOffset + - sizeof (UINT32) + - sizeof (EFI_IMAGE_FILE_HEADER) + - Hdr.Pe32->FileHeader.SizeOfOption= alHeader - ); - ImageRecord->CodeSegmentCount =3D 0; - InitializeListHead (&ImageRecord->CodeSegmentList); - for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { - Name =3D Section[Index].Name; - DEBUG (( - DEBUG_VERBOSE, - " Section - '%c%c%c%c%c%c%c%c'\n", - Name[0], - Name[1], - Name[2], - Name[3], - Name[4], - Name[5], - Name[6], - Name[7] - )); - - if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { - DEBUG ((DEBUG_VERBOSE, " VirtualSize - 0x%08x\n", Section[= Index].Misc.VirtualSize)); - DEBUG ((DEBUG_VERBOSE, " VirtualAddress - 0x%08x\n", Section[= Index].VirtualAddress)); - DEBUG ((DEBUG_VERBOSE, " SizeOfRawData - 0x%08x\n", Section[= Index].SizeOfRawData)); - DEBUG ((DEBUG_VERBOSE, " PointerToRawData - 0x%08x\n", Section[= Index].PointerToRawData)); - DEBUG ((DEBUG_VERBOSE, " PointerToRelocations - 0x%08x\n", Section[= Index].PointerToRelocations)); - DEBUG ((DEBUG_VERBOSE, " PointerToLinenumbers - 0x%08x\n", Section[= Index].PointerToLinenumbers)); - DEBUG ((DEBUG_VERBOSE, " NumberOfRelocations - 0x%08x\n", Section[= Index].NumberOfRelocations)); - DEBUG ((DEBUG_VERBOSE, " NumberOfLinenumbers - 0x%08x\n", Section[= Index].NumberOfLinenumbers)); - DEBUG ((DEBUG_VERBOSE, " Characteristics - 0x%08x\n", Section[= Index].Characteristics)); - - // - // Step 2: record code section - // - ImageRecordCodeSection =3D AllocatePool (sizeof (*ImageRecordCodeSec= tion)); - if (ImageRecordCodeSection =3D=3D NULL) { - return; - } - - ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; - - ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageAddress + Se= ction[Index].VirtualAddress; - ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; - - DEBUG ((DEBUG_VERBOSE, "ImageCode: 0x%016lx - 0x%016lx\n", ImageReco= rdCodeSection->CodeSegmentBase, ImageRecordCodeSection->CodeSegmentSize)); - - InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); - ImageRecord->CodeSegmentCount++; - } - } - if (ImageRecord->CodeSegmentCount =3D=3D 0) { - SetMemoryAttributesTableSectionAlignment (1); + mMemoryAttributesTableEnable =3D FALSE; DEBUG ((DEBUG_ERROR, "!!!!!!!! InsertImageRecord - CodeSegmentCount i= s 0 !!!!!!!!\n")); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); if (PdbPointer !=3D NULL) { DEBUG ((DEBUG_ERROR, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer)= ); } =20 + Status =3D EFI_ABORTED; goto Finish; } =20 - // - // Final - // - SortImageRecordCodeSection (ImageRecord); // // Check overlap all section in ImageBase/Size // if (!IsImageRecordCodeSectionValid (ImageRecord)) { DEBUG ((DEBUG_ERROR, "IsImageRecordCodeSectionValid - FAIL\n")); + Status =3D EFI_ABORTED; goto Finish; } =20 @@ -757,6 +639,10 @@ InsertImageRecord ( SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList); =20 Finish: + if (EFI_ERROR (Status) && (ImageRecord !=3D NULL)) { + DeleteImagePropertiesRecord (ImageRecord); + } + return; } =20 @@ -770,9 +656,7 @@ RemoveImageRecord ( IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage ) { - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *CodeSegmentListHead; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + IMAGE_PROPERTIES_RECORD *ImageRecord; =20 DEBUG ((DEBUG_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage)); DEBUG ((DEBUG_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); @@ -788,19 +672,7 @@ RemoveImageRecord ( return; } =20 - CodeSegmentListHead =3D &ImageRecord->CodeSegmentList; - while (!IsListEmpty (CodeSegmentListHead)) { - ImageRecordCodeSection =3D CR ( - CodeSegmentListHead->ForwardLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - RemoveEntryList (&ImageRecordCodeSection->Link); - FreePool (ImageRecordCodeSection); - } + DeleteImagePropertiesRecord (ImageRecord); =20 - RemoveEntryList (&ImageRecord->Link); - FreePool (ImageRecord); mImagePropertiesPrivateData.ImageRecordCount--; } diff --git a/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c b/MdeModul= ePkg/Core/PiSmmCore/MemoryAttributesTable.c index 2e4aaddef4e5..03de9b2c5fff 100644 --- a/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c @@ -251,25 +251,6 @@ SmmCoreGetMemoryMapMemoryAttributesTable ( // Below functions are for ImageRecord // =20 -/** - Set MemoryProtectionAttribute according to PE/COFF image section alignme= nt. - - @param[in] SectionAlignment PE/COFF section alignment -**/ -STATIC -VOID -SetMemoryAttributesTableSectionAlignment ( - IN UINT32 SectionAlignment - ) -{ - if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D= 0) && - ((mMemoryProtectionAttribute & EFI_MEMORY_ATTRIBUTES_RUNTIME_MEMORY_= PROTECTION_NON_EXECUTABLE_PE_DATA) !=3D 0)) - { - DEBUG ((DEBUG_VERBOSE, "SMM SetMemoryAttributesTableSectionAlignment -= Clear\n")); - mMemoryProtectionAttribute &=3D ~((UINT64)EFI_MEMORY_ATTRIBUTES_RUNTIM= E_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA); - } -} - /** Insert image record. =20 @@ -280,158 +261,61 @@ SmmInsertImageRecord ( IN EFI_SMM_DRIVER_ENTRY *DriverEntry ) { - VOID *ImageAddress; - EFI_IMAGE_DOS_HEADER *DosHdr; - UINT32 PeCoffHeaderOffset; - UINT32 SectionAlignment; - EFI_IMAGE_SECTION_HEADER *Section; - EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; - UINT8 *Name; - UINTN Index; - IMAGE_PROPERTIES_RECORD *ImageRecord; - CHAR8 *PdbPointer; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + EFI_STATUS Status; + IMAGE_PROPERTIES_RECORD *ImageRecord; + CHAR8 *PdbPointer; + UINT32 RequiredAlignment; =20 DEBUG ((DEBUG_VERBOSE, "SMM InsertImageRecord - 0x%x\n", DriverEntry)); - DEBUG ((DEBUG_VERBOSE, "SMM InsertImageRecord - 0x%016lx - 0x%08x\n", Dr= iverEntry->ImageBuffer, DriverEntry->NumberOfPage)); =20 ImageRecord =3D AllocatePool (sizeof (*ImageRecord)); if (ImageRecord =3D=3D NULL) { return; } =20 - ImageRecord->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + InitializeListHead (&ImageRecord->Link); + InitializeListHead (&ImageRecord->CodeSegmentList); =20 - DEBUG ((DEBUG_VERBOSE, "SMM ImageRecordCount - 0x%x\n", mImageProperties= PrivateData.ImageRecordCount)); - - // - // Step 1: record whole region - // - ImageRecord->ImageBase =3D DriverEntry->ImageBuffer; - ImageRecord->ImageSize =3D LShiftU64 (DriverEntry->NumberOfPage, EFI_PAG= E_SHIFT); - - ImageAddress =3D (VOID *)(UINTN)DriverEntry->ImageBuffer; - - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)DriverEntry->Im= ageBuffer); if (PdbPointer !=3D NULL) { DEBUG ((DEBUG_VERBOSE, "SMM Image - %a\n", PdbPointer)); } =20 - // - // Check PE/COFF image - // - DosHdr =3D (EFI_IMAGE_DOS_HEADER *)(UINTN)ImageAddress; - PeCoffHeaderOffset =3D 0; - if (DosHdr->e_magic =3D=3D EFI_IMAGE_DOS_SIGNATURE) { - PeCoffHeaderOffset =3D DosHdr->e_lfanew; - } - - Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *)(UINTN)ImageAddress + P= eCoffHeaderOffset); - if (Hdr.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE) { - DEBUG ((DEBUG_VERBOSE, "SMM Hdr.Pe32->Signature invalid - 0x%x\n", Hdr= .Pe32->Signature)); - goto Finish; - } - - // - // Get SectionAlignment - // - if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MA= GIC) { - SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionAlignment; - } else { - SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.SectionAlignment; - } - - SetMemoryAttributesTableSectionAlignment (SectionAlignment); - if ((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D = 0) { - DEBUG (( - DEBUG_WARN, - "SMM !!!!!!!! InsertImageRecord - Section Alignment(0x%x) is not %d= K !!!!!!!!\n", - SectionAlignment, - RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10 - )); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); - if (PdbPointer !=3D NULL) { - DEBUG ((DEBUG_WARN, "SMM !!!!!!!! Image - %a !!!!!!!!\n", PdbPoint= er)); + RequiredAlignment =3D RUNTIME_PAGE_ALLOCATION_GRANULARITY; + Status =3D CreateImagePropertiesRecord ( + (VOID *)(UINTN)DriverEntry->ImageBuffer, + LShiftU64 (DriverEntry->NumberOfPage, EFI_PAGE_SHI= FT), + &RequiredAlignment, + ImageRecord + ); + + if (EFI_ERROR (Status)) { + if (Status =3D=3D EFI_ABORTED) { + mMemoryProtectionAttribute &=3D + ~((UINT64)EFI_MEMORY_ATTRIBUTES_RUNTIME_MEMORY_PROTECTION_NON_EXEC= UTABLE_PE_DATA); } =20 goto Finish; } =20 - Section =3D (EFI_IMAGE_SECTION_HEADER *)( - (UINT8 *)(UINTN)ImageAddress + - PeCoffHeaderOffset + - sizeof (UINT32) + - sizeof (EFI_IMAGE_FILE_HEADER) + - Hdr.Pe32->FileHeader.SizeOfOption= alHeader - ); - ImageRecord->CodeSegmentCount =3D 0; - InitializeListHead (&ImageRecord->CodeSegmentList); - for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { - Name =3D Section[Index].Name; - DEBUG (( - DEBUG_VERBOSE, - "SMM Section - '%c%c%c%c%c%c%c%c'\n", - Name[0], - Name[1], - Name[2], - Name[3], - Name[4], - Name[5], - Name[6], - Name[7] - )); - - if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { - DEBUG ((DEBUG_VERBOSE, "SMM VirtualSize - 0x%08x\n", Sect= ion[Index].Misc.VirtualSize)); - DEBUG ((DEBUG_VERBOSE, "SMM VirtualAddress - 0x%08x\n", Sect= ion[Index].VirtualAddress)); - DEBUG ((DEBUG_VERBOSE, "SMM SizeOfRawData - 0x%08x\n", Sect= ion[Index].SizeOfRawData)); - DEBUG ((DEBUG_VERBOSE, "SMM PointerToRawData - 0x%08x\n", Sect= ion[Index].PointerToRawData)); - DEBUG ((DEBUG_VERBOSE, "SMM PointerToRelocations - 0x%08x\n", Sect= ion[Index].PointerToRelocations)); - DEBUG ((DEBUG_VERBOSE, "SMM PointerToLinenumbers - 0x%08x\n", Sect= ion[Index].PointerToLinenumbers)); - DEBUG ((DEBUG_VERBOSE, "SMM NumberOfRelocations - 0x%08x\n", Sect= ion[Index].NumberOfRelocations)); - DEBUG ((DEBUG_VERBOSE, "SMM NumberOfLinenumbers - 0x%08x\n", Sect= ion[Index].NumberOfLinenumbers)); - DEBUG ((DEBUG_VERBOSE, "SMM Characteristics - 0x%08x\n", Sect= ion[Index].Characteristics)); - - // - // Step 2: record code section - // - ImageRecordCodeSection =3D AllocatePool (sizeof (*ImageRecordCodeSec= tion)); - if (ImageRecordCodeSection =3D=3D NULL) { - return; - } - - ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; - - ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageAddress + Se= ction[Index].VirtualAddress; - ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; - - DEBUG ((DEBUG_VERBOSE, "SMM ImageCode: 0x%016lx - 0x%016lx\n", Image= RecordCodeSection->CodeSegmentBase, ImageRecordCodeSection->CodeSegmentSize= )); - - InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); - ImageRecord->CodeSegmentCount++; - } - } - if (ImageRecord->CodeSegmentCount =3D=3D 0) { - SetMemoryAttributesTableSectionAlignment (1); + mMemoryProtectionAttribute &=3D + ~((UINT64)EFI_MEMORY_ATTRIBUTES_RUNTIME_MEMORY_PROTECTION_NON_EXECUT= ABLE_PE_DATA); DEBUG ((DEBUG_ERROR, "SMM !!!!!!!! InsertImageRecord - CodeSegmentCou= nt is 0 !!!!!!!!\n")); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); if (PdbPointer !=3D NULL) { DEBUG ((DEBUG_ERROR, "SMM !!!!!!!! Image - %a !!!!!!!!\n", PdbPoin= ter)); } =20 + Status =3D EFI_ABORTED; goto Finish; } =20 - // - // Final - // - SortImageRecordCodeSection (ImageRecord); // // Check overlap all section in ImageBase/Size // if (!IsImageRecordCodeSectionValid (ImageRecord)) { DEBUG ((DEBUG_ERROR, "SMM IsImageRecordCodeSectionValid - FAIL\n")); + Status =3D EFI_ABORTED; goto Finish; } =20 @@ -445,6 +329,10 @@ SmmInsertImageRecord ( SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList); =20 Finish: + if (EFI_ERROR (Status) && (ImageRecord !=3D NULL)) { + DeleteImagePropertiesRecord (ImageRecord); + } + return; } =20 diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index 9b296aa45762..6c5eb1dc3185 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -13,6 +13,7 @@ #include #include #include +#include #include =20 #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ @@ -858,3 +859,188 @@ FindImageRecord ( =20 return NULL; } + +/** + Creates an IMAGE_PROPERTIES_RECORD from a loaded PE image. The PE/COFF h= eader will be found + and parsed to determine the number of code segments and their base addre= sses and sizes. + + @param[in] ImageBase Base of the PE image + @param[in] ImageSize Size of the PE image + @param[in] RequiredAlignment If non-NULL, the alignment speci= fied in the PE/COFF header + will be compared against this va= lue. + @param[out] ImageRecord On out, a populated image proper= ties record + + @retval EFI_INVALID_PARAMETER This function ImageBase or ImageReco= rd was NULL, or the + image located at ImageBase was not a= valid PE/COFF image + @retval EFI_OUT_OF_RESOURCES Failure to Allocate() + @retval EFI_ABORTED The input Alignment was non-NULL and= did not match the + alignment specified in the PE/COFF h= eader + @retval EFI_SUCCESS The image properties record was succ= essfully created +**/ +EFI_STATUS +EFIAPI +CreateImagePropertiesRecord ( + IN CONST VOID *ImageBase, + IN CONST UINT64 ImageSize, + IN CONST UINT32 *RequiredAlignment OPTIONAL, + OUT IMAGE_PROPERTIES_RECORD *ImageRecord + ) +{ + EFI_STATUS Status; + EFI_IMAGE_DOS_HEADER *DosHdr; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + EFI_IMAGE_SECTION_HEADER *Section; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + UINTN Index; + UINT8 *Name; + UINT32 SectionAlignment; + UINT32 PeCoffHeaderOffset; + + if ((ImageRecord =3D=3D NULL) || (ImageBase =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + DEBUG (( + DEBUG_VERBOSE, + "Creating Image Properties Record: 0x%016lx - 0x%016lx\n", + (EFI_PHYSICAL_ADDRESS)(UINTN)ImageBase, + ImageSize + )); + + // + // Step 1: record whole region + // + Status =3D EFI_SUCCESS; + ImageRecord->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + ImageRecord->ImageBase =3D (EFI_PHYSICAL_ADDRESS)(UINTN)ImageBase; + ImageRecord->ImageSize =3D ImageSize; + ImageRecord->CodeSegmentCount =3D 0; + InitializeListHead (&ImageRecord->Link); + InitializeListHead (&ImageRecord->CodeSegmentList); + + // Check PE/COFF image + DosHdr =3D (EFI_IMAGE_DOS_HEADER *)(UINTN)ImageBase; + PeCoffHeaderOffset =3D 0; + if (DosHdr->e_magic =3D=3D EFI_IMAGE_DOS_SIGNATURE) { + PeCoffHeaderOffset =3D DosHdr->e_lfanew; + } + + Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *)(UINTN)ImageBase + PeCo= ffHeaderOffset); + if (Hdr.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE) { + DEBUG ((DEBUG_VERBOSE, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr.Pe3= 2->Signature)); + return EFI_INVALID_PARAMETER; + } + + // Get SectionAlignment + if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MA= GIC) { + SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionAlignment; + } else { + SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.SectionAlignment; + } + + // Check RequiredAlignment + if ((RequiredAlignment !=3D NULL) && ((SectionAlignment & (*RequiredAlig= nment - 1)) !=3D 0)) { + DEBUG (( + DEBUG_WARN, + "!!!!!!!! Image Section Alignment(0x%x) does not match Required Ali= gnment (0x%x) !!!!!!!!\n", + SectionAlignment, + *RequiredAlignment + )); + + return EFI_ABORTED; + } + + Section =3D (EFI_IMAGE_SECTION_HEADER *)( + (UINT8 *)(UINTN)ImageBase + + PeCoffHeaderOffset + + sizeof (UINT32) + + sizeof (EFI_IMAGE_FILE_HEADER) + + Hdr.Pe32->FileHeader.SizeOfOption= alHeader + ); + for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { + Name =3D Section[Index].Name; + DEBUG (( + DEBUG_VERBOSE, + " Section - '%c%c%c%c%c%c%c%c'\n", + Name[0], + Name[1], + Name[2], + Name[3], + Name[4], + Name[5], + Name[6], + Name[7] + )); + + if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { + DEBUG ((DEBUG_VERBOSE, " VirtualSize - 0x%08x\n", Section[= Index].Misc.VirtualSize)); + DEBUG ((DEBUG_VERBOSE, " VirtualAddress - 0x%08x\n", Section[= Index].VirtualAddress)); + DEBUG ((DEBUG_VERBOSE, " SizeOfRawData - 0x%08x\n", Section[= Index].SizeOfRawData)); + DEBUG ((DEBUG_VERBOSE, " PointerToRawData - 0x%08x\n", Section[= Index].PointerToRawData)); + DEBUG ((DEBUG_VERBOSE, " PointerToRelocations - 0x%08x\n", Section[= Index].PointerToRelocations)); + DEBUG ((DEBUG_VERBOSE, " PointerToLinenumbers - 0x%08x\n", Section[= Index].PointerToLinenumbers)); + DEBUG ((DEBUG_VERBOSE, " NumberOfRelocations - 0x%08x\n", Section[= Index].NumberOfRelocations)); + DEBUG ((DEBUG_VERBOSE, " NumberOfLinenumbers - 0x%08x\n", Section[= Index].NumberOfLinenumbers)); + DEBUG ((DEBUG_VERBOSE, " Characteristics - 0x%08x\n", Section[= Index].Characteristics)); + + // Record code section(s) + ImageRecordCodeSection =3D AllocatePool (sizeof (*ImageRecordCodeSec= tion)); + if (ImageRecordCodeSection =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; + + ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageBase + Secti= on[Index].VirtualAddress; + ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; + + InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); + ImageRecord->CodeSegmentCount++; + } + } + + if (ImageRecord->CodeSegmentCount > 0) { + SortImageRecordCodeSection (ImageRecord); + } + + return Status; +} + +/** + Deleted an image properties record. The function will also call + RemoveEntryList() on each code segment and the input ImageRecord before + freeing each pool. + + @param[in] ImageRecord The IMAGE_PROPERTIES_RECORD to d= elete +**/ +VOID +EFIAPI +DeleteImagePropertiesRecord ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ) +{ + LIST_ENTRY *CodeSegmentListHead; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + + if (ImageRecord =3D=3D NULL) { + return; + } + + CodeSegmentListHead =3D &ImageRecord->CodeSegmentList; + while (!IsListEmpty (CodeSegmentListHead)) { + ImageRecordCodeSection =3D CR ( + CodeSegmentListHead->ForwardLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + RemoveEntryList (&ImageRecordCodeSection->Link); + FreePool (ImageRecordCodeSection); + } + + if (!IsListEmpty (&ImageRecord->Link)) { + RemoveEntryList (&ImageRecord->Link); + } + + FreePool (ImageRecord); +} diff --git a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h b/MdeM= odulePkg/Include/Library/ImagePropertiesRecordLib.h index e3f569ab03d1..5090a521536b 100644 --- a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h +++ b/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h @@ -192,4 +192,43 @@ DumpImageRecord ( IN LIST_ENTRY *ImageRecordList ); =20 +/** + Creates an IMAGE_PROPERTIES_RECORD from a loaded PE image. The PE/COFF h= eader will be found + and parsed to determine the number of code segments and their base addre= sses and sizes. + + @param[in] ImageBase Base of the PE image + @param[in] ImageSize Size of the PE image + @param[in] RequiredAlignment If non-NULL, the alignment speci= fied in the PE/COFF header + will be compared against this va= lue. + @param[out] ImageRecord On out, a populated image proper= ties record + + @retval EFI_INVALID_PARAMETER This function ImageBase or ImageReco= rd was NULL, or the + image located at ImageBase was not a= valid PE/COFF image + @retval EFI_OUT_OF_RESOURCES Failure to Allocate() + @retval EFI_ABORTED The input Alignment was non-NULL and= did not match the + alignment specified in the PE/COFF h= eader + @retval EFI_SUCCESS The image properties record was succ= essfully created +**/ +EFI_STATUS +EFIAPI +CreateImagePropertiesRecord ( + IN CONST VOID *ImageBase, + IN CONST UINT64 ImageSize, + IN CONST UINT32 *Alignment OPTIONAL, + OUT IMAGE_PROPERTIES_RECORD *ImageRecord + ); + +/** + Deleted an image properties record. The function will also call + RemoveEntryList() on each code segment and the input ImageRecord before + freeing each pool. + + @param[in] ImageRecord The IMAGE_PROPERTIES_RECORD to d= elete +**/ +VOID +EFIAPI +DeleteImagePropertiesRecord ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ); + #endif diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.inf b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertie= sRecordLib.inf index 4c1466fc3336..cfe0c04b3b05 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf @@ -23,6 +23,7 @@ [LibraryClasses] BaseLib BaseMemoryLib DebugLib + MemoryAllocationLib =20 [Packages] MdePkg/MdePkg.dec --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110661): https://edk2.groups.io/g/devel/message/110661 Mute This Topic: https://groups.io/mt/102368854/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-