wasip1-adapter: invalid UTF-8 path bytes trap instead of returning an error

Abierto
#15,426 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@NaniToka ya está trabajando en esto.

Desde el 23/11/2026.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
rust, wasm
Área
backend

Línea de trabajo

Start in crates/wasi-preview1-component-adapter/src/lib.rs, around lines 1781-1787 and 1912-1927, where path byte slices are passed to P2 string parameters. Build the reproducer with the Wasm C toolchain and run it under wasmtime run -S preview2=y, checking the path_* operations for index 0 through 6. Done means each invalid-UTF-8 path returns __WASI_ERRNO_ILSEQ (errno 25) instead of trapping, matching the Direct WASIp1 output.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

wasi

Environment

  • Wasmtime v50.0.1 (c307ec81de02852d815e9849f7c89d955c03c30d), x86_64 Linux
  • wasi_snapshot_preview1.command.wasm shipped with v50.0.1
  • Current main (fb792bf7a143801c22c0ba5b16fe2c6085a8598b) still passes unchecked byte slices to P2 string parameters, for example at crates/wasi-preview1-component-adapter/src/lib.rs:1781-1787 and 1912-1927.

Summary and root cause

Passing a path containing an invalid UTF-8 byte to path_open and the other path operations traps the guest through the adapter. Direct WASIp1 instead returns an error and lets the application continue.

The adapter constructs a &[u8] and passes it directly to a P2 interface whose Canonical ABI type is string. The runtime rejects the invalid string while lifting the import argument. No explicit UTF-8 validation in the adapter converts this condition into a WASI errno.

Minimal reproducer

The argument selects path_open, path_create_directory, path_unlink_file, path_remove_directory, path_rename, path_filestat_get, or path_symlink (0 through 6).

#include <wasi/api.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    const char bad[] = {'x', (char)0xff, 0};
    __wasi_fd_t fd = -1;
    __wasi_filestat_t stat = {0};
    __wasi_rights_t rights = __WASI_RIGHTS_FD_READ | __WASI_RIGHTS_FD_WRITE
                           | __WASI_RIGHTS_PATH_OPEN;
    __wasi_errno_t e;
    int which = argc > 1 ? atoi(argv[1]) : 0;
    switch (which) {
    case 0: e = __wasi_path_open(3, 0, bad, 0, rights, rights, 0, &fd); break;
    case 1: e = __wasi_path_create_directory(3, bad); break;
    case 2: e = __wasi_path_unlink_file(3, bad); break;
    case 3: e = __wasi_path_remove_directory(3, bad); break;
    case 4: e = __wasi_path_rename(3, bad, 3, "new"); break;
    case 5: e = __wasi_path_filestat_get(3, 0, bad, &stat); break;
    default: e = __wasi_path_symlink(bad, 3, "new"); break;
    }
    printf("operation=%d errno=%u\n", which, e);
    return 0;
}
"$WASI_SDK_PATH/bin/clang" --target=wasm32-wasip1 -O1 invalid_utf8.c -o invalid_utf8.p1.wasm
wasm-tools component new invalid_utf8.p1.wasm \
  --adapt wasi_snapshot_preview1=wasi_snapshot_preview1.command.wasm \
  -o invalid_utf8.adapter.wasm
mkdir -p fs
wasmtime run -S preview2=y -S inherit-env=n --dir fs::. invalid_utf8.p1.wasm 0
wasmtime run -S preview2=y -S inherit-env=n --dir fs::. invalid_utf8.adapter.wasm 0

Actual output

Direct WASIp1 exits successfully and prints:

operation=0 errno=25

Adapter P2 exits with a guest Wasm trap. The terminal cause is:

invalid utf-8 sequence of 1 bytes from index 1

The seven operations in this reproducer plus path_link, path_readlink, and path_filestat_set_times were each tested three times. Direct WASIp1 returned EILSEQ (errno=25, __WASI_ERRNO_ILSEQ) in every run; each adapter run trapped with the same UTF-8 conversion error.

Expected output

The adapter should reject the invalid path with EILSEQ (__WASI_ERRNO_ILSEQ) rather than trapping the guest, matching both Direct WASIp1 and the wasi-libc P2 backend. This is a correctness and robustness report, not a security report.

Suggested fix

Validate path bytes with str::from_utf8 before crossing the P2 string boundary and return an appropriate WASI error on failure. Apply the same handling consistently to path parameters and symlink targets.

Lenguaje dominante
Rust
Estrellas
18.9k
Forks
2k
Merge medio
1 d 6 h
PR fusionados (30 d)
238

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de bytecodealliance/wasmtime

Todos los issues de bytecodealliance/wasmtime

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.