From patchwork Wed Apr 13 13:49:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 12812044 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E29A9C433EF for ; Wed, 13 Apr 2022 13:49:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36CB66B0072; Wed, 13 Apr 2022 09:49:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FF176B0073; Wed, 13 Apr 2022 09:49:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1953F6B0074; Wed, 13 Apr 2022 09:49:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 04C0F6B0072 for ; Wed, 13 Apr 2022 09:49:56 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id C365880B6F for ; Wed, 13 Apr 2022 13:49:55 +0000 (UTC) X-FDA: 79351989150.16.41C6987 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf23.hostedemail.com (Postfix) with ESMTP id EF51C140004 for ; Wed, 13 Apr 2022 13:49:54 +0000 (UTC) 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 ams.source.kernel.org (Postfix) with ESMTPS id ECA9EB824C9; Wed, 13 Apr 2022 13:49:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C09E9C385A3; Wed, 13 Apr 2022 13:49:48 +0000 (UTC) From: Catalin Marinas To: Andrew Morton , Christoph Hellwig , Lennart Poettering , =?utf-8?q?Zbigniew_J=C4=99drze?= =?utf-8?q?jewski-Szmek?= Cc: Will Deacon , Alexander Viro , Eric Biederman , Kees Cook , Szabolcs Nagy , Mark Brown , Jeremy Linton , Topi Miettinen , linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-abi-devel@lists.sourceforge.net Subject: [PATCH RFC 0/4] mm, arm64: In-kernel support for memory-deny-write-execute (MDWE) Date: Wed, 13 Apr 2022 14:49:42 +0100 Message-Id: <20220413134946.2732468-1-catalin.marinas@arm.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf23.hostedemail.com: domain of cmarinas@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=cmarinas@kernel.org X-Stat-Signature: tby8c6b5xrksidw75pjik4ecagzy9npk X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: EF51C140004 X-HE-Tag: 1649857794-678188 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hi, The background to this is that systemd has a configuration option called MemoryDenyWriteExecute [1], 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 - [2] - and subsequent glibc workaround for libraries - [3]. Add in-kernel support for such feature as a DENY_WRITE_EXEC personality flag, inherited on fork() and execve(). The kernel tracks a previously writeable mapping via a new VM_WAS_WRITE flag (64-bit only architectures). I went for a personality flag by analogy with the READ_IMPLIES_EXEC one. However, I'm happy to change it to a prctl() if we don't want more personality flags. A minor downside with the personality flag is that there is no way for the user to query which flags are supported, so in patch 3 I added an AT_FLAGS bit to advertise this. Posting this as an RFC to start a discussion and cc'ing some of the systemd guys and those involved in the earlier thread around the glibc workaround for dynamic libraries [4]. Before thinking of upstreaming this we'd need the systemd folk to buy into replacing the MDWE SECCOMP BPF filter with the in-kernel one. Thanks, Catalin [1] https://www.freedesktop.org/software/systemd/man/systemd.exec.html#MemoryDenyWriteExecute= [2] https://bugzilla.redhat.com/show_bug.cgi?id=1888842 [3] https://sourceware.org/bugzilla/show_bug.cgi?id=26831 [3] https://lore.kernel.org/r/cover.1604393169.git.szabolcs.nagy@arm.com Catalin Marinas (4): mm: Track previously writeable vma permission mm, personality: Implement memory-deny-write-execute as a personality flag fs/binfmt_elf: Tell user-space about the DENY_WRITE_EXEC personality flag arm64: Select ARCH_ENABLE_DENY_WRITE_EXEC arch/arm64/Kconfig | 1 + fs/binfmt_elf.c | 2 ++ include/linux/mm.h | 6 ++++++ include/linux/mman.h | 18 +++++++++++++++++- include/uapi/linux/binfmts.h | 4 ++++ include/uapi/linux/personality.h | 1 + mm/Kconfig | 4 ++++ mm/mmap.c | 3 +++ mm/mprotect.c | 5 +++++ 9 files changed, 43 insertions(+), 1 deletion(-)