[PATCH] target/riscv: raise access fault for shadow stack accesses to non-SS pages

Christopher Elwood posted 1 patch 1 day, 18 hours ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261126063219.90134-1-christopher.elwood2024@gmail.com
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
target/riscv/cpu.h            |  1 +
target/riscv/tcg/cpu_helper.c | 23 ++++++++++++++++++++++-
2 files changed, 23 insertions(+), 1 deletion(-)
[PATCH] target/riscv: raise access fault for shadow stack accesses to non-SS pages
Posted by Christopher Elwood 1 day, 18 hours ago
Section 15.1.2.3 ("Shadow Stack Memory Protection") of the RISC-V
privileged specification says:

    "Shadow stack instructions are restricted to accessing shadow stack
    (pte.xwr=010b) pages. Should a shadow stack instruction access a
    page that is not designated as a shadow stack page and is not
    marked as read-only (pte.xwr=001), a store/AMO access-fault
    exception will be invoked."

QEMU does not implement this.  For sspush and ssamoswap.d in S-mode:

 - RW (xwr=011) and RWX (xwr=111) pages are written with no trap.
 - X (xwr=100) and RX (xwr=101) pages raise a store page fault
   (cause 15) instead of an access fault (cause 7).

SSPOPCHK loads through the same MMU index, so it is affected too: on RW,
RWX and RX pages it raised a software-check exception (cause 18) when
the loaded value mismatched, and on X pages a page fault.  It now raises
a store/AMO access fault on all four, which takes priority over the
software check.

Handle these encodings in the leaf PTE switch of get_physical_address()
and return a new TRANSLATE_SS_ACCESS_FAULT result for shadow stack
accesses on the first stage.  riscv_cpu_tlb_fill() reports it as a
store/AMO access fault.  Read-only pages keep raising a store page
fault, and G-stage leaves are unaffected.

Tested: unpatched master vs. this patch on riscv64-softmmu, same base
and configure flags.  Reproducer from #4622: exit status 255 before, 0
after, all eight cases raising cause 7.  Scenario 3 from #4379
(sspopchk with a mismatching value on an RW page): cause 18 before,
cause 7 after; scenario 1 (unmapped page) stays at cause 15.  sspopchk
on RW/RWX/X/RX pages now raises cause 7, and sspush, sspopchk and
ssamoswap.d on SS, read-only and unmapped pages are unchanged.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4622
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4379
Co-developed-by: Tarun Patanjali tarun.patanjali@utexas.edu
Signed-off-by: Tarun Patanjali tarun.patanjali@utexas.edu
Co-developed-by: Rishab Pradeep rishab@rispop.dev
Signed-off-by: Rishab Pradeep rishab@rispop.dev
Signed-off-by: Christopher Elwood christopher.elwood2024@gmail.com
---
Scenario 2 of the reproducer attached to #4379 (M-mode ssamoswap.w,
mtval = 0 instead of the faulting address) is a separate problem and is
not changed by this patch.

The new check is made before the U/SUM and A/D checks.  The spec says
U/SUM is enforced normally for shadow stack accesses, so I would like
confirmation that an access fault is the right result for an SS
instruction to an RW page that also fails one of those checks.

 target/riscv/cpu.h            |  1 +
 target/riscv/tcg/cpu_helper.c | 23 ++++++++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
index 49a1186bdbb..2dd76837a7e 100644
--- a/target/riscv/cpu.h
+++ b/target/riscv/cpu.h
@@ -139,6 +139,7 @@ enum {
     TRANSLATE_PMP_FAIL,
     TRANSLATE_G_STAGE_FAIL,
     TRANSLATE_PMA_FAIL,
+    TRANSLATE_SS_ACCESS_FAULT,
 };
 
 /* Extension context status */
diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c
index 58e98e975c2..d8af744068e 100644
--- a/target/riscv/tcg/cpu_helper.c
+++ b/target/riscv/tcg/cpu_helper.c
@@ -1438,6 +1438,19 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
             return TRANSLATE_FAIL;
         }
         break;
+    case PTE_R | PTE_W:
+    case PTE_R | PTE_W | PTE_X:
+    case PTE_X:
+    case PTE_R | PTE_X:
+        /*
+         * Zicfiss: a shadow stack access may only target a shadow stack
+         * page (xwr=010) or a read-only page (handled above as a store
+         * page fault). Any other leaf is a store/AMO access fault.
+         */
+        if (is_sstack_idx && first_stage) {
+            return TRANSLATE_SS_ACCESS_FAULT;
+        }
+        break;
     }
 
     int prot = 0;
@@ -1780,6 +1793,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
     bool two_stage_lookup = mmuidx_2stage(mmu_idx);
     bool two_stage_indirect_error = false;
     int ret = TRANSLATE_FAIL;
+    MMUAccessType fault_type = access_type;
     privilege_mode_t mode = mmuidx_priv(mmu_idx);
     /* default TLB page size */
     hwaddr tlb_size = TARGET_PAGE_SIZE;
@@ -1877,6 +1891,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
 
     if (ret == TRANSLATE_PMP_FAIL || ret == TRANSLATE_PMA_FAIL) {
         pmp_pma_violation = true;
+    } else if (ret == TRANSLATE_SS_ACCESS_FAULT) {
+        /*
+         * Shadow stack accesses to non-SS pages are always store/AMO
+         * access faults.
+         */
+        pmp_pma_violation = true;
+        fault_type = MMU_DATA_STORE;
     }
 
     if (ret == TRANSLATE_SUCCESS) {
@@ -1903,7 +1924,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
         cpu_check_watchpoint(cs, address, size, MEMTXATTRS_UNSPECIFIED,
                              wp_access, retaddr);
 
-        raise_mmu_exception(env, address, access_type, pmp_pma_violation,
+        raise_mmu_exception(env, address, fault_type, pmp_pma_violation,
                             first_stage_error, two_stage_lookup,
                             two_stage_indirect_error);
         cpu_loop_exit_restore(cs, retaddr);
-- 
2.39.5 (Apple Git-154)