Repository navigation
fix(builtins,executor): #866/#867 — unforgeable background-wrapper handshake + expected-parent PDEATHSIG race check - #873
Merged
Merged
Conversation
…shake + expected-parent PDEATHSIG race check #866: __RUBASH_BACKGROUND_WRAPPER was a plain user-guessable env var (constant "1") inserted by the wrapper spawn path and re-checked verbatim by child_pre_exec_reset, so 'export __RUBASH_BACKGROUND_WRAPPER=1' imposed PDEATHSIG semantics on every child the shell spawned afterwards — third- party programs included. The marker is now an unguessable spawn-time nonce (SplitMix64 over clock/pid/counter/ASLR anchor) carried by TWO handshake channels — the env key and a consumed --internal-bg-wrapper=<nonce> argv flag (main.rs, before any parser); the wrapper role arms in-process only when the channels agree, and the env key is scrubbed at startup. The role check is an exact predicate swap: pre_exec runs in the forked child BEFORE execve, so the old env::var probe always read the forking PARENT's environ — 'parent env has the marker' and 'parent holds the in-process role' decide every spawn identically in clean environments (the main shell never arms, so a wrapper survives its shell's exit as GNU's background jobs do; a wrapper arms every child it spawns — #737 contract), while forged values arm nothing. #867: the fork-race check compared getppid() against the constant 1, which a healthy child of a namespace's PID 1 matches before any race and deterministically self-SIGKILLed at spawn (repro: unshare --pid --fork with the forged marker -> every background job rc=137 at creation). The expected parent pid (the wrapper's own pid at handshake time) is now captured in-process and the child self-kills only when getppid() DIVERGES from it. Probes (WSL, GNU bash 5.3.0 oracle): forged-env child killed at shell exit pre-fix / survives post-fix; forged+namespace child killed at spawn (pid 3, rc=137) pre-fix / survives post-fix; kill -l/kill wording/rc byte-identical to GNU pre and post; kill -9 on a wrapper still takes its child (#737). Catalog case issue-866 (must-match).
… — ExitStatusExt import caught by the WSL lane
Member
Author
独立评审:APPROVE-WITH-NITS评审对象:PR #873(fix/866-867 @ 99b8c6e,base master@bd24cf8b)。自建 detached worktree 独立复跑,未触碰他人 worktree。 BLOCKER:无。 安全审计(攻击者思维,逐项)
语义对等独立复核
回归(全部自跑)
NITS(非阻塞)
验证声明核对实现代理 5 项主张中:#866 双通道 nonce、#867 expected-pid、对等论证、对抗探针、验证清单——逐项复跑均属实(唯一出入:全量失败计数其称 19、我测 11,属 lane 范围差异;base==head 集合精确相同才是关键,已证)。 结论:APPROVE-WITH-NITS(无 BLOCKER;nit 1 建议随本 PR 或紧随 hygiene PR 修掉) |
# Conflicts: # scripts/regression-catalog/manifest.tsv
This was referenced Oct 19, 2026
# Conflicts: # scripts/regression-catalog/manifest.tsv
This was referenced Oct 19, 2026
Closed
Member
Author
caomengxuan666
added a commit
that referenced
this pull request
Oct 20, 2026
…e argv flag only when the host CLI consumes it (#906) The --internal-bg-wrapper=<nonce> channel is niu-CLI-specific. The wrapper spawn site attached it unconditionally, so any host that never consumes the handshake received an unknown option in its respawned argv: the niubash launcher (which embeds this engine) answers with the GNU-aligned usage error — bash: --internal-bg-wrapper=<nonce>: invalid option + usage, rc 2 — and every wrapper background command died at spawn (niubash master CI, tests/background_spawn_args.rs 2 red). A foreign bash face would reject it the same way. Gate the attach on a process-level consumer bit: consume_background_wrapper_handshake now marks the CLI at its very top (rubash main.rs calls it at every startup; adopting hosts make the same one call — niubash does, in the companion launcher commit), and the spawn site splices the flag only when background_wrapper_flag_consumer_present() is set. Non-consuming hosts get env-nonce-only respawns: the env key is inert for a non-consumer (never read; any rubash that does start scrubs it) and a single channel can never arm, so the #866 forgery contract is unchanged; #867's expected-parent check and the #737 cascade are untouched for consuming hosts. cfg(test) keeps the attach unconditional in this crate's unit tests: the harness process never runs main.rs, so without it the attach would be order-dependent on the handshake unit tests; there the respawn target is always this repo's own engine binary (CARGO_BIN_EXE / test_rubash_binary_from_current_exe), a consumer by construction. Verified: WSL lib 634/634 (incl. the new consumer-bit unit test), tests issue866 4/4 (W1 #737 cascade intact through the real binary) + issue867 2/2 (unshare namespace probes), Windows family issue294/382/549/550 28/28, catalog issue-866 match + issue-737 still diverges, fmt clean, -D warnings clean for lib+bins; niubash companion branch flips the two CI-red tests green against this tree.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #866. Closes #867.
What was wrong
#866 —
compound_exec.rsinserted the plain constant__RUBASH_BACKGROUND_WRAPPER=1into the wrapper's env andkill.rs child_pre_exec_reset()re-checked that exact value, so any script doingexport __RUBASH_BACKGROUND_WRAPPER=1imposed the wrapper'sPR_SET_PDEATHSIGsemantics on every child the shell spawned afterwards (third-party programs included). Same reserved-namespace collision class as #735.#867 — the PDEATHSIG fork-race patch compared
getppid()against the constant1. Inside a PID namespace a healthy child of the namespace's PID 1 observesgetppid() == 1before any race is possible and deterministically SIGKILLed itself at spawn (unshare --pid --fork+ forged marker: every background job died at creation,rc=137at pid 3).The fix
SplitMix64over clock nanos / pid / process-local counter / ASLR stack anchor — 128 bits, no RNG crate) carried by two handshake channels: the env key and a consumed--internal-bg-wrapper=<nonce>argv flag (main.rs, before any parser, same consume-on-read face asNIU_INVOKED_AS/--internal-*). The wrapper role arms in-process (AtomicBoolstatic in kill.rs) only when both channels agree; the env key is scrubbed at startup either way. A forgedexportarms nothing and leaks nowhere.getppid()diverges from it — never against the constant1.pre_execruns in the forked child beforeexecve, so the oldenv::varprobe always read the forking parent's environ — the check was always a parent-state predicate. Swapping "parent env has the marker" for "parent holds the in-process role" keeps every spawn's arming decision identical in clean environments: the main shell never arms (a wrapper survives its shell's exit, as GNU background jobs do), a wrapper arms every child it spawns ([S9] process ownership: kill does not terminate background shell children (and wait fabricates 128+n); no SIGPIPE for builtin writers #737 contract), nested wrappers arm exactly as before. The only intended divergence is the forged value, which now arms nothing.std::env::var(heap allocation insidepre_exec); it is syscalls + atomic loads only.Adversarial probes (WSL, arm = Linux build, oracle = GNU bash 5.3.0
/usr/local/bin/bash)45f537ea/e6a54457)export __RUBASH_BACKGROUND_WRAPPER=1+ direct-pathsh -c 'sleep 2; echo SURVIVED' &, shell exitsSURVIVEDSURVIVEDunshare --pid --fork env __RUBASH_BACKGROUND_WRAPPER=1 rubash -c 'sh -c "sleep 2; echo ALIVE" & wait'Killed … rc=137(self-kill at spawn, ns pid 3)ALIVE rc=0ALIVE rc=0kill -9on a wrapper-path jobkill -l 15,kill -l TERM,kill 999999,kill -0 999999,kill %nosuch)Regression pins
tests/issue866_background_wrapper_nonce_handshake_regressions.rs— clean-env wrapper job (W0), [S9] process ownership: kill does not terminate background shell children (and wait fabricates 128+n); no SIGPIPE for builtin writers #737 kill-the-wrapper contract (W1, unix), forged-env direct-child survival (W2, linux — fails on pre-fix), forged-env wrapper job undisturbed (W3). Windows side: W0/W3 green (W1's PDEATHSIG contract is Linux-delivered; the pre-fix master Windows binary shows the same ORPHAN-SURVIVED, its [S9] process ownership: kill does not terminate background shell children (and wait fabricates 128+n); no SIGPIPE for builtin writers #737 coverage rides job-object tracking).tests/issue867_expected_parent_regressions.rs(linux) — namespace probes L1 (clean, wrapper-path job survives as ns-PID-1 child) and L2 (forged compound repro).builtins/kill.rsunit modules — handshake matrix (env-only / argv-only / mismatch / agree: arms, captures expected pid, consumes both) and the expected-vs-observed race check driven through the real statics + realpre_exec(correct parent survives; wrong expected pid self-SIGKILLs).scripts/regression-catalog/cases/issue-866.sh+ manifest row (must-match) — runner verdict: match (issue-737 stays at its declared known divergence, Post-merge review triage of #790/#789/#791: user-reachable wrapper flag, getppid() race in PID namespaces, carrier-blind quote simulation #794 wording).Verification
RUSTFLAGS="-D warnings" cargo check --lib --bins+ the two new test targets clean (the pre-existing-D warningsfailures inissue531/issue621/issue804test targets reproduce on master untouched);cargo check --target x86_64-unknown-linux-gnuclean.--test-threads=1,--no-fail-fast): 3835 passed / 19 failed — cli_tests 13 (missingchelper, passwd-home; env), executor_tests 5 (cd/PWD/-p/-V— identical set fails on master), lib 1 (flaky; clean on immediate rerun, 689/689). Zero new semantic failures.--lib615/615; family files issue294 (4), issue549 (11), issue550 (3), issue866 (4), issue867 (2) all green; kill semantics byte-identical to the 5.3.0 oracle.bd24cf8b) and ready for it.GNU source note
PDEATHSIG arming is rubash-internal #737 machinery — GNU bash spawns background jobs as plain forks (
execute_cmd.c:5562+execute_connection&,execute_in_subshell) and imposes no parent-death signal; the parity obligations pinned here are exactly those: bg children survive shell exit (jobs.cno-op on exit),kill $!wording/rc (jobs.c:3064wait_for /kill.def), unchanged pre/post.