@@ -27,6 +27,8 @@ use std::process::Stdio;
2727use std:: sync:: OnceLock ;
2828use tokio:: process:: { Child , Command } ;
2929use tracing:: debug;
30+ #[ cfg( target_os = "linux" ) ]
31+ use tracing:: warn;
3032
3133const SUPERVISOR_ONLY_ENV_VARS : & [ & str ] = & [
3234 openshell_core:: sandbox_env:: SANDBOX_TOKEN ,
@@ -189,12 +191,84 @@ 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) . is_err_and ( |e| e. code ( ) == libc:: EPERM )
240+ } ) ;
241+
242+ if !clear_blocked {
243+ return ;
244+ }
245+
246+ openshell_ocsf:: ocsf_emit!(
247+ openshell_ocsf:: DetectionFindingBuilder :: new( openshell_ocsf:: ctx:: ctx( ) )
248+ . activity( openshell_ocsf:: ActivityId :: Open )
249+ . severity( openshell_ocsf:: SeverityId :: High )
250+ . confidence( openshell_ocsf:: ConfidenceId :: High )
251+ . is_alert( true )
252+ . finding_info(
253+ openshell_ocsf:: FindingInfo :: new(
254+ "bounding-set-clear-blocked" ,
255+ "Capability Bounding Set Clear Blocked" ,
256+ )
257+ . with_desc(
258+ "The supervisor cannot clear the child capability bounding set \
259+ because PR_CAPBSET_DROP returns EPERM. \
260+ The child process will rely on seccomp for confinement. \
261+ This is expected in rootless container runtimes with \
262+ AppArmor user-namespace restrictions.",
263+ ) ,
264+ )
265+ . message( format!(
266+ "PR_CAPBSET_DROP blocked, capability bounding set non-empty: {bounding:?}"
267+ ) )
268+ . build( )
269+ ) ;
270+ }
271+
198272// Pins the pre-seccomp child mount namespace where supervisor identity sockets
199273// are shadowed. Children enter it with setns before dropping privileges.
200274#[ cfg( target_os = "linux" ) ]
@@ -548,11 +622,14 @@ impl ProcessHandle {
548622 }
549623 }
550624
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.
625+ // Probe Landlock and capability bounding-set availability and emit
626+ // OCSF logs from the parent process where the tracing subscriber is
627+ // functional. The child's pre_exec context cannot reliably emit
628+ // structured logs.
554629 #[ cfg( target_os = "linux" ) ]
555630 sandbox:: linux:: log_sandbox_readiness ( policy, workdir) ;
631+ #[ cfg( target_os = "linux" ) ]
632+ log_capability_bounding_set_readiness ( ) ;
556633
557634 // Phase 1 (as root): Prepare Landlock ruleset by opening PathFds.
558635 // This MUST happen before drop_privileges() so that root-only paths
@@ -1150,22 +1227,17 @@ mod tests {
11501227
11511228 #[ test]
11521229 #[ cfg( target_os = "linux" ) ]
1153- fn capability_bounding_set_clear_rejects_nonempty_eperm ( ) {
1230+ fn capability_bounding_set_clear_tolerates_nonempty_eperm ( ) {
11541231 let mut remaining = capctl:: caps:: CapSet :: empty ( ) ;
11551232 remaining. add ( capctl:: caps:: Cap :: CHOWN ) ;
11561233
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( ) ) ;
11641234 assert ! (
1165- result
1166- . unwrap_err( )
1167- . to_string( )
1168- . contains( "Failed to clear child capability bounding set" )
1235+ validate_capability_bounding_set_clear(
1236+ Err ( capctl:: Error :: from_code( libc:: EPERM ) ) ,
1237+ remaining,
1238+ || panic!( "unknown capabilities should not be checked when known caps remain" ) ,
1239+ )
1240+ . is_ok( )
11691241 ) ;
11701242 }
11711243
@@ -1270,21 +1342,7 @@ mod tests {
12701342
12711343 let result = drop_privileges ( & policy) ;
12721344
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( ) ) ;
1345+ assert ! ( result. is_ok( ) , "drop_privileges failed: {result:?}" ) ;
12881346 }
12891347
12901348 #[ test]
0 commit comments