Skip to content

WIP: fix(fd): mirror governance over File entries (#2170); child /proc fd view must not consume parent FileFd (#2171) - #2180

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2170-2171-fd-mirror
Oct 30, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2170-2171-fd-mirror

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Two fd-transport fixes discovered by the fd-stdin cluster (PR #2167), one commit each. Draft for CI adjudication — the main session closes the issues after verification.

Refs #2170
Refs #2171

#2170 — mirror governance over File entries

After exec 0<file, a compound's text-mirror rebinding ({ ...; } < /dev/null, heredoc, here-string) was shadowed at the transport layer: bind_command_fd0_input's mirror branch staged FUNCTION_STDIN but left the fd-0 fd-table entry in place, so the File entry kept governing the body's readers (builtin read read the old file, children inherited it, facts stated it) while the staged channel claimed the mirror's source. GNU installs the command's own redirection on fd 0 (execute_cmd.c + redir.c:767-955 do_redirection_internal), superseding the enclosing binding for the scope.

Fix, in the mirror-governance domain (no fact-layer patch, no /dev/null/fd-0 special cases): the mirror branch now displaces the fd-0 entry for the scope's duration — every descriptor branch of the bind already replaces the entry and lets CommandFd0InputGuard restore it verbatim; the #2131 fact pass already treated an absent slot as mirror-governed, so facts follow with no fact-layer change. The flat-subshell region feed stages the same channel and gets the same displacement (its cloned fd table is restored wholesale at the boundary).

Pins: tests/issue2170_mirror_governance_over_file_regressions.rs — 8 tests, 6 red on master → green; GNU 5.3.20 script-file probes (11 shapes: /dev/null, heredoc + facts, external child, restore, subshell region, loop, stream-file guard, inherited guard, pipeline guard, function guard) byte-match after the fix.

#2171 — child /proc/self/fd view must not consume the parent's FileFd

After exec 0<dir, a child inspecting /proc/self/fd/0 (ls -l) killed the parent's fd-0 FileFd: later read reported EBADF where GNU reports EISDIR. Root cause (unix branch of the operand materialization): the File arm recorded the parent endpoint's OWN handle in unix_fd_attach, whose record Drop closes every recorded handle after the child exits — a contract that assumes the record owns what it attaches (the other two arms record fresh, owned handles). Fix: the File arm attaches a duplicate (crate::fd::duplicate_handle — dup shares the open file description and its offset, so the child's view is unchanged) and the record closes only its dup.

Collision note for the main session: this is a one-hunk change inside src/executor/dev_fd_operands.rs, which open PR #2156 also touches (#2156's hunks are the drain/finish regions; this hunk is the unix materialize loop's File arm). The borrowed-handle invariant lives only in this file — no mechanism-true fix exists outside it — so the hunk is kept minimal and flagged here for merge sequencing.

Pins: tests/issue2171_proc_fd_operand_survival_regressions.rs — 4 tests, 3 red on master → green (directory EISDIR survival, numbered-fd survival, repeated views), 1 guard green both ways. The Windows materialization never hands the child the parent handle (path/temp based; its consume step seeks, never closes), so the defect and fix are unix-branch only; the read-side EISDIR shape itself is #2101/#2167's and is not touched.

…ntry (rubash#2170)

After `exec 0<file`, a compound command's mirror rebinding
(`{ ...; } < /dev/null`, a heredoc body, a here-string) was broken
at the transport layer: bind_command_fd0_input's mirror branch
staged the FUNCTION_STDIN text but left the fd-0 fd-table entry in
place, so the File entry kept governing every consumer in the body
— the builtin read's fd-table arm read the old file, a spawned
child inherited the file, and the fd-alias facts stated the file —
while the staged channel claimed the mirror's source. The mirror
only ever governed over the ambient InheritedProcessStdin entry.

GNU installs a command's own redirection on fd 0 before it runs
(execute_cmd.c dispatch + redir.c:767-955
do_redirection_internal), superseding the enclosing scope's
binding, and restores it at scope end. Every descriptor branch of
the bind already replaces the fd-table entry and lets
CommandFd0InputGuard restore it; the mirror branch now does the
same: it removes the fd-0 entry for the scope's duration (the
guard's snapshot restores it verbatim). With the slot displaced,
reads take the FUNCTION_STDIN arm, children take the staged
payload, and the #2131 fact pass states the mirror's own fact —
its supersede check already treated an absent slot as
mirror-governed, so the fact layer needs no change. The
flat-subshell region feed (ast_exec) stages the same channel for a
region's readers and gets the same displacement; the region's
cloned fd table is restored wholesale at the boundary.

The rule is about entry kinds in mirror scope generally — no
/dev/null or fd-0 special cases beyond the mirror's own scope.

Pins: tests/issue2170_mirror_governance_over_file_regressions.rs
(8 tests; 6 red on master, 2 guard pins green both ways; GNU bash
5.3.20 script-file probes p2170_a..k byte-match after the fix).

Refs #2170
…ndle (rubash#2171)

After `exec 0<dir`, a child that merely inspected the descriptor
(`ls -l /proc/self/fd/0`) invalidated the parent's fd-0 FileFd:
the next builtin read reported `Bad file descriptor` where GNU
reports the descriptor's own `Is a directory` (read.def:911).
Same death for any File endpoint on any fd (an exec-bound regular
file on fd 3 died identically).

Root cause, in the unix branch of materialize_dev_fd_operands:
the attach record (DevOperandMaterialization) owns what it
records — its Drop closes every recorded handle once the child
exits, like GNU closing the parent's pipe end once the forked
child owns its copy. Two of its three arms record fresh, owned
handles (a capture-pipe read end, a just-opened procsub file).
The File arm recorded the endpoint's OWN handle — the parent's
live descriptor — so the record's drop closed the parent's
FileFd under it; the fd-table entry survived but pointed at a
dead handle.

Fix: the File arm attaches a duplicate
(crate::fd::duplicate_handle). A dup shares the open file
description, offset included, so the child's /proc view and the
#1952 shared-offset read face are unchanged (verified: a child
reading through the operand behaves byte-identically to GNU
before and after); the record now closes only its dup, and the
parent's descriptor survives any number of inspecting children.

Platform shape: the defect and the fix are unix-branch only. The
Windows materialization never hands a child the parent handle —
it rewrites the operand to the endpoint's path or a temp file,
and its deferred consume step seeks (consume_all_text) rather
than closes. The read-side EISDIR shape is #2101/#2167's and is
not touched.

Collision note: this is the cluster's only hunk in
src/executor/dev_fd_operands.rs, which open PR #2156 also edits
(its hunks are the drain/finish regions; this one is the unix
materialize loop's File arm). The borrowed-handle invariant
lives only in this file, so the hunk is minimal and flagged in
the PR body for merge sequencing.

Pins: tests/issue2171_proc_fd_operand_survival_regressions.rs
(4 tests; 3 red on master, 1 guard green both ways).

Refs #2171
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 30, 2026 07:38
@caomengxuan666
caomengxuan666 merged commit 786e121 into master Oct 30, 2026
9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2170-2171-fd-mirror branch October 30, 2026 07:38
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