Skip to content

Commit 9ab5230

Browse files
committed
feat(supervisor): derive policy from CDI context
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 4913587 commit 9ab5230

9 files changed

Lines changed: 1567 additions & 299 deletions

File tree

‎architecture/security-policy.md‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,30 @@ parameter names are open user-data maps rather than schema extensions.
4343

4444
Before applying Landlock, the supervisor enriches baseline filesystem paths that
4545
the runtime needs. Missing baseline paths are skipped so one absent runtime path
46-
does not weaken the whole ruleset. When GPU devices are present, GPU baseline
47-
enrichment adds existing GPU device nodes as read-write paths and promotes
48-
`/proc` to read-write because CUDA workloads write thread metadata under
49-
`/proc/<pid>/task/<tid>/comm`.
46+
does not weaken the whole ruleset. When GPU devices are present without a CDI
47+
context, GPU baseline enrichment adds existing GPU device nodes as read-write
48+
paths. GPU sandboxes with CDI context use CDI-derived paths instead of the
49+
hard-coded GPU baseline. Both paths grant read-only access to the existing
50+
container sysfs view because CUDA reads hardware and driver topology during
51+
initialization, and promote `/proc` to read-write because CUDA workloads write
52+
thread metadata under `/proc/<pid>/task/<tid>/comm`.
53+
54+
GPU/CDI sandboxes can also carry a supervisor-only CDI context from the compute
55+
driver. The supervisor resolves selected CDI IDs from mounted CDI specs and
56+
adds derived device nodes, exact destinations of non-library read-only mounts,
57+
parent directories of shared-library mounts, and supplemental GIDs before agent
58+
exec. CDI host paths are ignored for policy. Library filenames must end in
59+
`.so` or a numeric SONAME suffix with any number of dot-separated components,
60+
such as `.so.1`, `.so.1.2`, or `.so.1.2.3`. A derived library directory or
61+
non-library mount is omitted when an existing read-only or read-write ancestor
62+
already covers it. Writable CDI single-file mounts require an exact
63+
`filesystem_policy.read_write` opt-in, and writable CDI directory mounts fail
64+
closed. CDI resolution errors are security-relevant startup failures and emit
65+
OCSF findings.
66+
67+
The `/sys` grant is a supervisor-owned GPU compatibility baseline, not a
68+
CDI-derived permission. CDI specs remain unable to request broad `/sys` access,
69+
and non-GPU sandboxes do not receive the grant.
5070

5171
Landlock rules are tailored to the inode type reported by the already-opened
5272
path descriptor. Directories retain the requested directory and file rights;

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use serde::{Deserialize, Serialize};
99

1010
pub const CDI_CONTEXT_VERSION: u32 = 1;
1111

12+
/// Absolute supervisor path for the CDI context file mounted by a compute driver.
13+
pub const CDI_CONTEXT_PATH: &str = "/run/openshell/supervisor/cdi-context.json";
14+
15+
/// Base supervisor path under which compute drivers mount CDI specification directories.
16+
pub const CDI_SPEC_DIR_BASE: &str = "/run/openshell/supervisor/cdi-specs";
17+
1218
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1319
pub struct CdiContext {
1420
pub version: u32,
@@ -46,7 +52,9 @@ impl CdiSpecDirectory {
4652
#[derive(Debug, Clone, Default, PartialEq, Eq)]
4753
pub struct CdiDerivedRequirements {
4854
pub device_node_paths: Vec<String>,
49-
pub read_only_mount_paths: Vec<String>,
55+
/// Derived read-only policy paths. Shared-library mounts contribute their
56+
/// parent directory; other read-only mounts contribute their exact path.
57+
pub read_only_paths: Vec<String>,
5058
pub read_write_mount_paths: Vec<String>,
5159
pub additional_gids: Vec<u32>,
5260
}
@@ -118,6 +126,51 @@ pub fn read_context(path: impl AsRef<Path>) -> Result<CdiContext, CdiError> {
118126
})
119127
}
120128

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

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

Lines changed: 65 additions & 10 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},
@@ -151,17 +154,19 @@ impl RequirementAccumulator {
151154
}
152155

153156
fn build(self) -> CdiDerivedRequirements {
154-
let mut read_only_mount_paths = Vec::new();
157+
let mut read_only_paths = BTreeSet::new();
155158
let mut read_write_mount_paths = Vec::new();
156159
for (path, access) in self.mount_paths {
157160
match access {
158-
CdiAccess::ReadOnly => read_only_mount_paths.push(path),
161+
CdiAccess::ReadOnly => {
162+
read_only_paths.insert(shared_library_parent(&path).unwrap_or(path));
163+
}
159164
CdiAccess::ReadWrite => read_write_mount_paths.push(path),
160165
}
161166
}
162167
CdiDerivedRequirements {
163168
device_node_paths: self.device_node_paths.into_iter().collect(),
164-
read_only_mount_paths,
169+
read_only_paths: read_only_paths.into_iter().collect(),
165170
read_write_mount_paths,
166171
additional_gids: self.additional_gids.into_iter().collect(),
167172
}
@@ -472,10 +477,7 @@ devices:
472477
requirements.device_node_paths,
473478
vec!["/dev/nvidia0", "/dev/nvidiactl"]
474479
);
475-
assert_eq!(
476-
requirements.read_only_mount_paths,
477-
vec!["/usr/local/cuda/lib64/libcuda.so.1"]
478-
);
480+
assert_eq!(requirements.read_only_paths, vec!["/usr/local/cuda/lib64"]);
479481
}
480482

