Skip to content

openvmm: fuse: write and copy_file_range byte counts exceeding u32::MAX panic #1631

Open

Description

@tangruize

Summary

fuse::Fuse::write and fuse::Fuse::copy_file_range both return lx::Result<usize>, and fuse_copy_file_range_in::len is a u64, but Session::dispatch_core converts the returned byte count into fuse_write_out.size (u32) with out.try_into().unwrap(). On 64-bit hosts, a Fuse implementation returning a count larger than u32::MAX panics and aborts the VMM process instead of replying with -EIO. Found by formal verification.

Root Cause

In vm/devices/support/fs/fuse/src/session.rs and vm/devices/support/fs/fuse/src/session.rs, both FuseOperation::Write and FuseOperation::CopyFileRange unwrap out.try_into() when populating fuse_write_out:

            FuseOperation::Write { arg, data } => {
                let out = self.fs.write(&request, arg, data)?;
                sender.send_arg(
                    request.unique(),
                    fuse_write_out {
                        size: out.try_into().unwrap(),
                        padding: 0,
                    },
                )?;
            }
            FuseOperation::CopyFileRange { arg } => {
                let out = self.fs.copy_file_range(&request, arg)?;
                sender.send_arg(
                    request.unique(),
                    fuse_write_out {
                        size: out.try_into().unwrap(),
                        padding: 0,
                    },
                )?;
            }

The Fuse trait signatures for write and copy_file_range both return lx::Result<usize> without documenting a u32::MAX bound, and fuse_copy_file_range_in carries a 64-bit pub len: u64 even though its reply reuses fuse_write_out (pub size: u32).

Consequence

Because OpenVMM configures panic = 'abort' in both [profile.dev] and [profile.release] (Cargo.toml), any Fuse implementation on a 64-bit target whose write or copy_file_range returns a count above u32::MAX terminates the VMM process instead of failing the FUSE operation cleanly. OpenVMM's built-in VirtioFs implementation caps guest write buffers at 1 MiB (MAX_GUEST_BUFFER_SIZE) and leaves copy_file_range unimplemented (ENOSYS), so this is not reachable through VirtioFs today.

Reproduction

Initialize a fuse::Session over a minimal Fuse implementation whose write and copy_file_range return Ok(u32::MAX as usize + 1), then dispatch a FUSE_WRITE or FUSE_COPY_FILE_RANGE request. On commit 9d64daad3cbe14fb0591081b73c6c9648e18d0fb, Session::dispatch panics at vm/devices/support/fs/fuse/src/session.rs:213:46 (or session.rs:396:46) with called Result::unwrap() on an Err value: TryFromIntError(PosOverflow). Running cargo nextest run --profile agent -p fuse -E 'test(dispatch_write_and_copy_beyond_u32_fail)' reproduces the panic before fix.patch and passes afterward.

Related: #1629, #1628, #1630.

