From patchwork Fri Mar 29 04:44:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610053 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 6D771C6FD1F for ; Fri, 29 Mar 2024 04:46:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED1586B00A1; Fri, 29 Mar 2024 00:46:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E7F926B00A2; Fri, 29 Mar 2024 00:46:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C85906B00A3; Fri, 29 Mar 2024 00:46:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A72616B00A1 for ; Fri, 29 Mar 2024 00:46:19 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 38811A1119 for ; Fri, 29 Mar 2024 04:46:19 +0000 (UTC) X-FDA: 81948840078.02.02767C9 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf28.hostedemail.com (Postfix) with ESMTP id 63C6DC0007 for ; Fri, 29 Mar 2024 04:46:17 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VV+S4kcz; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687577; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; b=tSMo+dAsF5K5L5rHWJkpM7C8+dWWo/CMPE4dXnMUJ/gEHbp3kjqtBzpZjffHalm1k8BMMN nLRkFVuY3hJd3y0h0IIlvGDva3HTdJzGw5OOFZTPJd9d/a1asEdaYLV898isiR8srWIpYZ jMxC2abue5Iozi7YSzRcrtDiNLPFYN8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687577; a=rsa-sha256; cv=none; b=2xUsmU1LPUhT9gg6X/ruC0ABA6x6Ymus+Y/CeF7IhBIHW8krcsgN9gWGjqDEGXeX2ZXGyx WRn5cCxIth5YkIkm0GTFUdmx8cT9Tp6H39AfS6LWRlmMYBfpOsKP1IjZOTvLpYH8VwBH2a lNXf56mM5z/b7YrRE/ppivTPV4wka2o= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VV+S4kcz; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-6e782e955adso1551909b3a.3 for ; Thu, 28 Mar 2024 21:46:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687576; x=1712292376; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; b=VV+S4kczzSNNZ7aiX2lG714UtDtSkNa+B6GVFstVZ7W7OLHqNlspuPjB4wfaA3LBOv CtVkYcg0nHrMiOXcfxIFPNi2PWc/HZ9WlM0eH3MBd8IznVDVgaSaDM1jERnD3cvJXQSU 1Rpid20Ux73UZaTaWJtNNW6YD33wGXnMBiYIWMoQpGcQM3IVztbvsbUai3ciSCSsFjJa 9XSCc/8/+8A2QIfgakViLw9SqvJnKbAgSIRy0zFDITcBTAjVI53cQi2FR2K45TsKMAYF wuyfyKrWk9D1YYmq8viZXdcp02eloKaKfHFJ8zLWqX67eOIpH/MyxRp+rj8ep1Ts9etm ggsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687576; x=1712292376; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; b=C9qJ2L/Ac03YIXdix4dWuKgWKAyMJkGuiU4qUV36kLgZ0AO69ANyBnX40LiHrCHn7I lh9kHLm0Qjrg9NMN+fCdZR7TMJ2H7vHx/Q5hEzWifXz7v60zt+iRYmolZlKLa1qtXSFd ga1CzSkHdFTmuYIDqOzLXeqGj/XaBhkoqJ7sY01GknEibmaqwtLur+YvdKrF0A/jIvA7 GXuQ146CLH7FwsbrPdJxiknfbs3SwKl/BOTMFSLgX1QBXl/AoqFiiwUiMMN+foRJ1q2w 8ud9X0p8l5V6SHtz4AlBPGC0FadZPESz74w/K2dQfpX7C8N1Qa6DSTTD8NiTWCT4/Gk+ LrwQ== X-Forwarded-Encrypted: i=1; AJvYcCUkowVftkh9uJ4GTpKcBeFhMNUaBiQVVk/h5s/Tz68uoet0Q+oTtRGGYHZ7rAiCizz8jajqRCvS5Mf+eDsqcH8rz6g= X-Gm-Message-State: AOJu0YwqCiNDCiPeroalUkPK5ew0TUFxHP5KcVTNoN6/e8UuJdeswQwV 4cJiGtaV3LdX65PrxYbf62iKhkC5EHlFUzqLjbth88/pXLpqg0Dls3u5PPNXLNk= X-Google-Smtp-Source: AGHT+IEEpy9pSrtr+BvW+AqTbkR2NWQ8ISKhZnwm1fAksXWCTeDRwW/aV3jv7PvAv025C+VRS8TIYg== X-Received: by 2002:a05:6a00:3ccb:b0:6ea:74d4:a01c with SMTP id ln11-20020a056a003ccb00b006ea74d4a01cmr1489683pfb.14.1711687576218; Thu, 28 Mar 2024 21:46:16 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:15 -0700 (PDT) From: Deepak Gupta To: 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, palmer@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 Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, 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, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.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: [PATCH v2 09/27] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Date: Thu, 28 Mar 2024 21:44:41 -0700 Message-Id: <20240329044459.3990638-10-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: 63e1iajgg8ewwju7fxdx1ct8cfxf76jb X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 63C6DC0007 X-Rspam-User: X-HE-Tag: 1711687577-457027 X-HE-Meta: U2FsdGVkX1/oJjx46Zbhm1aieQfxv1jiQt86+1W0vyD6FGGSAAglLxYcvUS2eaSzNVfSB29/75gcUtI0Xu5X/oTi/GTRO6d06uy/y//jEh1aBxpXoiMXM/7Ew3SMDmnvxvknYh/1l2MrT20DQaTGxx+2XTotFv09c+ybSEnfjxGdeTv4qq2/rIhYMCUhtWhHn5xmK6n4tuwX66PkievG8zrWb3LwmwoERVDzig0NpibNKxCdZI9+AY3teH3NAsv8J7QavKyJPPxrmxXC5OGHx/JYOg+f6loNjT0re0GwL5BQc0P6XMzS4kPSJyIXZVyDg1bUc38goADA+9W9qBUjlUQ4UcMqMgfRolw/iQYaqX3V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vbHCbvC0zMuooTtvF62kHoMF5RbWUCvnO+1BAZgk7wAVozH2YUpvUT6XR1M/wMJ9nI1KpYXbrbDmQ32RNWo6W79cHjoZ0HkxWkD1nx8LPuV4H3H1UuQmZESgZOnu+OQQ 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: `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))) + 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 #include #include