Skip to content

openvmm: virtiofs: guest SETATTR with out-of-range nanoseconds panics in Duration::new #1597

Open

Description

@tangruize

Summary

A guest FUSE_SETATTR request that sets atime, mtime, or ctime with a nanosecond field of 1_000_000_000 or greater and secs = u64::MAX causes virtiofs to panic inside std::time::Duration::new with overflow in Duration::new. Found by formal verification.

Root Cause

In vm/devices/virtio/virtiofs/src/util.rs#L99-L142 (specifically #L121-L139), fuse_set_attr_to_lxutil passes the untrusted guest atime/atimensec, mtime/mtimensec, and ctime/ctimensec fields directly to std::time::Duration::new:

    if arg.valid & FATTR_ATIME != 0 {
        attr.atime = if arg.valid & FATTR_ATIME_NOW != 0 {
            lxutil::SetTime::Now
        } else {
            lxutil::SetTime::Set(Duration::new(arg.atime, arg.atimensec))
        };
    }

    if arg.valid & FATTR_MTIME != 0 {
        attr.mtime = if arg.valid & FATTR_MTIME_NOW != 0 {
            lxutil::SetTime::Now
        } else {
            lxutil::SetTime::Set(Duration::new(arg.mtime, arg.mtimensec))
        };
    }

    if arg.valid & FATTR_CTIME != 0 {
        attr.ctime = lxutil::SetTime::Set(Duration::new(arg.ctime, arg.ctimensec));
    }

Both VirtioFsFile::set_attr and VirtioFsInode::set_attr, dispatched from VirtioFs::set_attr, invoke fuse_set_attr_to_lxutil infallibly on the parsed fuse_setattr_in payload. Because Duration::new(secs, nanos) adds (nanos / 1_000_000_000) as u64 onto secs and panics on u64 overflow in both debug and release builds, any nanosecond value >= 1_000_000_000 paired with secs == u64::MAX panics immediately.

Consequence

Any guest with access to a writable virtio-fs share (in direct, aggregate, or microVM mode, using either node-based SETATTR or handle-based SETATTR with FATTR_FH) can send a crafted FUSE_SETATTR message that panics the virtio-fs worker and crashes the VMM process. Even when secs is below u64::MAX, out-of-range nanosecond fields (>= 1_000_000_000) silently carry into seconds instead of failing with EINVAL as POSIX utimensat(2) specifies.

Reproduction

  1. Attach a writable VirtioFs share containing a regular file, look up the file's node ID, and optionally open a writable file handle.
  2. Issue a FUSE_SETATTR (fuse_setattr_in) request on that node (with or without FATTR_FH) setting valid = FATTR_ATIME (and FATTR_ATIME_NOW unset), atime = u64::MAX, and atimensec = 1_000_000_000 (or analogously for FATTR_MTIME or FATTR_CTIME).
  3. Observe that VirtioFs::set_attr panics with overflow in Duration::new instead of returning Err(lx::Error::EINVAL).

Related: #1607, #1596, #1598.

Activity

  1. tangruize commented on Nov 15, 2026

    @tangruize
    ContributorAuthor

    Suggested Fix

    Validate guest-supplied nanosecond fields in util::fuse_set_attr_to_lxutil via a small helper (for example guest_time(secs: u64, nanos: u32) -> lx::Result<Duration>) that returns Err(lx::Error::EINVAL) whenever nanos >= 1_000_000_000 before calling Duration::new(secs, nanos). Change fuse_set_attr_to_lxutil to return lx::Result<lxutil::SetAttributes> and propagate the error with ? in both VirtioFsInode::set_attr and VirtioFsFile::set_attr. Rejecting nanos >= 1_000_000_000 matches POSIX utimensat(2) and does not affect standard Linux FUSE clients, which always send normalized sub-second nanosecond fields (if preserving carry behavior for non-overflowing timestamps were desired, guest_time could instead use checked_add on the carried seconds, though rejecting out-of-range nanoseconds up front is cleaner and standard).

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

    fix.patch
    diff --git a/vm/devices/virtio/virtiofs/src/file.rs b/vm/devices/virtio/virtiofs/src/file.rs
    index e4b1f5567..738422a92 100644
    --- a/vm/devices/virtio/virtiofs/src/file.rs
    +++ b/vm/devices/virtio/virtiofs/src/file.rs
    @@ -92,7 +92,7 @@ pub fn get_statx(&self) -> lx::Result<fuse_statx> {
     
         /// Sets the attributes of the open file.
         pub fn set_attr(&self, arg: &fuse_setattr_in, request_uid: lx::uid_t) -> lx::Result<()> {
    -        let attr = util::fuse_set_attr_to_lxutil(arg, request_uid);
    +        let attr = util::fuse_set_attr_to_lxutil(arg, request_uid)?;
     
             // Because FUSE_HANDLE_KILLPRIV is set, set-user-ID and set-group-ID must be cleared
             // depending on the attributes being set. Lxutil takes care of that on Windows (and Linux
    diff --git a/vm/devices/virtio/virtiofs/src/inode.rs b/vm/devices/virtio/virtiofs/src/inode.rs
    index 6ec061ce3..e63a335ed 100644
    --- a/vm/devices/virtio/virtiofs/src/inode.rs
    +++ b/vm/devices/virtio/virtiofs/src/inode.rs
    @@ -306,7 +306,7 @@ pub fn get_statx(&self) -> lx::Result<fuse_statx> {
         /// Sets the attributes of this inode.
         pub fn set_attr(&self, arg: &fuse_setattr_in, request_uid: lx::uid_t) -> lx::Result<fuse_attr> {
             self.validate_confined_object()?;
    -        let attr = util::fuse_set_attr_to_lxutil(arg, request_uid);
    +        let attr = util::fuse_set_attr_to_lxutil(arg, request_uid)?;
     
             // Because FUSE_HANDLE_KILLPRIV is set, set-user-ID and set-group-ID must be cleared
             // depending on the attributes being set. Lxutil takes care of that on Windows (and Linux
    diff --git a/vm/devices/virtio/virtiofs/src/lib.rs b/vm/devices/virtio/virtiofs/src/lib.rs
    index 71780f585..6dac9aad9 100644
    --- a/vm/devices/virtio/virtiofs/src/lib.rs
    +++ b/vm/devices/virtio/virtiofs/src/lib.rs
    @@ -1295,6 +1295,42 @@ fn request(node_id: u64) -> Request {
             Request::new(bytes.as_slice()).unwrap()
         }
     
    +    /// A `SETATTR` whose timestamp has a nanosecond field outside
    +    /// `0..1_000_000_000` fails with `EINVAL`, matching `utimensat`, rather
    +    /// than panicking inside `Duration::new` when nanoseconds carry into
    +    /// seconds at `u64::MAX`.
    +    #[test]
    +    fn set_attr_refuses_out_of_range_nanoseconds() {
    +        let tmp = tempfile::tempdir().unwrap();
    +        std::fs::write(tmp.path().join("file"), b"data").unwrap();
    +        let fs = VirtioFs::new(tmp.path(), None).unwrap();
    +        let entry = fs.lookup(&request(FUSE_ROOT_ID), "file".as_ref()).unwrap();
    +        let open = fs.open(&request(entry.nodeid), lx::O_RDWR as u32).unwrap();
    +        for valid in [FATTR_ATIME, FATTR_MTIME, FATTR_CTIME] {
    +            for handle in [0, FATTR_FH] {
    +                let arg = fuse_setattr_in {
    +                    valid: valid | handle,
    +                    fh: open.fh,
    +                    atime: u64::MAX,
    +                    mtime: u64::MAX,
    +                    ctime: u64::MAX,
    +                    atimensec: 1_000_000_000,
    +                    mtimensec: 1_000_000_000,
    +                    ctimensec: 1_000_000_000,
    +                    ..fuse_setattr_in::new_zeroed()
    +                };
    +                let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
    +                    fs.set_attr(&request(entry.nodeid), &arg)
    +                }));
    +                assert!(
    +                    matches!(result, Ok(Err(e)) if e == lx::Error::EINVAL),
    +                    "valid {:#x}",
    +                    valid | handle
    +                );
    +            }
    +        }
    +    }
    +
         pub(super) fn read_directory(
             fs: &VirtioFs,
             file: &super::VirtioFsFile,
    diff --git a/vm/devices/virtio/virtiofs/src/util.rs b/vm/devices/virtio/virtiofs/src/util.rs
    index 703c41f30..ebe77097b 100644
    --- a/vm/devices/virtio/virtiofs/src/util.rs
    +++ b/vm/devices/virtio/virtiofs/src/util.rs
    @@ -96,10 +96,13 @@ pub fn statx_to_fuse_statx(stat: &lx::StatEx) -> fuse_statx {
     }
     
     /// Convert a FUSE setattr message to a lxutil `SetAttributes` struct.
    +///
    +/// Fails with `EINVAL`, matching `utimensat`, when a timestamp to set has a
    +/// nanosecond field outside `0..1_000_000_000`.
     pub fn fuse_set_attr_to_lxutil(
         arg: &fuse_setattr_in,
         thread_uid: lx::uid_t,
    -) -> lxutil::SetAttributes {
    +) -> lx::Result<lxutil::SetAttributes> {
         let mut attr = lxutil::SetAttributes::default();
         attr.thread_uid = thread_uid;
         if arg.valid & FATTR_MODE != 0 {
    @@ -122,7 +125,7 @@ pub fn fuse_set_attr_to_lxutil(
             attr.atime = if arg.valid & FATTR_ATIME_NOW != 0 {
                 lxutil::SetTime::Now
             } else {
    -            lxutil::SetTime::Set(Duration::new(arg.atime, arg.atimensec))
    +            lxutil::SetTime::Set(guest_time(arg.atime, arg.atimensec)?)
             };
         }
     
    @@ -130,15 +133,27 @@ pub fn fuse_set_attr_to_lxutil(
             attr.mtime = if arg.valid & FATTR_MTIME_NOW != 0 {
                 lxutil::SetTime::Now
             } else {
    -            lxutil::SetTime::Set(Duration::new(arg.mtime, arg.mtimensec))
    +            lxutil::SetTime::Set(guest_time(arg.mtime, arg.mtimensec)?)
             };
         }
     
         if arg.valid & FATTR_CTIME != 0 {
    -        attr.ctime = lxutil::SetTime::Set(Duration::new(arg.ctime, arg.ctimensec));
    +        attr.ctime = lxutil::SetTime::Set(guest_time(arg.ctime, arg.ctimensec)?);
         }
     
    -    attr
    +    Ok(attr)
    +}
    +
    +/// Converts a guest-supplied timestamp of `secs` seconds and `nanos`
    +/// nanoseconds into a [`Duration`], rejecting out-of-range nanoseconds with
    +/// `EINVAL` before `Duration::new` can carry them into `secs` and panic on
    +/// overflow at `u64::MAX`.
    +fn guest_time(secs: u64, nanos: u32) -> lx::Result<Duration> {
    +    const NANOS_PER_SEC: u32 = 1_000_000_000;
    +    if nanos >= NANOS_PER_SEC {
    +        return Err(lx::Error::EINVAL);
    +    }
    +    Ok(Duration::new(secs, nanos))
     }
     
     #[cfg(test)]
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: guestInitial title-keyword triage suggestionarea: 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