[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 09/11] linux-user: Make the qemu detection for /proc/$pid/exe
From: |
YAMAMOTO Takashi |
Subject: |
[PATCH v2 09/11] linux-user: Make the qemu detection for /proc/$pid/exe a bit conservative |
Date: |
Mon, 31 May 2021 14:50:16 +0900 |
Perform the qemu special case only when the binary seems the same as
our own executable.
This is enough for my use case (docker and runc) where the involved
qemu binaries are always for the same arch.
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
---
linux-user/syscall.c | 38 ++++++++++++++++++++++++++++++++++----
1 file changed, 34 insertions(+), 4 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 6f9161dbe4..56a3c37d83 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7986,6 +7986,26 @@ static int open_self_auxv(void *cpu_env, int fd)
return 0;
}
+static int is_same_qemu(const char *their_exe)
+{
+ struct stat our_st;
+ struct stat their_st;
+
+ if (stat("/proc/self/exe", &our_st) != 0) {
+ return 0;
+ }
+ if (stat(their_exe, &their_st) != 0) {
+ return 0;
+ }
+ if (our_st.st_dev != their_st.st_dev) {
+ return 0;
+ }
+ if (our_st.st_ino != their_st.st_ino) {
+ return 0;
+ }
+ return 1;
+}
+
static const char *get_exe_path(int pid, char *buf, size_t bufsize)
{
ssize_t ssz;
@@ -7996,6 +8016,20 @@ static const char *get_exe_path(int pid, char *buf,
size_t bufsize)
}
/* dockerd makes runc invoke dockerd using "/proc/${dockerd_pid}/exe". */
+
+ /*
+ * Check that it's the same qemu binary as ours
+ * to avoid false positives.
+ *
+ * While ideally we want to allow different qemu binaries,
+ * (E.g. linux-user for a different arch)
+ * I can't think of any reliable way to detect the cases.
+ */
+ snprintf(buf, bufsize, "/proc/%d/exe", pid);
+ if (!is_same_qemu(buf)) {
+ return NULL;
+ }
+
snprintf(buf, bufsize, "/proc/%d/cmdline", pid);
fd = open(buf, O_RDONLY);
if (fd == -1) {
@@ -8033,10 +8067,6 @@ static const char *get_exe_path(int pid, char *buf,
size_t bufsize)
}
}
- /*
- * XXX a bit too loose detection of qemu.
- * maybe we can compare /proc/$pid/exe with ours.
- */
slash = strrchr(argv0, '/');
if (slash != NULL) {
argv0 = slash + 1; /* basename */
--
2.21.1 (Apple Git-122.3)
- [PATCH v2 00/11] linux-user changes to run docker, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 01/11] linux-user: handle /proc/self/exe for execve, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 02/11] linux-user: Fix the execfd case of /proc/self/exe open, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 03/11] linux-user: dup the execfd on start up, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 04/11] linux-user: make exec_path realpath, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 05/11] linux-user: Implement pivot_root, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 06/11] linux-user: add get_exe_path, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 07/11] linux-user: simplify is_proc_myself, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 08/11] linux-user: Implement exec of /proc/$pid/exe of qemu process, YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 09/11] linux-user: Make the qemu detection for /proc/$pid/exe a bit conservative,
YAMAMOTO Takashi <=
- [PATCH v2 10/11] linux-user: a crude hack for libcontainer (CLONE_PARENT) [!MERGE], YAMAMOTO Takashi, 2021/05/31
- [PATCH v2 11/11] linux-user: always assume preserve_argv0 for now [!MERGE], YAMAMOTO Takashi, 2021/05/31
- Re: [PATCH v2 00/11] linux-user changes to run docker, no-reply, 2021/05/31