From patchwork Tue Oct 29 23:44:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855726 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 977C3D7494F for ; Tue, 29 Oct 2024 23:44:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 353896B00A3; Tue, 29 Oct 2024 19:44:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 301736B00A4; Tue, 29 Oct 2024 19:44:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1552B6B00A6; Tue, 29 Oct 2024 19:44:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E7D756B00A3 for ; Tue, 29 Oct 2024 19:44:44 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AE30912076A for ; Tue, 29 Oct 2024 23:44:44 +0000 (UTC) X-FDA: 82728271374.12.7AA5982 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf23.hostedemail.com (Postfix) with ESMTP id 87770140018 for ; Tue, 29 Oct 2024 23:44:26 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sTmpCCW8; dmarc=none; spf=pass (imf23.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245353; a=rsa-sha256; cv=none; b=wneGPE3n97nhYVXtONNfMnsghuqA8SK9bTIfDqmWDlZQKiHOT3JAEbUrsKraaa39rkdE+4 GJU3semXbmhUq00iPzU34vGSdDdKcMzp+b6NgTl3aYHGRMfGUvFKERe7Y/vkRsE8KqWGf+ CPNV8Qgham+3dXwQ8/BTT+1AdqZu41A= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sTmpCCW8; dmarc=none; spf=pass (imf23.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245353; 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=2uB6wgL7AUIk24SoLAfGxw+LLkCCjYapUwagzJacJBw=; b=biAGvCzZbon/daTNmDTotNufMOyV//71SivSa4jIkFxl4VQvJEfIsYbPku4O1Kvza97CZv IvHBM772/OIpkCOxp2eZjQa77LzfrtnLX3wjfJTTuWTtbqjwi0oxIGMJes5lLX84vMzY8A GyY07BybVnzXO6fSt+D9iCBtnNWvANA= Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7209415ca51so1143712b3a.2 for ; Tue, 29 Oct 2024 16:44:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245482; x=1730850282; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=2uB6wgL7AUIk24SoLAfGxw+LLkCCjYapUwagzJacJBw=; b=sTmpCCW8saSelxpn9yJP/ggrdVr3yvepfcVv0wvMlJpdTJ15iAttQDOB26r11Qt8ii L+Y8IkAskk3IhA6a6S86eb3l3TCT4V7C47wv7rPcA/0syO0Fnz66aFZv2biDGrZNu6Bg BN4w/6sldpo3yQX9P6iwh9dMVcmEr4g2spoveHjV+qZyS0P7dLLzjnf0GyeHe3GKKHqr BzMtlp9fHJQ0DsYy9kXzgBZefxrZ+Vj5vsqf4rvSYA5EVzesWEsLDcsiLQDMZcPovFkX 1+X0iyMOw0M4BQlkKscG+BQK5UB985vWqfujN0Ja45Qs3KMbyahTHyVr4ThMzohEBe/O O86A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245482; x=1730850282; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2uB6wgL7AUIk24SoLAfGxw+LLkCCjYapUwagzJacJBw=; b=lc9sAlAsIbOq32yQLq1KxqPtckhs6IdTDMnTt3VqjnsRxEZHnuvoKjRqY8nBQcxTXx MZfmm/wfRsFHMm4FJ89nid7PToUJ6yrrRa2fTXRoUcTI/2oM7KOJzntmQNHoAsX669Nu 1trxMwyE/VNv9w9WNHyxsPDArQBGj3RLH6sXyZIYyH+eAkjUeSnNqV4V3rlIbALlr0/L ncDoAZzdevdSHl0BJ/FRPOyVb2DAe67cvZwcqECEB+OQ2NAikMrDDUOlqyZzJTgEo5EI yKVw05i4ReMidLDHiQrfq2An+QM76gEK7+/H7jCKk8V2weHk2E7LYFzufAT9sekEm/16 /Y7Q== X-Forwarded-Encrypted: i=1; AJvYcCU+OIts5+QDWR8YcKmDE5cRPYXiBufTz2q44yv683S12tQeLYMl4nqfVOmDBO+wTxZ1ylU4mDNaYg==@kvack.org X-Gm-Message-State: AOJu0YyH2zoond9maxgXSVEnGh1QdgrCrwHcG2axukWcU28UlkFX1wuK 46c7j+/jfV9sLJuCmiFZcBd7pH2PA0pU1f6WbmKT714AquMwFVZ5yUjiF0ZXmM0= X-Google-Smtp-Source: AGHT+IE221LbNxuFRa65lyyYoevgLhuJqbF2HZlgl90nwLOZSnaHzzPzVHYzhAHk2jRfxhGW9tswvg== X-Received: by 2002:a05:6a00:2d84:b0:71e:693c:107c with SMTP id d2e1a72fcca58-72062fb8107mr18791154b3a.11.1730245481560; Tue, 29 Oct 2024 16:44:41 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:41 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:10 -0700 Subject: [PATCH v7 10/32] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-10-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 87770140018 X-Stat-Signature: faihbo3yjk4qfe5fyma5579p8gjqidpm X-Rspam-User: X-HE-Tag: 1730245466-800273 X-HE-Meta: U2FsdGVkX18be7R2oASyDZIW8qwS0cPn6gaEnszdUOq8zlVMCIeCgBdKo7P1AVaqYjd142fFu+klzAkzpkxdSUamw3Zn7tKdNlDv6kJ0o2w/Y/CvJA6LiuUnarg4S1SZDGP7hdfCBbXntml6ME/GdbW9638VYR9a4p6+SacuQ2jpM3HZk24CbpInA6SVb1hwO/Hy/B0n5+AEfF8HduNN0GYyFKTbwDPr4Ace5GSeyWaFOA/GHon7IdujaeFsUNN4uUjRaj+fbRtAWjoPaK9PVT+Uf0mzo9QTyvjzcgz2VhgIURdj/qinH+b1dkKPEIpbeEyZ87PlOgjAEZznxjK6t7j2NPz+yrUvrgA1Gy3V12i6ePnHGZmLQilZoNFue25WidVyP5ErgDUKC3ecsQ2hipgayir5SBgC6XrbwzNgeamHrjj/aHRwMx/oCF1slWUFSDRcD370NQSmC/kySECKLt1QKJu3AMtilwPyN6tqi08E1/2kd3RymLAzUivWXWuGMCZBjquN7hgJZHrL21pozBQpMfL7U9+SG4cAVb1UaOVJMP6mTGVKHHzcGRjeDJBb+AXh7l+bn+ynwoOXU8Z/Q9TZHdK0iEpFXCNc3fCTNAfTRwXSqAhgfyG4jlnHZwNhUmH45WOM3M+cAkDe9CONX9Y5yYddXrRwz9ktXExe0zDRq5lxhTtrEYxQEuGY+svPDR1vA4o8dxl6zMk1Ch/OSW9Ib/GLV2VYQ2F/STKn8jPamIwGWZ6Y1xtZWIkpvAkloTnS+fUdf7ZtBOmIMjMf9oLXsNiEX3eevXSzQSTNyPe2/sSQkquVktvF+f/bjqCFNAHCzLSU/KkYVR9LiL5DVWQWXxFqGh4nZlkrquVdP1/fHJKZ2LIyDyr4LYk667xo7yvDCV6oUhnj39JOifZhuyzljKe8bPTxEuZ3z0nF+ARQGm6Fmn/ZKKyJCgbj6jsy8HRrZ8ljD0wFmlmbHru eW9V9pAe 39BpO1dyOxYS/NVreNAORtzYewW6njV22el5OPRII6Qs/2z+6R8sz7H638xGJIg9fUeU6YetCHNtAEHCic+5wZ7HNuCV3nC9I+VfNiXgP9YBaj3tgrDByJO5u6M9ydR5foS1TFvx1c62lqJf/X3h2KjZWpnTjSuUca0u2ApLIYvhUa7K+0wsfjw0uAQ6wvwY4eTDS3kz5O4tkf23ycJmQn7Cv9yPimXuE6ALtTMKkxc1iyA9CiptbCHq/ystNnk5INqwrrNM3QiKVeC0V1x4HYQAAJ6gIRQueuDeQLxVYoJp8DGSFVFtGqnnWcDYNVNbVS+2Zf6XptEfSDU9HavXAiL8StbWwxBaHbcV6/n6qOyQLVV3CTpHyCYzoB0OghXhJCUdko+KCa3qYRpgb68mrCYxLm5DRsdoxS8bMUwGG3HYwvakabnw6dj45AMb2OXTGlGCSH/SLLIR5rOQFwGMq72BpE1l68QO2WmvRGGtqOSgouhDQ8FrOPFhKwF10G5eLgSTKKwidNRFrMrFp08u3VZ9rNZdRYLpILCPkJPkMrRh5jOI= 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 | 10 ++++++++++ arch/riscv/mm/init.c | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) 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 e79f15293492..4948a1f18ae8 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -177,6 +177,7 @@ extern struct pt_alloc_ops pt_ops __meminitdata; #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 d77afe05578f..43a448bf254b 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -7,6 +7,7 @@ #include #include +#include static long riscv_sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, @@ -16,6 +17,15 @@ static long riscv_sys_mmap(unsigned long addr, unsigned long len, if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) return -EINVAL; + /* + * If 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 0e8c20adcd98..964810aeb405 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -326,7 +326,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,