481483
#[test]
@@ -538,9 +540,62 @@ devices:
538540
.unwrap();
539541

540542
assert_eq!(requirements.device_node_paths, vec!["/dev/dxg"]);
543+
assert_eq!(requirements.read_only_paths, vec!["/usr/lib/wsl/lib"]);
544+
}
545+
546+
#[test]
547+
fn resolves_supported_shared_library_parent_directories() {
548+
let dir = tempfile::tempdir().unwrap();
549+
write_spec(
550+
dir.path(),
551+
"libraries.yaml",
552+
r#"
553+
cdiVersion: 1.1.0
554+
kind: nvidia.com/gpu
555+
devices:
556+
- name: "0"
557+
containerEdits:
558+
mounts:
559+
- hostPath: /host/lib/libcuda.so
560+
containerPath: /opt/nvidia/lib/libcuda.so
561+
- hostPath: /host/lib/libnvidia-ml.so.1
562+
containerPath: /opt/nvidia/lib/libnvidia-ml.so.1
563+
- hostPath: /host/lib/libnvidia-encode.so.9
564+
containerPath: /opt/nvidia/encode/libnvidia-encode.so.9
565+
- hostPath: /host/lib/libnvidia-eglcore.so.540.5.0
566+
containerPath: /opt/nvidia/versioned/libnvidia-eglcore.so.540.5.0
567+
- hostPath: /host/lib/libunsupported.so.debug
568+
containerPath: /opt/nvidia/debug/libunsupported.so.debug
569+
- hostPath: /host/lib/libunsupported.so.1beta
570+
containerPath: /opt/nvidia/debug/libunsupported.so.1beta
571+
- hostPath: /host/config/ld.so.conf
572+
containerPath: /etc/nvidia/ld.so.conf
573+
- hostPath: /host/bin/nvidia-smi
574+
containerPath: /usr/bin/nvidia-smi
575+
- hostPath: /host/config/runtime.json
576+
containerPath: /etc/nvidia/runtime.json
577+
"#,
578+
);
579+
580+
let requirements = resolve_with_kind(
581+
&context(dir.path(), &["nvidia.com/gpu=0"]),
582+
&[],
583+
always_missing,
584+
)
585+
.unwrap();
586+
541587
assert_eq!(
542-
requirements.read_only_mount_paths,
543-
vec!["/usr/lib/wsl/lib/libcuda.so.1"]
588+
requirements.read_only_paths,
589+
vec![
590+
"/etc/nvidia/ld.so.conf",
591+
"/etc/nvidia/runtime.json",
592+
"/opt/nvidia/debug/libunsupported.so.1beta",
593+
"/opt/nvidia/debug/libunsupported.so.debug",
594+
"/opt/nvidia/encode",
595+
"/opt/nvidia/lib",
596+
"/opt/nvidia/versioned",
597+
"/usr/bin/nvidia-smi",
598+
]
544599
);
545600
}
546601

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ pub struct ProcessPolicy {
8383

8484
/// Group name to run the sandboxed process as.
8585
pub run_as_group: Option<String>,
86+
87+
/// Linux supplemental groups to apply before dropping privileges.
88+
///
89+
/// Runtime-specific inputs can use different terminology; CDI
90+
/// `additionalGids` are converted into this process-level representation.
91+
pub supplemental_groups: Vec<u32>,
8692
}
8793

8894
#[derive(Debug, Clone, Default)]
@@ -185,6 +191,7 @@ impl From<ProtoProcessPolicy> for ProcessPolicy {
185191
} else {
186192
Some(proto.run_as_group)
187193
},
194+
supplemental_groups: Vec::new(),
188195
}
189196
}
190197
}

‎crates/openshell-sandbox-backend/src/boundary_protocol.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,8 @@ pub struct SandboxPolicyWire {
919919
pub landlock: LandlockCompatibilityWire,
920920
pub run_as_user: Option<String>,
921921
pub run_as_group: Option<String>,
922+
#[serde(default)]
923+
pub supplemental_groups: Vec<u32>,
922924
}
923925

924926
impl From<SandboxPolicy> for SandboxPolicyWire {
@@ -943,6 +945,7 @@ impl From<SandboxPolicy> for SandboxPolicyWire {
943945
let ProcessPolicy {
944946
run_as_user,
945947
run_as_group,
948+
supplemental_groups,
946949
} = process;
947950
Self {
948951
version,
@@ -954,6 +957,7 @@ impl From<SandboxPolicy> for SandboxPolicyWire {
954957
landlock: LandlockCompatibilityWire::from(compatibility),
955958
run_as_user,
956959
run_as_group,
960+
supplemental_groups,
957961
}
958962
}
959963
}
@@ -980,6 +984,7 @@ impl From<SandboxPolicyWire> for SandboxPolicy {
980984
process: ProcessPolicy {
981985
run_as_user: policy.run_as_user,
982986
run_as_group: policy.run_as_group,
987+
supplemental_groups: policy.supplemental_groups,
983988
},
984989
}
985990
}

0 commit comments

Comments
 (0)