target/riscv/kvm: Limit SBI DBCN transfers to 4 KiB
PATCH 1/1 target/riscv/kvm: Limit SBI DBCN transfers to 4 KiB
DBCN console read and write use a guest-provided byte count directly as the size of a g_malloc0() allocation. An oversized request can therefore make QEMU allocate excessive host memory or terminate on allocation failure.
Cap each bulk transfer at 4 KiB before allocating the buffer or calling the character backend. DBCN permits partial transfers, so the existing return path reports the actual progress and the caller can retry the remainder. Zero-length requests and WRITE_BYTE are unchanged.
Choose 4 KiB as a bounded transfer size consistent with QEMU's memory allocation guidelines for small allocations. Linux's DBCN read and write helpers already limit each SBI request to the remainder of the current page. For guests using 4 KiB base pages, this cap accommodates the largest request issued by these helpers without additional size-based truncation, while keeping the backend length within its int range.
The 4 KiB cap is a QEMU implementation limit, not an SBI-defined request-size restriction or a dependency on the host or guest page size.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4431
--- target/riscv/kvm/kvm-cpu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
target/riscv/kvm/kvm-cpu.c +5 -1
@@ -1699,7 +1699,11 @@ static void kvm_riscv_handle_sbi_dbcn(CPUState *cs, struct kvm_run *run)switch (run->riscv_sbi.function_id) {case SBI_EXT_DBCN_CONSOLE_READ :case SBI_EXT_DBCN_CONSOLE_WRITE : - num_bytes = run->riscv_sbi.args[0 ]; +/* +* DBCN allows partial transfers. Limit each call to 4 KiB so the +* guest cannot request an unbounded host allocation. +*/ + num_bytes =MIN (run->riscv_sbi.args[0 ],4096 );if (num_bytes ==0 ) { run->riscv_sbi.ret[0 ] =SBI_SUCCESS ;
Signature · 1 line
2.43.0
Quoted 18 lines · Zephyr Li
DBCN console read and write use a guest-provided byte count directly as the size of a
g_malloc0()allocation. An oversized request can therefore make QEMU allocate excessive host memory or terminate on allocation failure.Cap each bulk transfer at 4 KiB before allocating the buffer or calling the character backend. DBCN permits partial transfers, so the existing return path reports the actual progress and the caller can retry the remainder. Zero-length requests and
WRITE_BYTEare unchanged.Choose 4 KiB as a bounded transfer size consistent with QEMU's memory allocation guidelines for small allocations. Linux's DBCN read and write helpers already limit each SBI request to the remainder of the current page. For guests using 4 KiB base pages, this cap accommodates the largest request issued by these helpers without additional size-based truncation, while keeping the backend length within its int range.
Quoted 7 lines
The 4 KiB cap is a QEMU implementation limit, not an SBI-defined request-size restriction or a dependency on the host or guest page size.
Fixes a6b53378f537 ("target/riscv/kvm: implement SBI debug console (DBCN) calls")Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4431
Signed-off-by Zephyr Li <[email protected]>---
Quoted 20 lines
target/riscv/kvm/kvm-cpu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)target/riscv/kvm/kvm-cpu.c +5
-1@@ -1699,7 +1699,11 @@ static void kvm_riscv_handle_sbi_dbcn(CPUState *cs, struct kvm_run *run)switch (run->riscv_sbi.function_id) {case SBI_EXT_DBCN_CONSOLE_READ :case SBI_EXT_DBCN_CONSOLE_WRITE : - num_bytes = run->riscv_sbi.args[0 ]; +/* +* DBCN allows partial transfers. Limit each call to 4 KiB so the +* guest cannot request an unbounded host allocation. +*/ + num_bytes =MIN (run->riscv_sbi.args[0 ],4096 );if (num_bytes ==0 ) { run->riscv_sbi.ret[0 ] =SBI_SUCCESS ;