Skip to content

Commit ffc7284

Browse files
committed
fix(sandbox): deduplicate CDI library paths
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 03f5ffb commit ffc7284

5 files changed

Lines changed: 154 additions & 40 deletions

File tree

‎architecture/security-policy.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ GPU/CDI sandboxes can also carry a supervisor-only CDI context from the compute
3333
driver. The supervisor resolves selected CDI IDs from mounted CDI specs and
3434
adds derived device nodes, parent directories of shared-library mounts, and
3535
supplemental GIDs before agent exec. CDI host paths and non-library read-only
36-
mounts are ignored for policy. Library filenames must end in `.so` or
37-
`.so.<single digit>`. Writable CDI single-file mounts require an exact
36+
mounts are ignored for policy. Library filenames must end in `.so` or a
37+
numeric SONAME suffix such as `.so.1` or `.so.1.2`. A derived library directory
38+
is omitted when an existing read-only or read-write ancestor already covers
39+
it. Writable CDI single-file mounts require an exact
3840
`filesystem_policy.read_write` opt-in, and writable CDI directory mounts fail
3941
closed. CDI resolution errors are security-relevant startup failures and emit
4042
OCSF findings.

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,51 @@ pub fn read_context(path: impl AsRef<Path>) -> Result<CdiContext, CdiError> {
124124
})
125125
}
126126

127+
#[cfg(any(target_os = "linux", test))]
128+
fn shared_library_parent(path: &str) -> Option<String> {
129+
let path = Path::new(path);
130+
let name = path.file_name()?.to_str()?;
131+
let versioned_library = name.rsplit_once(".so.").is_some_and(|(_, version)| {
132+
version.split('.').all(|component| {
133+
!component.is_empty() && component.bytes().all(|byte| byte.is_ascii_digit())
134+
})
135+
});
136+
let unversioned_library = name
137+
.rsplit_once('.')
138+
.is_some_and(|(_, extension)| extension == "so");
139+
if !unversioned_library && !versioned_library {
140+
return None;
141+
}
142+
path.parent()?.to_str().map(ToOwned::to_owned)
143+
}
144+
145+
#[cfg(test)]
146+
mod tests {
147+
use super::shared_library_parent;
148+
149+
#[test]
150+
fn identifies_shared_library_parent_directories() {
151+
for path in [
152+
"/opt/nvidia/lib/libcuda.so",
153+
"/opt/nvidia/lib/libnvidia-ml.so.1",
154+
"/opt/nvidia/lib/libnvidia-eglcore.so.540.5.0",
155+
] {
156+
assert_eq!(
157+
shared_library_parent(path).as_deref(),
158+
Some("/opt/nvidia/lib")
159+
);
160+
}
161+
for path in [
162+
"/etc/ld.so.conf",
163+
"/opt/nvidia/lib/libcuda.so.debug",
164+
"/opt/nvidia/lib/libcuda.so.1beta",
165+
"/usr/bin/nvidia-smi",
166+
] {
167+
assert_eq!(shared_library_parent(path), None);
168+
}
169+
}
170+
}
171+
127172
#[cfg(target_os = "linux")]
128173
#[path = "cdi_linux.rs"]
129174
mod cdi_linux;

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

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

44
//! Linux implementation of CDI policy resolution.
55
6-
use super::{CDI_CONTEXT_VERSION, CdiContext, CdiDerivedRequirements, CdiError, CdiSpecDirectory};
6+
use super::{
7+
CDI_CONTEXT_VERSION, CdiContext, CdiDerivedRequirements, CdiError, CdiSpecDirectory,
8+
shared_library_parent,
9+
};
710
use crate::paths::normalize_path;
811
use container_device_interface::{
912
cache::{Cache, with_auto_refresh},
@@ -172,19 +175,6 @@ impl RequirementAccumulator {
172175
}
173176
}
174177

175-
fn shared_library_parent(path: &str) -> Option<String> {
176-
let path = Path::new(path);
177-
let name = path.file_name()?.to_str()?;
178-
let versioned_library = name
179-
.as_bytes()
180-
.split_last()
181-
.is_some_and(|(last, prefix)| last.is_ascii_digit() && prefix.ends_with(b".so."));
182-
if !name.ends_with(".so") && !versioned_library {
183-
return None;
184-
}
185-
path.parent()?.to_str().map(ToOwned::to_owned)
186-
}
187-
188178
pub fn resolve_cdi_context<S: BuildHasher>(
189179
context: &CdiContext,
190180
writable_file_allowlist: &HashSet<String, S>,
@@ -596,7 +586,7 @@ devices:
596586
}
597587

