From patchwork Thu Jan 19 16:03:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joey Gouly X-Patchwork-Id: 13108324 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 80EABC00A5A for ; Thu, 19 Jan 2023 16:06:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:CC :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=bkwWXDjPGrd8uD86kcZgGKErNB3WehHTrT+jnfSjYNg=; b=HbgvEAPjTTStPE w9D4KyKB8D/zwdeFlqjxviWF6Xvh/hsID8Sr4YMEaCEyWH0PKuGkEGrCecaJtQ8usCGwWGXG/CX9G cZ4zSMtJwIyEU93Dl+tdCoEPpS5br5IWZu1hxDbdTDt2//jKkSbl0E7vTJEIcFQjmNydfqXAO72qs V+nBLxsXUBIGhZKc+y4GVyAImsqWP7mGvzgZ3ImSo/6Me4jodKMioTTxrnFiYuM5bqQRCcykeO9XY st6r4p4hIUIeI1lc8T1gfNJSxc/po3jlZleM5uMSbggEoLcs+wScIOk5L/iN0Wx+WRqY/tvduWRUX idXyqolVSgM/cOYJ6d6A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pIXPF-005pp1-1w; Thu, 19 Jan 2023 16:04:50 +0000 Received: from mail-db8eur05on2049.outbound.protection.outlook.com ([40.107.20.49] helo=EUR05-DB8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pIXP7-005pka-SU for linux-arm-kernel@lists.infradead.org; Thu, 19 Jan 2023 16:04:44 +0000 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=eSz4XrLrOU/Q0X8dyvT5gsrdo+lHEbTiMfz9tT5sCxE=; b=yRWW5KHGdhFUE3VJUOE2HiGYbbFw1X4sIgYpUOFGgi/BaQEQsHpjCSZifJ3pAEsNeCOR4UIya1JTb4Ww64/haAH3Y38ey+dXEZ/DPcHVd4Hyac7TuwzTyuJ4/1aR8pgCU1BEWYeqyGuCKvtVNpB6oC4GRwhD1jdiwtKRNhHsacM= Received: from AS8P189CA0049.EURP189.PROD.OUTLOOK.COM (2603:10a6:20b:458::22) by AS8PR08MB5927.eurprd08.prod.outlook.com (2603:10a6:20b:292::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.12; Thu, 19 Jan 2023 16:04:36 +0000 Received: from AM7EUR03FT024.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:458:cafe::3d) by AS8P189CA0049.outlook.office365.com (2603:10a6:20b:458::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.25 via Frontend Transport; Thu, 19 Jan 2023 16:04:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;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; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM7EUR03FT024.mail.protection.outlook.com (100.127.140.238) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.13 via Frontend Transport; Thu, 19 Jan 2023 16:04:36 +0000 Received: ("Tessian outbound baf1b7a96f25:v132"); Thu, 19 Jan 2023 16:04:35 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 6534bfdff3293073 X-CR-MTA-TID: 64aa7808 Received: from f0b93e1d57fe.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 53D9ED59-5EA8-46CF-AD54-6ABF85BB3334.1; Thu, 19 Jan 2023 16:04:05 +0000 Received: from EUR03-AM7-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id f0b93e1d57fe.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 19 Jan 2023 16:04:05 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Jf2KAhZPJS6i4LP82P7FHExpOvabULfbU+u6Ws11HueHw/CpJgPbmTRUSckfAU03kot+oCJqi+ZQJLS16DlEuCABmD7Qa2varMrOtqnsKI4IiDFPMVa3QYiK4mISR5lJjs9TgPmhufZnPIijTXcOe/qL0It2ukaI8rIhM2/KVcB+ai7WQuZPbgP1mMYkglWXJaHGYJAf9MYnTa/u8TXEax4pLxDL3udmpEGlwGmvVNFJMZiBM5opTt1KMDqYmnxkyes2UBrKiCSuDSrxDkt7rbcTHjXsiF1R8YRVepBPqsLsC+ZFiT/A8nNJoAqwjpzLdXJ3Dj7g6HcB/f6JOBmyrw== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=eSz4XrLrOU/Q0X8dyvT5gsrdo+lHEbTiMfz9tT5sCxE=; b=NULLEK/sFVKxWQ6i1eWNGYB1JH5gQTt8HsD2ylFWcd66mblgM8Nsdb6a9zzH7EuhqKZ6J6cx5PmX7ANaVqEC7FaKis2DdUvluPk2iVAG46vXlGg4wSnj34g24UHlqZ2u2LTEf6zXj41gN+dI2hWqXK8IhcmDKyEMBU8aqNnORSYEYggPelZfNDFJNLqhFjhenYetk4JDICw/DvzJEch4YoT3dzLl9ididRkwMVPB9oeiYErc9ohFPMhV72nTkimG3TcrPeYuOqpsv7CdhacoXXzMwai1qsa7ljo5Yff9M8oRRRf3+lNeMVnYxFl9dkVxCylhauME0UWXReItdesyUQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=linux-foundation.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); 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=eSz4XrLrOU/Q0X8dyvT5gsrdo+lHEbTiMfz9tT5sCxE=; b=yRWW5KHGdhFUE3VJUOE2HiGYbbFw1X4sIgYpUOFGgi/BaQEQsHpjCSZifJ3pAEsNeCOR4UIya1JTb4Ww64/haAH3Y38ey+dXEZ/DPcHVd4Hyac7TuwzTyuJ4/1aR8pgCU1BEWYeqyGuCKvtVNpB6oC4GRwhD1jdiwtKRNhHsacM= Received: from AS9PR05CA0315.eurprd05.prod.outlook.com (2603:10a6:20b:491::9) by DU0PR08MB9002.eurprd08.prod.outlook.com (2603:10a6:10:467::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.12; Thu, 19 Jan 2023 16:04:00 +0000 Received: from AM7EUR03FT056.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:491:cafe::80) by AS9PR05CA0315.outlook.office365.com (2603:10a6:20b:491::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.25 via Frontend Transport; Thu, 19 Jan 2023 16:04:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; pr=C Received: from nebula.arm.com (40.67.248.234) by AM7EUR03FT056.mail.protection.outlook.com (100.127.140.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6002.13 via Frontend Transport; Thu, 19 Jan 2023 16:04:00 +0000 Received: from AZ-NEU-EX03.Arm.com (10.251.24.31) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Thu, 19 Jan 2023 16:03:59 +0000 Received: from e124191.cambridge.arm.com (10.1.197.45) by mail.arm.com (10.251.24.31) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Thu, 19 Jan 2023 16:03:59 +0000 From: Joey Gouly To: Catalin Marinas , Andrew Morton , Lennart Poettering , =?utf-8?q?Zbigniew_J=C4=99drze?= =?utf-8?q?jewski-Szmek?= CC: Alexander Viro , Kees Cook , Szabolcs Nagy , Mark Brown , Jeremy Linton , Topi Miettinen , , , , , , , Subject: [PATCH v2 0/2] mm: In-kernel support for memory-deny-write-execute (MDWE) Date: Thu, 19 Jan 2023 16:03:42 +0000 Message-ID: <20230119160344.54358-1-joey.gouly@arm.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: AM7EUR03FT056:EE_|DU0PR08MB9002:EE_|AM7EUR03FT024:EE_|AS8PR08MB5927:EE_ X-MS-Office365-Filtering-Correlation-Id: 865a375b-0a69-49ca-3657-08dafa36dc7f x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: qWUH5axFNnx0Y830fduFk256PntJHMaRiTJ0Fb4+qrqb4AOqzwdY+eJmeQ1ZDdAU//FnEaolFb/x0fZ8Kdq76E/BC/1Gj7iT2jpiaMNVmc+8AeJM64hmsobEF/Sr05TV3tNbQ9CqIUogofqnFA8QdP6yw3Rb6ZN6+/qJPoaIYxf5rToxUlH/vNckxwcLh2Zby/h6IJsxTp+kWWq/ErR44XrZ/xBapNLsWI6MnygT5W8MHqlrLFsYPCdKsm14k5JLl4sXZNj/1nsazXdr1yWBUVKK8LucqMsBU1/tA00S5aTehp7sNpZSHiDNGawaF/NmUtZ50hbp9L0PX6FJBdcvDNTyd2p+IA/CoteWj37XRWGHpoBxO59Z9cek879UedoonPcToH4r8Bxi1moxUKYn+VRjwLNLoJQIkLO7bBofPgdkJ3hkplG5GstOATzs4/xA7myFFRO0kvTPJHEyEtQ/Jl+KFW6t6DWydaSoCFEXgeBZXTSK1kWM71A/4b+fA1P+td7YBWfqZ3N2TXe1qK0pE6u5N01nhwliofXcLM1uORmdmvpRachd1ebDlD5pQnE+YpKLw+oONH7PmgSBdyZMsS2+JZMejFJfVT1Dd/ZJ+qkriCib+A+18WbOKtAA2npcJY+s/7Ki+j5+Xhyv1JQhS28xyT02mESZbF2kVcpiwbrzrScgbuEU8a+9fVpYUwIMHA+NxNdojYN5ww/VZxP6+XfWDVnht6Z33Xz/Rst8HDmTd5k3AhrKuOrlKzRhxFH0MUnnqxjJef0+obZ4FlHZ68Yyoo0KnA3kNz3ccJX9PojkeaRptmkEAMxlBkmZSKMd X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:nebula.arm.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(396003)(136003)(346002)(376002)(451199015)(46966006)(40470700004)(36840700001)(7416002)(36756003)(44832011)(41300700001)(82310400005)(83380400001)(36860700001)(2616005)(1076003)(478600001)(4326008)(5660300002)(8676002)(966005)(7696005)(356005)(82740400003)(426003)(8936002)(40480700001)(26005)(81166007)(6666004)(186003)(336012)(47076005)(54906003)(86362001)(40460700003)(70586007)(70206006)(316002)(110136005)(2906002)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR08MB9002 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM7EUR03FT024.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: e664ca60-6c8e-4e57-8fcf-08dafa36c778 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KtOpbo92pI/ubFo9jtaH65a3BGScful7BuRO1XgRs22GK3SpS472a3YdQIFLxftButFLmWRlNgsyIo9kMXi8oy3ar4yRaxrqSHA0F6iB8s4d9ORzxjhzHY2uBfU86k9NivbnObL4RLjaeA7elWyvrsgFGkUGlJJCcFF1a9SwyD8rTQjv92HCn/EBMTA+sEi/o2oxSEbwiMAwIJ8T10/4tobW5CDok3kkQlaCZZeNpAJnqtjaz6lEhryURKurNaZAnjmqceW6GCVMtTCpO/U6A+g8kN2cAA1QwjzeHe1V8WiLBLCfpaPwxjf5XiKG9xqQje/NlEZAA+7tsyjGH/erPbeR11QdQBKelmMfjf+hg2ffkl3rlbkRZcRbfgxzU5nNUZzsZCKLfQ8bxpDYPAEPQ1rrZjH2V70Jg/X70PRLOS/z9mexQuajHfU82K5oHD3HJsYzkGbCIQ/JWTXElXRbMKRtAJBtHUjGfbpfFF5SgihdQfl6sdnPGoyPoyHChbSMXTyIQ77jq1rTtX/fJxHqEQ2dlsiQf03bejz6g4/d70Egg7qd5jNfMkdvMgijhceaCVIFyKr3TKchEJ9GSAGL8wmRNX4QgdtdSZtp2dObcEFbWkEKfCxm/DjT8jkWjA/pdlKox4XYTL34XhkxM30zZ9c2rkWQViuX0KJh4k1Rh42agcIkKk3LEvv2XfsP1AM2hOGSBTBzPBCRAKcXOIFq0441Ii+ZuzhFseqOCa6+1lEM6EjOr3/2nM6OM0dMFFueVIT8KX1KLjcVf0xwVRmn7A== 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:(13230022)(4636009)(346002)(396003)(39860400002)(136003)(376002)(451199015)(40470700004)(36840700001)(46966006)(6666004)(44832011)(70586007)(41300700001)(86362001)(478600001)(966005)(7696005)(40480700001)(186003)(2616005)(336012)(1076003)(26005)(426003)(47076005)(110136005)(36756003)(83380400001)(54906003)(82740400003)(40460700003)(4326008)(8676002)(107886003)(316002)(5660300002)(2906002)(70206006)(36860700001)(81166007)(82310400005)(8936002);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jan 2023 16:04:36.0380 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 865a375b-0a69-49ca-3657-08dafa36dc7f 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: AM7EUR03FT024.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB5927 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230119_080441_996124_0CE05C7E X-CRM114-Status: GOOD ( 17.61 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi all, This is v2 of the MDWE patchset. Changes since v1: - Rewritten test - thanks Kees! - Added comment to `map_deny_write_exec` - Moved flag check into mmap_region, should be no functional change - Rebased onto v6.2-rc4 The background to this is that systemd has a configuration option called MemoryDenyWriteExecute [2], implemented as a SECCOMP BPF filter. Its aim is to prevent a user task from inadvertently creating an executable mapping that is (or was) writeable. Since such BPF filter is stateless, it cannot detect mappings that were previously writeable but subsequently changed to read-only. Therefore the filter simply rejects any mprotect(PROT_EXEC). The side-effect is that on arm64 with BTI support (Branch Target Identification), the dynamic loader cannot change an ELF section from PROT_EXEC to PROT_EXEC|PROT_BTI using mprotect(). For libraries, it can resort to unmapping and re-mapping but for the main executable it does not have a file descriptor. The original bug report in the Red Hat bugzilla - [3] - and subsequent glibc workaround for libraries - [4]. This series adds in-kernel support for this feature as a prctl PR_SET_MDWE, that is inherited on fork(). The prctl denies PROT_WRITE | PROT_EXEC mappings. Like the systemd BPF filter it also denies adding PROT_EXEC to mappings. However unlike the BPF filter it only denies it if the mapping didn't previous have PROT_EXEC. This allows to PROT_EXEC -> PROT_EXEC | PROT_BTI with mprotect(), which is a problem with the BPF filter. Thanks, Joey [1] https://lore.kernel.org/linux-arm-kernel/20221026150457.36957-1-joey.gouly@arm.com/ [2] https://www.freedesktop.org/software/systemd/man/systemd.exec.html#MemoryDenyWriteExecute= [3] https://bugzilla.redhat.com/show_bug.cgi?id=1888842 [4] https://sourceware.org/bugzilla/show_bug.cgi?id=26831 Joey Gouly (1): mm: Implement memory-deny-write-execute as a prctl Kees Cook (1): kselftest: vm: add tests for memory-deny-write-execute include/linux/mman.h | 34 +++++ include/linux/sched/coredump.h | 6 +- include/uapi/linux/prctl.h | 6 + kernel/sys.c | 33 +++++ mm/mmap.c | 10 ++ mm/mprotect.c | 5 + tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/mdwe_test.c | 197 +++++++++++++++++++++++++ 8 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/vm/mdwe_test.c