@samitouri / QOSamiQemu / commits / 0ef6910e32

target/riscv: Avoid NULL deref in IMSIC CSR write

rmw_xireg_aia() and rmw_xtopei() were changed to store the IMSIC callback's value in a local before passing it back to the caller. For write only CSR accesses that pointer is NULL, causing a crash when guest programs write to xireg or xtopei, such as when the guest sets up the IMSIC. This only happens when setting aia=aplic-imsic so none of the existing boot tests caught it. Fix it by guarding the pointer dereference as done for other CSRs. Fixes: 63469ad75dcc ("target/riscv: Fix arguments to board IMSIC emulation callbacks") Signed-off-by: Joel Stanley <joel@jms.id.au> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Anton Johansson <anjo@rev.ng> Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> Message-ID: <20260617054034.1020724-2-joel@jms.id.au> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

Joel Stanley committed Jun 17, 2026 at 15:09 UTC 0ef6910e3269548112c8d81e511c8627d0b9396a
1 file changed +6 -2
target/riscv/csr.c
+6 -2
@@ -2733,7 +2733,9 @@ static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno,
2733 AIA_MAKE_IREG(isel, priv, virt, vgein,
2734 riscv_cpu_mxl_bits(env)),
2735 &wide_val, new_val, wr_mask);
2736 - *val = wide_val;
2736 + if (val) {
2737 + *val = wide_val;
2738 + }
2739 }
2740 } else {
2741 isel_reserved = true;
@@ -3009,7 +3011,9 @@ static RISCVException rmw_xtopei(CPURISCVState *env, int csrno,
3011 AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
3012 riscv_cpu_mxl_bits(env)),
3013 &wide_val, new_val, wr_mask);
3012 - *val = wide_val;
3014 + if (val) {
3015 + *val = wide_val;
3016 + }
3017
3018 done:
3019 if (ret) {