From patchwork Wed Dec 2 08:55:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 41251 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 BEC3F3959E55; Wed, 2 Dec 2020 08:56:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BEC3F3959E55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1606899369; bh=bo4g1VjjeNpD+m+0h54bDJSCv5eSt52W6wWRAK1/PYM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=EiDVUVkVj3OQaj2FiPDqjYPlcuB1e6bUn4UfMx1ttSwR+miJ1wZX7wg0LO6vBJLWd +GEed9ykwIErhByJAehrHGqMUFqb+ha+6n1gEEHLwB+qdb/F1PSDG92prsElig7eCK LPVtRAXAi33r8jlfgt/06laxUwvTjHIujBMJV07s= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-eopbgr150085.outbound.protection.outlook.com [40.107.15.85]) by sourceware.org (Postfix) with ESMTPS id D0BDE386101E for ; Wed, 2 Dec 2020 08:56:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D0BDE386101E Received: from DB6PR0601CA0038.eurprd06.prod.outlook.com (2603:10a6:4:17::24) by AM6PR08MB2968.eurprd08.prod.outlook.com (2603:10a6:209:43::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3611.25; Wed, 2 Dec 2020 08:56:04 +0000 Received: from DB5EUR03FT008.eop-EUR03.prod.protection.outlook.com (2603:10a6:4:17:cafe::a4) by DB6PR0601CA0038.outlook.office365.com (2603:10a6:4:17::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.17 via Frontend Transport; Wed, 2 Dec 2020 08:56:04 +0000 X-MS-Exchange-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=pass 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 DB5EUR03FT008.mail.protection.outlook.com (10.152.20.98) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.17 via Frontend Transport; Wed, 2 Dec 2020 08:56:04 +0000 Received: ("Tessian outbound eeda57fffe7b:v71"); Wed, 02 Dec 2020 08:56:04 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: cc848dc2bf9ee73f X-CR-MTA-TID: 64aa7808 Received: from 0fa8f2697906.2 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 37FA4EAB-0DB6-468C-BFA6-02371FBAA784.1; Wed, 02 Dec 2020 08:55:47 +0000 Received: from EUR03-DB5-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 0fa8f2697906.2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 02 Dec 2020 08:55:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F1/1/+BFQhLpclKryv/5T9Sqn2L1R9HY1wAPVRAxi8kcy1JI6QIOpY3Thq4jqB6JxAjn78UodcJq9wEaNlCMq+a5wrgK4J2dBTANCTdc5Mg51z4InKoyIUHa2ckirN9FhJw/GTabp+MU0SUiUol8yxvw/pnQNBMs4oRUGgVca/FLhW+msWhBK0nSBioEEEHkSU/a37HCKKP8Bkopj/DvQ02jzsRiL0GEnRjDaNdI91Nj8fPk13yzSk7roy5kjDPikG4eScSmoYaOQtOCZ+NL4v3Oy5PapvQzh2x3OvulxvcScl70KhuG7x40fAuRKg7oWYjn8eOPdXX3s+xtC7C1sA== 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=bo4g1VjjeNpD+m+0h54bDJSCv5eSt52W6wWRAK1/PYM=; b=aiszp8W6+gV9pf6VFMGKYbHYJZvH56b+uhhSJQTvrF9KJnnd6vzrBHf6rj3m0Lb1Ob7uy93M3e/9qjWBOQv6URfu/DO3bb9sY8oc2Ifz27Q8ZXfW+XUw/9PZ2Vcc+w/ndDH9H+Fz+EvvLa5R6+8qn4gPVa2v/Wl4gXr+b/PdshWYTU6lcUrL8RtVODUYrJW/4Te09S9TGArIh0iKN9qUbJGqxO8DbrlX4ijPzltPpRMGFAIopP9Tq8PK+tGloh/MN6F5JkMZ0OVeUUCJ4JyecbYXdCRhMgAK+hpN9PX4xJwp0VvQGNQ4L2gwbHN5lTVtrFTM/fPkQFtYct7dt1kBtg== 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 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 PR3PR08MB5564.eurprd08.prod.outlook.com (2603:10a6:102:87::18) by PR3PR08MB5644.eurprd08.prod.outlook.com (2603:10a6:102:86::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.17; Wed, 2 Dec 2020 08:55:46 +0000 Received: from PR3PR08MB5564.eurprd08.prod.outlook.com ([fe80::ac13:db5:ef4:2dd2]) by PR3PR08MB5564.eurprd08.prod.outlook.com ([fe80::ac13:db5:ef4:2dd2%4]) with mapi id 15.20.3611.025; Wed, 2 Dec 2020 08:55:46 +0000 To: libc-alpha@sourceware.org Subject: [PATCH v3 2/2] aarch64: Use mmap to add PROT_BTI instead of mprotect [BZ #26831] Date: Wed, 2 Dec 2020 08:55:40 +0000 Message-Id: <7e800caa0485fec67b21ebab1e27bb23b2f0d971.1606898457.git.szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [217.140.106.54] X-ClientProxiedBy: LO2P265CA0345.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:d::21) To PR3PR08MB5564.eurprd08.prod.outlook.com (2603:10a6:102:87::18) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (217.140.106.54) by LO2P265CA0345.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:d::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.17 via Frontend Transport; Wed, 2 Dec 2020 08:55:45 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: d3ed0d4a-8f36-4e80-c892-08d896a019c2 X-MS-TrafficTypeDiagnostic: PR3PR08MB5644:|AM6PR08MB2968: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:1824;OLM:1824; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: teIoYpkYojRI/jYUYuTkWXQf5GOXRSS2bpllNpYsSAsPClOX3OVjyCp2Uh32oooSxOCi77vFkN2xWjE5gSz8vlBd6gP85FObE6EmXyqhetwurUxpoUV1mK9wEmDniAxbxh2eHfkwON9nsCTI2fjeXryyUgT5CJgl1IsU7mI6ygF2ChXqmsHyChtHj4g/qpsLdTsETxlhBpA3KbP/0dqKAlnfcybwfXcZ1O5JC45tRvjdcoIztdKuzS66o/YC+mijWZluwsmxDn8VDpR3LL2gTC1jLNu/fEkilj69lDKj+1evtS46O9w3j1UorztSMsyrRxYCZ74NnfDMm+zrhfaTZxdL9/k8K4A7P2ol1r3ruW/UYkp6mBSAplhra/0v8d92 X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PR3PR08MB5564.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(376002)(366004)(396003)(39850400004)(346002)(136003)(44832011)(956004)(2906002)(316002)(86362001)(8676002)(8936002)(54906003)(6666004)(36756003)(478600001)(66946007)(66476007)(66556008)(5660300002)(69590400008)(2616005)(52116002)(6512007)(4326008)(186003)(6486002)(16526019)(6506007)(6916009)(26005)(83380400001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: jWjZJ7jpYgfUVyQ90oifYa0y+saFIDNkgH2AZFtIuJg4GU19GLDb1HHGKm3r0WFWH5auFkkLgXYSZKNRjsdmES3r+ltfzUobUxl9PLkaqxCEEjNi1+dT1eENK8P+1MaWg2Lchmz+vRp4ERFM0V5OFQ39q7RBfACr6nNl0LAIjMv/idohamA/O/P0LQ0p9hVvAYfofrum0X7cx1ENvSuVWI0o0sCpZOEnB8mWIVFbYWjAJaXG7rG1WRDSGQCy+q6BownUtb/6an3nbNzIHzJrmihjwdSBW+KR7AVu5N/JKJz5rmXS1/VyrLoznqXd7rbj6CjnEZqGqYfQLDoTGfuZXz/XyNAtK4RmNsSQq28/M2MM/AM63r01EWz4kXAIRr8+E9htuxfHMu2BTQRGZ5NjEawojZ/FOsQGAxzZFcZw2OlRvom9qtmodJEk4eA1C5wtjhn+FGZ8P6xdd+JpIC5+e08dh+5BAxPKYVjsD8uIM5tpcA2mR8LDHlLcgVfPVw9q0sVz7r009/GD5eI1kBep5Snm+7orPb2Q2As+RAAz76N7WaaCXoYWQDEAadhT6I8lCiJMilKU/nmq3PvzpiByfR9TC2jO2gZtq6KACYixmyCYALDQKKfWlTXQLZ50krQNfsAQ3zfn0B3sWI/+W2mzKXYKlvEYZmzebZb8Igpiyc3QJ6TmCmsomMSTlwmZDgAaBcntdNuGB7UU8dosE0jz9qlDFgLsjnEPKBt4mYHiGVJOiSiAmBwiVS4nrytRXBzXPSnaSoH65JjN4GImTmHG4FumUtV7fALtOudtUyRncOn+heFjgpm1LmnY734IbD4qtzGwgYy9XbnGmlFkV7x+FxWtSMwtz4VRqq+IKm+Iam5WngxbYKpP13Ks+T5tVuQVgXJAWFs4qWfgYULnwfn/bHTuIxPPezEXMmFKtBlBZ14nfxAtXgRae23uWbCkeVALJexpcRaqOOqs/tjK9kn0gD3V8jb/muhDZXxd3zEvcWkwdRuEg0U5sXaDT7cNv0Gd X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR08MB5644 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: DB5EUR03FT008.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: d0817323-7a2e-48b1-7f54-08d896a00ed4 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nwZhgYhLcEbhIp67EKoZlRHOkAhSmRfA7w+qtu5Nbi64ETOVY3uzkm2kiibLij6ofArzG07DUMMj9bUGScVce36CV8aO2UsvZ6smoKAlKfNEOmaUryvyvbOzaYC7MVzHa1fWZRHIPymy+RxJShap5/Nuo8HDF7hcaunOmFtf6gzkB4tYpzpYAJeCb5iUtB32t5U+iU6H4jtyHQV8M5peb4v7ggLA9rn0O28iuowmR0+F+4+WORKWx1sGZPrji2s3mLCSZZl+o8XJnF4dmkYWrI0q2ounuC7L35xcXr+OlNZX8DGuoroGVxWHBtqDr1aAtIbaxFexnLTTHFk+Fy6F5Lsk3IduaXOJQWIQRkJrQ5CTeTeKPBBf+6BvcRnKk7wWawXMccu0FIa8YuhQJ0aszAJ3dfaK7mBGqa0yhzvcZkhN6jTCBHvH2/nMlxojJDFW 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; SFS:(4636009)(396003)(136003)(346002)(376002)(39860400002)(46966005)(478600001)(16526019)(5660300002)(956004)(316002)(82740400003)(81166007)(47076004)(2906002)(8676002)(356005)(336012)(2616005)(44832011)(6916009)(36756003)(4326008)(86362001)(69590400008)(8936002)(70206006)(6506007)(6512007)(6486002)(107886003)(186003)(54906003)(26005)(83380400001)(6666004)(82310400003)(70586007); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Dec 2020 08:56:04.3984 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d3ed0d4a-8f36-4e80-c892-08d896a019c2 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-AuthSource: DB5EUR03FT008.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB2968 X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, 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 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: , X-Patchwork-Original-From: Szabolcs Nagy via Libc-alpha From: Szabolcs Nagy Reply-To: Szabolcs Nagy Cc: Mark Rutland , kernel-hardening@lists.openwall.com, Catalin Marinas , linux-kernel@vger.kernel.org, Jeremy Linton , Mark Brown , Topi Miettinen , Will Deacon , linux-arm-kernel@lists.infradead.org Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Re-mmap executable segments if possible instead of using mprotect to add PROT_BTI. This allows using BTI protection with security policies that prevent mprotect with PROT_EXEC. If the fd of the ELF module is not available because it was kernel mapped then mprotect is used and failures are ignored. To protect the main executable even when mprotect is filtered the linux kernel will have to be changed to add PROT_BTI to it. The delayed failure reporting is mainly needed because currently _dl_process_gnu_properties does not propagate failures such that the required cleanups happen. Using the link_map_machine struct for error propagation is not ideal, but this seemed to be the least intrusive solution. Fixes bug 26831. Reviewed-by: Adhemerval Zanella --- v3: - split the last patch in two. - pushed to nsz/btifix-v3 branch. sysdeps/aarch64/dl-bti.c | 54 ++++++++++++++++++++++++++------------- sysdeps/aarch64/dl-prop.h | 8 +++++- sysdeps/aarch64/linkmap.h | 2 +- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/sysdeps/aarch64/dl-bti.c b/sysdeps/aarch64/dl-bti.c index 67d63c8a73..ff26c98ccf 100644 --- a/sysdeps/aarch64/dl-bti.c +++ b/sysdeps/aarch64/dl-bti.c @@ -19,9 +19,17 @@ #include #include #include +#include -static void -enable_bti (struct link_map *map, const char *program) +/* See elf/dl-load.h. */ +#ifndef MAP_COPY +# define MAP_COPY (MAP_PRIVATE | MAP_DENYWRITE) +#endif + +/* Enable BTI protection for MAP. */ + +void +_dl_bti_protect (struct link_map *map, int fd) { const size_t pagesz = GLRO(dl_pagesize); const ElfW(Phdr) *phdr; @@ -41,19 +49,31 @@ enable_bti (struct link_map *map, const char *program) if (phdr->p_flags & PF_W) prot |= PROT_WRITE; - if (__mprotect (start, len, prot) < 0) - { - if (program) - _dl_fatal_printf ("%s: mprotect failed to turn on BTI\n", - map->l_name); - else - _dl_signal_error (errno, map->l_name, "dlopen", - N_("mprotect failed to turn on BTI")); - } + if (fd == -1) + /* Ignore failures for kernel mapped binaries. */ + __mprotect (start, len, prot); + else + map->l_mach.bti_fail = __mmap (start, len, prot, + MAP_FIXED|MAP_COPY|MAP_FILE, + fd, off) == MAP_FAILED; } } -/* Enable BTI for MAP and its dependencies. */ + +static void +bti_failed (struct link_map *l, const char *program) +{ + if (program) + _dl_fatal_printf ("%s: %s: failed to turn on BTI protection\n", + program, l->l_name); + else + /* Note: the errno value is not available any more. */ + _dl_signal_error (0, l->l_name, "dlopen", + N_("failed to turn on BTI protection")); +} + + +/* Report BTI protection failures for MAP and its dependencies. */ void _dl_bti_check (struct link_map *map, const char *program) @@ -61,16 +81,14 @@ _dl_bti_check (struct link_map *map, const char *program) if (!GLRO(dl_aarch64_cpu_features).bti) return; - if (map->l_mach.bti) - enable_bti (map, program); + if (map->l_mach.bti_fail) + bti_failed (map, program); unsigned int i = map->l_searchlist.r_nlist; while (i-- > 0) { struct link_map *l = map->l_initfini[i]; - if (l->l_init_called) - continue; - if (l->l_mach.bti) - enable_bti (l, program); + if (l->l_mach.bti_fail) + bti_failed (l, program); } } diff --git a/sysdeps/aarch64/dl-prop.h b/sysdeps/aarch64/dl-prop.h index 2016d1472e..e926e54984 100644 --- a/sysdeps/aarch64/dl-prop.h +++ b/sysdeps/aarch64/dl-prop.h @@ -19,6 +19,8 @@ #ifndef _DL_PROP_H #define _DL_PROP_H +extern void _dl_bti_protect (struct link_map *, int) attribute_hidden; + extern void _dl_bti_check (struct link_map *, const char *) attribute_hidden; @@ -43,6 +45,10 @@ static inline int _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, uint32_t datasz, void *data) { + if (!GLRO(dl_aarch64_cpu_features).bti) + /* Skip note processing. */ + return 0; + if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) { /* Stop if the property note is ill-formed. */ @@ -51,7 +57,7 @@ _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, unsigned int feature_1 = *(unsigned int *) data; if (feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) - l->l_mach.bti = true; + _dl_bti_protect (l, fd); /* Stop if we processed the property note. */ return 0; diff --git a/sysdeps/aarch64/linkmap.h b/sysdeps/aarch64/linkmap.h index 847a03ace2..b3f7663b07 100644 --- a/sysdeps/aarch64/linkmap.h +++ b/sysdeps/aarch64/linkmap.h @@ -22,5 +22,5 @@ struct link_map_machine { ElfW(Addr) plt; /* Address of .plt */ void *tlsdesc_table; /* Address of TLS descriptor hash table. */ - bool bti; /* Branch Target Identification is enabled. */ + bool bti_fail; /* Failed to enable Branch Target Identification. */ };