Skip to contentSkip to conversation

linux user: make execfd global (like exec path) and keep it open

PATCH In review Andrew Aladjev 2 participants 7 messages Aug 17, 2020 – Aug 18, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 linux user: make execfd global (like exec path) and keep it open

User opens /proc/self/exe symlink, than kernel should create /proc/self/fd/<execfd> symlink. We should be able to detect both exe and fd/<execfd> symlinks to provide common behaviour. The easiest solution is to make execfd global and keep it open. This solution looks acceptable because exec_path is already global.

---
 linux-user/elfload.c |  3 ++-
 linux-user/exit.c    |  7 +++++--
 linux-user/main.c    |  2 +-
 linux-user/qemu.h    |  1 +
 linux-user/syscall.c | 18 ++++++++++++++----
 5 files changed, 23 insertions(+), 8 deletions(-)
linux-user/elfload.c +2 -1
index fe9dfe795d..dfaf937ab9 100644
@@ -2363,6 +2363,7 @@ void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
 
    IMAGE_NAME is the filename of the image, to use in error messages.
    IMAGE_FD is the open file descriptor for the image.
+   WARNING: IMAGE_FD won't be closed.
 
    BPRM_BUF is a copy of the beginning of the file; this of course
    contains the elf file header at offset 0.  It is assumed that this
@@ -2632,7 +2633,6 @@ static void load_elf_image(const char *image_name, int image_fd,
 
     mmap_unlock();
 
-    close(image_fd);
     return;
 
  exit_read:
@@ -2666,6 +2666,7 @@ static void load_elf_interp(const char *filename, struct image_info *info,
     }
 
     load_elf_image(filename, fd, info, NULL, bprm_buf);
+    close(fd);
     return;
 
  exit_perror:
linux-user/exit.c +5 -2
index 1594015444..f0626fc432 100644
@@ -28,12 +28,15 @@ extern void __gcov_dump(void);
 
 void preexit_cleanup(CPUArchState *env, int code)
 {
+    close(execfd);
+
 #ifdef CONFIG_GPROF
         _mcleanup();
 #endif
 #ifdef CONFIG_GCOV
         __gcov_dump();
 #endif
-        gdb_exit(env, code);
-        qemu_plugin_atexit_cb();
+
+    gdb_exit(env, code);
+    qemu_plugin_atexit_cb();
 }
linux-user/main.c +1 -1
index 75c9785157..27644a831a 100644
@@ -49,6 +49,7 @@
 #include "crypto/init.h"
 
 char *exec_path;
+int execfd;
 
 int singlestep;
 static const char *argv0;
@@ -629,7 +630,6 @@ int main(int argc, char **argv, char **envp)
     int target_argc;
     int i;
     int ret;
-    int execfd;
     int log_mask;
     unsigned long max_reserved_va;
 
linux-user/qemu.h +1 -0
index 5c964389c1..f99be78d42 100644
@@ -156,6 +156,7 @@ typedef struct TaskState {
 } __attribute__((aligned(16))) TaskState;
 
 extern char *exec_path;
+extern int execfd;
 void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
linux-user/syscall.c +14 -4
index 945fc25279..5741c72733 100644
@@ -7613,8 +7613,7 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags,
     };
 
     if (is_proc_myself(pathname, "exe")) {
-        int execfd = qemu_getauxval(AT_EXECFD);
-        return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
+        return execfd;
     }
 
     for (fake_open = fakes; fake_open->filename; fake_open++) {
@@ -7872,8 +7871,19 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         return ret;
 #endif
     case TARGET_NR_close:
-        fd_trans_unregister(arg1);
-        return get_errno(close(arg1));
+        {
+            int fd = arg1;
+            if (fd == execfd) {
+                /*
+                 * We don't need to close execfd.
+                 * It will be closed on QEMU exit.
+                 */
+                return 0;
+            }
+
+            fd_trans_unregister(fd);
+            return get_errno(close(fd));
+        }
 
     case TARGET_NR_brk:
         return do_brk(arg1);
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/3 linux user: moving is proc functions to separate file

---
 linux-user/Makefile.objs  |  5 +++--
 linux-user/syscall.c      | 33 +--------------------------------
 linux-user/syscall_proc.c | 32 ++++++++++++++++++++++++++++++++
 linux-user/syscall_proc.h |  7 +++++++
 4 files changed, 43 insertions(+), 34 deletions(-)
 create mode 100644 linux-user/syscall_proc.c
 create mode 100644 linux-user/syscall_proc.h
linux-user/Makefile.objs +3 -2
index 1940910a73..20f8828b86 100644
@@ -1,7 +1,8 @@
 obj-y = main.o syscall.o strace.o mmap.o signal.o \
 	elfload.o linuxload.o uaccess.o uname.o \
-	safe-syscall.o $(TARGET_ABI_DIR)/signal.o \
-        $(TARGET_ABI_DIR)/cpu_loop.o exit.o fd-trans.o
+	safe-syscall.o syscall_proc.o \
+	$(TARGET_ABI_DIR)/cpu_loop.o $(TARGET_ABI_DIR)/signal.o \
+        exit.o fd-trans.o
 
 obj-$(TARGET_HAS_BFLT) += flatload.o
 obj-$(TARGET_I386) += vm86.o
linux-user/syscall.c +1 -32
index 5741c72733..01edc9b68d 100644
@@ -125,6 +125,7 @@
 #include "qapi/error.h"
 #include "fd-trans.h"
 #include "tcg/tcg.h"
+#include "syscall_proc.h"
 
 #ifndef CLONE_IO
 #define CLONE_IO                0x80000000      /* Clone io context */
@@ -7482,38 +7483,6 @@ static int open_self_auxv(void *cpu_env, int fd)
     return 0;
 }
 
