Repository navigation
fix(startup): a shell invoked as rbash starts restricted (#400) - #413
Conversation
56028aa to
5dd00ef
Compare
caomengxuan666
left a comment
There was a problem hiding this comment.
CR verdict: REQUEST CHANGES (posted as comment — this token is the author identity, GitHub refuses self request-changes) — do not merge until the activation-timing finding is addressed
The recognition half is right; the activation-timing half is not, and it is runtime-demonstrable on this branch.
Blocking: the argv0 face activates restriction BEFORE startup files; the -r flag face activates AFTER (and that is the GNU order you cite)
cr-413 shell.rs:429-431 writes set_shell_option("restricted", true) inside Shell::new. But the flag form's write does not land "after Shell::new returns" as the PR body claims — it lands in ShellInvocation::finish_startup (rubash invocation.rs:424: if self.restricted { executor.set_shell_option("restricted", true) }), which Shell::finish_startup() calls at the tail of run_startup_files/run_interactive_startup_rc/source_non_interactive_env — i.e. after the startup files are sourced. The engine's own apply_to_executor_before_startup comment says exactly this: "GNU shell.c:679-744 main saves -r across run_startup_files and activates it afterward through maybe_make_restricted". That window is what lets GNU's restricted shell source its startup files: the read-only walk pins the final values precisely because the files ran before the pin.
Consequence on this branch (all built and run by me, pseudo-terminal; the last two rows need #412 merged, demonstrated on a merge scratch of both branches):
| face | startup file staged | result |
|---|---|---|
rbash.exe -c (this PR) + BASH_ENV |
env file sources? | refused — : <path>: restricted (engine's . slash-path gate, active early) |
niu -r -c + same BASH_ENV |
sourced fine | |
GNU 5.3 rbash -c + same BASH_ENV |
sourced fine (restriction activates after run_startup_files) | |
rbash -l -c (this PR + #412) + .bash_profile |
refused — whole profile chain | |
niu -r -l -c + same profile |
sourced | |
GNU rbash -l -c + same profile |
sourced (probe-confirmed) |
So the two niu restricted faces answer differently on the entire startup-file surface, and the argv0 face — the one this PR adds — is the un-GNU one. NIU_ENV (the agent hook) is refused the same way as BASH_ENV under the argv0 face. The contract table misses this because rows 1-12 probe only post-startup behavior ($-, cd, readonly, command names); no row sources a startup file. The claim "identical to the -r flag form" is falsified by exactly the face #412 (same launch surface, same author) makes prominent.
Suggested fix (small): don't write the option in Shell::new — record invoked_as_restricted (you already thread the flag) and activate it where the flag form activates, i.e. fold it into Shell::finish_startup() alongside startup_invocation.finish_startup(). That pins the same final values, puts both faces in GNU's activation window, and needs no engine change. Then add the missing contract row: rbash + BASH_ENV (and, post-#412, rbash -l + profile) must source on both faces.
Minor
- shell.rs:383-385: dead code —
invoked_nameis alreadytrim_end_matches(".exe")+ lowercased three lines above, soface_stem.strip_suffix(".exe")can never match, and the-strip duplicates the existingunmarked_name.invoked_as_restricted = unmarked_name == "rbash"says the whole thing. (The recognition that remains is correct: rbash-only matches GNU 5.3 — I probedrshargv0 there:hBc, unrestricted — and the-marker strip handles-rbash→hrBc.) issue400_flag_form_matches_the_argv0_faceequates the faces only on post-startup probes, which is why it passes while the faces actually diverge — extend it with the startup-file row once the timing is fixed.
Verified good
- Base claim confirmed: 7c42373
rbash.exe -c→hBc,cd /succeeds, PATH assignable; this branch →hrBc, cd refused, PATH/SHELL readonly rc 1,set +rrefused,P0=rbashon the shim face, plain-name control unrestricted,RBASH.EXEspelling covered by the lowercase stem path. All 4issue400_*cases pass 4/4 runs at--test-threads=1; fullinteractivetarget green except the base-confirmed environment failures (one loaded run also showed 6 transient failures — all 6 pass on an idle rerun; contention artifacts, not regressions).startup_files/host_contract/completion_probe/invocation_surface/smokematch base (the two failures I saw fail identically on pristine 7c42373). Diff surface matches the PR description; no GPL fragments or translation wording. Driver threading (binary: &Path) is mechanical and preserves existing call sites; both new spawn helpers are necessary for their faces.
Merge-order note
Whichever of #412/#413 merges second needs a trivial EOF conflict resolution in tests/interactive.rs (both append at the same anchor); driver.rs and shell.rs auto-merge. Joint rbash -l behavior is defined (login chain per #412 + restricted per #413) but is currently reasonable only for the flag face — see the blocking finding.
…orm; login env gates stay independent (review round) CR round on #413 and #412. #413 blocking - activation timing: the rbash face armed the restricted option inside Shell::new, before any startup file runs, while the -r/--restricted flag form activates in the engine's ShellInvocation::finish_startup (invocation.rs: 'after the host finishes startup files' - the deliberate GNU order). The divergence was runtime-visible: 'rbash.exe -c' refused a BASH_ENV file with '.: <path>: restricted' and 'rbash -l -c' refused the whole profile chain, where the flag form and GNU load both fine. Shell::new now only records the face mark (one line keyed off unmarked_name, dropping the redundant .exe strip and '-' strip); the option write moved into Shell::finish_startup, right beside the flag write. Startup-file equivalence is pinned by a test: argv0 face and flag form load BASH_ENV and the profile chain identically and go restricted for the command line. CR's two blocking repros both pass. #412 follow-ups: three comments over-claimed GNU exclusivity and the behavior followed them - the login profile chain replaces only the interactive-RC branch, while the env-file gates are independent of login state: 'bash -l -c' sources the chain AND BASH_ENV (shell.c 1214-1220 keys on interactivity/posix), and 'sh -l -i -c' sources the chain AND $ENV (shell.c 1238-1246 keys on posix+interactive). Both re-measured against the WSL 5.3.0 oracle and implemented. Also adds the missing regression for the run_repl RefCell fix: a plain REPL with 'exit 5' in the rc ends with status 5 and no panic.
5dd00ef to
f24f7e4
Compare
The rbash argv0 face was not assembled: a niu copy named rbash.exe ran fully unrestricted - $- read hBc (no r) and cd / succeeded, so nothing downstream could detect the mode either. The engine derives the same face from argv[0] in its own binary (shell.c:1258-1299 shell_is_restricted/maybe_make_restricted via rubash#598); the host constructor now hands the executor the identical state when the invocation name (argv[0] basename or the shim-forwarded NIU_INVOKED_AS, .exe normalized case-insensitively, the leading - login marker stripped) is rbash. The option is applied after the startup PATH/SHELL assembly so the maybe_make_restricted read-only walk pins the final values, exactly like the -r/--restricted flag whose executor write lands after Shell::new returns - the flag form and the name form now probe identically. Oracle (WSL GNU Bash 5.3.0 via an rbash symlink, stdin method): $- = hrBc, cd refused (cd: restricted), PATH/SHELL/ENV/BASH_ENV/HISTFILE read-only, slash command names refused, exec-with-operand refused, source-with-slash refused, set +r refused. Both ends probed: the rubash engine binary invoked as rbash (already restricted, rubash#598) and niu.exe invoked as rbash (fixed here) answer the same contract. Known engine-side gaps shared with the -r flag form and unchanged in this host-face ticket: output redirections (>, >>, <>, >&, >|) are not gated, input redirection is refused where GNU allows it, and 'command /cmd' is not gated - rubash-side work. Regressions in tests/interactive.rs (the driver gains spawn_face_with_args to stage the niu copy under an invocation face).
…orm; login env gates stay independent (review round) CR round on #413 and #412. #413 blocking - activation timing: the rbash face armed the restricted option inside Shell::new, before any startup file runs, while the -r/--restricted flag form activates in the engine's ShellInvocation::finish_startup (invocation.rs: 'after the host finishes startup files' - the deliberate GNU order). The divergence was runtime-visible: 'rbash.exe -c' refused a BASH_ENV file with '.: <path>: restricted' and 'rbash -l -c' refused the whole profile chain, where the flag form and GNU load both fine. Shell::new now only records the face mark (one line keyed off unmarked_name, dropping the redundant .exe strip and '-' strip); the option write moved into Shell::finish_startup, right beside the flag write. Startup-file equivalence is pinned by a test: argv0 face and flag form load BASH_ENV and the profile chain identically and go restricted for the command line. CR's two blocking repros both pass. #412 follow-ups: three comments over-claimed GNU exclusivity and the behavior followed them - the login profile chain replaces only the interactive-RC branch, while the env-file gates are independent of login state: 'bash -l -c' sources the chain AND BASH_ENV (shell.c 1214-1220 keys on interactivity/posix), and 'sh -l -i -c' sources the chain AND $ENV (shell.c 1238-1246 keys on posix+interactive). Both re-measured against the WSL 5.3.0 oracle and implemented. Also adds the missing regression for the run_repl RefCell fix: a plain REPL with 'exit 5' in the rc ends with status 5 and no panic.
f24f7e4 to
4fd135b
Compare
caomengxuan666
left a comment
There was a problem hiding this comment.
CR round 2 verdict: APPROVE — the blocking finding is fixed and runtime-verified (posted as comment: this token is the author identity, GitHub refuses self reviews)
Rebuilt 4fd135b on the 717327a base and re-ran the full verification on this host (idle machine, pty, --test-threads=1).
Blocking finding (activation timing) — fixed
Shell::new now only records invoked_as_rbash (shell.rs:189/:360/:390 — the one-line unmarked_name == "rbash", redundant strips gone as requested), and the option write moved to Shell::finish_startup() (shell.rs:981), immediately after the -r flag form's ShellInvocation::finish_startup write — the same GNU window, every entry path (run_startup_files repl/non-repl, run_interactive_startup_rc, source_non_interactive_env) ends in finish_startup. Both of my round-1 repros re-run clean, independently re-derived (fresh markers, not just the new tests):
rbash.exe -l -c+ BASH_ENV +.bash_profile(profile assigns HISTFILE):PROF+BENVmarkers print, the profile's HISTFILE assignment sticks, then the command phase is restricted —FLAGS=hrBc,CD-BLOCKED,PATHRC=1. No.: restrictedanywhere.- The new
issue400_startup_files_load_unrestricted_like_the_flag_formpins exactly this face-equivalence for BASH_ENV and the chain on both faces.
set_shell_optionidempotence makes the doubledfinish_startuppasses on the REPL paths harmless — correct call.
#412 follow-ups — verified against the WSL 5.3.0 oracle and implementation
The three overclaiming comments are gone and the new model matches my probes: login_shell_active doc now says the chain replaces only the interactive-RC branch (shell.rs:1083-1086); source_non_interactive_env documents the BASH_ENV gate as login-independent (shell.rs:1355-1361) and the code implements it — bash -l -c sources chain and env file (matches GNU probe), profile exit still short-circuits before the env file; the posix ENV comment says the gate keys on posix+interactive (shell.rs:1168-1172) and sh -l -i -c sources chain + $ENV (matches GNU probe). issue399_login_noninteractive_sources_bash_env_after_the_chain and issue399_posix_login_interactive_sources_env_after_the_chain pin both rows. Bonus divergence fixed as a side effect: --noprofile -l -c + BASH_ENV now sources the env file like GNU (--noprofile only suppresses the chain), where round 1 sourced nothing. repl_rc_exit_carries_status_without_panic closes the RefCell test gap.
Rebase on #421 — conflict resolution checked
The generated-loader step sits in the non-login rc branch only (shell.rs:1209-1213), after the login early-return and before the rc source; its exit propagates like any startup file. The rbash activation lands after the loader on every path (finish_startup tail), so the loader sources in the same unrestricted window as GNU's run_startup_files — and the loader is zero-write at startup, so the restricted read-only walk (PATH/SHELL/ENV/BASH_ENV/HISTFILE) never interacts with it. The rbash×loader combination has no dedicated test, but it is safe by construction (and rc_readonly_loader 13/13 pass alongside); a combined smoke would be nice-to-have only.
Tests (all re-run by me, idle)
issue399+issue400+repl_rc_exit: 18/18, three consecutive runs.- Full
interactivetarget: 71 passed, 5 failed — exactly the known environment set (continuation_single_quote/paren, ctrl_c, ctrl_d, gitbash_factory_prompt), unchanged from base. startup_files29/29,rc_readonly_loader13/13,niubash-runtime --lib550/0,cargo fmt --checkclean. CI 39176359839 green confirmed.- Fix-commit surface: exactly shell.rs + tests/interactive.rs + driver.rs; no GPL fragments or translation wording.
Non-blocking nits (no re-review needed)
- shell.rs:1171-1181 — on the login branch, a posix $ENV
exitrunsupdate_completion_state/finish_startup/run_greeting_hooksbefore returning the jump status, unlike every sibling exit path (profile, rc, non-login ENV) that returns early — so greeting output can appear after an ENVexitundersh -l -i. Aligning it with the early-return discipline would tidy the #402 invariant. - Informational:
bash --posix -l -csources nothing in GNU 5.3 (no chain, no ENV non-interactively; GNU distinguishes--posixfrom sh-invoked) while niu sources the chain. The new comments don't claim otherwise, so this is a residual known divergence, not an overclaim. - Informational: the rbash×generated-loader combination is untested (safe by construction, see above).
Ship it.
Root cause
The host never assembled the rbash face.
Shell::newderivessh/ash(POSIX mode) and the login marker from the invoked basename, butrbashfell through with no option write, so a niu copy namedrbash.exeran fully unrestricted:hBcreadhBc(nor) andcd /succeeded — and sincehBcis exactly how a program detects restricted mode, nothing downstream could notice either. The engine keys the face offargv[0]in its own binary (shell.c:1258-1299shell_is_restricted/maybe_make_restricted, rubash#598); the host face now hands the executor the identical state.Review round (CR) — activation timing
v1 armed the option inside
Shell::new, before any startup file runs; the-r/--restrictedflag form activates in the engine'sShellInvocation::finish_startup(invocation.rs: "after the host finishes startup files" — the deliberate GNU order). The divergence was runtime-visible:rbash.exe -c+ BASH_ENV refused the file (.: <path>: restricted) andrbash -l -crefused the whole profile chain, where the flag form and GNU load both fine.Now
Shell::newonly records the face mark (one line keyed offunmarked_name; the redundant.exestrip and-strip are gone), andShell::finish_startupwrites the option right beside the flag write — after every startup file, idempotently across the REPL's extrafinish_startuppasses. Startup-file equivalence is pinned by a new test: the argv0 face and the flag form load BASH_ENV and the login profile chain identically (both unrestricted) and go restricted for the command line.rbash contract, GNU vs fixed niu (argv0 face)
Oracle: WSL GNU Bash 5.3.0 via an
rbashsymlink; niu probes via therbash.execopy. Both ends tested: the rubash engine binary invoked asrbash(rubash#598 — already restricted) andniu.exeinvoked asrbash(this fix); the two answer identically on every row.hBchrBchrBccd /(+ subshell/function)cd: restricted, refusedbashrbashrbashPATH=/export PATH/unset PATHSHELL=/restricted: cannot specify '/'…execwith operandexec: restricted. /etc/profileset +rescaperbash -crbash -l -culimit>>><>>&>|-rflag form; rubash work< filecommand /bin/echoniucontrolAlso carried: #412 follow-ups (merged PR, CR comments)
bash -l -csources the chain AND BASH_ENV (shell.c:1214-1220 keys on interactivity/posix, never login), andsh -l -i -csources the chain AND `` (shell.c:1238-1246 keys on posix+interactive). Both re-measured against the WSL oracle and implemented (source_non_interactive_env+ the `run_startup_files` login branch now fall through to the env gates).run_replRefCell fix: a plain REPL withexit 5in the rc ends with status 5 and no panic (repl_rc_exit_carries_status_without_panic).Tests
issue400_*(4 + the new startup-file equivalence case covering both faces on BASH_ENV and the profile chain),issue399_*(10 + the two login-env-gate cases), andrepl_rc_exit_carries_status_without_panic— 18 green under--test-threads=1; runtime lib, startup_files, and invocation-surface targets pass. The 5 interactive failures seen locally are pre-existing environment flakes that fail identically on pristine master on this host.Closes #400. The #412 follow-ups ride this branch per review guidance.