Skip to content

Commit 180fd66

Browse files
committed
fix(supervisor): tolerate non-empty bounding set when CAP_SETPCAP is unavailable
When running inside rootless Podman on Ubuntu 24.04 with AppArmor's apparmor_restrict_unprivileged_userns=1, prctl(PR_CAPBSET_DROP) returns EPERM even though CAP_SETPCAP may be nominally granted. The capability bounding set remains non-empty, causing the supervisor to abort sandbox creation. Add a new match arm in validate_capability_bounding_set_clear() that tolerates EPERM when the bounding set is non-empty: log a warning and continue, relying on seccomp to block dangerous syscalls. The existing privileged-environment behavior (fail-closed on non-empty success) is unchanged. Emit a parent-side OCSF DetectionFinding alert so the degraded mode is visible to operators and SIEM. The readiness probe performs a non-destructive bounding::drop() on an already-absent capability to detect AppArmor restrictions even when CAP_SETPCAP is nominally present in the effective set. Closes NVIDIA#2069 Signed-off-by: Wayne Sun <[email protected]> Signed-off-by: Wayne Sun <[email protected]>
1 parent 5477e2f commit 180fd66

1 file changed

Lines changed: 89 additions & 30 deletions

File tree

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

Lines changed: 89 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,85 @@ 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 `bounding::clear()`
210+
/// would fail and the bounding set is non-empty. Called once before
211+
/// `pre_exec`/`fork()` so the event reaches the tracing subscriber.
212+
///
213+
/// The probe tries a non-destructive `bounding::drop()` on a capability
214+
/// that is already absent from the bounding set. This triggers the same
215+
/// `prctl(PR_CAPBSET_DROP)` syscall that `bounding::clear()` uses, so
216+
/// AppArmor restrictions that block the syscall are detected even when
217+
/// `CAP_SETPCAP` is nominally present in the effective set.
218+
#[cfg(target_os = "linux")]
219+
fn log_capability_bounding_set_readiness() {
220+
use std::sync::Once;
221+
static PROBED: Once = Once::new();
222+
let mut already_probed = true;
223+
PROBED.call_once(|| already_probed = false);
224+
if already_probed {
225+
return;
226+
}
227+
228+
let bounding = capctl::caps::bounding::probe();
229+
if bounding.is_empty() {
230+
return;
231+
}
232+
233+
// Find a capability NOT in the bounding set so that drop() is a no-op
234+
// when the syscall is permitted. If every known capability is raised
235+
// (unusual), skip the probe — clear() will be attempted in the child
236+
// and the warn!() path handles failure there.
237+
let probe_cap = capctl::caps::Cap::iter().find(|cap| !bounding.has(*cap));
238+
let clear_blocked = probe_cap.is_some_and(|cap| {
239+
capctl::caps::bounding::drop(cap)
240+
.is_err_and(|e| e.code() == libc::EPERM)
241+
});
242+
243+
if !clear_blocked {
244+
return;
245+
}
246+
247+
openshell_ocsf::ocsf_emit!(
248+
openshell_ocsf::DetectionFindingBuilder::new(openshell_ocsf::ctx::ctx())
249+
.activity(openshell_ocsf::ActivityId::Open)
250+
.severity(openshell_ocsf::SeverityId::High)
251+
.confidence(openshell_ocsf::ConfidenceId::High)
252+
.is_alert(true)
253+
.finding_info(
254+
openshell_ocsf::FindingInfo::new(
255+
"bounding-set-clear-blocked",
256+
"Capability Bounding Set Clear Blocked",
257+
)
258+
.with_desc(
259+
"The supervisor cannot clear the child capability bounding set \
260+
because PR_CAPBSET_DROP returns EPERM. \
261+
The child process will rely on seccomp for confinement. \
262+
This is expected in rootless container runtimes with \
263+
AppArmor user-namespace restrictions.",
264+
),
265+
)
266+
.message(format!(
267+
"PR_CAPBSET_DROP blocked, capability bounding set non-empty: {bounding:?}"
268+
))
269+
.build()
270+
);
271+
}
272+
198273
// Pins the pre-seccomp child mount namespace where supervisor identity sockets
199274
// are shadowed. Children enter it with setns before dropping privileges.
200275
#[cfg(target_os = "linux")]
@@ -548,11 +623,14 @@ impl ProcessHandle {
548623
}
549624
}
550625

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.
626+
// Probe Landlock and capability bounding-set availability and emit
627+
// OCSF logs from the parent process where the tracing subscriber is
628+
// functional. The child's pre_exec context cannot reliably emit
629+
// structured logs.
554630
#[cfg(target_os = "linux")]
555631
sandbox::linux::log_sandbox_readiness(policy, workdir);
632+
#[cfg(target_os = "linux")]
633+
log_capability_bounding_set_readiness();
556634

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

11511229
#[test]
11521230
#[cfg(target_os = "linux")]
1153-
fn capability_bounding_set_clear_rejects_nonempty_eperm() {
1231+
fn capability_bounding_set_clear_tolerates_nonempty_eperm() {
11541232
let mut remaining = capctl::caps::CapSet::empty();
11551233
remaining.add(capctl::caps::Cap::CHOWN);
11561234

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());
11641235
assert!(
1165-
result
1166-
.unwrap_err()
1167-
.to_string()
1168-
.contains("Failed to clear child capability bounding set")
1236+
validate_capability_bounding_set_clear(
1237+
Err(capctl::Error::from_code(libc::EPERM)),
1238+
remaining,
1239+
|| panic!("unknown capabilities should not be checked when known caps remain"),
1240+
)
1241+
.is_ok()
11691242
);
11701243
}
11711244

@@ -1270,21 +1343,7 @@ mod tests {
12701343

12711344
let result = drop_privileges(&policy);
12721345

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());
1346+
assert!(result.is_ok(), "drop_privileges failed: {result:?}");
12881347
}
12891348

12901349
#[test]

0 commit comments

Comments
 (0)