openvmm: virtiofs: preflight_microvm_insert treats retyped dedup keys as alias additions, bypassing MAX_INODES

Aperta Adatta ai principianti
#1,626 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Inizia in vm/devices/virtio/virtiofs/src/microvm/limits.rs da preflight_microvm_insert e confrontalo con InodeMap::dedup_entry in vm/devices/virtio/virtiofs/src/lib.rs, che elimina già una chiave quando file_type() è diverso. Fai prendere al percorso di preflight lo stesso ramo, trattando una chiave con tipo cambiato come un nuovo inode anziché un alias, poi conferma che l'invariante controllato da save_microvm_state in microvm/fs.rs vale ancora. Fatto significa che insert_microvm restituisce ENOSPC a MAX_INODES per una chiave con tipo cambiato, con un test unitario nel crate virtiofs che copre un file seguito da una directory che riutilizza la stessa chiave di dedup.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area: openvmm

Summary

In microVM virtio-fs, InodeMap::preflight_microvm_insert checks self.inodes_by_key.get(&inode.dedup_key()) without verifying that the existing entry has the same file_type() as inode. When a host inode number (DedupKey) is reused by an object of a different file type (such as a file unlinked while still tracked by the guest and replaced by a directory or symlink that reuses the freed host inode number), InodeMap::dedup_entry evicts the old inodes_by_key entry and allocates a brand-new node ID in inodes_by_node_id, yet preflight_microvm_insert ran preflight_microvm_alias_add instead of preflight_microvm_new_inode, bypassing the MAX_INODES (4096) cap and can_insert() check. Found during formal specification (blocks formal verification).

Root Cause

InodeMap::preflight_microvm_insert assumes any match in self.inodes_by_key will reuse the existing node and add an alias:

    pub(crate) fn preflight_microvm_insert(&self, inode: &VirtioFsInode) -> lx::Result<()> {
        let path = inode.clone_path();
        Self::validate_microvm_alias_path(&path)?;
        if let Some((existing, _)) = self.inodes_by_key.get(&inode.dedup_key()) {
            return self.preflight_microvm_alias_add(existing, &path);
        }
        self.preflight_microvm_new_inode(&path)
    }

However, InodeMap::insert and InodeMap::insert_reserved look up inodes_by_key through InodeMap::dedup_entry, which explicitly evicts the inodes_by_key mapping whenever existing.file_type() != inode.file_type() while leaving existing live inside self.inodes_by_node_id.values:

    fn dedup_entry<'a>(
        inodes_by_key: &'a mut HashMap<DedupKey, (Arc<VirtioFsInode>, u64)>,
        inode: &VirtioFsInode,
    ) -> Entry<'a, DedupKey, (Arc<VirtioFsInode>, u64)> {
        let key = inode.dedup_key();
        if inodes_by_key
            .get(&key)
            .is_some_and(|(existing, _)| existing.file_type() != inode.file_type())
        {
            inodes_by_key.remove(&key);
        }
        inodes_by_key.entry(key)
    }

Because dedup_entry returns Entry::Vacant, InodeMap::insert allocates a fresh node in self.inodes_by_node_id without ever having checked self.inodes_by_node_id.values.len() >= MAX_INODES || !self.inodes_by_node_id.can_insert().

Consequence

When self.inodes_by_node_id.values.len() has reached MAX_INODES (4096), looking up or inserting a host object whose (volume_id, device_nr, inode_nr) matches a still-tracked inode of a different file type bypasses MAX_INODES and inserts a 4,097th (and arbitrarily more) active inode into inodes_by_node_id.values. Once inodes_by_node_id.values.len() > MAX_INODES, subsequent microVM snapshot capture fails in save_microvm_state (anyhow::ensure!(inodes.inodes_by_node_id.values.len() <= MAX_INODES)). In addition, if inodes_by_node_id is exhausted (!can_insert()), InodeMap::insert reaches dedup_entry first and evicts existing from inodes_by_key before inodes_by_node_id.insert fails with ENOSPC; conversely, if existing already holds MAX_ALIASES_PER_INODE (256) aliases while inodes_by_node_id still has room, preflight_microvm_insert falsely rejects the retyped object with ENOSPC even though no alias is added to existing.

Reproduction

  1. Attach a writable microVM share, look up a regular file "file" so its (volume_id, device_nr, inode_nr) is recorded in inodes_by_key and inodes_by_node_id, and populate inodes_by_node_id up to MAX_INODES (4096).
  2. Replace that host inode number with an object of another file type (for example, unlink "file" while keeping its FUSE node referenced, create a directory or symlink that reuses the freed host inode number on tmpfs/ext4, or construct a VirtioFsInode sharing stat.device_nr and stat.inode_nr with S_IFDIR mode).
  3. Call inodes.insert_microvm(other) (via FUSE_LOOKUP or FUSE_READDIRPLUS): insert_microvm succeeds instead of returning Err(lx::Error::ENOSPC), growing inodes_by_node_id.values.len() to 4097 (MAX_INODES + 1), after which save_microvm_state fails.

Related: #1611, #1613.

Lingua principale
Rust
Stelle
848
Fork
63
Merge medio
12h 32m
PR unite (30g)
602

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/nvx

Tutte le issue di microsoft/nvx

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.