@@ -25,10 +25,11 @@ use serde::Deserialize;
2525use composefs:: {
2626 fsverity:: { FsVerityHashValue , Sha512HashValue } ,
2727 mount:: FsHandle ,
28+ mount:: { MountOptions , VerityRequirement , composefs_fsmount} ,
2829 mountcompat:: { overlayfs_set_fd, overlayfs_set_lower_and_data_fds, prepare_mount} ,
29- repository:: Repository ,
30+ repository:: { ImageNotFound , Repository } ,
3031} ;
31- use composefs_boot:: cmdline:: ComposefsCmdline ;
32+ use composefs_boot:: cmdline:: { ComposefsCmdline , KARG_COMPOSEFS_DIGEST , KARG_V2 , split_cmdline } ;
3233use composefs_ctl:: composefs;
3334use composefs_ctl:: composefs_boot;
3435
@@ -353,15 +354,89 @@ pub fn mount_composefs_image(
353354 if allow_missing_fsverity {
354355 repo. set_insecure ( ) ;
355356 }
356- let rootfs = repo
357- . mount ( name)
358- . context ( "Failed to mount composefs image" ) ?;
357+ let ( image, enable_verity) = repo. open_image ( name) ?;
358+ validate_image_verity ( enable_verity, allow_missing_fsverity)
359+ . with_context ( || format ! ( "Validating fs-verity for composefs image {name}" ) ) ?;
360+ let objects = repo. objects_dir ( ) . context ( "Getting objects directory" ) ?;
361+ let verity = if enable_verity {
362+ VerityRequirement :: Required
363+ } else {
364+ VerityRequirement :: Disabled
365+ } ;
366+ let rootfs = composefs_fsmount (
367+ image,
368+ name,
369+ & [ objects. as_fd ( ) ] ,
370+ verity,
371+ & MountOptions :: default ( ) ,
372+ )
373+ . context ( "Creating filesystem mount" ) ?;
359374
360375 set_mount_readonly ( & rootfs) ?;
361376
362377 Ok ( rootfs)
363378}
364379
380+ fn parse_composefs_candidates ( cmdline : & str ) -> Result < Vec < ComposefsCmdline < Sha512HashValue > > > {
381+ let mut candidates = Vec :: new ( ) ;
382+ for token in split_cmdline ( cmdline) {
383+ if token. starts_with ( & format ! ( "{KARG_COMPOSEFS_DIGEST}=" ) )
384+ || token. starts_with ( & format ! ( "{KARG_V2}=" ) )
385+ {
386+ if let Some ( candidate) = ComposefsCmdline :: < Sha512HashValue > :: from_cmdline ( token) ? {
387+ candidates. push ( candidate) ;
388+ }
389+ }
390+ }
391+ Ok ( candidates)
392+ }
393+
394+ fn validate_image_verity ( enable_verity : bool , allow_missing_fsverity : bool ) -> Result < ( ) > {
395+ if !allow_missing_fsverity && !enable_verity {
396+ anyhow:: bail!( "composefs image is not fs-verity sealed" ) ;
397+ }
398+ Ok ( ( ) )
399+ }
400+
401+ fn select_composefs_candidate < T > (
402+ candidates : & [ ComposefsCmdline < Sha512HashValue > ] ,
403+ mut mount : impl FnMut ( & ComposefsCmdline < Sha512HashValue > ) -> Result < Option < T > > ,
404+ ) -> Result < ( T , ComposefsCmdline < Sha512HashValue > ) > {
405+ for candidate in candidates {
406+ if let Some ( mounted) = mount ( candidate) ? {
407+ return Ok ( ( mounted, candidate. clone ( ) ) ) ;
408+ }
409+ }
410+ let tried = candidates
411+ . iter ( )
412+ . map ( |candidate| candidate. digest ( ) . to_hex ( ) )
413+ . collect :: < Vec < _ > > ( )
414+ . join ( ", " ) ;
415+ anyhow:: bail!( "no composefs image found (tried: {tried})" )
416+ }
417+
418+ fn mount_composefs_candidate (
419+ sysroot : & OwnedFd ,
420+ candidate : & ComposefsCmdline < Sha512HashValue > ,
421+ allow_missing_fsverity : bool ,
422+ ) -> Result < Option < OwnedFd > > {
423+ if candidate. is_insecure ( ) && !allow_missing_fsverity {
424+ anyhow:: bail!(
425+ "composefs candidate requests insecure fs-verity, but the repository policy is strict"
426+ ) ;
427+ }
428+
429+ match mount_composefs_image (
430+ sysroot,
431+ & candidate. digest ( ) . to_hex ( ) ,
432+ allow_missing_fsverity,
433+ ) {
434+ Ok ( rootfs) => Ok ( Some ( rootfs) ) ,
435+ Err ( error) if error. downcast_ref :: < ImageNotFound > ( ) . is_some ( ) => Ok ( None ) ,
436+ Err ( error) => Err ( error) ,
437+ }
438+ }
439+
365440/// Mounts a subdirectory with the specified configuration
366441#[ context( "Mounting subdirectory" ) ]
367442pub fn mount_subdir (
@@ -468,17 +543,27 @@ pub fn setup_root(args: Args) -> Result<()> {
468543 config
469544 } ;
470545
471- let composefs_info = ComposefsCmdline :: < Sha512HashValue > :: from_cmdline ( & cmdline)
472- . context ( "Failed to parse composefs cmdline" ) ?
473- . ok_or_else ( || anyhow:: anyhow!( "No composefs image in cmdline" ) ) ?;
474-
475- let new_root = match & args. root_fs {
476- Some ( path) => open_root_fs ( path) . context ( "Failed to clone specified root fs" ) ?,
477- None => mount_composefs_image (
478- & sysroot,
479- & composefs_info. digest ( ) . to_hex ( ) ,
480- composefs_info. is_insecure ( ) ,
481- ) ?,
546+ let candidates =
547+ parse_composefs_candidates ( & cmdline) . context ( "Failed to parse composefs cmdline" ) ?;
548+ if candidates. is_empty ( ) {
549+ anyhow:: bail!( "No composefs image in cmdline" ) ;
550+ }
551+ let allow_missing_fsverity = candidates[ 0 ] . is_insecure ( ) ;
552+ if candidates
553+ . iter ( )
554+ . any ( |candidate| candidate. is_insecure ( ) != allow_missing_fsverity)
555+ {
556+ anyhow:: bail!( "composefs candidates have mixed fs-verity policy" ) ;
557+ }
558+
559+ let ( new_root, composefs_info) = match & args. root_fs {
560+ Some ( path) => (
561+ open_root_fs ( path) . context ( "Failed to clone specified root fs" ) ?,
562+ candidates[ 0 ] . clone ( ) ,
563+ ) ,
564+ None => select_composefs_candidate ( & candidates, |candidate| {
565+ mount_composefs_candidate ( & sysroot, candidate, allow_missing_fsverity)
566+ } ) ?,
482567 } ;
483568
484569 // we need to clone this before the next step to make sure we get the old one
@@ -633,4 +718,53 @@ mod tests {
633718 assert_eq ! ( config. root. transient, true ) ;
634719 assert_eq ! ( config. etc. mount, Some ( MountType :: None ) ) ;
635720 }
721+
722+ fn v1 ( digest : Sha512HashValue , insecure : bool ) -> String {
723+ ComposefsCmdline :: new_v1 ( digest, insecure) . to_cmdline_arg ( )
724+ }
725+
726+ fn v2 ( digest : Sha512HashValue , insecure : bool ) -> String {
727+ ComposefsCmdline :: new_v2 ( digest, insecure) . to_cmdline_arg ( )
728+ }
729+
730+ #[ test]
731+ fn test_composefs_candidate_priority_and_selected_state ( ) {
732+ let v1_digest = Sha512HashValue :: EMPTY ;
733+ let v2_digest = Sha512HashValue :: from_hex ( "aa" . repeat ( 64 ) ) . unwrap ( ) ;
734+ let cmdline = format ! (
735+ "quiet {} {}" ,
736+ v1( v1_digest. clone( ) , false ) ,
737+ v2( v2_digest. clone( ) , false )
738+ ) ;
739+ let candidates = parse_composefs_candidates ( & cmdline) . unwrap ( ) ;
740+ let ( selected, selected_cmdline) = select_composefs_candidate ( & candidates, |candidate| {
741+ Ok ( ( candidate. digest ( ) == & v2_digest) . then_some ( candidate. digest ( ) . to_hex ( ) ) )
742+ } )
743+ . unwrap ( ) ;
744+ assert_eq ! ( selected, v2_digest. to_hex( ) ) ;
745+ assert_eq ! ( selected_cmdline. digest( ) , & v2_digest) ;
746+
747+ let ( selected, _) = select_composefs_candidate ( & candidates, |candidate| {
748+ Ok ( Some ( candidate. digest ( ) . to_hex ( ) ) )
749+ } )
750+ . unwrap ( ) ;
751+ assert_eq ! ( selected, v1_digest. to_hex( ) ) ;
752+ }
753+
754+ #[ test]
755+ fn test_composefs_candidate_parse_and_policy_errors ( ) {
756+ assert ! ( parse_composefs_candidates( "composefs=not-a-digest" ) . is_err( ) ) ;
757+ assert ! ( parse_composefs_candidates( "composefs.digest=v9-sha512-12:aa" ) . is_err( ) ) ;
758+
759+ let strict = parse_composefs_candidates ( & v1 ( Sha512HashValue :: EMPTY , false ) ) . unwrap ( ) ;
760+ let insecure = parse_composefs_candidates ( & v2 ( Sha512HashValue :: EMPTY , true ) ) . unwrap ( ) ;
761+ assert_ne ! ( strict[ 0 ] . is_insecure( ) , insecure[ 0 ] . is_insecure( ) ) ;
762+ }
763+
764+ #[ test]
765+ fn test_image_verity_policy_is_per_image ( ) {
766+ assert ! ( validate_image_verity( true , false ) . is_ok( ) ) ;
767+ assert ! ( validate_image_verity( false , false ) . is_err( ) ) ;
768+ assert ! ( validate_image_verity( false , true ) . is_ok( ) ) ;
769+ }
636770}
0 commit comments