@@ -10,9 +10,9 @@ use vmm_sys_util::syscall::SyscallReturnCode;
1010
1111use super :: { to_cstring, JailerError } ;
1212
13- const OLD_ROOT_DIR_NAME_NUL_TERMINATED : & [ u8 ] = b "old_root\0 ";
14- const ROOT_DIR_NUL_TERMINATED : & [ u8 ] = b"/ \0 ";
15- const CURRENT_DIR_NUL_TERMINATED : & [ u8 ] = b". \0 ";
13+ const OLD_ROOT_DIR : & CStr = c "old_root";
14+ const ROOT_DIR : & CStr = c"/ ";
15+ const CURRENT_DIR : & CStr = c". ";
1616
1717// This uses switching to a new mount namespace + pivot_root(), together with the regular chroot,
1818// to provide a hardened jail (at least compared to only relying on chroot).
@@ -24,16 +24,13 @@ pub fn chroot(path: &Path) -> Result<(), JailerError> {
2424 . into_empty_result ( )
2525 . map_err ( JailerError :: UnshareNewNs ) ?;
2626
27- let root_dir = CStr :: from_bytes_with_nul ( ROOT_DIR_NUL_TERMINATED )
28- . map_err ( JailerError :: FromBytesWithNul ) ?;
29-
3027 // Recursively change the propagation type of all the mounts in this namespace to SLAVE, so
3128 // we can call pivot_root.
3229 // SAFETY: Safe because we provide valid parameters.
3330 SyscallReturnCode ( unsafe {
3431 libc:: mount (
3532 null ( ) ,
36- root_dir . as_ptr ( ) ,
33+ ROOT_DIR . as_ptr ( ) ,
3734 null ( ) ,
3835 libc:: MS_SLAVE | libc:: MS_REC ,
3936 null ( ) ,
@@ -64,45 +61,41 @@ pub fn chroot(path: &Path) -> Result<(), JailerError> {
6461 // Change current dir to the chroot dir, so we only need to handle relative paths from now on.
6562 env:: set_current_dir ( path) . map_err ( JailerError :: SetCurrentDir ) ?;
6663
67- // We use the CStr conversion to make sure the contents of the byte slice would be a
68- // valid C string (and for the as_ptr() method).
69- let old_root_dir = CStr :: from_bytes_with_nul ( OLD_ROOT_DIR_NAME_NUL_TERMINATED )
70- . map_err ( JailerError :: FromBytesWithNul ) ?;
71-
7264 // Create the old_root folder we're going to use for pivot_root, using a relative path.
7365 // SAFETY: The call is safe because we provide valid arguments.
74- SyscallReturnCode ( unsafe { libc:: mkdir ( old_root_dir . as_ptr ( ) , libc:: S_IRUSR | libc:: S_IWUSR ) } )
66+ SyscallReturnCode ( unsafe { libc:: mkdir ( OLD_ROOT_DIR . as_ptr ( ) , libc:: S_IRUSR | libc:: S_IWUSR ) } )
7567 . into_empty_result ( )
7668 . map_err ( JailerError :: MkdirOldRoot ) ?;
7769
78- let cwd = CStr :: from_bytes_with_nul ( CURRENT_DIR_NUL_TERMINATED )
79- . map_err ( JailerError :: FromBytesWithNul ) ?;
80-
8170 // We are now ready to call pivot_root. We have to use sys_call because there is no libc
8271 // wrapper for pivot_root.
8372 // SAFETY: Safe because we provide valid parameters.
8473 SyscallReturnCode ( unsafe {
85- libc:: syscall ( libc:: SYS_pivot_root , cwd. as_ptr ( ) , old_root_dir. as_ptr ( ) )
74+ libc:: syscall (
75+ libc:: SYS_pivot_root ,
76+ CURRENT_DIR . as_ptr ( ) ,
77+ OLD_ROOT_DIR . as_ptr ( ) ,
78+ )
8679 } )
8780 . into_empty_result ( )
8881 . map_err ( JailerError :: PivotRoot ) ?;
8982
9083 // pivot_root doesn't guarantee that we will be in "/" at this point, so switch to "/"
9184 // explicitly.
9285 // SAFETY: Safe because we provide valid parameters.
93- SyscallReturnCode ( unsafe { libc:: chdir ( root_dir . as_ptr ( ) ) } )
86+ SyscallReturnCode ( unsafe { libc:: chdir ( ROOT_DIR . as_ptr ( ) ) } )
9487 . into_empty_result ( )
9588 . map_err ( JailerError :: ChdirNewRoot ) ?;
9689
9790 // Umount the old_root, thus isolating the process from everything outside the jail root folder.
9891 // SAFETY: Safe because we provide valid parameters.
99- SyscallReturnCode ( unsafe { libc:: umount2 ( old_root_dir . as_ptr ( ) , libc:: MNT_DETACH ) } )
92+ SyscallReturnCode ( unsafe { libc:: umount2 ( OLD_ROOT_DIR . as_ptr ( ) , libc:: MNT_DETACH ) } )
10093 . into_empty_result ( )
10194 . map_err ( JailerError :: UmountOldRoot ) ?;
10295
10396 // Remove the no longer necessary old_root directory.
10497 // SAFETY: Safe because we provide valid parameters.
105- SyscallReturnCode ( unsafe { libc:: rmdir ( old_root_dir . as_ptr ( ) ) } )
98+ SyscallReturnCode ( unsafe { libc:: rmdir ( OLD_ROOT_DIR . as_ptr ( ) ) } )
10699 . into_empty_result ( )
107100 . map_err ( JailerError :: RmOldRootDir )
108101}
0 commit comments