All of lore.kernel.org
 help / color / mirror / Atom feed
From: YAMAMOTO Takashi <yamamoto@midokura.com>
To: qemu-devel@nongnu.org
Cc: YAMAMOTO Takashi <yamamoto@midokura.com>,
	Laurent Vivier <laurent@vivier.eu>
Subject: [PATCH v2 06/11] linux-user: add get_exe_path
Date: Mon, 31 May 2021 14:50:13 +0900	[thread overview]
Message-ID: <20210531055019.10149-7-yamamoto@midokura.com> (raw)
In-Reply-To: <20210531055019.10149-1-yamamoto@midokura.com>

Refactor to prepare the special cases for /proc/$pid/exe where
pid is not the calling process.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
---
 linux-user/syscall.c | 48 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 46 insertions(+), 2 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 51144c6d29..999760448d 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7986,6 +7986,45 @@ static int open_self_auxv(void *cpu_env, int fd)
     return 0;
 }
 
+static const char *get_exe_path(int pid, char *buf, size_t bufsize)
+{
+    if (pid == getpid()) {
+        return exec_path;
+    }
+
+    return NULL;
+}
+
+static int is_proc_file(const char *filename, int *pidp, const char *entry)
+{
+    if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
+        int pid;
+
+        filename += strlen("/proc/");
+        if (!strncmp(filename, "self/", strlen("self/"))) {
+            pid = getpid();
+            filename += strlen("self/");
+        } else if (*filename >= '1' && *filename <= '9') {
+            pid = 0;
+            while (*filename >= '0' && *filename <= '9') {
+                pid = pid * 10 + *filename - '0';
+                filename++;
+            }
+            if (*filename != '/') {
+                return 0;
+            }
+            filename++;
+        } else {
+            return 0;
+        }
+        if (!strcmp(filename, entry)) {
+            *pidp = pid;
+            return 1;
+        }
+    }
+    return 0;
+}
+
 static int is_proc_myself(const char *filename, const char *entry)
 {
     if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
@@ -8492,6 +8531,8 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
             abi_ulong addr;
             char **q;
             int total_size = 0;
+            int pid;
+            char path_store[PATH_MAX];
 
             argc = 0;
             guest_argp = arg2;
@@ -8552,8 +8593,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
              * program's problem.
              */
             path = p;
-            if (is_proc_myself(path, "exe")) {
-                path = exec_path;
+            if (is_proc_file(path, &pid, "exe")) {
+                path = get_exe_path(pid, path_store, sizeof(path_store));
+                if (path == NULL) {
+                    path = p;
+                }
             }
             ret = get_errno(safe_execve(path, argp, envp));
             unlock_user(p, arg1, 0);
-- 
2.21.1 (Apple Git-122.3)



  parent reply	other threads:[~2021-05-31  5:55 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-31  5:50 [PATCH v2 00/11] linux-user changes to run docker YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 01/11] linux-user: handle /proc/self/exe for execve YAMAMOTO Takashi
2021-06-20 14:14   ` Laurent Vivier
2021-06-21  2:02     ` Takashi Yamamoto
2021-06-22 13:47       ` Laurent Vivier
2021-05-31  5:50 ` [PATCH v2 02/11] linux-user: Fix the execfd case of /proc/self/exe open YAMAMOTO Takashi
2021-06-20 14:16   ` Laurent Vivier
2021-06-21  1:19     ` Takashi Yamamoto
2021-05-31  5:50 ` [PATCH v2 03/11] linux-user: dup the execfd on start up YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 04/11] linux-user: make exec_path realpath YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 05/11] linux-user: Implement pivot_root YAMAMOTO Takashi
2021-06-20 14:02   ` Laurent Vivier
2021-06-20 14:05   ` Laurent Vivier
2021-05-31  5:50 ` YAMAMOTO Takashi [this message]
2021-05-31  5:50 ` [PATCH v2 07/11] linux-user: simplify is_proc_myself YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 08/11] linux-user: Implement exec of /proc/$pid/exe of qemu process YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 09/11] linux-user: Make the qemu detection for /proc/$pid/exe a bit conservative YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 10/11] linux-user: a crude hack for libcontainer (CLONE_PARENT) [!MERGE] YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 11/11] linux-user: always assume preserve_argv0 for now [!MERGE] YAMAMOTO Takashi
2021-05-31  6:07 ` [PATCH v2 00/11] linux-user changes to run docker no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210531055019.10149-7-yamamoto@midokura.com \
    --to=yamamoto@midokura.com \
    --cc=laurent@vivier.eu \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.