Skip to contentSkip to conversation

lib: sbi: expected trap must always clear MPRV

PATCH Applied Deepak Gupta 3 participants 9 messages Nov 24, 2025 – Dec 26, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi: expected trap must always clear MPRV

Expected trap must always clear MPRV. Currently it doesn't. There is a security issue here where if firmware was doing ld/st with MPRV=1 and since there would be a expected trap, opensbi will continue to run as MPRV=1. Security impact is DoS where opensbi will just keep trapping.

---
 lib/sbi/sbi_expected_trap.S | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/sbi_expected_trap.S +4 -0
index 99dede5f..8fbd2cb2 100644
@@ -23,6 +23,8 @@
 	.global __sbi_expected_trap
 __sbi_expected_trap:
 	/* Without H-extension so, MTVAL2 and MTINST CSRs and GVA not available */
+	lui	a4, 0x20 /* MSTATUS_MPRV */
+	csrc	CSR_MSTATUS, a4
 	csrr	a4, CSR_MCAUSE
 	REG_S	a4, SBI_TRAP_INFO_OFFSET(cause)(a3)
 	csrr	a4, CSR_MTVAL
@@ -39,6 +41,8 @@ __sbi_expected_trap:
 	.global __sbi_expected_trap_hext
 __sbi_expected_trap_hext:
 	/* With H-extension so, MTVAL2 and MTINST CSRs and GVA available */
+	lui	a4, 0x20 /* MSTATUS_MPRV */
+	csrc	CSR_MSTATUS, a4
 	csrr	a4, CSR_MCAUSE
 	REG_S	a4, SBI_TRAP_INFO_OFFSET(cause)(a3)
 	csrr	a4, CSR_MTVAL
Signature · 7 lines
2.43.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw
2025-11-24T14:03:39-08:00, Deepak Gupta <[email protected]>:
Quoted 4 lines

Expected trap must always clear MPRV. Currently it doesn't. There is a security issue here where if firmware was doing ld/st with MPRV=1 and since there would be a expected trap, opensbi will continue to run as MPRV=1. Security impact is DoS where opensbi will just keep trapping.

Does the DoS happen on some implementation?

The expected trap came from M-mode, therefore will have mstatus.MPP=3, so MPRV=1 should behave the same as MPRV=0.

Thanks.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw
Quoted 7 lines · Radim Krčmář
2025-11-24T14:03:39-08:00, Deepak Gupta <[email protected]>:
Quoted 4 lines

Expected trap must always clear MPRV. Currently it doesn't. There is a security issue here where if firmware was doing ld/st with MPRV=1 and since there would be a expected trap, opensbi will continue to run as MPRV=1. Security impact is DoS where opensbi will just keep trapping.

Does the DoS happen on some implementation?

I ran into it while doing something else. So it was result of basically eyeballing. Didn't observe on real system.

Quoted 3 lines

The expected trap came from M-mode, therefore will have mstatus.MPP=3, so MPRV=1 should behave the same as MPRV=0.

Yeah I missed that part. You have a point here.

However if we read priv spec
"21.4.1. Machine Status (mstatus and mstatush) Registers"

...
The MPV bit (Machine Previous Virtualization Mode) is written by the implementation whenever a trap is taken into M-mode. Just as the MPP field is set to the (nominal) privilege mode at the time of the trap,
...

Above text seems to suggest that nominal privilege at time of trap is set in MPP.

And then just a few paragraph below if we read,

...
When MPRV=1, explicit memory accesses are translated and protected, and endianness is applied, as though the current virtualization mode were set to MPV and the current nominal privilege mode were set to MPP
...

So if take them together, it seems like nominal priv at time trap can be less than 3 and same should reflect in MPP if it gets trapped.

I don't know what implementations are doing. Should ask around.

Quoted 2 lines

Thanks.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#3 lore raw
2025-11-25T10:03:12-08:00, Deepak Gupta <[email protected]>:
Quoted 30 lines · Radim Krčmář
Quoted 7 lines
2025-11-24T14:03:39-08:00, Deepak Gupta <[email protected]>:
Quoted 4 lines

Expected trap must always clear MPRV. Currently it doesn't. There is a security issue here where if firmware was doing ld/st with MPRV=1 and since there would be a expected trap, opensbi will continue to run as MPRV=1. Security impact is DoS where opensbi will just keep trapping.

