Linux 7.3 PAGEMAP_SCAN leaves pooled linear memory uncleared between Wasmtime instances

Aperta
#15,395 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
18/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
linux, rust, wasm

Direzione di ricerca

Start in crates/wasmtime/src/runtime/vm/sys/unix/pagemap.rs: reset_with_pagemap() around lines 318-321 asks for WRITTEN, and PageMap::new() only validates an empty range, so Enabled::Auto can pick this path too. Build the PoC crate and run cargo run --release -- no then -- yes on Linux 7.3 to reproduce the stale 0x5a, then trace how the pooling allocator calls the reset path. Done means the scan-enabled probe reports 0x00, by registering ranges for userfaultfd write-protect or correctly falling back to reset_manually/decommit for unmatched pages.

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

Descrizione

bug wasmtime:pooling-allocator

On Linux 7.3.4, Wasmtime 50.0.2 leaves a previous instance's contents in a reused pooled linear-memory slot. The PoC observes this through Wasmtime's host memory API. This requires an application to opt into PoolingAllocationConfig::pagemap_scan(Enabled::Yes) (or Auto) and set linear_memory_keep_resident above zero. A fresh instance of the test module should start with zeroed memory, yet the one-slot probe reads the previous instance's value at byte zero. In the recorded 50.0.2 runs, scan enabled reports that stale value three times out of three; scan disabled reports zero three times out of three. The pooling configuration defaults to scan disabled and both keep-resident values set to zero.

Details

On Linux 7.3, reporting PAGE_IS_WRITTEN requires explicit userfaultfd write tracking: a program must register the memory range with UFFDIO_REGISTER_MODE_WP and write-protect its pages before subsequent writes receive that flag. See the Linux PAGEMAP_SCAN documentation, userfaultfd guide, and kernel change. Wasmtime does not register its pooled memory for this tracking mode.

In Wasmtime 50.0.2, crates/wasmtime/src/runtime/vm/sys/unix/pagemap.rs, reset_with_pagemap() asks for pages matching WRITTEN while excluding zero and file-backed pages:

.category_inverted(Categories::PFNZERO | Categories::FILE)
.category_mask(Categories::WRITTEN | Categories::PFNZERO | Categories::FILE)

Under the changed kernel semantics, this written anonymous page cannot match the WRITTEN filter. On a successful scan that reaches the end without reporting it, the function returns without calling reset_manually or decommit for that page. The next instance receives the slot with its old contents. The probe directly observes that stale byte; it does not trace the ioctl's returned regions, so the omitted-region explanation is inferred from the kernel and Wasmtime source. PageMap::new() only checks that the ioctl accepts an empty range; that check does not test the semantics of WRITTEN, so Enabled::Auto can also select this path.

The earliest released Wasmtime version I verified with this scan logic is 37.0.0; 36.0.20 does not contain the scanner. I checked available release tags through 50.0.2 for the WRITTEN requirement. The 50.0.2 scan filter and main at 3ce2209, inspected November 19, still require WRITTEN. I ran the reproducer against 50.0.2.

Proof of concept

Run this standalone crate on Linux 7.3 with the changed PAGE_IS_WRITTEN semantics. The recorded run used Ubuntu 7.3.4-070304-generic on ARM64 with 4096-byte pages; x86-64 was not tested. The test creates one memory slot, writes 0x5a in a first Store, drops that store, then reads byte zero from a second Store. 0x5a is an arbitrary test marker; the issue is that previous contents remain in the reused memory instead of its required initial contents. This test exercises byte zero with that marker.

# Cargo.toml
[package]
name = "pagemap-linux73-repro"
version = "0.1.0"
edition = "2024"

[workspace]

[dependencies]
wasmtime = { version = "=50.0.2", default-features = false, features = ["cranelift", "pooling-allocator", "wat"] }
// src/main.rs
use std::error::Error;
use std::process::ExitCode;
use wasmtime::{Config, Enabled, Engine, Instance, Module, PoolingAllocationConfig, Store};

fn run(scan: Enabled) -> Result<u8, Box<dyn Error>> {
    let mut pool = PoolingAllocationConfig::new();
    pool.total_memories(1)
        .total_core_instances(1)
        .total_tables(1)
        .linear_memory_keep_resident(65_536)
        .pagemap_scan(scan);

    let mut config = Config::new();
    config.allocation_strategy(pool);
    let engine = Engine::new(&config)?;
    let module = Module::new(&engine, r#"(module (memory (export "m") 1))"#)?;

    {
        let mut store = Store::new(&engine, ());
        let instance = Instance::new(&mut store, &module, &[])?;
        let memory = instance.get_memory(&mut store, "m").unwrap();
        memory.data_mut(&mut store)[0] = 0x5a;
    }

    let mut store = Store::new(&engine, ());
    let instance = Instance::new(&mut store, &module, &[])?;
    let memory = instance.get_memory(&mut store, "m").unwrap();
    Ok(memory.data(&store)[0])
}

fn main() -> Result<ExitCode, Box<dyn Error>> {
    let mode = std::env::args().nth(1).ok_or("usage: probe yes|no")?;
    let scan = match mode.as_str() {
        "yes" => Enabled::Yes,
        "no" => Enabled::No,
        _ => return Err("usage: probe yes|no".into()),
    };
    let observed = run(scan)?;
    println!("pagemap_scan={mode} observed=0x{observed:02x} expected=0x00");
    Ok(if observed == 0 {
        ExitCode::SUCCESS
    } else {
        ExitCode::FAILURE
    })
}
uname -r
cargo run --release -- no
cargo run --release -- yes

Representative output from the Wasmtime 50.0.2 run on Linux 7.3.4:

7.3.4-070304-generic
pagemap_scan=no observed=0x00 expected=0x00
pagemap_scan=yes observed=0x5a expected=0x00

Impact

The demonstrated failure is a prior instance's linear-memory byte remaining visible through the host memory API in a later instance using the same pool slot. It also breaks the expected fresh-memory state. In a service that shares a pooling allocator between distinct tenants, the same mechanism could expose one tenant's guest-memory contents to another.

Lingua principale
Rust
Stelle
18.9k
Fork
2k
Merge medio
1g 6h
PR unite (30g)
238

Preparare l'ambiente

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 bytecodealliance/wasmtime

Tutte le issue di bytecodealliance/wasmtime

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.