@@ -234,12 +234,87 @@ fn validate_capability_bounding_set_clear(
234234 "Failed to clear unknown child capability bounding set entries: {unknown_err}"
235235 ) ) ,
236236 } ,
237+ Err ( err) if err. code ( ) == libc:: EPERM => Err ( miette:: miette!(
238+ "Failed to clear child capability bounding set: CAP_SETPCAP is not in the \
239+ effective set (EPERM) and capabilities remain raised: {remaining:?}; \
240+ configure the container runtime to grant CAP_SETPCAP to the supervisor"
241+ ) ) ,
237242 Err ( err) => Err ( miette:: miette!(
238243 "Failed to clear child capability bounding set: {err}"
239244 ) ) ,
240245 }
241246}
242247
248+ /// Preflight check for the child capability bounding-set clear, run in the
249+ /// parent before `fork()`. The clear is fail-closed inside `pre_exec`, a
250+ /// context that cannot reliably emit structured logs — without this check a
251+ /// blocked clear surfaces only as an opaque child exit.
252+ ///
253+ /// The probe tries a non-destructive `bounding::drop()` on a capability
254+ /// that is already absent from the bounding set. This triggers the same
255+ /// `prctl(PR_CAPBSET_DROP)` syscall that `bounding::clear()` uses, so
256+ /// environments where `CAP_SETPCAP` is missing from the effective set
257+ /// (e.g. container runtimes that do not grant it, LSM restrictions) are
258+ /// detected here. On detection, emit an OCSF `DetectionFinding` and refuse
259+ /// the spawn with a diagnostic error.
260+ #[ cfg( target_os = "linux" ) ]
261+ fn check_capability_bounding_set_readiness ( ) -> Result < ( ) > {
262+ // The child clears the bounding set only when it starts as root.
263+ if !nix:: unistd:: geteuid ( ) . is_root ( ) {
264+ return Ok ( ( ) ) ;
265+ }
266+
267+ let bounding = capctl:: caps:: bounding:: probe ( ) ;
268+ // An EPERM clear is tolerated when the set is already empty.
269+ if bounding. is_empty ( ) {
270+ return Ok ( ( ) ) ;
271+ }
272+
273+ // Find a capability NOT in the bounding set so that drop() is a no-op
274+ // when the syscall is permitted. If every known capability is raised
275+ // (unusual), skip the probe — clear() is attempted in the child and
276+ // fails the spawn there.
277+ let probe_cap = capctl:: caps:: Cap :: iter ( ) . find ( |cap| !bounding. has ( * cap) ) ;
278+ let clear_blocked = probe_cap. is_some_and ( |cap| {
279+ capctl:: caps:: bounding:: drop ( cap) . is_err_and ( |e| e. code ( ) == libc:: EPERM )
280+ } ) ;
281+
282+ if !clear_blocked {
283+ return Ok ( ( ) ) ;
284+ }
285+
286+ openshell_ocsf:: ocsf_emit!(
287+ openshell_ocsf:: DetectionFindingBuilder :: new( openshell_ocsf:: ctx:: ctx( ) )
288+ . activity( openshell_ocsf:: ActivityId :: Open )
289+ . severity( openshell_ocsf:: SeverityId :: High )
290+ . confidence( openshell_ocsf:: ConfidenceId :: High )
291+ . is_alert( true )
292+ . finding_info(
293+ openshell_ocsf:: FindingInfo :: new(
294+ "bounding-set-clear-blocked" ,
295+ "Capability Bounding Set Clear Blocked" ,
296+ )
297+ . with_desc(
298+ "The supervisor cannot clear the child capability bounding set \
299+ because PR_CAPBSET_DROP returns EPERM — CAP_SETPCAP is not \
300+ in the effective set. The spawn is refused to keep privilege \
301+ drop fail-closed. Configure the container runtime to grant \
302+ CAP_SETPCAP to the supervisor.",
303+ ) ,
304+ )
305+ . message( format!(
306+ "PR_CAPBSET_DROP blocked, refusing spawn with non-empty capability bounding set: {bounding:?}"
307+ ) )
308+ . build( )
309+ ) ;
310+
311+ Err ( miette:: miette!(
312+ "Cannot clear the child capability bounding set: PR_CAPBSET_DROP returns EPERM \
313+ (CAP_SETPCAP is not in the effective set) and capabilities remain raised: {bounding:?}; \
314+ configure the container runtime to grant CAP_SETPCAP to the supervisor"
315+ ) )
316+ }
317+
243318// Pins the pre-seccomp child mount namespace where supervisor identity sockets
244319// are shadowed. Children enter it with setns before dropping privileges.
245320#[ cfg( target_os = "linux" ) ]
@@ -607,6 +682,15 @@ impl ProcessHandle {
607682 sandbox:: linux:: log_sandbox_readiness ( policy, workdir) ;
608683 }
609684
685+ // Fail fast when the child's capability bounding-set clear would
686+ // fail: the clear is fail-closed inside pre_exec, which cannot emit
687+ // structured logs, so the parent probes first and refuses the spawn
688+ // with a diagnostic error and an OCSF finding.
689+ #[ cfg( target_os = "linux" ) ]
690+ if enforcement_mode. uses_privileged_process_setup ( ) {
691+ check_capability_bounding_set_readiness ( ) ?;
692+ }
693+
610694 // Phase 1: Prepare Landlock ruleset by opening PathFds.
611695 // In full mode this runs before drop_privileges() so root-only paths
612696 // can be opened. In sidecar network-only mode the container already
@@ -1542,6 +1626,21 @@ mod tests {
15421626 || capctl:: caps:: bounding:: probe ( ) . is_empty ( )
15431627 }
15441628
1629+ #[ test]
1630+ #[ cfg( target_os = "linux" ) ]
1631+ fn capability_bounding_set_readiness_matches_clear_availability ( ) {
1632+ let result = check_capability_bounding_set_readiness ( ) ;
1633+ if !nix:: unistd:: geteuid ( ) . is_root ( )
1634+ || capctl:: caps:: bounding:: probe ( ) . is_empty ( )
1635+ || capability_bounding_set_clear_available ( )
1636+ {
1637+ assert ! ( result. is_ok( ) , "readiness check failed: {result:?}" ) ;
1638+ } else {
1639+ let msg = format ! ( "{}" , result. unwrap_err( ) ) ;
1640+ assert ! ( msg. contains( "CAP_SETPCAP" ) , "unexpected error: {msg}" ) ;
1641+ }
1642+ }
1643+
15451644 #[ test]
15461645 #[ cfg( target_os = "linux" ) ]
15471646 fn capability_bounding_set_clear_accepts_empty_eperm ( ) {
@@ -1570,12 +1669,9 @@ mod tests {
15701669 ) ;
15711670
15721671 assert ! ( result. is_err( ) ) ;
1573- assert ! (
1574- result
1575- . unwrap_err( )
1576- . to_string( )
1577- . contains( "Failed to clear child capability bounding set" )
1578- ) ;
1672+ let msg = result. unwrap_err ( ) . to_string ( ) ;
1673+ assert ! ( msg. contains( "Failed to clear child capability bounding set" ) ) ;
1674+ assert ! ( msg. contains( "CAP_SETPCAP" ) ) ;
15791675 }
15801676
15811677 #[ test]
0 commit comments