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.web11.105360.1680622843548907559 for ; Tue, 04 Apr 2023 08:40:43 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=khjUn6TM; 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 0772F6361D; Tue, 4 Apr 2023 15:40:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8BFEC433D2; Tue, 4 Apr 2023 15:40:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680622842; bh=adcnOxhZ0jKjrP4TGZM2AUx+t5zdkgQCfCRuPUKlKVY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=khjUn6TMSvp75owWVJZTbY4XImS34Vm6iyVxv5I1vrKJ+K3Kh+xgCc++o7Dgt9A/H f7KVgCqwO1WNJ4auLzqbU6VD0nxVrwJ8Xf2TnLOwqbAhd9jtwa2J94+wXHfXgu4oRX bFHeV/nohSiXGYGzsndV/YXMiY2boy/C3HdEsSQFxFwK9o5EHxM6I10iek7zxaK5PF k0F/be60qQ//7T3+rzpM5GBAUxgw9eiDNFWBO9t+MeGRUn6BxdCl01P7c7oKW5UWm3 YmQuneYAD3ocGeQi8Agi/WGdWyVGOkogMDCik2aGyFNlm5OuA7PYQHy3+S9x2qwkBQ tvy8f4a+qqj5A== 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 , Oliver Smith-Denny Subject: [PATCH v3 3/4] MdePkg/PeCoffLib: Capture DLL characteristics fields in image context Date: Tue, 4 Apr 2023 17:40:21 +0200 Message-Id: <20230404154022.2776035-4-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230404154022.2776035-1-ardb@kernel.org> References: <20230404154022.2776035-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 Reviewed-by: Leif Lindholm Reviewed-by: Oliver Smith-Denny --- 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..8646ff22b55faff0 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 + UINT32 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..74cceb37bf39ffc6 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 + UINT32 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