Skip to contentSkip to conversation

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

PATCH In review Zephyr Li 2 participants 2 messages Nov 18 – Nov 23 RISC-V

lore
Share & tools
lore

Zephyr Li

author
#0 lore raw

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

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

---
 target/riscv/tcg/cpu_helper.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/riscv/tcg/cpu_helper.c +6 -0
index 58e98e975c..8f6f980ecf 100644
@@ -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;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Zephyr Li

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.

Quoted 8 lines

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

---

Quoted 27 lines
  target/riscv/tcg/cpu_helper.c | 6 ++++++
  1 file changed, 6 insertions(+)
target/riscv/tcg/cpu_helper.c +6 -0
index 58e98e975c..8f6f980ecf 100644
@@ -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;