From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by mx.groups.io with SMTP id smtpd.web10.31904.1679914935350134566 for ; Mon, 27 Mar 2023 04:02:15 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RBjnjR8b; spf=pass (domain: kernel.org, ip: 145.40.73.55, 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 sin.source.kernel.org (Postfix) with ESMTPS id 2FCA2CE0E4D; Mon, 27 Mar 2023 11:02:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DAC88C433D2; Mon, 27 Mar 2023 11:02:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679914930; bh=OazCBC5GgmiqZ0mO6WHyalFYwqrEqD036t8Pd48Q/20=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RBjnjR8b2L6+30ktMlihx3tLL78lnOKkYJ+RyjPKAJxgWdKGk/G6hNlQK9YRGO36s ksOcu7KkMYuhjwFo+OravNhIQMAS5elxsGUbPH8W1o9nct9n0RUkpU8VK6/mXh7aLA AOGZ6iHIQ7u16kqGRsTGCJesGaWJFoYGWJ72LR8JC3qfw2mxj5l4rrx4yjO0gcesQz Tqtwo5CcweMRZOqGirv0oIgQEDbOHA56EOluHPBUH0Ni1sH0J8SFV3/HxuUGqn22Pu 4VWj+RQejoCoy/sizCHcVao/14J7fgXXz6ORpwlcqW/zFTIuVobFO9yQWJeAInj4qX ahMc3XFerfusA== 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 13/17] BaseTools/GenFw: Parse IBT/BTI support status from ELF note Date: Mon, 27 Mar 2023 13:01:08 +0200 Message-Id: <20230327110112.262503-14-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 performing ELF to PE/COFF conversion, parse any notes sections to decide whether the image supports forward CFI landing pads. This will be used to set the associated DllCharacteristicsEx flag in a subsequent patch. Signed-off-by: Ard Biesheuvel --- BaseTools/Source/C/GenFw/Elf64Convert.c | 50 ++++++++++++++++++++ BaseTools/Source/C/GenFw/elf_common.h | 9 ++++ 2 files changed, 59 insertions(+) diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/G= enFw/Elf64Convert.c index 8b50774beb1eebda..2a810e835d4a4a66 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -770,6 +770,49 @@ WriteSectionRiscV64 ( }=0D }=0D =0D +STATIC UINT16 mDllCharacteristicsEx;=0D +=0D +STATIC=0D +VOID=0D +ParseNoteSection (=0D + CONST Elf_Shdr *Shdr=0D + )=0D +{=0D + CONST Elf_Note *Note;=0D + CONST UINT32 *Prop;=0D + UINT32 Prop0;=0D + UINT32 Prop2;=0D +=0D + Note =3D (Elf_Note *)((UINT8 *)mEhdr + Shdr->sh_offset);=0D +=0D + if ((Note->n_type =3D=3D NT_GNU_PROPERTY_TYPE_0) &&=0D + (Note->n_namesz =3D=3D sizeof ("GNU")) &&=0D + (strcmp ((CHAR8 *)(Note + 1), "GNU") =3D=3D 0) &&=0D + (Note->n_descsz > sizeof (UINT32[2]))) {=0D + Prop =3D (UINT32 *)((UINT8 *)(Note + 1) + sizeof("GNU"));=0D +=0D + switch (mEhdr->e_machine) {=0D + case EM_AARCH64:=0D + Prop0 =3D GNU_PROPERTY_AARCH64_FEATURE_1_AND;=0D + Prop2 =3D GNU_PROPERTY_AARCH64_FEATURE_1_BTI;=0D + break;=0D +=0D + case EM_X86_64:=0D + Prop0 =3D GNU_PROPERTY_X86_FEATURE_1_AND;=0D + Prop2 =3D GNU_PROPERTY_X86_FEATURE_1_IBT;=0D + break;=0D +=0D + default:=0D + return;=0D + }=0D + if ((Prop[0] =3D=3D Prop0) &&=0D + (Prop[1] >=3D sizeof (UINT32)) &&=0D + ((Prop[2] & Prop2) !=3D 0)) {=0D + mDllCharacteristicsEx |=3D EFI_IMAGE_DLLCHARACTERISTICS_EX_FORWARD_C= FI_COMPAT;=0D + }=0D + }=0D +}=0D +=0D //=0D // Elf functions interface implementation=0D //=0D @@ -826,6 +869,13 @@ ScanSections64 ( }=0D }=0D =0D + for (i =3D 0; i < mEhdr->e_shnum; i++) {=0D + Elf_Shdr *shdr =3D GetShdrByIndex(i);=0D + if (shdr->sh_type =3D=3D SHT_NOTE) {=0D + ParseNoteSection (shdr);=0D + }=0D + }=0D +=0D //=0D // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT=0D //=0D diff --git a/BaseTools/Source/C/GenFw/elf_common.h b/BaseTools/Source/C/Gen= Fw/elf_common.h index 7b7fdeb3290dfa88..ccd32804b090a226 100644 --- a/BaseTools/Source/C/GenFw/elf_common.h +++ b/BaseTools/Source/C/GenFw/elf_common.h @@ -59,6 +59,15 @@ typedef struct { UINT32 n_type; /* Type of this note. */=0D } Elf_Note;=0D =0D +#define NT_GNU_PROPERTY_TYPE_0 5=0D +=0D +#define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002=0D +#define GNU_PROPERTY_X86_FEATURE_1_IBT 0x1=0D +=0D +#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000=0D +#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 0x1=0D +#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 0x2=0D +=0D /* Indexes into the e_ident array. Keep synced with=0D http://www.sco.com/developers/gabi/latest/ch4.eheader.html */=0D #define EI_MAG0 0 /* Magic number, byte 0. */=0D --=20 2.39.2