Skip to content

fix(fd): land dup redirects and >=10 placements, restore scope saves onto the slot (Refs #3938) - #3957

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3938-exec-redirect-integrity
Nov 19, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3938-exec-redirect-integrity

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

fix(fd): land dup redirects and >=10 placements, restore scope saves onto the slot (rubash#3938)

Three residuals of the #3930 placement, fixed through the same
landing mechanism (one dup2 discipline, not per-face patches), all
verified against WSL GNU bash 5.3.20 script-file probes:

Face A - dup redirects materialize. exec 3>&1 was a table-only Rc
alias: kernel descriptor 3 never existed and readlink /proc/$$/fd/3 failed while GNU's duplicating redirector dup2's the
source onto the redirector unconditionally (redir.c:1190, case
r_duplicating_input/r_duplicating_output). New
FdTable::place_persistent_dup_arms runs the same guard suite as
place_persistent_file (arming, adopted-copy eviction, held-scan,
dir/socket refusal, placement undo save, CLOEXEC restore) and lands
the kernel dup2. The caller's clone arms stay: a kernel-backed
source keeps sharing the source's Rc (one open file description),
a symbolic stdio source keeps its marker arm - the rubash#3432
fact marks are minted per endpoint kind, and rewriting a marker
slot to a File arm rewrote [ -e /proc/$$/fd/N ] operands to an
unresolvable path label (measured: the -e verdict flipped). The
known residual: exec 3>&1 > /dev/null; echo hi >&3 still follows
the live stdout through the marker (pre-#3938 behavior, the
Stdout-marker model, unchanged by this lane).

Face B - fd >= SHELL_FD_BASE occupied. exec 12>b when 12 was
already occupied kept the #3213 free-only materialization (refuse
and stay virtual) while GNU's dup2 replaces the occupant
unconditionally (redir.c:1017). The SHELL_FD_BASE ceiling inside
place_persistent_file is lifted; the held-scan still refuses a
number a live {var} allocation owns and the dir/socket refusal
still protects the cwd anchor and the signal socketpair.

Face C - scope restores land the save back on the slot. A scope
save (saved_entry/saved_table, rubash#3706/#3814) duplicates each
kernel-backed endpoint so the bind-time retire cannot kill the
description; replaying it into a slot whose kernel number the
scope's bind vacated used to leave the entry naming the PARKED
duplicate - kernel fd 3 vanished (the compound's own bind closed
it) and the private duplicate leaked until process end
(exec 3>f; { :; } 3</dev/null left an extra /proc descriptor
naming f beside a dead fd 3). The three compound restore points
(numbered input, compound output, dynamic-close snapshots) now run
FdTable::land_restored_entry: kernel fd free -> dup2 the save onto
the number, rebuild the arms on the landed descriptor, close the
parked duplicate once nothing else names it - GNU's undo replay,
a r_move record (redir.c:1399 add_undo_fd_redirect; dup2 at :1190,
source close at :1230). The landing never clobbers: an occupied
slot number keeps the parked shape.

Legs: new pins tests/issue3938_exec_redirect_integrity.rs 7/7
(fail-first verified on the merged master: 5 of 7 red there);
issue3640 9/9; issue1184 f4 7/7; cli fd_redirects 19/19,
dev_fd_operands 21/21, process_substitution 11/11; full lib
1280/1280; rustfmt --check clean; RUSTFLAGS=-Dwarnings clean.
GNU oracle: exec 3>&1, exec 3<&0, exec 12>a; exec 12>b and
the compound/function restore shapes byte-identical on
/proc/$$/fd stable sets (the #3640 cwd anchor at 255 remains the
one documented non-GNU descriptor).

Refs #3917. Fixes #3938.

…onto the slot (rubash#3938)

Three residuals of the #3930 placement, fixed through the same
landing mechanism (one dup2 discipline, not per-face patches), all
verified against WSL GNU bash 5.3.20 script-file probes:

Face A - dup redirects materialize. `exec 3>&1` was a table-only Rc
alias: kernel descriptor 3 never existed and `readlink
/proc/$$/fd/3` failed while GNU's duplicating redirector dup2's the
source onto the redirector unconditionally (redir.c:1190, case
r_duplicating_input/r_duplicating_output). New
FdTable::place_persistent_dup_arms runs the same guard suite as
place_persistent_file (arming, adopted-copy eviction, held-scan,
dir/socket refusal, placement undo save, CLOEXEC restore) and lands
the kernel dup2. The caller's clone arms stay: a kernel-backed
source keeps sharing the source's Rc (one open file description),
a symbolic stdio source keeps its marker arm - the rubash#3432
fact marks are minted per endpoint kind, and rewriting a marker
slot to a File arm rewrote `[ -e /proc/$$/fd/N ]` operands to an
unresolvable path label (measured: the -e verdict flipped). The
known residual: `exec 3>&1 > /dev/null; echo hi >&3` still follows
the live stdout through the marker (pre-#3938 behavior, the
Stdout-marker model, unchanged by this lane).

Face B - fd >= SHELL_FD_BASE occupied. `exec 12>b` when 12 was
already occupied kept the #3213 free-only materialization (refuse
and stay virtual) while GNU's dup2 replaces the occupant
unconditionally (redir.c:1017). The SHELL_FD_BASE ceiling inside
place_persistent_file is lifted; the held-scan still refuses a
number a live {var} allocation owns and the dir/socket refusal
still protects the cwd anchor and the signal socketpair.

Face C - scope restores land the save back on the slot. A scope
save (saved_entry/saved_table, rubash#3706/#3814) duplicates each
kernel-backed endpoint so the bind-time retire cannot kill the
description; replaying it into a slot whose kernel number the
scope's bind vacated used to leave the entry naming the PARKED
duplicate - kernel fd 3 vanished (the compound's own bind closed
it) and the private duplicate leaked until process end
(`exec 3>f; { :; } 3</dev/null` left an extra /proc descriptor
naming f beside a dead fd 3). The three compound restore points
(numbered input, compound output, dynamic-close snapshots) now run
FdTable::land_restored_entry: kernel fd free -> dup2 the save onto
the number, rebuild the arms on the landed descriptor, close the
parked duplicate once nothing else names it - GNU's undo replay,
a r_move record (redir.c:1399 add_undo_fd_redirect; dup2 at :1190,
source close at :1230). The landing never clobbers: an occupied
slot number keeps the parked shape.

Legs: new pins tests/issue3938_exec_redirect_integrity.rs 7/7
(fail-first verified on the merged master: 5 of 7 red there);
issue3640 9/9; issue1184 f4 7/7; cli fd_redirects 19/19,
dev_fd_operands 21/21, process_substitution 11/11; full lib
1280/1280; rustfmt --check clean; RUSTFLAGS=-Dwarnings clean.
GNU oracle: `exec 3>&1`, `exec 3<&0`, `exec 12>a; exec 12>b` and
the compound/function restore shapes byte-identical on
/proc/$$/fd stable sets (the #3640 cwd anchor at 255 remains the
one documented non-GNU descriptor).

Refs #3917. Fixes #3938.
@caomengxuan666
caomengxuan666 merged commit 370155e into master Nov 19, 2026
8 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 19, 2026
…le (Follows #3957)

PR #3957 (the #3938 landing) shipped two regressions its amendment
had already fixed locally but whose push lost the race with the
merge (the amendment 6803396 never landed; the squash went in
from 740f1c1):

- issue2385_x03_readwrite_bind_serves_both_directions: the `<>`
  placement's readwrite wrapper dup2'd the READ arm's description
  over the target number while the complementary O_WRONLY arm's own
  handle sat on that very number - the kept arm answered EBADF. The
  wrapper now refuses the placement when the other arm's own kernel
  handle is the target number (the split-model rubash#2385
  complementary shape); the shared one-Rc pair still places.
- issue3706_readwrite_pair_write_survives_group: the restore
  landing's share scan closed the saved `<>` pair's parked duplicate
  while the entry's own write arm still named it (a saved pair is
  one Rc across the arms). The landing now treats the opposite arm
  as a sharer, lands the shared duplicate once, rebuilds both arms
  on the landed descriptor, and closes the duplicate only when
  nothing else names it.

Verified on the current master tip (2139a7c): issue2385 19/19,
issue3706 12/12, the #3938 pins 7/7 (tests/
issue3938_exec_redirect_integrity.rs), issue3640 9/9,
issue1184-f4 7/7, cli fd_redirects 19/19, dev_fd_operands 21/21,
process_substitution 11/11, rustfmt --check clean,
RUSTFLAGS=-Dwarnings clean.

Follows #3957. Refs #3938.

Co-authored-by: caomengxuan666 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant