@@ -353,28 +353,191 @@ mod windows {
353353 #[ cfg( test) ]
354354 mod tests {
355355 use super :: * ;
356+ use std:: ffi:: OsString ;
356357 use std:: fs;
358+ use std:: mem:: size_of;
359+ use std:: os:: windows:: ffi:: OsStringExt ;
360+ use std:: path:: { Path , PathBuf } ;
361+ use windows:: Win32 :: Security :: {
362+ ACCESS_ALLOWED_ACE , CreateWellKnownSid , GetAce , GetSecurityDescriptorControl ,
363+ SE_DACL_PROTECTED , WELL_KNOWN_SID_TYPE , WinWorldSid ,
364+ } ;
365+ use windows:: Win32 :: Storage :: FileSystem :: FILE_ALL_ACCESS ;
357366
358- #[ test]
359- fn secure_path_applies_an_idempotent_protected_dacl ( ) {
360- let path = std:: env:: temp_dir ( ) . join ( format ! (
361- "acl-prototype-windows-{}-{}.conf" ,
367+ const ACCESS_ALLOWED_ACE_TYPE : u8 = 0 ;
368+ const OBJECT_INHERIT_ACE : u8 = 0x01 ;
369+ const CONTAINER_INHERIT_ACE : u8 = 0x02 ;
370+ const INHERITED_ACE : u8 = 0x10 ;
371+
372+ fn temporary_path ( name : & str ) -> PathBuf {
373+ std:: env:: temp_dir ( ) . join ( format ! (
374+ "acl-prototype-windows-{name}-{}-{}" ,
362375 std:: process:: id( ) ,
363376 std:: time:: SystemTime :: now( )
364377 . duration_since( std:: time:: UNIX_EPOCH )
365378 . unwrap( )
366379 . as_nanos( ) ,
367- ) ) ;
368- fs:: write ( & path, "Host acl-prototype\n " ) . unwrap ( ) ;
380+ ) )
381+ }
382+
383+ fn well_known_sid ( kind : WELL_KNOWN_SID_TYPE ) -> Sid {
384+ let mut length = 0 ;
385+ let _ = unsafe { CreateWellKnownSid ( kind, None , None , & mut length) } ;
386+ assert_ne ! ( length, 0 , "Windows did not report the well-known SID size" ) ;
387+
388+ let mut storage = vec ! [ 0usize ; ( length as usize ) . div_ceil( size_of:: <usize >( ) ) ] ;
389+ unsafe {
390+ CreateWellKnownSid (
391+ kind,
392+ None ,
393+ Some ( PSID ( storage. as_mut_ptr ( ) . cast ( ) ) ) ,
394+ & mut length,
395+ )
396+ . unwrap ( ) ;
397+ }
398+ Sid ( storage)
399+ }
400+
401+ fn trusted_sids ( ) -> ( Sid , Sid , Sid ) {
402+ (
403+ current_user_sid ( ) . unwrap ( ) ,
404+ well_known_sid ( WinLocalSystemSid ) ,
405+ well_known_sid ( WinBuiltinAdministratorsSid ) ,
406+ )
407+ }
408+
409+ fn assert_acl ( path : & Path , expected_sids : [ & Sid ; 3 ] , expected_flags : u8 , protected : bool ) {
410+ let file = open ( path, READ_CONTROL . 0 ) . unwrap ( ) ;
411+ let descriptor = SecurityDescriptor :: get ( & file) . unwrap ( ) ;
412+ let mut control = 0 ;
413+ let mut revision = 0 ;
414+ unsafe {
415+ GetSecurityDescriptorControl ( descriptor. 0 , & mut control, & mut revision) . unwrap ( ) ;
416+ }
417+ assert_eq ! (
418+ control & SE_DACL_PROTECTED . 0 != 0 ,
419+ protected,
420+ "protected DACL state was unexpected for {}" ,
421+ path. display( )
422+ ) ;
423+
424+ let dacl = descriptor. dacl ( ) . unwrap ( ) ;
425+ assert_eq ! ( dacl. AceCount , 3 , "DACL must contain three ACEs" ) ;
426+
427+ for ( index, expected_sid) in expected_sids. into_iter ( ) . enumerate ( ) {
428+ let mut ace = std:: ptr:: null_mut ( ) ;
429+ unsafe {
430+ GetAce ( dacl, index as u32 , & mut ace) . unwrap ( ) ;
431+ let ace = ace. cast :: < ACCESS_ALLOWED_ACE > ( ) ;
432+ assert_eq ! (
433+ ( * ace) . Header . AceType ,
434+ ACCESS_ALLOWED_ACE_TYPE ,
435+ "ACE {index} must allow access" ,
436+ ) ;
437+ assert_eq ! (
438+ ( * ace) . Header . AceFlags ,
439+ expected_flags,
440+ "ACE {index} inheritance flags were unexpected" ,
441+ ) ;
442+ assert_eq ! (
443+ ( * ace) . Mask ,
444+ FILE_ALL_ACCESS . 0 ,
445+ "ACE {index} must grant full control" ,
446+ ) ;
447+ let sid = PSID ( std:: ptr:: addr_of!( ( * ace) . SidStart ) . cast_mut ( ) . cast ( ) ) ;
448+ assert ! (
449+ EqualSid ( sid, expected_sid. as_psid( ) ) . is_ok( ) ,
450+ "ACE {index} SID did not match" ,
451+ ) ;
452+ }
453+ }
454+ }
455+
456+ #[ test]
457+ fn validate_owner_accepts_trusted_sids_and_rejects_world ( ) {
458+ let ( current_user, system, administrators) = trusted_sids ( ) ;
459+ for owner in [ & current_user, & system, & administrators] {
460+ validate_owner ( owner. as_psid ( ) , current_user. as_psid ( ) ) . unwrap ( ) ;
461+ }
462+
463+ let world = well_known_sid ( WinWorldSid ) ;
464+ assert_eq ! (
465+ validate_owner( world. as_psid( ) , current_user. as_psid( ) )
466+ . unwrap_err( )
467+ . kind( ) ,
468+ io:: ErrorKind :: PermissionDenied
469+ ) ;
470+ }
471+
472+ #[ test]
473+ fn open_rejects_relative_and_nul_paths ( ) {
474+ assert_eq ! (
475+ open( Path :: new( "relative" ) , READ_CONTROL . 0 )
476+ . unwrap_err( )
477+ . kind( ) ,
478+ io:: ErrorKind :: InvalidInput
479+ ) ;
480+ let nul_path = PathBuf :: from ( OsString :: from_wide ( & [
481+ b'C' as u16 ,
482+ b':' as u16 ,
483+ b'\\' as u16 ,
484+ 0 ,
485+ b'x' as u16 ,
486+ ] ) ) ;
487+ assert_eq ! (
488+ open( & nul_path, READ_CONTROL . 0 ) . unwrap_err( ) . kind( ) ,
489+ io:: ErrorKind :: InvalidInput
490+ ) ;
491+ }
492+
493+ #[ test]
494+ fn secure_path_applies_an_exact_protected_file_dacl_idempotently ( ) {
495+ let ( current_user, system, administrators) = trusted_sids ( ) ;
496+ let path = temporary_path ( "file" ) ;
497+ fs:: write ( & path, "test" ) . unwrap ( ) ;
369498
370499 secure_path ( & path) . unwrap ( ) ;
371500 let first = inspect_path ( & path) . unwrap ( ) ;
372501 secure_path ( & path) . unwrap ( ) ;
373- let second = inspect_path ( & path) . unwrap ( ) ;
502+ assert_eq ! (
503+ inspect_path( & path) . unwrap( ) ,
504+ first,
505+ "file DACL must be idempotent"
506+ ) ;
507+ assert ! (
508+ !first. ends_with( '\0' ) ,
509+ "SDDL must not retain the API terminator"
510+ ) ;
511+ assert_acl ( & path, [ & current_user, & system, & administrators] , 0 , true ) ;
374512
375- assert ! ( first. contains( "D:P" ) , "DACL must be protected: {first}" ) ;
376- assert_eq ! ( second, first, "secure_path must be idempotent" ) ;
377513 fs:: remove_file ( path) . unwrap ( ) ;
378514 }
515+
516+ #[ test]
517+ fn secure_path_applies_inheritable_protected_directory_aces ( ) {
518+ let ( current_user, system, administrators) = trusted_sids ( ) ;
519+ let directory = temporary_path ( "directory" ) ;
520+ fs:: create_dir ( & directory) . unwrap ( ) ;
521+
522+ secure_path ( & directory) . unwrap ( ) ;
523+ assert_acl (
524+ & directory,
525+ [ & current_user, & system, & administrators] ,
526+ OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE ,
527+ true ,
528+ ) ;
529+
530+ let child = directory. join ( "child.txt" ) ;
531+ fs:: write ( & child, "test" ) . unwrap ( ) ;
532+ assert_acl (
533+ & child,
534+ [ & current_user, & system, & administrators] ,
535+ INHERITED_ACE ,
536+ false ,
537+ ) ;
538+
539+ fs:: remove_file ( child) . unwrap ( ) ;
540+ fs:: remove_dir ( directory) . unwrap ( ) ;
541+ }
379542 }
380543}
0 commit comments