Skip to content

Using wasmtime_wasi Dir struct can cause panic of TryFromIntError #6978

Description

@lewisfm

Thanks for filing a bug report! Please fill out the TODOs below.

Note: if you want to report a security issue, please read our security policy!

Test Case

Here's the WASM code required to reproduce this bug:

fn main() {
    let dir = std::fs::read_dir("/").unwrap();
    println!("{:?}", dir.collect::<Vec<_>>());
}
cargo build --target wasm32-wasi --release

Compiled in release mode with debug = true:
test.wasm.tgz

Here's the Rust host code required, modified from the WASI example:

use wasmtime::*;
use wasmtime_wasi::{ambient_authority, sync::WasiCtxBuilder, Dir};

fn main() -> Result<()> {
    // Define the WASI functions globally on the `Config`.
    let engine = Engine::default();
    let mut linker = Linker::new(&engine);
    wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;

    let wasm_fs = Dir::open_ambient_dir("/", ambient_authority())
        .expect("Filesystem root should be accessible");

    // Create a WASI context and put it in a Store; all instances in the store
    // share this context. `WasiCtxBuilder` provides a number of ways to
    // configure what the target program will have access to.
    let wasi = WasiCtxBuilder::new()
        .preopened_dir(wasm_fs, "/")
        .unwrap()
        .inherit_stdio()
        .inherit_args()?
        .build();
    let mut store = Store::new(&engine, wasi);

    // Instantiate our module with the imports we've created, and run it.
    let module = Module::from_file(&engine, "./test.wasm")?;
    linker.module(&mut store, "", &module)?;
    // --> Panic occurs here
    linker
        .get_default(&mut store, "")?
        .typed::<(), ()>(&store)?
        .call(&mut store, ())?;

    Ok(())
}

Steps to Reproduce

  • Use wasmtime_wasi::Dir to allow access to the root directory
  • Attempt to read the root directory from WASM

Expected Results

The program will print the files in the root directory: bin, dev, usr, etc.

Actual Results

Program panics:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TryFromIntError(())', <...>/cap-primitives-2.0.0/src/rustix/fs/metadata_ext.rs:167:49

Versions and Environment

Wasmtime version or commit: v12.0.1

Operating system: macOS Ventura 13.5.1

Architecture: arm64 Apple Silicon

Extra Info

I'm not familiar with ambient authority.

Metadata

Metadata

Assignees

Labels

bugIncorrect behavior in the current implementation that needs fixing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions