Skip to content

fix(virtio): round up blk device capacity and zero-pad unaligned tail… - #580

Merged
Lencerf merged 3 commits into
google:mainfrom
jacobgorm:alignfix
Dec 5, 2026
Merged

Lencerf merged 3 commits into
google:mainfrom
jacobgorm:alignfix

Conversation

@jacobgorm

Copy link
Copy Markdown
Contributor

… reads

A file-backed block device floored the file size to whole 512-byte sectors, making the tail bytes inaccessible from the VM.
Sometimes it can be useful to pass unaligned files as block devices to a VM, e.g, to be able to bootstrap a VM install from a .tar.gz file on the host.

Round the capacity up to whole sectors and serve reads that legitimately cross EOF with NUL padding, on both the Mio and io_uring workers; a read that starts beyond EOF still reports IOERR.

@coveralls

coveralls commented Dec 4, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 47580470799

Coverage increased (+0.9%) to 49.145%

Details

  • Coverage increased (+0.9%) from the base build.
  • Patch coverage: 9 uncovered changes across 1 file (52 of 61 lines covered, 85.25%).
  • 1 coverage regression across 1 file.

Uncovered Changes

File Changed Covered %
alioth/src/virtio/dev/blk.rs 61 52 85.25%

Coverage Regressions

1 previously-covered line in 1 file lost coverage.

File Lines Losing Coverage Coverage
alioth/src/virtio/dev/blk.rs 1 73.04%

Coverage Stats

Coverage Status
Relevant Lines: 17150
Covered Lines: 8209
Line Coverage: 47.87%
Relevant Branches: 2024
Covered Branches: 1214
Branch Coverage: 59.98%
Branches in Coverage %: Yes
Coverage Strength: 969.03 hits per line

💛 - Coveralls

@Lencerf
Lencerf force-pushed the alignfix branch 2 times, most recently from 20f6ef7 to 9d6841b Compare December 5, 2026 15:31
A read or write request was passed on to the disk image at whatever
sector the driver asked for. A write beyond the capacity of the device
extended the image on the host, and a large enough sector overflowed
the offset in the image.

Fail a request with IOERR unless its data is whole sectors within the
capacity of the device.

Fixes: 01f5cac ("feat(block): impl raw file backed virtio-block dev")
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
jacobgorm and others added 2 commits December 5, 2026 15:04
… reads

A file-backed block device floored the file size to whole 512-byte
sectors, making trailing bytes inaccessible from the VM. Round the
capacity up to whole sectors and zero-pad reads that legitimately cross
EOF in the final partial sector on both Mio and io_uring workers.

Signed-off-by: Jacob Gorm Hansen <jacobgorm@gmail.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
The io_uring worker parses a request from guest memory again when its
I/O completes. A driver that changed the header in between, e.g. to an
unsupported type or a sector out of range, got the request completed by
the parsing, which the worker took as unreachable and panicked.

Return the request to the driver with the status from the parsing.

Fixes: 342e016 ("feat(blk): io_uring based virtio-blk device")
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
@Lencerf

Lencerf commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix! I pushed a few updates on top of your branch to tighten bounds handling and cover a few edge cases. Here is a summary of what changed compared with your previous commit:

  • Reject out-of-range / unaligned requests up front (fix(virtio): reject out-of-range blk requests):
    • Added Block::check_range for both IN and OUT requests to require whole 512-byte sectors within capacity and guard against sector-to-offset overflow.
    • Prevents reads that start before EOF and cross past capacity (on both aligned and unaligned images) from silently succeeding with zero-padding, and stops out-of-bounds writes from extending the backing file.
  • Hardened unaligned tail reads on both workers (fix(virtio): round up blk device capacity and zero-pad unaligned tail reads):
    • Updated read_exact_zero_tail to retry on EINTR, use checked offset arithmetic, fail with UnexpectedEof if a full sector or more remains unread, and take an initial filled byte count.
    • Updated VirtioIoUring::complete_desc to use usize::try_from(result) (avoiding data.len() as i32 truncation), return 1 byte written on IOERR to match Mio, and finish short reads via read_exact_zero_tail rather than assuming every short read is EOF. IORING_OP_READ can legitimately return short reads before EOF. Blindly zero-filling data[result as usize..] without verifying EOF corrupts guest data.

@Lencerf
Lencerf added this pull request to the merge queue Dec 5, 2026
Merged via the queue into google:main with commit 48eb2d2 Dec 5, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants