@@ -308,6 +308,10 @@ mod linux {
308308 config : GuestConfig ,
309309 process_runtime : tokio:: runtime:: Handle ,
310310 state : Mutex < RuntimeState > ,
311+ /// The wire policy bound at first attach, so an idempotent attach retry
312+ /// carrying a different policy is denied instead of silently keeping
313+ /// the first policy.
314+ attached_policy : Mutex < Option < SandboxPolicyWire > > ,
311315 next_exec_id : AtomicU64 ,
312316 exec_handles : Mutex < std:: collections:: HashMap < String , ExecHandle > > ,
313317 }
@@ -425,6 +429,7 @@ mod linux {
425429 config,
426430 process_runtime,
427431 state : Mutex :: new ( RuntimeState :: AwaitingAttach ) ,
432+ attached_policy : Mutex :: new ( None ) ,
428433 next_exec_id : AtomicU64 :: new ( 1 ) ,
429434 exec_handles : Mutex :: new ( std:: collections:: HashMap :: new ( ) ) ,
430435 }
@@ -435,7 +440,7 @@ mod linux {
435440 return guest_error ( "denied" , "control authentication failed" ) ;
436441 }
437442 match envelope. request {
438- Request :: Attach { policy } => self . attach ( ( * policy) . into ( ) ) ,
443+ Request :: Attach { policy } => self . attach ( * policy) ,
439444 Request :: Confirm => self . confirm ( ) ,
440445 Request :: StartAgent {
441446 sandbox_id,
@@ -572,27 +577,40 @@ mod linux {
572577 session : ExecSession ,
573578 ) -> Result < ( ) , String > {
574579 let process_id = process_id. to_string ( ) ;
575- self . process_runtime . block_on ( async move {
580+ let bridged = self . process_runtime . block_on ( async move {
576581 let stream = stream. into_tokio ( ) ?;
577582 bridge_exec_stream ( stream, session) . await
578- } ) ?;
583+ } ) ;
584+ // Remove the handle even when the bridge fails; a failed stream
585+ // must not leak its exec registration.
579586 lock ( & self . exec_handles ) . remove ( & process_id) ;
580- Ok ( ( ) )
587+ bridged
581588 }
582589
583- fn attach ( & self , policy : openshell_core :: policy :: SandboxPolicy ) -> Response {
590+ fn attach ( & self , policy : SandboxPolicyWire ) -> Response {
584591 let mut state = lock ( & self . state ) ;
585592 match & * state {
586593 RuntimeState :: AwaitingAttach => {
587- let prepared = match PreparedBoundary :: establish ( & self . process_runtime , & policy)
588- {
594+ let prepared = match PreparedBoundary :: establish (
595+ & self . process_runtime ,
596+ & policy. clone ( ) . into ( ) ,
597+ ) {
589598 Ok ( prepared) => prepared,
590599 Err ( error) => return guest_error ( "failed" , error) ,
591600 } ;
601+ * lock ( & self . attached_policy ) = Some ( policy) ;
592602 * state = RuntimeState :: Bound ( prepared) ;
593603 Response :: Attached
594604 }
595- RuntimeState :: Bound ( _) => Response :: Attached ,
605+ RuntimeState :: Bound ( _) => {
606+ // Idempotent retry of the same attach; a different policy
607+ // must not be silently coalesced onto the bound boundary.
608+ if lock ( & self . attached_policy ) . as_ref ( ) == Some ( & policy) {
609+ Response :: Attached
610+ } else {
611+ guest_error ( "denied" , "attach policy does not match the bound boundary" )
612+ }
613+ }
596614 _ => guest_error ( "invalid" , "boundary has already advanced past attach" ) ,
597615 }
598616 }
0 commit comments