Skip to content

fix(startup): a shell invoked as rbash starts restricted (#400) - #413

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/400-rbash-argv0-face
Oct 18, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/400-rbash-argv0-face

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Root cause

The host never assembled the rbash face. Shell::new derives sh/ash (POSIX mode) and the login marker from the invoked basename, but rbash fell through with no option write, so a niu copy named rbash.exe ran fully unrestricted: hBc read hBc (no r) and cd / succeeded — and since hBc is exactly how a program detects restricted mode, nothing downstream could notice either. The engine keys the face off argv[0] in its own binary (shell.c:1258-1299 shell_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/--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 + BASH_ENV refused the file (.: <path>: restricted) and rbash -l -c refused the whole profile chain, where the flag form and GNU load both fine.

Now Shell::new only records the face mark (one line keyed off unmarked_name; the redundant .exe strip and - strip are gone), and Shell::finish_startup writes the option right beside the flag write — after every startup file, idempotently across the REPL's extra finish_startup passes. 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 rbash symlink; niu probes via the rbash.exe copy. Both ends tested: the rubash engine binary invoked as rbash (rubash#598 — already restricted) and niu.exe invoked as rbash (this fix); the two answer identically on every row.

# contract face GNU rbash niu rbash (this PR)
1 hBc hrBc hrBc
2 cd / (+ subshell/function) cd: restricted, refused same
3 bash rbash rbash
4 PATH= / export PATH / unset PATH readonly error, rc 1 same
5 SHELL= readonly error, rc 1 same
6 command name with / restricted: cannot specify '/'… same
7 exec with operand exec: restricted same
8 . /etc/profile restricted same
9 set +r escape refused, mode stays same
10 BASH_ENV file + rbash -c loads unrestricted loads unrestricted (v1 refused — fixed in review)
11 login chain + rbash -l -c loads unrestricted loads unrestricted (v1 refused — fixed in review)
12 background jobs, ulimit allowed allowed
13 output redirects > >> <> >& >| refused allowed — engine-side gap, shared with the -r flag form; rubash work
14 input redirect < file allowed refused-but-continues — engine-side divergence, same on the flag form and engine binary; rubash work
15 command /bin/echo refused allowed — engine-side gap, same on the flag form; rubash work
16 plain niu control — unrestricted, unchanged

Also carried: #412 follow-ups (merged PR, CR comments)

  • Three comments over-claimed GNU exclusivity and the behavior followed them. The login profile chain replaces only the interactive-RC branch; the env-file gates are independent of login: bash -l -c sources the chain AND BASH_ENV (shell.c:1214-1220 keys on interactivity/posix, never login), and sh -l -i -c sources 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).
  • 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 (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), and repl_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.

@caomengxuan666 caomengxuan666 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_name is already trim_end_matches(".exe") + lowercased three lines above, so face_stem.strip_suffix(".exe") can never match, and the - strip duplicates the existing unmarked_name. invoked_as_restricted = unmarked_name == "rbash" says the whole thing. (The recognition that remains is correct: rbash-only matches GNU 5.3 — I probed rsh argv0 there: hBc, unrestricted — and the - marker strip handles -rbash → hrBc.)
  • issue400_flag_form_matches_the_argv0_face equates 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 +r refused, P0=rbash on the shim face, plain-name control unrestricted, RBASH.EXE spelling covered by the lowercase stem path. All 4 issue400_* cases pass 4/4 runs at --test-threads=1; full interactive target 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/smoke match 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.

caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
…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.
@caomengxuan666
caomengxuan666 force-pushed the fix/400-rbash-argv0-face branch from 5dd00ef to f24f7e4 Compare October 18, 2026 13:10
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.
@caomengxuan666
caomengxuan666 force-pushed the fix/400-rbash-argv0-face branch from f24f7e4 to 4fd135b Compare October 18, 2026 23:24

@caomengxuan666 caomengxuan666 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 + BENV markers print, the profile's HISTFILE assignment sticks, then the command phase is restricted — FLAGS=hrBc, CD-BLOCKED, PATHRC=1. No .: restricted anywhere.
  • The new issue400_startup_files_load_unrestricted_like_the_flag_form pins exactly this face-equivalence for BASH_ENV and the chain on both faces.
    set_shell_option idempotence makes the doubled finish_startup passes 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 interactive target: 71 passed, 5 failed — exactly the known environment set (continuation_single_quote/paren, ctrl_c, ctrl_d, gitbash_factory_prompt), unchanged from base.
  • startup_files 29/29, rc_readonly_loader 13/13, niubash-runtime --lib 550/0, cargo fmt --check clean. 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)

  1. shell.rs:1171-1181 — on the login branch, a posix $ENV exit runs update_completion_state/finish_startup/run_greeting_hooks before returning the jump status, unlike every sibling exit path (profile, rc, non-login ENV) that returns early — so greeting output can appear after an ENV exit under sh -l -i. Aligning it with the early-return discipline would tidy the #402 invariant.
  2. Informational: bash --posix -l -c sources nothing in GNU 5.3 (no chain, no ENV non-interactively; GNU distinguishes --posix from sh-invoked) while niu sources the chain. The new comments don't claim otherwise, so this is a residual known divergence, not an overclaim.
  3. Informational: the rbash×generated-loader combination is untested (safe by construction, see above).

Ship it.

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.

[host] the rbash argv0 face is not restricted: $- has no r and cd / succeeds

1 participant