@@ -10,9 +10,9 @@ use vmm_sys_util::syscall::SyscallReturnCode;
10
10
11
11
use super :: { to_cstring, JailerError } ;
12
12
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". ";
16
16
17
17
// This uses switching to a new mount namespace + pivot_root(), together with the regular chroot,
18
18
// to provide a hardened jail (at least compared to only relying on chroot).
@@ -24,16 +24,13 @@ pub fn chroot(path: &Path) -> Result<(), JailerError> {
24
24
. into_empty_result ( )
25
25
. map_err ( JailerError :: UnshareNewNs ) ?;
26
26
27
- let root_dir = CStr :: from_bytes_with_nul ( ROOT_DIR_NUL_TERMINATED )
28
- . map_err ( JailerError :: FromBytesWithNul ) ?;
29
-
30
27
// Recursively change the propagation type of all the mounts in this namespace to SLAVE, so
31
28
// we can call pivot_root.
32
29
// SAFETY: Safe because we provide valid parameters.
33
30
SyscallReturnCode ( unsafe {
34
31
libc:: mount (
35
32
null ( ) ,
36
- root_dir . as_ptr ( ) ,
33
+ ROOT_DIR . as_ptr ( ) ,
37
34
null ( ) ,
38
35
libc:: MS_SLAVE | libc:: MS_REC ,
39
36
null ( ) ,
@@ -64,45 +61,41 @@ pub fn chroot(path: &Path) -> Result<(), JailerError> {
64
61
// Change current dir to the chroot dir, so we only need to handle relative paths from now on.
65
62
env:: set_current_dir ( path) . map_err ( JailerError :: SetCurrentDir ) ?;
66
63
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
-
72
64
// Create the old_root folder we're going to use for pivot_root, using a relative path.
73
65
// 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 ) } )
75
67
. into_empty_result ( )
76
68
. map_err ( JailerError :: MkdirOldRoot ) ?;
77
69
78
- let cwd = CStr :: from_bytes_with_nul ( CURRENT_DIR_NUL_TERMINATED )
79
- . map_err ( JailerError :: FromBytesWithNul ) ?;
80
-
81
70
// We are now ready to call pivot_root. We have to use sys_call because there is no libc
82
71
// wrapper for pivot_root.
83
72
// SAFETY: Safe because we provide valid parameters.
84
73
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
+ )
86
79
} )
87
80
. into_empty_result ( )
88
81
. map_err ( JailerError :: PivotRoot ) ?;
89
82
90
83
// pivot_root doesn't guarantee that we will be in "/" at this point, so switch to "/"
91
84
// explicitly.
92
85
// 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 ( ) ) } )
94
87
. into_empty_result ( )
95
88
. map_err ( JailerError :: ChdirNewRoot ) ?;
96
89
97
90
// Umount the old_root, thus isolating the process from everything outside the jail root folder.
98
91
// 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 ) } )
100
93
. into_empty_result ( )
101
94
. map_err ( JailerError :: UmountOldRoot ) ?;
102
95
103
96
// Remove the no longer necessary old_root directory.
104
97
// 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 ( ) ) } )
106
99
. into_empty_result ( )
107
100
. map_err ( JailerError :: RmOldRootDir )
108
101
}
0 commit comments