From patchwork Mon Nov 11 20:53:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871344 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 49ABCD3ABF5 for ; Mon, 11 Nov 2024 20:54:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21CF66B00AD; Mon, 11 Nov 2024 15:54:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A51A6B00AE; Mon, 11 Nov 2024 15:54:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC37B6B00C3; Mon, 11 Nov 2024 15:54:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id CCB756B00C1 for ; Mon, 11 Nov 2024 15:54:16 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6549D81B00 for ; Mon, 11 Nov 2024 20:54:16 +0000 (UTC) X-FDA: 82775015610.10.5522D7E Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf12.hostedemail.com (Postfix) with ESMTP id 0AB054000D for ; Mon, 11 Nov 2024 20:53:55 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="sRwwIuz/"; dmarc=none; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358326; a=rsa-sha256; cv=none; b=RBIxJf77zLYJO4z/Jn2jlwFqOXpWqxmXO54l8q2cVCiQLMaR4oXummm3/LPnDBgwaA5RyV Glk8jg4unEbyZywAZKg4fPAQuJSpFLz3PGFSySWzKS5tX0+up9IS4oeizMq2hZLg5TLg8p +JJ/snTfPAK/WilOAJTEafBrC5DooLY= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="sRwwIuz/"; dmarc=none; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 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=1731358326; 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=KPIeex8TzLPUDDSwLAkzt/bShtl/Fs0bUTE8bjGtA9Y=; b=8SGHU9CJbi7wKAw9mVSs1JvFlq+cdFdZi0lqnqVkjPRjHdTSUz6p8rIl6ki8pNjpc8dsQG hz7ihK9i0f59bhFtj5JzVzPWg1+InMPElppJINbspfPRsU9c7sDDoqAb7dxCzAT4W3yb6F pA439daOAtb7tjJBBbB+9yKtP25q7dc= Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2e2a999b287so3952314a91.0 for ; Mon, 11 Nov 2024 12:54:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358453; x=1731963253; 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=KPIeex8TzLPUDDSwLAkzt/bShtl/Fs0bUTE8bjGtA9Y=; b=sRwwIuz/ObP8/3Ud4Axte0bSadfD+676wIKyTtZnl/sifyNpMehdkoCngMrUAYk8oI oAv89zbqoYbMWhg0o3v27NzPkGJPHXBtx+eWHqeHSt4XPrcE7PM0hoX4RE1MbU/d0ZOs GHUcTsyv/E2sEt0V5/PXhd+JO1AA9528QY+ZlxoJvNoDad43UVGAZveZEsYk2AOfuZ3/ NSqzaNVS5yAUO3ernsTORR4VBZnbshj3eGvROmAaCcurbdQHgtn5rY9UutJRRod+9RxC lUE24FqfoMzgLWuhgDRupDEgxIQbC6h9oChI5cH7RQQpUiq1ExKhzDW02mCtMfQ9CM3x 2ulw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358453; x=1731963253; 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=KPIeex8TzLPUDDSwLAkzt/bShtl/Fs0bUTE8bjGtA9Y=; b=XmmKc/QIzZpC3zrEB7JpVZqZY7Ex70Ttwtogoax2+UIJq5Gwg0W7tCKuztNdVcSRyn f0Tdz51KeJCpUwBpv1KcsfoCb+lcl5jXC6bZ58iRxXmmH3CIU2SLDAJfMmmSTnm7TxXW EZxKrzXnh8jnKIuzH/MpbIdcFHXTi6VcFdvDgKazrZtyzq0p843KkDZYQ8TdOrrVdcgx tBFxt4vG/P0BYU0xtYw5SNLwEt2GCMFGuLLLCB8L0lwhQ8DbYU5/sweKVsnt/J59bgVZ mJQq9dqurdV/tN/H6/2ELRNq0iNllHtdm6WaNmffZiqqauuJqoAVSwGGNNMsfPjOAgPK ZGNg== X-Forwarded-Encrypted: i=1; AJvYcCWhV+x/XN0+wsDfWwEXYwJmcQ5ZopN9jA1yAt9dWtvlA/aOGvH3fleRj9pIgKejMkz3hfJN7+KqUA==@kvack.org X-Gm-Message-State: AOJu0Yxi3xDlY9tLAquw3ZpWzV65ecjEIC0Y96geXdL5n2Xt2O/R3c4+ 0q+opfvnpiLjzPTbFxoequszJWydNrExOV/wl5Ls1FJbfODMstmMzFKd5TDVCZM= X-Google-Smtp-Source: AGHT+IHxrhthgasHXbf/UNeTJ/b0NrvHjmhrINV/NhvROg5Fgd3+BWnrPq93OPr1Rkxjz5Z0BvI9CQ== X-Received: by 2002:a17:90b:2642:b0:2d3:ca3f:7f2a with SMTP id 98e67ed59e1d1-2e9b16c9138mr21909754a91.22.1731358453300; Mon, 11 Nov 2024 12:54:13 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:12 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:52 -0800 Subject: [PATCH v8 07/29] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-7-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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-Queue-Id: 0AB054000D X-Stat-Signature: ja68w116rt3gsso4hnzs8xhth4f94tp1 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1731358435-644481 X-HE-Meta: U2FsdGVkX1+Ul/aoTO4PX+/d4hCjiErmvKLlhrfy4rat6w7DUj5tzrtT8vPuMU0+DTHYiPUtjVcwxVTu99nXdVyEVSl61z3d7Qe0kweTGk5JC6VdXJrxP5NQamkueuIrXWlLkJ+pEuiw5fF5N56N7lAjM+Ma5Bssq4x4zGRzfpsHyzAv0mQt5NpT1/wtG98LzNeW//U4YvlpCO+cQLD96qb0dvLFD8U64QjBIT+wbRrLX2ewABpxBsHhA0u3rz+lwhKUr+0T7XPVTI10yxYWD6bTzOGRGXgJ7fGWOWoJEuefqJYdqgqb9VeONtYaEHn5z6efeRrSEpWuev3PMmllGcPID3BwLZrTeP1asfKGtj8pI5vZdrpUU/Y19+n25o/yO6M3dxyrmLqI0JbYA8Qng3s2eBabeP+QI2aMZrnikbVzEUBvFOj9xPNZaHvC8ErnLgsqUsUn+WxxynL7+EBjBkKedFfCgUDCvFarVDzBfbJ22bEtNO8tZenipxpw5LdQQ04Pe4Zkt1PdeRgKiNwcENh2zWBqHTGO+QyOspAi+AdrYjKs+7NoHI8BZAtexY2RKEo6uV4rhY8cjC6JoioPY8wA8gDIjKWQLvYSFEGPkauWZDW+DSiueICrYvqvG+OKLkmsa5u7qJSvBqOS7iANTlO+Scd+N/lks7BGQ/7+rA+t1oO5SJ1tAl1jIUmuQnQFSx8oyVV9gRCr8KrpTtsxlDHVE5vmORwdDwRfNlzIk9vpueChvH9cfkYh+8XNctyzRXKTIksrx7ucbKVodad4hm6gPV3rgrRAgrfEZNJAVsTKXHXZ7BIJ/pbufcBRqCanY7JUcZXige2IvWs59lJpVqhbDMGV4R/4sd4L8IaR9RHyajODtRmnQ+cIulu5ntsb0FvN1v5UoiNKKso6rZJoXwJh3aw8jU7qnlQeJwtdPaIZOr7W9GRsFq3BJCWj4AUCXlKo1wVzTGnNLtX30Xm dPTMB3Ez 6HblMqmEDLYHijJ0hTWCSJZ7LQQDIhqstsAYOLsQLTr7vx0gZxjyxYCCruIBVlUC14fR7rSuipTJRquWStXQpLGVQAz424lVVVgE6s8Jo95Kp+KxB/Nwklf4F0hpXoVN5uF21SIU91MkD/3Bf9cw9GrO8le6JJVLTLT0/wZbYAcDa5nl47X/HSt424fao34a1K6qr8T0WhCHrlaCxwzjoaYMdiwK2F2j2UODh7oXEUSWO9ozl1NoAmmKXMIt11L1nCDsmTJ4HXuKSl8HUuX31y95yDgYzIR2WWm0tyobd3oSKS0U7V6NFmPgU77kvaDiKzSzsJQRls0LWRVz6ExOAD6tALVNz1P1LjmRHx0uo3nty9kZQiF2YZky37vuf12eJXTkkLQakDVqK20BLNJ2L363AQ9GUTjCjdkUDkyPa42k7Id06KgG8JXzo76mP3QUc+lnRDTY+wSP6OXdRcXi31/T3PSuQRei1mVmvCNGJpsNw1nlpIiZrFBdsIKgwIHQFOmZWL+qfNthejIdJPcbqrJh6MABMy7zw5TiwI2FpGvrO2Fg= 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,