Skip to content

Commit 8e463f5

Browse files
committed
unistd: add execveat() on Linux and Android
This adds execveat() to `nix::unistd`. It uses the execveat(2) Linux kernel syscall, which is available since 3.19. This is a Linux-specific extension which is not covered by POSIX and does not have any userland libc wrapper. Ref: http://man7.org/linux/man-pages/man2/execveat.2.html
1 parent cf3f236 commit 8e463f5

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4343
- Added `nix::unistd::{getgroups, setgroups, getgrouplist, initgroups}`. ([#733](https://github.com/nix-rust/nix/pull/733))
4444
- Added `nix::sys::socket::UnixAddr::as_abstract` on Linux and Android.
4545
([#785](https://github.com/nix-rust/nix/pull/785))
46+
- Added `nix::unistd::execveat` on Linux and Android.
47+
([#800](https://github.com/nix-rust/nix/pull/800))
4648

4749
### Changed
4850
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))

src/unistd.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,31 @@ pub fn fexecve(fd: RawFd, args: &[CString], env: &[CString]) -> Result<Void> {
644644
Err(Error::Sys(Errno::last()))
645645
}
646646

647+
/// Execute program relative to a directory file descriptor (see
648+
/// [execveat(2)](http://man7.org/linux/man-pages/man2/execveat.2.html)).
649+
///
650+
/// The `execveat` function allows for another process to be "called" which will
651+
/// replace the current process image. That is, this process becomes the new
652+
/// command that is run. On success, this function will not return. Instead,
653+
/// the new program will run until it exits.
654+
///
655+
/// This function is similar to `execve`, except that the program to be executed
656+
/// is referenced as a file descriptor to the base directory plus a path.
657+
#[cfg(any(target_os = "android", target_os = "linux"))]
658+
#[inline]
659+
pub fn execveat(dirfd: RawFd, pathname: &CString, args: &[CString],
660+
env: &[CString], flags: super::fcntl::AtFlags) -> Result<Void> {
661+
let args_p = to_exec_array(args);
662+
let env_p = to_exec_array(env);
663+
664+
unsafe {
665+
libc::syscall(libc::SYS_execveat, dirfd, pathname.as_ptr(),
666+
args_p.as_ptr(), env_p.as_ptr(), flags);
667+
};
668+
669+
Err(Error::Sys(Errno::last()))
670+
}
671+
647672
/// Daemonize this process by detaching from the controlling terminal (see
648673
/// [daemon(3)](http://man7.org/linux/man-pages/man3/daemon.3.html)).
649674
///

test/test_unistd.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern crate tempdir;
22

3+
use nix::fcntl;
34
use nix::unistd::*;
45
use nix::unistd::ForkResult::*;
56
use nix::sys::wait::*;
@@ -190,7 +191,7 @@ fn test_initgroups() {
190191
}
191192

192193
macro_rules! execve_test_factory(
193-
($test_name:ident, $syscall:ident, $exe: expr) => (
194+
($test_name:ident, $syscall:ident, $exe: expr $(, $pathname:expr, $flags:expr)*) => (
194195
#[test]
195196
fn $test_name() {
196197
#[allow(unused_variables)]
@@ -211,12 +212,14 @@ macro_rules! execve_test_factory(
211212
// exec!
212213
$syscall(
213214
$exe,
215+
$(&CString::new($pathname).unwrap(), )*
214216
&[CString::new(b"".as_ref()).unwrap(),
215217
CString::new(b"-c".as_ref()).unwrap(),
216218
CString::new(b"echo nix!!! && echo foo=$foo && echo baz=$baz"
217219
.as_ref()).unwrap()],
218220
&[CString::new(b"foo=bar".as_ref()).unwrap(),
219-
CString::new(b"baz=quux".as_ref()).unwrap()]).unwrap();
221+
CString::new(b"baz=quux".as_ref()).unwrap()]
222+
$(, $flags)*).unwrap();
220223
},
221224
Parent { child } => {
222225
// Wait for the child to exit.
@@ -252,6 +255,24 @@ cfg_if!{
252255
}
253256
}
254257

258+
cfg_if!{
259+
if #[cfg(target_os = "android")] {
260+
execve_test_factory!(test_execveat_empty, execveat, File::open("/system/bin/sh").unwrap().into_raw_fd(),
261+
"", fcntl::AT_EMPTY_PATH);
262+
execve_test_factory!(test_execveat_relative, execveat, File::open("/system/bin/").unwrap().into_raw_fd(),
263+
"./sh", fcntl::AtFlags::empty());
264+
execve_test_factory!(test_execveat_absolute, execveat, File::open("/").unwrap().into_raw_fd(),
265+
"/system/bin/sh", fcntl::AtFlags::empty());
266+
} else if #[cfg(all(target_os = "linux"), any(target_arch ="x86_64", target_arch ="x86"))] {
267+
execve_test_factory!(test_execveat_empty, execveat, File::open("/bin/sh").unwrap().into_raw_fd(),
268+
"", fcntl::AT_EMPTY_PATH);
269+
execve_test_factory!(test_execveat_relative, execveat, File::open("/bin/").unwrap().into_raw_fd(),
270+
"./sh", fcntl::AtFlags::empty());
271+
execve_test_factory!(test_execveat_absolute, execveat, File::open("/").unwrap().into_raw_fd(),
272+
"/bin/sh", fcntl::AtFlags::empty());
273+
}
274+
}
275+
255276
#[test]
256277
fn test_fchdir() {
257278
// fchdir changes the process's cwd

0 commit comments

Comments
 (0)