From nobody Mon Sep 16 19:41:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+101937+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101937+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1679914936; cv=none; d=zohomail.com; s=zohoarc; b=hVDun8HINXEHPTqxQF1mj5YzK1OLrZDrH4kFhF8+kuWBidb3Y19X0eabU9zNTnTogWWR+AN4eTyuUzGWTu3HFsyMnz64hqcgQ3poLrwab/QFNpyy12jPiNTM+hsY96T/lwe7c/J3jmaB24xmYYpTQQ8/4QHvnZdAlC59uXoZqTA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1679914936; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=DCr9XjPlMJLP8NMSoJy5nju43IGeKmh4vt6I/vtZjwg=; b=Oh6x5aiqPYIOY/u3MjYsoOsoY6kN4auey2+2TGGBe+KjIw9i/iaJVbn4KsThW9iU3vuPkoynfeMciKE6eZeYLhCMMw1OEITDAqGIwtri3Qj4ozhXMdxqx2hs0DWfIOsnvgqD+6bPXvG1wYeu18/mN54en8zqUz5/ws959WJH6uw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101937+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1679914936909126.20475423604341; Mon, 27 Mar 2023 04:02:16 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id FfDZYY1788612xPzUuzgtQOt; Mon, 27 Mar 2023 04:02:16 -0700 X-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 X-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) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id DAC88C433D2; Mon, 27 Mar 2023 11:02:07 +0000 (UTC) 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: [edk2-devel] [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> In-Reply-To: <20230327110112.262503-1-ardb@kernel.org> References: <20230327110112.262503-1-ardb@kernel.org> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ardb@kernel.org X-Gm-Message-State: 8NtIZoKAexJWCnYRgtZeTZoIx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1679914936; bh=gsPqMb1EtPbj49y6kCp6qGwlq3Wt+9zt0ByzXpnMpSM=; h=Cc:Date:From:Reply-To:Subject:To; b=vatB/fVgZQiB+KI2+Y1FY4Ds8PDgmyWMqMVanu9Mm4gfvuJX/9kEBBV1GiMe8pyF+hc knqnkC/zhYoMCxFnoyiYMuKlE9grWwgvAhRKSWWcf1UX3SkxSxrRjHRaE3hw3icPKBmzu Ya1bDUqlNcCIVheRiAk8iWy8zIb6KqkoCk0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1679914937396100001 Content-Type: text/plain; charset="utf-8" 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 ( } } =20 +STATIC UINT16 mDllCharacteristicsEx; + +STATIC +VOID +ParseNoteSection ( + CONST Elf_Shdr *Shdr + ) +{ + CONST Elf_Note *Note; + CONST UINT32 *Prop; + UINT32 Prop0; + UINT32 Prop2; + + Note =3D (Elf_Note *)((UINT8 *)mEhdr + Shdr->sh_offset); + + if ((Note->n_type =3D=3D NT_GNU_PROPERTY_TYPE_0) && + (Note->n_namesz =3D=3D sizeof ("GNU")) && + (strcmp ((CHAR8 *)(Note + 1), "GNU") =3D=3D 0) && + (Note->n_descsz > sizeof (UINT32[2]))) { + Prop =3D (UINT32 *)((UINT8 *)(Note + 1) + sizeof("GNU")); + + switch (mEhdr->e_machine) { + case EM_AARCH64: + Prop0 =3D GNU_PROPERTY_AARCH64_FEATURE_1_AND; + Prop2 =3D GNU_PROPERTY_AARCH64_FEATURE_1_BTI; + break; + + case EM_X86_64: + Prop0 =3D GNU_PROPERTY_X86_FEATURE_1_AND; + Prop2 =3D GNU_PROPERTY_X86_FEATURE_1_IBT; + break; + + default: + return; + } + if ((Prop[0] =3D=3D Prop0) && + (Prop[1] >=3D sizeof (UINT32)) && + ((Prop[2] & Prop2) !=3D 0)) { + mDllCharacteristicsEx |=3D EFI_IMAGE_DLLCHARACTERISTICS_EX_FORWARD_C= FI_COMPAT; + } + } +} + // // Elf functions interface implementation // @@ -826,6 +869,13 @@ ScanSections64 ( } } =20 + for (i =3D 0; i < mEhdr->e_shnum; i++) { + Elf_Shdr *shdr =3D GetShdrByIndex(i); + if (shdr->sh_type =3D=3D 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/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. */ } Elf_Note; =20 +#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. */ --=20 2.39.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#101937): https://edk2.groups.io/g/devel/message/101937 Mute This Topic: https://groups.io/mt/97879299/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-