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 CA59C941C0D for ; Mon, 11 Mar 2024 21:29:30 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=yfAPH+9i+T3pjdv7oJi/lBvqM3CyODxm3H63VfrgKK4=; 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:Resent-Date:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1710192569; v=1; b=TgZj0YFpuq3L+cowopZxfbaToVEUOTVEuNs5JiXTapkJTBKknDsdWSjCSUTqcAkmud+Y2pji BjlkpzXf7ExXUKQNG0aetapToUnaRwsBy0EGCAPsKbgiJpUASPo/iM2F3P4r7tg0+8nK7ZUooTr OKl7NdwKaKSeCWksENsrw9H6H5Jdm6KMrCpgenFeWWmS43Ji7MMOBXnLrSm3qhQun4vDSiAhiPG r6m/epZ+j8NKa7gvOPSv2yPmAxy+QI6yZ6pnkjOw/wLRUaC22htAhrArJSj1EqJuCAisnhcraH3 omSc/SRgogAIzdzhsCSutFfd0vWjTxFQ2N9nnJcfxgheQ== X-Received: by 127.0.0.2 with SMTP id SeauYY7687511xrIqV0RoFny; Mon, 11 Mar 2024 14:29:29 -0700 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web11.5297.1710192567250316269 for ; Mon, 11 Mar 2024 14:29:27 -0700 X-Received: from OSD-Desktop.redmond.corp.microsoft.com (unknown [131.107.160.171]) by linux.microsoft.com (Postfix) with ESMTPSA id 87CCD20B74C3; Mon, 11 Mar 2024 14:29:26 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 87CCD20B74C3 From: "Oliver Smith-Denny" To: devel@edk2.groups.io Cc: Liming Gao , Taylor Beebe Subject: [edk2-devel] [PATCH v2 2/3] MdeModulePkg: ImagePropertiesRecordLib: Consolidate Usage Date: Mon, 11 Mar 2024 14:29:23 -0700 Message-Id: <20240311212924.11633-3-osde@linux.microsoft.com> In-Reply-To: <20240311212924.11633-1-osde@linux.microsoft.com> References: <20240311212924.11633-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 Resent-Date: Mon, 11 Mar 2024 14:29:27 -0700 Reply-To: devel@edk2.groups.io,osde@linux.microsoft.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: sgQc9Be0T3bk10xS9CU7Sdxzx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=TgZj0YFp; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=linux.microsoft.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io 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 763a8d65d565..3ac043f98098 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 @@ -1103,6 +1143,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 @@ -1119,24 +1180,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 (#116660): https://edk2.groups.io/g/devel/message/116660 Mute This Topic: https://groups.io/mt/104873194/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-