From patchwork Thu Apr 30 17:44:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 38890 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7116F3896815; Thu, 30 Apr 2020 17:44:29 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-eopbgr60040.outbound.protection.outlook.com [40.107.6.40]) by sourceware.org (Postfix) with ESMTPS id 6BF32383F86A for ; Thu, 30 Apr 2020 17:44:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6BF32383F86A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=Szabolcs.Nagy@arm.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=h3RQHAqYkcXuFETMFssHWSa/TRQITkgDgkfFAfWSA+A=; b=B1zzgmdFdaE4VhLHie8df6DLrf4DzOsUMm93NG8d5Prebt7YuG7ogzg2Af4OtlGwb8ghfCCCXGQKSOtreWZ1LD80F+jszUeZEiYbhnwWgyKZDbKqPBf8y4YxSq3prnzFqFXc3GV+YTKB9XLQCDQaJBg3DKV1iymiYMVhmXkLUvA= Received: from DB7PR03CA0106.eurprd03.prod.outlook.com (2603:10a6:10:72::47) by VI1PR08MB3695.eurprd08.prod.outlook.com (2603:10a6:803:c4::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2937.22; Thu, 30 Apr 2020 17:44:15 +0000 Received: from DB5EUR03FT022.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:72:cafe::c8) by DB7PR03CA0106.outlook.office365.com (2603:10a6:10:72::47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2958.20 via Frontend Transport; Thu, 30 Apr 2020 17:44:15 +0000 Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; sourceware.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; sourceware.org; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DB5EUR03FT022.mail.protection.outlook.com (10.152.20.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2958.20 via Frontend Transport; Thu, 30 Apr 2020 17:44:15 +0000 Received: ("Tessian outbound 5abcb386707e:v54"); Thu, 30 Apr 2020 17:44:15 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: a048f0d551e78beb X-CR-MTA-TID: 64aa7808 Received: from 54d91bd41124.2 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 0497847D-E009-4326-BEED-CDB495EBEF84.1; Thu, 30 Apr 2020 17:44:10 +0000 Received: from EUR05-AM6-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 54d91bd41124.2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 30 Apr 2020 17:44:10 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jRq+7kZZw7UEH5cLMXwR+8BFWn5LyFTyYf6P5GHE4qXL5KzUztlgBlu2sLapFbW0H4LL6qj9txWYOhDF2i7hyfbdnYL/pjpXXASoSfuzoa0mJuaycXmmt9ljBR/Chi0r3X2TKMWCPY4QgpLavsbvZJSWJnOK5GBpNwefBC78oAR8MskXZPg6tR7At0puMXpIiAadE+n5wIYtaWDgIabCKfaN+WrUre7fitYemgPf3aXP8OznFczOBIt+ATgIgnW6eiS/PwibJYR7i06fbBYzFXPQ72Nu3UHcnQauHgonw/fEg5Me/tL+2Dimj7oSqfZjbg9Tjyoo423ydNW0PApRsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=h3RQHAqYkcXuFETMFssHWSa/TRQITkgDgkfFAfWSA+A=; b=jJHvlv0qI9Tia9M7W4fbUNk+DBp3MjQuUaiOr3ifz8N4rQRuCarM2JW4y1Q5gUJ4R/riS1rpeQy9W7R1dyPwrya3AxCeJxgcVugElO27u3TYQR/eQJjA8ra4LcL1oP8OexngEoihHFremIc61RqJFhKXjKYtyM/vT7YT0335WHXvYVG6IPwPpHiVpM1196QgERGibW0ot9mO7LaOeySmA52GfrH2gaV+Gc4GOb8LZAjzjpr4hFL8TGfTRJMcVOSfk315wYvuB1xr2vULi+GroNQ/dPfnncstgC1pwr3I3LPjBogKV2GqWSmWw/vvHNTO+82g4kT0ZUMKe0REmgLv8g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=h3RQHAqYkcXuFETMFssHWSa/TRQITkgDgkfFAfWSA+A=; b=B1zzgmdFdaE4VhLHie8df6DLrf4DzOsUMm93NG8d5Prebt7YuG7ogzg2Af4OtlGwb8ghfCCCXGQKSOtreWZ1LD80F+jszUeZEiYbhnwWgyKZDbKqPBf8y4YxSq3prnzFqFXc3GV+YTKB9XLQCDQaJBg3DKV1iymiYMVhmXkLUvA= Authentication-Results-Original: sourceware.org; dkim=none (message not signed) header.d=none;sourceware.org; dmarc=none action=none header.from=arm.com; Received: from AM6PR08MB3047.eurprd08.prod.outlook.com (2603:10a6:209:4c::23) by AM6PR08MB3224.eurprd08.prod.outlook.com (2603:10a6:209:47::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2958.19; Thu, 30 Apr 2020 17:44:09 +0000 Received: from AM6PR08MB3047.eurprd08.prod.outlook.com ([fe80::49fd:6ded:4da7:8862]) by AM6PR08MB3047.eurprd08.prod.outlook.com ([fe80::49fd:6ded:4da7:8862%7]) with mapi id 15.20.2958.020; Thu, 30 Apr 2020 17:44:09 +0000 Date: Thu, 30 Apr 2020 18:44:07 +0100 From: Szabolcs Nagy To: libc-alpha@sourceware.org Subject: [PATCH 09/12] aarch64: support BTI enabled binaries Message-ID: <20200430174407.GE29015@arm.com> References: <20200430173458.GV29015@arm.com> In-Reply-To: <20200430173458.GV29015@arm.com> User-Agent: Mutt/1.9.4 (2018-02-28) X-ClientProxiedBy: LO2P265CA0271.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a1::19) To AM6PR08MB3047.eurprd08.prod.outlook.com (2603:10a6:209:4c::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from arm.com (217.140.106.55) by LO2P265CA0271.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2958.20 via Frontend Transport; Thu, 30 Apr 2020 17:44:08 +0000 X-Originating-IP: [217.140.106.55] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 91239c80-c0ef-4d05-33d8-08d7ed2e19e9 X-MS-TrafficTypeDiagnostic: AM6PR08MB3224:|AM6PR08MB3224:|VI1PR08MB3695: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:8882;OLM:8882; X-Forefront-PRVS: 0389EDA07F X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: LBYx95SxpQXHlliQdahcVVbtBl2fJYngmyNlQA+LoktIGtgSngHkO5HI/IBPBZfoLlGmHBl48l2D6NqFvO31vz/rQXQ8aDTUSfH8h5I7rTMB7E0U/AVgsT0U+RXXTJorIXab/2q/h3ZX9BUT7fNcz+P3+9HS33K4tu9uVXA7S57TXEJP7f9YQ7HGNatI/GMOdbGN4nmnA3gsGjKrVAVEHpnvt+JFIXcdQL+ZbsOKsCBgR9qIPcTShybv3aTsp4DFTzdeMTM7EvJw0s/q8JtJGqRnnF8aJX+K/RUgPUnpvaXZFtGHQu2WQMI1A1EJK+4aTbbYEwZS9ew47qBP18lj0mI+XBGQX0/LC1CK9IaLgZbBnma+62xtxNGK8Ge2a1gh X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM6PR08MB3047.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFTY:; SFS:(4636009)(346002)(376002)(366004)(396003)(136003)(39860400002)(1076003)(86362001)(33656002)(36756003)(66946007)(235185007)(66476007)(8886007)(55016002)(66556008)(66616009)(564344004)(478600001)(16526019)(26005)(186003)(52116002)(6916009)(44832011)(7696005)(33964004)(2906002)(4326008)(8676002)(8936002)(44144004)(956004)(2616005)(316002)(2700100001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 5EqIah2kKAKXhC0H/ief0ReatXCe27Pc3Nvzlp69AYAo+r8sFBYIcv5wRMznAMqxj1l7zc2wUqntLtj51NFFgEu+jqTWk9fAntdnUoaRmr0AUX6W+d/KZlA3P6wpggUNYVRqdtTvh55AdRkwaKX1DXNvSMhSoqwE7bPL82PJg0ziVyOquJrOxuFqTe5CQxbb1OXcETxDxoSQ0cvwv3ccXuqgDEnX3v82rONmH/VPwOXGQbauZXCtFck/VA4RY5iVUFacjNziuiWYRtPxCEirgJLj6XfyRu8WiRuQQEhYrY20eJizn0J5xa/Q2BD6SnqUwaEgn776w9Ac9hVV3WM49B3JlbL70hRShisx6W/RtxwIchXtmuQjp31fG5zhd0kScSFMFJ8SOoVzvpfqu0XDQzL+aQjCGjE1HctqOi2xMZVrMl0L5UWaxIRtk0Ka5W6fu8Ug5d2yIIVe519d1VAf5T9roi/jXeRj29Vm8vnpovoBYPv9v8KFn8lT6xs41T15bdTScqbF8cI9ImZPrkeA9ICQpPePQIH23f7XamD7lFDMW4BTvIjXDSglV/4BPwQahQ1B9Q6yaVSPgVzFQXNeWLV/SVEF2r/Y6KYCZoYFxpDl8x+dyOc+1bG3HVS5I7fdFoVBVUrHd3x/iS+hXx0il5aB7iRpBJntaDn8f2nEw4D3XOg00bWyjbSs/xLKtULLHGSLpeWkxcDJQ5kicws/Q6GRieZMo1s7V+eAXdz6mLJf97F0uR5fi6M61Ybj2/KkRjn5Hd7pceB4WsAJhm5MV75zj6gHpuB9TObr7NtV+Uk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB3224 Original-Authentication-Results: sourceware.org; dkim=none (message not signed) header.d=none; sourceware.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB5EUR03FT022.eop-EUR03.prod.protection.outlook.com X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFTY:; SFS:(4636009)(46966005)(47076004)(2616005)(956004)(1076003)(33656002)(44144004)(33964004)(26005)(8886007)(4326008)(498600001)(6916009)(44832011)(86362001)(8676002)(336012)(36756003)(70206006)(235185007)(55016002)(66616009)(2906002)(186003)(70586007)(16526019)(82310400002)(7696005)(356005)(81166007)(564344004)(8936002)(2700100001); DIR:OUT; SFP:1101; X-MS-Office365-Filtering-Correlation-Id-Prvs: b2ca3033-e59e-4434-9e1e-08d7ed2e1607 X-Forefront-PRVS: 0389EDA07F X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EgWFRvy+CTQGctwSMeMlKrHeVatPK4CN/XLw7xaQt4npFkR1mMl5m29qks58HqCLaGXgazTucOsqX4IYnvzYzQOF5sJJ9MbC0Xezk7leGMsZW4MgCnKfE18ulpq2iDzyk8rNWYJgh+Ijb1QqMtIwxUXlMUoQ6tdu1n5x04p4wV5dXM6cNE0KnbLfMHp/B8C4wgJHyAfUdHyIg5oUiWonTifumWC64+OVm5TqrRKIHVGpjj1kbyu9G07AWKFvT+mcKO3BRpKUyQ4UF0tPXTYfCe1ald8XxGWv0ugGn+WU4kyulqzicLrdu5IIfQ7RFyv5fw1SajzJVs08oW0mZrMj+Hfwupbn43Wckx8tkXLS2extB5XnNT91zrdZ8DSXacNXLgZORBGUft0SKzpLm2ojwCGRAwnxyaMjD1bZqlWog78= X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Apr 2020 17:44:15.5131 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 91239c80-c0ef-4d05-33d8-08d7ed2e19e9 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB3695 X-Spam-Status: No, score=-29.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Disposition: attachment; filename="0009-aarch64-support-BTI-enabled-binaries.patch" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Sudakshina Das Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" From 45c6bce5a691ecec9bba52785bd1f3a4cbc76fd4 Mon Sep 17 00:00:00 2001 From: Sudakshina Das Date: Tue, 17 Mar 2020 15:54:12 +0000 Subject: [PATCH 09/12] aarch64: support BTI enabled binaries Binaries can opt-in to using BTI via an ELF property marking. The dynamic linker has to then mprotect the executable segments with PROT_BTI. In case of static linked executables or in case of the dynamic linker itself, PROT_BTI protection is done by the operating system. On AArch64 glibc uses PT_GNU_PROPERTY instead of PT_NOTE to check the properties of a binary because PT_NOTE can be unreliable with old linkers. Co-authored-by: Szabolcs Nagy --- elf/dl-load.c | 2 + elf/rtld.c | 2 + sysdeps/aarch64/Makefile | 4 + sysdeps/aarch64/dl-bti.c | 54 ++++++ sysdeps/aarch64/dl-prop.h | 170 ++++++++++++++++++ sysdeps/aarch64/linkmap.h | 1 + sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h | 1 + sysdeps/unix/sysv/linux/aarch64/bits/mman.h | 31 ++++ .../unix/sysv/linux/aarch64/cpu-features.c | 3 + .../unix/sysv/linux/aarch64/cpu-features.h | 1 + 10 files changed, 269 insertions(+) create mode 100644 sysdeps/aarch64/dl-bti.c create mode 100644 sysdeps/aarch64/dl-prop.h create mode 100644 sysdeps/unix/sysv/linux/aarch64/bits/mman.h diff --git a/elf/dl-load.c b/elf/dl-load.c index a6b80f9395..0930250619 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1145,6 +1145,8 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, l->l_relro_size = ph->p_memsz; break; + case PT_GNU_PROPERTY: + /* Fall through. PT_GNU_PROPERTY holds property notes. */ case PT_NOTE: if (_dl_process_pt_note (l, ph, fd, fbp)) { diff --git a/elf/rtld.c b/elf/rtld.c index b2ea21c98b..88b8e74de0 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1505,6 +1505,8 @@ of this helper program; chances are you did not intend to run this program.\n\ main_map->l_relro_size = ph->p_memsz; break; + case PT_GNU_PROPERTY: + /* Fall through. PT_GNU_PROPERTY holds property notes. */ case PT_NOTE: if (_rtld_process_pt_note (main_map, ph)) _dl_error_printf ("\ diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile index 9cb141004d..5ae8b082b0 100644 --- a/sysdeps/aarch64/Makefile +++ b/sysdeps/aarch64/Makefile @@ -1,5 +1,9 @@ long-double-fcts = yes +ifeq ($(subdir),elf) +sysdep-dl-routines += dl-bti +endif + ifeq ($(subdir),elf) sysdep-dl-routines += tlsdesc dl-tlsdesc gen-as-const-headers += dl-link.sym diff --git a/sysdeps/aarch64/dl-bti.c b/sysdeps/aarch64/dl-bti.c new file mode 100644 index 0000000000..9ce697527d --- /dev/null +++ b/sysdeps/aarch64/dl-bti.c @@ -0,0 +1,54 @@ +/* AArch64 BTI initializers function. + Copyright (C) 2020 Free Software Foundation, Inc. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +static int +enable_bti (struct link_map *map, const char *program) +{ + const ElfW(Phdr) *phdr; + unsigned prot = PROT_READ | PROT_EXEC | PROT_BTI; + + for (phdr = map->l_phdr; phdr < &map->l_phdr[map->l_phnum]; ++phdr) + if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X)) + { + ElfW(Addr) start = phdr->p_vaddr + map->l_addr; + ElfW(Addr) len = phdr->p_memsz; + if (__mprotect ((void *)start, len, prot) < 0) + { + if (program) + _dl_fatal_printf ("%s: mprotect failed to turn on BTI\n", + map->l_name); + else + _dl_signal_error (EINVAL, map->l_name, "dlopen", + N_("mprotect failed to turn on BTI")); + } + } + return 0; +} + +/* Enable BTI for L if required. */ + +void +_dl_bti_check (struct link_map *l, const char *program) +{ + if (GLRO(dl_aarch64_cpu_features).bti && l->l_mach.bti_guarded) + enable_bti (l, program); +} diff --git a/sysdeps/aarch64/dl-prop.h b/sysdeps/aarch64/dl-prop.h new file mode 100644 index 0000000000..6662e4ab14 --- /dev/null +++ b/sysdeps/aarch64/dl-prop.h @@ -0,0 +1,170 @@ +/* Support for GNU properties. AArch64 version. + Copyright (C) 2018-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _DL_PROP_H +#define _DL_PROP_H + +#include + +extern void _dl_bti_check (struct link_map *, const char *) + attribute_hidden; + +static inline void __attribute__ ((always_inline)) +_rtld_main_check (struct link_map *m, const char *program) +{ + _dl_bti_check (m, program); +} + +static inline void __attribute__ ((always_inline)) +_dl_open_check (struct link_map *m) +{ + _dl_bti_check (m, 0); +} + +static inline void __attribute__ ((unused)) +_dl_process_aarch64_property (struct link_map *l, + const ElfW(Nhdr) *note, + const ElfW(Addr) size, + const ElfW(Addr) align) +{ + /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in + 32-bit objects and to 8 bytes in 64-bit objects. Skip notes + with incorrect alignment. */ + if (align != (__ELF_NATIVE_CLASS / 8)) + return; + + const ElfW(Addr) start = (ElfW(Addr)) note; + + unsigned int feature_1 = 0; + unsigned int last_type = 0; + + while ((ElfW(Addr)) (note + 1) - start < size) + { + /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ + if (note->n_namesz == 4 + && note->n_type == NT_GNU_PROPERTY_TYPE_0 + && memcmp (note + 1, "GNU", 4) == 0) + { + /* Check for invalid property. */ + if (note->n_descsz < 8 + || (note->n_descsz % sizeof (ElfW(Addr))) != 0) + return; + + /* Start and end of property array. */ + unsigned char *ptr = (unsigned char *) (note + 1) + 4; + unsigned char *ptr_end = ptr + note->n_descsz; + + do + { + unsigned int type = *(unsigned int *) ptr; + unsigned int datasz = *(unsigned int *) (ptr + 4); + + /* Property type must be in ascending order. */ + if (type < last_type) + return; + + ptr += 8; + if ((ptr + datasz) > ptr_end) + return; + + last_type = type; + + if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) + { + /* The size of GNU_PROPERTY_AARCH64_FEATURE_1_AND is 4 + bytes. When seeing GNU_PROPERTY_AARCH64_FEATURE_1_AND, + we stop the search regardless if its size is correct + or not. There is no point to continue if this note + is ill-formed. */ + if (datasz != 4) + return; + + feature_1 = *(unsigned int *) ptr; + if ((feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) + l->l_mach.bti_guarded = true; + + /* Stop if we found the property note. */ + return; + } + else if (type > GNU_PROPERTY_AARCH64_FEATURE_1_AND) + { + /* Stop since property type is in ascending order. */ + return; + } + + /* Check the next property item. */ + ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); + } + while ((ptr_end - ptr) >= 8); + } + + /* NB: Note sections like .note.ABI-tag and .note.gnu.build-id are + aligned to 4 bytes in 64-bit ELF objects. */ + note = ((const void *) note + + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz, + align)); + } +} + +#ifdef FILEBUF_SIZE +static inline int __attribute__ ((unused)) +_dl_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph, + int fd, struct filebuf *fbp) +{ + if (ph->p_type != PT_GNU_PROPERTY) + return 0; + + const ElfW(Nhdr) *note; + ElfW(Nhdr) *note_malloced = NULL; + ElfW(Addr) size = ph->p_filesz; + + if (ph->p_offset + size <= (size_t) fbp->len) + note = (const void *) (fbp->buf + ph->p_offset); + else + { + if (size < __MAX_ALLOCA_CUTOFF) + note = alloca (size); + else + note = note_malloced = malloc (size); + if (note == NULL) + return -1; + if (__pread64_nocancel (fd, (void *) note, size, ph->p_offset) != size) + { + if (note_malloced) + free (note_malloced); + return -1; + } + } + _dl_process_aarch64_property (l, note, ph->p_filesz, ph->p_align); + if (note_malloced) + free (note_malloced); + return 0; +} +#endif + +static inline int __attribute__ ((unused)) +_rtld_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph) +{ + if (ph->p_type != PT_GNU_PROPERTY) + return 0; + const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); + _dl_process_aarch64_property (l, note, ph->p_memsz, ph->p_align); + return 0; +} + +#endif /* _DL_PROP_H */ diff --git a/sysdeps/aarch64/linkmap.h b/sysdeps/aarch64/linkmap.h index 943a9ee9e4..cc196512d7 100644 --- a/sysdeps/aarch64/linkmap.h +++ b/sysdeps/aarch64/linkmap.h @@ -20,4 +20,5 @@ struct link_map_machine { ElfW(Addr) plt; /* Address of .plt */ void *tlsdesc_table; /* Address of TLS descriptor hash table. */ + int bti_guarded; /* Branch Target Identification mechanism enabled. */ }; diff --git a/sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h b/sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h index 4ee14b4208..af90d8a626 100644 --- a/sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h +++ b/sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h @@ -72,3 +72,4 @@ #define HWCAP2_BF16 (1 << 14) #define HWCAP2_DGH (1 << 15) #define HWCAP2_RNG (1 << 16) +#define HWCAP2_BTI (1 << 17) diff --git a/sysdeps/unix/sysv/linux/aarch64/bits/mman.h b/sysdeps/unix/sysv/linux/aarch64/bits/mman.h new file mode 100644 index 0000000000..ecae046344 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/bits/mman.h @@ -0,0 +1,31 @@ +/* Definitions for POSIX memory map interface. Linux/AArch64 version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SYS_MMAN_H +# error "Never use directly; include instead." +#endif + +/* AArch64 specific definitions, should be in sync with + arch/arm64/include/uapi/asm/mman.h. */ + +#define PROT_BTI 0x10 + +#include + +/* Include generic Linux declarations. */ +#include diff --git a/sysdeps/unix/sysv/linux/aarch64/cpu-features.c b/sysdeps/unix/sysv/linux/aarch64/cpu-features.c index 896c588fee..c2385fb498 100644 --- a/sysdeps/unix/sysv/linux/aarch64/cpu-features.c +++ b/sysdeps/unix/sysv/linux/aarch64/cpu-features.c @@ -83,4 +83,7 @@ init_cpu_features (struct cpu_features *cpu_features) if ((dczid & DCZID_DZP_MASK) == 0) cpu_features->zva_size = 4 << (dczid & DCZID_BS_MASK); + + /* Check if BTI is enabled. */ + cpu_features->bti = (GLRO (dl_hwcap2) & HWCAP2_BTI); } diff --git a/sysdeps/unix/sysv/linux/aarch64/cpu-features.h b/sysdeps/unix/sysv/linux/aarch64/cpu-features.h index 1389cea1b3..88983eb723 100644 --- a/sysdeps/unix/sysv/linux/aarch64/cpu-features.h +++ b/sysdeps/unix/sysv/linux/aarch64/cpu-features.h @@ -64,6 +64,7 @@ struct cpu_features { uint64_t midr_el1; unsigned zva_size; + int bti; }; #endif /* _CPU_FEATURES_AARCH64_H */ -- 2.17.1