Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp975535pxb; Fri, 15 Apr 2022 17:08:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDE3CWjPmq2VyuUPi7a8a0PaR/najcLOQO/CIacfV5yyvP6he7ipEcqA065+OpjRBd82qR X-Received: by 2002:a17:903:20f:b0:158:d86a:f473 with SMTP id r15-20020a170903020f00b00158d86af473mr1201058plh.92.1650067734506; Fri, 15 Apr 2022 17:08:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650067734; cv=none; d=google.com; s=arc-20160816; b=Rfwmkwcv6Kuro0UzpRn/HPAsc8dwfcmPJfSgyc9TTSu7so7IUBQtYgh2bSDYmZljME T1XocX5hGbALHCBlxP0vrHFn8cZ/d/P3K/7rNAy4tcGVyaEvsN3AWuq/ZmN+gf9T8xCY mEQDFL/8/CozNn1OhESEuUWl8BjpTmCXSOliFaXs+ktcKQgL2VCxpmdBt2kKAn0X3BXi 7GKmOa7TEvIoE9lC+4WgkYBWdpGIrqvZ7saYr98yP++kwyXLR/SGyWJkyc4mRFo9Uro2 uUlTDjtDBbHsiiVie3uSFXQzEQ9U+rqabihbScpO5BdrmJi3J7nlYy5B6XKcsPL+qzYX 6o0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=KPncbYJNbA92y1tOvGAA/kfwDjyfM1X8IrkxMDKfJ+M=; b=Y4ofTGGU+cA8YHF0MTBPaAOllTK377lSrcLbYc/vk7Wzfl9BfPAZ+tlS12KGYI+BUW IT1FoHrDgQu7lFw/wEyr9N9k6AoMQ+rsWP6kmWEN9/kAWboTVlH/uRThSMCT2j8rrTp1 fAEf+Wl4yZYUBi+BBKBPtGUo+0mgjSbMK8dqgtJYAqT+nD7v+XYsBlC2KAdGW+py369r EVo348X6AAlN/PkR4dS3qXYVFqkzR8oONvhdfuaWRla6zgtFWk7OdKN03R5j23V31+b9 8GszPDpkv+bnb5E6v4e2v8jhSaJ3V0gN7xigT6SRR+/ythAxyCZsP7oCG/cBGN9mSyGV XQOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=oNJvsyY0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j37-20020a634a65000000b003a283acb929si2663307pgl.490.2022.04.15.17.08.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 17:08:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=oNJvsyY0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9D5E05C37E; Fri, 15 Apr 2022 17:08:52 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231607AbiDNSyq (ORCPT + 99 others); Thu, 14 Apr 2022 14:54:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231168AbiDNSyo (ORCPT ); Thu, 14 Apr 2022 14:54:44 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B83CE0AC4 for ; Thu, 14 Apr 2022 11:52:19 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id t4so5546423pgc.1 for ; Thu, 14 Apr 2022 11:52:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=KPncbYJNbA92y1tOvGAA/kfwDjyfM1X8IrkxMDKfJ+M=; b=oNJvsyY0dZL8jzpd34U1+lD1Mh8SQf6LfMh/R2x0RNJXbEdp0Xg7P85/Y+6PQt0AES rhaP4cro55hlKlnS+hw2JRDJMYhHFgA8+9OIE42iBmqFG6lmbZCLPNkLehXQLN21cuOM 91/dtOApOgZJLABiR/avAVberdE2aWu4pBxhU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=KPncbYJNbA92y1tOvGAA/kfwDjyfM1X8IrkxMDKfJ+M=; b=QTazqMWt7zt1mI08CoWgQ/P87R7an7r1OGDxpe0KATda5zXOLxbuolM+rqhT7HkBBB O1LzVEnA2+U/zM0g+kGZMajIztmON6lg0Dx2Jy5Ef4oOjFFqOJ6yLcCvtMli8JPgCBGb NaqHUJyLMcSscuRDiufhjNMiD2CZDYuNawQa+7plnS5+DHvVAH+rrHmTR9uq9LdJQOxZ XKL7loeeWSCQEY8Uz7eOZhrDBtnPYoK4PSsljfj3H2Yx2w5kytMClgYXe/npEnaiZmuy +WUu+0+CMJfL8CFTIV2QhWNqgrIu9SEYWrggrKgfKq6oDaetegRRbLvPhEcI6u+tEqHc Tyvw== X-Gm-Message-State: AOAM532G7YwGiUD1slN9AKcefctclBkQHDWPxVTDxlzfwXd6G3LpGaFL O0IOn+IQP7MJe/KyeCy+jYfgMA== X-Received: by 2002:a62:1548:0:b0:505:fd84:33f1 with SMTP id 69-20020a621548000000b00505fd8433f1mr5299523pfv.66.1649962338843; Thu, 14 Apr 2022 11:52:18 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c64-20020a624e43000000b005081ec7d679sm613192pfb.1.2022.04.14.11.52.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Apr 2022 11:52:18 -0700 (PDT) Date: Thu, 14 Apr 2022 11:52:17 -0700 From: Kees Cook To: Catalin Marinas Cc: Andrew Morton , Christoph Hellwig , Lennart Poettering , Zbigniew =?utf-8?Q?J=C4=99drzejewski-Szmek?= , Will Deacon , Alexander Viro , Eric Biederman , 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, linux-hardening@vger.kernel.org, Jann Horn , Salvatore Mesoraca , Igor Zhbanov Subject: Re: [PATCH RFC 0/4] mm, arm64: In-kernel support for memory-deny-write-execute (MDWE) Message-ID: <202204141028.0482B08@keescook> References: <20220413134946.2732468-1-catalin.marinas@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220413134946.2732468-1-catalin.marinas@arm.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 13, 2022 at 02:49:42PM +0100, Catalin Marinas wrote: > 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]. Right, so, the systemd filter is a big hammer solution for the kernel not having a very easy way to provide W^X mapping protections to userspace. There's stuff in SELinux, and there have been several attempts[1] at other LSMs to do it too, but nothing stuck. Given the filter, and the implementation of how to enable BTI, I see two solutions: - provide a way to do W^X so systemd can implement the feature differently - provide a way to turn on BTI separate from mprotect to bypass the filter I would agree, the latter seems like the greater hack, so I welcome this RFC, though I think it might need to explore a bit of the feature space exposed by other solutions[1] (i.e. see SARA and NAX), otherwise it risks being too narrowly implemented. For example, playing well with JITs should be part of the design, and will likely need some kind of ELF flags and/or "sealing" mode, and to handle the vma alias case as Jann Horn pointed out[2]. > 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. My instinct here is to use a prctl(), which maps to other kinds of modern inherited state (like no_new_privs). > 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 So, yes, let's do it. It's long long overdue in the kernel. :) -Kees [1] https://github.com/KSPP/linux/issues/32 [2] https://github.com/KSPP/linux/issues/32#issuecomment-1084859611 > > 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(-) > -- Kees Cook