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 B286D78003C for ; Fri, 29 Mar 2024 20:21:27 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=Wo3m/qwnBkLPwCFKiupaSFIgkcOyPtwzJjyAfes/BQ8=; 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=1711743686; v=1; b=TNWAx3EEZus+en08px4jrZv81zkLPYiQiAxkrtvDrhPBmWDeP0oAKPB/LO9w5CB9P5f87L80 1AyhATw28lsuOif9WhchBllLjj9LLmyn2eGcf75BE8/vSI3Og6b994j/khYLRs9q/U3l/TFt5Ly m+xUW63uROU2mtBGHK4A5G6TwSk2k7jvxjRGlDKRO6pNb2W9Ic5McDwDfj/RjR4GDk3l/6JBSx5 neHFkMXQM2/r8hwxwgFDBOoVIulci3uklkMFds5e1ageHTy+MSgSjlRE+HplWZjHl9o/dz9Dm+y JmG3xxol2uipWqSPdRqktuRl5RfJxir2GOtT9cfHDqRqg== X-Received: by 127.0.0.2 with SMTP id 3Y4AYY7687511xvfG74c2Al4; Fri, 29 Mar 2024 13:21:26 -0700 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web10.975.1711743685541374713 for ; Fri, 29 Mar 2024 13:21:25 -0700 X-Received: from OSD-Desktop.redmond.corp.microsoft.com (unknown [131.107.159.43]) by linux.microsoft.com (Postfix) with ESMTPSA id 178DF20E6F5B; Fri, 29 Mar 2024 13:21:25 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 178DF20E6F5B From: "Oliver Smith-Denny" To: devel@edk2.groups.io Cc: Liming Gao , Taylor Beebe , Michael D Kinney , Ard Biesheuvel Subject: [edk2-devel] [PATCH v3 2/3] MdeModulePkg: ImagePropertiesRecordLib: Consolidate Usage Date: Fri, 29 Mar 2024 13:21:28 -0700 Message-Id: <20240329202129.12988-3-osde@linux.microsoft.com> In-Reply-To: <20240329202129.12988-1-osde@linux.microsoft.com> References: <20240329202129.12988-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: Fri, 29 Mar 2024 13:21:25 -0700 Reply-To: devel@edk2.groups.io,osde@linux.microsoft.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: sfUOlvi9WCP9scxEskiMGG16x7686176AA= 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=TNWAx3EE; 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 Acked-by: Michael D Kinney =0D Reviewed-by: Ard Biesheuvel =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 (#117241): https://edk2.groups.io/g/devel/message/117241 Mute This Topic: https://groups.io/mt/105223004/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-