Skip to content

Commit 0e36df2

Browse files
committed
fix(isolation): resolve scratch command in sandbox
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 89a0b08 commit 0e36df2

5 files changed

Lines changed: 60 additions & 17 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const MAIN_PROCESS_SPEC_BASE64URL_PREFIX: &str = "base64url:";
3939
pub struct MainProcessConfig {
4040
pub version: u32,
4141
/// Canonical command. Empty means "no command supplied": the supervisor
42-
/// resolves the default login shell against the sandbox image. A non-empty
42+
/// asks the sandbox boundary to resolve the default login shell against
43+
/// the agent image. A non-empty
4344
/// command is the exact program+args and is run verbatim.
4445
pub command: Vec<String>,
4546
pub tty: bool,
@@ -51,8 +52,8 @@ impl MainProcessConfig {
5152
pub const VERSION: u32 = 1;
5253

5354
/// 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
55+
/// left empty on purpose: the sandbox boundary picks a login shell that
56+
/// exists in the agent image (bash when present, otherwise `/bin/sh`). A TTY is
5657
/// requested because the default is an interactive login shell.
5758
#[must_use]
5859
pub fn scratch() -> Self {
@@ -99,7 +100,7 @@ impl MainProcessConfig {
99100
));
100101
}
101102
// 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+
// sandbox boundary resolves the default login shell. Only a present-but-blank
103104
// program is rejected.
104105
if !config.command.is_empty() && config.command[0].is_empty() {
105106
return Err(format!(
@@ -316,7 +317,7 @@ mod tests {
316317

317318
#[test]
318319
fn omitted_command_stays_empty_for_supervisor_resolution() {
319-
// No command supplied → empty command; the supervisor resolves the
320+
// No command supplied → empty command; the sandbox boundary resolves the
320321
// default login shell against the sandbox image.
321322
let empty = crate::proto::compute::v1::DriverSandboxSpec::default();
322323
assert!(

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
//! Login-shell resolution for sandbox images.
55
//!
6-
//! The default sandbox command and the interactive SSH session need a shell,
6+
//! The default sandbox command and interactive SSH sessions need a shell,
77
//! but not every base image ships the same one. Debian-based images provide
88
//! `bash`; minimal images such as Alpine only provide `/bin/sh` (`BusyBox`
99
//! `ash`). Hard-coding `/bin/bash` makes sandbox startup fail on those images
@@ -51,8 +51,9 @@ pub fn is_executable(path: &str) -> bool {
5151
/// Resolve a login shell that exists in the current root filesystem.
5252
///
5353
/// 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.
54+
/// this inspects the filesystem, call it from the sandbox boundary or another
55+
/// process inside the workload filesystem, never from the external supervisor
56+
/// or gateway.
5657
///
5758
/// `$SHELL` is intentionally not consulted: it is image/user-controlled, the
5859
/// result is later invoked with `-lc`, and an executable that is not a

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,10 @@ mod linux {
13201320
provider_env_revision: u64,
13211321
provider_env: std::collections::HashMap<String, String>,
13221322
) -> Response {
1323+
let spec = match resolve_agent_spec(spec) {
1324+
Ok(spec) => spec,
1325+
Err(error) => return guest_error("failed", error),
1326+
};
13231327
let mut state = lock(&self.state);
13241328
let requested = StartedAgent {
13251329
sandbox_id: sandbox_id.clone(),
@@ -1669,6 +1673,24 @@ mod linux {
16691673
ca_file_paths: Option<(std::path::PathBuf, std::path::PathBuf)>,
16701674
}
16711675

1676+
fn resolve_agent_spec(mut spec: AgentSpecWire) -> Result<AgentSpecWire, String> {
1677+
if !spec.program.is_empty() {
1678+
return Ok(spec);
1679+
}
1680+
if !spec.args.is_empty() {
1681+
return Err("default agent command cannot include arguments".to_string());
1682+
}
1683+
let shell = openshell_core::shell::detect_login_shell();
1684+
if !openshell_core::shell::is_executable(&shell) {
1685+
return Err(format!(
1686+
"sandbox image does not provide an executable login shell at {shell}"
1687+
));
1688+
}
1689+
spec.program = shell;
1690+
spec.args = vec!["-l".to_string()];
1691+
Ok(spec)
1692+
}
1693+
16721694
impl ManagedProcess {
16731695
fn spawn(
16741696
runtime: &tokio::runtime::Handle,
@@ -1684,9 +1706,7 @@ mod linux {
16841706
provider_env,
16851707
ca_file_paths,
16861708
} = launch;
1687-
if spec.program.is_empty() {
1688-
return Err("agent program must not be empty".to_string());
1689-
}
1709+
debug_assert!(!spec.program.is_empty());
16901710
let boundary_runtime = BoundaryRuntimeState::new_exclusive_pid_namespace();
16911711
let entrypoint_pid = Arc::new(AtomicU32::new(0));
16921712
let provider_credentials = ProviderCredentialState::from_child_env_snapshot(
@@ -2378,6 +2398,23 @@ mod linux {
23782398
assert_eq!(ledger.entries.len(), MAX_REPLAY_LEDGER_ENTRIES);
23792399
}
23802400

2401+
#[test]
2402+
fn scratch_agent_command_resolves_inside_the_workload_filesystem() {
2403+
let resolved = resolve_agent_spec(AgentSpecWire {
2404+
program: String::new(),
2405+
args: Vec::new(),
2406+
workdir: Some("/sandbox".to_string()),
2407+
timeout_secs: 0,
2408+
interactive: true,
2409+
})
2410+
.expect("resolve scratch command");
2411+
2412+
assert!(openshell_core::shell::is_executable(&resolved.program));
2413+
assert_eq!(resolved.args, vec!["-l".to_string()]);
2414+
assert_eq!(resolved.workdir.as_deref(), Some("/sandbox"));
2415+
assert!(resolved.interactive);
2416+
}
2417+
23812418
fn placeholder_server_tls() -> BoundaryServerTls {
23822419
BoundaryServerTls {
23832420
certificate_chain_path: Path::new("/tmp/openshell-sandbox.crt").to_path_buf(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ async fn handle_create_sandbox_inner(
298298
};
299299

300300
// Leave an omitted command empty rather than persisting a concrete shell:
301-
// the supervisor resolves the default login shell against the sandbox image
301+
// the sandbox boundary resolves the default login shell against the agent image
302302
// (bash when present, otherwise /bin/sh on minimal images like Alpine),
303303
// which the gateway cannot do since it does not see the sandbox filesystem.
304304
// The default is an interactive login shell, so request a TTY.

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,13 @@ pub async fn run_sandbox(
246246
admitted_isolation_backend: Option<String>,
247247
main_exit_marker: Option<std::path::PathBuf>,
248248
) -> Result<i32> {
249-
let (program, args) = command
250-
.split_first()
251-
.ok_or_else(|| miette::miette!("No command specified"))?;
249+
// An empty command is the versioned scratch-sandbox sentinel. The
250+
// external supervisor cannot inspect the workload filesystem, so preserve
251+
// it for openshell-sandbox to resolve against the agent image.
252+
let (program, args) = command.split_first().map_or_else(
253+
|| (String::new(), Vec::new()),
254+
|(program, args)| (program.clone(), args.to_vec()),
255+
);
252256

253257
// Initialize the process-wide OCSF context early so that events emitted
254258
// during policy loading (filesystem config, validation) have a context.
@@ -453,8 +457,8 @@ pub async fn run_sandbox(
453457
sandbox_id: sandbox_id.clone().unwrap_or_default(),
454458
policy: policy.clone(),
455459
agent: openshell_isolation_interface::AgentSpec {
456-
program: program.clone(),
457-
args: args.to_vec(),
460+
program,
461+
args,
458462
workdir: workspace,
459463
timeout_secs,
460464
interactive,

0 commit comments

Comments
 (0)