Skip to content

Commit c5feda5

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 Linus-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 2d270c9 commit c5feda5

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4141
- Added `nix::sys::uio::{process_vm_readv, process_vm_writev}` on Linux
4242
([#568](https://github.com/nix-rust/nix/pull/568))
4343
- Added `nix::unistd::{getgroups, setgroups, getgrouplist, initgroups}`. ([#733](https://github.com/nix-rust/nix/pull/733))
44+
- Added `nix::unistd::execveat` on Linux and Android.
45+
([#800](https://github.com/nix-rust/nix/pull/800))
4446

4547
### Changed
4648
- 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 & 5 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.
@@ -239,16 +242,31 @@ cfg_if!{
239242
if #[cfg(target_os = "android")] {
240243
execve_test_factory!(test_execve, execve, &CString::new("/system/bin/sh").unwrap());
241244
execve_test_factory!(test_fexecve, fexecve, File::open("/system/bin/sh").unwrap().into_raw_fd());
245+
execve_test_factory!(test_execveat_empty, execveat, File::open("/system/bin/sh").unwrap().into_raw_fd(),
246+
"", fcntl::AT_EMPTY_PATH);
247+
execve_test_factory!(test_execveat_relative, execveat, File::open("/system/bin/").unwrap().into_raw_fd(),
248+
"./sh", fcntl::AtFlags::empty());
249+
execve_test_factory!(test_execveat_absolute, execveat, File::open("/").unwrap().into_raw_fd(),
250+
"/system/bin/sh", fcntl::AtFlags::empty());
251+
} else if #[cfg(target_os = "linux")] {
252+
execve_test_factory!(test_execve, execve, &CString::new("/bin/sh").unwrap());
253+
execve_test_factory!(test_fexecve, fexecve, File::open("/bin/sh").unwrap().into_raw_fd());
254+
execve_test_factory!(test_execveat_empty, execveat, File::open("/bin/sh").unwrap().into_raw_fd(),
255+
"", fcntl::AT_EMPTY_PATH);
256+
execve_test_factory!(test_execveat_relative, execveat, File::open("/bin/").unwrap().into_raw_fd(),
257+
"./sh", fcntl::AtFlags::empty());
258+
execve_test_factory!(test_execveat_absolute, execveat, File::open("/").unwrap().into_raw_fd(),
259+
"/bin/sh", fcntl::AtFlags::empty());
242260
} else if #[cfg(any(target_os = "dragonfly",
243261
target_os = "freebsd",
244262
target_os = "netbsd",
245-
target_os = "openbsd",
246-
target_os = "linux", ))] {
263+
target_os = "openbsd", ))] {
264+
// No execveat() on BSD.
247265
execve_test_factory!(test_execve, execve, &CString::new("/bin/sh").unwrap());
248266
execve_test_factory!(test_fexecve, fexecve, File::open("/bin/sh").unwrap().into_raw_fd());
249267
} else if #[cfg(any(target_os = "ios", target_os = "macos", ))] {
250268
execve_test_factory!(test_execve, execve, &CString::new("/bin/sh").unwrap());
251-
// No fexecve() on macos/ios.
269+
// No fexecve() and execveat() on macos/ios.
252270
}
253271
}
254272

0 commit comments

Comments
 (0)