Activity

  1. tangruize commented on Nov 15, 2026

    @tangruize
    ContributorAuthor

    Suggested Fix

    Add a Session::reply_count(count: usize) -> lx::Result<u32> helper that converts count via try_into() and, on overflow, logs a rate-limited error via tracelimit::error_ratelimited! and returns Err(lx::Error::EIO), and use it in both FuseOperation::Write and FuseOperation::CopyFileRange so oversized counts produce a standard -EIO FUSE error reply instead of panicking.

    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/session.rs b/vm/devices/support/fs/fuse/src/session.rs
    index 4508e7944..ec0a2eed7 100644
    --- a/vm/devices/support/fs/fuse/src/session.rs
    +++ b/vm/devices/support/fs/fuse/src/session.rs
    @@ -207,13 +207,8 @@ fn dispatch_helper(
                 }
                 FuseOperation::Write { arg, data } => {
                     let out = self.fs.write(&request, arg, data)?;
    -                sender.send_arg(
    -                    request.unique(),
    -                    fuse_write_out {
    -                        size: out.try_into().unwrap(),
    -                        padding: 0,
    -                    },
    -                )?;
    +                let size = Self::reply_count(out)?;
    +                sender.send_arg(request.unique(), fuse_write_out { size, padding: 0 })?;
                 }
                 FuseOperation::StatFs {} => {
                     let out = self.fs.statfs(&request)?;
    @@ -390,13 +385,8 @@ fn dispatch_helper(
                 }
                 FuseOperation::CopyFileRange { arg } => {
                     let out = self.fs.copy_file_range(&request, arg)?;
    -                sender.send_arg(
    -                    request.unique(),
    -                    fuse_write_out {
    -                        size: out.try_into().unwrap(),
    -                        padding: 0,
    -                    },
    -                )?;
    +                let size = Self::reply_count(out)?;
    +                sender.send_arg(request.unique(), fuse_write_out { size, padding: 0 })?;
                 }
                 FuseOperation::SetupMapping { arg } => {
                     if let Some(mapper) = mapper {
    @@ -578,6 +568,16 @@ fn send_max_size(
             Ok(())
         }
     
    +    /// Converts a byte count returned by the file system into the `u32` the
    +    /// FUSE `fuse_write_out` reply carries, failing with `EIO` if it does not
    +    /// fit.
    +    fn reply_count(count: usize) -> lx::Result<u32> {
    +        count.try_into().map_err(|_| {
    +            tracelimit::error_ratelimited!(count, "File system returned a byte count beyond u32");
    +            lx::Error::EIO
    +        })
    +    }
    +
         /// Process `FUSE_BATCH_FORGET` by repeatedly calling `forget`.
         fn batch_forget(&self, count: u32, mut nodes: &[u8]) {
             for _ in 0..count {
    @@ -1259,4 +1259,85 @@ 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 `write` and `copy_file_range` return counts beyond `u32::MAX`.
    +    struct HugeWriteFs;
    +
    +    impl Fuse for HugeWriteFs {
    +        fn write(
    +            &self,
    +            _request: &Request,
    +            _arg: &fuse_write_in,
    +            _data: &[u8],
    +        ) -> lx::Result<usize> {
    +            Ok(u32::MAX as usize + 1)
    +        }
    +
    +        fn copy_file_range(
    +            &self,
    +            _request: &Request,
    +            _arg: &fuse_copy_file_range_in,
    +        ) -> lx::Result<usize> {
    +            Ok(u32::MAX as usize + 1)
    +        }
    +    }
    +
    +    /// 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)
    +    }
    +
    +    /// The error code in the last reply sent to `sender`.
    +    fn reply_error(sender: &CapturingSender) -> i32 {
    +        fuse_out_header::read_from_prefix(&sender.data)
    +            .unwrap()
    +            .0
    +            .error
    +    }
    +
    +    /// `write` or `copy_file_range` returning a count larger than `u32::MAX`
    +    /// replies with `EIO` rather than aborting.
    +    #[test]
    +    fn dispatch_write_and_copy_beyond_u32_fail() {
    +        let (session, mut sender) = initialized(HugeWriteFs);
    +
    +        let write_arg = fuse_write_in::new_zeroed();
    +        session.dispatch(
    +            Request::new(make_request(FUSE_WRITE, 2, write_arg.as_bytes()).as_slice()).unwrap(),
    +            &mut sender,
    +            None,
    +        );
    +        assert_eq!(reply_error(&sender), -lx::EIO);
    +
    +        let copy_arg = fuse_copy_file_range_in::new_zeroed();
    +        session.dispatch(
    +            Request::new(make_request(FUSE_COPY_FILE_RANGE, 2, copy_arg.as_bytes()).as_slice())
    +                .unwrap(),
    +            &mut sender,
    +            None,
    +        );
    +        assert_eq!(reply_error(&sender), -lx::EIO);
    +    }
     }
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