Repository navigation
fix(fd): protect symbolic stdio backing fds from persistent placement and pre_exec aliasing (Fixes #4091) - #4245
Conversation
… and child pre_exec clobbering (Fixes #4091)
dc47642 to
05d37f5
Compare
|
CAPTAIN-VERIFY(只读复核,head |
|
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 一名(修复落地),巡检差分勿误判。 |
|
CAPTAIN 跟踪闭合(合入后 Windows fd 面):在 master |
Fixes #4091.
GNU C Specification
third_party/bash/redir.c:1190(do_redirection_internal,r_duplicating_output/r_duplicating_input):exec 5>&1callsdup2(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 subsequentexec 1>$Freplaces only fd 1; writes to saved fd 5 continue going to original stdout, andexec 1>&5restores fd 1 to original stdout.Root Cause
src/executor/fd_table.rs):FdReadEndpoint::InheritedProcessStdin,FdWriteEndpoint::Stdout, andFdWriteEndpoint::Stderrsymbolically resolve through process descriptors0,1, and2respectively (trap_exec.rs:8678-8691close_orphaned_process_fdalready accounts for this).place_persistent_fileandplace_persistent_dup_armschecked whether another slot heldtargetonly forFile/Coprocarms — omitting!entry.closedand the three symbolic stdio endpoints.exec 5>&1(leaving slot 5 withFdWriteEndpoint::Stdout),exec 1>$Fclobbered kernel fd 1 viadup2($F, 1)and storedFileFd { handle: 1 }in slot 1:echo >&5) resolvedStdoutthrough kernel fd 1 and landed in$F(#4091).exec 1>&5retired the outgoingFileFd { handle: 1 }, callingclose(1)so all subsequent stdout (echo restored, andissue1902_stdout_stderr_names_follow_their_bindingsin Linux CI census) failedEBADFand vanished.src/executor/external_inner.rs,pipeline_exec.rs,command_substitution_values.rs):AmbientFdPinspinned symbolic stdio endpoints directly from source handle0/1/2. Becausestd::process::Commandwires the child's stdio beforepre_execruns,dup2(1, 5)inpre_execduplicated 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:#[cfg(unix)] fn other_entry_holds_kernel_fd(&self, exclude_fd: u32, target: HANDLE) -> booland used it in bothplace_persistent_fileandplace_persistent_dup_arms. Counts live (!entry.closed)File/Coprochandles AND symbolicInheritedProcessStdin(0),Stdout(1), andStderr(2).src/executor/external_inner.rs,src/executor/pipeline_exec.rs,src/executor/command_substitution_values.rs:AmbientFdPins::push_standard_backing_pinto duplicate symbolic stdio backing handles viaF_DUPFD_CLOEXEC(>= 10) before spawn sopre_execpins the parent's saved descriptor even when the child's fd 0/1/2 was redirected; threadedbacking_dupsinto parent-side cleanup (parent_side_dups) across all spawn paths.tests/issue1902_fd_alias_fact_regressions.rs:#1902domain test crate (no new top-leveltests/issue*.rsbinary per#4210): separateexec 5>&1; exec 1>$F; ...; exec 1>&5, single-listexec 5>&1 1>$F, stderr twin (exec 6>&2; exec 2>$F), stdin twin (exec 5<&0; exec 0<$F), and Unix external childsh -c 'echo ext-via-5 >&5'. Also gavefixture_path()per-test unique names to eliminate parallel test fixture collisions.What this PR deletes
heldscans inFdTable::place_persistent_fileandFdTable::place_persistent_dup_arms(replaced by sharedother_entry_holds_kernel_fd).0/1/2source-fd pinning inAmbientFdPinsfor symbolic stdio endpoints.Verification
/usr/local/bin/bash) — byte-identical on stdout, stderr, and redirected file contents.RUSTFLAGS="-D warnings" cargo check --lib --locked— passedcargo test --test issue1902_fd_alias_fact_regressions— 13/13 passed (fixes baseline Linux CI census failureissue1902_stdout_stderr_names_follow_their_bindings+ all 5#4091pins)cargo test --test issue3938_exec_redirect_integrity— 7/7 passedcargo test --test issue4024_exec_walk_save_stdio_source— 7/7 passedcargo test --test issue4024_exec_walk_save_stdio_graft— 8/8 passedRUSTFLAGS="-D warnings" cargo check --lib --locked— passedissue1902_fd_alias_fact_regressions(12/12),issue4024_exec_walk_save_stdio_source(7/7),issue4024_exec_walk_save_stdio_graft(8/8) — all passedscripts/check-api-ledger.py,scripts/check-marker-literals.py, and pre-commitscripts/check-rustfmt.pyall passed.