Does the DoS happen on some implementation?

I ran into it while doing something else. So it was result of basically eyeballing. Didn't observe on real system.

Quoted 3 lines

The expected trap came from M-mode, therefore will have mstatus.MPP=3, so MPRV=1 should behave the same as MPRV=0.

Yeah I missed that part. You have a point here.

However if we read priv spec
"21.4.1. Machine Status (mstatus and mstatush) Registers"

...
The MPV bit (Machine Previous Virtualization Mode) is written by the implementation whenever a trap is taken into M-mode. Just as the MPP field is set to the (nominal) privilege mode at the time of the trap,
...

Above text seems to suggest that nominal privilege at time of trap is set in MPP.

Quoted 7 lines

And then just a few paragraph below if we read,

...
When MPRV=1, explicit memory accesses are translated and protected, and endianness is applied, as though the current virtualization mode were set to MPV and the current nominal privilege mode were set to MPP
...

I think that MPRV doesn't change the nominal privilege mode. MPRV just modifies explicit memory accesses to behave "as through" the nominal privilege mode was MPP.

e.g. load instruction fetched with M-mode implicit access (nominal privilege) performs non-M-mode explicit load (effective privilege).

(The architecture would be broken otherwise.)

Quoted 2 lines

So if take them together, it seems like nominal priv at time trap can be less than 3 and same should reflect in MPP if it gets trapped.

non-M nominal privilege mode can only be reached via mret/sret, and mret/sret clear MPRV, so there should be no way to enter a trap handler with MPRV == 1 && MPP != 3.

(The expected trap handler should only be configured during opensbi's
M-mode execution anyway.)

Quoted 1 line

I don't know what implementations are doing. Should ask around.

I wouldn't be surprised to find bugs in those corner cases...

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw
Quoted 39 lines · Radim Krčmář
2025-11-25T10:03:12-08:00, Deepak Gupta <[email protected]>:
Quoted 30 lines · Radim Krčmář
Quoted 7 lines
2025-11-24T14:03:39-08:00, Deepak Gupta <[email protected]>:
Quoted 4 lines

Expected trap must always clear MPRV. Currently it doesn't. There is a security issue here where if firmware was doing ld/st with MPRV=1 and since there would be a expected trap, opensbi will continue to run as MPRV=1. Security impact is DoS where opensbi will just keep trapping.

Does the DoS happen on some implementation?

I ran into it while doing something else. So it was result of basically eyeballing. Didn't observe on real system.

Quoted 3 lines

The expected trap came from M-mode, therefore will have mstatus.MPP=3, so MPRV=1 should behave the same as MPRV=0.

Yeah I missed that part. You have a point here.

However if we read priv spec
"21.4.1. Machine Status (mstatus and mstatush) Registers"

...
The MPV bit (Machine Previous Virtualization Mode) is written by the implementation whenever a trap is taken into M-mode. Just as the MPP field is set to the (nominal) privilege mode at the time of the trap,
...

Above text seems to suggest that nominal privilege at time of trap is set in MPP.

Quoted 7 lines

And then just a few paragraph below if we read,

...
When MPRV=1, explicit memory accesses are translated and protected, and endianness is applied, as though the current virtualization mode were set to MPV and the current nominal privilege mode were set to MPP
...

Quoted 8 lines

I think that MPRV doesn't change the nominal privilege mode. MPRV just modifies explicit memory accesses to behave "as through" the nominal privilege mode was MPP.

e.g. load instruction fetched with M-mode implicit access (nominal privilege) performs non-M-mode explicit load (effective privilege).

(The architecture would be broken otherwise.)

Yeah I understand that's the desired behavior.
Although current patch is additional safety and that too in not very perf critical path.

Do you see any issue with additional safety part in the patch?
I can modify the commit message to remove security impact (that it seems like how implementations are implementing it) and re-send it.

Quoted 14 lines
Quoted 3 lines

So if take them together, it seems like nominal priv at time trap can be less than 3 and same should reflect in MPP if it gets trapped.

non-M nominal privilege mode can only be reached via mret/sret, and mret/sret clear MPRV, so there should be no way to enter a trap handler with MPRV == 1 && MPP != 3.

(The expected trap handler should only be configured during opensbi's M-mode execution anyway.)

Quoted 1 line

I don't know what implementations are doing. Should ask around.

I wouldn't be surprised to find bugs in those corner cases...

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#5 lore raw
2025-11-25T11:17:29-08:00, Deepak Gupta <[email protected]>:
Quoted 47 lines · Radim Krčmář
Quoted 46 lines
2025-11-25T10:03:12-08:00, Deepak Gupta <[email protected]>:
Quoted 30 lines · Radim Krčmář
Quoted 7 lines
2025-11-24T14:03:39-08:00, Deepak Gupta <[email protected]>:
Quoted 4 lines

Expected trap must always clear MPRV. Currently it doesn't. There is a security issue here where if firmware was doing ld/st with MPRV=1 and since there would be a expected trap, opensbi will continue to run as MPRV=1. Security impact is DoS where opensbi will just keep trapping.

Does the DoS happen on some implementation?

I ran into it while doing something else. So it was result of basically eyeballing. Didn't observe on real system.

Quoted 3 lines

The expected trap came from M-mode, therefore will have mstatus.MPP=3, so MPRV=1 should behave the same as MPRV=0.

Yeah I missed that part. You have a point here.

However if we read priv spec
"21.4.1. Machine Status (mstatus and mstatush) Registers"

...
The MPV bit (Machine Previous Virtualization Mode) is written by the implementation whenever a trap is taken into M-mode. Just as the MPP field is set to the (nominal) privilege mode at the time of the trap,
...

Above text seems to suggest that nominal privilege at time of trap is set in MPP.

Quoted 7 lines

And then just a few paragraph below if we read,

...
When MPRV=1, explicit memory accesses are translated and protected, and endianness is applied, as though the current virtualization mode were set to MPV and the current nominal privilege mode were set to MPP
...

I think that MPRV doesn't change the nominal privilege mode. MPRV just modifies explicit memory accesses to behave "as through" the nominal privilege mode was MPP.

e.g. load instruction fetched with M-mode implicit access (nominal privilege) performs non-M-mode explicit load (effective privilege).

Quoted 7 lines
Quoted 1 line

(The architecture would be broken otherwise.)

Yeah I understand that's the desired behavior.
Although current patch is additional safety and that too in not very perf critical path.

Do you see any issue with additional safety part in the patch?

All code is an issue, and I don't see a real benefit to balance it out, but I think it's acceptable if opensbi maintainers like the idea.

Quoted 2 lines

I can modify the commit message to remove security impact (that it seems like how implementations are implementing it) and re-send it.

That would help. The patch also uses a wrong bitmask for csrc:
MPRV is bit 17, but you're clearing bit 5, SPIE. (Isn't it possible to use MSTATUS_MPRV there?)

Thanks.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#6 lore raw
Quoted 50 lines · Radim Krčmář
2025-11-25T11:17:29-08:00, Deepak Gupta <[email protected]>:
Quoted 49 lines · Radim Krčmář
Quoted 47 lines
2025-11-25T10:03:12-08:00, Deepak Gupta <[email protected]>:
Quoted 30 lines · Radim Krčmář
Quoted 7 lines
2025-11-24T14:03:39-08:00, Deepak Gupta <[email protected]>:
Quoted 4 lines

Expected trap must always clear MPRV. Currently it doesn't. There is a security issue here where if firmware was doing ld/st with MPRV=1 and since there would be a expected trap, opensbi will continue to run as MPRV=1. Security impact is DoS where opensbi will just keep trapping.

Does the DoS happen on some implementation?

I ran into it while doing something else. So it was result of basically eyeballing. Didn't observe on real system.

Quoted 3 lines

The expected trap came from M-mode, therefore will have mstatus.MPP=3, so MPRV=1 should behave the same as MPRV=0.

Yeah I missed that part. You have a point here.

However if we read priv spec
"21.4.1. Machine Status (mstatus and mstatush) Registers"

...
The MPV bit (Machine Previous Virtualization Mode) is written by the implementation whenever a trap is taken into M-mode. Just as the MPP field is set to the (nominal) privilege mode at the time of the trap,
...

Above text seems to suggest that nominal privilege at time of trap is set in MPP.

Quoted 7 lines

And then just a few paragraph below if we read,

