Skip to content

fix(fd): protect symbolic stdio backing fds from persistent placement and pre_exec aliasing (Fixes #4091) - #4245

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/4091-exec-persistent-stdio-save-aliasing
Nov 27, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/4091-exec-persistent-stdio-save-aliasing

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #4091.

GNU C Specification

  • third_party/bash/redir.c:1190 (do_redirection_internal, r_duplicating_output / r_duplicating_input): exec 5>&1 calls dup2(1, 5), making fd 5 an independent descriptor referencing the original stdout open file description.
  • third_party/bash/redir.c:1017 / redir.c:1120 (r_output_direction / r_input_direction): a subsequent exec 1>$F replaces only fd 1; writes to saved fd 5 continue going to original stdout, and exec 1>&5 restores fd 1 to original stdout.

Root Cause

  1. Persistent placement clobbering symbolic stdio backing fds (src/executor/fd_table.rs):
    • FdReadEndpoint::InheritedProcessStdin, FdWriteEndpoint::Stdout, and FdWriteEndpoint::Stderr symbolically resolve through process descriptors 0, 1, and 2 respectively (trap_exec.rs:8678-8691 close_orphaned_process_fd already accounts for this).
    • place_persistent_file and place_persistent_dup_arms checked whether another slot held target only for File / Coproc arms — omitting !entry.closed and the three symbolic stdio endpoints.
    • After exec 5>&1 (leaving slot 5 with FdWriteEndpoint::Stdout), exec 1>$F clobbered kernel fd 1 via dup2($F, 1) and stored FileFd { handle: 1 } in slot 1:
      • Writes to saved fd 5 (echo >&5) resolved Stdout through kernel fd 1 and landed in $F (#4091).
      • Restoring exec 1>&5 retired the outgoing FileFd { handle: 1 }, calling close(1) so all subsequent stdout (echo restored, and issue1902_stdout_stderr_names_follow_their_bindings in Linux CI census) failed EBADF and vanished.
  2. External child pre_exec pinning of symbolic stdio dups (src/executor/external_inner.rs, pipeline_exec.rs, command_substitution_values.rs):
    • AmbientFdPins pinned symbolic stdio endpoints directly from source handle 0/1/2. Because std::process::Command wires the child's stdio before pre_exec runs, dup2(1, 5) in pre_exec duplicated the child's newly redirected fd 1 ($F) onto fd 5 instead of the parent's original stdout.

Summary of Changes

  • src/executor/fd_table.rs:
    • Extracted #[cfg(unix)] fn other_entry_holds_kernel_fd(&self, exclude_fd: u32, target: HANDLE) -> bool and used it in both place_persistent_file and place_persistent_dup_arms. Counts live (!entry.closed) File/Coproc handles AND symbolic InheritedProcessStdin (0), Stdout (1), and Stderr (2).
  • src/executor/external_inner.rs, src/executor/pipeline_exec.rs, src/executor/command_substitution_values.rs:
    • Added AmbientFdPins::push_standard_backing_pin to duplicate symbolic stdio backing handles via F_DUPFD_CLOEXEC (>= 10) before spawn so pre_exec pins the parent's saved descriptor even when the child's fd 0/1/2 was redirected; threaded backing_dups into parent-side cleanup (parent_side_dups) across all spawn paths.
  • tests/issue1902_fd_alias_fact_regressions.rs:
    • Added 5 regression pins inside the existing #1902 domain test crate (no new top-level tests/issue*.rs binary per #4210): separate exec 5>&1; exec 1>$F; ...; exec 1>&5, single-list exec 5>&1 1>$F, stderr twin (exec 6>&2; exec 2>$F), stdin twin (exec 5<&0; exec 0<$F), and Unix external child sh -c 'echo ext-via-5 >&5'. Also gave fixture_path() per-test unique names to eliminate parallel test fixture collisions.

What this PR deletes

  • Deletes the duplicated inline held scans in FdTable::place_persistent_file and FdTable::place_persistent_dup_arms (replaced by shared other_entry_holds_kernel_fd).
  • Deletes direct 0/1/2 source-fd pinning in AmbientFdPins for symbolic stdio endpoints.

Verification

  • Oracle: WSL GNU Bash 5.3.20 (/usr/local/bin/bash) — byte-identical on stdout, stderr, and redirected file contents.
  • WSL Linux:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked — passed
    • cargo test --test issue1902_fd_alias_fact_regressions — 13/13 passed (fixes baseline Linux CI census failure issue1902_stdout_stderr_names_follow_their_bindings + all 5 #4091 pins)
    • cargo test --test issue3938_exec_redirect_integrity — 7/7 passed
    • cargo test --test issue4024_exec_walk_save_stdio_source — 7/7 passed
    • cargo test --test issue4024_exec_walk_save_stdio_graft — 8/8 passed
  • Windows:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked — passed
    • issue1902_fd_alias_fact_regressions (12/12), issue4024_exec_walk_save_stdio_source (7/7), issue4024_exec_walk_save_stdio_graft (8/8) — all passed
  • Hygiene: scripts/check-api-ledger.py, scripts/check-marker-literals.py, and pre-commit scripts/check-rustfmt.py all passed.

@caomengxuan666
caomengxuan666 force-pushed the fix/4091-exec-persistent-stdio-save-aliasing branch from dc47642 to 05d37f5 Compare November 27, 2026 08:40
@caomengxuan666

Copy link
Copy Markdown
Member Author

CAPTAIN-VERIFY(只读复核,head 05d37f50):达合入门(只读,待运行时)。双根因(placement 覆盖符号 backing fd + pre_exec 别名)均正对 #4091,backing dups 回收线程逐点无遗漏;census 8 FAIL + 1 SIGABRT 零新名,目标钉 issue1902_stdout_stderr_names 本轮翻绿、issue1902 二进制 13/13 绿。待补运行时:X299 Windows fd 面实跑 + mac 邻域回归,过后终裁。

@caomengxuan666

Copy link
Copy Markdown
Member Author

CAPTAIN-VERIFY(运行时复验,devin-mac 实跑):达合入门(mac 面),队长合入。issue4091 五新钉全绿、目标钉 issue1902_stdout_stderr_names 由基线确定性红翻绿,fail-first 抽查成立(中和符号 stdio 保护臂五钉全红、还原复绿);邻域 issue3938 三红与 issue1902 leading_zero 红经 base 对照证为 mac 先存面、零回归,issue4024 15/15 绿。本 PR 全部新机制 #[cfg(unix)] 门控、Windows 走既有路径且 CI Windows 编译检查绿,故 Windows fd 面实跑列为合入后跟踪项(X299 队列),不作合入门。注记:基线 census 自此少 issue1902_stdout_stderr_names 一名(修复落地),巡检差分勿误判。

@caomengxuan666
caomengxuan666 merged commit 946a3fc into master Nov 27, 2026
11 of 13 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

CAPTAIN 跟踪闭合(合入后 Windows fd 面):在 master 5113cc4a 树上 X299 实跑 issue1902 文件——Windows 可编译的 12 形全绿(目标钉 + 4 个 issue4091 新钉;第 13 形 cfg(unix) 不编译),符号 stdio 既有路径无回归。本 PR 全部证据面闭合。

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.

[longtail G7] fd save/restore aliasing: writes to a saved fd land in the file fd 1 was redirected to (exec 5>&1, 1>file, 1>&5)

1 participant