From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web11.120653.1680659866995635530 for ; Tue, 04 Apr 2023 18:57:47 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=dwAyzbiE; spf=pass (domain: linux.microsoft.com, ip: 13.77.154.182, mailfrom: mikuback@linux.microsoft.com) Received: from [192.168.4.22] (unknown [47.201.8.94]) by linux.microsoft.com (Postfix) with ESMTPSA id 2BD9A210DEA4; Tue, 4 Apr 2023 18:57:45 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2BD9A210DEA4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680659866; bh=taAo5JdBvSZK+6LcdBnPifgEUD9DgWXQ0TZD2gqdREw=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=dwAyzbiESYbT8BF7aFUzQbkI0y07BZKTbwjyGUlqWFO5iKI0nYwMpp/4/d0tJVWOF oyfmVrChs1eOH3uNEzLkFNLbhN2WusmbsTz9RYUUPZmhyhW1i4+c6mrI3swuRmkqE8 yFnuWuWsSQseFE6goMxiZpaf4XZHLfb5cJ7Hg3jw= Message-ID: <9401cd23-134c-b9fd-6c33-306510ed7cb0@linux.microsoft.com> Date: Tue, 4 Apr 2023 21:57:44 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [edk2-devel] [PATCH v3 1/4] BaseTools/GenFw: Parse IBT/BTI support status from ELF note To: devel@edk2.groups.io, ardb@kernel.org Cc: Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe , =?UTF-8?Q?Marvin_H=c3=a4user?= , Bob Feng , Oliver Smith-Denny References: <20230404154022.2776035-1-ardb@kernel.org> <20230404154022.2776035-2-ardb@kernel.org> From: "Michael Kubacki" In-Reply-To: <20230404154022.2776035-2-ardb@kernel.org> Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Acked-by: Michael Kubacki On 4/4/2023 11:40 AM, Ard Biesheuvel wrote: > 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 > Reviewed-by: Leif Lindholm > Reviewed-by: Oliver Smith-Denny > --- > 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/GenFw/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 ( > } > > } > > > > +STATIC UINT16 mDllCharacteristicsEx; > > + > > +STATIC > > +VOID > > +ParseNoteSection ( > > + CONST Elf_Shdr *Shdr > > + ) > > +{ > > + CONST Elf_Note *Note; > > + CONST UINT32 *Prop; > > + UINT32 Prop0; > > + UINT32 Prop2; > > + > > + Note = (Elf_Note *)((UINT8 *)mEhdr + Shdr->sh_offset); > > + > > + if ((Note->n_type == NT_GNU_PROPERTY_TYPE_0) && > > + (Note->n_namesz == sizeof ("GNU")) && > > + (strcmp ((CHAR8 *)(Note + 1), "GNU") == 0) && > > + (Note->n_descsz > sizeof (UINT32[2]))) { > > + Prop = (UINT32 *)((UINT8 *)(Note + 1) + sizeof("GNU")); > > + > > + switch (mEhdr->e_machine) { > > + case EM_AARCH64: > > + Prop0 = GNU_PROPERTY_AARCH64_FEATURE_1_AND; > > + Prop2 = GNU_PROPERTY_AARCH64_FEATURE_1_BTI; > > + break; > > + > > + case EM_X86_64: > > + Prop0 = GNU_PROPERTY_X86_FEATURE_1_AND; > > + Prop2 = GNU_PROPERTY_X86_FEATURE_1_IBT; > > + break; > > + > > + default: > > + return; > > + } > > + if ((Prop[0] == Prop0) && > > + (Prop[1] >= sizeof (UINT32)) && > > + ((Prop[2] & Prop2) != 0)) { > > + mDllCharacteristicsEx |= EFI_IMAGE_DLLCHARACTERISTICS_EX_FORWARD_CFI_COMPAT; > > + } > > + } > > +} > > + > > // > > // Elf functions interface implementation > > // > > @@ -826,6 +869,13 @@ ScanSections64 ( > } > > } > > > > + for (i = 0; i < mEhdr->e_shnum; i++) { > > + Elf_Shdr *shdr = GetShdrByIndex(i); > > + if (shdr->sh_type == SHT_NOTE) { > > + ParseNoteSection (shdr); > > + } > > + } > > + > > // > > // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT > > // > > diff --git a/BaseTools/Source/C/GenFw/elf_common.h b/BaseTools/Source/C/GenFw/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. */ > > } Elf_Note; > > > > +#define NT_GNU_PROPERTY_TYPE_0 5 > > + > > +#define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 > > +#define GNU_PROPERTY_X86_FEATURE_1_IBT 0x1 > > + > > +#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000 > > +#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 0x1 > > +#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 0x2 > > + > > /* Indexes into the e_ident array. Keep synced with > > http://www.sco.com/developers/gabi/latest/ch4.eheader.html */ > > #define EI_MAG0 0 /* Magic number, byte 0. */ >