Repository navigation
fix(childproc): launch overlong shebang lines within the kernel buffer (Refs #1628 #1623) - #1664
Merged
Merged
Conversation
…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.
Member
Author
|
CI note: |
caomengxuan666
marked this pull request as ready for review
November 14, 2026 09:45
This was referenced Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
为什么
#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.cppmparse_shebang_line去掉line_end == body_size && head.size() == kShebangHeadBytes → nullopt;解析窗口固定为
kShebangHeadBytes - 1(255)。BINPRM_BUF_SIZE - 1的来源。#!无 interpreter 仍是唯一的 nullopt。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.cppenv_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 边界(GNUenv的 0 来自 libcexecvp
/bin/sh回退,raw exec 是 ENOEXEC)。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)
#!/bin/sh,相对 operandscript-ok MARKER#!/bin/echo opt-argopt-arg ./s3.sh MARKER#!env0(execvp/bin/sh回退);raw exec ENOEXEC errno 8#!/bin/echo+ 300 x#!/nonexistent/interp+ 300 x#!env sh(裸名)#!/bin/echo是 12 字节,255 − 12 = 243,与实测一致 —— 这就是 255字节窗口(而非 256)的直接证据。
本地验证
winuxcmd-tests --gtest_filter=childproc.*:27 passed, 0 failed(main 是 26 个
TEST;本 PR 替换 1 个 + 新增 1 个)。cargo test -p winux-cmds --locked childproc:14 passed, 0 failed(main 13 个
#[test])。origin/main(含 fix(xargs): migrate test Ctx literals to Ctx::new after the host seam #1659 的 xargsCtx::new修复,否则winux-cmds测试根本编译不过)。scripts/format.py:本机没有clang-format(脚本直接报错退出);改动沿用文件现有风格。CI 若有格式门请以 CI 为准。
仍保留的 accommodation(不是内核等价)
#!→ 126(产品 BadFormat 边界;GNUenv的 0 依赖 libc 回退)。#!也非 MZ 的常规文件仍交CreateProcessW→ 126;Rust 侧是 BadFormat([bug] env 启动 #! 脚本失败(Permission denied/126):未接入 execvp 形 childproc 解析,#!/usr/bin/env 的 git hook 依赖它(niubash#1002) #1623 明确不在范围内)。
/bin;注释已标注为 accommodation)。对可观察行为等价。
Refs #1628 #1623