...
When MPRV=1, explicit memory accesses are translated and protected, and endianness is applied, as though the current virtualization mode were set to MPV and the current nominal privilege mode were set to MPP
...

I think that MPRV doesn't change the nominal privilege mode. MPRV just modifies explicit memory accesses to behave "as through" the nominal privilege mode was MPP.

e.g. load instruction fetched with M-mode implicit access (nominal privilege) performs non-M-mode explicit load (effective privilege).

(The architecture would be broken otherwise.)

Quoted 8 lines
Quoted 5 lines

Yeah I understand that's the desired behavior.
Although current patch is additional safety and that too in not very perf critical path.

Do you see any issue with additional safety part in the patch?

All code is an issue, and I don't see a real benefit to balance it out, but I think it's acceptable if opensbi maintainers like the idea.

I'll wait for Atish and Anup then.

Quoted 7 lines
Quoted 3 lines

I can modify the commit message to remove security impact (that it seems like how implementations are implementing it) and re-send it.

That would help. The patch also uses a wrong bitmask for csrc:
MPRV is bit 17, but you're clearing bit 5, SPIE. (Isn't it possible to use MSTATUS_MPRV there?)

It's lui (load upper immediate). I could just do li a4, MTSTATUS_PRV. Although its a larger immediate, so most likely assembler will split into two instructions or would replace it with lui.

Quoted 2 lines

Thanks.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#7 lore raw
2025-11-25T12:04:44-08:00, Deepak Gupta <[email protected]>:
Quoted 5 lines · Radim Krčmář
Quoted 4 lines
2025-11-25T11:17:29-08:00, Deepak Gupta <[email protected]>:
Quoted 2 lines

I can modify the commit message to remove security impact (that it seems like how implementations are implementing it) and re-send it.

Quoted 7 lines
Quoted 3 lines

That would help. The patch also uses a wrong bitmask for csrc:
MPRV is bit 17, but you're clearing bit 5, SPIE. (Isn't it possible to use MSTATUS_MPRV there?)

It's lui (load upper immediate). I could just do li a4, MTSTATUS_PRV. Although its a larger immediate, so most likely assembler will split into two instructions or would replace it with lui.

Sorry, I missed that. Yeah, assembler should translate it to just lui.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 1/1 Applied
#8 lore raw
In reply to Deepak Gupta
Quoted 16 lines · Deepak Gupta

Expected trap must always clear MPRV. Currently it doesn't. There is a security issue here where if firmware was doing ld/st with MPRV=1 and since there would be a expected trap, opensbi will continue to run as MPRV=1. Security impact is DoS where opensbi will just keep trapping.

---
 lib/sbi/sbi_expected_trap.S | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/sbi_expected_trap.S +0 -0
index 99dede5f..8fbd2cb2 100644

lib/sbi/sbi_expected_trap.S line 26

Quoted 4 lines
lib/sbi/sbi_expected_trap.S +1 -0
@@ -23,6 +23,8 @@
        .global __sbi_expected_trap
 __sbi_expected_trap:
        /* Without H-extension so, MTVAL2 and MTINST CSRs and GVA not available */
+       lui     a4, 0x20 /* MSTATUS_MPRV */

This should be "li a4, MSTATUS_MPRV". I will take care of it at the time of merging this patch.

lib/sbi/sbi_expected_trap.S __sbi_expected_trap: · line 44

Quoted 9 lines
lib/sbi/sbi_expected_trap.S +2 -0
+       csrc    CSR_MSTATUS, a4
        csrr    a4, CSR_MCAUSE
        REG_S   a4, SBI_TRAP_INFO_OFFSET(cause)(a3)
        csrr    a4, CSR_MTVAL
@@ -39,6 +41,8 @@ __sbi_expected_trap:
        .global __sbi_expected_trap_hext
 __sbi_expected_trap_hext:
        /* With H-extension so, MTVAL2 and MTINST CSRs and GVA available */
+       lui     a4, 0x20 /* MSTATUS_MPRV */

Same comment as above.

Quoted 8 lines
lib/sbi/sbi_expected_trap.S +1 -1
+       csrc    CSR_MSTATUS, a4
        csrr    a4, CSR_MCAUSE
        REG_S   a4, SBI_TRAP_INFO_OFFSET(cause)(a3)
        csrr    a4, CSR_MTVAL
--

2.43.0

Quoted 4 lines

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi