Skip to content

Commit c37f287

Browse files
committed
fix(sandbox): drop $SHELL preference in shell detection
$SHELL is image/user-controlled and the detected shell is later invoked with `-lc`, so an executable that is not a compatible shell (e.g. SHELL=/bin/false) would pass the executable check and then break command execution even when /bin/sh is available. Resolve only from known shell paths instead. Also add a USR_BASH constant for /usr/bin/bash rather than a string literal in SHELL_CANDIDATES. Refs NVIDIA#3146 Signed-off-by: Akram <[email protected]>
1 parent 158c9cb commit c37f287

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

‎crates/openshell-core/src/shell.rs‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
/// Preferred interactive shell when the image provides it.
1717
pub const BASH: &str = "/bin/bash";
1818

19+
/// Preferred interactive shell on `usr`-merged images where `/bin` is not a
20+
/// top-level directory.
21+
pub const USR_BASH: &str = "/usr/bin/bash";
22+
1923
/// POSIX shell. Guaranteed on virtually every image, including Alpine/`BusyBox`.
2024
/// Used as the ultimate fallback.
2125
pub const POSIX_SH: &str = "/bin/sh";
2226

2327
/// Shell paths tried, in preference order, by [`detect_login_shell`].
24-
pub const SHELL_CANDIDATES: &[&str] = &[BASH, "/usr/bin/bash", POSIX_SH];
28+
pub const SHELL_CANDIDATES: &[&str] = &[BASH, USR_BASH, POSIX_SH];
2529

2630
/// Return `true` if `path` is a regular, executable file in the current root
2731
/// filesystem.
@@ -46,17 +50,17 @@ pub fn is_executable(path: &str) -> bool {
4650

4751
/// Resolve a login shell that exists in the current root filesystem.
4852
///
49-
/// Resolution order: `$SHELL` (when set and executable), then
50-
/// [`SHELL_CANDIDATES`], falling back to [`POSIX_SH`]. Because this inspects
51-
/// the filesystem, call it from the supervisor (inside the sandbox), never on
52-
/// the gateway.
53+
/// Tries [`SHELL_CANDIDATES`] in order and falls back to [`POSIX_SH`]. Because
54+
/// this inspects the filesystem, call it from the supervisor (inside the
55+
/// sandbox), never on the gateway.
56+
///
57+
/// `$SHELL` is intentionally not consulted: it is image/user-controlled, the
58+
/// result is later invoked with `-lc`, and an executable that is not a
59+
/// compatible shell (e.g. `SHELL=/bin/false`) would pass the executable check
60+
/// yet break command execution. Resolving only from known shell paths avoids
61+
/// that footgun.
5362
#[must_use]
5463
pub fn detect_login_shell() -> String {
55-
if let Ok(shell) = std::env::var("SHELL")
56-
&& is_executable(&shell)
57-
{
58-
return shell;
59-
}
6064
SHELL_CANDIDATES
6165
.iter()
6266
.find(|candidate| is_executable(candidate))

0 commit comments

Comments
 (0)