@@ -16,6 +16,7 @@ use camino::{Utf8Path, Utf8PathBuf};
16
16
use cap_std_ext:: cap_std;
17
17
use cap_std_ext:: cap_std:: fs:: { Dir , MetadataExt } ;
18
18
use cap_std_ext:: cmdext:: CapStdExtCommandExt ;
19
+ use cap_std_ext:: dirext:: CapStdExtDirExt ;
19
20
use containers_image_proxy:: { ImageProxy , OpenedImage } ;
20
21
use flate2:: Compression ;
21
22
use fn_error_context:: context;
@@ -462,6 +463,21 @@ fn timestamp_of_manifest_or_config(
462
463
. log_err_default ( )
463
464
}
464
465
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
+
465
481
impl ImageImporter {
466
482
/// The metadata key used in ostree commit metadata to serialize
467
483
const CACHED_KEY_MANIFEST_DIGEST : & ' static str = "ostree-ext.cached.manifest-digest" ;
@@ -1058,6 +1074,8 @@ impl ImageImporter {
1058
1074
unreachable ! ( )
1059
1075
}
1060
1076
1077
+ cleanup_root ( & td) ?;
1078
+
1061
1079
let mt = ostree:: MutableTree :: new ( ) ;
1062
1080
repo. write_dfd_to_mtree (
1063
1081
( * td) . as_raw_fd ( ) ,
@@ -1922,6 +1940,7 @@ pub(crate) fn verify_container_image(
1922
1940
1923
1941
#[ cfg( test) ]
1924
1942
mod tests {
1943
+ use cap_std_ext:: cap_tempfile;
1925
1944
use oci_image:: { DescriptorBuilder , MediaType , Sha256Digest } ;
1926
1945
1927
1946
use super :: * ;
@@ -1941,4 +1960,28 @@ mod tests {
1941
1960
. unwrap ( ) ;
1942
1961
assert_eq ! ( ref_for_layer( & d) . unwrap( ) , "ostree/container/blob/sha256_3A_2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" ) ;
1943
1962
}
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
+ }
1944
1987
}
0 commit comments