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 99A2EC25B10 for ; Mon, 13 May 2024 21:26:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D614F6B02DE; Mon, 13 May 2024 17:26:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D38F36B02DF; Mon, 13 May 2024 17:26:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD8BC6B02E0; Mon, 13 May 2024 17:26:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9F1CC6B02DE for ; Mon, 13 May 2024 17:26:42 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 19536A0FEC for ; Mon, 13 May 2024 21:26:42 +0000 (UTC) X-FDA: 82114657044.04.FA3491B Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf27.hostedemail.com (Postfix) with ESMTP id 242B640010 for ; Mon, 13 May 2024 21:26:39 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iXsGN2vB; spf=pass (imf27.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715635600; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1Av9RgaPQsUZwKrihewqWhIa6dx8V7kURy0F4c3tXz8=; b=ahRtBN1imnKNqGzpNadS2kAW8ICZxobWG8QHhqXTx21zyi3ry9Mdbozeug6dEwVDz5EFQq t03N0pWstB0tFFMgDKmfm6O5P0fQauHmULatFVwEq2ZmTF9LbzQrz52KNdMeDBa7BI229u q+5yO+nOx7q1KBt/nVntE+nX6nhTQEc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715635600; a=rsa-sha256; cv=none; b=HolOqoXk2Fn/Szqehf1303c95UtCN+CKVTFambsLiAm2mP1bu9qXgCGmaVjrg45D8PlyHt 7ukQZsQEmODRBtBsqWXaast9d8SfLeig6wu90+s2qx+QEcc0f8UE/Pu71JqUufsmQowQ+m rRFUwXuExqGo2aToOJxsF2HEKwnhNGU= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iXsGN2vB; spf=pass (imf27.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1edf506b216so33011445ad.2 for ; Mon, 13 May 2024 14:26:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1715635599; x=1716240399; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=1Av9RgaPQsUZwKrihewqWhIa6dx8V7kURy0F4c3tXz8=; b=iXsGN2vBVDBheSzcXKDkI34BwdyRaJvGN4EDKoI3+lZARiuJAQtejfHqWJl2EBzlSn E5XPrpE3YSt7cmctffe0G2uBO249GNKgMTJmw+y0q+JijrR9aq2sfkUgy98aQ7awwOEG JimbE6xePD63KEbVnLDXyOF7mb2Vrb7G/rJGz82ATbDxUP2j3Bg3ElgoEJy26dpmVXk8 k3QR/DMtILP3jj8yb1NhvFxlRIeduvXLcZtZ9tfaiAzYJo8vMynAQ7TbvkntKKMencg1 41TEXX3duNamHRaudAMiRhNbqTFiGdntfhikWITuCWsldZEY3hEtFc7dyYUxe/SUbPlu +pmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715635599; x=1716240399; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=1Av9RgaPQsUZwKrihewqWhIa6dx8V7kURy0F4c3tXz8=; b=AKxT6oZ35brBO3WDmzNspZZ5wJW8zcMcUJvxpqMc+kF/kui7Sq3w+1l/nYAxFPsUgR elX1GtlMCQotKr6bEnrQYBdVxVygnlxnJnvm0H7e3u3o7NcfZUTBmj4nTpqhmmB5pNjC eAMpS1053/UMjVlEqELHyd7+edTkgxDeX7R/KI2+jT2u+eR7P+sKSV+8Qbu9oxozJ+DR X3ozNCXjkq0aL8YyjtcLt6FvI4QcSDgNDYTDTzRFgQnPozgCK1cu01bm/N4c8z/vhoPc Zd+xApSRicmslwd3aVktv++98hse0fFIkg5zFVEcohXWBDQ3oeMhpZbmZvr0C3wo8K+h an0w== X-Forwarded-Encrypted: i=1; AJvYcCXmcSjI38IrushaeSrdZOfIgdHX6vAV8LFmNyJJNSAmPBV2oppmqP6B1sURaCUZm/y11glyogUfGSnhbtLw6axRKKY= X-Gm-Message-State: AOJu0YxwKLInmw7NaMycvrvHZA73Sm4fcEZLVcUrVFdKiJi8bSRGHCd9 tN66iBKo/OzufzsAQCUuBMokO1/RpNVWxBBHCXy/jhcxYc4RDyR/CuFEzwOYgFo= X-Google-Smtp-Source: AGHT+IF+SpgNET3nuWJL5IGg9s5FZ86fG3Sc9l5DoORtZhuAz8MnSfbhg/ec9V32N2UyWUAOUoMzhg== X-Received: by 2002:a17:902:b70b:b0:1ec:76a6:ea9 with SMTP id d9443c01a7336-1ef43e338fcmr103173085ad.26.1715635598847; Mon, 13 May 2024 14:26:38 -0700 (PDT) Received: from ghost ([50.145.13.30]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0b9d16cfsm84105425ad.18.2024.05.13.14.26.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 14:26:38 -0700 (PDT) Date: Mon, 13 May 2024 14:26:31 -0700 From: Charlie Jenkins To: Deepak Gupta Cc: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: Re: [PATCH v3 10/29] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Message-ID: References: <20240403234054.2020347-1-debug@rivosinc.com> <20240403234054.2020347-11-debug@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 242B640010 X-Rspam-User: X-Stat-Signature: sek5ptnw78hnm9jjiy5wxcqa56h4aki1 X-HE-Tag: 1715635599-377838 X-HE-Meta: U2FsdGVkX18BaDza8O4wHHPTzvJ92nqMkhrVoQFFfuSyugh5DFyLMeiD8+yZlLqLK6aHjPL8aeU2/y0z6Dg7VuJRc2V81F6r3amIGwm2o++Tze+Q1rcCvaNdnAUuv1fLBqJEQM/S3Th3wLZQDSnem5bIAgx5sAjvGA6i1molagJVotfkQmy7dOZRnswC/4P4fKIEkgZkD1gdvY8Y9xptuEwKR1XETb4zrGKKhAYzRv2AXTdl4GahQuOkvLiF+uVQYk8GqR54BJHrFGj9BBIohJEftqGZuW1itJmBkfwy7VtGYolQOm6YBaQE1C26oYRqFLbZ6R7ksXBDk8rDTUpfhH0pXpgWgjpDUtFNUG560P9X+RuIKetpC4ev0pO4t5508bj3fBte7T2+zbAkoXeCrSysF6dKZXbdEYjEu6UeyUCbiVGdRLF0GlRnAtS33WEyLudOq7YJvcmoL38X+XX8b5dyFsiNe0sabgX4rA46nfDhfUz6201fCLxg2T8t9vCUtgILcfXMdKTpSfkYHSYklW4D/sEMxi6154ZHIQV2J4Kqu3q3bY9LqDQoq0DIKi8CEkp6CPqpwkfdkJedm+/xKZppEv0SKeSw/YPsrEvT6XqIo8fQy/EAia2bvdaKUI1lDTDXFTSxh3yadFyZyulLVLh8AxjbG/hV8BpF/K3mqNdWGY3qnLSRBS09uTLQ2m3Joscnm+LizRnatEFNN4oxx60soI8BYBETSBUs4wUdsVWode5OkHdwyiM6Xv+y0x6zcAOzAZCGMPCP5JLlE5yA/Rfa1mGPegvHa8YNJxw2z5OpOVyHsw293o4jXxThgVxDFnhMFiNs5zbJUnsR/O0lFmvqixa+b8Ml4uXBEBBL2DAMheHnJO0D7e9Z5GcFEekGt//RaQPWxFf4q7brF/0gfTDgQcFIlPGnRvGXEIbkrCGfMk7dH7KnNW0q3d4drTqb+xU58ht9VsdzbLmoVc8 1ZBMxYU+ rhLJzN5tsvPxQEjGiY29AEBhBhtawIhFK6LqPmTPsSke3fTqKHEedy8iiDzMqD3RdUguh9b6NC5SkE7fcjUiLxZ25jUOeKM/ZW0pjbvUpuU0ujbYyeCJ8b7j0Dr0bOGjiapEXoyXo1Oh0N5olN2yFirIMlYK7IWvT1NAdpB1uXUi2vtPdJrnAmEBNL4GYwpDitJit8ziZJP7mIZVUP9FeXIBY4YxO+S8PV6uNlK8yCM9+/TnG3BOe5luHsHvJbLRf0lxGiIBKRKIb365FdydaVdYtocqRMEzqk5pXMJUc7ofOc/+F3upWeQuD/bUHFM0O3wgqfGsafqBXnYCIVPHMFPCZEPvOImYAoxfEY9eZkOKl76zsn33r79w4bLQmoS4sk4F99eeV9DTkpxnE5VeaMw6tUA9YfaWTPKaA/RomcWKJRIxYjF0hjOwfPAF+ChJRa1/Gzkbo2CXrHdw6TxI31WuRA5K5VKt7Z+4Ixx8Mbg9FYIkjfddGDoewQwxVMvnFkb86+EcE/oAiV7KsBKvCE1JuP0qxnMLP6Rj1LfEVELQh8KlfVH2KznZf74krj+GVWr0e 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: List-Subscribe: List-Unsubscribe: On Mon, May 13, 2024 at 11:41:34AM -0700, Deepak Gupta wrote: > On Mon, May 13, 2024 at 11:36:49AM -0700, Charlie Jenkins wrote: > > On Mon, May 13, 2024 at 10:47:25AM -0700, Deepak Gupta wrote: > > > On Fri, May 10, 2024 at 02:02:54PM -0700, Charlie Jenkins wrote: > > > > On Wed, Apr 03, 2024 at 04:34:58PM -0700, Deepak Gupta wrote: > > > > > `arch_calc_vm_prot_bits` is implemented on risc-v to return VM_READ | > > > > > VM_WRITE if PROT_WRITE is specified. Similarly `riscv_sys_mmap` is > > > > > updated to convert all incoming PROT_WRITE to (PROT_WRITE | PROT_READ). > > > > > This is to make sure that any existing apps using PROT_WRITE still work. > > > > > > > > > > Earlier `protection_map[VM_WRITE]` used to pick read-write PTE encodings. > > > > > Now `protection_map[VM_WRITE]` will always pick PAGE_SHADOWSTACK PTE > > > > > encodings for shadow stack. Above changes ensure that existing apps > > > > > continue to work because underneath kernel will be picking > > > > > `protection_map[VM_WRITE|VM_READ]` PTE encodings. > > > > > > > > > > Signed-off-by: Deepak Gupta > > > > > --- > > > > > arch/riscv/include/asm/mman.h | 24 ++++++++++++++++++++++++ > > > > > arch/riscv/include/asm/pgtable.h | 1 + > > > > > arch/riscv/kernel/sys_riscv.c | 11 +++++++++++ > > > > > arch/riscv/mm/init.c | 2 +- > > > > > mm/mmap.c | 1 + > > > > > 5 files changed, 38 insertions(+), 1 deletion(-) > > > > > create mode 100644 arch/riscv/include/asm/mman.h > > > > > > > > > > diff --git a/arch/riscv/include/asm/mman.h b/arch/riscv/include/asm/mman.h > > > > > new file mode 100644 > > > > > index 000000000000..ef9fedf32546 > > > > > --- /dev/null > > > > > +++ b/arch/riscv/include/asm/mman.h > > > > > @@ -0,0 +1,24 @@ > > > > > +/* SPDX-License-Identifier: GPL-2.0 */ > > > > > +#ifndef __ASM_MMAN_H__ > > > > > +#define __ASM_MMAN_H__ > > > > > + > > > > > +#include > > > > > +#include > > > > > +#include > > > > > + > > > > > +static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, > > > > > + unsigned long pkey __always_unused) > > > > > +{ > > > > > + unsigned long ret = 0; > > > > > + > > > > > + /* > > > > > + * If PROT_WRITE was specified, force it to VM_READ | VM_WRITE. > > > > > + * Only VM_WRITE means shadow stack. > > > > > + */ > > > > > + if (prot & PROT_WRITE) > > > > > + ret = (VM_READ | VM_WRITE); > > > > > + return ret; > > > > > +} > > > > > +#define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey) > > > > > + > > > > > +#endif /* ! __ASM_MMAN_H__ */ > > > > > diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h > > > > > index 6066822e7396..4d5983bc6766 100644 > > > > > --- a/arch/riscv/include/asm/pgtable.h > > > > > +++ b/arch/riscv/include/asm/pgtable.h > > > > > @@ -184,6 +184,7 @@ extern struct pt_alloc_ops pt_ops __initdata; > > > > > #define PAGE_READ_EXEC __pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_EXEC) > > > > > #define PAGE_WRITE_EXEC __pgprot(_PAGE_BASE | _PAGE_READ | \ > > > > > _PAGE_EXEC | _PAGE_WRITE) > > > > > +#define PAGE_SHADOWSTACK __pgprot(_PAGE_BASE | _PAGE_WRITE) > > > > > > > > > > #define PAGE_COPY PAGE_READ > > > > > #define PAGE_COPY_EXEC PAGE_READ_EXEC > > > > > diff --git a/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c > > > > > index f1c1416a9f1e..846c36b1b3d5 100644 > > > > > --- a/arch/riscv/kernel/sys_riscv.c > > > > > +++ b/arch/riscv/kernel/sys_riscv.c > > > > > @@ -8,6 +8,8 @@ > > > > > #include > > > > > #include > > > > > #include > > > > > +#include > > > > > +#include > > > > > > > > > > static long riscv_sys_mmap(unsigned long addr, unsigned long len, > > > > > unsigned long prot, unsigned long flags, > > > > > @@ -17,6 +19,15 @@ static long riscv_sys_mmap(unsigned long addr, unsigned long len, > > > > > if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) > > > > > return -EINVAL; > > > > > > > > > > + /* > > > > > + * If only PROT_WRITE is specified then extend that to PROT_READ > > > > > + * protection_map[VM_WRITE] is now going to select shadow stack encodings. > > > > > + * So specifying PROT_WRITE actually should select protection_map [VM_WRITE | VM_READ] > > > > > + * If user wants to create shadow stack then they should use `map_shadow_stack` syscall. > > > > > + */ > > > > > + if (unlikely((prot & PROT_WRITE) && !(prot & PROT_READ))) > > > > > > > > The comments says that this should extend to PROT_READ if only > > > > PROT_WRITE is specified. This condition instead is checking if > > > > PROT_WRITE is selected but PROT_READ is not. If prot is (VM_EXEC | > > > > VM_WRITE) then it would be extended to (VM_EXEC | VM_WRITE | VM_READ). > > > > This will not currently cause any issues because these both map to the > > > > same value in the protection_map PAGE_COPY_EXEC, however this seems to > > > > be not the intention of this change. > > > > > > > > prot == PROT_WRITE better suits the condition explained in the comment. > > > > > > If someone specifies this (PROT_EXEC | PROT_WRITE) today, it works because > > > of the way permissions are setup in `protection_map`. On risc-v there is no > > > way to have a page which is execute and write only. So expectation is that > > > if some apps were using `PROT_EXEC | PROT_WRITE` today, they were working > > > because internally it was translating to read, write and execute on page > > > permissions level. This patch make sure that, it stays same from page > > > permissions perspective. > > > > > > If someone was using PROT_EXEC, it may translate to execute only and this change > > > doesn't impact that. > > > > > > Patch simply looks for presence of `PROT_WRITE` and absence of `PROT_READ` in > > > protection flags and if that condition is satisfied, it assumes that caller assumed > > > page is going to be read allowed as well. > > > > The purpose of this change is for compatibility with shadow stack pages > > but this affects flags for pages that are not shadow stack pages. > > Adding PROT_READ to the other cases is redundant as protection_map > > already handles that mapping. Permissions being strictly PROT_WRITE is > > the only case that needs to be handled, and is the only case that is > > called out in the commit message and in the comment. > > Yeah that's fine. > I can change the commit message or just strictly check for PROT_WRITE. > It doesn't change bottomline, I am fine with either option. > > Let me know your preference. I would prefer the strict check. This is not critical though so I will support whatever you decide! - Charlie > > > > > - Charlie > > > > > > > > > > > > > > > > > + prot |= PROT_READ; > > > > > + > > > > > return ksys_mmap_pgoff(addr, len, prot, flags, fd, > > > > > offset >> (PAGE_SHIFT - page_shift_offset)); > > > > > } > > > > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > > > > > index fa34cf55037b..98e5ece4052a 100644 > > > > > --- a/arch/riscv/mm/init.c > > > > > +++ b/arch/riscv/mm/init.c > > > > > @@ -299,7 +299,7 @@ pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); > > > > > static const pgprot_t protection_map[16] = { > > > > > [VM_NONE] = PAGE_NONE, > > > > > [VM_READ] = PAGE_READ, > > > > > - [VM_WRITE] = PAGE_COPY, > > > > > + [VM_WRITE] = PAGE_SHADOWSTACK, > > > > > [VM_WRITE | VM_READ] = PAGE_COPY, > > > > > [VM_EXEC] = PAGE_EXEC, > > > > > [VM_EXEC | VM_READ] = PAGE_READ_EXEC, > > > > > diff --git a/mm/mmap.c b/mm/mmap.c > > > > > index d89770eaab6b..57a974f49b00 100644 > > > > > --- a/mm/mmap.c > > > > > +++ b/mm/mmap.c > > > > > @@ -47,6 +47,7 @@ > > > > > #include > > > > > #include > > > > > #include > > > > > +#include > > > > > > > > It doesn't seem like this is necessary for this patch. > > > > > > Thanks. Yeah it looks like I forgot to remove this over the churn. > > > Will fix it. > > > > > > > > > > > - Charlie > > > > > > > > > > > > > > #include > > > > > #include > > > > > -- > > > > > 2.43.2 > > > > >