Skip to content

Commit c1f70bf

Browse files
committed
fix(entropy): handle 0 size buffers from guest
In theory guests should not pass us 0 size buffers, but if they do right now we crash. Instead handle the case by adding back the buffer as used, having touched 0 bytes. Also add a unit-test for this case. Signed-off-by: Babis Chalios <[email protected]>
1 parent 0cd455f commit c1f70bf

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/vmm/src/devices/virtio/rng/device.rs

+8
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ impl Entropy {
103103
}
104104

105105
fn handle_one(&self, iovec: &mut IoVecBufferMut) -> Result<u32> {
106+
// If guest provided us with an empty buffer just return directly
107+
if iovec.len() == 0 {
108+
return Ok(0);
109+
}
110+
106111
let mut rand_bytes = vec![0; iovec.len()];
107112
rand::fill(&mut rand_bytes).map_err(|err| {
108113
METRICS.entropy.host_rng_fails.inc();
@@ -412,6 +417,9 @@ mod tests {
412417
// Add a write-only descriptor with 10 bytes
413418
th.add_desc_chain(RNG_QUEUE, 0, &[(1, 10, VIRTQ_DESC_F_WRITE)]);
414419

420+
// Add a write-only descriptor with 0 bytes. This should not fail.
421+
th.add_desc_chain(RNG_QUEUE, 0, &[(2, 0, VIRTQ_DESC_F_WRITE)]);
422+
415423
let mut entropy_dev = th.device();
416424

417425
// This should succeed, we just added two descriptors

0 commit comments

Comments
 (0)