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

オープン
#15,426 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@NaniToka がすでに取り組んでいます。

2026年11月23日 から。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust, wasm
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
18.9k
フォーク
2k
平均マージ
1日 6時間
マージ済み PR(30日)
238

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bytecodealliance/wasmtime のほかの issue

bytecodealliance/wasmtime の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。