@@ -2023,10 +2023,12 @@ impl EnrichmentPlan {
20232023 }
20242024 }
20252025
2026- let read_write = read_write
2026+ let mut read_write = read_write
20272027 . into_iter ( )
20282028 . map ( |( path, policy) | EnrichmentPath { path, policy } )
2029- . collect ( ) ;
2029+ . collect :: < Vec < _ > > ( ) ;
2030+ read_only. sort_by ( enrichment_path_order) ;
2031+ read_write. sort_by ( enrichment_path_order) ;
20302032 ( read_only, read_write)
20312033 }
20322034
@@ -2075,12 +2077,18 @@ impl EnrichmentPlan {
20752077 if !addition. should_apply ( "read_only" , & path_exists) ? {
20762078 continue ;
20772079 }
2078- if !fs. read_only . iter ( ) . any ( |p| p == & addition. path )
2079- && !fs. read_write . iter ( ) . any ( |p| p == & addition. path )
2080- {
2081- fs. read_only . push ( addition. path ) ;
2082- application. record ( addition. policy . sources ) ;
2080+ let addition_path = std:: path:: Path :: new ( & addition. path ) ;
2081+ if path_is_covered_by (
2082+ addition_path,
2083+ fs. read_only
2084+ . iter ( )
2085+ . chain ( & fs. read_write )
2086+ . map ( std:: path:: Path :: new) ,
2087+ ) {
2088+ continue ;
20832089 }
2090+ fs. read_only . push ( addition. path ) ;
2091+ application. record ( addition. policy . sources ) ;
20842092 }
20852093 for addition in read_write {
20862094 if !addition. should_apply ( "read_write" , & path_exists) ? {
@@ -2126,12 +2134,19 @@ impl EnrichmentPlan {
21262134 continue ;
21272135 }
21282136 let p = PathBuf :: from ( & addition. path ) ;
2129- if !policy. filesystem . read_only . contains ( & p)
2130- && !policy. filesystem . read_write . contains ( & p)
2131- {
2132- policy. filesystem . read_only . push ( p) ;
2133- application. record ( addition. policy . sources ) ;
2137+ if path_is_covered_by (
2138+ & p,
2139+ policy
2140+ . filesystem
2141+ . read_only
2142+ . iter ( )
2143+ . chain ( & policy. filesystem . read_write )
2144+ . map ( PathBuf :: as_path) ,
2145+ ) {
2146+ continue ;
21342147 }
2148+ policy. filesystem . read_only . push ( p) ;
2149+ application. record ( addition. policy . sources ) ;
21352150 }
21362151 for addition in read_write {
21372152 if !addition. should_apply ( "read_write" , & |path| std:: path:: Path :: new ( path) . exists ( ) ) ? {
@@ -2192,6 +2207,23 @@ impl EnrichmentPlan {
21922207 }
21932208}
21942209
2210+ fn enrichment_path_order ( left : & EnrichmentPath , right : & EnrichmentPath ) -> std:: cmp:: Ordering {
2211+ std:: path:: Path :: new ( & left. path )
2212+ . components ( )
2213+ . count ( )
2214+ . cmp ( & std:: path:: Path :: new ( & right. path ) . components ( ) . count ( ) )
2215+ . then_with ( || left. path . cmp ( & right. path ) )
2216+ }
2217+
2218+ fn path_is_covered_by < ' a > (
2219+ candidate : & std:: path:: Path ,
2220+ existing : impl IntoIterator < Item = & ' a std:: path:: Path > ,
2221+ ) -> bool {
2222+ existing
2223+ . into_iter ( )
2224+ . any ( |allowed| candidate. starts_with ( allowed) )
2225+ }
2226+
21952227/// Returns true if GPU devices are present in the container.
21962228///
21972229/// Checks both the native Linux NVIDIA control device (`/dev/nvidiactl`) and
@@ -2877,6 +2909,20 @@ mod baseline_tests {
28772909 assert_eq ! ( plan. additional_gids( ) , vec![ 44 ] ) ;
28782910 }
28792911
2912+ #[ test]
2913+ fn enrichment_plan_orders_paths_by_depth_then_lexically ( ) {
2914+ let mut plan = EnrichmentPlan :: default ( ) ;
2915+ for path in [ "/usr/lib/wsl/lib" , "/opt/nvidia" , "/usr" , "/opt" ] {
2916+ plan. insert_read_only_path ( path, EnrichmentPathPolicy :: baseline ( ) ) ;
2917+ }
2918+
2919+ let ( read_only, _) = plan. paths ( ) ;
2920+ assert_eq ! (
2921+ read_only,
2922+ [ "/opt" , "/usr" , "/opt/nvidia" , "/usr/lib/wsl/lib" ]
2923+ ) ;
2924+ }
2925+
28802926 #[ test]
28812927 fn enrichment_plan_merges_baseline_and_runtime_path_sources ( ) {
28822928 let mut plan = EnrichmentPlan :: default ( ) ;
@@ -2890,7 +2936,7 @@ mod baseline_tests {
28902936 }
28912937
28922938 #[ test]
2893- fn proto_cdi_enrichment_adds_derived_paths ( ) {
2939+ fn proto_cdi_enrichment_omits_library_directory_covered_by_policy_parent ( ) {
28942940 let mut policy = openshell_policy:: restrictive_default_policy ( ) ;
28952941 let requirements = openshell_core:: cdi:: CdiDerivedRequirements {
28962942 device_node_paths : vec ! [ "/dev/dxg" . to_string( ) ] ,
@@ -2909,13 +2955,34 @@ mod baseline_tests {
29092955 ) ;
29102956 let filesystem = policy. filesystem . expect ( "filesystem policy" ) ;
29112957 assert ! (
2912- filesystem
2958+ ! filesystem
29132959 . read_only
2914- . contains( & "/usr/lib/wsl/lib" . to_string( ) )
2960+ . contains( & "/usr/lib/wsl/lib" . to_string( ) ) ,
2961+ "the existing /usr rule should cover the derived library directory"
29152962 ) ;
29162963 assert ! ( filesystem. read_write. contains( & "/dev/dxg" . to_string( ) ) ) ;
29172964 }
29182965
2966+ #[ test]
2967+ fn proto_cdi_enrichment_validates_covered_runtime_path_before_omitting_it ( ) {
2968+ let mut policy = openshell_policy:: restrictive_default_policy ( ) ;
2969+ let requirements = openshell_core:: cdi:: CdiDerivedRequirements {
2970+ device_node_paths : Vec :: new ( ) ,
2971+ library_directories : vec ! [ "/usr/lib/wsl/lib" . to_string( ) ] ,
2972+ read_write_mount_paths : Vec :: new ( ) ,
2973+ additional_gids : Vec :: new ( ) ,
2974+ } ;
2975+
2976+ let plan = EnrichmentPlan :: cdi_gpu ( & requirements) ;
2977+ let err = plan
2978+ . apply_to_proto_policy_with ( & mut policy, |path| path == "/proc" )
2979+ . unwrap_err ( ) ;
2980+ assert ! (
2981+ err. to_string( ) . contains( "/usr/lib/wsl/lib" ) ,
2982+ "unexpected error: {err}"
2983+ ) ;
2984+ }
2985+
29192986 #[ test]
29202987 fn proto_cdi_enrichment_errors_for_missing_runtime_path ( ) {
29212988 let mut policy = openshell_policy:: restrictive_default_policy ( ) ;
0 commit comments