Skip to content

executor/builtins: cd and shell-side redirects spend the cwd anchor from a deleted cwd - #3283

Merged
caomengxuan666 merged 7 commits into
masterfrom
fix-3268-bc
Nov 10, 2026
Merged

caomengxuan666 merged 7 commits into
masterfrom
fix-3268-bc

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3268

Scope

This PR covers faces (b) and (c) of #3268 only:

  • (b) the cd builtin's own move out of a deleted cwd (cd ..,
    cd -L .., cd -P .., cd ../a2, repeated cd ..).
  • (c) shell-side redirection opens from a deleted cwd: input,
    output, append and read/write targets across the open ladders,
    the rejection gates' validation probes, the compound fd-0 bind,
    the read builtin's target funnel, and the text channel's
    slurp — including the composite cell x=$(cat <../f) that the
    (a)(d)(e) design lane assigned to this face.

Faces (a), (d) and (e) are separate lanes and are not addressed
here. The deleted-then-recreated ("ghost") shape is #3275's
scope; this PR's consumers reuse the shared anchor gate and
neither add nor remove ghost behavior, and the pin deliberately
carries no ghost cell. #2796's symlink face and #2798's pwd face
are untouched (the design lane's scope check; the one shared
evidence cell, deleted-cwd cd .. in #2796's list, is this
face's headline and is covered here on its own mechanism).

Mechanism

GNU owners: change_to_directory (builtins/cd.def:522) —
canonicalized chdir first (sh_canonpath, lib/sh/pathcanon.c,
with PATH_CHECKEXISTS | PATH_CHECKDOTDOT, so the deleted
component itself fails it), then the verbatim chdir of the
operand (POSIX step 9 tail) against the process cwd inode;
do_redirections (redir.c:237) opens redirection targets in
the forked child (execute_cmd.c execute_disk_command), whose
inherited cwd is that same inode. Both resolve relative to an
inode that survives unlinking.

Under G-02 the session cwd is a field: cd probed
canonicalize(cwd + operand) as a string, and the shell-side
redirect opens joined the target onto the same string, so both
died at the deleted component. This PR spends the existing cwd
anchor (the #3198 family's open handle) under its two-state
rule — relative filesystem operand, the session cwd's path no
longer resolves, the anchor naming exactly that cwd:

  • cd: the executor call sites lend execute_with_io an anchor
    resolver; when the joined-string probe fails for a relative
    operand, the anchor walk (openat O_DIRECTORY from the anchor
    handle) supplies the physical directory and the shared tail
    commits the move exactly as for the probed route; the walk's
    own error is the reported verdict when it fails.
  • Redirects: anchor arms at the shell-process open points of a
    relative target — open_input_redirect,
    probe_input_redirect_readwrite, create_redirect_output,
    open_append_target, the rejection gates in redirection.rs
    and command_input_scope.rs (via one shared
    anchored_redirect_probe), the compound fd-0 stream bind
    (fstatat classification, openat acquisition), the read
    builtin's target funnel, and the text channel's one slurp
    open. rubash#1820's FIFO probe discipline is preserved on the
    anchor route (fstatat classification, faccessat validation,
    never a probe open of a FIFO); the empty word never takes the
    anchor route.

Absolute spellings through the dead directory keep the plain
string verdict on both faces — oracle-verified, and pinned as
controls (cd $PWD/.., cat $PWD/../f).

What this PR deletes

No file, code path, or test is deleted. The change is additive
anchor arms on existing funnels plus one signature widening:
cd::execute_with_io gains an anchor_resolver parameter
(the standalone cd::execute entry and the two in-file unit
tests pass None). The joined-path opens remain the verdict
in every state where the anchor does not apply.
9 files changed, +1066/−34 against the base below.

Evidence

Base: 5e0a4e14 (master at branch time). Oracle: GNU bash
5.3.20 (~/workspace/rubash-audit/gnu-bash-5.3.20/bin/bash),
per-cell probes with the deleted cwd staged by
cd a; (cd ..; rmdir a); every pin expectation is an oracle
measurement.

Pin tests/issue3268_deleted_cwd_cd_redirect.rs — 19
completion-form cells (assertions are output bytes + status,
never timing; Unix-only because the anchor is a Unix handle):
19 passed; 0 failed on the head tree; on the base tree the
fail-first red set is exactly the defect cells
(5 passed / 14 failed), the controls stay green on both.

Neighbour pins on the head tree: issue3198 7 passed / 0 failed,
issue3201 5 passed / 0 failed. Lib suite: head
1230 passed; 0 failed; 4 ignored, base 1230 passed; 0 failed; 4 ignored — failure name
sets identical (difference empty). RUSTFLAGS="-D warnings"
check (lib + bins) rc=0; cargo check --tests rc=0;
cargo fmt --check rc=0; check-marker-literals.py rc=0;
check-utf8-boundary-hygiene.sh clean.

Known residual, deliberately not pinned: cd . from a deleted
cwd — GNU succeeds with a getcwd diagnostic on stderr and
$PWD spelled D/a/. (the change_to_directory resetpwd
residue); this PR succeeds with $PWD kept at D/a and no
diagnostic. The diagnostic half belongs to the
getcwd-reporting family next to #2798, not to this face; the
pin asserts the verdict and continued function only.

…rom a deleted cwd (rubash#3268 (b)(c))

GNU: change_to_directory (builtins/cd.def:522) tries the
canonicalized path first (sh_canonpath, lib/sh/pathcanon.c, with
PATH_CHECKEXISTS | PATH_CHECKDOTDOT — the deleted component itself
makes it fail) and then chdirs the operand VERBATIM against the
process cwd inode; do_redirections (redir.c:237) opens redirection
targets in the forked child (execute_cmd.c execute_disk_command),
whose inherited cwd is that same inode. Under G-02 rubash's session
cwd is a field: cd probed the joined string, and every shell-side
redirect open used the joined string too, so relative spellings
died at the deleted component while a spawned child resolved them.

(b) cd: the executor call sites lend execute_with_io a cwd-anchor
resolver. When the joined-string probe fails for a RELATIVE
operand, the anchor walk (openat O_DIRECTORY from the anchor
handle) supplies the move's physical directory, committed by the
same shared tail as the probed route; when the walk itself fails,
its error is the reported verdict. An absolute spelling never
consults the resolver — GNU's verbatim chdir of it dies at the
same dead component, and so does the probe.

(c) redirects: the cwd anchor family's two-state rule (relative
filesystem operand, the session cwd's path no longer resolves, the
anchor naming exactly that cwd — the rule the spawn stamp and the
emulated-cat operand read already spend, rubash#3198) now also
covers the shell-process redirect open points: the
open_input_redirect / probe_input_redirect_readwrite /
create_redirect_output / open_append_target ladders, the rejection
gates' validation probes (redirection.rs,
command_input_scope.rs), the compound fd-0 stream bind
(classification by fstatat, the bind's one open by openat), the
read builtin's target funnel, and the text channel's slurp.
rubash#1820's FIFO probe discipline is preserved on the anchor
route: probes classify by fstatat and validate FIFOs by faccessat,
never by taking a rendezvous. The empty word never takes the
anchor route (GNU's open of "" fails in the child).

Pin: tests/issue3268_deleted_cwd_cd_redirect.rs — 19
completion-form cells, every expectation measured against the
GNU bash 5.3.20 oracle: both cd modes and the sibling walk, the
absolute-spelling controls, input/output/append/readwrite
redirects, the read builtin, a compound loop, and the
x=$(cat <../f) composite cell.

Refs #3268
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 10, 2026 19:48
@caomengxuan666
caomengxuan666 marked this pull request as draft November 10, 2026 19:56
Merge round 1 stopped at CI: the new anchor code assumed host libc
widths. darwin types mode_t as u16, which the variadic libc::openat
cannot take (E0617); on armv7-android the S_IF* constants and
st_mode disagree in width, so the raw st_mode & S_IFMT comparisons
do not compile there (E0308/E0277). Host and aarch64 widths agree,
so every local leg stayed green.

- open_via_cwd_anchor: pass the variadic mode as c_uint (the
  identity where mode_t is already u32).
- All five fstatat_via_cwd_anchor classification sites: widen
  through u32 on both sides of the comparison, the form already
  used by fd_is_anonymous_pipe / pipe_identity in external_inner.rs.

No semantic change on any target: the widened comparisons test the
same bits, and linux x86_64/aarch64 see identity casts throughout.
…entity verdict

anchor_fd_for_dead_cwd asked refresh_cwd_anchor_for — the ADOPT
refresh owned by the cd funnels — whether the session cwd still
resolves, and cd_target_via_cwd_anchor asked a bare metadata()
check. Both predate the identity gate #3281 put on the spawn
stamp and the anchored read. In the deleted-then-recreated
(ghost) state the refresh answers true (and re-anchors onto the
ghost), and the metadata check succeeds on the ghost's spelling,
so the (b)/(c) consumers fell back to string resolution into the
recreated directory while the stamp and the read spend the
anchor.

Both gates now ask the identity question.
anchor_fd_for_dead_cwd asks session_cwd_path_resolves directly.
cd_target_via_cwd_anchor is a free function over the anchor cell
with no &self to ask, so it applies the same (dev, ino)
comparison against the anchor handle's own identity, falling
back to the plain resolvability check when that identity is
unreadable — the same fallback the identity verdict takes when
no anchor names the cwd.

Only the source of the resolvability verdict changes: the
two-state rule, the rubash#1820 FIFO probe discipline, and every
anchored walk are untouched.

Merge-order remainder of the #3268 (c) face: #3281 reached
master first (bc049e1), so the convergence the captain review
booked lands in this merge round.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 10, 2026 22:10
@caomengxuan666
caomengxuan666 merged commit 36590c5 into master Nov 10, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
… groups and procsub seeds (#3297)

Merge `refs/pull/3297/head` (`76ee0dfe`) — capture-span relative-line recovery for folded brace groups and procsub seeds. Refs #3210.

## What it fixes

- `{...}` groups containing a line continuation are no longer folded into a single Keyword token: when a continuation join falls strictly inside the group's span, the lexer emits a bare `{` and the body is re-parsed from feeder-stamped tokens. Function bodies, bare groups, for/select/C-for bodies and coproc bodies all take this path.
- Procsub spawn sites now seed the body line from the first element's end line (`procsub_body_seed_line`: `simple_command_first_word_end_line(cmd).or(cmd.line)`), at all 7 spawn sites (trap, external setup, read io, input scope, redirected fd). A split outer word (`ec\<NL>ho done > >(foo\<NL>bar)`) also reports the end line now.

## Verification (merge-round re-run on the Simular box, base `36590c5f`)

- Head pin `issue3210_procsub_body_split_word_end_line`: **32/0**; the comsub expectation (rc 127) was re-measured against the self-built GNU 5.3.20 oracle, all 32 cells byte-identical with GNU (stdout/stderr/rc).
- Fail-first on the base tree (full 32-cell pin copied into the base tree): **10 passed / 22 failed** — the 22 reds are exactly the defect cells; the 10 greens are the controls.
- Neighbours: issue3053 20/0, issue3054 12/0, issue1184 7/0, issue2332 12/0, issue2202 10/0, issue3028 23/0.
- pin-3209: 22/2 on head and on base, same two cells (`background_first`, `coproc_first`); lib bilateral 1233 passed / 0 failed / 4 ignored on both trees (built `--bin rubash` first), failure-name-set diff empty.
- `RUSTFLAGS="-D warnings"` (flag load proven: 2 flagged rustc invocations, lib + bin), `cargo fmt --check`, marker-lint, `cargo check --tests`: all rc=0. UTF-8 boundary hygiene clean.
- PR CI: 10 pass + 1 skipping, zero red; canary fully green.

## Notes for the record

- `background_first` (pin-3209) is accepted as a pre-existing environment-sensitive cell, not a regression of this change: head and base report the identical line 2 on the box (5/5 no-sleep, pin harness), and both report line 1 with `sleep 1` and locally. The pin expectation (line 1, GNU) is correct. The timing behaviour is filed separately as rubash #3300.
- `coproc_first` is the pre-registered #3267 residual; #3267 stays open.
- Residuals filed before merge (review §5): composite-stdin procsub third mechanism (GNU 2 / head 1) as rubash #3299, `background_first` timing as rubash #3300, coproc folded body (GNU 2 / head 1, pre-existing) as rubash #3301; all three OPEN.
- Merge history: update-branch ran twice in this round (first to master `3a93856b`, then to master `36590c5f` after PR #3283 merged); the final head was re-run in full after the second update-branch. This PR shares `src/executor/command_input_scope.rs` and `src/executor/read_io.rs` with the merged #3283.

## Deleted

Nothing.
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
…ed / (rubash#3268 (e)) (#3298)

Refs #3268

(e) 启动面:已删 cwd 里出生的会话保有其 inode,不再被伪造的 `/` 移植。父进程坐在已删目录 inode 里拉起的 rubash 此前整场被移植到根目录:`pwd` 印 `/` rc 0、`$PWD` 被种为 `/`、子进程被盖戳进 `/`、`ls ..` 列根目录。

## 机制

GNU 在启动期不持有 cwd 字符串状态——进程 inode 即状态;variables.c:907 `set_pwd()` 只种 `$PWD`(导入 PWD 仅当绝对且 `same_file(PWD, ".")` 时受信,否则走 builtins/common.c:592 的 `get_working_directory("shell-init")`,失败则 PWD 不绑定)。本 PR 让出生期服从与运行期同一条两态规则:

- 出生 cwd 在 `Executor::new` 只捕获一次,同一值喂 env 种子与 ShellState 字段(此前两处独立 `current_dir()` 可跨 getcwd 边界互相不一致)。成功路零变化:字段 = 路径、PWD 照旧、锚保持惰性,respawn 热路径不新增 open。
- 捕获失败时字段取空路径——诚实的「未知」(永不解析、永不与真实目录相等)——出生锚以 `File::open(".")` 装入并命名该字段,两态机制(`stamp_spawn_cwd` 的 fchdir、`read_session_anchored` 的 openat)自此接管;首次成功 `cd` 自然退出异常态。
- PWD 种子在未知态不写:GNU 的 set_pwd 在 getcwd 失败时不重绑——有导入则保留陈值、无导入则保持未绑定,两种形态各有钉格锁定(此点为对 DESIGN §5.2.3 的源码订正,队长复核已亲读 variables.c 与 oracle 实测确认)。
- 新生(非 respawn)出生恰印一行 `shell-init:` 警告(strerror 文本经 `libc::strerror`,与 common.c 的格式逐字同构);respawn 子进程静默(GNU 的 fork 子不重跑启动);GNU 的逐命令 `job-working-directory:` 重复按队长裁定不跟。
- `pwd` 在空字段下双模式皆失败:stdout 空、`pwd:` 同式一行、rc 1(errno 文本为出生失败的 ENOENT 实测形;EACCES 出生失败同印此文本,为已注明的近似——本环境内核 getcwd 在父目录 chmod 000 下仍成功,该形态不可构造实测)。
- E6 respawn:`WhitelistedSpawn` 新增 `cwd_anchor_fd`(Windows 侧 parity 接收并忽略),不可解析时 spawner 在 fork 与 exec 之间 fchdir 进会话锚 inode——此前 async 子进程根本不盖戳、生在宿主进程 cwd;validated respawn 在出生 getcwd 失败时采信运输 PWD 为字段(GNU fork 子原样继承 `the_current_working_directory`),采信由 respawn 出身验证门把守、且 #3275 身份门保证该串永不冒充解析结果,环境 PWD 永不采信。

## 本 PR 删除了什么

- `Executor::new` 里两处 `unwrap_or_else(|_| PathBuf::from("/"))` 出生回退(env 种子与 ShellState 字面量)——伪造本身。
- `initialize_fresh_shell_env_vars` 的无条件 PWD 写入(改按已知 cwd 条件写入)。
- fd/unix.rs 里「respawned child 从运输状态重导会话 cwd」的失实旧注释(运输层并无 cwd 键),替换为真实机制。

## 队长复核(`~/workspace/review-3298/CAPTAIN-VERIFY.md`,全部独立复跑)

- 头钉 11 过 / 0 红 / 1 ignore;ignore 格显式运行 1 过(归因见卷宗:绿来自空字段裸相对落出生 inode 的独立窗口,非锚定 serve——(a) 的收敛仍属必需,该格按预登记保持 `#[ignore]` 至 (a) 合入)。基底 fail-first 3 过 / 8 红 / 1 ignore,8 红逐名恰为修法目标格、3 过恰为对等锁格(abs_dead/recover/normal_birth);ignore 格基底显式运行亦红。
- 邻茎五茎各有独立 `test result: ok`(issue3198 7、issue3201 5、issue3275 4、issue2526 两茎 4 与 3);lib 双边先建 `--bin rubash` 后同为 1233 过 / 0 红 / 4 ignore,失败名集 ∅=∅。
- `RUSTFLAGS="-D warnings"` 本 PR 目标面(lib+bins+新钉,touch + `-v` 亲证真载)rc=0;`--all-targets` rc=101 的 error 全在未被本 diff 触碰的既存测试文件(#2631 在册的 --tests 警告债),与本 PR 零交集。fmt `--check`、marker-lint(117 注册常量、267 allowlisted sites)、UTF-8 边界卫生三腿 rc=0;armv7-linux-androideabi `cargo check --tests`(NDK r26d 配方)rc=0;darwin 以本 PR 自身 CI 为凭(Cross-target check (darwin) success)。
- PR CI 终局 10 success + 1 skipping、零红(canary 全绿);merge-tree 对 master `2ae82963` 零冲突、文件集恰 8。
- 合入轮(终头 `9ec2f1a7`,三轮 update-branch:master 经 `3a93856b`、`36590c5f`(#3283)、`2ae82963`(#3297)推进):终头全腿 box 复跑——头钉 11/0/1、ignore 格显式 1 过、邻茎 7/5/4/4/3、lib 1233/0/4 与基底失败名集差 ∅、`-D warnings` 目标面 rc=0(touch + `-v` 亲证带 flag 调用)、`--all-targets` error 文件集与基底逐字相同(16 个既存他茎文件,#2631)、fmt/marker/UTF-8 三腿 rc=0、armv7-linux-androideabi `cargo check --tests`(NDK r26d)rc=0、darwin 以终头 CI 的 Cross-target check (darwin) 与 macOS tests 为凭(success);终头 CI 10 success + 1 skipping 零红、canary 全绿。基底 fail-first 对照(钉拷入基底树)3 过 / 8 红 / 1 ignore、ignore 格基底显式运行亦红,与复核账一致。
- 硬门普查:头/基底两树 `.session_path(` 消费调用点共 86 处,逐文件计数与逐行抽查(trap_exec/compound_exec/shell_options/external_file_builtins/command_input_scope 五行全对)与车道普查表全合——重定向 60 归 (c)、模拟命令 8 归 (d) 与 #3276、comsub 2 归 (a),H 残窗与 G-H 残窗均按已知残窗诚实登记,无一冒充已覆盖。车道报告的标题总数「87」多计一处(path.rs:5273 系含 session_path 字样的测试名、非调用点),卷宗已订正,不影响覆盖完备性。
- 三方探针(GNU 5.3.20 / 基底 / 头树):出生各形(cat、pwd 未绑定、pwd 陈值导入、-P/-L、respawn 静默、comsub 快捷形)头树与 GNU 逐项一致(`job-working-directory:` 重复除外,按裁定不跟);运行期已删 cwd 的 `pwd -P` 印字符串一格头树与基底同(属已立案的 #2798 面,本 PR 不动)。

本 PR 只及五面中的 (e) 面,父单保持开启。
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