openvmm: virtiofs: zero-length virtqueue payload descriptors truncate FUSE request reads and reply writes

Open Beginner friendly
#1,621 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Start in vm/devices/virtio/virtiofs/src/virtio_util.rs at VirtioPayloadReader::next_payload/get_payload and the mirror in VirtioPayloadWriter; read Read::read, RequestReader::read_until, and Write::write to see how a zero-length VirtioQueuePayload yields remaining == 0. Then follow the callers in vm/devices/virtio/virtiofs/src/virtio.rs: process_virtiofs_request and VirtioReplySender::send. Done means scatter-gather chains containing zero-length readable/writable descriptors no longer cause premature EOF, EINVAL, or WriteZero, verified with tests for the split FUSE_INIT and FUSE_LOOKUP chains.

Written by the indexing model from the issue text.

Description

area: openvmm

Summary

VirtioPayloadReader::get_payload and VirtioPayloadWriter::get_payload advance past an exhausted buffer with a single if self.offset == payload.length as usize check rather than a while loop (and next_payload does not skip zero-length descriptors). Whenever a descriptor chain contains a zero-length readable or writable VirtioQueuePayload after a non-empty buffer (or two consecutive zero-length buffers at the start of the chain), get_payload returns a buffer with remaining == 0, causing Read::read to report premature EOF (Ok(0)), RequestReader::read_until to fail with EINVAL, and Write::write to return Ok(0) so VirtioReplySender::send aborts with WriteZero and completes the virtqueue item with 0 bytes written. Found by testing.

Root Cause

In VirtioPayloadReader, next_payload selects the next !p.writeable descriptor regardless of p.length, and get_payload advances at most once when self.offset == payload.length as usize:

    fn next_payload(&mut self) {
        self.current = self.payload.find(|p| !p.writeable);
    }

    fn get_payload(&mut self) -> Option<&'payload VirtioQueuePayload> {
        let payload = self.current?;
        if self.offset == payload.length as usize {
            self.next_payload();
            self.offset = 0;
        }

        self.current
    }

VirtioPayloadWriter has the identical single-step logic for writable descriptors:

    fn next_payload(&mut self) {
        self.current = self.payload.find(|p| p.writeable);
    }

    fn get_payload(&mut self) -> Option<&'payload VirtioQueuePayload> {
        let payload = self.current?;
        if self.offset == payload.length as usize {
            self.next_payload();
            self.offset = 0;
        }

        self.current
    }

When next_payload() lands on a descriptor with length == 0 (either after finishing a preceding non-empty descriptor where self.offset == payload.length, or after advancing past a first leading length == 0 descriptor onto a second length == 0 descriptor), get_payload() returns Some(payload) with payload.length == 0 and self.offset == 0 even when subsequent descriptors in the chain still hold readable data or writable reply capacity.

Consequence

Valid scatter-gather virtqueue chains that include zero-length descriptors fail across all three payload I/O paths:

  • Header and fixed-struct reads (Read::read -> read_exact): In Read::read, remaining evaluates to 0, so read returns Ok(0) for a non-empty caller buffer, which read_exact treats as ErrorKind::UnexpectedEof. If the 40-byte fuse_in_header spans [readable(30), readable(0), readable(74)] (or starts with [readable(0), readable(0), readable(104)]), fuse::Request::new fails in process_virtiofs_request, firing (worker.notify_corruption)() and completing the descriptor with 0 used bytes and no FUSE reply.
  • NUL-terminated string reads (RequestReader::read_until): In read_until, get_current_remaining_len() returns 0 upon advancing onto the empty descriptor and immediately returns Err(lx::Error::EINVAL) without inspecting following readable descriptors (for example, FUSE_LOOKUP split as [readable(40), readable(0), readable(10)]).
  • Reply writes (Write::write -> write_all in VirtioReplySender::send): In Write::write, remaining evaluates to 0 and write returns Ok(0). VirtioReplySender::send calls writer.write_all(buf)?, which fails with ErrorKind::WriteZero before updating self.bytes_written. On a FUSE_INIT chain [readable(104), writable(16), writable(0), writable(64)], the 16-byte fuse_out_header is written into writable(16), the 64-byte fuse_init_out body fails at writable(0), and the queue item completes with bytes_written == 0 even though FUSE state transitions (and partial guest-memory header writes) already took place.

Reproduction

  1. Construct guest memory containing a valid 104-byte FUSE_INIT request (40-byte fuse_in_header + 64-byte fuse_init_in) and 80 bytes of writable reply space.
  2. Submit the request through process_virtiofs_request with readable descriptors split as [readable(30), readable(0), readable(74), writable(80)]: VirtioPayloadReader reads 30 bytes, advances onto readable(0), returns Ok(0) on the remaining 10 header bytes, triggers notify_corruption, and returns 0 bytes written.
  3. Submit a FUSE_LOOKUP request split as [readable(40), readable(0), readable(6), writable(144)] where "hello\0" resides in the third descriptor: read_until(0) sees get_current_remaining_len() == 0 at readable(0) and replies with -EINVAL.
  4. Submit FUSE_INIT with writable descriptors split as [readable(104), writable(16), writable(0), writable(64)]: VirtioReplySender::send writes the 16-byte fuse_out_header, hits WriteZero on writable(0) when writing fuse_init_out, and returns bytes_written == 0.

Suggested Fix

Skip zero-length descriptors in both VirtioPayloadReader and VirtioPayloadWriter by changing next_payload to filter on p.length > 0 (self.payload.find(|p| !p.writeable && p.length > 0) and self.payload.find(|p| p.writeable && p.length > 0)) and/or changing if self.offset == payload.length as usize in both get_payload implementations to while let Some(payload) = self.current { if self.offset < payload.length as usize { break; } self.next_payload(); self.offset = 0; }.

Dominant language
Rust
Stars
848
Forks
63
Avg merge
13h 37m
Merged PRs (30d)
571

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/nvx

All issues in microsoft/nvx

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.