target/riscv: fix access permission checks for CSR_SSP
PATCH 1/2 target/riscv: fix access permission checks for CSR_SSP
Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced CSR_SSP but it mis-interpreted the spec on access to CSR_SSP in M-mode. Gated to CSR_SSP is not gated via xSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer" of zicfiss specification. Thanks to Adam Zabrocki for bringing this to attention.
for zicfiss"
--- target/riscv/csr.c | 5 +++++ 1 file changed, 5 insertions(+)
target/riscv/csr.c +5 -0
@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +/* If ext implemented, M-mode always have access to SSP CSR */ +if (env->priv ==PRV_M ) { +return RISCV_EXCP_NONE ; + } +/* if bcfi not active for current env, access to csr is illegal */ if (!cpu_get_bcfien (env)) {#if !defined(CONFIG_USER_ONLY )
Signature · 1 line
2.34.1
PATCH 2/2 target/riscv: fixes a bug against `ssamoswap` behavior in M-mode
Commit f06bfe3dc38c ("target/riscv: implement zicfiss instructions") adds ssamoswap instruction. ssamoswap takes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), then ssamoswap must result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.
Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However, ssamoswap can still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior for ssamoswap.
--- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
target/riscv/insn_trans/trans_rvzicfiss.c.inc +11 -2
@@ -15,6 +15,13 @@ * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ + + #define REQUIRE_ZICFISS(ctx) do { \ + if (!ctx->cfg_ptr->ext_zicfiss) { \ + return false; \ + } \ +} while (0) + static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) { if (!ctx->bcfi_enabled) {
@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a) static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; }
@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_64BIT(ctx); REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; }
Signature · 1 line
2.34.1
Re: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode
Quoted 8 lines · Deepak Gupta
Commit
f06bfe3dc38c("target/riscv: implement zicfiss instructions") addsssamoswapinstruction.ssamoswaptakes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), thenssamoswapmust result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.
Quoted 4 lines
Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However,
ssamoswapcan still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior forssamoswap.
Section "22.2.3. Shadow Stack Memory Protection " of the latest priv spec [1] seems to say: "When the effective privilege mode is M, any memory access by an SSAMOSWAP.W/D
instruction will result in a store/AMO access-fault exception."
1: https://github.com/riscv/riscv-isa-manual/releases/tag/riscv-isa-release-9cfaf37-2025-03-06
Alistair
Quoted 51 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")Reported-by Ved Shanbhogue <[email protected]>Signed-off-by Deepak Gupta <[email protected]>--- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)target/riscv/insn_trans/trans_rvzicfiss.c.inc +11
-3@@ -15,6 +15,13 @@ * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ + + #define REQUIRE_ZICFISS(ctx) do { \ + if (!ctx->cfg_ptr->ext_zicfiss) { \ + return false; \ + } \ +} while (0) + static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) { if (!ctx->bcfi_enabled) {@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a) static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; }@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_64BIT(ctx); REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; } --2.34.1
Re: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode
Quoted 14 lines · Alistair Francis
Quoted 8 lines · Deepak Gupta
Commit
f06bfe3dc38c("target/riscv: implement zicfiss instructions") addsssamoswapinstruction.ssamoswaptakes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), thenssamoswapmust result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.Quoted 4 lines
Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However,
ssamoswapcan still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior forssamoswap.
Quoted 4 lines
Section "22.2.3. Shadow Stack Memory Protection " of the latest priv spec [1] seems to say: "When the effective privilege mode is M, any memory access by an SSAMOSWAP.W/D
instruction will result in a store/AMO access-fault exception."
Hmm I didn't look at priv spec. Let me fix this one.
Quoted 56 lines
1: https://github.com/riscv/riscv-isa-manual/releases/tag/riscv-isa-release-9cfaf37-2025-03-06
Alistair
Quoted 51 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")Reported-by Ved Shanbhogue <[email protected]>Signed-off-by Deepak Gupta <[email protected]>--- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)target/riscv/insn_trans/trans_rvzicfiss.c.inc +11
-3@@ -15,6 +15,13 @@ * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ + + #define REQUIRE_ZICFISS(ctx) do { \ + if (!ctx->cfg_ptr->ext_zicfiss) { \ + return false; \ + } \ +} while (0) + static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) { if (!ctx->bcfi_enabled) {@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a) static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; }@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_64BIT(ctx); REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; } --2.34.1
Re: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode
Quoted 16 lines · Deepak Gupta
Quoted 14 lines · Alistair Francis
Quoted 12 lines · Deepak Gupta
Commit
f06bfe3dc38c("target/riscv: implement zicfiss instructions") addsssamoswapinstruction.ssamoswaptakes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), thenssamoswapmust result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However,
ssamoswapcan still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior forssamoswap.
Quoted 6 lines
Quoted 4 lines
Section "22.2.3. Shadow Stack Memory Protection " of the latest priv spec [1] seems to say: "When the effective privilege mode is M, any memory access by an SSAMOSWAP.W/D
instruction will result in a store/AMO access-fault exception."Hmm I didn't look at priv spec. Let me fix this one.
I hope the two don't conflict, that will be a nightmare
Alistair
Quoted 57 lines
Quoted 57 lines
1: https://github.com/riscv/riscv-isa-manual/releases/tag/riscv-isa-release-9cfaf37-2025-03-06
Alistair
Quoted 51 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")Reported-by Ved Shanbhogue <[email protected]>Signed-off-by Deepak Gupta <[email protected]>--- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)target/riscv/insn_trans/trans_rvzicfiss.c.inc +11
-3@@ -15,6 +15,13 @@ * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ + + #define REQUIRE_ZICFISS(ctx) do { \ + if (!ctx->cfg_ptr->ext_zicfiss) { \ + return false; \ + } \ +} while (0) + static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) { if (!ctx->bcfi_enabled) {@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a) static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; }@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_64BIT(ctx); REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; } --2.34.1
Re: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode
Quoted 17 lines · Alistair Francis
Quoted 16 lines · Deepak Gupta
Quoted 14 lines · Alistair Francis
Quoted 12 lines · Deepak Gupta
Commit
f06bfe3dc38c("target/riscv: implement zicfiss instructions") addsssamoswapinstruction.ssamoswaptakes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), thenssamoswapmust result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However,
ssamoswapcan still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior forssamoswap.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Section "22.2.3. Shadow Stack Memory Protection " of the latest priv spec [1] seems to say: "When the effective privilege mode is M, any memory access by an SSAMOSWAP.W/D
instruction will result in a store/AMO access-fault exception."Hmm I didn't look at priv spec. Let me fix this one.
I hope the two don't conflict, that will be a nightmare
No they don't conflict. Last "else" block below basically means that it should be store/AMO access fault because there is no shadow stack page.
"""
if privilege_mode != M && menvcfg.SSE == 0
raise illegal-instruction exception
if S-mode not implemented
raise illegal-instruction exception
else if privilege_mode == U && senvcfg.SSE == 0
raise illegal-instruction exception
else if privilege_mode == VS && henvcfg.SSE == 0
raise virtual instruction exception
else if privilege_mode == VU && senvcfg.SSE == 0
raise virtual instruction exception
else
temp[31:0] = mem[X(rs1)] X(rd) = SignExtend(temp[31:0]) mem[X(rs1)] = X(rs2)[31:0]
endif
"""
Quoted 60 lines
Alistair
Quoted 57 lines
Quoted 57 lines
1: https://github.com/riscv/riscv-isa-manual/releases/tag/riscv-isa-release-9cfaf37-2025-03-06
Alistair
Quoted 51 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")Reported-by Ved Shanbhogue <[email protected]>Signed-off-by Deepak Gupta <[email protected]>--- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)target/riscv/insn_trans/trans_rvzicfiss.c.inc +11
-3@@ -15,6 +15,13 @@ * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ + + #define REQUIRE_ZICFISS(ctx) do { \ + if (!ctx->cfg_ptr->ext_zicfiss) { \ + return false; \ + } \ +} while (0) + static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) { if (!ctx->bcfi_enabled) {@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a) static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; }@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_64BIT(ctx); REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; } --2.34.1
Quoted 5 lines · Deepak Gupta
Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced
CSR_SSPbut it mis-interpreted the spec on access toCSR_SSPin M-mode. Gated toCSR_SSPis not gated viaxSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer"
Do you mean "22.2.1. Shadow Stack Pointer (ssp) CSR access contr" in the priv spec?
Quoted 2 lines
of
zicfissspecification. Thanks to Adam Zabrocki for bringing this to attention.
The thanks should probably be below the line
Quoted 6 lines
Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controlsfor zicfiss"
Reported-by Adam Zabrocki <[email protected]>Signed-off-by Deepak Gupta <[email protected]>
The actual change looks good:
Alistair
Quoted 24 lines
--- target/riscv/csr.c | 5 +++++ 1 file changed, 5 insertions(+)target/riscv/csr.c +5
-1@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +/* If ext implemented, M-mode always have access to SSP CSR */ +if (env->priv ==PRV_M ) { +return RISCV_EXCP_NONE ; + } +/* if bcfi not active for current env, access to csr is illegal */ if (!cpu_get_bcfien (env)) {#if !defined(CONFIG_USER_ONLY ) --2.34.1
Quoted 9 lines · Alistair Francis
Quoted 5 lines · Deepak Gupta
Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced
CSR_SSPbut it mis-interpreted the spec on access toCSR_SSPin M-mode. Gated toCSR_SSPis not gated viaxSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer"Do you mean "22.2.1. Shadow Stack Pointer (ssp) CSR access contr" in the priv spec?
No I meant 2.2.4 of zicfiss specification. Section 22.2.1 of priv spec says same.
Quoted 5 lines
Quoted 3 lines
of
zicfissspecification. Thanks to Adam Zabrocki for bringing this to attention.The thanks should probably be below the line
Sure
Quoted 38 lines
Quoted 7 lines
Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controlsfor zicfiss"
Reported-by Adam Zabrocki <[email protected]>Signed-off-by Deepak Gupta <[email protected]>The actual change looks good:
Reviewed-by Alistair Francis <[email protected]>Alistair
Quoted 24 lines
--- target/riscv/csr.c | 5 +++++ 1 file changed, 5 insertions(+)target/riscv/csr.c +5
-1@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +/* If ext implemented, M-mode always have access to SSP CSR */ +if (env->priv ==PRV_M ) { +return RISCV_EXCP_NONE ; + } +/* if bcfi not active for current env, access to csr is illegal */ if (!cpu_get_bcfien (env)) {#if !defined(CONFIG_USER_ONLY ) --2.34.1
Quoted 9 lines · Deepak Gupta
Quoted 7 lines · Alistair Francis
Quoted 5 lines · Deepak Gupta
Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced
CSR_SSPbut it mis-interpreted the spec on access toCSR_SSPin M-mode. Gated toCSR_SSPis not gated viaxSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer"
Quoted 5 lines
Quoted 2 lines
Do you mean "22.2.1. Shadow Stack Pointer (ssp) CSR access contr" in the priv spec?
No I meant 2.2.4 of zicfiss specification. Section 22.2.1 of priv spec says same.
I meant that it's now just in the priv spec, the zicfiss spec is no longer maintained so we should just reference the priv spec
Alistair
Quoted 47 lines
Quoted 6 lines
Quoted 4 lines
of
zicfissspecification. Thanks to Adam Zabrocki for bringing this to attention.The thanks should probably be below the line
Sure
Quoted 38 lines
Quoted 7 lines
Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controlsfor zicfiss"
Reported-by Adam Zabrocki <[email protected]>Signed-off-by Deepak Gupta <[email protected]>The actual change looks good:
Reviewed-by Alistair Francis <[email protected]>Alistair
Quoted 24 lines
--- target/riscv/csr.c | 5 +++++ 1 file changed, 5 insertions(+)target/riscv/csr.c +5
-1@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +/* If ext implemented, M-mode always have access to SSP CSR */ +if (env->priv ==PRV_M ) { +return RISCV_EXCP_NONE ; + } +/* if bcfi not active for current env, access to csr is illegal */ if (!cpu_get_bcfien (env)) {#if !defined(CONFIG_USER_ONLY ) --2.34.1
Quoted 10 lines · Alistair Francis
Quoted 9 lines · Deepak Gupta
Quoted 7 lines · Alistair Francis
Quoted 5 lines · Deepak Gupta
Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced
CSR_SSPbut it mis-interpreted the spec on access toCSR_SSPin M-mode. Gated toCSR_SSPis not gated viaxSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer"
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Do you mean "22.2.1. Shadow Stack Pointer (ssp) CSR access contr" in the priv spec?
No I meant 2.2.4 of zicfiss specification. Section 22.2.1 of priv spec says same.
I meant that it's now just in the priv spec, the zicfiss spec is no longer maintained so we should just reference the priv spec
Got it.
Quoted 50 lines
Alistair
Quoted 47 lines
Quoted 6 lines
Quoted 4 lines
of
zicfissspecification. Thanks to Adam Zabrocki for bringing this to attention.The thanks should probably be below the line
Sure
Quoted 38 lines
Quoted 7 lines
Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controlsfor zicfiss"
Reported-by Adam Zabrocki <[email protected]>Signed-off-by Deepak Gupta <[email protected]>The actual change looks good:
Reviewed-by Alistair Francis <[email protected]>Alistair
Quoted 24 lines
--- target/riscv/csr.c | 5 +++++ 1 file changed, 5 insertions(+)target/riscv/csr.c +5
-1@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +/* If ext implemented, M-mode always have access to SSP CSR */ +if (env->priv ==PRV_M ) { +return RISCV_EXCP_NONE ; + } +/* if bcfi not active for current env, access to csr is illegal */ if (!cpu_get_bcfien (env)) {#if !defined(CONFIG_USER_ONLY ) --2.34.1