Skip to content

openvmm: fuse: reply exceeding u32 header length panics in make_header #1628

Open

Description

@tangruize

Summary

fuse::reply::make_header computes the total FUSE reply length (size_of::<fuse_out_header>() + extra_len) and converts it to u32 using .try_into().unwrap(), while ReplySender::send_string computes value.len() + 1 without saturation. Whenever a fuse::Fuse implementation or ReplySender caller sends more than u32::MAX - 16 payload bytes (such as a FUSE_READ reply of u32::MAX bytes or a u32::MAX - 16-byte symlink target), make_header panics and aborts the VMM process instead of returning an io::Error. Found by formal verification.

Root Cause

In vm/devices/support/fs/fuse/src/reply.rs, every ReplySender helper (send_arg, send_error, send_data, send_arg_data, and send_string) constructs the 16-byte fuse_out_header via an infallible make_header helper that unwraps try_into() into u32:

    /// Send a string reply.
    fn send_string(&mut self, unique: u64, value: lx::LxString) -> io::Result<()> {
        tracing::trace!(unique, len = value.len(), "reply");
        let header = make_header(unique, value.len() + 1, 0);
        let header = header.as_bytes();
        self.send(&[
            io::IoSlice::new(header),
            io::IoSlice::new(value.as_bytes()),
            io::IoSlice::new(&[0]),
        ])
    }
}

fn make_header(unique: u64, extra_len: usize, error: i32) -> fuse_out_header {
    fuse_out_header {
        len: (size_of::<fuse_out_header>() + extra_len)
            .try_into()
            .unwrap(),
        error,
        unique,
    }
}

Even when a Fuse implementation strictly honors the guest's u32 size parameter in read, read_dir, read_dir_plus, get_xattr, or list_xattr (passing Session::send_max_size for max_size in (u32::MAX - 15)..=u32::MAX), adding size_of::<fuse_out_header>() (16 bytes) pushes the total frame size past u32::MAX, causing .try_into().unwrap() to panic. Likewise, Fuse::read_link returns an arbitrary LxString without any length cap, and send_string adds 1 for the NUL terminator unchecked before calling make_header.

Consequence

Because OpenVMM sets panic = 'abort' in both [profile.dev] and [profile.release] (Cargo.toml), a Fuse implementation returning a valid-within-arg.size buffer of u32::MAX - 15 to u32::MAX bytes (or a symlink target of at least u32::MAX - 16 bytes) aborts the entire VMM process rather than surfacing an io::Error from ReplySender. OpenVMM's built-in VirtioFs implementation bounds replies to at most 1 MiB (MAX_GUEST_BUFFER_SIZE), so this is not reachable through VirtioFs in practice.

Reproduction

Call ReplySender::send_data with u32::MAX as usize - 15 zero bytes, call ReplySender::send_string with a u32::MAX as usize - 16-byte LxString, or dispatch a FUSE_READ request with arg.size = u32::MAX to a Fuse implementation whose read returns Ok(vec![0; arg.size as usize]). On commit 9d64daad3cbe14fb0591081b73c6c9648e18d0fb, all three panic at vm/devices/support/fs/fuse/src/reply.rs:91:14 with called Result::unwrap() on an Err value: TryFromIntError(PosOverflow). Running cargo nextest run --profile agent -p fuse -E 'test(reply_longer_than_u32_is_refused) | test(dispatch_reply_longer_than_u32_is_not_sent)' reproduces both panics before fix.patch and passes afterward.

Related: #1616, #1629, #1631, #1630.

