Skip to content

openvmm: virtiofs: guest inode numbers omit host device ID and collide across btrfs subvolumes #1619

Open

Description

@tangruize

Summary

VirtioFsInode computes the guest-visible inode number (fuse_attr.ino, fuse_statx.ino, and fuse_dirent.ino) solely from (volume_id, stat.inode_nr) via namespace_ino, omitting the host device ID (stat.device_nr). Because the guest kernel mounts a virtiofs share under a single FUSE superblock with one guest st_dev, distinct files and directories residing on different host st_dev domains inside the same share — such as btrfs subvolumes, snapshots, or nested mounts — report identical (st_dev, st_ino) pairs to guest userspace. Found by testing.

Root Cause

In vm/devices/virtio/virtiofs/src/inode.rs, namespace_ino namespaces raw host inode numbers only by volume_id (leaving volume_id == 0 completely unchanged), and VirtioFsVolume::map_inode passes only raw: lx::ino_t:

pub(crate) fn namespace_ino(volume_id: u32, raw: lx::ino_t) -> lx::ino_t {
    if volume_id == 0 {
        return raw;
    }
    raw ^ u64::from(volume_id).wrapping_mul(INO_NAMESPACE_MULTIPLIER)
}

When constructing an inode in VirtioFsInode::with_attr and reporting attributes in VirtioFsInode::attr_from_stat / statx_from, stat.device_nr is ignored:

    pub fn with_attr(volume: Arc<VirtioFsVolume>, path: PathBuf, stat: &lx::Stat) -> Self {
        let guest_inode_nr = volume.map_inode(stat.inode_nr);

On btrfs, every subvolume and snapshot has its own anonymous block device (st_dev) and its own independent 64-bit object-ID (st_ino) sequence starting at 256 for the subvolume root (257 for the first created file, 258 for the second, and so on). Even when DedupKey::Ino includes stat.device_nr so InodeMap allocates distinct FUSE nodeids, fuse_attr.ino and fuse_statx.ino still report identical st_ino values under the guest's single virtiofs st_dev.

Consequence

POSIX applications rely on (st_dev, st_ino) uniquely identifying every live filesystem object:

  • Directory traversal false loop detection: Every btrfs subvolume root inside a share reports st_ino = 256 with the same guest st_dev. Tools that track visited ancestor or sibling directories by (st_dev, st_ino) (find, glibc fts/nftw, du, rm -rf, chown -R) can falsely diagnose directory loops or skip entire subvolume subtrees.
  • Silent data corruption or omission in backup/copy/disk-accounting tools: Two independent subvolumes created inside one share routinely allocate identical st_ino numbers (257, 258, ...) to unrelated regular files. du deduplicates seen (st_dev, st_ino) pairs when st_nlink > 1 (or across directories) and undercounts usage; tar, cp -a, and rsync -H can record or materialize an unrelated file in a second subvolume as a hard link to a file in the first subvolume; and diff -r or build caches checking (st_dev, st_ino) equality can treat distinct files as the same object.

Reproduction

  1. On a Linux btrfs host, create a shared directory root containing two subvolumes btrfs subvolume create root/sub_a and btrfs subvolume create root/sub_b, and create root/sub_a/file ("alpha") and root/sub_b/file ("beta").
  2. Expose root through VirtioFs (volume_id == 0) and issue FUSE_LOOKUP + FUSE_GETATTR (or FUSE_STATX) on sub_a and sub_b, and on sub_a/file and sub_b/file.
  3. Observe sub_a and sub_b both return attr.ino == 256, and sub_a/file and sub_b/file both return attr.ino == 257, despite having distinct host st_dev values and distinct contents.

Suggested Fix

Record the volume root's root_device_nr: u64 on VirtioFsVolume and incorporate stat.device_nr into map_inode(device_nr, raw_ino) whenever device_nr != root_device_nr (for example mixing (volume_id, device_nr ^ root_device_nr) with a bijective 64-bit mixer before XORing into raw_ino). That preserves exact identity mapping (guest_ino == raw_ino) for single-device, single-root shares (volume_id == 0 && device_nr == root_device_nr) while keeping the mapping bijective within each (volume_id, device_nr) domain so genuine hard links inside a subvolume still share attr.ino. Two coupled paths should be handled alongside this change: plain FUSE_READDIR (getdents64 provides d_ino without child st_dev, so plain READDIR in multi-device shares either needs child lstat when crossing devices or relies on READDIRPLUS entry_out.attr.ino overriding dirent.ino), and microVM saved-state migration (SavedInode.guest_inode_id and SavedDirectoryEntry.guest_inode_id are validated on restore against inode.guest_inode_nr(), requiring either a SCHEMA_VERSION bump or preserving saved guest_inode_id values across restore).

Related: #1604, #1605.

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