Skip to content

Commit 1c6b125

Browse files
committed
fix(supervisor): gate sbin path repair on CDI policy
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 7480b9b commit 1c6b125

4 files changed

Lines changed: 260 additions & 34 deletions

File tree

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

Lines changed: 135 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
use std::io::{Read, Write};
55
use std::path::Path;
66

7+
use openshell_core::policy::SandboxPolicy;
8+
79
const LOCAL_NO_PROXY: &str = "127.0.0.1,localhost,::1";
8-
pub const DEFAULT_CHILD_PATH: &str = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
10+
pub const DEFAULT_CHILD_PATH: &str = "/usr/local/bin:/usr/bin:/bin";
911
const STANDARD_SBIN_PATHS: &[&str] = &["/usr/local/sbin", "/usr/sbin", "/sbin"];
1012
const ENSURE_STANDARD_SBIN_PATHS_SCRIPT: &str = "for dir in /usr/local/sbin /usr/sbin /sbin; do case \":${PATH:-}:\" in *:\"$dir\":*) ;; *) PATH=\"${PATH:+$PATH:}$dir\" ;; esac; done; export PATH";
1113
const STARTUP_SNIPPET_MARKER: &str = "# OpenShell standard sbin PATH";
@@ -17,14 +19,45 @@ enum StartupFile {
1719
Unsafe,
1820
}
1921

