@@ -26,6 +26,7 @@ enum CdiPathKind {
2626 Directory ,
2727 CharacterDevice ,
2828 BlockDevice ,
29+ Socket ,
2930 Other ,
3031}
3132
@@ -36,6 +37,7 @@ impl fmt::Display for CdiPathKind {
3637 Self :: Directory => f. write_str ( "directory" ) ,
3738 Self :: CharacterDevice => f. write_str ( "character device" ) ,
3839 Self :: BlockDevice => f. write_str ( "block device" ) ,
40+ Self :: Socket => f. write_str ( "Unix socket" ) ,
3941 Self :: Other => f. write_str ( "other" ) ,
4042 }
4143 }
@@ -78,11 +80,15 @@ enum CdiAccess {
7880struct RequirementAccumulator {
7981 device_node_paths : BTreeSet < String > ,
8082 mount_paths : BTreeMap < String , CdiAccess > ,
83+ socket_paths : BTreeSet < String > ,
8184 additional_gids : BTreeSet < u32 > ,
8285}
8386
8487impl RequirementAccumulator {
8588 fn add_device_node ( & mut self , path : String ) -> Result < ( ) , CdiError > {
89+ if self . socket_paths . contains ( & path) {
90+ return Err ( CdiError :: ConflictingAccess { path } ) ;
91+ }
8692 match self . mount_paths . get ( & path) . copied ( ) {
8793 Some ( CdiAccess :: ReadOnly ) => Err ( CdiError :: ConflictingAccess { path } ) ,
8894 Some ( CdiAccess :: ReadWrite ) | None => {
@@ -93,7 +99,9 @@ impl RequirementAccumulator {
9399 }
94100
95101 fn add_mount ( & mut self , path : String , access : CdiAccess ) -> Result < ( ) , CdiError > {
96- if access == CdiAccess :: ReadOnly && self . device_node_paths . contains ( & path) {
102+ if self . socket_paths . contains ( & path)
103+ || ( access == CdiAccess :: ReadOnly && self . device_node_paths . contains ( & path) )
104+ {
97105 return Err ( CdiError :: ConflictingAccess { path } ) ;
98106 }
99107 match self . mount_paths . get ( & path) . copied ( ) {
@@ -106,6 +114,14 @@ impl RequirementAccumulator {
106114 }
107115 }
108116
117+ fn add_socket ( & mut self , path : String ) -> Result < ( ) , CdiError > {
118+ if self . device_node_paths . contains ( & path) || self . mount_paths . contains_key ( & path) {
119+ return Err ( CdiError :: ConflictingAccess { path } ) ;
120+ }
121+ self . socket_paths . insert ( path) ;
122+ Ok ( ( ) )
123+ }
124+
109125 fn add_gid ( & mut self , gid : u32 ) -> Result < ( ) , CdiError > {
110126 if gid == 0 {
111127 return Err ( CdiError :: RootAdditionalGid ) ;
@@ -133,6 +149,7 @@ impl RequirementAccumulator {
133149 }
134150 Ok ( CdiDerivedRequirements {
135151 device_node_paths : self . device_node_paths . into_iter ( ) . collect ( ) ,
152+ socket_paths : self . socket_paths . into_iter ( ) . collect ( ) ,
136153 read_only_paths : read_only_paths. into_iter ( ) . collect ( ) ,
137154 read_write_mount_paths,
138155 additional_gids : self . additional_gids . into_iter ( ) . collect ( ) ,
@@ -310,13 +327,21 @@ fn classify_cdi_mounts(requirements: &mut CdiDerivedRequirements) -> Result<(),
310327 for gid in & requirements. additional_gids {
311328 accumulator. add_gid ( * gid) ?;
312329 }
330+ for path in & requirements. socket_paths {
331+ accumulator. add_socket ( path. clone ( ) ) ?;
332+ }
313333 for mount in & requirements. mounts {
314334 let fd = rustix:: fs:: open (
315335 & mount. path ,
316336 rustix:: fs:: OFlags :: PATH | rustix:: fs:: OFlags :: CLOEXEC ,
317337 rustix:: fs:: Mode :: empty ( ) ,
318338 )
319339 . map_err ( |error| mount_inspection ( & mount. path , error) ) ?;
340+ let stat = rustix:: fs:: fstat ( & fd) . map_err ( |error| mount_inspection ( & mount. path , error) ) ?;
341+ if rustix:: fs:: FileType :: from_raw_mode ( stat. st_mode ) == rustix:: fs:: FileType :: Socket {
342+ accumulator. add_socket ( mount. path . clone ( ) ) ?;
343+ continue ;
344+ }
320345 let read_only = table. read_only ( & mount. path , & fd) ?;
321346 accumulator. add_mount (
322347 mount. path . clone ( ) ,
@@ -367,6 +392,15 @@ where
367392 } ) ;
368393 }
369394 }
395+ for path in & requirements. socket_paths {
396+ let kind = path_kind ( path) ;
397+ if kind != Some ( CdiPathKind :: Socket ) {
398+ return Err ( CdiError :: SocketPathNotSocket {
399+ path : path. clone ( ) ,
400+ kind : kind. map_or_else ( || "missing" . to_string ( ) , |kind| kind. to_string ( ) ) ,
401+ } ) ;
402+ }
403+ }
370404 for path in & requirements. read_only_paths {
371405 if path_kind ( path) . is_none ( ) {
372406 return Err ( CdiError :: ReadOnlyPathMissing { path : path. clone ( ) } ) ;
@@ -572,7 +606,9 @@ fn filesystem_path_kind(path: &str) -> Option<CdiPathKind> {
572606 if file_type. is_block_device ( ) {
573607 return Some ( CdiPathKind :: BlockDevice ) ;
574608 }
575- if file_type. is_file ( ) {
609+ if file_type. is_socket ( ) {
610+ Some ( CdiPathKind :: Socket )
611+ } else if file_type. is_file ( ) {
576612 Some ( CdiPathKind :: File )
577613 } else if file_type. is_dir ( ) {
578614 Some ( CdiPathKind :: Directory )
@@ -585,6 +621,50 @@ fn filesystem_path_kind(path: &str) -> Option<CdiPathKind> {
585621mod tests {
586622 use super :: * ;
587623
624+ #[ test]
625+ fn runtime_resolution_keeps_socket_destinations_exact_without_file_grants ( ) {
626+ let directory = tempfile:: tempdir ( ) . unwrap ( ) ;
627+ let specs = directory. path ( ) . join ( "specs" ) ;
628+ std:: fs:: create_dir ( & specs) . unwrap ( ) ;
629+ for name in [
630+ "persistenced/socket" ,
631+ "fabricmanager/socket" ,
632+ "libdaemon.so.1" ,
633+ ] {
634+ let target = directory. path ( ) . join ( name) ;
635+ std:: fs:: create_dir_all ( target. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
636+ let _listener = std:: os:: unix:: net:: UnixListener :: bind ( & target) . unwrap ( ) ;
637+ for options in [ "[]" , "[ro, bind]" , "[rw, rbind]" ] {
638+ write_spec (
639+ & specs,
640+ "socket.yaml" ,
641+ & format ! (
642+ "cdiVersion: 0.7.0\n kind: example.com/device\n devices:\n - name: test\n containerEdits:\n mounts:\n - hostPath: /host/socket\n containerPath: {}\n options: {}\n " ,
643+ target. display( ) ,
644+ options,
645+ ) ,
646+ ) ;
647+ let requirements =
648+ resolve_cdi_context ( & context ( & specs, & [ "example.com/device=test" ] ) ) . unwrap ( ) ;
649+ assert_eq ! ( requirements. socket_paths, vec![ target. to_string_lossy( ) ] ) ;
650+ assert ! ( requirements. read_only_paths. is_empty( ) ) ;
651+ assert ! ( requirements. read_write_mount_paths. is_empty( ) ) ;
652+ assert ! ( requirements. mounts. is_empty( ) ) ;
653+ validate_cdi_requirements ( & requirements, & HashSet :: < String > :: new ( ) ) . unwrap ( ) ;
654+ }
655+ let requirements = CdiDerivedRequirements {
656+ socket_paths : vec ! [ target. to_string_lossy( ) . into_owned( ) ] ,
657+ ..Default :: default ( )
658+ } ;
659+ std:: fs:: remove_file ( & target) . unwrap ( ) ;
660+ std:: fs:: write ( & target, "replacement" ) . unwrap ( ) ;
661+ assert ! ( matches!(
662+ validate_cdi_requirements( & requirements, & HashSet :: <String >:: new( ) ) ,
663+ Err ( CdiError :: SocketPathNotSocket { .. } )
664+ ) ) ;
665+ }
666+ }
667+
588668 #[ test]
589669 fn runtime_resolution_classifies_a_mount_without_access_options ( ) {
590670 let directory = tempfile:: tempdir ( ) . unwrap ( ) ;
0 commit comments