[PATCH] target/riscv: Reject shadow-stack accesses in Bare mode

Zephyr Li posted 1 patch 4 days, 21 hours ago
target/riscv/tcg/cpu_helper.c | 6 ++++++
1 file changed, 6 insertions(+)
[PATCH] target/riscv: Reject shadow-stack accesses in Bare mode
Posted by Zephyr Li 4 days, 21 hours ago
Zicfiss requires shadow-stack memory accesses to raise a store/AMO
access fault when the effective privilege is below M and satp.MODE
(or vsatp.MODE when virtualized) is Bare.

get_physical_address() currently returns an identity mapping with full
permissions for Bare before applying any shadow-stack checks. A
shadow-stack instruction can therefore access ordinary memory instead
of taking the required exception.

Reject shadow-stack accesses in the first-stage Bare path using
TRANSLATE_PMP_FAIL, so the existing exception handling reports an
access fault. SSPOPCHK's existing unwind metadata promotes its load
fault to the required store/AMO fault.

Apply the same restriction to the MMU-disabled identity-mapping path,
while preserving the M-mode fast path.

Restrict the check to the first stage so a valid VS-stage shadow-stack
mapping can still be used with G-stage Bare, subject to the usual
PMP/PMA checks.

Fixes: 669b4867495c ("target/riscv: mmu changes for zicfiss shadow stack protection")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4432
Signed-off-by: Zephyr Li <fritchleybohrer@gmail.com>
---
 target/riscv/tcg/cpu_helper.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c
index 58e98e975c..8f6f980ecf 100644
--- a/target/riscv/tcg/cpu_helper.c
+++ b/target/riscv/tcg/cpu_helper.c
@@ -1131,6 +1131,9 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
     }
 
     if (mode == PRV_M || !riscv_cpu_cfg(env)->mmu) {
+        if (mode != PRV_M && first_stage && is_sstack_idx) {
+            return TRANSLATE_PMP_FAIL;
+        }
         *physical = addr;
         *ret_prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
         return TRANSLATE_SUCCESS;
@@ -1181,6 +1184,9 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
     case VM_1_10_SV57:
       levels = 5; ptidxbits = 9; ptesize = 8; break;
     case VM_1_10_MBARE:
+        if (first_stage && is_sstack_idx) {
+            return TRANSLATE_PMP_FAIL;
+        }
         *physical = addr;
         *ret_prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
         return TRANSLATE_SUCCESS;
-- 
2.43.0