Use execveat syscall in fexecve
Commit Message
By using execveat we no longer depend on /proc. The execveat syscall was
introduced in 3.19, except for a few late comers.
* sysdeps/unix/sysv/linux/fexecve.c (fexecve) [__NR_execveat]: Try
execveat first.
[!__ASSUME_EXECVEAT]: Fall back to /proc if execveat is
unimplemented.
* sysdeps/unix/sysv/linux/kernel-features.h (__ASSUME_EXECVEAT)
[__LINUX_KERNEL_VERSION >= 0x031300]: Define.
* sysdeps/unix/sysv/linux/alpha/kernel-features.h
(__ASSUME_EXECVEAT) [__LINUX_KERNEL_VERSION < 0x040200]: Undef.
* sysdeps/unix/sysv/linux/hppa/kernel-features.h
(__ASSUME_EXECVEAT) [__LINUX_KERNEL_VERSION < 0x040000]: Undef.
* sysdeps/unix/sysv/linux/microblaze/kernel-features.h
(__ASSUME_EXECVEAT) [__LINUX_KERNEL_VERSION < 0x040000]: Undef.
---
sysdeps/unix/sysv/linux/alpha/kernel-features.h | 5 +++++
sysdeps/unix/sysv/linux/fexecve.c | 15 +++++++++++++++
sysdeps/unix/sysv/linux/hppa/kernel-features.h | 5 +++++
sysdeps/unix/sysv/linux/kernel-features.h | 5 +++++
sysdeps/unix/sysv/linux/microblaze/kernel-features.h | 5 +++++
5 files changed, 35 insertions(+)
Comments
* Andreas Schwab:
> By using execveat we no longer depend on /proc. The execveat syscall was
> introduced in 3.19, except for a few late comers.
Ideally, this should have a test that executing “/bin/sh -c true”
works, both with an O_PATH descriptor and a regular descriptor.
The implementation itself (including the AT_EMPTY_PATH flag) and the
__ASSUME_EXECVEAT conditions look good to me.
@@ -35,4 +35,9 @@
#define __ASSUME_RECV_SYSCALL 1
#define __ASSUME_SEND_SYSCALL 1
+/* Support for the execveat syscall was added in 4.2. */
+#if __LINUX_KERNEL_VERSION < 0x040200
+# undef __ASSUME_EXECVEAT
+#endif
+
#endif /* _KERNEL_FEATURES_H */
@@ -19,8 +19,13 @@
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
+#include <fcntl.h>
#include <sys/stat.h>
+#include <sysdep.h>
+#include <sys/syscall.h>
+#include <kernel-features.h>
+
/* Execute the file FD refers to, overlaying the running program image.
ARGV and ENVP are passed to the new program, as for `execve'. */
@@ -33,6 +38,15 @@ fexecve (int fd, char *const argv[], char *const envp[])
return -1;
}
+#ifdef __NR_execveat
+ INLINE_SYSCALL (execveat, 5, fd, "", argv, envp, AT_EMPTY_PATH);
+# ifndef __ASSUME_EXECVEAT
+ if (errno != ENOSYS)
+ return -1;
+# endif
+#endif
+
+#ifndef __ASSUME_EXECVEAT
/* We use the /proc filesystem to get the information. If it is not
mounted we fail. */
char buf[sizeof "/proc/self/fd/" + sizeof (int) * 3];
@@ -50,6 +64,7 @@ fexecve (int fd, char *const argv[], char *const envp[])
save = ENOSYS;
__set_errno (save);
+#endif
return -1;
}
@@ -27,3 +27,8 @@
#define __ASSUME_RECV_SYSCALL 1
#define __ASSUME_SEND_SYSCALL 1
+
+/* Support for the execveat syscall was added in 4.0. */
+#if __LINUX_KERNEL_VERSION < 0x040000
+# undef __ASSUME_EXECVEAT
+#endif
@@ -102,3 +102,8 @@
implementation does not assume the __ASSUME_* and instead use a fallback
implementation based on p{read,write}v and returning an error for
non supported flags. */
+
+/* Support for the execveat syscall was added in 3.19. */
+#if __LINUX_KERNEL_VERSION >= 0x031300
+# define __ASSUME_EXECVEAT 1
+#endif
@@ -47,3 +47,8 @@
#if __LINUX_KERNEL_VERSION < 0x030300
# undef __ASSUME_SENDMMSG_SYSCALL
#endif
+
+/* Support for the execveat syscall was added in 4.0. */
+#if __LINUX_KERNEL_VERSION < 0x040000
+# undef __ASSUME_EXECVEAT
+#endif