Skip to content

Commit 8ee29c1

Browse files
committed
fix(supervisor): tolerate non-empty bounding set when CAP_SETPCAP is unavailable
On rootless Podman with AppArmor unprivileged user namespace restrictions (Ubuntu 24.04 default), CAP_SETPCAP is granted by Podman but blocked at the kernel level by AppArmor. This causes drop_capability_bounding_set() to fail with EPERM while the bounding set is non-empty — a case that was unhandled, crashing sandbox creation. Add a match arm that logs a warning and continues when EPERM is returned with a non-empty bounding set. The child process is still confined by seccomp. Add a parent-side OCSF DetectionFinding alert (log_capability_bounding_set_readiness) emitted before fork, following the same pattern as Landlock's log_sandbox_readiness(). This ensures the degraded confinement posture appears in structured security telemetry. Fixes: NVIDIA#2069 Signed-off-by: Wayne Sun <[email protected]>
1 parent 5477e2f commit 8ee29c1

1 file changed

Lines changed: 72 additions & 30 deletions

File tree

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

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use std::process::Stdio;
2727
use std::sync::OnceLock;
2828
use tokio::process::{Child, Command};
2929
use tracing::debug;
30+
#[cfg(target_os = "linux")]
31+
use tracing::warn;
3032

3133
const SUPERVISOR_ONLY_ENV_VARS: &[&str] = &[
3234
openshell_core::sandbox_env::SANDBOX_TOKEN,
@@ -189,12 +191,68 @@ fn validate_capability_bounding_set_clear(
189191
"Failed to clear unknown child capability bounding set entries: {unknown_err}"
190192
)),
191193
},
194+
Err(err) if err.code() == libc::EPERM => {
195+
warn!(
196+
?remaining,
197+
"CAP_SETPCAP is unavailable and the child capability bounding set is non-empty; \
198+
the child process relies on seccomp for confinement"
199+
);
200+
Ok(())
201+
}
192202
Err(err) => Err(miette::miette!(
193203
"Failed to clear child capability bounding set: {err}"
194204
)),
195205
}
196206
}
197207

208+
/// Probe capability bounding-set availability and emit an OCSF
209+
/// `DetectionFinding` from the parent process when `CAP_SETPCAP` is
210+
/// unavailable and the bounding set is non-empty. Called once before
211+
/// `pre_exec`/`fork()` so the event reaches the tracing subscriber.
212+
#[cfg(target_os = "linux")]
213+
fn log_capability_bounding_set_readiness() {
214+
use std::sync::Once;
215+
static PROBED: Once = Once::new();
216+
let mut already_probed = true;
217+
PROBED.call_once(|| already_probed = false);
218+
if already_probed {
219+
return;
220+
}
221+
222+
let has_setpcap = capctl::caps::CapState::get_current()
223+
.is_ok_and(|state| state.effective.has(capctl::caps::Cap::SETPCAP));
224+
let bounding = capctl::caps::bounding::probe();
225+
226+
if has_setpcap || bounding.is_empty() {
227+
return;
228+
}
229+
230+
openshell_ocsf::ocsf_emit!(
231+
openshell_ocsf::DetectionFindingBuilder::new(openshell_ocsf::ctx::ctx())
232+
.activity(openshell_ocsf::ActivityId::Open)
233+
.severity(openshell_ocsf::SeverityId::High)
234+
.confidence(openshell_ocsf::ConfidenceId::High)
235+
.is_alert(true)
236+
.finding_info(
237+
openshell_ocsf::FindingInfo::new(
238+
"setpcap-unavailable",
239+
"CAP_SETPCAP Unavailable With Non-Empty Bounding Set",
240+
)
241+
.with_desc(
242+
"The supervisor cannot clear the child capability bounding set \
243+
because CAP_SETPCAP is not in the effective set. \
244+
The child process will rely on seccomp for confinement. \
245+
This is expected in rootless container runtimes with \
246+
AppArmor user-namespace restrictions.",
247+
),
248+
)
249+
.message(format!(
250+
"CAP_SETPCAP unavailable, capability bounding set non-empty: {bounding:?}"
251+
))
252+
.build()
253+
);
254+
}
255+
198256
// Pins the pre-seccomp child mount namespace where supervisor identity sockets
199257
// are shadowed. Children enter it with setns before dropping privileges.
200258
#[cfg(target_os = "linux")]
@@ -548,11 +606,14 @@ impl ProcessHandle {
548606
}
549607
}
550608

551-
// Probe Landlock availability and emit OCSF logs from the parent
552-
// process where the tracing subscriber is functional. The child's
553-
// pre_exec context cannot reliably emit structured logs.
609+
// Probe Landlock and capability bounding-set availability and emit
610+
// OCSF logs from the parent process where the tracing subscriber is
611+
// functional. The child's pre_exec context cannot reliably emit
612+
// structured logs.
554613
#[cfg(target_os = "linux")]
555614
sandbox::linux::log_sandbox_readiness(policy, workdir);
615+
#[cfg(target_os = "linux")]
616+
log_capability_bounding_set_readiness();
556617

557618
// Phase 1 (as root): Prepare Landlock ruleset by opening PathFds.
558619
// This MUST happen before drop_privileges() so that root-only paths
@@ -1150,22 +1211,17 @@ mod tests {
11501211

11511212
#[test]
11521213
#[cfg(target_os = "linux")]
1153-
fn capability_bounding_set_clear_rejects_nonempty_eperm() {
1214+
fn capability_bounding_set_clear_tolerates_nonempty_eperm() {
11541215
let mut remaining = capctl::caps::CapSet::empty();
11551216
remaining.add(capctl::caps::Cap::CHOWN);
11561217

1157-
let result = validate_capability_bounding_set_clear(
1158-
Err(capctl::Error::from_code(libc::EPERM)),
1159-
remaining,
1160-
|| panic!("unknown capabilities should not be checked when known caps remain"),
1161-
);
1162-
1163-
assert!(result.is_err());
11641218
assert!(
1165-
result
1166-
.unwrap_err()
1167-
.to_string()
1168-
.contains("Failed to clear child capability bounding set")
1219+
validate_capability_bounding_set_clear(
1220+
Err(capctl::Error::from_code(libc::EPERM)),
1221+
remaining,
1222+
|| panic!("unknown capabilities should not be checked when known caps remain"),
1223+
)
1224+
.is_ok()
11691225
);
11701226
}
11711227

@@ -1270,21 +1326,7 @@ mod tests {
12701326

12711327
let result = drop_privileges(&policy);
12721328

1273-
#[cfg(target_os = "linux")]
1274-
{
1275-
if capability_bounding_set_clear_available() {
1276-
assert!(result.is_ok(), "drop_privileges failed: {result:?}");
1277-
} else {
1278-
let msg = format!("{}", result.unwrap_err());
1279-
assert!(
1280-
msg.contains("Failed to clear child capability bounding set"),
1281-
"unexpected failure: {msg}"
1282-
);
1283-
}
1284-
}
1285-
1286-
#[cfg(not(target_os = "linux"))]
1287-
assert!(result.is_ok());
1329+
assert!(result.is_ok(), "drop_privileges failed: {result:?}");
12881330
}
12891331

12901332
#[test]

0 commit comments

Comments
 (0)