]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE
authorDeepak Gupta <debug@rivosinc.com>
Mon, 26 Jan 2026 04:09:53 +0000 (21:09 -0700)
committerPaul Walmsley <pjw@kernel.org>
Mon, 26 Jan 2026 04:09:53 +0000 (21:09 -0700)
'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. The above changes ensure that existing apps
continue to work because underneath, the kernel will be picking
'protection_map[VM_WRITE|VM_READ]' PTE encodings.

Reviewed-by: Zong Li <zong.li@sifive.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Deepak Gupta <debug@rivosinc.com>
Tested-by: Andreas Korb <andreas.korb@aisec.fraunhofer.de> # QEMU, custom CVA6
Tested-by: Valentin Haudiquet <valentin.haudiquet@canonical.com>
Link: https://patch.msgid.link/20251112-v5_user_cfi_series-v23-6-b55691eacf4f@rivosinc.com
Signed-off-by: Paul Walmsley <pjw@kernel.org>
arch/riscv/include/asm/mman.h [new file with mode: 0644]
arch/riscv/include/asm/pgtable.h
arch/riscv/kernel/sys_riscv.c
arch/riscv/mm/init.c

diff --git a/arch/riscv/include/asm/mman.h b/arch/riscv/include/asm/mman.h
new file mode 100644 (file)
index 0000000..0ad1d19
--- /dev/null
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_MMAN_H__
+#define __ASM_MMAN_H__
+
+#include <linux/compiler.h>
+#include <linux/types.h>
+#include <linux/mm.h>
+#include <uapi/asm/mman.h>
+
+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__ */
index 9acd58a67123b658801913d2a25454b01cc24ee4..b384a1a9327ef6981a79a588f83e12a0f363b716 100644 (file)
@@ -178,6 +178,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
index 795b2e815ac9232c87a521e87970866c0c9e165c..22fc9b3268bea506bdfe7d796599f08d3f536f5a 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <linux/syscalls.h>
 #include <asm/cacheflush.h>
+#include <asm-generic/mman-common.h>
 
 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));
 }
index addb8a9305bee939a09e0af867351a89b3f948a1..25a8f693a765f77f86c69d42cf4c42faf63e3d72 100644 (file)
@@ -376,7 +376,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,