Skip to content

execscript: ENOEXEC binary gate on every face - #3718

Merged
caomengxuan666 merged 3 commits into
masterfrom
devin/enoexec-binary-gate
Nov 16, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
devin/enoexec-binary-gate

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

#3605 execscript cluster: the ENOEXEC binary gate (check_binary_file, general.c:739) must classify an operand by its first 80 bytes before any fallback on every face — three places diverged on macOS, all verified against GNU bash 5.3.20 (/opt/homebrew/bin/bash).

is_enoexec_candidate treated \x7fELF as a kernel-native image everywhere. Darwin's execve refuses ELF with ENOEXEC like any other non-Mach-O file, so a corrupt ELF operand skipped classification and fell into libc's silent /bin/sh spawn substitution — the binary's bytes ran as a script:

$ ./x            # �ELFgarbage
./x: line 1: ELFgarbage: command not found        # rc 127, WRONG
bash: line 1: ./x: cannot execute binary file: Exec format error   # GNU, rc 126

ELF now stays a candidate unless the local kernel accepts it (cfg!(not(target_vendor = "apple"))), so the existing classification refuses it.

exec_format_refusal's binary arm printed bash: <word> bare; GNU's internal_error report (execute_cmd.c:6228) carries the command's line and appends strerror(ENOEXEC). Now error_prefix_for_line + cannot execute binary file: Exec format error — the prolog also honors the real shell name and gnu_errfmt.

execute_direct_shell_script read the operand with fs::read_to_string, surfacing a binary as a UTF-8 decode error. It now applies check_binary_file on the first min(80, len) bytes and reports the entry face's own diagnostic — <file>: <file>: for the same-shell script child ($0 already bound to the filename, shell.c:1659-1692 open_shell_script), <name>: line N: <word>: ... : Exec format error for the ENOEXEC arm. Bytes that pass the gate decode through bytes_to_script_text (rubash#132), never a validity refusal.

Pins (tests/issue3605_enoexec_binary_gate.rs, 3 tests): ELF refused at line 1 and line 2, NUL-in-first-line refused, MZ bytes and plain #!-less text keeping the ENOEXEC script fallback — matching GNU's observed behavior for each.

Companion niubash PR (operand prolog + harness THIS_SH symlink): unixwin/niubash#1023.

Refs #3605

…ry face

GNU execute_cmd.c:6139-6233 shell_execve classifies an ENOEXEC operand
by its first 80 bytes (general.c:739 check_binary_file: ELF magic, or
a NUL in the first line — two under '#!') BEFORE any fallback, and
refuses it via internal_error (execute_cmd.c:6228) with
EX_BINARY_FILE = 126. Three places diverged on macOS:

- is_enoexec_candidate treated \x7fELF as kernel-native everywhere;
  Darwin's execve refuses ELF like any other non-Mach-O file, so a
  corrupt ELF operand skipped classification and fell into libc's
  silent /bin/sh spawn substitution — the binary's bytes ran as a
  script ('./x: line 1: ELFgarbage: command not found', rc 127) where
  GNU refuses: 'bash: line 1: ./x: cannot execute binary file: Exec
  format error'. ELF is now a candidate unless execve accepts it
  (non-Apple).
- exec_format_refusal's binary arm printed 'bash: <word>' bare;
  GNU's internal_error report carries the command line and appends
  strerror(ENOEXEC) — now error_prefix_for_line + 'cannot execute
  binary file: Exec format error'.