-static int is_proc_myself(const char *filename, const char *entry)
-{
-    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;
-        }
-    }
-    return 0;
-}
-
-#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
-    defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
-static int is_proc(const char *filename, const char *entry)
-{
-    return strcmp(filename, entry) == 0;
-}
-#endif
-
 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
 static int open_net_route(void *cpu_env, int fd)
 {
linux-user/syscall_proc.c new file +32 -0
new file mode 100644
index 0000000000..34051a8e6b
@@ -0,0 +1,32 @@
+#include "qemu/osdep.h"
+
+#include "syscall_proc.h"
+
+int is_proc_myself(const char *filename, const char *entry)
+{
+    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;
+        }
+    }
+    return 0;
+}
+
+int is_proc(const char *filename, const char *entry)
+{
+    return strcmp(filename, entry) == 0;
+}
linux-user/syscall_proc.h new file +7 -0
new file mode 100644
index 0000000000..3098af931f
@@ -0,0 +1,7 @@
+#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);
+
+#endif
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 2/3] linux user: moving is proc functions to separate file

Le 18/08/2020 à 01:57, Andrew Aladjev a écrit :

Quoted 15 lines
---
 linux-user/Makefile.objs  |  5 +++--
 linux-user/syscall.c      | 33 +--------------------------------
 linux-user/syscall_proc.c | 32 ++++++++++++++++++++++++++++++++
 linux-user/syscall_proc.h |  7 +++++++
 4 files changed, 43 insertions(+), 34 deletions(-)
 create mode 100644 linux-user/syscall_proc.c
 create mode 100644 linux-user/syscall_proc.h
linux-user/Makefile.objs +0 -0
index 1940910a73..20f8828b86 100644

linux-user/Makefile.objs line 6

Quoted 8 lines
linux-user/Makefile.objs +3 -2
@@ -1,7 +1,8 @@
 obj-y = main.o syscall.o strace.o mmap.o signal.o \
 	elfload.o linuxload.o uaccess.o uname.o \
-	safe-syscall.o $(TARGET_ABI_DIR)/signal.o \
-        $(TARGET_ABI_DIR)/cpu_loop.o exit.o fd-trans.o
+	safe-syscall.o syscall_proc.o \
+	$(TARGET_ABI_DIR)/cpu_loop.o $(TARGET_ABI_DIR)/signal.o \
+        exit.o fd-trans.o
 

I think this will collide with Paolo's meson pull request.

Quoted 98 lines
linux-user/Makefile.objs +0 -0
 obj-$(TARGET_HAS_BFLT) += flatload.o
 obj-$(TARGET_I386) += vm86.o