598588
#[test]
599-
fn resolves_only_supported_shared_library_parent_directories() {
589+
fn resolves_supported_shared_library_parent_directories() {
600590
let dir = tempfile::tempdir().unwrap();
601591
write_spec(
602592
dir.path(),
@@ -614,10 +604,14 @@ devices:
614604
containerPath: /opt/nvidia/lib/libnvidia-ml.so.1
615605
- hostPath: /host/lib/libnvidia-encode.so.9
616606
containerPath: /opt/nvidia/encode/libnvidia-encode.so.9
617-
- hostPath: /host/lib/libunsupported.so.12
618-
containerPath: /opt/nvidia/lib/libunsupported.so.12
619-
- hostPath: /host/lib/libunsupported.so.1.2
620-
containerPath: /opt/nvidia/lib/libunsupported.so.1.2
607+
- hostPath: /host/lib/libnvidia-eglcore.so.540.5.0
608+
containerPath: /opt/nvidia/versioned/libnvidia-eglcore.so.540.5.0
609+
- hostPath: /host/lib/libunsupported.so.debug
610+
containerPath: /opt/nvidia/debug/libunsupported.so.debug
611+
- hostPath: /host/lib/libunsupported.so.1beta
612+
containerPath: /opt/nvidia/debug/libunsupported.so.1beta
613+
- hostPath: /host/config/ld.so.conf
614+
containerPath: /etc/nvidia/ld.so.conf
621615
- hostPath: /host/bin/nvidia-smi
622616
containerPath: /usr/bin/nvidia-smi
623617
- hostPath: /host/config/runtime.json
@@ -634,7 +628,11 @@ devices:
634628

635629
assert_eq!(
636630
requirements.library_directories,
637-
vec!["/opt/nvidia/encode", "/opt/nvidia/lib"]
631+
vec![
632+
"/opt/nvidia/encode",
633+
"/opt/nvidia/lib",
634+
"/opt/nvidia/versioned"
635+
]
638636
);
639637
}
640638

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

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,10 +2023,12 @@ impl EnrichmentPlan {
20232023
}
20242024
}
20252025

2026-
let read_write = read_write
2026+
let mut read_write = read_write
20272027
.into_iter()
20282028
.map(|(path, policy)| EnrichmentPath { path, policy })
2029-
.collect();
2029+
.collect::<Vec<_>>();
2030+
read_only.sort_by(enrichment_path_order);
2031+
read_write.sort_by(enrichment_path_order);
20302032
(read_only, read_write)
20312033
}
20322034