- execute_direct_shell_script's read_to_string let a binary operand
  surface as a UTF-8 decode error (or run as carrier-marked text); it
  now applies check_binary_file on the first min(80, len) bytes and
  reports the entry face's own diagnostic — '<file>: <file>:' for the
  same-shell script child (bash already bound to the filename,
  shell.c:1659-1692 open_shell_script), '<name>: line N: <word>: ...
  Exec format error' for the ENOEXEC arm. Bytes that pass decode via
  bytes_to_script_text (rubash#132), never a validity refusal.

Verified against GNU bash 5.3.20 on macOS arm64 (ELF operand line
1/line 2 shapes, NUL-in-first-line, MZ bytes keeping the script
fallback, plain text ENOEXEC still runs). 3 new pins in
tests/issue3605_enoexec_binary_gate.rs.

Refs #3605

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT-u3】PR #3718 — 续作开工
head: 95cba99(gh 实时)
状态:draft / CONFLICTING。

发现与计划:

  1. 冲突在 is_enoexec_candidate(external_inner.rs):本 PR 让 ELF 在 apple 上保持 ENOEXEC 候选(Darwin 内核拒 ELF),master 经 fix(executor): yield parseable sleep to a real spawn on unix (Refs #3488) #3510 返工让 Mach-O 在 macOS 上退出候选(Darwin 内核直接执行 Mach-O)。两条是不同二进制格式的不同平台判据,正确解是合取:!#! && (apple || !ELF) && !(macos && macho)——每种格式只在内核拒绝它的平台是候选。ELF-on-macOS 走 pre-spawn 分类(否则 libc posix_spawn 的 /bin/sh 静默替换会让 ELF 字节按脚本跑成 127);Mach-O-on-Linux 保持候选(GNU post-ENOEXEC 126)。
  2. 新发现缺陷:tests/issue3605_enoexec_binary_gate.rs 用 std::os::unix::fs::PermissionsExt 但无 #![cfg(unix)]——Windows 下该测试 target 编译失败(仓库惯例见 fix(redirect): open output/append FIFO redirects via cwd anchor in bind phase (Refs #3286) #3735 的 #![cfg(unix)])。续作补 file-level cfg。
  3. 解冲突 + cfg 修复后跑全腿(钉自跑 + fail-first + lib 双边 + warnings + 卫生 + CI + GNU 对照),判词按模板给出;不合入。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3718 第1轮 — 建议:FAIL
head: 358488a(= merge c4e1e75 + cfg 钉修复 358488a;原 head 95cba99 + origin/master 5d253cf)base(merge-base): 96802ca 文件: 3 个(gh 口径 +177/-11)
关联 issue: #3605(伞目录,保持 OPEN)

机制核查:

  • merge 冲突解法核过:is_enoexec_candidate 终态 = !#! 且 (apple 或 !ELF) 且 !(macos 且 Mach-O)——shebang 恒排除;ELF 在 Darwin(内核拒)为候选、Linux(内核收真 ELF)非候选;Mach-O 在 macOS 非候选、Linux 候选;MZ 恒候选。与 execute_cmd.c:6126-6260 shell_execve「以本机内核实收为准」一致。
  • external_finish.rs 的 check_binary_file 80 字节门 + bytes_to_script_text 替代 read_to_string,对应 general.c:718-741 / shell.c:1659-1692;两面诊断文案(THIS_SH 子进程双名 vs ENOEXEC fallback 带行号+strerror)与 GNU 5.3.20 实测一致。
  • diff 无顺手改无关面;无桥接层。

腿:

  • 钉自跑:issue3605_enoexec_binary_gate — Windows 0 tests(#![cfg(unix)],预期);Linux head(WSL rustup 1.99.0)2 passed / 1 failed。
  • fail-first:merge-base 为 unix-only 钉,本地 Windows VOID;Linux head 上 elf_operand_is_refused_not_run_as_script 红,CI census(run 43914783242 job 133465889113)同格复现。
  • lib 双边(Windows):head {issue2084, +3 瞬时红串行复跑全绿} / base(5d253cf) {issue2084} / 差集 ∅。
  • -D warnings:cargo check 通过,0 警告。卫生:fmt/marker/utf8 无新增。
  • CI:goldens 红 = 基线同名两格;census 差集 vs master 基线 = 本 PR 钉 elf_operand_is_refused_not_run_as_script(可归因)+ issue1902×2/issue2771/issue371 抖动簇。
  • GNU 对照:WSL GNU bash 5.3.20(1)-release;bash file 面(elf.bin/nulbyte.bin/mz.bin/ok.sh)rc 与判定全一致(诊断前缀差异属 main.rs 既有面、非本 PR);./x(\x7fELFgarbage,chmod +x)GNU Linux 实测 cannot execute binary file: Exec format error rc=126。

F 点:

  • F1: is_enoexec_candidate(src/executor/external_inner.rs ~6528)在非 Apple 平台把所有 \x7fELF 排除出候选,假设「内核会收 ELF」。Linux execve 只收合法 ELF——\x7fELFgarbage 这类坏 ELF 返回 ENOEXEC 后落入 glibc execvp 的 /bin/sh 静默替换,字节被当脚本跑:./x: 1: ELFgarbage: not found rc=127。GNU shell_execve 对真实 ENOEXEC 结果再跑 check_binary_file(execute_cmd.c:6228→general.c:718-741)→ 126。复现:printf '\x7fELFgarbage' > x; chmod +x x; rubash -c './x' → 本 PR head 127 vs GNU 126;本 PR 自己的钉 elf_operand_is_refused_not_run_as_script 在 Linux CI 与本地 WSL 均红(left 127 / right 126)。钉的期望本身与 GNU Linux 一致(已用 WSL GNU 5.3.20 复验),是产品机制缺口。修法方向:候选判定需覆盖「ELF magic 但内核会 ENOEXEC」的情形,或在 spawn 后对真实 ENOEXEC 做 check_binary_file——需队长定夺(前者要引入 ELF 有效性判定,后者动 spawn 路径,均超出本轮小修范围)。

续作记录:

未跑/VOID 的腿:Windows 侧无法产生真 ENOEXEC(无 execve),./x 面结论以 WSL Linux 原生构建为准;macOS 面(ELF 候选分支)本地无 macOS runner,CI Rust tests (macOS) pass 但未见该 stem 单独记录——钉集内 elf_operand 在 macOS 预期绿(CI macOS job 未跑 integration stems,按名核查 VOID)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 17:36
@caomengxuan666
caomengxuan666 merged commit fcc1de5 into master Nov 16, 2026
10 of 12 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