lib: Missing architectural CSR preservation during domain context switches
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- c
- Lĩnh vực
- operating-systems
Hướng nghiên cứu
Start with switch_to_next_domain_context() and the hart_context structure in lib/sbi/sbi_domain_context.c, then inspect how extension checks and context initialization are handled nearby. Determine which listed CSRs are writable, aliased, indirect, or hardware-maintained before adding preservation. Done means supported, independently owned CSR state survives domain switches and the htimedelta, hcounteren, and stimecmp context-switch cases pass.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
lib/sbi/sbi_domain_context.c:switch_to_next_domain_context() does not save and restore several architectural CSRs when switching between S/HS-mode domains.
The current implementation preserves several supervisor CSRs, including sstatus, sie, stvec, satp, scounteren, and senvcfg.
However, I found 30 additional CSR names that are not handled by the domain context switching implementation.
These registers include hypervisor state, interrupt configuration, counter configuration, control transfer recording state, indirect CSR state, and timer state.
I also observed CSR preservation failures when switching between two domain contexts.
Affected registers
The following registers are missing from the current save/restore implementation.
Hypervisor CSRs
hgatp
hcounteren
hedeleg
henvcfg
hgeie
hideleg
hie
hip
hstateen0
hstateen1
hstateen2
hstateen3
hstatus
htimedelta
htinst
htval
hvien
hvip
Supervisor counter configuration
scountinhibit
Control Transfer Records (CTR)
sctrctl
sctrdepth
sctrstatus
Supervisor indirect CSRs
siselect
sireg
sireg2
sireg3
sireg4
sireg5
sireg6
Supervisor timer
stimecmp
These 30 CSR names are not explicitly saved or restored in switch_to_next_domain_context().
Not every register necessarily requires an independent context field. Some registers are aliases, contain hardware-generated state, or depend on optional extensions. Their appropriate preservation mechanisms may differ.
Test case: htimedelta
The htimedelta register controls the time offset used by a virtual machine.
The test checks whether OpenSBI restores the original htimedelta value when returning to a previous domain context.
# 240 ### Switch to S-mode Context A
li a0, 1
csrw htimedelta, a0
### Switch to S-mode Context B
li a0, 0
csrw htimedelta, a0
### Switch to S-mode Context A
csrr a0, htimedelta
### Result: a0 = 1
Expected result
a0 == 1
Observed result
ITER 0240 | FAIL | expected=0x1 observed=0x0
EXPECTED RESULT: a0 == 1
OBSERVED RESULT: a0 = 0x0
Context A writes 1 to htimedelta.
Context B writes 0 to the same CSR.
After switching back to Context A, the register contains 0 instead of 1.
The result is consistent with the missing htimedelta save/restore implementation.
Test case: hcounteren
The hcounteren register controls guest access to hardware counters.
# 179 ### Switch to S-mode Context A
li a0, 1
csrw hcounteren, a0
### Switch to S-mode Context B
li a0, 0
csrw hcounteren, a0
### Switch to S-mode Context A
csrr a0, hcounteren
### Result: a0 = 1
Expected result
a0 == 1
Observed result
ITER 0179 | FAIL | expected=0x1 observed=0x0
EXPECTED RESULT: a0 == 1
OBSERVED RESULT: a0 = 0x0
The original value was not observed after returning to Context A.
Since some hcounteren bits can be read-only zero, the writable bits should be confirmed on the tested platform before attributing the entire failure to context switching.
However, the source code independently confirms that OpenSBI does not explicitly preserve hcounteren in this function.
Test case: stimecmp
The stimecmp register controls supervisor timer interrupts when the Sstc extension is available.
# 305 ### Switch to S-mode Context A
li a0, 1
csrw stimecmp, a0
### Switch to S-mode Context B
li a0, 0
csrw stimecmp, a0
### Switch to S-mode Context A
csrr a0, stimecmp
### Result: a0 = 1
Expected result
a0 == 1
Observed result
ITER 0305 | FAIL | expected=0x1 observed=0x0
EXPECTED RESULT: a0 == 1
OBSERVED RESULT: a0 = 0x0
Context A writes 1 to stimecmp.
Context B writes 0.
After switching back to Context A, the register contains 0 instead of 1.
The source code also does not contain a stimecmp save/restore operation.
A follow-up test using future timer deadlines would help exclude interference from an immediately pending timer interrupt.
Affected code
File:
lib/sbi/sbi_domain_context.c
The current hart_context structure contains the following supervisor state:
struct hart_context {
struct sbi_trap_context trap_ctx;
unsigned long sstatus;
unsigned long sie;
unsigned long stvec;
unsigned long sscratch;
unsigned long sepc;
unsigned long scause;
unsigned long stval;
unsigned long sip;
unsigned long satp;
unsigned long scounteren;
unsigned long senvcfg;
unsigned long srmcfg;
struct sbi_fp_context fp_ctx;
struct sbi_vector_context *vec_ctx;
struct sbi_domain *dom;
struct hart_context *prev_ctx;
bool initialized;
};
The structure does not contain fields for the 30 additional CSRs listed above.
The context switching function currently saves and restores:
ctx->sstatus = csr_swap(CSR_SSTATUS, dom_ctx->sstatus);
ctx->sie = csr_swap(CSR_SIE, dom_ctx->sie);
ctx->stvec = csr_swap(CSR_STVEC, dom_ctx->stvec);
ctx->sscratch = csr_swap(CSR_SSCRATCH, dom_ctx->sscratch);
ctx->sepc = csr_swap(CSR_SEPC, dom_ctx->sepc);
ctx->scause = csr_swap(CSR_SCAUSE, dom_ctx->scause);
ctx->stval = csr_swap(CSR_STVAL, dom_ctx->stval);
ctx->sip = csr_swap(CSR_SIP, dom_ctx->sip);
ctx->satp = csr_swap(CSR_SATP, dom_ctx->satp);
if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_10)
ctx->scounteren = csr_swap(CSR_SCOUNTEREN, dom_ctx->scounteren);
if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_12)
ctx->senvcfg = csr_swap(CSR_SENVCFG, dom_ctx->senvcfg);
if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSQOSID))
ctx->srmcfg = csr_swap(CSR_SRMCFG, dom_ctx->srmcfg);
The implementation also provides floating-point and Vector context switching.
However, it does not have corresponding operations for the missing hypervisor, timer, counter, CTR, and indirect CSR state.
Expected behavior
When switching between independent S/HS-mode domains, the architectural state owned by each domain should be preserved.
For example:
- Domain A configures its hypervisor CSRs.
- OpenSBI switches to Domain B.
- Domain B uses its own CSR configuration.
- OpenSBI switches back to Domain A.
- Domain A should recover its original architectural state.
The exact state that must be preserved depends on the supported extensions and the domain context switching contract.
For registers that contain hardware-generated state or act as aliases, OpenSBI may need a mechanism other than directly saving and restoring the CSR value.
Actual behavior
The current implementation does not explicitly preserve the 30 listed CSR names.
The tests also show that some register values are not recovered after switching between domains.
Consequently, a resumed domain may observe state left by another domain, or state modified by the platform while another domain was executing.
This can lead to incorrect architectural behavior and incomplete isolation between domain contexts.
Proposed fix
I suggest extending the domain context switching implementation to preserve the missing architectural state when the corresponding extensions are available.
The implementation should distinguish ordinary writable CSRs from indirect, aliased, and hardware-generated state.
Hypervisor CSRs
For ordinary writable hypervisor CSRs, one possible approach is to add fields to hart_context.
For example:
unsigned long hstatus;
unsigned long hedeleg;
unsigned long hideleg;
unsigned long hie;
unsigned long hcounteren;
unsigned long henvcfg;
unsigned long hgatp;
u64 htimedelta;
Then add save and restore operations for supported H-extension state.
For example:
if (misa_extension('H')) {
ctx->hstatus =
csr_swap(CSR_HSTATUS, dom_ctx->hstatus);
ctx->hcounteren =
csr_swap(CSR_HCOUNTEREN, dom_ctx->hcounteren);
ctx->htimedelta =
csr_swap64(CSR_HTIMEDELTA, dom_ctx->htimedelta);
}
This is only an example of the proposed implementation.
The remaining hypervisor CSRs should also be considered, including:
hedeleg
henvcfg
hgeie
hideleg
hie
hip
hstateen0
hstateen1
hstateen2
hstateen3
hgatp
htinst
htval
hvien
hvip
Additional considerations include:
hgatpmust be handled with valid translation settings and appropriate HFENCE synchronization.hipcontains hardware-derived pending interrupt state. Its writable and read-only fields must be handled correctly.hvipcontains virtual interrupt pending state and may interact withhip.hgeiedepends on the guest external interrupt resources available on the platform.hstateen0tohstateen3require the corresponding state-enable extension.hviendepends on optional interrupt architecture support.- Trap-related registers such as
htinstandhtvalshould be preserved consistently with the trap context when required.
The final implementation should also handle WARL fields and initialize state for newly created domains.
Supervisor timer
For the Sstc extension, add a separate timer comparison value:
u64 stimecmp;
Then save and restore it when the extension is available:
if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSTC))
ctx->stimecmp =
csr_swap64(CSR_STIMECMP, dom_ctx->stimecmp);
A newly initialized context should use a suitable initial deadline, such as a maximum comparison value, until the domain programs its timer.
The implementation should account for OpenSBI's existing timer management.
Supervisor counter configuration
The scountinhibit CSR requires special handling because it is a masked alias of mcountinhibit.
It should not automatically be treated as an independent counter state register.
If counters can be delegated to supervisor domains, the context switching implementation should preserve or enforce the appropriate counter inhibition policy without interfering with shared machine-level counter management.
Control Transfer Records
For systems supporting the Smctr extension, the following state should be considered:
sctrctl
sctrdepth
sctrstatus
The implementation should determine which fields are writable and which represent hardware-maintained state.
CTR also uses a recording buffer. Preserving only the control CSRs may not be sufficient to isolate the complete CTR state between domains.
Depending on the supported implementation, the fix may require separate CTR configuration and buffer management.
Supervisor indirect CSRs
The following CSRs provide indirect access to architectural state:
siselect
sireg
sireg2
sireg3
sireg4
sireg5
sireg6
The siselect register selects the indirectly accessed state.
The sireg* registers are aliases whose behavior depends on the selected index and implemented extension.
Therefore, saving the values of sireg* once is not sufficient to preserve all indirectly accessible state.
A complete solution should preserve the relevant selector and underlying state for each supported indirect CSR range.
This is particularly important when indirectly accessed state controls interrupts or memory protection.
Impact
Missing architectural-state preservation can affect reliability and isolation between supervisor domains.
Hypervisor state
Incorrect hypervisor CSR restoration may affect:
- Guest address translation.
- Guest exception delegation.
- Guest interrupt configuration.
- Guest counter permissions.
- Virtual time offsets.
- Guest state access permissions.
Timer state
Incorrect stimecmp restoration may cause unexpected or missing supervisor timer interrupts.
Counter state
Incorrect counter configuration may affect performance monitoring and delegated counter control.
Control Transfer Records
Incorrect CTR state isolation may allow one domain to observe records associated with another domain or interfere with its recording configuration, if the feature is supported and accessible.
Indirect CSR state
Incorrectly restored indirect CSR state may affect the features accessed through the selected register bank.
Some of these registers control security-relevant behavior. Missing preservation may therefore introduce confidentiality or integrity risks depending on how independent domains are used.
The current report identifies missing context handling and includes runtime preservation failures. It does not claim that all 30 CSRs have individually demonstrated exploitable security vulnerabilities.
Environment
- Project: OpenSBI
- Architecture: RISC-V RV64
- QEMU version available: 11.0.50 (v11.0.0-1713-gde5d8bfd61)
- Compiler version available: riscv64-unknown-elf-gcc 15.1.0
- Context switching: S/HS-mode Domain A → Domain B → Domain A
Questions
Could you please confirm whether sbi_domain_context_enter() and sbi_domain_context_exit() are expected to preserve the architectural state of independent S/HS-mode domains, including supported hypervisor and optional extension state?
If so, I believe the listed registers should be covered by the context switching implementation, either through direct CSR preservation or another appropriate mechanism.
I would also appreciate a review of the potential security implications of the missing state isolation, including CVE consideration if a security vulnerability is confirmed.
Thank you.
- Ngôn ngữ chính
- C
- Star
- 1.6k
- Fork
- 734
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của riscv-software-src/opensbi
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
riscv-software-src/opensbi#425 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
riscv-software-src/opensbi#424 · 1 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
riscv-software-src/opensbi#439 ·
-
sbi_trap_redirect() does not clear SPELP when the interrupted context had no landing pad expectedĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 67/100
riscv-software-src/opensbi#438 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
riscv-software-src/opensbi#436 · 3 bình luận ·
Tất cả issue của riscv-software-src/opensbi
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
OpenTenBase/OpenTenBase#557 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Claude:NeedsReview GPT:ConfirmedBug GPT:NeedsReview triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
open5gs/open5gs#5141 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonCó thể đã có người làm @ronaldtse đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
-
Typos and grammar issuesĐang mởbug good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
TanvirHasanAkash/CSE114-Lab-Project-72_B2_G2#37 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
issue: bug report
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 2 ngày