Description
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 = 256with the same guestst_dev. Tools that track visited ancestor or sibling directories by(st_dev, st_ino)(find, glibcfts/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_inonumbers (257, 258, ...) to unrelated regular files.dudeduplicates seen(st_dev, st_ino)pairs whenst_nlink > 1(or across directories) and undercounts usage;tar,cp -a, andrsync -Hcan record or materialize an unrelated file in a second subvolume as a hard link to a file in the first subvolume; anddiff -ror build caches checking(st_dev, st_ino)equality can treat distinct files as the same object.
Reproduction
- On a Linux btrfs host, create a shared directory
rootcontaining two subvolumesbtrfs subvolume create root/sub_aandbtrfs subvolume create root/sub_b, and createroot/sub_a/file("alpha") androot/sub_b/file("beta"). - Expose
rootthroughVirtioFs(volume_id == 0) and issueFUSE_LOOKUP+FUSE_GETATTR(orFUSE_STATX) onsub_aandsub_b, and onsub_a/fileandsub_b/file. - Observe
sub_aandsub_bboth returnattr.ino == 256, andsub_a/fileandsub_b/fileboth returnattr.ino == 257, despite having distinct hostst_devvalues 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).
Activity