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

未关闭
#15,426 5 条评论 0 个 reaction 已指派 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 小时
30 天内合并 PR
238

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bytecodealliance/wasmtime 的其他 Issue

查看 bytecodealliance/wasmtime 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。