linux-user/syscall.c +1 -32
index 5741c72733..01edc9b68d 100644
@@ -125,6 +125,7 @@
 #include "qapi/error.h"
 #include "fd-trans.h"
 #include "tcg/tcg.h"
+#include "syscall_proc.h"
 
 #ifndef CLONE_IO
 #define CLONE_IO                0x80000000      /* Clone io context */
@@ -7482,38 +7483,6 @@ static int open_self_auxv(void *cpu_env, int fd)
     return 0;
 }
 
-static int is_proc_myself(const char *filename, const char *entry)
-{
-    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;
-        }
-    }
-    return 0;
-}
-
-#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
-    defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
-static int is_proc(const char *filename, const char *entry)
-{
-    return strcmp(filename, entry) == 0;
-}
-#endif
-
 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
 static int open_net_route(void *cpu_env, int fd)
 {
linux-user/syscall_proc.c new file +32 -0
new file mode 100644
index 0000000000..34051a8e6b
@@ -0,0 +1,32 @@
+#include "qemu/osdep.h"
+
+#include "syscall_proc.h"
+
+int is_proc_myself(const char *filename, const char *entry)
+{
+    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;
+        }
+    }
+    return 0;
+}
+
+int is_proc(const char *filename, const char *entry)
+{
+    return strcmp(filename, entry) == 0;
+}
linux-user/syscall_proc.h new file +0 -0
new file mode 100644
index 0000000000..3098af931f

linux-user/syscall_proc.h line 7

Quoted 7 lines
linux-user/syscall_proc.h new file +7 -0
@@ -0,0 +1,7 @@
+#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);
+
+#endif

Please add a "SPDX-License-Identifier: GPL-2.0-or-later" header in the new files.

Thanks,
Laurent

#3 lore raw

Re: [PATCH 2/3] linux user: moving is proc functions to separate file

Ok, we may store these functions in syscall.c.

вт, 18 авг. 2020 г. в 18:56, Laurent Vivier <[email protected]>:

Quoted 145 lines · Andrew Aladjev
Quoted 15 lines
---
 linux-user/Makefile.objs  |  5 +++--
 linux-user/syscall.c      | 33 +--------------------------------
 linux-user/syscall_proc.c | 32 ++++++++++++++++++++++++++++++++
 linux-user/syscall_proc.h |  7 +++++++
 4 files changed, 43 insertions(+), 34 deletions(-)
 create mode 100644 linux-user/syscall_proc.c
 create mode 100644 linux-user/syscall_proc.h
linux-user/Makefile.objs +0 -0
index 1940910a73..20f8828b86 100644

linux-user/Makefile.objs line 6

Quoted 8 lines
linux-user/Makefile.objs +3 -2
@@ -1,7 +1,8 @@
 obj-y = main.o syscall.o strace.o mmap.o signal.o \
      elfload.o linuxload.o uaccess.o uname.o \
-     safe-syscall.o $(TARGET_ABI_DIR)/signal.o \
-        $(TARGET_ABI_DIR)/cpu_loop.o exit.o fd-trans.o
+     safe-syscall.o syscall_proc.o \
+     $(TARGET_ABI_DIR)/cpu_loop.o $(TARGET_ABI_DIR)/signal.o \
+        exit.o fd-trans.o
 

I think this will collide with Paolo's meson pull request.

Quoted 33 lines
linux-user/Makefile.objs +0 -0
 obj-$(TARGET_HAS_BFLT) += flatload.o
 obj-$(TARGET_I386) += vm86.o
linux-user/syscall.c +1 -15
index 5741c72733..01edc9b68d 100644
@@ -125,6 +125,7 @@
 #include "qapi/error.h"
 #include "fd-trans.h"
 #include "tcg/tcg.h"
+#include "syscall_proc.h"
 
 #ifndef CLONE_IO
 #define CLONE_IO                0x80000000      /* Clone io context */
@@ -7482,38 +7483,6 @@ static int open_self_auxv(void *cpu_env, int fd)
     return 0;
 }
 
