diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2021-04-22 01:04:21 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2021-04-22 01:04:21 +0000 |
commit | 2ae59f84fd254b277fe46c782382d6dbd25aa35b (patch) | |
tree | ed9e2ba99262f6bc53486bbc24a947f4b894bee2 | |
parent | b752a3a945487cfe1b73c9ab106fa82a51c22816 (diff) | |
parent | 23838c955e111a2d24fe2aadc60892d94e79c4b7 (diff) | |
download | minijail-2ae59f84fd254b277fe46c782382d6dbd25aa35b.tar.gz |
Snap for 7302914 from 23838c955e111a2d24fe2aadc60892d94e79c4b7 to sc-release
Change-Id: I1469f2379fadd09581a98645f84e7449d30fef22
-rw-r--r-- | rust/minijail/src/lib.rs | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/rust/minijail/src/lib.rs b/rust/minijail/src/lib.rs index d4f5787..ba9c8af 100644 --- a/rust/minijail/src/lib.rs +++ b/rust/minijail/src/lib.rs @@ -640,11 +640,11 @@ impl Minijail { /// FDs 0, 1, and 2 are overwritten with /dev/null FDs unless they are included in the /// inheritable_fds list. This function may abort in the child on error because a partially /// entered jail isn't recoverable. - pub fn run<P: AsRef<Path>>( + pub fn run<P: AsRef<Path>, S: AsRef<str>>( &self, cmd: P, inheritable_fds: &[RawFd], - args: &[&str], + args: &[S], ) -> Result<pid_t> { self.run_remap( cmd, @@ -658,11 +658,11 @@ impl Minijail { /// Behaves the same as `run()` except `inheritable_fds` is a list of fd /// mappings rather than just a list of fds to preserve. - pub fn run_remap<P: AsRef<Path>>( + pub fn run_remap<P: AsRef<Path>, S: AsRef<str>>( &self, cmd: P, inheritable_fds: &[(RawFd, RawFd)], - args: &[&str], + args: &[S], ) -> Result<pid_t> { let cmd_os = cmd .as_ref() @@ -674,8 +674,9 @@ impl Minijail { // into a null terminated array, suitable for use as an argv parameter to `execve`. let mut args_cstr = Vec::with_capacity(args.len()); let mut args_array = Vec::with_capacity(args.len()); - for &arg in args { - let arg_cstr = CString::new(arg).map_err(|_| Error::StrToCString(arg.to_owned()))?; + for arg in args { + let arg_cstr = CString::new(arg.as_ref()) + .map_err(|_| Error::StrToCString(arg.as_ref().to_owned()))?; args_array.push(arg_cstr.as_ptr()); args_cstr.push(arg_cstr); } @@ -850,6 +851,7 @@ mod tests { use super::*; const SHELL: &str = "/bin/sh"; + const EMPTY_STRING_SLICE: &[&str] = &[]; #[test] fn create_and_free() { @@ -883,9 +885,9 @@ mod tests { // Using libc to open/close FDs for testing. const FILE_PATH: &[u8] = b"/dev/null\0"; let j = Minijail::new().unwrap(); - let first = libc::open(FILE_PATH.as_ptr() as *const i8, libc::O_RDONLY); + let first = libc::open(FILE_PATH.as_ptr() as *const c_char, libc::O_RDONLY); assert!(first >= 0); - let second = libc::open(FILE_PATH.as_ptr() as *const i8, libc::O_RDONLY); + let second = libc::open(FILE_PATH.as_ptr() as *const c_char, libc::O_RDONLY); assert!(second >= 0); let fds: Vec<RawFd> = vec![0, 1, 2, first]; if j.fork(Some(&fds)).unwrap() == 0 { @@ -911,7 +913,7 @@ mod tests { #[test] fn wait_success() { let j = Minijail::new().unwrap(); - j.run("/bin/true", &[1, 2], &[]).unwrap(); + j.run("/bin/true", &[1, 2], &EMPTY_STRING_SLICE).unwrap(); expect_result!(j.wait(), Ok(())); } @@ -930,21 +932,22 @@ mod tests { #[test] fn wait_returncode() { let j = Minijail::new().unwrap(); - j.run("/bin/false", &[1, 2], &[]).unwrap(); + j.run("/bin/false", &[1, 2], &EMPTY_STRING_SLICE).unwrap(); expect_result!(j.wait(), Err(Error::ReturnCode(1))); } #[test] fn wait_noaccess() { let j = Minijail::new().unwrap(); - j.run("/dev/null", &[1, 2], &[]).unwrap(); + j.run("/dev/null", &[1, 2], &EMPTY_STRING_SLICE).unwrap(); expect_result!(j.wait(), Err(Error::NoAccess)); } #[test] fn wait_nocommand() { let j = Minijail::new().unwrap(); - j.run("/bin/does not exist", &[1, 2], &[]).unwrap(); + j.run("/bin/does not exist", &[1, 2], &EMPTY_STRING_SLICE) + .unwrap(); expect_result!(j.wait(), Err(Error::NoCommand)); } @@ -984,6 +987,13 @@ mod tests { #[test] fn run() { let j = Minijail::new().unwrap(); - j.run("/bin/true", &[], &[]).unwrap(); + j.run("/bin/true", &[], &EMPTY_STRING_SLICE).unwrap(); + } + + #[test] + fn run_string_vec() { + let j = Minijail::new().unwrap(); + let args = vec!["ignored".to_string()]; + j.run(Path::new("/bin/true"), &[], &args).unwrap(); } } |