Skip to content

Commit 08eac8c

Browse files
authored
fix(sandbox): detect an available login shell instead of hardcoding /bin/bash (NVIDIA#3147)
* fix(sandbox): detect an available login shell instead of hardcoding /bin/bash The built-in default sandbox command and the interactive SSH session hardcoded /bin/bash. Minimal images such as Alpine ship only /bin/sh (BusyBox ash), so sandbox startup failed with an opaque "No such file or directory (os error 2)" that never named the missing binary. Add openshell-core::shell with shell-path constants and a runtime detect_login_shell() that resolves a shell present in the sandbox image ($SHELL if executable, then bash, then /bin/sh). Use it for: - the built-in default command (only the default is remapped; explicit user commands are never rewritten), resolved in the supervisor so it inspects the sandbox filesystem rather than the gateway's - the SSH interactive shell - the SHELL environment variable Also name the program in the spawn error so a missing shell/binary is diagnosable instead of a bare ENOENT. Refs NVIDIA#3146 Signed-off-by: Akram <[email protected]> * 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]> * fix(sandbox): resolve the default login shell in the supervisor (empty command = default) Addresses review: interactive PTY SSH now uses the detected shell, the shell tests are portable across the Windows lane, and default-shell provenance is carried without a new spec field. An omitted command is left empty end to end and resolved in the supervisor, which is the only place that sees the sandbox image: - The CLI forwards the command as-is; the gateway persists an omitted command as empty (no baked /bin/bash -l) and requests a TTY. - MainProcessConfig carries the command empty (the transport now allows it); the supervisor resolves a login shell that exists in the sandbox image (bash when present, otherwise /bin/sh on minimal images like Alpine) and logs the resolved shell. - Interactive PTY SSH (spawn_pty_shell) uses the detected shell; a shared build_ssh_shell_command helper covers the PTY and non-PTY paths, with a deterministic sh-only regression test. - Unix-only shell tests are gated with cfg(unix). An explicit command is always run verbatim. Refs NVIDIA#3146 Signed-off-by: Akram <[email protected]> --------- Signed-off-by: Akram <[email protected]>
1 parent 52b1d78 commit 08eac8c

10 files changed

Lines changed: 288 additions & 53 deletions

File tree

‎crates/openshell-cli/src/run.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,11 @@ pub async fn sandbox_create(
589589

590590
let main_terminal = tty_override
591591
.unwrap_or_else(|| std::io::stdin().is_terminal() && std::io::stdout().is_terminal());
592-
let main_command = if command.is_empty() {
593-
vec!["/bin/bash".to_string(), "-l".to_string()]
594-
} else {
595-
command.to_vec()
596-
};
592+
// Forward the command as-is. When empty, the gateway persists it empty and
593+
// the supervisor resolves the default login shell against the sandbox image
594+
// (bash when present, otherwise /bin/sh on minimal images like Alpine).
595+
// Baking a shell here would force a shell the image may not ship.
596+
let main_command = command.to_vec();
597597
let persist = sandbox_should_persist(keep, forward.as_ref());
598598
let create_detaches = detach
599599
|| (persist

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub mod provider_credentials;
4545
pub mod sandbox_env;
4646
pub mod secrets;
4747
pub mod settings;
48+
pub mod shell;
4849
pub mod spiffe;
4950
pub mod telemetry;
5051
pub mod time;

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const MAIN_PROCESS_SPEC_BASE64URL_PREFIX: &str = "base64url:";
3838
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
3939
pub struct MainProcessConfig {
4040
pub version: u32,
41+
/// Canonical command. Empty means "no command supplied": the supervisor
42+
/// resolves the default login shell against the sandbox image. A non-empty
43+
/// command is the exact program+args and is run verbatim.
4144
pub command: Vec<String>,
4245
pub tty: bool,
4346
#[serde(default)]
@@ -47,11 +50,15 @@ pub struct MainProcessConfig {
4750
impl MainProcessConfig {
4851
pub const VERSION: u32 = 1;
4952

53+
/// Default config for a sandbox created without a command. The command is
54+
/// left empty on purpose: the supervisor picks a login shell that exists in
55+
/// the sandbox image (bash when present, otherwise `/bin/sh`). A TTY is
56+
/// requested because the default is an interactive login shell.
5057
#[must_use]
5158
pub fn scratch() -> Self {
5259
Self {
5360
version: Self::VERSION,
54-
command: vec!["/bin/bash".to_string(), "-l".to_string()],
61+
command: Vec::new(),
5562
tty: true,
5663
await_main_process_attachment: false,
5764
}
@@ -91,8 +98,13 @@ impl MainProcessConfig {
9198
config.version
9299
));
93100
}
94-
if config.command.is_empty() || config.command[0].is_empty() {
95-
return Err(format!("{MAIN_PROCESS_SPEC} command must not be empty"));
101+
// An empty command is valid: it means "no command supplied", and the
102+
// supervisor resolves the default login shell. Only a present-but-blank
103+
// program is rejected.
104+
if !config.command.is_empty() && config.command[0].is_empty() {
105+
return Err(format!(
106+
"{MAIN_PROCESS_SPEC} command program must not be empty"
107+
));
96108
}
97109
Ok(config)
98110
}
@@ -283,4 +295,40 @@ mod tests {
283295
let encoded = serde_json::to_string(&config).unwrap();
284296
assert_eq!(MainProcessConfig::decode(&encoded).unwrap(), config);
285297
}
298+
299+
#[test]
300+
fn omitted_command_stays_empty_for_supervisor_resolution() {
301+
// No command supplied → empty command; the supervisor resolves the
302+
// default login shell against the sandbox image.
303+
let empty = crate::proto::compute::v1::DriverSandboxSpec::default();
304+
assert!(
305+
MainProcessConfig::from_driver_spec(Some(&empty))
306+
.command
307+
.is_empty()
308+
);
309+
assert!(MainProcessConfig::from_driver_spec(None).command.is_empty());
310+
311+
// An explicit command is preserved verbatim and never rewritten.
312+
let explicit = crate::proto::compute::v1::DriverSandboxSpec {
313+
command: vec!["/bin/bash".into(), "-l".into()],
314+
tty: true,
315+
..Default::default()
316+
};
317+
assert_eq!(
318+
MainProcessConfig::from_driver_spec(Some(&explicit)).command,
319+
vec!["/bin/bash".to_string(), "-l".to_string()]
320+
);
321+
322+
// An empty command survives the transport round-trip.
323+
let encoded = serde_json::to_string(&MainProcessConfig::scratch()).unwrap();
324+
assert!(
325+
MainProcessConfig::decode(&encoded)
326+
.unwrap()
327+
.command
328+
.is_empty()
329+
);
330+
331+
// A present-but-blank program is still rejected.
332+
assert!(MainProcessConfig::decode(r#"{"version":1,"command":[""],"tty":false}"#).is_err());
333+
}
286334
}

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Login-shell resolution for sandbox images.
5+
//!
6+
//! The default sandbox command and the interactive SSH session need a shell,
7+
//! but not every base image ships the same one. Debian-based images provide
8+
//! `bash`; minimal images such as Alpine only provide `/bin/sh` (`BusyBox`
9+
//! `ash`). Hard-coding `/bin/bash` makes sandbox startup fail on those images
10+
//! with an opaque `No such file or directory`.
11+
//!
12+
//! These helpers resolve a shell that actually exists in the current root
13+
//! filesystem. They must run inside the sandbox (i.e. in the supervisor), not
14+
//! on the gateway, because the answer depends on the sandbox image's contents.
15+
16+
/// Preferred interactive shell when the image provides it.
17+
pub const BASH: &str = "/bin/bash";
18+
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+
23+
/// POSIX shell. Guaranteed on virtually every image, including Alpine/`BusyBox`.
24+
/// Used as the ultimate fallback.
25+
pub const POSIX_SH: &str = "/bin/sh";
26+
27+
/// Shell paths tried, in preference order, by [`detect_login_shell`].
28+
pub const SHELL_CANDIDATES: &[&str] = &[BASH, USR_BASH, POSIX_SH];
29+
30+
/// Return `true` if `path` is a regular, executable file in the current root
31+
/// filesystem.
32+
#[must_use]
33+
pub fn is_executable(path: &str) -> bool {
34+
let Ok(meta) = std::fs::metadata(path) else {
35+
return false;
36+
};
37+
if !meta.is_file() {
38+
return false;
39+
}
40+
#[cfg(unix)]
41+
{
42+
use std::os::unix::fs::PermissionsExt;
43+
meta.permissions().mode() & 0o111 != 0
44+
}
45+
#[cfg(not(unix))]
46+
{
47+
true
48+
}
49+
}
50+
51+
/// Resolve a login shell that exists in the current root filesystem.
52+
///
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.
62+
#[must_use]
63+
pub fn detect_login_shell() -> String {
64+
SHELL_CANDIDATES
65+
.iter()
66+
.find(|candidate| is_executable(candidate))
67+
.map_or_else(
68+
|| POSIX_SH.to_string(),
69+
|candidate| (*candidate).to_string(),
70+
)
71+
}
72+
73+
#[cfg(test)]
74+
mod tests {
75+
use super::*;
76+
77+
// These assume a Unix root filesystem (`/bin/sh`, POSIX permission bits) and
78+
// are skipped on the Windows workspace lane where openshell-core also builds.
79+
#[cfg(unix)]
80+
#[test]
81+
fn posix_sh_exists_on_test_host() {
82+
// /bin/sh is present on all supported Unix CI hosts (Linux and macOS).
83+
assert!(is_executable(POSIX_SH));
84+
}
85+
86+
#[test]
87+
fn missing_path_is_not_executable() {
88+
assert!(!is_executable("/nonexistent/definitely/not/here"));
89+
}
90+
91+
#[cfg(unix)]
92+
#[test]
93+
fn non_file_is_not_executable() {
94+
// A directory is not an executable shell.
95+
assert!(!is_executable("/"));
96+
}
97+
98+
#[cfg(unix)]
99+
#[test]
100+
fn detect_returns_an_executable_shell() {
101+
let shell = detect_login_shell();
102+
assert!(
103+
is_executable(&shell),
104+
"detected shell {shell} is not executable"
105+
);
106+
}
107+
}

‎crates/openshell-driver-docker/src/tests.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,9 @@ fn build_environment_sets_docker_tls_paths() {
12271227
})
12281228
.expect("main-process transport");
12291229
let main = openshell_core::sandbox_env::MainProcessConfig::decode(&encoded).unwrap();
1230-
assert_eq!(main.command, vec!["/bin/bash", "-l"]);
1230+
// An omitted command is forwarded empty; the supervisor resolves the default
1231+
// login shell against the sandbox image at startup.
1232+
assert!(main.command.is_empty());
12311233
assert!(main.tty);
12321234
}
12331235

‎crates/openshell-sandbox/src/main.rs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ impl std::str::FromStr for Mode {
111111
#[command(about = "Process sandbox and monitor", long_about = None)]
112112
struct Args {
113113
/// Command to execute in the sandbox.
114-
/// Defaults to `/bin/bash -l` if neither this nor the driver specification is provided.
114+
/// Defaults to a login shell if neither this nor the driver specification is
115+
/// provided: `/bin/bash -l` when available, otherwise a shell detected in the
116+
/// sandbox image (e.g. `/bin/sh` on Alpine).
115117
#[arg(trailing_var_arg = true)]
116118
command: Vec<String>,
117119

@@ -678,6 +680,12 @@ fn main() -> Result<()> {
678680
)
679681
};
680682

683+
// An omitted command (the gateway leaves the default empty rather than
684+
// baking a shell it cannot verify) is resolved to a login shell here, in
685+
// the supervisor, so it matches the sandbox image: bash when present,
686+
// otherwise /bin/sh (e.g. Alpine). An explicit command is used verbatim.
687+
let command = resolve_default_command(command);
688+
681689
info!(command = ?command, "Starting sandbox");
682690
// Note: "Starting sandbox" stays as plain info!() since the OCSF context
683691
// is not yet initialized at this point (run_sandbox hasn't been called).
@@ -718,6 +726,20 @@ fn main() -> Result<()> {
718726
std::process::exit(exit_code);
719727
}
720728

729+
/// Resolve an omitted canonical command to a login shell that exists in this
730+
/// sandbox image. Empty means "use the default": the gateway leaves an omitted
731+
/// command empty rather than persisting a shell it cannot verify, so the
732+
/// supervisor picks one here against the real sandbox filesystem (bash when
733+
/// present, otherwise `/bin/sh`). An explicit command is returned unchanged.
734+
fn resolve_default_command(command: Vec<String>) -> Vec<String> {
735+
if !command.is_empty() {
736+
return command;
737+
}
738+
let shell = openshell_core::shell::detect_login_shell();
739+
info!(shell = %shell, "no command specified; resolved default login shell");
740+
vec![shell, "-l".to_string()]
741+
}
742+
721743
#[cfg(test)]
722744
mod tests {
723745
use super::*;

‎crates/openshell-server/src/grpc/sandbox.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,12 @@ async fn handle_create_sandbox_inner(
297297
(resolved, Some(provenance))
298298
};
299299

300-
// Every newly persisted sandbox has one explicit canonical process. This
301-
// portable default also preserves compatibility with callers compiled
302-
// before the main-process field was introduced.
300+
// Leave an omitted command empty rather than persisting a concrete shell:
301+
// the supervisor resolves the default login shell against the sandbox image
302+
// (bash when present, otherwise /bin/sh on minimal images like Alpine),
303+
// which the gateway cannot do since it does not see the sandbox filesystem.
304+
// The default is an interactive login shell, so request a TTY.
303305
if spec.command.is_empty() {
304-
spec.command = vec!["/bin/bash".to_string(), "-l".to_string()];
305306
spec.tty = true;
306307
}
307308

‎crates/openshell-supervisor-process/src/process.rs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,14 @@ fn apply_canonical_process_environment(
225225
user_environment: &HashMap<String, String>,
226226
) {
227227
let (session_user, session_home) = session_user_and_home(policy, workspace.home());
228+
// Resolve a shell present in the sandbox image (minimal images such as
229+
// Alpine ship only `/bin/sh`, not bash). Runs in the supervisor.
230+
let shell = openshell_core::shell::detect_login_shell();
228231

229232
for (key, value) in [
230233
("HOME", session_home.as_str()),
231234
("USER", session_user.as_str()),
232-
("SHELL", "/bin/bash"),
235+
("SHELL", shell.as_str()),
233236
(
234237
"TERM",
235238
if interactive {
@@ -900,15 +903,19 @@ impl ProcessHandle {
900903
}
901904
}
902905

906+
// Name the program in the error: a bare "No such file or directory"
907+
// here is otherwise indistinguishable from a missing working directory
908+
// or interpreter, and is a common failure on images that lack the
909+
// requested shell/binary (e.g. bash on Alpine).
903910
#[cfg(target_os = "linux")]
904911
let mut child = spawn_command_with_supervisor_identity_namespace(cmd)
905912
.into_diagnostic()
906-
.wrap_err("failed to spawn sandbox entrypoint process")?;
913+
.wrap_err_with(|| format!("failed to spawn sandbox entrypoint process '{program}'"))?;
907914
#[cfg(not(target_os = "linux"))]
908915
let mut child = cmd
909916
.spawn()
910917
.into_diagnostic()
911-
.wrap_err("failed to spawn sandbox entrypoint process")?;
918+
.wrap_err_with(|| format!("failed to spawn sandbox entrypoint process '{program}'"))?;
912919
let pid = child.id().unwrap_or(0);
913920
managed_children::register(pid);
914921

@@ -2475,7 +2482,10 @@ mod tests {
24752482
Some(&current_user.dir.to_string_lossy().as_ref())
24762483
);
24772484
assert_eq!(variables.get("USER"), Some(&current_user.name.as_str()));
2478-
assert_eq!(variables.get("SHELL"), Some(&"/bin/bash"));
2485+
// SHELL is the shell detected in the current root filesystem, not a
2486+
// hardcoded path (bash-less images resolve to /bin/sh).
2487+
let expected_shell = openshell_core::shell::detect_login_shell();
2488+
assert_eq!(variables.get("SHELL"), Some(&expected_shell.as_str()));
24792489
assert_eq!(variables.get("TERM"), Some(&"xterm-256color"));
24802490
}
24812491

0 commit comments

Comments
 (0)