qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] linux-user: handle /proc/self/exe with execve() syscall


From: Laurent Vivier
Subject: [PATCH 1/2] linux-user: handle /proc/self/exe with execve() syscall
Date: Sun, 25 Sep 2022 18:15:26 +0200

If path is /proc/self/exe, use the executable file descriptor
provided by binfmt_misc (or opened by main()) with execveat().

Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
 linux-user/main.c           | 9 +++++++--
 linux-user/syscall.c        | 9 ++++++++-
 linux-user/user-internals.h | 1 +
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/linux-user/main.c b/linux-user/main.c
index e44bdb17b853..f915bdd7cef7 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -64,6 +64,7 @@
 #endif
 
 char *exec_path;
+int execfd;
 
 int singlestep;
 static const char *argv0;
@@ -646,7 +647,6 @@ int main(int argc, char **argv, char **envp)
     int target_argc;
     int i;
     int ret;
-    int execfd;
     unsigned long max_reserved_va;
     bool preserve_argv0;
 
@@ -845,7 +845,12 @@ int main(int argc, char **argv, char **envp)
 
     fd_trans_init();
 
-    ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
+    /*
+     * loader_exec() closes the file descriptor provided by the caller.
+     * As we need to keep it available for execve("/proc/self/exe")
+     * we provide a copy to loader_exec().
+     */
+    ret = loader_exec(dup(execfd), exec_path, target_argv, target_environ, 
regs,
         info, &bprm);
     if (ret != 0) {
         printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index f4091212027c..6642652b7644 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -649,6 +649,8 @@ safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, 
options, \
 safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
               int, options, struct rusage *, rusage)
 safe_syscall3(int, execve, const char *, filename, char **, argv, char **, 
envp)
+safe_syscall5(int, execveat, int, dirfd, const char *, pathname, char **, \
+              argv, char **, envp, int, flags)
 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
     defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
 safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, 
\
@@ -8843,7 +8845,12 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int 
num, abi_long arg1,
              * before the execve completes and makes it the other
              * program's problem.
              */
-            ret = get_errno(safe_execve(p, argp, envp));
+            if (is_proc_myself(p, "exe")) {
+                ret = get_errno(safe_execveat(execfd, "", argp, envp,
+                                AT_EMPTY_PATH));
+            } else {
+                ret = get_errno(safe_execve(p, argp, envp));
+            }
             unlock_user(p, arg1, 0);
 
             goto execve_end;
diff --git a/linux-user/user-internals.h b/linux-user/user-internals.h
index 0280e76addda..84f29a1e2990 100644
--- a/linux-user/user-internals.h
+++ b/linux-user/user-internals.h
@@ -23,6 +23,7 @@
 #include "qemu/log.h"
 
 extern char *exec_path;
+extern int execfd;
 void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
-- 
2.37.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]