From mboxrd@z Thu Jan 1 00:00:00 1970 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 8DEA5C433FE for ; Tue, 15 Nov 2022 19:31:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B2E886B0071; Tue, 15 Nov 2022 14:31:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ADE786B0072; Tue, 15 Nov 2022 14:31:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A6DE6B0073; Tue, 15 Nov 2022 14:31:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8C8196B0071 for ; Tue, 15 Nov 2022 14:31:07 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 590E11404B3 for ; Tue, 15 Nov 2022 19:31:07 +0000 (UTC) X-FDA: 80136669774.22.A7988A9 Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) by imf24.hostedemail.com (Postfix) with ESMTP id D2BDA180012 for ; Tue, 15 Nov 2022 19:31:06 +0000 (UTC) Received: by mail-lj1-f177.google.com with SMTP id s24so18908719ljs.11 for ; Tue, 15 Nov 2022 11:31:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=GyIes3iMStNxnqC1zOqI35/7e76O5dm/XUcc8xXZAU0=; b=BwbnG2RfwynQyF60LQWulCgZz/hWVDpZdDS/W4Afc0GwH9VaX/6v6MpM6FEicoD3qh kBXdUWCdP5DC6h8mf/mrnzxSXR4bYpUaF5FDGkrw4k+jZpzvIlkdVAYAv1nLUyuYgfAa nEvcfHM60NM+upX/oP3JL3COzv02zmik+Pjw0Z14qW+f0Lheo2gLM/N6Pi02pMo9NDzy 0QGl5Wq9Kh0BxozfGXGpDQFXzOb7LZVCPt0TCIbTR3OFcM2+VLvFGmB5tryDDYgYbyj3 mYi1eHd+nMCdFvqLKrmEhIi/rAvyUgVf2Mo6BvxaGGwhBLq+b+71j38DXgdOyLej9R2A 4vtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GyIes3iMStNxnqC1zOqI35/7e76O5dm/XUcc8xXZAU0=; b=M4uua6Z3NfzjdIBel3lMfLiY/nqeN58VWp5AdLdongzOnLgNNXc60zo3c3Jjveh7O0 3xGqO0Nd2Xsf7U3WsIql4OgD7G/D3VyZa59CQ9BYHyyMF+Km+ZRWWked/i+17954Ynpq Bn1I6Ik3G3rpMavTY9Es8WBufQbxGI/NMfyOb46zVmLlB6Lk34hFv9hCpDwo+0gImNJO xr3iVq7/Ib7jutOYcYjaydmRKvBq2iu4v8v0KlxtvGkAnK4gBUkz2V7ni4t9BM4ioVBR G98tXxKr8rQKPkOapQidBZY7YPGGVhgHY7nBXwN8QmpHtc2kpFclIvl1MmKYD0EOAu8Q b0qg== X-Gm-Message-State: ANoB5pmprNZ16X3x3JZ+WKvaiz3sQDP4v0TFjKYq2QgvPNuHrRvTENRP VpM/DmHD05fqAeI1zqH9DxM= X-Google-Smtp-Source: AA0mqf5E7tedpNnEAREDyyxzme4BhAXLv4FsoAZtAChSVnsk1YdNBoZXKk4qdoZLXnO/ckEuuwCskQ== X-Received: by 2002:a05:651c:220f:b0:277:1d5b:74b with SMTP id y15-20020a05651c220f00b002771d5b074bmr6339410ljq.522.1668540664883; Tue, 15 Nov 2022 11:31:04 -0800 (PST) Received: from [192.168.1.12] (91-159-148-109.elisa-laajakaista.fi. [91.159.148.109]) by smtp.gmail.com with ESMTPSA id g25-20020a19ee19000000b004ac393ecc32sm2304658lfb.304.2022.11.15.11.31.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Nov 2022 11:31:04 -0800 (PST) Message-ID: <3238cc28-218e-ae73-2e12-a7c1a08bc353@gmail.com> Date: Tue, 15 Nov 2022 21:31:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH v1 1/2] mm: Implement memory-deny-write-execute as a prctl To: Catalin Marinas Cc: Joey Gouly , Kees Cook , Andrew Morton , Lennart Poettering , =?UTF-8?Q?Zbigniew_J=c4=99drzejewski-Szmek?= , Alexander Viro , Szabolcs Nagy , Mark Brown , Jeremy Linton , linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-abi-devel@lists.sourceforge.net, nd@arm.com, shuah@kernel.org References: <20221026150457.36957-1-joey.gouly@arm.com> <20221026150457.36957-2-joey.gouly@arm.com> <202210281053.904BE2F@keescook> <20221110112714.GA1201@e124191.cambridge.arm.com> <45419a7d-04dd-2749-2534-6ba3bbd5d060@gmail.com> Content-Language: en-US From: Topi Miettinen In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668540667; a=rsa-sha256; cv=none; b=ECXckS2rbKx1h/VoiShwkpTyKDO4h1xqG2LhZUPv2Fb0BaG8k4/4RzN7NaJwVgrYOcLQFk yt2qpPO/7eK492HwU/wg+YiXd/yUhVMqdLW63Cwp+Wp2DihBEexmSg8A0UsjixqZT2eADn GenPO0vAePcYs49/QJviqSi0nGtPpSs= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=BwbnG2Rf; spf=pass (imf24.hostedemail.com: domain of toiwoton@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=toiwoton@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668540667; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GyIes3iMStNxnqC1zOqI35/7e76O5dm/XUcc8xXZAU0=; b=NBesveqGVX0y9sSvauVf6bofLexThziq85T3mJZBZftEPEj8DF8hpycxZ7xX9CgAmDTmlV KzVcBagNtemiRtJKSRt1vUFgakqjcIbf+ygMHWnkPf9ew7Ei/w5rKf7axSuwm4ruOm2p0a ntvGsXsGJSvffdEpc/2S/WoPye5urgM= X-Stat-Signature: x1n4g5ostzkzopm7pgue4igdgxnzs4ci X-Rspamd-Queue-Id: D2BDA180012 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=BwbnG2Rf; spf=pass (imf24.hostedemail.com: domain of toiwoton@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=toiwoton@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1668540666-784818 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: On 15.11.2022 17.35, Catalin Marinas wrote: > On Sat, Nov 12, 2022 at 08:11:24AM +0200, Topi Miettinen wrote: >> On 10.11.2022 14.03, Catalin Marinas wrote: >>> On Thu, Nov 10, 2022 at 11:27:14AM +0000, Joey Gouly wrote: >>>> On Fri, Oct 28, 2022 at 11:51:00AM -0700, Kees Cook wrote: >>>>> On Wed, Oct 26, 2022 at 04:04:56PM +0100, Joey Gouly wrote: >>>>>> diff --git a/mm/mmap.c b/mm/mmap.c >>>>>> index 099468aee4d8..42eaf6683216 100644 >>>>>> --- a/mm/mmap.c >>>>>> +++ b/mm/mmap.c >>>>>> @@ -1409,6 +1409,9 @@ unsigned long do_mmap(struct file *file, unsigned long addr, >>>>>> vm_flags |= VM_NORESERVE; >>>>>> } >>>>>> + if (map_deny_write_exec(NULL, vm_flags)) >>>>>> + return -EACCES; >>>>>> + >>>>> >>>>> This seems like the wrong place to do the check -- that the vma argument >>>>> is a hard-coded "NULL" is evidence that something is wrong. Shouldn't >>>>> it live in mmap_region()? What happens with MAP_FIXED, when there is >>>>> an underlying vma? i.e. an MAP_FIXED will, I think, bypass the intended >>>>> check. For example, we had "c" above: >>>>> >>>>> c) mmap(PROT_READ); >>>>> mprotect(PROT_READ|PROT_EXEC); // fails >>>>> >>>>> But this would allow another case: >>>>> >>>>> e) addr = mmap(..., PROT_READ, ...); >>>>> mmap(addr, ..., PROT_READ | PROT_EXEC, MAP_FIXED, ...); // passes >>>> >>>> I can move the check into mmap_region() but it won't fix the MAP_FIXED >>>> example that you showed here. >>>> >>>> mmap_region() calls do_mas_munmap(..) which will unmap overlapping regions. >>>> However the `vma` for the 'old' region is not kept around, and a new vma will >>>> be allocated later on "vma = vm_area_alloc(mm);", and the vm_flags are just set >>>> to what is passed into mmap_region(), so map_deny_write_exec(vma, vm_flags) >>>> will just be as good as passing NULL. >>>> >>>> It's possible to save the vm_flags from the region that is unmapped, but Catalin >>>> suggested it might be better if that is part of a later extension, what do you >>>> think? >>> >>> I thought initially we should keep the behaviour close to what systemd >>> achieves via SECCOMP while only relaxing an mprotect(PROT_EXEC) if the >>> vma is already executable (i.e. check actual permission change not just >>> the PROT_* flags). >>> >>> We could pass the old vm_flags for that region (and maybe drop the vma >>> pointer entirely, just check old and new vm_flags). But this feels like >>> tightening slightly systemd's MDWE approach. If user-space doesn't get >>> confused by this, I'm fine to go with it. Otherwise we can add a new >>> flag later for this behaviour >>> >>> I guess that's more of a question for Topi on whether point tightening >>> point (e) is feasible/desirable. >> >> I think we want 1:1 compatibility with seccomp() for the basic version, so >> MAP_FIXED shouldn't change the verdict. Later we can introduce more versions >> (perhaps even less strict, too) when it's requested by configuration, like >> MemoryDenyWriteExecute=[relaxed | strict]. > > Are you ok with allowing mprotect(PROT_EXEC|PROT_BTI) if the mapping is > already PROT_EXEC? Or you'd rather reject that as well? > I think that it's OK to allow that. It's an incompatible change, but it shouldn't break anything. -Topi