-static int is_proc_myself(const char *filename, const char *entry)
-{
-    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 {

linux-user/syscall.c static int open_self_auxv(void *cpu_env, int fd) · line 7509

Quoted 10 lines
linux-user/syscall.c +0 -10
-            return 0;
-        }
-        if (!strcmp(filename, entry)) {
-            return 1;
-        }
-    }
-    return 0;
-}
-
-#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) ||

\

linux-user/syscall.c static int open_self_auxv(void *cpu_env, int fd) · line 7510

Quoted 1 line
linux-user/syscall.c +0 -1
-    defined(TARGET_SPARC) || defined(TARGET_M68K) ||

defined(TARGET_HPPA)

Quoted 54 lines
linux-user/syscall.c +0 -6
-static int is_proc(const char *filename, const char *entry)
-{
-    return strcmp(filename, entry) == 0;
-}
-#endif
-
 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
 static int open_net_route(void *cpu_env, int fd)
 {
linux-user/syscall_proc.c new file +32 -0
new file mode 100644
index 0000000000..34051a8e6b
@@ -0,0 +1,32 @@
+#include "qemu/osdep.h"
+
+#include "syscall_proc.h"
+
+int is_proc_myself(const char *filename, const char *entry)
+{
+    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;
+        }
+    }
+    return 0;
+}
+
+int is_proc(const char *filename, const char *entry)
+{
+    return strcmp(filename, entry) == 0;
+}
linux-user/syscall_proc.h new file +0 -0
new file mode 100644
index 0000000000..3098af931f

linux-user/syscall_proc.h line 7

Quoted 7 lines
linux-user/syscall_proc.h new file +7 -0
@@ -0,0 +1,7 @@
+#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);
+
+#endif

Please add a "SPDX-License-Identifier: GPL-2.0-or-later" header in the new files.

Thanks,
Laurent

#4 lore raw

PATCH 3/3 linux user: refactored is proc myself, added support for fd/<execfd>

In reply to Andrew Aladjev
---
 linux-user/syscall.c      |  12 ++--
 linux-user/syscall_proc.c | 113 +++++++++++++++++++++++++++++++-------
 linux-user/syscall_proc.h |   5 +-
 3 files changed, 101 insertions(+), 29 deletions(-)
linux-user/syscall.c +6 -6
index 01edc9b68d..7f970ccf35 100644
@@ -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) ;
linux-user/syscall_proc.c +92 -21

124 lines not on this page · raw message

linux-user/syscall_proc.h +3 -2
index 3098af931f..f0e59c0e96 100644
@@ -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
Signature · 1 line
2.26.2
#5 lore raw
In reply to Andrew Aladjev

Le 18/08/2020 à 01:57, Andrew Aladjev a écrit :

Quoted 112 lines

User opens /proc/self/exe symlink, than kernel should create /proc/self/fd/<execfd> symlink. We should be able to detect both exe and fd/<execfd> symlinks to provide common behaviour. The easiest solution is to make execfd global and keep it open. This solution looks acceptable because exec_path is already global.

---
 linux-user/elfload.c |  3 ++-
 linux-user/exit.c    |  7 +++++--
 linux-user/main.c    |  2 +-
 linux-user/qemu.h    |  1 +
 linux-user/syscall.c | 18 ++++++++++++++----
 5 files changed, 23 insertions(+), 8 deletions(-)
linux-user/elfload.c +2 -1
index fe9dfe795d..dfaf937ab9 100644
@@ -2363,6 +2363,7 @@ void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
 
    IMAGE_NAME is the filename of the image, to use in error messages.
    IMAGE_FD is the open file descriptor for the image.
+   WARNING: IMAGE_FD won't be closed.
 
    BPRM_BUF is a copy of the beginning of the file; this of course
    contains the elf file header at offset 0.  It is assumed that this
