@@ -16,6 +16,7 @@ use camino::{Utf8Path, Utf8PathBuf};
1616use cap_std_ext:: cap_std;
1717use cap_std_ext:: cap_std:: fs:: { Dir , MetadataExt } ;
1818use cap_std_ext:: cmdext:: CapStdExtCommandExt ;
19+ use cap_std_ext:: dirext:: CapStdExtDirExt ;
1920use containers_image_proxy:: { ImageProxy , OpenedImage } ;
2021use flate2:: Compression ;
2122use fn_error_context:: context;
@@ -462,6 +463,21 @@ fn timestamp_of_manifest_or_config(
462463 . log_err_default ( )
463464}
464465
466+ /// Automatically clean up files that may have been injected by container
467+ /// builds. xref https://github.com/containers/buildah/issues/4242
468+ fn cleanup_root ( root : & Dir ) -> Result < ( ) > {
469+ const RUNTIME_INJECTED : & [ & str ] = & [ "etc/hostname" , "etc/resolv.conf" ] ;
470+ for ent in RUNTIME_INJECTED {
471+ if let Some ( meta) = root. symlink_metadata_optional ( ent) ? {
472+ if meta. is_file ( ) && meta. size ( ) == 0 {
473+ tracing:: debug!( "Removing {ent}" ) ;
474+ root. remove_file ( ent) ?;
475+ }
476+ }
477+ }
478+ Ok ( ( ) )
479+ }
480+
465481impl ImageImporter {
466482 /// The metadata key used in ostree commit metadata to serialize
467483 const CACHED_KEY_MANIFEST_DIGEST : & ' static str = "ostree-ext.cached.manifest-digest" ;
@@ -1058,6 +1074,8 @@ impl ImageImporter {
10581074 unreachable ! ( )
10591075 }
10601076
1077+ cleanup_root ( & td) ?;
1078+
10611079 let mt = ostree:: MutableTree :: new ( ) ;
10621080 repo. write_dfd_to_mtree (
10631081 ( * td) . as_raw_fd ( ) ,
@@ -1922,6 +1940,7 @@ pub(crate) fn verify_container_image(
19221940
19231941#[ cfg( test) ]
19241942mod tests {
1943+ use cap_std_ext:: cap_tempfile;
19251944 use oci_image:: { DescriptorBuilder , MediaType , Sha256Digest } ;
19261945
19271946 use super :: * ;
@@ -1941,4 +1960,28 @@ mod tests {
19411960 . unwrap ( ) ;
19421961 assert_eq ! ( ref_for_layer( & d) . unwrap( ) , "ostree/container/blob/sha256_3A_2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" ) ;
19431962 }
1963+
1964+ #[ test]
1965+ fn test_cleanup_root ( ) -> Result < ( ) > {
1966+ let td = cap_tempfile:: TempDir :: new ( cap_std:: ambient_authority ( ) ) ?;
1967+
1968+ cleanup_root ( & td) . unwrap ( ) ;
1969+ td. create_dir ( "etc" ) ?;
1970+ td. write ( "etc/hostname" , b"hostname" ) ?;
1971+ cleanup_root ( & td) . unwrap ( ) ;
1972+ assert ! ( td. try_exists( "etc/hostname" ) ?) ;
1973+ td. write ( "etc/hostname" , b"" ) ?;
1974+ cleanup_root ( & td) . unwrap ( ) ;
1975+ assert ! ( !td. try_exists( "etc/hostname" ) ?) ;
1976+
1977+ td. symlink_contents ( "../run/systemd/stub-resolv.conf" , "etc/resolv.conf" ) ?;
1978+ cleanup_root ( & td) . unwrap ( ) ;
1979+ assert ! ( td. symlink_metadata( "etc/resolv.conf" ) ?. is_symlink( ) ) ;
1980+ td. remove_file ( "etc/resolv.conf" ) ?;
1981+ td. write ( "etc/resolv.conf" , b"" ) ?;
1982+ cleanup_root ( & td) . unwrap ( ) ;
1983+ assert ! ( !td. try_exists( "etc/resolv.conf" ) ?) ;
1984+
1985+ Ok ( ( ) )
1986+ }
19441987}
0 commit comments