From nobody Tue Dec 16 13:33:15 2025 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 022B134A3CD for ; Fri, 5 Dec 2025 18:37:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764959828; cv=none; b=PKRDx5wCDD2PYqwY1onGfPECwcaTGOZMEEjGkb1IaceY9TWrkSSqODn2TWY/hut58sOXEGbKehgC9fyVhfYBjBZQXWDnT6O6f5jHSaR1Rwp7+DMEny7v59PvCmmBHVpgGiW69srh15UTO5sYvKVEwtqo5nc41vz3V50MXj4ZWRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764959828; c=relaxed/simple; bh=7yLe9hWq2WlkWVyw5cBB8KO8waygymuFRUw/WyJutrk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nGXJfTObnISrSFFY4+/qc8jBugU/J7OysiBIEiLqt5RbN2sDG5TKGdoMiHzdmjGmY/RmI1oElkiUZYvkTgmEyvjVnBAlx/Nli5ByLmheP+coQdE7xS2fEXGZKGzOruIBUoEmV3Qoc6AroMINFJSLUH21RuueXqVLDaknwyP8wCw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc.com header.i=@rivosinc.com header.b=DBJSkVBM; arc=none smtp.client-ip=209.85.215.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc.com header.i=@rivosinc.com header.b="DBJSkVBM" Received: by mail-pg1-f194.google.com with SMTP id 41be03b00d2f7-bc17d39ccd2so1453923a12.3 for ; Fri, 05 Dec 2025 10:37:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc.com; s=google; t=1764959822; x=1765564622; darn=vger.kernel.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=Qa06X7BN7aAxeMRj1RtrRZw6MLM+PhxlK6i/AnJxckg=; b=DBJSkVBMi42qPtEzRXZpY+31ucmN6Q2tMtQYXIHd7zxPDn5VimsDZb9DCsyCxC6Sav nOpKFiSO5YFwHISd1t5ObK+xEIShFR8g+sUGvB3Dn6zmOJ1jEgxBXtj8qagY6xZG091U AsGeuiSfnjf3eFRiH0QIV8CHRiPS5C+0nbK3Y+9RLX+/6ySuA+NQp14Ex2axYkc5uUKN k1wT+jKTDG9U/Rv9tP2B2MSn1rUqgcJPDFJgWC4IEL6xfDr26HI81Vt3XSa7m9mXRgAT 4KF4eLTB2Gxnuk5ryQ1p5Rtsxy+KJslGBC4gUq4bcrkCjgzDZ7xGjF5gun2tCr8Nqcej dbDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764959822; x=1765564622; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Qa06X7BN7aAxeMRj1RtrRZw6MLM+PhxlK6i/AnJxckg=; b=UgUkEFzkI9jEIqN6wXDhzwQN92499wMGbCO7hEAdzxkikRtuD3KGk2N28MUv1zy2y0 aclz2kCSIgZA15O5uHYn3jp9FcEiDwnyWxk4AXn9ESicnM05ym8m9LzZlXnh1lNp795e /OwHvmVQrg1OlAZUzFcmotkM9IC8bOMfWLRkYyWPw5VXlbaYHOdEmwpG7CT4dfEPfLDc QvuDJl9UoOkCvR42/n1MVuBoXqfCwnu13jditngTfHV2jhGu6G2iNUHuKGjtyBxLzBX5 8LG6/+sPheLxpDxcG/pRkfqYilsHNprSHnQTnnkMfxS17V4ZG/NnJft3JvzQSCI7AUXU kMjQ== X-Gm-Message-State: AOJu0Yx8yok9ZSEFe+SnzKaATZfBB800NtYxsdTTQB35SW0ngQFiy9VH t2s2W76SVHjo+5gv49NCXupOAd/Pf3oX+KijXnhwqCrhked7oJWTXHg9aEKbQnQ9ES4= X-Gm-Gg: ASbGnctYzvLm9ijo9EY+E4nJpE4D7q//YuwnECabSCr0wetvFihX1O1HXs9fqOCKO8X TdmP5ZUG4Mz7uPS8fUWazjwUp5wKH+Bi2vNrHp5GAAlbi4xIrSxWzZ+v+k5ziVf9/VzOnSqlIKD Id9V3Uggc1cn5S2OJ7ZlcckHpnMJpkUWAKkzGMg/bX5t8EAXwF1E+TfYlh1bTva1oCznBzNyt1u UA2StqlDmlLHxdgmtkl8FuKV5J6cHmqnK8rWw/UndwA1JnTD1A7z3uCKg3C3bdnFWEoXXDJ8ULP vPLvZg30+F8OIeaKZICO3q9iIjNItaF/5Tdssc8iu0UIYfgD44GJKH2x+GSArQMeDiqGEtrCh29 qsLwbdTsr5K1MaRbO3K+WbLibwzSUVaBnRg8CG1TA41sRr9O3n4NWvuxWKRVlQfwWDfl+v/1thv whjT+aAOjUtBIPzDHK6qX7tsFhXjK+Y54= X-Google-Smtp-Source: AGHT+IFSuIato7By2LD1j5BN0WlXvTN4dkTg3+zsfi//usJ4CscSRgzvoiXp1vgnstjZYR4syaJugA== X-Received: by 2002:a05:7300:3084:b0:2ab:c151:3534 with SMTP id 5a478bee46e88-2abc71139a9mr127815eec.5.1764959822438; Fri, 05 Dec 2025 10:37:02 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2aba8395d99sm23933342eec.1.2025.12.05.10.37.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Dec 2025 10:37:01 -0800 (PST) From: Deepak Gupta Date: Fri, 05 Dec 2025 10:36:52 -0800 Subject: [PATCH v25 06/28] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251205-v5_user_cfi_series-v25-6-8a3570c3e145@rivosinc.com> References: <20251205-v5_user_cfi_series-v25-0-8a3570c3e145@rivosinc.com> In-Reply-To: <20251205-v5_user_cfi_series-v25-0-8a3570c3e145@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 , Jann Horn , Conor Dooley , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Benno Lossin 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, rust-for-linux@vger.kernel.org, Zong Li , Andreas Korb , Valentin Haudiquet , Deepak Gupta X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1764959808; l=4345; i=debug@rivosinc.com; s=20251023; h=from:subject:message-id; bh=7yLe9hWq2WlkWVyw5cBB8KO8waygymuFRUw/WyJutrk=; b=brRg4Sn8vtoSLKpcwx4+loNsIV5h3ZB1DTXP5anyC0qWKOWE2LiMPjioo+IiPxRaUxf/54FOC aXStfisB/ojBRwd6CwGucPTtiKuZH9f6XIZV3TLOlrJBIxk+6atHMhk X-Developer-Key: i=debug@rivosinc.com; a=ed25519; pk=O37GQv1thBhZToXyQKdecPDhtWVbEDRQ0RIndijvpjk= `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. Reviewed-by: Zong Li Reviewed-by: Alexandre Ghiti Signed-off-by: Arnd Bergmann Tested-by: Andreas Korb Tested-by: Valentin Haudiquet Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/mman.h | 26 ++++++++++++++++++++++++++ arch/riscv/include/asm/pgtable.h | 1 + arch/riscv/kernel/sys_riscv.c | 10 ++++++++++ arch/riscv/mm/init.c | 2 +- 4 files changed, 38 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..0ad1d19832eb --- /dev/null +++ b/arch/riscv/include/asm/mman.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_MMAN_H__ +#define __ASM_MMAN_H__ + +#include +#include +#include +#include + +static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, + unsigned long pkey __always_unused) +{ + unsigned long ret =3D 0; + + /* + * If PROT_WRITE was specified, force it to VM_READ | VM_WRITE. + * Only VM_WRITE means shadow stack. + */ + if (prot & PROT_WRITE) + ret =3D (VM_READ | VM_WRITE); + return ret; +} + +#define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pk= ey) + +#endif /* ! __ASM_MMAN_H__ */ diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 29e994a9afb6..4c4057a2550e 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -182,6 +182,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) =20 #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 795b2e815ac9..22fc9b3268be 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -7,6 +7,7 @@ =20 #include #include +#include =20 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; =20 + /* + * 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_WRI= TE | 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 |=3D 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 d85efe74a4b6..62ab2c7de7c8 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -376,7 +376,7 @@ pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(P= AGE_SIZE); static const pgprot_t protection_map[16] =3D { [VM_NONE] =3D PAGE_NONE, [VM_READ] =3D PAGE_READ, - [VM_WRITE] =3D PAGE_COPY, + [VM_WRITE] =3D PAGE_SHADOWSTACK, [VM_WRITE | VM_READ] =3D PAGE_COPY, [VM_EXEC] =3D PAGE_EXEC, [VM_EXEC | VM_READ] =3D PAGE_READ_EXEC, --=20 2.45.0