20-
pub fn child_path_from_env() -> String {
21-
std::env::var("PATH")
22+
pub fn standard_sbin_path_repair_enabled(policy: &SandboxPolicy) -> bool {
23+
let cdi_context = std::env::var(openshell_core::sandbox_env::CDI_CONTEXT);
24+
standard_sbin_path_repair_enabled_for_context(policy, cdi_context.as_deref().ok())
25+
}
26+
27+
fn standard_sbin_path_repair_enabled_for_context(
28+
policy: &SandboxPolicy,
29+
cdi_context: Option<&str>,
30+
) -> bool {
31+
cdi_context
32+
.map(str::trim)
33+
.is_some_and(|path| path == openshell_core::cdi::CDI_CONTEXT_PATH)
34+
&& policy_has_standard_sbin_path(policy)
35+
}
36+
37+
fn policy_has_standard_sbin_path(policy: &SandboxPolicy) -> bool {
38+
policy
39+
.filesystem
40+
.read_only
41+
.iter()
42+
.chain(policy.filesystem.read_write.iter())
43+
.any(|path| STANDARD_SBIN_PATHS.iter().any(|dir| path.starts_with(dir)))
44+
}
45+
46+
pub fn child_path_from_env(repair_standard_sbin: bool) -> String {
47+
let path = std::env::var("PATH")
2248
.ok()
2349
.filter(|path| !path.trim().is_empty())
24-
.map_or_else(
25-
|| DEFAULT_CHILD_PATH.to_string(),
26-
|path| path_with_standard_sbin_paths(&path),
27-
)
50+
.unwrap_or_else(|| DEFAULT_CHILD_PATH.to_string());
51+
52+
maybe_path_with_standard_sbin_paths(&path, repair_standard_sbin)
53+
}
54+
55+
pub fn maybe_path_with_standard_sbin_paths(path: &str, repair_standard_sbin: bool) -> String {
56+
if repair_standard_sbin {
57+
path_with_standard_sbin_paths(path)
58+
} else {
59+
path.to_string()
60+
}
2861
}
2962

3063
pub fn path_with_standard_sbin_paths(path: &str) -> String {
@@ -50,6 +83,17 @@ pub fn shell_command_with_standard_sbin_paths(command: &str) -> String {
5083
format!("{ENSURE_STANDARD_SBIN_PATHS_SCRIPT}\n{command}")
5184
}
5285

86+
pub fn maybe_shell_command_with_standard_sbin_paths(
87+
command: &str,
88+
repair_standard_sbin: bool,
89+
) -> String {
90+
if repair_standard_sbin {
91+
shell_command_with_standard_sbin_paths(command)
92+
} else {
93+
command.to_string()
94+
}
95+
}
96+
5397
pub fn install_standard_sbin_path_startup_files(home: Option<&str>) {
5498
let profile_path = Path::new(PROFILE_D_SNIPPET_PATH);
5599
if let Err(error) = write_profile_snippet(profile_path) {
@@ -271,7 +315,10 @@ mod tests {
271315

272316
#[test]
273317
fn path_with_standard_sbin_paths_uses_default_for_empty_path() {
274-
assert_eq!(path_with_standard_sbin_paths(""), DEFAULT_CHILD_PATH);
318+
assert_eq!(
319+
path_with_standard_sbin_paths(""),
320+
"/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin"
321+
);
275322
}
276323

277324
#[test]
@@ -284,12 +331,90 @@ mod tests {
284331

285332
#[test]
286333
fn path_with_standard_sbin_paths_is_idempotent() {
334+
let path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
335+
assert_eq!(path_with_standard_sbin_paths(path), path);
336+
}
337+
338+
#[test]
339+
fn maybe_path_with_standard_sbin_paths_respects_gate() {
340+
let path = "/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin";
341+
342+
assert_eq!(maybe_path_with_standard_sbin_paths(path, false), path);
287343
assert_eq!(
288-
path_with_standard_sbin_paths(DEFAULT_CHILD_PATH),
289-
DEFAULT_CHILD_PATH
344+
maybe_path_with_standard_sbin_paths(path, true),
345+
"/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin"
290346
);
291347
}
292348

349+
#[test]
350+
fn maybe_shell_command_with_standard_sbin_paths_respects_gate() {
351+
assert_eq!(
352+
maybe_shell_command_with_standard_sbin_paths("nvidia-smi -L", false),
353+
"nvidia-smi -L"
354+
);
355+
assert!(
356+
maybe_shell_command_with_standard_sbin_paths("nvidia-smi -L", true)
357+
.contains("/usr/sbin")
358+
);
359+
}
360+
361+
#[test]
362+
fn standard_sbin_repair_requires_expected_cdi_context_and_policy_path() {
363+
let policy = policy_with_read_only(["/usr/sbin/nvidia-smi"]);
364+
365+
assert!(standard_sbin_path_repair_enabled_for_context(
366+
&policy,
367+
Some(openshell_core::cdi::CDI_CONTEXT_PATH)
368+
));
369+
assert!(!standard_sbin_path_repair_enabled_for_context(
370+
&policy,
371+
Some("/tmp/cdi-context.json")
372+
));
373+
assert!(!standard_sbin_path_repair_enabled_for_context(
374+
&policy, None
375+
));
376+
}
377+
378+
#[test]
379+
fn standard_sbin_repair_requires_standard_sbin_policy_path() {
380+
let policy = policy_with_read_only(["/usr/local/bin/nvidia-smi"]);
381+
382+
assert!(!standard_sbin_path_repair_enabled_for_context(
383+
&policy,
384+
Some(openshell_core::cdi::CDI_CONTEXT_PATH)
385+
));
386+
}
387+
388+
#[test]
389+
fn standard_sbin_repair_accepts_read_write_standard_sbin_policy_path() {
390+
let mut policy = policy_with_read_only(std::iter::empty::<&str>());
391+
policy
392+
.filesystem
393+
.read_write
394+
.push("/sbin/vendor-tool".into());
395+
396+
assert!(standard_sbin_path_repair_enabled_for_context(
397+
&policy,
398+
Some(openshell_core::cdi::CDI_CONTEXT_PATH)
399+
));
400+
}
401+
402+
fn policy_with_read_only(
403+
paths: impl IntoIterator<Item = impl Into<std::path::PathBuf>>,
404+
) -> SandboxPolicy {
405+
SandboxPolicy {
406+
version: 1,
407+
filesystem: openshell_core::policy::FilesystemPolicy {
408+
read_only: paths.into_iter().map(Into::into).collect(),
409+
read_write: Vec::new(),
410+
include_workdir: false,
411+
},
412+
network: openshell_core::policy::NetworkPolicy::default(),
413+
landlock: openshell_core::policy::LandlockPolicy::default(),
414+
process: openshell_core::policy::ProcessPolicy::default(),
415+
}
416+
}
417+
293418
#[test]
294419
fn shell_command_with_standard_sbin_paths_extends_runtime_path() {
295420
let command = shell_command_with_standard_sbin_paths("printf '%s' \"$PATH\"");

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

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,20 @@ fn strip_supervisor_only_env(cmd: &mut Command) {
166166
}
167167
}
168168

169-
fn inject_provider_env(cmd: &mut Command, provider_env: &HashMap<String, String>) {
169+
fn inject_provider_env(
170+
cmd: &mut Command,
171+
provider_env: &HashMap<String, String>,
172+
repair_standard_sbin: bool,
173+
) {
170174
for (key, value) in provider_env {
171175
if is_supervisor_only_env_var(key) {
172176
continue;
173177
}
174178
if key == "PATH" {
175-
cmd.env(key, child_env::path_with_standard_sbin_paths(value));
179+
cmd.env(
180+
key,
181+
child_env::maybe_path_with_standard_sbin_paths(value, repair_standard_sbin),
182+
);
176183
} else {
177184
cmd.env(key, value);
178185
}
@@ -563,8 +570,16 @@ fn shell_command_arg_index(args: &[String]) -> Option<usize> {
563570
None
564571
}
565572

566-
fn process_args_with_standard_sbin_paths(program: &str, args: &[String]) -> Vec<String> {
573+
fn process_args_with_standard_sbin_paths(
574+
program: &str,
575+
args: &[String],
576+
repair_standard_sbin: bool,
577+
) -> Vec<String> {
567578
let mut args = args.to_vec();
579+
if !repair_standard_sbin {
580+
return args;
581+
}
582+
568583
let basename = program.rsplit('/').next().unwrap_or(program);
569584
if matches!(basename, "bash" | "sh")
570585
&& let Some(command_index) = shell_command_arg_index(&args)
@@ -654,23 +669,24 @@ impl ProcessHandle {
654669
ca_paths: Option<&(PathBuf, PathBuf)>,
655670
provider_env: &HashMap<String, String>,
656671
) -> Result<Self> {
657-
let args = process_args_with_standard_sbin_paths(program, args);
672+
let repair_standard_sbin = child_env::standard_sbin_path_repair_enabled(policy);
673+
let args = process_args_with_standard_sbin_paths(program, args, repair_standard_sbin);
658674
let mut cmd = Command::new(program);
659675
cmd.args(&args)
660676
.stdin(Stdio::inherit())
661677
.stdout(Stdio::inherit())
662678
.stderr(Stdio::inherit())
663679
.kill_on_drop(true)
664680
.env(openshell_core::sandbox_env::SANDBOX, "1")
665-
.env("PATH", child_env::child_path_from_env());
681+
.env("PATH", child_env::child_path_from_env(repair_standard_sbin));
666682

667683
// Strip supervisor-only identity material from the entrypoint's
668684
// inherited environment. The entrypoint drops to the sandbox user
669685
// before `exec`; without this strip, sandbox code could recover
670686
// supervisor credentials from its inherited environment.
671687
strip_supervisor_only_env(&mut cmd);
672688

673-
inject_provider_env(&mut cmd, provider_env);
689+
inject_provider_env(&mut cmd, provider_env, repair_standard_sbin);
674690

675691
if let Some(dir) = workspace.root() {
676692
cmd.current_dir(dir);
@@ -813,21 +829,22 @@ impl ProcessHandle {
813829
ca_paths: Option<&(PathBuf, PathBuf)>,
814830
provider_env: &HashMap<String, String>,
815831
) -> Result<Self> {
816-
let args = process_args_with_standard_sbin_paths(program, args);
832+
let repair_standard_sbin = child_env::standard_sbin_path_repair_enabled(policy);
833+
let args = process_args_with_standard_sbin_paths(program, args, repair_standard_sbin);
817834
let mut cmd = Command::new(program);
818835
cmd.args(&args)
819836
.stdin(Stdio::inherit())
820837
.stdout(Stdio::inherit())
821838
.stderr(Stdio::inherit())
822839
.kill_on_drop(true)
823840
.env(openshell_core::sandbox_env::SANDBOX, "1")
824-
.env("PATH", child_env::child_path_from_env());
841+
.env("PATH", child_env::child_path_from_env(repair_standard_sbin));
825842

826843
// Strip supervisor-only identity material from the entrypoint's
827844
// inherited environment.
828845
strip_supervisor_only_env(&mut cmd);
829846

830-
inject_provider_env(&mut cmd, provider_env);
847+
inject_provider_env(&mut cmd, provider_env, repair_standard_sbin);
831848

832849
if let Some(dir) = workspace.root() {
833850
cmd.current_dir(dir);
@@ -2252,7 +2269,7 @@ mod tests {
22522269
fn process_args_wrap_shell_c_command_with_standard_sbin_paths() {
22532270
let args = vec!["-lc".to_string(), "nvidia-smi -L".to_string()];
22542271

2255-
let wrapped = process_args_with_standard_sbin_paths("sh", &args);
2272+
let wrapped = process_args_with_standard_sbin_paths("sh", &args, true);
22562273

22572274
assert_eq!(wrapped[0], "-lc");
22582275
assert!(wrapped[1].contains("/usr/sbin"));
@@ -2263,7 +2280,16 @@ mod tests {
22632280
fn process_args_leave_non_shell_commands_unchanged() {
22642281
let args = vec!["nvidia-smi -L".to_string()];
22652282

2266-
let wrapped = process_args_with_standard_sbin_paths("python", &args);
2283+
let wrapped = process_args_with_standard_sbin_paths("python", &args, true);
2284+
2285+
assert_eq!(wrapped, args);
2286+
}
2287+
2288+
#[test]
2289+
fn process_args_do_not_wrap_when_standard_sbin_repair_disabled() {
2290+
let args = vec!["-lc".to_string(), "nvidia-smi -L".to_string()];
2291+
2292+
let wrapped = process_args_with_standard_sbin_paths("sh", &args, false);
22672293

22682294
assert_eq!(wrapped, args);
22692295
}
@@ -2786,15 +2812,15 @@ mod tests {
27862812
))
27872813
.collect();
27882814

2789-
inject_provider_env(&mut cmd, &provider_env);
2815+
inject_provider_env(&mut cmd, &provider_env, false);
27902816

27912817
let output = cmd.output().await.expect("spawn env");
27922818
let stdout = String::from_utf8(output.stdout).expect("utf8");
27932819
assert!(stdout.contains("ANTHROPIC_API_KEY=openshell:resolve:env:ANTHROPIC_API_KEY"));
27942820
}
27952821

27962822
#[tokio::test]
2797-
async fn inject_provider_env_appends_standard_sbin_to_path() {
2823+
async fn inject_provider_env_appends_standard_sbin_to_path_when_enabled() {
27982824
let mut cmd = Command::new("/usr/bin/env");
27992825
cmd.env_clear()
28002826
.stdin(StdStdio::null())
@@ -2806,7 +2832,7 @@ mod tests {
28062832
"/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin".to_string(),
28072833
)]);
28082834

2809-
inject_provider_env(&mut cmd, &provider_env);
2835+
inject_provider_env(&mut cmd, &provider_env, true);
28102836

28112837
let output = cmd.output().await.expect("spawn env");
28122838
assert!(output.status.success());
@@ -2816,6 +2842,31 @@ mod tests {
28162842
}));
28172843
}
28182844

2845+
#[tokio::test]
2846+
async fn inject_provider_env_leaves_path_unchanged_when_standard_sbin_repair_disabled() {
2847+
let mut cmd = Command::new("/usr/bin/env");
2848+
cmd.env_clear()
2849+
.stdin(StdStdio::null())
2850+
.stdout(StdStdio::piped())
2851+
.stderr(StdStdio::null());
2852+
2853+
let provider_env = HashMap::from([(
2854+
"PATH".to_string(),
2855+
"/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin".to_string(),
2856+
)]);
2857+
2858+
inject_provider_env(&mut cmd, &provider_env, false);
2859+
2860+
let output = cmd.output().await.expect("spawn env");
2861+
assert!(output.status.success());
2862+
let stdout = String::from_utf8(output.stdout).expect("utf8");
2863+
assert!(
2864+
stdout
2865+
.lines()
2866+
.any(|line| { line == "PATH=/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin" })
2867+
);
2868+
}
2869+
28192870
#[cfg(unix)]
28202871
fn sandbox_policy_with_read_write(
28212872
path: PathBuf,
@@ -3693,7 +3744,7 @@ mod tests {
36933744
),
36943745
]);
36953746

3696-
inject_provider_env(&mut cmd, &provider_env);
3747+
inject_provider_env(&mut cmd, &provider_env, false);
36973748

36983749
let output = cmd.output().await.expect("spawn env");
36993750
assert!(output.status.success());

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ pub async fn run_process(
104104
)?;
105105
}
106106

107-
crate::child_env::install_standard_sbin_path_startup_files(workspace.home());
107+
if crate::child_env::standard_sbin_path_repair_enabled(policy) {
108+
crate::child_env::install_standard_sbin_path_startup_files(workspace.home());
109+
}
108110

109111
// Eagerly fetch initial settings and install the agent skill if the
110112
// proposals flag is on at startup, rather than waiting for the policy

0 commit comments

Comments
 (0)