[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 3/3] linux user: refactored is proc myself, added support for fd/
From: |
Andrew Aladjev |
Subject: |
[PATCH 3/3] linux user: refactored is proc myself, added support for fd/<execfd> |
Date: |
Tue, 18 Aug 2020 02:57:12 +0300 |
Signed-off-by: Andrew Aladjev <aladjev.andrew@gmail.com>
---
linux-user/syscall.c | 12 ++--
linux-user/syscall_proc.c | 113 +++++++++++++++++++++++++++++++-------
linux-user/syscall_proc.h | 5 +-
3 files changed, 101 insertions(+), 29 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 01edc9b68d..7f970ccf35 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7570,18 +7570,18 @@ static int do_openat(void *cpu_env, int dirfd, const
char *pathname, int flags,
{ "auxv", open_self_auxv, is_proc_myself },
{ "cmdline", open_self_cmdline, is_proc_myself },
#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
- { "/proc/net/route", open_net_route, is_proc },
+ { "net/route", open_net_route, is_proc },
#endif
#if defined(TARGET_SPARC) || defined(TARGET_HPPA)
- { "/proc/cpuinfo", open_cpuinfo, is_proc },
+ { "cpuinfo", open_cpuinfo, is_proc },
#endif
#if defined(TARGET_M68K)
- { "/proc/hardware", open_hardware, is_proc },
+ { "hardware", open_hardware, is_proc },
#endif
{ NULL, NULL, NULL }
};
- if (is_proc_myself(pathname, "exe")) {
+ if (is_proc_myself_exe(pathname)) {
return execfd;
}
@@ -9161,7 +9161,7 @@ static abi_long do_syscall1(void *cpu_env, int num,
abi_long arg1,
} else if (!arg3) {
/* Short circuit this for the magic exe check. */
ret = -TARGET_EINVAL;
- } else if (is_proc_myself((const char *)p, "exe")) {
+ } else if (is_proc_myself_exe((const char *)p)) {
char real[PATH_MAX], *temp;
temp = realpath(exec_path, real);
/* Return value is # of bytes that we wrote to the buffer. */
@@ -9190,7 +9190,7 @@ static abi_long do_syscall1(void *cpu_env, int num,
abi_long arg1,
p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
if (!p || !p2) {
ret = -TARGET_EFAULT;
- } else if (is_proc_myself((const char *)p, "exe")) {
+ } else if (is_proc_myself_exe((const char *)p)) {
char real[PATH_MAX], *temp;
temp = realpath(exec_path, real);
ret = temp == NULL ? get_errno(-1) : strlen(real) ;
diff --git a/linux-user/syscall_proc.c b/linux-user/syscall_proc.c
index 34051a8e6b..3cf986b25f 100644
--- a/linux-user/syscall_proc.c
+++ b/linux-user/syscall_proc.c
@@ -1,32 +1,103 @@
#include "qemu/osdep.h"
+#include "elf.h"
#include "syscall_proc.h"
+#include "qemu.h"
-int is_proc_myself(const char *filename, const char *entry)
+#define PROC "/proc/"
+#define SELF "self/"
+#define FD "fd/"
+
+#define STARTS_WITH(path, CONSTANT) ( \
+ strlen(path) >= strlen(CONSTANT) && \
+ strncmp(path, CONSTANT, strlen(CONSTANT)) == 0 \
+)
+
+static inline char *scope_to_proc(const char *path)
{
- if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
- filename += strlen("/proc/");
- if (!strncmp(filename, "self/", strlen("self/"))) {
- filename += strlen("self/");
- } else if (*filename >= '1' && *filename <= '9') {
- char myself[80];
- snprintf(myself, sizeof(myself), "%d/", getpid());
- if (!strncmp(filename, myself, strlen(myself))) {
- filename += strlen(myself);
- } else {
- return 0;
- }
- } else {
- return 0;
- }
- if (!strcmp(filename, entry)) {
- return 1;
+ if (STARTS_WITH(path, PROC)) {
+ return (char *)path + strlen(PROC);
+ }
+
+ return NULL;
+}
+
+static inline char *scope_to_proc_myself(const char *path)
+{
+ char *scope_path = scope_to_proc(path);
+ if (scope_path == NULL) {
+ return NULL;
+ }
+
+ if (STARTS_WITH(scope_path, SELF)) {
+ return scope_path + strlen(SELF);
+ }
+
+ if (strlen(scope_path) >= 1 &&
+ *scope_path >= '1' && *scope_path <= '9') {
+ char pid_path[80];
+ snprintf(pid_path, sizeof(pid_path), "%d/", getpid());
+ if (STARTS_WITH(scope_path, pid_path)) {
+ return scope_path + strlen(pid_path);
}
}
- return 0;
+
+ return NULL;
+}
+
+int is_proc(const char *path, const char *entry)
+{
+ char *scope_path = scope_to_proc(path);
+ if (scope_path == NULL) {
+ return 0;
+ }
+
+ return strcmp(scope_path, entry) == 0;
}
-int is_proc(const char *filename, const char *entry)
+int is_proc_myself(const char *path, const char *entry)
{
- return strcmp(filename, entry) == 0;
+ char *scope_path = scope_to_proc_myself(path);
+ if (scope_path == NULL) {
+ return 0;
+ }
+
+ return strcmp(scope_path, entry) == 0;
+}
+
+/*
+ * Kernel creates "fd/#{number}" link after opening "exe" link.
+ * Both "exe" and "fd/#{number}" can be used by application.
+ *
+ * Kernel can provide infinite amount of fd numbers.
+ * QEMU is going to always return single global execfd.
+ *
+ * So we need to check "exe" and "fd/#{execfd}" only.
+ */
+
+int is_proc_myself_exe(const char *path)
+{
+ char *scope_path = scope_to_proc_myself(path);
+ if (scope_path == NULL) {
+ return 0;
+ }
+
+ if (strcmp(scope_path, "exe") == 0) {
+ return 1;
+ }
+
+ if (STARTS_WITH(scope_path, FD)) {
+ scope_path += strlen(FD);
+
+ if (strlen(scope_path) >= 1 &&
+ *scope_path >= '1' && *scope_path <= '9') {
+ char execfd_path[80];
+ snprintf(execfd_path, sizeof(execfd_path), "%d", execfd);
+ if (strcmp(scope_path, execfd_path) == 0) {
+ return 1;
+ }
+ }
+ }
+
+ return 0;
}
diff --git a/linux-user/syscall_proc.h b/linux-user/syscall_proc.h
index 3098af931f..f0e59c0e96 100644
--- a/linux-user/syscall_proc.h
+++ b/linux-user/syscall_proc.h
@@ -1,7 +1,8 @@
#ifndef SYSCALL_PROC_H
#define SYSCALL_PROC_H
-int is_proc(const char *filename, const char *entry);
-int is_proc_myself(const char *filename, const char *entry);
+int is_proc(const char *path, const char *entry);
+int is_proc_myself(const char *path, const char *entry);
+int is_proc_myself_exe(const char *path);
#endif
--
2.26.2