@@ -2632,7 +2633,6 @@ static void load_elf_image(const char *image_name, int image_fd,
 
     mmap_unlock();
 
-    close(image_fd);
     return;
 
  exit_read:
@@ -2666,6 +2666,7 @@ static void load_elf_interp(const char *filename, struct image_info *info,
     }
 
     load_elf_image(filename, fd, info, NULL, bprm_buf);
+    close(fd);
     return;
 
  exit_perror:
linux-user/exit.c +5 -2
index 1594015444..f0626fc432 100644
@@ -28,12 +28,15 @@ extern void __gcov_dump(void);
 
 void preexit_cleanup(CPUArchState *env, int code)
 {
+    close(execfd);
+
 #ifdef CONFIG_GPROF
         _mcleanup();
 #endif
 #ifdef CONFIG_GCOV
         __gcov_dump();
 #endif
-        gdb_exit(env, code);
-        qemu_plugin_atexit_cb();
+
+    gdb_exit(env, code);
+    qemu_plugin_atexit_cb();
 }
linux-user/main.c +1 -1
index 75c9785157..27644a831a 100644
@@ -49,6 +49,7 @@
 #include "crypto/init.h"
 
 char *exec_path;
+int execfd;
 
 int singlestep;
 static const char *argv0;
@@ -629,7 +630,6 @@ int main(int argc, char **argv, char **envp)
     int target_argc;
     int i;
     int ret;
-    int execfd;
     int log_mask;
     unsigned long max_reserved_va;
 
linux-user/qemu.h +1 -0
index 5c964389c1..f99be78d42 100644
@@ -156,6 +156,7 @@ typedef struct TaskState {
 } __attribute__((aligned(16))) TaskState;
 
 extern char *exec_path;
+extern int execfd;
 void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
linux-user/syscall.c +1 -3
index 945fc25279..5741c72733 100644
@@ -7613,8 +7613,7 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags,
     };
 
     if (is_proc_myself(pathname, "exe")) {
-        int execfd = qemu_getauxval(AT_EXECFD);
-        return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
+        return execfd;
     }
 
     for (fake_open = fakes; fake_open->filename; fake_open++) {
@@ -7872,8 +7871,19 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         return ret;
 #endif
     case TARGET_NR_close:
-        fd_trans_unregister(arg1);

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 7882

Quoted 10 lines
linux-user/syscall.c +9 -1
-        return get_errno(close(arg1));
+        {
+            int fd = arg1;
+            if (fd == execfd) {
+                /*
+                 * We don't need to close execfd.
+                 * It will be closed on QEMU exit.
+                 */
+                return 0;
+            }

Why do you prevent the user to close it?

It's his own responsability if he breaks something.

And for instance, doing that breaks a call to dup() if it was done on purpose.

Except that, the patch looks good.

Thanks,
Laurent

#6 lore raw

Hello. I want to explain situation we have in qemu today. It looks simple, but gives complex problems.

Please open "linux-user/main.c":

execfd = qemu_getauxval(AT_EXECFD);
if (execfd == 0) {
execfd = open(exec_path, O_RDONLY);
if (execfd < 0) { ... }
}
...
close(execfd);

We may take AT_EXECFD and than close it.
After that AT_EXECFD will store closed fd.
Kernel doesn't block closing of AT_EXECFD.

Same problem exists in linux-user/syscall.c:

if (is_proc_myself(pathname, "exe")) {
int execfd = qemu_getauxval(AT_EXECFD);
return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
}

We are providing AT_EXECFD value for user and he closes it. Than we are providing same AT_EXECFD value once again. This fd has already been closed.

I've just re-analyzed patch and agree with you that dup/clone will be broken.
We shouldn't provide global execfd.
So these patches are wrong.

I am going to create new patches tomorrow.
It will block closing of AT_EXECFD and remove additional AT_EXECFD usage. I will try to add several tests for this functionality.

Thank you.

вт, 18 авг. 2020 г. в 18:23, Laurent Vivier <[email protected]>:

Quoted 146 lines · Andrew Aladjev
Quoted 1 line

User opens /proc/self/exe symlink, than kernel should create

/proc/self/fd/<execfd> symlink. We should be able to detect both exe and fd/<execfd> symlinks to provide common behaviour. The easiest solution is to make execfd global and keep it open. This solution looks acceptable because exec_path is already global.

linux-user/elfload.c void probe_guest_base(const char *image_name,

Quoted 15 lines
---
 linux-user/elfload.c |  3 ++-
 linux-user/exit.c    |  7 +++++--
 linux-user/main.c    |  2 +-
 linux-user/qemu.h    |  1 +
 linux-user/syscall.c | 18 ++++++++++++++----
 5 files changed, 23 insertions(+), 8 deletions(-)
linux-user/elfload.c +0 -0
index fe9dfe795d..dfaf937ab9 100644
@@ -2363,6 +2363,7 @@ void probe_guest_base(const char *image_name,

abi_ulong guest_loaddr,

linux-user/elfload.c static void load_elf_image(const char *image_name,

Quoted 8 lines
linux-user/elfload.c +1 -0
    IMAGE_NAME is the filename of the image, to use in error messages.
    IMAGE_FD is the open file descriptor for the image.
+   WARNING: IMAGE_FD won't be closed.
 
    BPRM_BUF is a copy of the beginning of the file; this of course
    contains the elf file header at offset 0.  It is assumed that this
@@ -2632,7 +2633,6 @@ static void load_elf_image(const char *image_name,

int image_fd,

linux-user/elfload.c static void load_elf_interp(const char *filename,

Quoted 8 lines
linux-user/elfload.c +0 -1
     mmap_unlock();
 
-    close(image_fd);
     return;
 
  exit_read:
@@ -2666,6 +2666,7 @@ static void load_elf_interp(const char *filename,

struct image_info *info,

linux-user/syscall.c static int do_openat(void *cpu_env, int dirfd,

Quoted 66 lines
linux-user/elfload.c +1 -0
     }
 
     load_elf_image(filename, fd, info, NULL, bprm_buf);
+    close(fd);
     return;
 
  exit_perror:
linux-user/exit.c +5 -2
index 1594015444..f0626fc432 100644
@@ -28,12 +28,15 @@ extern void __gcov_dump(void);
 
 void preexit_cleanup(CPUArchState *env, int code)
 {
+    close(execfd);
+
 #ifdef CONFIG_GPROF
         _mcleanup();
 #endif
 #ifdef CONFIG_GCOV
         __gcov_dump();
 #endif
-        gdb_exit(env, code);
-        qemu_plugin_atexit_cb();
+
+    gdb_exit(env, code);
+    qemu_plugin_atexit_cb();
 }
linux-user/main.c +1 -1
index 75c9785157..27644a831a 100644
@@ -49,6 +49,7 @@
 #include "crypto/init.h"
 
 char *exec_path;
+int execfd;
 
 int singlestep;
 static const char *argv0;
@@ -629,7 +630,6 @@ int main(int argc, char **argv, char **envp)
     int target_argc;
     int i;
     int ret;
-    int execfd;
     int log_mask;
     unsigned long max_reserved_va;
 
linux-user/qemu.h +1 -0
index 5c964389c1..f99be78d42 100644
@@ -156,6 +156,7 @@ typedef struct TaskState {
 } __attribute__((aligned(16))) TaskState;
 
 extern char *exec_path;
+extern int execfd;
 void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
linux-user/syscall.c +0 -0
index 945fc25279..5741c72733 100644
@@ -7613,8 +7613,7 @@ static int do_openat(void *cpu_env, int dirfd,

const char *pathname, int flags,

linux-user/syscall.c static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags,

Quoted 5 lines
linux-user/syscall.c +0 -2
     };
 
     if (is_proc_myself(pathname, "exe")) {
-        int execfd = qemu_getauxval(AT_EXECFD);
-        return execfd ? execfd : safe_openat(dirfd, exec_path, flags,

mode);

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int

Quoted 5 lines
linux-user/syscall.c +1 -0
+        return execfd;
     }
 
     for (fake_open = fakes; fake_open->filename; fake_open++) {
@@ -7872,8 +7871,19 @@ static abi_long do_syscall1(void *cpu_env, int

num, abi_long arg1,

Quoted 4 lines
linux-user/syscall.c +0 -1
         return ret;
 #endif
     case TARGET_NR_close:
-        fd_trans_unregister(arg1);

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 7882

Quoted 10 lines
linux-user/syscall.c +9 -1
-        return get_errno(close(arg1));
+        {
+            int fd = arg1;
+            if (fd == execfd) {
+                /*
+                 * We don't need to close execfd.
+                 * It will be closed on QEMU exit.
+                 */
+                return 0;
+            }

Why do you prevent the user to close it?

It's his own responsability if he breaks something.

And for instance, doing that breaks a call to dup() if it was done on purpose.

Except that, the patch looks good.

Thanks,
Laurent