From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id E2A63D80CF6 for ; Tue, 27 Feb 2024 20:27:29 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=Ep1OJ5epeRZEQZHo+eoX32geUyJYkWqS9MmCELIbS4U=; c=relaxed/simple; d=groups.io; h=DKIM-Filter: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=1709065648; v=1; b=dlw1xOO9g3BGFdy0BK8PUgl6bih/f9bJZKJGHyYb32sj+mW1m4eAHqy9ktVqZrvX+LXjOJZ/ LZ4a8SVZ62oTkVYzVki8gPnX7AuCyTW10PLWJiq30jnMk1zp3goHT+jYrWmX4dp5cD/KuNsJlTF wMng+9JrLYDbXxmYlyi9EwCk= X-Received: by 127.0.0.2 with SMTP id 6wFCYY7687511xfJodeg8iTa; Tue, 27 Feb 2024 12:27:28 -0800 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web10.4669.1709065646441199138 for ; Tue, 27 Feb 2024 12:27:26 -0800 X-Received: from OSD-Desktop.redmond.corp.microsoft.com (unknown [131.107.160.171]) by linux.microsoft.com (Postfix) with ESMTPSA id 025DC20B74C2; Tue, 27 Feb 2024 12:27:26 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 025DC20B74C2 From: "Oliver Smith-Denny" To: devel@edk2.groups.io Cc: Liming Gao , Taylor Beebe Subject: [edk2-devel][PATCH v1 2/3] MdeModulePkg: ImagePropertiesRecordLib: Consolidate Usage Date: Tue, 27 Feb 2024 12:27:20 -0800 Message-Id: <20240227202721.30070-3-osde@linux.microsoft.com> In-Reply-To: <20240227202721.30070-1-osde@linux.microsoft.com> References: <20240227202721.30070-1-osde@linux.microsoft.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,osde@linux.microsoft.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: JyZT2NwI350PcYrAtSeg6UzSx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=dlw1xOO9; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=linux.microsoft.com (policy=none) Currently, there are multiple instances of code create image=0D records. ImagePropertiesRecordLib was created to only have=0D this code in one place. Update the lib to use additional=0D logic from the copy in MemoryProtection.c before converging=0D that code to use the lib.=0D =0D Cc: Liming Gao =0D Cc: Taylor Beebe =0D =0D Signed-off-by: Oliver Smith-Denny =0D ---=0D MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c |= 82 +++++++++++++++-----=0D 1 file changed, 63 insertions(+), 19 deletions(-)=0D =0D diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c=0D index 07ced0e54e38..9b99cb0f77d2 100644=0D --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c=0D +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c=0D @@ -60,6 +60,39 @@ EfiSizeToPages (=0D return RShiftU64 (Size, EFI_PAGE_SHIFT) + ((((UINTN)Size) & EFI_PAGE_MAS= K) ? 1 : 0);=0D }=0D =0D +/**=0D + Frees the memory for each ImageRecordCodeSection within an ImageRecord=0D + and removes the entries from the list. It does not free the ImageRecord= =0D + itself.=0D +=0D + @param[in] ImageRecord The ImageRecord in which to free code sections=0D +**/=0D +STATIC=0D +VOID=0D +FreeImageRecordCodeSections (=0D + IMAGE_PROPERTIES_RECORD *ImageRecord=0D + )=0D +{=0D + LIST_ENTRY *CodeSegmentListHead;=0D + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;=0D +=0D + if (ImageRecord =3D=3D NULL) {=0D + return;=0D + }=0D +=0D + CodeSegmentListHead =3D &ImageRecord->CodeSegmentList;=0D + while (!IsListEmpty (CodeSegmentListHead)) {=0D + ImageRecordCodeSection =3D CR (=0D + CodeSegmentListHead->ForwardLink,=0D + IMAGE_PROPERTIES_RECORD_CODE_SECTION,=0D + Link,=0D + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE=0D + );=0D + RemoveEntryList (&ImageRecordCodeSection->Link);=0D + FreePool (ImageRecordCodeSection);=0D + }=0D +}=0D +=0D /**=0D Sort memory map entries based upon PhysicalStart from low to high.=0D =0D @@ -993,6 +1026,7 @@ CreateImagePropertiesRecord (=0D UINT8 *Name;=0D UINT32 SectionAlignment;=0D UINT32 PeCoffHeaderOffset;=0D + CHAR8 *PdbPointer;=0D =0D if ((ImageRecord =3D=3D NULL) || (ImageBase =3D=3D NULL)) {=0D return EFI_INVALID_PARAMETER;=0D @@ -1016,6 +1050,11 @@ CreateImagePropertiesRecord (=0D InitializeListHead (&ImageRecord->Link);=0D InitializeListHead (&ImageRecord->CodeSegmentList);=0D =0D + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageBase);=0D + if (PdbPointer !=3D NULL) {=0D + DEBUG ((DEBUG_ERROR, " Image - %a\n", PdbPointer));=0D + }=0D +=0D // Check PE/COFF image=0D DosHdr =3D (EFI_IMAGE_DOS_HEADER *)(UINTN)ImageBase;=0D PeCoffHeaderOffset =3D 0;=0D @@ -1084,7 +1123,8 @@ CreateImagePropertiesRecord (=0D // Record code section(s)=0D ImageRecordCodeSection =3D AllocatePool (sizeof (*ImageRecordCodeSec= tion));=0D if (ImageRecordCodeSection =3D=3D NULL) {=0D - return EFI_OUT_OF_RESOURCES;=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + goto CreateImagePropertiesRecordEnd;=0D }=0D =0D ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE;=0D @@ -1101,6 +1141,27 @@ CreateImagePropertiesRecord (=0D SortImageRecordCodeSection (ImageRecord);=0D }=0D =0D + //=0D + // Check overlap all section in ImageBase/Size=0D + //=0D + if (!IsImageRecordCodeSectionValid (ImageRecord)) {=0D + DEBUG ((DEBUG_ERROR, "IsImageRecordCodeSectionValid - FAIL\n"));=0D + Status =3D EFI_INVALID_PARAMETER;=0D + goto CreateImagePropertiesRecordEnd;=0D + }=0D +=0D + //=0D + // Round up the ImageSize, some CPU arch may return EFI_UNSUPPORTED if I= mageSize is not aligned.=0D + // Given that the loader always allocates full pages, we know the space = after the image is not used.=0D + //=0D + ImageRecord->ImageSize =3D ALIGN_VALUE (ImageRecord->ImageSize, EFI_PAGE= _SIZE);=0D +=0D +CreateImagePropertiesRecordEnd:=0D + if (EFI_ERROR (Status)) {=0D + // we failed to create a valid record, free the section memory that wa= s allocated=0D + FreeImageRecordCodeSections (ImageRecord);=0D + }=0D +=0D return Status;=0D }=0D =0D @@ -1117,24 +1178,7 @@ DeleteImagePropertiesRecord (=0D IN IMAGE_PROPERTIES_RECORD *ImageRecord=0D )=0D {=0D - LIST_ENTRY *CodeSegmentListHead;=0D - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;=0D -=0D - if (ImageRecord =3D=3D NULL) {=0D - return;=0D - }=0D -=0D - CodeSegmentListHead =3D &ImageRecord->CodeSegmentList;=0D - while (!IsListEmpty (CodeSegmentListHead)) {=0D - ImageRecordCodeSection =3D CR (=0D - CodeSegmentListHead->ForwardLink,=0D - IMAGE_PROPERTIES_RECORD_CODE_SECTION,=0D - Link,=0D - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE=0D - );=0D - RemoveEntryList (&ImageRecordCodeSection->Link);=0D - FreePool (ImageRecordCodeSection);=0D - }=0D + FreeImageRecordCodeSections (ImageRecord);=0D =0D if (!IsListEmpty (&ImageRecord->Link)) {=0D RemoveEntryList (&ImageRecord->Link);=0D -- =0D 2.40.1=0D =0D -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#116061): https://edk2.groups.io/g/devel/message/116061 Mute This Topic: https://groups.io/mt/104610771/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-