Activity

  1. tangruize commented on Nov 15, 2026

    @tangruize
    ContributorAuthor

    Suggested Fix

    Change make_header to return io::Result<fuse_out_header>, rejecting extra_len > u32::MAX as usize - size_of::<fuse_out_header>() with Err(io::ErrorKind::InvalidInput.into()) before casting (size_of::<fuse_out_header>() + extra_len) as u32. Propagate make_header(...)? across send_arg, send_error, send_data, send_arg_data, and send_string, and compute value.len().saturating_add(1) in send_string to avoid usize addition overflow. In Session::dispatch, the resulting OperationError::SendError is logged via tracelimit::error_ratelimited! without sending a malformed frame or aborting the VMM.

    Patch against OpenVMM 9d64daad3cbe14fb0591081b73c6c9648e18d0fb. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/vm/devices/support/fs/fuse/src/reply.rs b/vm/devices/support/fs/fuse/src/reply.rs
    index d765af056..c38948c2f 100644
    --- a/vm/devices/support/fs/fuse/src/reply.rs
    +++ b/vm/devices/support/fs/fuse/src/reply.rs
    @@ -27,7 +27,7 @@ fn send_arg<T: std::fmt::Debug + IntoBytes + Immutable + KnownLayout>(
             arg: T,
         ) -> io::Result<()> {
             tracing::trace!(unique, ?arg, "reply");
    -        let header = make_header(unique, size_of_val(&arg), 0);
    +        let header = make_header(unique, size_of_val(&arg), 0)?;
             let header = header.as_bytes();
             let arg = arg.as_bytes();
             self.send(&[io::IoSlice::new(header), io::IoSlice::new(arg)])
    @@ -39,7 +39,7 @@ fn send_error(&mut self, unique: u64, error: i32) -> io::Result<()> {
                 tracing::debug!(unique, error, "reply");
             }
     
    -        let header = make_header(unique, 0, -error);
    +        let header = make_header(unique, 0, -error)?;
             let header = header.as_bytes();
             self.send(&[io::IoSlice::new(header)])
         }
    @@ -47,7 +47,7 @@ fn send_error(&mut self, unique: u64, error: i32) -> io::Result<()> {
         /// Send a reply with arbitrary data.
         fn send_data(&mut self, unique: u64, data: &[u8]) -> io::Result<()> {
             tracing::trace!(unique, len = data.len(), "reply");
    -        let header = make_header(unique, data.len(), 0);
    +        let header = make_header(unique, data.len(), 0)?;
             let header = header.as_bytes();
             self.send(&[io::IoSlice::new(header), io::IoSlice::new(data)])
         }
    @@ -61,7 +61,7 @@ fn send_arg_data<T: std::fmt::Debug + IntoBytes + Immutable + KnownLayout>(
         ) -> io::Result<()> {
             tracing::trace!(unique, ?arg, len = data.len(), "reply");
     
    -        let header = make_header(unique, size_of_val(&arg), 0);
    +        let header = make_header(unique, size_of_val(&arg), 0)?;
             let header = header.as_bytes();
             let arg = arg.as_bytes();
             self.send(&[
    @@ -74,7 +74,7 @@ fn send_arg_data<T: std::fmt::Debug + IntoBytes + Immutable + KnownLayout>(
         /// Send a string reply.
         fn send_string(&mut self, unique: u64, value: lx::LxString) -> io::Result<()> {
             tracing::trace!(unique, len = value.len(), "reply");
    -        let header = make_header(unique, value.len() + 1, 0);
    +        let header = make_header(unique, value.len().saturating_add(1), 0)?;
             let header = header.as_bytes();
             self.send(&[
                 io::IoSlice::new(header),
    @@ -84,14 +84,18 @@ fn send_string(&mut self, unique: u64, value: lx::LxString) -> io::Result<()> {
         }
     }
     
    -fn make_header(unique: u64, extra_len: usize, error: i32) -> fuse_out_header {
    -    fuse_out_header {
    -        len: (size_of::<fuse_out_header>() + extra_len)
    -            .try_into()
    -            .unwrap(),
    +/// The header of a reply of `extra_len` bytes after the header.
    +///
    +/// Fails if the reply, header included, is longer than the header's `len` can say.
    +fn make_header(unique: u64, extra_len: usize, error: i32) -> io::Result<fuse_out_header> {
    +    if extra_len > u32::MAX as usize - size_of::<fuse_out_header>() {
    +        return Err(io::ErrorKind::InvalidInput.into());
    +    }
    +    Ok(fuse_out_header {
    +        len: (size_of::<fuse_out_header>() + extra_len) as u32,
             error,
             unique,
    -    }
    +    })
     }
     
     /// Helpers for writing a reply for `read_dir` or `read_dir_plus`.
    @@ -202,3 +206,36 @@ fn write_padding(writer: &mut impl Write, count: usize) {
         const PADDING: [u8; 8] = [0; 8];
         writer.write_all(&PADDING[..count]).unwrap();
     }
    +
    +#[cfg(test)]
    +mod tests {
    +    use super::*;
    +
    +    /// Keeps the bytes of the last reply.
    +    #[derive(Default)]
    +    struct CapturingSender {
    +        data: Vec<u8>,
    +    }
    +
    +    impl ReplySender for CapturingSender {
    +        fn send(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<()> {
    +            self.data = bufs.iter().flat_map(|s| s.iter()).copied().collect();
    +            Ok(())
    +        }
    +    }
    +
    +    /// A reply whose length, header included, does not fit the header's `len`
    +    /// (a `u32`) is refused with an error, not sent and not a panic. (The
    +    /// buffers are zeroed lazily and never read.)
    +    #[test]
    +    fn reply_longer_than_u32_is_refused() {
    +        let longest = u32::MAX as usize - size_of::<fuse_out_header>();
    +        let mut sender = CapturingSender::default();
    +        let data = vec![0u8; longest + 1];
    +        assert!(sender.send_data(1, &data).is_err());
    +        drop(data);
    +        let target = lx::LxString::from_vec(vec![0u8; longest]);
    +        assert!(sender.send_string(1, target).is_err());
    +        assert!(sender.data.is_empty());
    +    }
    +}
    diff --git a/vm/devices/support/fs/fuse/src/session.rs b/vm/devices/support/fs/fuse/src/session.rs
    index 4508e7944..81a16d0ad 100644
    --- a/vm/devices/support/fs/fuse/src/session.rs
    +++ b/vm/devices/support/fs/fuse/src/session.rs
    @@ -1259,4 +1259,57 @@ fn make_lookup_name_too_long() -> Vec<u8> {
     
             data
         }
    +
    +    /// Builds a request with the given opcode, node and payload.
    +    fn make_request(opcode: u32, nodeid: u64, payload: &[u8]) -> Vec<u8> {
    +        let header = fuse_in_header {
    +            len: (size_of::<fuse_in_header>() + payload.len()) as u32,
    +            opcode,
    +            unique: 2,
    +            nodeid,
    +            uid: 0,
    +            gid: 0,
    +            pid: 0,
    +            padding: 0,
    +        };
    +        let mut data = Vec::new();
    +        data.extend_from_slice(header.as_bytes());
    +        data.extend_from_slice(payload);
    +        data
    +    }
    +
    +    /// A file system whose `read` returns as many zero bytes as requested.
    +    struct ExactSizeReadFs;
    +
    +    impl Fuse for ExactSizeReadFs {
    +        fn read(&self, _request: &Request, arg: &fuse_read_in) -> lx::Result<Vec<u8>> {
    +            Ok(vec![0; arg.size as usize])
    +        }
    +    }
    +
    +    /// An initialized session of `fs`, and the sender that saw its replies.
    +    fn initialized<T: 'static + Fuse + Send + Sync>(fs: T) -> (Session, CapturingSender) {
    +        let session = Session::new(fs);
    +        let mut sender = CapturingSender::default();
    +        session.dispatch(Request::new(FUSE_INIT_REQUEST).unwrap(), &mut sender, None);
    +        assert!(session.is_initialized());
    +        (session, sender)
    +    }
    +
    +    /// A reply too long for the header's `len` (a `u32`) is not sent: the
    +    /// session reports a send error rather than aborting.
    +    #[test]
    +    fn dispatch_reply_longer_than_u32_is_not_sent() {
    +        let (session, mut sender) = initialized(ExactSizeReadFs);
    +        let before = sender.data.clone();
    +        let mut arg = fuse_read_in::new_zeroed();
    +        arg.size = u32::MAX;
    +        // The reply is u32::MAX zero bytes, allocated lazily and never read.
    +        session.dispatch(
    +            Request::new(make_request(FUSE_READ, 2, arg.as_bytes()).as_slice()).unwrap(),
    +            &mut sender,
    +            None,
    +        );
    +        assert_eq!(sender.data, before);
    +    }
     }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: openvmmInitial title-keyword triage suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions