@@ -552,7 +552,7 @@ fn prepare_sandbox_rootfs(
552552
553553 let init_path = rootfs. join ( "srv/openshell-vm-sandbox-init.sh" ) ;
554554 if let Some ( parent) = init_path. parent ( ) {
555- fs :: create_dir_all ( parent) . map_err ( |e| format ! ( "create {}: {e}" , parent . display ( ) ) ) ?;
555+ create_guest_dir_all ( parent) ?;
556556 }
557557 fs:: write (
558558 & init_path,
@@ -572,7 +572,7 @@ fn prepare_sandbox_rootfs(
572572 ensure_umoci_binary ( rootfs) ?;
573573
574574 let opt_dir = rootfs. join ( "opt/openshell" ) ;
575- fs :: create_dir_all ( & opt_dir) . map_err ( |e| format ! ( "create {}: {e}" , opt_dir . display ( ) ) ) ?;
575+ create_guest_dir_all ( & opt_dir) ?;
576576 fs:: write ( opt_dir. join ( ".rootfs-type" ) , "sandbox\n " )
577577 . map_err ( |e| format ! ( "write sandbox rootfs marker: {e}" ) ) ?;
578578 ensure_sandbox_guest_user ( rootfs, sandbox_uid, sandbox_gid) ?;
@@ -606,14 +606,33 @@ pub fn validate_sandbox_rootfs(rootfs: &Path) -> Result<(), String> {
606606}
607607
608608fn create_sandbox_mountpoint ( path : & Path ) -> Result < ( ) , String > {
609- fs:: create_dir_all ( path) . map_err ( |e| format ! ( "create {}: {e}" , path. display( ) ) ) ?;
609+ create_guest_dir_all ( path) ?;
610+ chmod_guest_dir ( path)
611+ }
612+
613+ /// Like `create_dir_all`, but each directory it makes is 0755 whatever the
614+ /// umask. The sandbox user must be able to traverse the guest rootfs.
615+ fn create_guest_dir_all ( path : & Path ) -> Result < ( ) , String > {
616+ if path. is_dir ( ) {
617+ return Ok ( ( ) ) ;
618+ }
619+ if let Some ( parent) = path. parent ( ) {
620+ create_guest_dir_all ( parent) ?;
621+ }
622+ fs:: create_dir ( path) . map_err ( |e| format ! ( "create {}: {e}" , path. display( ) ) ) ?;
623+ chmod_guest_dir ( path)
624+ }
625+
626+ fn chmod_guest_dir ( path : & Path ) -> Result < ( ) , String > {
610627 #[ cfg( unix) ]
611628 {
612629 use std:: os:: unix:: fs:: PermissionsExt as _;
613630
614631 fs:: set_permissions ( path, fs:: Permissions :: from_mode ( 0o755 ) )
615632 . map_err ( |e| format ! ( "chmod {}: {e}" , path. display( ) ) ) ?;
616633 }
634+ #[ cfg( not( unix) ) ]
635+ let _ = path;
617636 Ok ( ( ) )
618637}
619638
@@ -1104,7 +1123,7 @@ fn ensure_supervisor_binary(rootfs: &Path) -> Result<(), String> {
11041123 }
11051124 } else {
11061125 if let Some ( parent) = path. parent ( ) {
1107- fs :: create_dir_all ( parent) . map_err ( |e| format ! ( "create {}: {e}" , parent . display ( ) ) ) ?;
1126+ create_guest_dir_all ( parent) ?;
11081127 }
11091128
11101129 let sandbox = zstd:: decode_all ( Cursor :: new ( SANDBOX ) )
@@ -1134,7 +1153,7 @@ fn ensure_vm_init_binary(rootfs: &Path) -> Result<(), String> {
11341153 }
11351154 } else {
11361155 if let Some ( parent) = path. parent ( ) {
1137- fs :: create_dir_all ( parent) . map_err ( |e| format ! ( "create {}: {e}" , parent . display ( ) ) ) ?;
1156+ create_guest_dir_all ( parent) ?;
11381157 }
11391158 let helper = zstd:: decode_all ( Cursor :: new ( VM_INIT ) )
11401159 . map_err ( |e| format ! ( "decompress VM guest init helper: {e}" ) ) ?;
@@ -1163,7 +1182,7 @@ fn ensure_umoci_binary(rootfs: &Path) -> Result<(), String> {
11631182 }
11641183 } else {
11651184 if let Some ( parent) = path. parent ( ) {
1166- fs :: create_dir_all ( parent) . map_err ( |e| format ! ( "create {}: {e}" , parent . display ( ) ) ) ?;
1185+ create_guest_dir_all ( parent) ?;
11671186 }
11681187
11691188 let umoci =
@@ -1435,6 +1454,49 @@ mod tests {
14351454 ) ;
14361455 }
14371456
1457+ #[ cfg( unix) ]
1458+ #[ test]
1459+ fn guest_dirs_are_traversable_under_a_private_umask ( ) {
1460+ use std:: os:: unix:: fs:: PermissionsExt as _;
1461+
1462+ // The umask is process-wide, so the check runs in a child.
1463+ const DIR : & str = "OPENSHELL_TEST_GUEST_DIR" ;
1464+ if let Ok ( dir) = std:: env:: var ( DIR ) {
1465+ create_guest_dir_all ( & Path :: new ( & dir) . join ( "opt/openshell/bin" ) ) . expect ( "create dirs" ) ;
1466+ return ;
1467+ }
1468+
1469+ let dir = unique_temp_dir ( ) ;
1470+ let test = "rootfs::tests::guest_dirs_are_traversable_under_a_private_umask" ;
1471+ let status = Command :: new ( "sh" )
1472+ . args ( [ "-c" , "umask 077; exec \" $0\" --exact \" $1\" " ] )
1473+ . arg ( std:: env:: current_exe ( ) . expect ( "test binary" ) )
1474+ . arg ( test)
1475+ . env ( DIR , & dir)
1476+ . status ( )
1477+ . expect ( "run child" ) ;
1478+ assert ! ( status. success( ) ) ;
1479+
1480+ for path in [ "opt" , "opt/openshell" , "opt/openshell/bin" ] {
1481+ let mode = fs:: metadata ( dir. join ( path) ) . expect ( "stat" ) . permissions ( ) ;
1482+ assert_eq ! ( mode. mode( ) & 0o777 , 0o755 , "{path}" ) ;
1483+ }
1484+ let _ = fs:: remove_dir_all ( & dir) ;
1485+ }
1486+
1487+ #[ test]
1488+ fn guest_init_normalizes_the_overlay_root_before_mounting_it ( ) {
1489+ // Overlayfs takes the root's owner and mode from upper, which the host
1490+ // creates under the gateway's umask. A private umask would make / 0700
1491+ // and unreadable to the sandbox user.
1492+ let script = include_str ! ( "../scripts/openshell-vm-sandbox-init.sh" ) ;
1493+ let position = |needle : & str | script. find ( needle) . unwrap_or_else ( || panic ! ( "{needle}" ) ) ;
1494+ let chown = position ( "chown 0:0 /overlay/upper" ) ;
1495+ let chmod = position ( "chmod 0755 /overlay/upper" ) ;
1496+ let mount = position ( "mount -t overlay overlay" ) ;
1497+ assert ! ( chown < mount && chmod < mount) ;
1498+ }
1499+
14381500 #[ test]
14391501 fn prepare_sandbox_rootfs_preserves_image_workdir_contents_in_rootfs ( ) {
14401502 let dir = unique_temp_dir ( ) ;
0 commit comments