From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web10.31906.1679914939933390791 for ; Mon, 27 Mar 2023 04:02:20 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KQJiv3Z5; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6D978611AF; Mon, 27 Mar 2023 11:02:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5D94C433D2; Mon, 27 Mar 2023 11:02:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679914939; bh=VDIVj0flgriBTMRO7bvIaIYz9hlmiYxm98FkEHy38FY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KQJiv3Z5yY/jiBP9q5GW5s2F4vfOyQmKYxNDZ1WeJKo6+KHL3zqAF534MGIpQQOSP H5orH48AGtxZyzmpmehxgZPOna1cyKdRdrciiT4srF43Z3klaqnSoZ6fc+MZ6swzrU CNd3rH0t4kFlu6VRemV0ch9PEIBO6/mI89pI64HLAa3cSkudbYEyqUleoWkFeQG85v ThRyvh/knqoB5oB+jsrdlPdoVYS8c6xD/lFK0j35+r5ab9t9Q/86Gj41pXzhuIDiQF a5kkRTo/+PzFHdpkke8UmxUJvaRAxH/tIbgMxal4XbrJWKQs4GQ0hL36VBMMMijQNW Sug/E9lbOgPMQ== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe , =?UTF-8?q?Marvin=20H=C3=A4user?= , Bob Feng Subject: [PATCH v2 16/17] MdePkg/PeCoffLib: Capture DLL characteristics fieldis in image context Date: Mon, 27 Mar 2023 13:01:11 +0200 Message-Id: <20230327110112.262503-17-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230327110112.262503-1-ardb@kernel.org> References: <20230327110112.262503-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable When loading a PE/COFF image, capture the DLL characteristics fields of the header into our image context structure so we can refer to them when mapping the image. Signed-off-by: Ard Biesheuvel --- MdePkg/Include/IndustryStandard/PeImage.h | 13 +++++- MdePkg/Include/Library/PeCoffLib.h | 6 +++ MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 46 +++++++++++++++----- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/MdePkg/Include/IndustryStandard/PeImage.h b/MdePkg/Include/Ind= ustryStandard/PeImage.h index dd4cc25483bc4bcf..a5b9b848ed391f98 100644 --- a/MdePkg/Include/IndustryStandard/PeImage.h +++ b/MdePkg/Include/IndustryStandard/PeImage.h @@ -625,7 +625,8 @@ typedef struct { UINT32 FileOffset; ///< The file pointer to the debug data.=0D } EFI_IMAGE_DEBUG_DIRECTORY_ENTRY;=0D =0D -#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2 ///< The Visual C++ debug info= rmation.=0D +#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2 ///< The Visual C= ++ debug information.=0D +#define EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS 20=0D =0D ///=0D /// Debug Data Structure defined in Microsoft C++.=0D @@ -669,6 +670,16 @@ typedef struct { //=0D } EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY;=0D =0D +///=0D +/// Extended DLL Characteristics=0D +///=0D +#define EFI_IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT 0x0001=0D +#define EFI_IMAGE_DLLCHARACTERISTICS_EX_FORWARD_CFI_COMPAT 0x0040=0D +=0D +typedef struct {=0D + UINT16 DllCharacteristicsEx;=0D +} EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY;=0D +=0D ///=0D /// Resource format.=0D ///=0D diff --git a/MdePkg/Include/Library/PeCoffLib.h b/MdePkg/Include/Library/Pe= CoffLib.h index b45879453785c77d..d21c932076c072f6 100644 --- a/MdePkg/Include/Library/PeCoffLib.h +++ b/MdePkg/Include/Library/PeCoffLib.h @@ -171,6 +171,12 @@ typedef struct { ///=0D UINT16 ImageType;=0D ///=0D + /// Set by PeCoffLoaderGetImageInfo() to the DLL flags stored in the PE/= COFF header and=0D + /// in the DllCharacteristicsEx debug table.=0D + ///=0D + UINT16 DllCharacteristics;=0D + UINT16 DllCharacteristicsEx;=0D + ///=0D /// Set by PeCoffLoaderGetImageInfo() to TRUE if the PE/COFF image does = not contain=0D /// relocation information.=0D ///=0D diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/Bas= ePeCoffLib/BasePeCoff.c index 97a8aaf8c73d3e3c..4b71176a0c7c2ed0 100644 --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c @@ -308,10 +308,11 @@ PeCoffLoaderGetPeHeader ( //=0D // Use PE32 offset=0D //=0D - ImageContext->ImageType =3D Hdr.Pe32->OptionalHeader.Subsyste= m;=0D - ImageContext->ImageSize =3D (UINT64)Hdr.Pe32->OptionalHeader.= SizeOfImage;=0D - ImageContext->SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionA= lignment;=0D - ImageContext->SizeOfHeaders =3D Hdr.Pe32->OptionalHeader.SizeOfHe= aders;=0D + ImageContext->ImageType =3D Hdr.Pe32->OptionalHeader.Subsys= tem;=0D + ImageContext->ImageSize =3D (UINT64)Hdr.Pe32->OptionalHeade= r.SizeOfImage;=0D + ImageContext->SectionAlignment =3D Hdr.Pe32->OptionalHeader.Sectio= nAlignment;=0D + ImageContext->SizeOfHeaders =3D Hdr.Pe32->OptionalHeader.SizeOf= Headers;=0D + ImageContext->DllCharacteristics =3D Hdr.Pe32->OptionalHeader.DllCha= racteristics;=0D } else if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL= _HDR64_MAGIC) {=0D //=0D // 1. Check FileHeader.NumberOfRvaAndSizes filed.=0D @@ -429,10 +430,11 @@ PeCoffLoaderGetPeHeader ( //=0D // Use PE32+ offset=0D //=0D - ImageContext->ImageType =3D Hdr.Pe32Plus->OptionalHeader.Subs= ystem;=0D - ImageContext->ImageSize =3D (UINT64)Hdr.Pe32Plus->OptionalHea= der.SizeOfImage;=0D - ImageContext->SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.Sect= ionAlignment;=0D - ImageContext->SizeOfHeaders =3D Hdr.Pe32Plus->OptionalHeader.Size= OfHeaders;=0D + ImageContext->ImageType =3D Hdr.Pe32Plus->OptionalHeader.Su= bsystem;=0D + ImageContext->ImageSize =3D (UINT64)Hdr.Pe32Plus->OptionalH= eader.SizeOfImage;=0D + ImageContext->SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.Se= ctionAlignment;=0D + ImageContext->SizeOfHeaders =3D Hdr.Pe32Plus->OptionalHeader.Si= zeOfHeaders;=0D + ImageContext->DllCharacteristics =3D Hdr.Pe32Plus->OptionalHeader.Dl= lCharacteristics;=0D } else {=0D ImageContext->ImageError =3D IMAGE_ERROR_INVALID_MACHINE_TYPE;=0D return RETURN_UNSUPPORTED;=0D @@ -545,8 +547,9 @@ PeCoffLoaderGetPeHeader ( Retrieves information about a PE/COFF image.=0D =0D Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, Ima= geSize,=0D - DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders= , and=0D - DebugDirectoryEntryRva fields of the ImageContext structure.=0D + DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders= ,=0D + DllCharacteristics, DllCharacteristicsEx and DebugDirectoryEntryRva fiel= ds of=0D + the ImageContext structure.=0D If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.=0D If the PE/COFF image accessed through the ImageRead service in the Image= Context=0D structure is not a supported PE/COFF image type, then return RETURN_UNSU= PPORTED.=0D @@ -752,7 +755,28 @@ PeCoffLoaderGetImageInfo ( ImageContext->ImageSize +=3D DebugEntry.SizeOfData;=0D }=0D =0D - return RETURN_SUCCESS;=0D + continue;=0D + }=0D +=0D + if (DebugEntry.Type =3D=3D EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERI= STICS) {=0D + Size =3D sizeof (EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENT= RY);=0D + ReadSize =3D sizeof (EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENT= RY);=0D + Status =3D ImageContext->ImageRead (=0D + ImageContext->Handle,=0D + DebugEntry.FileOffset,=0D + &Size,=0D + &ImageContext->DllCharacteristicsEx= =0D + );=0D + if (RETURN_ERROR (Status) || (Size !=3D ReadSize)) {=0D + ImageContext->ImageError =3D IMAGE_ERROR_IMAGE_READ;=0D + if (Size !=3D ReadSize) {=0D + Status =3D RETURN_UNSUPPORTED;=0D + }=0D +=0D + return Status;=0D + }=0D +=0D + continue;=0D }=0D }=0D }=0D --=20 2.39.2