Skip to content

fix(childproc): launch overlong shebang lines within the kernel buffer (Refs #1628 #1623) - #1664

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/1628-shebang-buffer-model
Nov 14, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/1628-shebang-buffer-model

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

为什么

#1628 把「首行填满 256 字节无换行」建模成 ENOEXEC(126),并在 C++/Rust
两侧都写进了注释和钉子。GNU 并不是这样:

  • fs/exec.c 把 BINPRM_BUF_SIZE(256)缓冲区清零后只读 255 字节,
    最后 1 字节保持 NUL;
  • fs/binfmt_script.c 在这个 255 字节窗口内解析 shebang 行,超长行在窗口
    边缘被截断后照常启动,不是 ENOEXEC。

所以 #1628 合入的 126 模型是错的:它让一个能跑的脚本在 WinuxCmd 下报 126。
本 PR 把两侧实现都改成「在 255 字节内核窗口内解析」。

改了什么

  • src/utils/childproc.cppm
    • parse_shebang_line 去掉 line_end == body_size && head.size() == kShebangHeadBytes → nullopt;
      解析窗口固定为 kShebangHeadBytes - 1(255)。
    • 注释不再声称内核拒绝超长行;改为「窗口边缘截断后仍启动」,并记录
      BINPRM_BUF_SIZE - 1 的来源。#! 无 interpreter 仍是唯一的 nullopt。
    • 「Pass C 叶名走 PATH」的注释改成明确的已知 accommodation:内核从不
      PATH 搜索 shebang interpreter(#!/nonexistent/interp 即便 interp 在
      PATH 上仍 127)。
  • rust2/crates/winux-cmds/src/childproc.rs:同一语义镜像
    (SHEBANG_LINE_BYTES = HEAD_BYTES - 1),注释同步。
  • tests/unit/childproc/childproc_unit_test.cpp
    • env_shebang_script_overlong_first_line_reports_126 →
      env_shebang_script_overlong_line_still_launches:rc 0,且可选参数恰好
      截到 255 字节窗口(#!./interp.cmd 占 15 字节 → 240 个 x;
      断言 240 个 x 存在、241 个不存在,256 字节模型会多 1 个)。
    • 新增 env_shebang_script_overlong_line_missing_interpreter_reports_127。
    • 裸 #! 的 126 钉子改写为产品 BadFormat 边界(GNU env 的 0 来自 libc
      execvp /bin/sh 回退,raw exec 是 ENOEXEC)。
  • Rust 侧同样替换:shebang_overlong_first_line_still_launches(argv 精确
    钉到 SHEBANG_LINE_BYTES - 10)+ shebang_overlong_line_missing_interpreter_is_not_found。

GNU 实测(WSL Ubuntu,kernel 6.18.33.2,GNU env 9.4)

注意:本机 WSL 的 env 被 atuin shim(/root/.atuin/bin/env)遮蔽,
第一次探测全 rc=0 是假结果;下表全部用 /usr/bin/env 重跑。

形态 GNU 结果
#!/bin/sh,相对 operand 0,script-ok MARKER
绝对路径 operand 0
#!/bin/echo opt-arg 0,opt-arg ./s3.sh MARKER
interpreter 缺失 127
裸 #! env 0(execvp /bin/sh 回退);raw exec ENOEXEC errno 8
#!/bin/echo + 300 x 0,参数精确 243 字节
#!/nonexistent/interp + 300 x 127
LF 在窗口内第 255 字节 0,参数 242 字节
LF 在第 256 字节(窗口外) 0,参数 243 字节(截断)
#!env sh(裸名) 127(内核不 PATH 搜索)

#!/bin/echo 是 12 字节,255 − 12 = 243,与实测一致 —— 这就是 255
字节窗口(而非 256)的直接证据。

本地验证

  • C++ winuxcmd-tests --gtest_filter=childproc.*:27 passed, 0 failed
    (main 是 26 个 TEST;本 PR 替换 1 个 + 新增 1 个)。
  • Rust cargo test -p winux-cmds --locked childproc:14 passed, 0 failed
    (main 13 个 #[test])。
  • 基线已 rebase 到 origin/main(含 fix(xargs): migrate test Ctx literals to Ctx::new after the host seam #1659 的 xargs Ctx::new 修复,否则
    winux-cmds 测试根本编译不过)。
  • 未跑 scripts/format.py:本机没有 clang-format(脚本直接报错退出);
    改动沿用文件现有风格。CI 若有格式门请以 CI 为准。

仍保留的 accommodation(不是内核等价)

  1. 裸 #! → 126(产品 BadFormat 边界;GNU env 的 0 依赖 libc 回退)。
  2. C++ 侧既非 #! 也非 MZ 的常规文件仍交 CreateProcessW → 126;
    Rust 侧是 BadFormat([bug] env 启动 #! 脚本失败(Permission denied/126):未接入 execvp 形 childproc 解析,#!/usr/bin/env 的 git hook 依赖它(niubash#1002) #1623 明确不在范围内)。
  3. POSIX 根路径 interpreter 的 Pass C 叶名走 PATH(Windows 主机没有
    /bin;注释已标注为 accommodation)。
  4. 窗口按 255 字节建模(内核实际做法是 255 字节读入 + 末字节 NUL);
    对可观察行为等价。

Refs #1628 #1623

…r (Refs #1628 #1623)

The merged #1628 modeled an overlong first line as ENOEXEC (126), reading
"the line fills BINPRM_BUF_SIZE with no newline" as "not a script". GNU does
not reject it: exec.c zero-fills a BINPRM_BUF_SIZE (256) buffer but reads
only BINPRM_BUF_SIZE - 1 (255) bytes into it, and binfmt_script.c parses the
line inside that window. Past the window the line is truncated at the edge
and the script still launches.

Parse within the 255-byte window in both implementations. The only
remaining nullopt/None cases are a `#!` with no interpreter token and the
recursion guard. The bare `#!` stays 126 as a product BadFormat boundary:
GNU env reaches 0 only through the libc execvp /bin/sh fallback, while raw
exec gets ENOEXEC.

Pins:
- tests/unit/childproc: the overlong-line 126 pin becomes a positive launch
  pin that asserts the optional argument is cut to exactly the 255-byte
  window (240 x's after "#!./interp.cmd "); a long line whose interpreter is
  missing stays 127. The bare-`#!` pin is reworded as the product boundary.
- rust2 childproc: the same replacement, exact argv pinned to
  SHEBANG_LINE_BYTES - 10, plus the missing-interpreter 127 case.

Probed WSL GNU 6.18 / GNU env 9.4: `#!/bin/echo ` + 300 x's -> rc 0 with a
243-byte argument; `#!/nonexistent/interp` + 300 x's -> 127; bare `#!` ->
env 0 (execvp fallback) vs raw exec ENOEXEC.
@caomengxuan666

Copy link
Copy Markdown
Member Author

CI note: rust2-gate fails with the same signature on main (job 132399216596 in run 43576074813, merged #1628): GNU-gated cases: pass=0 fail=81, every case g_rc=MISSING — the WSL GNU side produced no outputs. Same failure here, same signature, while rust2-surface-check, rust-tests, oracle, baseline, compare, i18n-drift and all four build-windows legs pass. The gate covers csplit/pathchk/ptx, not childproc, so this change does not touch it; it is an existing GNU-side provisioning problem to fix separately.

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 14, 2026 09:45
@caomengxuan666
caomengxuan666 merged commit 7a0873c into main Nov 14, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant