target/riscv/kvm/kvm-cpu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
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.
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 <fritchleybohrer@gmail.com>
---
target/riscv/kvm/kvm-cpu.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
index 5beb602338..a301b584da 100644
--- a/target/riscv/kvm/kvm-cpu.c
+++ b/target/riscv/kvm/kvm-cpu.c
@@ -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;
--
2.43.0
On 11/10/2026 10:57 PM, Zephyr Li wrote:
> 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.
>
> 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 <fritchleybohrer@gmail.com>
> ---
Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
> target/riscv/kvm/kvm-cpu.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
> index 5beb602338..a301b584da 100644
> --- a/target/riscv/kvm/kvm-cpu.c
> +++ b/target/riscv/kvm/kvm-cpu.c
> @@ -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;
© 2016 - 2026 Red Hat, Inc.