@@ -2075,12 +2077,18 @@ impl EnrichmentPlan {
20752077
if !addition.should_apply("read_only", &path_exists)? {
20762078
continue;
20772079
}
2078-
if !fs.read_only.iter().any(|p| p == &addition.path)
2079-
&& !fs.read_write.iter().any(|p| p == &addition.path)
2080-
{
2081-
fs.read_only.push(addition.path);
2082-
application.record(addition.policy.sources);
2080+
let addition_path = std::path::Path::new(&addition.path);
2081+
if path_is_covered_by(
2082+
addition_path,
2083+
fs.read_only
2084+
.iter()
2085+
.chain(&fs.read_write)
2086+
.map(std::path::Path::new),
2087+
) {
2088+
continue;
20832089
}
2090+
fs.read_only.push(addition.path);
2091+
application.record(addition.policy.sources);
20842092
}
20852093
for addition in read_write {
20862094
if !addition.should_apply("read_write", &path_exists)? {
@@ -2126,12 +2134,19 @@ impl EnrichmentPlan {
21262134
continue;
21272135
}
21282136
let p = PathBuf::from(&addition.path);
2129-
if !policy.filesystem.read_only.contains(&p)
2130-
&& !policy.filesystem.read_write.contains(&p)
2131-
{
2132-
policy.filesystem.read_only.push(p);
2133-
application.record(addition.policy.sources);
2137+
if path_is_covered_by(
2138+
&p,
2139+
policy
2140+
.filesystem
2141+
.read_only
2142+
.iter()
2143+
.chain(&policy.filesystem.read_write)
2144+
.map(PathBuf::as_path),
2145+
) {
2146+
continue;
21342147
}
2148+
policy.filesystem.read_only.push(p);
2149+
application.record(addition.policy.sources);
21352150
}
21362151
for addition in read_write {
21372152
if !addition.should_apply("read_write", &|path| std::path::Path::new(path).exists())? {
@@ -2192,6 +2207,23 @@ impl EnrichmentPlan {
21922207
}
21932208
}
21942209

2210+
fn enrichment_path_order(left: &EnrichmentPath, right: &EnrichmentPath) -> std::cmp::Ordering {
2211+
std::path::Path::new(&left.path)
2212+
.components()
2213+
.count()
2214+
.cmp(&std::path::Path::new(&right.path).components().count())
2215+
.then_with(|| left.path.cmp(&right.path))
2216+
}
2217+
2218+
fn path_is_covered_by<'a>(
2219+
candidate: &std::path::Path,
2220+
existing: impl IntoIterator<Item = &'a std::path::Path>,
2221+
) -> bool {
2222+
existing
2223+
.into_iter()
2224+
.any(|allowed| candidate.starts_with(allowed))
2225+
}
2226+
21952227
/// Returns true if GPU devices are present in the container.
21962228
///
21972229
/// Checks both the native Linux NVIDIA control device (`/dev/nvidiactl`) and
@@ -2877,6 +2909,20 @@ mod baseline_tests {
28772909
assert_eq!(plan.additional_gids(), vec![44]);
28782910
}
28792911

2912+
#[test]
2913+
fn enrichment_plan_orders_paths_by_depth_then_lexically() {
2914+
let mut plan = EnrichmentPlan::default();
2915+
for path in ["/usr/lib/wsl/lib", "/opt/nvidia", "/usr", "/opt"] {
2916+
plan.insert_read_only_path(path, EnrichmentPathPolicy::baseline());
2917+
}
2918+
2919+
let (read_only, _) = plan.paths();
2920+
assert_eq!(
2921+
read_only,
2922+
["/opt", "/usr", "/opt/nvidia", "/usr/lib/wsl/lib"]
2923+
);
2924+
}
2925+
28802926
#[test]
28812927
fn enrichment_plan_merges_baseline_and_runtime_path_sources() {
28822928
let mut plan = EnrichmentPlan::default();
@@ -2890,7 +2936,7 @@ mod baseline_tests {
28902936
}
28912937

28922938
#[test]
2893-
fn proto_cdi_enrichment_adds_derived_paths() {
2939+
fn proto_cdi_enrichment_omits_library_directory_covered_by_policy_parent() {
28942940
let mut policy = openshell_policy::restrictive_default_policy();
28952941
let requirements = openshell_core::cdi::CdiDerivedRequirements {
28962942
device_node_paths: vec!["/dev/dxg".to_string()],
@@ -2909,13 +2955,34 @@ mod baseline_tests {
29092955
);
29102956
let filesystem = policy.filesystem.expect("filesystem policy");
29112957
assert!(
2912-
filesystem
2958+
!filesystem
29132959
.read_only
2914-
.contains(&"/usr/lib/wsl/lib".to_string())
2960+
.contains(&"/usr/lib/wsl/lib".to_string()),
2961+
"the existing /usr rule should cover the derived library directory"
29152962
);
29162963
assert!(filesystem.read_write.contains(&"/dev/dxg".to_string()));
29172964
}
29182965

2966+
#[test]
2967+
fn proto_cdi_enrichment_validates_covered_runtime_path_before_omitting_it() {
2968+
let mut policy = openshell_policy::restrictive_default_policy();
2969+
let requirements = openshell_core::cdi::CdiDerivedRequirements {
2970+
device_node_paths: Vec::new(),
2971+
library_directories: vec!["/usr/lib/wsl/lib".to_string()],
2972+
read_write_mount_paths: Vec::new(),
2973+
additional_gids: Vec::new(),
2974+
};
2975+
2976+
let plan = EnrichmentPlan::cdi_gpu(&requirements);
2977+
let err = plan
2978+
.apply_to_proto_policy_with(&mut policy, |path| path == "/proc")
2979+
.unwrap_err();
2980+
assert!(
2981+
err.to_string().contains("/usr/lib/wsl/lib"),
2982+
"unexpected error: {err}"
2983+
);
2984+
}
2985+
29192986
#[test]
29202987
fn proto_cdi_enrichment_errors_for_missing_runtime_path() {
29212988
let mut policy = openshell_policy::restrictive_default_policy();

‎docs/sandboxes/policies.mdx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ Docker GPU/CDI sandboxes receive CDI-derived filesystem requirements instead
129129
of the hard-coded GPU device and library baseline. The supervisor resolves the
130130
selected CDI device specs inside the sandbox and adds CDI device nodes as
131131
read-write paths. For read-only mounts, it adds only the parent directories of
132-
shared libraries whose filenames end in `.so` or `.so.<single digit>`; other
133-
read-only CDI mounts do not alter the filesystem policy. CDI `additionalGids`
134-
are applied as supplemental groups before agent processes drop privileges.
132+
shared libraries whose filenames end in `.so` or a numeric SONAME suffix such
133+
as `.so.1` or `.so.1.2`; other read-only CDI mounts do not alter the filesystem
134+
policy. OpenShell does not add a library directory when an existing read-only
135+
or read-write parent already covers it. CDI `additionalGids` are applied as
136+
supplemental groups before agent processes drop privileges.
135137

136138
Writable CDI mount destinations are fail-closed. OpenShell accepts a writable
137139
CDI mount only when it targets a single file and that exact path is already

0 commit comments

Comments
 (0)