@@ -247,21 +247,45 @@ impl IsolationBackend for DockerIsolationBackend {
247247 Ok ( CreatedBoundary :: new ( descriptor, Box :: new ( bound) ) )
248248 }
249249
250+ async fn destroy (
251+ & self ,
252+ descriptor : VerifiedTopologyDescriptor ,
253+ sandbox_id : & str ,
254+ ) -> Result < ( ) , BackendError > {
255+ let topology = decode_topology ( & descriptor) ?;
256+ validate_topology_identity ( & topology, sandbox_id, & self . listener_dir ) ?;
257+
258+ let inspected = match self
259+ . docker
260+ . inspect_container ( & topology. container_id , None )
261+ . await
262+ {
263+ Ok ( inspected) => Some ( inspected) ,
264+ Err ( BollardError :: DockerResponseServerError {
265+ status_code : 404 , ..
266+ } ) => None ,
267+ Err ( error) => return Err ( docker_error ( "inspect Docker boundary for destroy" , error) ) ,
268+ } ;
269+ if let Some ( inspected) = inspected {
270+ validate_container_labels (
271+ & inspected,
272+ & topology. sandbox_id ,
273+ & topology. launch_generation ,
274+ & topology. plan_fingerprint ,
275+ ) ?;
276+ remove_container ( & self . docker , & topology. container_id ) . await ?;
277+ }
278+ remove_listener_artifacts ( & topology. listener_path ) ?;
279+ Ok ( ( ) )
280+ }
281+
250282 async fn attach (
251283 & self ,
252284 descriptor : VerifiedTopologyDescriptor ,
253285 sandbox : SandboxContext ,
254286 ) -> Result < Box < dyn BoundBoundary > , BackendError > {
255- let topology: DockerTopology =
256- serde_json:: from_slice ( descriptor. payload ( ) ) . map_err ( |error| {
257- BackendError :: Descriptor ( format ! ( "decode Docker topology: {error}" ) )
258- } ) ?;
259- if topology. sandbox_id != sandbox. sandbox_id {
260- return Err ( BackendError :: Denied ( format ! (
261- "Docker boundary sandbox {:?} does not match admitted sandbox {:?}" ,
262- topology. sandbox_id, sandbox. sandbox_id
263- ) ) ) ;
264- }
287+ let topology = decode_topology ( & descriptor) ?;
288+ validate_topology_identity ( & topology, & sandbox. sandbox_id , & self . listener_dir ) ?;
265289 let inspected = self
266290 . docker
267291 . inspect_container ( & topology. container_id , None )
@@ -381,11 +405,68 @@ fn topology_descriptor(topology: &DockerTopology) -> Result<TopologyDescriptor,
381405 } )
382406}
383407
408+ fn decode_topology (
409+ descriptor : & VerifiedTopologyDescriptor ,
410+ ) -> Result < DockerTopology , BackendError > {
411+ serde_json:: from_slice ( descriptor. payload ( ) )
412+ . map_err ( |error| BackendError :: Descriptor ( format ! ( "decode Docker topology: {error}" ) ) )
413+ }
414+
415+ fn validate_topology_identity (
416+ topology : & DockerTopology ,
417+ sandbox_id : & str ,
418+ listener_dir : & Path ,
419+ ) -> Result < ( ) , BackendError > {
420+ if topology. sandbox_id != sandbox_id {
421+ return Err ( BackendError :: Denied ( format ! (
422+ "Docker boundary sandbox {:?} does not match admitted sandbox {sandbox_id:?}" ,
423+ topology. sandbox_id
424+ ) ) ) ;
425+ }
426+ let key = resource_key ( & topology. sandbox_id , & topology. launch_generation ) ;
427+ let expected_name = format ! ( "openshell-boundary-{key}" ) ;
428+ let expected_listener = listener_dir. join ( format ! ( "{key}.sock" ) ) ;
429+ let expected_metadata_prefix = format ! (
430+ "openshell:{}:{}:" ,
431+ topology. sandbox_id, topology. launch_generation
432+ ) ;
433+ if topology. container_id . is_empty ( )
434+ || topology. container_name != expected_name
435+ || topology. listener_path != expected_listener
436+ || !topology
437+ . listener_metadata
438+ . starts_with ( & expected_metadata_prefix)
439+ || topology. listener_metadata . len ( )
440+ < expected_metadata_prefix. len ( ) + MIN_LISTENER_TOKEN_BYTES
441+ {
442+ return Err ( BackendError :: Denied (
443+ "Docker topology identity does not match its trusted resource key" . to_string ( ) ,
444+ ) ) ;
445+ }
446+ Ok ( ( ) )
447+ }
448+
384449fn validate_existing_container (
385450 inspected : & bollard:: models:: ContainerInspectResponse ,
386451 sandbox_id : & str ,
387452 launch_generation : & str ,
388453 plan_fingerprint : & str ,
454+ ) -> Result < ( ) , BackendError > {
455+ validate_container_labels ( inspected, sandbox_id, launch_generation, plan_fingerprint) ?;
456+ let status = inspected. state . as_ref ( ) . and_then ( |state| state. status ) ;
457+ if status != Some ( ContainerStateStatusEnum :: CREATED ) {
458+ return Err ( BackendError :: Denied ( format ! (
459+ "Docker create/attach prototype requires a non-running container, found {status:?}"
460+ ) ) ) ;
461+ }
462+ Ok ( ( ) )
463+ }
464+
465+ fn validate_container_labels (
466+ inspected : & bollard:: models:: ContainerInspectResponse ,
467+ sandbox_id : & str ,
468+ launch_generation : & str ,
469+ plan_fingerprint : & str ,
389470) -> Result < ( ) , BackendError > {
390471 let labels = inspected
391472 . config
@@ -401,11 +482,42 @@ fn validate_existing_container(
401482 "existing Docker boundary does not match sandbox launch generation" . to_string ( ) ,
402483 ) ) ;
403484 }
404- let status = inspected. state . as_ref ( ) . and_then ( |state| state. status ) ;
405- if status != Some ( ContainerStateStatusEnum :: CREATED ) {
406- return Err ( BackendError :: Denied ( format ! (
407- "Docker create/attach prototype requires a non-running container, found {status:?}"
408- ) ) ) ;
485+ Ok ( ( ) )
486+ }
487+
488+ async fn remove_container ( docker : & Docker , container_id : & str ) -> Result < ( ) , BackendError > {
489+ if let Err ( error) = docker
490+ . remove_container (
491+ container_id,
492+ Some ( RemoveContainerOptionsBuilder :: default ( ) . force ( true ) . build ( ) ) ,
493+ )
494+ . await
495+ && !matches ! (
496+ error,
497+ BollardError :: DockerResponseServerError {
498+ status_code: 404 ,
499+ ..
500+ }
501+ )
502+ {
503+ return Err ( docker_error ( "remove Docker boundary" , error) ) ;
504+ }
505+ Ok ( ( ) )
506+ }
507+
508+ fn remove_listener_artifacts ( listener_path : & Path ) -> Result < ( ) , BackendError > {
509+ for path in [
510+ listener_path. to_path_buf ( ) ,
511+ listener_path. with_extension ( "lock" ) ,
512+ ] {
513+ if let Err ( error) = std:: fs:: remove_file ( & path)
514+ && error. kind ( ) != std:: io:: ErrorKind :: NotFound
515+ {
516+ return Err ( BackendError :: Attach ( format ! (
517+ "remove Docker listener artifact {}: {error}" ,
518+ path. display( )
519+ ) ) ) ;
520+ }
409521 }
410522 Ok ( ( ) )
411523}
@@ -665,24 +777,7 @@ impl BoundaryProcess for DockerProcess {
665777 }
666778
667779 async fn terminate ( & self ) -> Result < ( ) , BackendError > {
668- if let Err ( error) = self
669- . docker
670- . remove_container (
671- & self . container_id ,
672- Some ( RemoveContainerOptionsBuilder :: default ( ) . force ( true ) . build ( ) ) ,
673- )
674- . await
675- && !matches ! (
676- error,
677- BollardError :: DockerResponseServerError {
678- status_code: 404 ,
679- ..
680- }
681- )
682- {
683- return Err ( docker_error ( "remove Docker boundary" , error) ) ;
684- }
685- Ok ( ( ) )
780+ remove_container ( & self . docker , & self . container_id ) . await
686781 }
687782}
688783
@@ -991,6 +1086,30 @@ mod tests {
9911086 ) ;
9921087 }
9931088
1089+ #[ test]
1090+ fn topology_identity_pins_cleanup_to_the_backend_listener_directory ( ) {
1091+ let listener_dir = tempfile:: tempdir ( ) . expect ( "listener tempdir" ) ;
1092+ let key = resource_key ( "sandbox-1" , "generation-1" ) ;
1093+ let topology = DockerTopology {
1094+ sandbox_id : "sandbox-1" . to_string ( ) ,
1095+ launch_generation : "generation-1" . to_string ( ) ,
1096+ container_id : "container-id" . to_string ( ) ,
1097+ container_name : format ! ( "openshell-boundary-{key}" ) ,
1098+ listener_path : listener_dir. path ( ) . join ( format ! ( "{key}.sock" ) ) ,
1099+ listener_metadata : format ! (
1100+ "openshell:sandbox-1:generation-1:{}" ,
1101+ "0" . repeat( MIN_LISTENER_TOKEN_BYTES )
1102+ ) ,
1103+ plan_fingerprint : "fingerprint" . to_string ( ) ,
1104+ } ;
1105+ validate_topology_identity ( & topology, "sandbox-1" , listener_dir. path ( ) )
1106+ . expect ( "matching topology" ) ;
1107+
1108+ let mut escaped = topology;
1109+ escaped. listener_path = listener_dir. path ( ) . join ( "../outside.sock" ) ;
1110+ assert ! ( validate_topology_identity( & escaped, "sandbox-1" , listener_dir. path( ) ) . is_err( ) ) ;
1111+ }
1112+
9941113 #[ tokio:: test]
9951114 #[ ignore = "requires a local Linux Docker daemon, runc seccomp-notify support, and a pre-pulled image" ]
9961115 async fn local_docker_denies_notified_uname_without_in_container_supervisor ( ) {
@@ -1033,6 +1152,11 @@ mod tests {
10331152 . expect ( "uname should not remain blocked" )
10341153 . expect ( "observe uname exit" ) ;
10351154 assert ! ( matches!( status, BoundaryExitStatus :: Exited ( code) if code != 0 ) ) ;
1036- process. terminate ( ) . await . expect ( "remove Docker boundary" ) ;
1155+ drop ( process) ;
1156+ drop ( running) ;
1157+ registry
1158+ . destroy_created ( descriptor, origin, BACKEND_NAME , "sandbox-1" )
1159+ . await
1160+ . expect ( "destroy supervisor-created Docker boundary" ) ;
10371161 }
10381162}
0 commit comments