Skip to contentSkip to conversation

linux-user: Add option to run `execve`d programs through QEMU

PATCH Superseded Noah Goldstein 5 participants 39 messages Aug 31, 2024 – Nov 7, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Add option to run `execve`d programs through QEMU

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
 linux-user/main.c           | 22 ++++++++++++++++++++++
 linux-user/syscall.c        | 20 +++++++++++++++-----
 linux-user/user-internals.h |  4 ++++
 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +22 -0
index 8143a0d4b0..dfb303a1f2 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char ** qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        int i;
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +15 -5
index 9d5415674d..732ef89054 100644
@@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = !is_execveat && qemu_dup_for_children;
 
     argc = 0;
 
@@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    }
+    else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8553,7 +8563,7 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +4 -0
index 5c7f173ceb..0719e65ff4 100644
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char ** qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
Signature · 1 line
2.34.1
#1 lore raw
Quoted 81 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
 linux-user/main.c           | 22 ++++++++++++++++++++++
 linux-user/syscall.c        | 20 +++++++++++++++-----
 linux-user/user-internals.h |  4 ++++
 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +22 -0
index 8143a0d4b0..dfb303a1f2 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char ** qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        int i;
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +0 -0
index 9d5415674d..732ef89054 100644
@@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {

linux-user/syscall.c static int do_execv(CPUArchState *cpu_env, int dirfd, · line 8470

Quoted 10 lines
linux-user/syscall.c +2 -1
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = !is_execveat && qemu_dup_for_children;
 

I wasn't sure if this was feasible for evecveat. If anyone has any thoughts it would be nice to support that as well.

Quoted 62 lines
linux-user/syscall.c +13 -4
     argc = 0;
 
@@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    }
+    else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8553,7 +8563,7 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +4 -1
index 5c7f173ceb..0719e65ff4 100644
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char ** qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
--

2.34.1

#2 lore raw
Quoted 154 lines · Noah Goldstein
Quoted 81 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
 linux-user/main.c           | 22 ++++++++++++++++++++++
 linux-user/syscall.c        | 20 +++++++++++++++-----
 linux-user/user-internals.h |  4 ++++
 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +22 -0
index 8143a0d4b0..dfb303a1f2 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char ** qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        int i;
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +0 -0
index 9d5415674d..732ef89054 100644
@@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {

linux-user/syscall.c static int do_execv(CPUArchState *cpu_env, int dirfd, · line 8470

Quoted 10 lines
linux-user/syscall.c +2 -1
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = !is_execveat && qemu_dup_for_children;
 

I wasn't sure if this was feasible for evecveat. If anyone has any thoughts it would be nice to support that as well.

Quoted 58 lines
linux-user/syscall.c +13 -4
     argc = 0;
 
@@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    }
+    else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8553,7 +8563,7 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +4 -0
index 5c7f173ceb..0719e65ff4 100644
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char ** qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
Quoted 4 lines
Quoted 4 lines
linux-user/user-internals.h +0 -1
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
--

2.34.1

ping

#3 lore raw
Quoted 156 lines · Noah Goldstein
Quoted 154 lines · Noah Goldstein
Quoted 81 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
 linux-user/main.c           | 22 ++++++++++++++++++++++
 linux-user/syscall.c        | 20 +++++++++++++++-----
 linux-user/user-internals.h |  4 ++++
 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +22 -0
index 8143a0d4b0..dfb303a1f2 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char ** qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        int i;
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +0 -0
index 9d5415674d..732ef89054 100644
@@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {

linux-user/syscall.c static int do_execv(CPUArchState *cpu_env, int dirfd, · line 8470

Quoted 10 lines
linux-user/syscall.c +2 -1
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = !is_execveat && qemu_dup_for_children;
 

I wasn't sure if this was feasible for evecveat. If anyone has any thoughts it would be nice to support that as well.

Quoted 58 lines
linux-user/syscall.c +13 -4
     argc = 0;
 
@@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    }
+    else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8553,7 +8563,7 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +4 -0
index 5c7f173ceb..0719e65ff4 100644
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char ** qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
linux-user/user-internals.h +0 -1
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
--

2.34.1

ping

ping

#4 lore raw
In reply to Noah Goldstein
Quoted 6 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 6 lines

The motivation for the change is to make it so that plugins running through qemu can continue to run on children.  Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

Another reason to have this is that one may not have root permissions to configure binfmt-misc.

There was a similar patch posted to the mailing list some years back, which I used to cherry-pick when I needed this. I'm not sure what happened to that discussion though.

Quoted 15 lines

---
 linux-user/main.c           | 22 ++++++++++++++++++++++ linux-user/syscall.c        | 20 +++++++++++++++----- linux-user/user-internals.h |  4 ++++

 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +0 -0
index 8143a0d4b0..dfb303a1f2 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
  uintptr_t guest_base;
  bool have_guest_base;

linux-user/main.c unsigned long mmap_min_addr; · line 86

Quoted 3 lines
linux-user/main.c +3 -0
+bool qemu_dup_for_children;
+int qemu_argc;
+char ** qemu_argv;

Style: ** belong to the variable name.
There are a couple other issues, please check the output of

git format-patch -1 --stdout | ./scripts/checkpatch.pl -

Quoted 32 lines

+
 /*
  * Used to implement backwards-compatibility for the -strace, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by

linux-user/main.c +0 -0
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
      perf_enable_jitdump();
  }

linux-user/main.c +5 -0
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+

 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 #ifdef CONFIG_PLUGIN

linux-user/main.c +3 -0
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] =
 {
       "",           "Generate a /tmp/perf-${pid}.map file for perf"},
      {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
       "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false,
 handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with

qemu "
+                   "(as instantiated for the parent)"}, {NULL, NULL, false, NULL, NULL, NULL}
 };

linux-user/main.c +0 -0
@@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
Quoted 5 lines


     optind = parse_args(argc, argv);

+    if (qemu_dup_for_children) {
+        int i;

I get the following build error:

qemu/linux-user/main.c: In function ‘main’:
qemu/linux-user/main.c:746:13: error: declaration of ‘i’ shadows a previous local [-Werror=shadow=compatible-local]
746 | int i;
| ^
qemu/linux-user/main.c:699:9: note: shadowed declaration is here
699 | int i;
| ^

I don't think this variable is needed at all.

Quoted 22 lines

+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename, last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);

linux-user/syscall.c +0 -0
index 9d5415674d..732ef89054 100644
@@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env,
 int dirfd,
                      abi_long pathname, abi_long guest_argp,

                     abi_long guest_envp, int flags, bool is_execveat)
 {
-    int ret;

Quoted 8 lines

+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = !is_execveat && qemu_dup_for_children;

Wouldn't it be better to check for dirfd == AT_FDCWD?

Quoted 67 lines

     argc = 0;

linux-user/syscall.c +0 -0
@@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env,
 int dirfd,
          envc++;
      }


-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1); envp = g_new0(char *, envc + 1);

-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }

linux-user/syscall.c +0 -0
@@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int
 dirfd,
      }


     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    }
+    else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags) : safe_execve(exe, argp, envp);

linux-user/syscall.c +0 -0
@@ -8553,7 +8563,7 @@ execve_efault:
      ret = -TARGET_EFAULT;


 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }

linux-user/user- renamed from linux-user/user-internals.h +0 -0

internals.h
index 5c7f173ceb..0719e65ff4 100644

linux-user/user-internals.h +0 -0
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
  extern const char *qemu_uname_release;
  extern unsigned long mmap_min_addr;

linux-user/user-internals.h +4 -0
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char ** qemu_argv;
+

 typedef struct IOCTLEntry IOCTLEntry;

 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,

#5 lore raw
Quoted 15 lines · Ilya Leoshkevich
Quoted 6 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 6 lines

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

Quoted 3 lines

Another reason to have this is that one may not have root permissions to configure binfmt-misc.

+1

Quoted 3 lines

There was a similar patch posted to the mailing list some years back, which I used to cherry-pick when I needed this. I'm not sure what happened to that discussion though.

Yes(ish): https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/

Quoted 16 lines
Quoted 16 lines
---
 linux-user/main.c           | 22 ++++++++++++++++++++++
 linux-user/syscall.c        | 20 +++++++++++++++-----
 linux-user/user-internals.h |  4 ++++
 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +0 -0
index 8143a0d4b0..dfb303a1f2 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
Quoted 9 lines

linux-user/main.c unsigned long mmap_min_addr; · line 86

Quoted 3 lines
linux-user/main.c +3 -0
+bool qemu_dup_for_children;
+int qemu_argc;
+char ** qemu_argv;

Style: ** belong to the variable name.
There are a couple other issues, please check the output of

git format-patch -1 --stdout | ./scripts/checkpatch.pl -

Will Do.

Quoted 49 lines
Quoted 32 lines
linux-user/main.c +1 -0
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be

overwritten by

linux-user/main.c +8 -0
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] =
 {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false,
 handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with

qemu "

linux-user/main.c +1 -0
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };

linux-user/main.c int main(int argc, char **argv, char **envp) · line 746

Quoted 5 lines
linux-user/main.c +2 -0
@@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        int i;

I get the following build error:

qemu/linux-user/main.c: In function ‘main’:
qemu/linux-user/main.c:746:13: error: declaration of ‘i’ shadows a previous local [-Werror=shadow=compatible-local]
746 | int i;
| ^
qemu/linux-user/main.c:699:9: note: shadowed declaration is here
699 | int i;
| ^

Quoted 2 lines

I don't think this variable is needed at all.

Bah sorry.

Quoted 24 lines
Quoted 24 lines
linux-user/main.c +7 -0
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace *
 LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +1 -1
index 9d5415674d..732ef89054 100644
@@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env,
 int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool
 is_execveat)
 {
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
Quoted 8 lines

linux-user/syscall.c static int do_execv(CPUArchState *cpu_env, int dirfd, · line 8469

Quoted 6 lines
linux-user/syscall.c +1 -0
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = !is_execveat && qemu_dup_for_children;

Wouldn't it be better to check for dirfd == AT_FDCWD?

Yeah that works.
Ill fix in V2.

Quoted 69 lines
Quoted 68 lines
linux-user/syscall.c +3 -2
     argc = 0;
@@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env,
 int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong),
 q++) {
+    for (gp = guest_argp, q = argp + argp_offset; gp; gp +=
 sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
linux-user/syscall.c +9 -1
@@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int
 dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    }
+    else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
linux-user/syscall.c +1 -1
@@ -8553,7 +8563,7 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong),
 q++) {
+    for (gp = guest_argp, q = argp + argp_offset; *q; gp +=
 sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user- renamed from linux-user/user-internals.h +0 -0

internals.h
index 5c7f173ceb..0719e65ff4 100644

linux-user/user-internals.h +4 -0
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char ** qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t

*buf_temp,

#6 lore raw
Quoted 26 lines · Noah Goldstein
Quoted 18 lines · Ilya Leoshkevich
Quoted 6 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 9 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children.  Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may
not be desirable.

Quoted 4 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

+1

Quoted 7 lines
Quoted 4 lines

There was a similar patch posted to the mailing list some years back,
which I used to cherry-pick when I needed this. I'm not sure what happened to that discussion though.

Yes(ish):
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/

Thanks for finding this! Don't we need the shebang handling here as well?

Laurent, do you per chance know why was it not accepted back then?Unfortunately I cannot find any discussion associated with v3 or v4
[1]. There were some concerns regarding v1 [2], but from what I can see they all were addressed.

[1]
https://patchew.org/QEMU/[email protected]/
[2]
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/

#7 lore raw
Quoted 33 lines · Ilya Leoshkevich
Quoted 31 lines · Noah Goldstein
Quoted 18 lines · Ilya Leoshkevich
Quoted 6 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 9 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may
not be desirable.

Quoted 4 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

+1

Quoted 4 lines

There was a similar patch posted to the mailing list some years back,
which I used to cherry-pick when I needed this. I'm not sure what happened to that discussion though.

Quoted 6 lines
Quoted 2 lines

Yes(ish):
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/

Thanks for finding this! Don't we need the shebang handling here as well?

I don't think so. In this case we aren't making it so execve can point to some arbitrary impl, just that we propagate the current running qemu env.

Quoted 10 lines

Laurent, do you per chance know why was it not accepted back then?Unfortunately I cannot find any discussion associated with v3 or v4
[1]. There were some concerns regarding v1 [2], but from what I can see they all were addressed.

[1]
https://patchew.org/QEMU/[email protected]/
[2]
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/

#8 lore raw
Quoted 38 lines · Noah Goldstein
Quoted 36 lines · Ilya Leoshkevich
Quoted 33 lines · Noah Goldstein
Quoted 18 lines · Ilya Leoshkevich
Quoted 6 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 9 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may
not be desirable.

Quoted 4 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

+1

Quoted 4 lines

There was a similar patch posted to the mailing list some years back,
which I used to cherry-pick when I needed this. I'm not sure what happened to that discussion though.

Yes(ish):
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/

Quoted 7 lines
Quoted 3 lines

Thanks for finding this! Don't we need the shebang handling here as well?

I don't think so. In this case we aren't making it so execve can point to some arbitrary impl, just that we propagate the current running qemu env.

ping

Quoted 10 lines
Quoted 10 lines

Laurent, do you per chance know why was it not accepted back then?Unfortunately I cannot find any discussion associated with v3 or v4
[1]. There were some concerns regarding v1 [2], but from what I can see they all were addressed.

[1]
https://patchew.org/QEMU/[email protected]/
[2]
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/

#9 lore raw
Quoted 43 lines · Noah Goldstein
Quoted 41 lines · Noah Goldstein
Quoted 39 lines · Ilya Leoshkevich
Quoted 26 lines · Noah Goldstein
Quoted 18 lines · Ilya Leoshkevich
Quoted 6 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 9 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may
not be desirable.

Quoted 4 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

+1

Quoted 7 lines
Quoted 4 lines

There was a similar patch posted to the mailing list some years back,
which I used to cherry-pick when I needed this. I'm not sure what happened to that discussion though.

Yes(ish):
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/

Thanks for finding this! Don't we need the shebang handling here as well?

Quoted 6 lines
Quoted 4 lines

I don't think so. In this case we aren't making it so execve can point to some arbitrary impl, just that we propagate the current running qemu env.

ping

ping2

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Laurent, do you per chance know why was it not accepted back then?Unfortunately I cannot find any discussion associated with v3 or v4
[1]. There were some concerns regarding v1 [2], but from what I can see they all were addressed.

[1]
https://patchew.org/QEMU/[email protected]/
[2]
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/

#10 lore raw
Quoted 45 lines · Noah Goldstein
Quoted 43 lines · Noah Goldstein
Quoted 41 lines · Noah Goldstein
Quoted 39 lines · Ilya Leoshkevich
Quoted 26 lines · Noah Goldstein
Quoted 18 lines · Ilya Leoshkevich
Quoted 6 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 9 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may
not be desirable.

Quoted 4 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

+1

Quoted 7 lines
Quoted 4 lines

There was a similar patch posted to the mailing list some years back,
which I used to cherry-pick when I needed this. I'm not sure what happened to that discussion though.

Yes(ish):
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/

Thanks for finding this! Don't we need the shebang handling here as well?

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

I don't think so. In this case we aren't making it so execve can point to some arbitrary impl, just that we propagate the current running qemu env.

ping

ping2

pint3

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Laurent, do you per chance know why was it not accepted back then?Unfortunately I cannot find any discussion associated with v3 or v4
[1]. There were some concerns regarding v1 [2], but from what I can see they all were addressed.

[1]
https://patchew.org/QEMU/[email protected]/
[2]
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/

#11 lore raw
In reply to Ilya Leoshkevich

Le 02/10/2024 à 10:08, Ilya Leoshkevich a écrit :

Quoted 14 lines · Noah Goldstein
Quoted 6 lines

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 6 lines

The motivation for the change is to make it so that plugins running through qemu can continue to run on children.  Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

Quoted 2 lines

Another reason to have this is that one may not have root permissions to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user namespace (since linux v6.7), so you can configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you can do things like that:

With 'F' flag, load the interpreter from the initial namespace:

    $ /bin/qemu-m68k-static --version
    qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
    Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
    $ unshare --map-root-user --fork --pid 
--load-interp=":qemu-m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/bin/qemu-m68k-static:OCF" --root=chroot/m68k/sid
    # QEMU_VERSION= ls
    qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
    Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
    # /qemu-m68k  --version
    qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
    Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers

Without 'F' flag, from inside the namespace:

    $ unshare --map-root-user --fork --pid 
--load-interp=":qemu-m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/qemu-m68k:OC" --root=chroot/m68k/sid
    # QEMU_VERSION= ls
    qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
    Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers
    # /qemu-m68k  --version
    qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
    Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers

Thanks,
Laurent

#12 lore raw
Quoted 69 lines · Laurent Vivier

Le 02/10/2024 à 10:08, Ilya Leoshkevich a écrit :

Quoted 17 lines · Noah Goldstein
Quoted 6 lines

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 9 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children.  Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may
not be desirable.

Quoted 3 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user namespace (since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you can do things like that:

   With 'F' flag, load the interpreter from the initial namespace:

     $ /bin/qemu-m68k-static --version
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
     $ unshare --map-root-user --fork --pid 
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/bin/qemu
-m68k-static:OCF" 
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers

   Without 'F' flag, from inside the namespace:

     $ unshare --map-root-user --fork --pid 
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/qemu-
m68k:OC" 
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it looks really useful.

IIUC it also resolves the main problem this patch is dealing with:

Enabling binfmt for all natively runnable elf files may not be desirable.

#13 lore raw
Quoted 71 lines · Ilya Leoshkevich
Quoted 69 lines · Laurent Vivier
Quoted 17 lines · Ilya Leoshkevich
Quoted 6 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 9 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may
not be desirable.

Quoted 3 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user namespace (since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you can do things like that:

With 'F' flag, load the interpreter from the initial namespace:

     $ /bin/qemu-m68k-static --version
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/bin/qemu
-m68k-static:OCF"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers

Without 'F' flag, from inside the namespace:

     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/qemu-
m68k:OC"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
Quoted 8 lines
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it looks really useful.

IIUC it also resolves the main problem this patch is dealing with:

I might misunderstand, but I don't think it does in the sense that it still might not be desirable to use the same qemu flags for the entire class of executables.

I.e the original motivating case was wanting to attach some plugins to a process and its children and AFAICT binfmt still doesn't give that level of control.

Quoted 3 lines

Enabling binfmt for all natively runnable elf files may not be desirable.

#14 lore raw
Quoted 95 lines · Noah Goldstein
Quoted 91 lines · Ilya Leoshkevich
Quoted 81 lines · Laurent Vivier
Quoted 20 lines · Ilya Leoshkevich
Quoted 7 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the
child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 11 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children.  Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run
natively. Enabling binfmt for all natively runnable elf files
may
not be desirable.

Quoted 3 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user namespace
(since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you can do
things like that:

   With 'F' flag, load the interpreter from the initial namespace:

     $ /bin/qemu-m68k-static --version
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/bin/
qemu
-m68k-static:OCF"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers

   Without 'F' flag, from inside the namespace:

     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/qemu
-
m68k:OC"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it looks
really useful.

Quoted 1 line

IIUC it also resolves the main problem this patch is dealing with:

Quoted 7 lines

I might misunderstand, but I don't think it does in the sense that it still might not be desirable to use the same qemu flags for the entire class of executables.

I.e the original motivating case was wanting to attach some plugins to a process and its children and AFAICT binfmt still doesn't give that level of control.

I think if you start a process in a user namespace, which has a binfmt_misc handler for a certain class of binaries, then this handler will affect only this process and its children, and not the rest of the system.

Quoted 2 lines
Quoted 2 lines

  Enabling binfmt for all natively runnable elf files may not be desirable.

#15 lore raw
Quoted 101 lines · Ilya Leoshkevich
Quoted 99 lines · Noah Goldstein
Quoted 91 lines · Ilya Leoshkevich
Quoted 81 lines · Laurent Vivier
Quoted 20 lines · Ilya Leoshkevich
Quoted 7 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the
child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 11 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run
natively. Enabling binfmt for all natively runnable elf files
may
not be desirable.

Quoted 3 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user namespace
(since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you can do
things like that:

With 'F' flag, load the interpreter from the initial
namespace:

     $ /bin/qemu-m68k-static --version
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/bin/
qemu
-m68k-static:OCF"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers

Without 'F' flag, from inside the namespace:

     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/qemu
-
m68k:OC"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it looks
really useful.

Quoted 1 line

IIUC it also resolves the main problem this patch is dealing with:

I might misunderstand, but I don't think it does in the sense that it still might not be desirable to use the same qemu flags for the entire class of executables.

Quoted 8 lines
Quoted 3 lines

I.e the original motivating case was wanting to attach some plugins to a process and its children and AFAICT binfmt still doesn't give that level of control.

I think if you start a process in a user namespace, which has a binfmt_misc handler for a certain class of binaries, then this handler will affect only this process and its children, and not the rest of the system.

It won't also affect other binaries in the user namespace?

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

Enabling binfmt for all natively runnable elf files may not be desirable.

#16 lore raw
Quoted 126 lines · Noah Goldstein
Quoted 124 lines · Ilya Leoshkevich
Quoted 121 lines · Noah Goldstein
Quoted 108 lines · Ilya Leoshkevich
Quoted 98 lines · Laurent Vivier
Quoted 25 lines · Ilya Leoshkevich
Quoted 9 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve
the
child
process will be launch by the same qemu executable that is
currently
running along with its current commandline arguments.

Quoted 14 lines

The motivation for the change is to make it so that plugins
running
through qemu can continue to run on children.  Why not just
binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can
run
natively. Enabling binfmt for all natively runnable elf files
may
not be desirable.

Quoted 3 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user namespace
(since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you can
do
things like that:

   With 'F' flag, load the interpreter from the initial namespace:

     $ /bin/qemu-m68k-static --version
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU
Project
developers
     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00
\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\x
fe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/
bin/
qemu
-m68k-static:OCF"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU
Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers

   Without 'F' flag, from inside the namespace:

     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00
\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\x
fe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/
qemu
-
m68k:OC"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it looks
really useful.

Quoted 2 lines

IIUC it also resolves the main problem this patch is dealing with:

I might misunderstand, but I don't think it does in the sense that it still might not be desirable to use the same qemu flags for the entire class of executables.

I.e the original motivating case was wanting to attach some plugins to a process and its children and AFAICT binfmt still doesn't give that level of control.

Quoted 8 lines
Quoted 6 lines

I think if you start a process in a user namespace, which has a binfmt_misc handler for a certain class of binaries, then this handler
will affect only this process and its children, and not the rest of the
system.

It won't also affect other binaries in the user namespace?

It would, but you should be able to create a user namespace just for your program. It should also be possible to nest user namespaces.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

  Enabling binfmt for all natively runnable elf files may not be desirable.

#17 lore raw
Quoted 135 lines · Ilya Leoshkevich
Quoted 133 lines · Noah Goldstein
Quoted 130 lines · Ilya Leoshkevich
Quoted 114 lines · Noah Goldstein
Quoted 108 lines · Ilya Leoshkevich
Quoted 98 lines · Laurent Vivier
Quoted 25 lines · Ilya Leoshkevich
Quoted 9 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve
the
child
process will be launch by the same qemu executable that is
currently
running along with its current commandline arguments.

Quoted 14 lines

The motivation for the change is to make it so that plugins
running
through qemu can continue to run on children. Why not just
binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can
run
natively. Enabling binfmt for all natively runnable elf files
may
not be desirable.

Quoted 3 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user namespace
(since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you can
do
things like that:

With 'F' flag, load the interpreter from the initial
namespace:

     $ /bin/qemu-m68k-static --version
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU
Project
developers
     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00
\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\x
fe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/
bin/
qemu
-m68k-static:OCF"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU
Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers

Without 'F' flag, from inside the namespace:

     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00
\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\x
fe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/
qemu
-
m68k:OC"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it looks
really useful.

Quoted 2 lines

IIUC it also resolves the main problem this patch is dealing with:

Quoted 7 lines

I might misunderstand, but I don't think it does in the sense that it still might not be desirable to use the same qemu flags for the entire class of executables.

I.e the original motivating case was wanting to attach some plugins to a process and its children and AFAICT binfmt still doesn't give that level of control.

I think if you start a process in a user namespace, which has a binfmt_misc handler for a certain class of binaries, then this handler
will affect only this process and its children, and not the rest of the
system.

Quoted 4 lines
Quoted 1 line

It won't also affect other binaries in the user namespace?

It would, but you should be able to create a user namespace just for your program. It should also be possible to nest user namespaces.

Okay fair enough. Still pro this patch as an easier means but guess it loses any necessity.

To be clear, are you rejecting?

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Enabling binfmt for all natively runnable elf files may not be desirable.

#18 lore raw
Quoted 159 lines · Noah Goldstein
Quoted 157 lines · Ilya Leoshkevich
Quoted 154 lines · Noah Goldstein
Quoted 142 lines · Ilya Leoshkevich
Quoted 131 lines · Noah Goldstein
Quoted 124 lines · Ilya Leoshkevich
Quoted 113 lines · Laurent Vivier
Quoted 30 lines · Ilya Leoshkevich
Quoted 19 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve
the
child
process will be launch by the same qemu executable that
is
currently
running along with its current commandline arguments.

The motivation for the change is to make it so that plugins
running
through qemu can continue to run on children.  Why not
just
binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files

Quoted 8 lines

that
can
run
natively. Enabling binfmt for all natively runnable elf
files
may
not be desirable.

Quoted 4 lines

Another reason to have this is that one may not have root
permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user
namespace
(since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you
can
do
things like that:

   With 'F' flag, load the interpreter from the initial namespace:

     $ /bin/qemu-m68k-static --version
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU
Project
developers
     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\
\x00
\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xf
f\\x
fe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\x
ff:/
bin/
qemu
-m68k-static:OCF"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU
Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers

   Without 'F' flag, from inside the namespace:

     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\
\x00
\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xf
f\\x
fe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\x
ff:/
qemu
-
m68k:OC"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it
looks
really useful.

Quoted 3 lines

IIUC it also resolves the main problem this patch is dealing
with:

Quoted 8 lines

I might misunderstand, but I don't think it does in the sense that it still might not be desirable to use the same qemu flags
for the entire class of executables.

I.e the original motivating case was wanting to attach some plugins to a process and its children and AFAICT binfmt still doesn't give that level of control.

Quoted 7 lines

I think if you start a process in a user namespace, which has a binfmt_misc handler for a certain class of binaries, then this handler
will affect only this process and its children, and not the rest of
the
system.

It won't also affect other binaries in the user namespace?

Quoted 8 lines
Quoted 3 lines

It would, but you should be able to create a user namespace just for your program. It should also be possible to nest user namespaces.

Okay fair enough. Still pro this patch as an easier means but guess it loses any necessity.

To be clear, are you rejecting?

No, personally I still find it useful, because the systems with old kernels will be around for quite some time.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

  Enabling binfmt for all natively runnable elf files may not be desirable.

#19 lore raw
Quoted 165 lines · Ilya Leoshkevich
Quoted 163 lines · Noah Goldstein
Quoted 160 lines · Ilya Leoshkevich
Quoted 153 lines · Noah Goldstein
Quoted 142 lines · Ilya Leoshkevich
Quoted 131 lines · Noah Goldstein
Quoted 124 lines · Ilya Leoshkevich
Quoted 113 lines · Laurent Vivier
Quoted 30 lines · Ilya Leoshkevich
Quoted 19 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve
the
child
process will be launch by the same qemu executable that
is
currently
running along with its current commandline arguments.

The motivation for the change is to make it so that plugins
running
through qemu can continue to run on children. Why not
just
binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files

Quoted 8 lines

that
can
run
natively. Enabling binfmt for all natively runnable elf
files
may
not be desirable.

Quoted 4 lines

Another reason to have this is that one may not have root
permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user
namespace
(since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you
can
do
things like that:

With 'F' flag, load the interpreter from the initial
namespace:

     $ /bin/qemu-m68k-static --version
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU
Project
developers
     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\
\x00
\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xf
f\\x
fe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\x
ff:/
bin/
qemu
-m68k-static:OCF"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
     Copyright (c) 2003-2023 Fabrice Bellard and the QEMU
Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers

Without 'F' flag, from inside the namespace:

     $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\
\x00
\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xf
f\\x
fe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\x
ff:/
qemu
-
m68k:OC"
--root=chroot/m68k/sid
     # QEMU_VERSION= ls
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers
     # /qemu-m68k  --version
     qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
     Copyright (c) 2003-2022 Fabrice Bellard and the QEMU
Project
developers
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it
looks
really useful.

Quoted 3 lines

IIUC it also resolves the main problem this patch is dealing
with:

Quoted 8 lines

I might misunderstand, but I don't think it does in the sense that it still might not be desirable to use the same qemu flags
for the entire class of executables.

I.e the original motivating case was wanting to attach some plugins to a process and its children and AFAICT binfmt still doesn't give that level of control.

Quoted 7 lines

I think if you start a process in a user namespace, which has a binfmt_misc handler for a certain class of binaries, then this handler
will affect only this process and its children, and not the rest of
the
system.

Quoted 1 line

It won't also affect other binaries in the user namespace?

It would, but you should be able to create a user namespace just for your program. It should also be possible to nest user namespaces.

Quoted 7 lines
Quoted 4 lines

Okay fair enough. Still pro this patch as an easier means but guess it loses any necessity.

To be clear, are you rejecting?

No, personally I still find it useful, because the systems with old kernels will be around for quite some time.

:)

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Enabling binfmt for all natively runnable elf files may not be desirable.

#20 lore raw
In reply to Ilya Leoshkevich

Le 02/10/2024 à 16:53, Ilya Leoshkevich a écrit :

Quoted 100 lines · Noah Goldstein
Quoted 99 lines
Quoted 91 lines · Ilya Leoshkevich
Quoted 81 lines · Laurent Vivier
Quoted 20 lines · Ilya Leoshkevich
Quoted 7 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the
child
process will be launch by the same qemu executable that is currently
running along with its current commandline arguments.

Quoted 11 lines

The motivation for the change is to make it so that plugins running
through qemu can continue to run on children.  Why not just binfmt?: Plugins can be desirable regardless of system/architecture
emulation, and can sometimes be useful for elf files that can run
natively. Enabling binfmt for all natively runnable elf files
may
not be desirable.

Quoted 3 lines

Another reason to have this is that one may not have root permissions
to configure binfmt-misc.

A little note on that: binfmt_misc is now part of the user namespace
(since linux v6.7), so you can
configure binfmt_misc as a non root user in a given namepace.

There is helper to use it with unshare from util-linux, you can do
things like that:

   With 'F' flag, load the interpreter from the initial
namespace:

      $ /bin/qemu-m68k-static --version
      qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
      Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
      $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/bin/
qemu
-m68k-static:OCF"
--root=chroot/m68k/sid
      # QEMU_VERSION= ls
      qemu-m68k version 8.2.2 (qemu-8.2.2-1.fc40)
      Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project
developers
      # /qemu-m68k  --version
      qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
      Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers

   Without 'F' flag, from inside the namespace:

      $ unshare --map-root-user --fork --pid
--load-interp=":qemu-
m68k:M::\\x7fELF\\x01\\x02\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x0
0\\x
00\\x00\\x00\\x02\\x00\\x04:\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\
x00\
\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:/qemu
-
m68k:OC"
--root=chroot/m68k/sid
      # QEMU_VERSION= ls
      qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
      Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
      # /qemu-m68k  --version
      qemu-m68k version 8.0.50 (v8.0.0-340-gb1cff5e2da95)
      Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project
developers
Quoted 3 lines

Thanks,
Laurent

Thanks for posting this, I wasn't aware of this feature and it looks
really useful.

Quoted 1 line

IIUC it also resolves the main problem this patch is dealing with:

I might misunderstand, but I don't think it does in the sense that it still might not be desirable to use the same qemu flags for the entire class of executables.

Quoted 9 lines
Quoted 3 lines

I.e the original motivating case was wanting to attach some plugins to a process and its children and AFAICT binfmt still doesn't give that level of control.

I think if you start a process in a user namespace, which has a binfmt_misc handler for a certain class of binaries, then this handler will affect only this process and its children, and not the rest of the system.

Yes, the binfmt_misc configuration is only available in the given namespace.

Thanks,
Laurent

#21 lore raw

PATCH v2 1/1 linux-user: Add option to run `execve`d programs through QEMU

In reply to Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
 linux-user/main.c           | 21 +++++++++++++++++++++
 linux-user/syscall.c        | 21 ++++++++++++++++-----
 linux-user/user-internals.h |  4 ++++
 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +21 -0
index 8143a0d4b0..5e3d41dc2b 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char **qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,14 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +16 -5
index a666986189..79019c0db0 100644
@@ -8457,13 +8457,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = dirfd == AT_FDCWD && qemu_dup_for_children;
 
     argc = 0;
 
@@ -8487,10 +8488,12 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset;
+         gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8535,9 +8538,16 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    } else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8551,7 +8561,8 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset;
+         *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +4 -0
index 5c7f173ceb..a4615eee54 100644
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char **qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
Signature · 1 line
2.43.0
#22 lore raw
In reply to Noah Goldstein
Quoted 10 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

Broadly I don't see anything wrong with the patch. However my principle concern is how test it. Currently nothing in check-tcg ensures this mechanism works and stresses the argument copying.

Quoted 139 lines
---
 linux-user/main.c           | 22 ++++++++++++++++++++++
 linux-user/syscall.c        | 20 +++++++++++++++-----
 linux-user/user-internals.h |  4 ++++
 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +22 -0
index 8143a0d4b0..dfb303a1f2 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char ** qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        int i;
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +15 -5
index 9d5415674d..732ef89054 100644
@@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = !is_execveat && qemu_dup_for_children;
 
     argc = 0;
 
@@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    }
+    else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8553,7 +8563,7 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +4 -0
index 5c7f173ceb..0719e65ff4 100644
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char ** qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#23 lore raw
Quoted 14 lines · Alex Bennée
Quoted 10 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

Quoted 3 lines

Broadly I don't see anything wrong with the patch. However my principle concern is how test it. Currently nothing in check-tcg ensures this mechanism works and stresses the argument copying.

Fair enough. I will add some tests.

Quoted 144 lines
Quoted 130 lines
---
 linux-user/main.c           | 22 ++++++++++++++++++++++
 linux-user/syscall.c        | 20 +++++++++++++++-----
 linux-user/user-internals.h |  4 ++++
 3 files changed, 41 insertions(+), 5 deletions(-)
linux-user/main.c +22 -0
index 8143a0d4b0..dfb303a1f2 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char ** qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        int i;
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +15 -5
index 9d5415674d..732ef89054 100644
@@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = !is_execveat && qemu_dup_for_children;
 
     argc = 0;
 
@@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    }
+    else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8553,7 +8563,7 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset; *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +0 -0
index 5c7f173ceb..0719e65ff4 100644

linux-user/user-internals.h void stop_all_tasks(void); · line 39

Quoted 10 lines
linux-user/user-internals.h +4 -0
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char ** qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,

--
Alex Bennée
Virtualisation Tech Lead @ Linaro

#24 lore raw
In reply to Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
 linux-user/main.c                             | 21 ++++++
 linux-user/syscall.c                          | 21 ++++--
 linux-user/user-internals.h                   |  4 ++
 tests/tcg/multiarch/Makefile.target           |  8 +++
 .../linux/linux-execve-qemu-children.c        | 68 +++++++++++++++++++
 5 files changed, 117 insertions(+), 5 deletions(-)
 create mode 100644 tests/tcg/multiarch/linux/linux-execve-qemu-children.c
linux-user/main.c +21 -0
index 8143a0d4b0..5e3d41dc2b 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char **qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,14 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +16 -5
index 59b2080b98..96b105e9ce 100644
@@ -8550,13 +8550,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = dirfd == AT_FDCWD && qemu_dup_for_children;
 
     argc = 0;
 
@@ -8580,10 +8581,12 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset;
+         gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8628,9 +8631,16 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    } else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8644,7 +8654,8 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset;
+         *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +4 -0
index 46ffc093f4..ed3ed666a0 100644
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char **qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
tests/tcg/multiarch/Makefile.target +8 -0
index 78b83d5575..0e220953e7 100644
@@ -30,6 +30,14 @@ run-float_%: float_%
 	$(call conditional-diff-out,$<,$(SRC_PATH)/tests/tcg/$(TARGET_NAME)/$<.ref)
 
 
+run-linux-execve-qemu-children: linux-execve-qemu-children
+	$(call run-test,$<, $(QEMU) $(QEMU_OPTS) -qemu-children $< $(QEMU) 0)
+	$(call run-test,$<, $(QEMU) $(QEMU_OPTS) $< linux-execve 0 skip)
+
+run-plugin-linux-execve-qemu-children-with-%: linux-execve-qemu-children
+	$(call run-test,$<, $(QEMU) $(QEMU_OPTS) -qemu-children $< $(QEMU) 0)
+	$(call run-test,$<, $(QEMU) $(QEMU_OPTS) $< linux-execve 0 skip)
+
 testthread: LDFLAGS+=-lpthread
 
 threadcount: LDFLAGS+=-lpthread
tests/tcg/multiarch/linux/linux-execve-qemu-children.c new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.43.0
#25 lore raw
Quoted 247 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
 linux-user/main.c                             | 21 ++++++
 linux-user/syscall.c                          | 21 ++++--
 linux-user/user-internals.h                   |  4 ++
 tests/tcg/multiarch/Makefile.target           |  8 +++
 .../linux/linux-execve-qemu-children.c        | 68 +++++++++++++++++++
 5 files changed, 117 insertions(+), 5 deletions(-)
 create mode 100644 tests/tcg/multiarch/linux/linux-execve-qemu-children.c
linux-user/main.c +21 -0
index 8143a0d4b0..5e3d41dc2b 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
 uintptr_t guest_base;
 bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char **qemu_argv;
+
 /*
  * Used to implement backwards-compatibility for the `-strace`, and
  * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -729,6 +742,14 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);
+        }
+    }
+
     qemu_set_log_filename_flags(last_log_filename,
                                 last_log_mask | (enable_strace * LOG_STRACE),
                                 &error_fatal);
linux-user/syscall.c +16 -5
index 59b2080b98..96b105e9ce 100644
@@ -8550,13 +8550,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
                     abi_long pathname, abi_long guest_argp,
                     abi_long guest_envp, int flags, bool is_execveat)
 {
-    int ret;
+    int ret, argp_offset;
     char **argp, **envp;
     int argc, envc;
     abi_ulong gp;
     abi_ulong addr;
     char **q;
     void *p;
+    bool through_qemu = dirfd == AT_FDCWD && qemu_dup_for_children;
 
     argc = 0;
 
@@ -8580,10 +8581,12 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
         envc++;
     }
 
-    argp = g_new0(char *, argc + 1);
+    argp_offset = through_qemu ? qemu_argc : 0;
+    argp = g_new0(char *, argc + argp_offset + 1);
     envp = g_new0(char *, envc + 1);
 
-    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset;
+         gp; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp)) {
             goto execve_efault;
         }
@@ -8628,9 +8631,16 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
     }
 
     const char *exe = p;
-    if (is_proc_myself(p, "exe")) {
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    } else if (is_proc_myself(p, "exe")) {
         exe = exec_path;
     }
+
     ret = is_execveat
         ? safe_execveat(dirfd, exe, argp, envp, flags)
         : safe_execve(exe, argp, envp);
@@ -8644,7 +8654,8 @@ execve_efault:
     ret = -TARGET_EFAULT;
 
 execve_end:
-    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+    for (gp = guest_argp, q = argp + argp_offset;
+         *q; gp += sizeof(abi_ulong), q++) {
         if (get_user_ual(addr, gp) || !addr) {
             break;
         }
linux-user/user-internals.h +4 -0
index 46ffc093f4..ed3ed666a0 100644
@@ -30,6 +30,10 @@ void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 extern unsigned long mmap_min_addr;
 
+extern bool qemu_dup_for_children;
+extern int qemu_argc;
+extern char **qemu_argv;
+
 typedef struct IOCTLEntry IOCTLEntry;
 
 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
tests/tcg/multiarch/Makefile.target +8 -0
index 78b83d5575..0e220953e7 100644
@@ -30,6 +30,14 @@ run-float_%: float_%
        $(call conditional-diff-out,$<,$(SRC_PATH)/tests/tcg/$(TARGET_NAME)/$<.ref)
 
 
+run-linux-execve-qemu-children: linux-execve-qemu-children
+       $(call run-test,$<, $(QEMU) $(QEMU_OPTS) -qemu-children $< $(QEMU) 0)
+       $(call run-test,$<, $(QEMU) $(QEMU_OPTS) $< linux-execve 0 skip)
+
+run-plugin-linux-execve-qemu-children-with-%: linux-execve-qemu-children
+       $(call run-test,$<, $(QEMU) $(QEMU_OPTS) -qemu-children $< $(QEMU) 0)
+       $(call run-test,$<, $(QEMU) $(QEMU_OPTS) $< linux-execve 0 skip)
+
 testthread: LDFLAGS+=-lpthread
 
 threadcount: LDFLAGS+=-lpthread
tests/tcg/multiarch/linux/linux-execve-qemu-children.c new file +68 -1
new file mode 100644
index 0000000000..60d6537666
@@ -0,0 +1,68 @@
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#define MAX_COMM_SIZE (4096)
+
+int
+main(int argc, char ** argv, char ** envp) {
+    int          fd;
+    char         next_arg[2];
+    char *       buf;
+    ssize_t      off;
+    const char * expec_comm;
+    assert(argc == 3 || argc == 4);
+    fd = open("/proc/self/comm", O_RDONLY);
+    assert(fd > 0);
+
+    buf = calloc(MAX_COMM_SIZE + 1, 1);
+    assert(buf != NULL);
+
+    off = 0;
+    for (;;) {
+        ssize_t res = read(fd, buf + off, 1);
+        if (res < 0 && errno != EAGAIN) {
+            perror("Failed to read comm");
+            return -1;
+        }
+        if (res == 0) {
+            break;
+        }
+
+        off += res;
+
+        if (off >= MAX_COMM_SIZE) {
+            fprintf(stderr, "/proc/self/comm too large for test\n");
+            return -1;
+        }
+    }
+    assert(off && buf[off] == '\0' && buf[off - 1] == '\n');
+    buf[off - 1] = '\0';
+    expec_comm   = basename(argv[1]);
+    if (argc == 3 && strncmp(buf, expec_comm, strlen(expec_comm))) {
+        fprintf(stderr,
+                "Didn't propagate qemu settings\nComm:  '%s'\nExpec: '%s'\n",
+                buf, expec_comm);
+        return -1;
+    }
+    free(buf);
+    next_arg[0] = argv[2][0];
+    next_arg[1] = '\0';
+    if (next_arg[0] == '9') {
+        return 0;
+    }
+    next_arg[0] += 1;
+    char * next_args[] = { argv[0], argv[1], next_arg, NULL };
+    int    eres        = execve(argv[0], &next_args[0], envp);
+    if (eres != 0) {
+        fprintf(stderr, "Unable to execve: %d/%d -> %s\n", eres, errno,
+                strerror(errno));
+        return -1;
+    }
+    return 0;
+}
--
Quoted 2 lines

2.43.0

Added test that tests both old behavior (no propagation of qemu) and new behavior (propagation of qemu + cmdline).

Tested on Aarch64 + Linux with:

```
make check-tcg
```
#26 lore raw
In reply to Noah Goldstein
Quoted 60 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
  linux-user/main.c                             | 21 ++++++
  linux-user/syscall.c                          | 21 ++++--
  linux-user/user-internals.h                   |  4 ++
  tests/tcg/multiarch/Makefile.target           |  8 +++
  .../linux/linux-execve-qemu-children.c        | 68 +++++++++++++++++++
  5 files changed, 117 insertions(+), 5 deletions(-)

create mode 100644 tests/tcg/multiarch/linux/linux-execve-qemu-children.c

linux-user/main.c +13 -0
index 8143a0d4b0..5e3d41dc2b 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
  uintptr_t guest_base;
  bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char **qemu_argv;
+
  /*
   * Used to implement backwards-compatibility for the `-strace`, and
   * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
      perf_enable_jitdump();
  }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
  static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
  #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
       "",           "Generate a /tmp/perf-${pid}.map file for perf"},
      {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
       "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
      {NULL, NULL, false, NULL, NULL, NULL}
  };
 

linux-user/main.c int main(int argc, char **argv, char **envp) · line 749

Quoted 8 lines
linux-user/main.c +5 -0
@@ -729,6 +742,14 @@ int main(int argc, char **argv, char **envp)
 
      optind = parse_args(argc, argv);
 
+    if (qemu_dup_for_children) {
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);

g_strdup.

linux-user/main.c int main(int argc, char **argv, char **envp) · line 750

Quoted 1 line
linux-user/main.c +1 -0
+    bool through_qemu = dirfd == AT_FDCWD && qemu_dup_for_children;

Why is this limited to AT_FDCWD? Why not for execvat too?

Quoted 5 lines
linux-user/main.c +0 -1
@@ -8628,9 +8631,16 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
      }
 
      const char *exe = p;
-    if (is_proc_myself(p, "exe")) {

linux-user/main.c static int do_execv(CPUArchState *cpu_env, int dirfd, · line 8643

Quoted 10 lines
linux-user/main.c +8 -0
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    } else if (is_proc_myself(p, "exe")) {
          exe = exec_path;
      }
+

You still need to handle is_proc_myself, for the guest binary.

I wonder if those two cases are related. Do we need to also add an argument so that we can pass the executable to the next qemu via file descriptor? I.e. execvat becomes

f = openat()
execv(qemu, "-execfd", f)

and is_proc_myself uses execfd, which we already have open.

r~

#27 lore raw
Quoted 65 lines · Richard Henderson
Quoted 63 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
  linux-user/main.c                             | 21 ++++++
  linux-user/syscall.c                          | 21 ++++--
  linux-user/user-internals.h                   |  4 ++
  tests/tcg/multiarch/Makefile.target           |  8 +++
  .../linux/linux-execve-qemu-children.c        | 68 +++++++++++++++++++
  5 files changed, 117 insertions(+), 5 deletions(-)

create mode 100644 tests/tcg/multiarch/linux/linux-execve-qemu-children.c

linux-user/main.c +13 -0
index 8143a0d4b0..5e3d41dc2b 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
  uintptr_t guest_base;
  bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char **qemu_argv;
+
  /*
   * Used to implement backwards-compatibility for the `-strace`, and
   * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
      perf_enable_jitdump();
  }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
  static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
  #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
       "",           "Generate a /tmp/perf-${pid}.map file for perf"},
      {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
       "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
      {NULL, NULL, false, NULL, NULL, NULL}
  };
 
@@ -729,6 +742,14 @@ int main(int argc, char **argv, char **envp)
 
      optind = parse_args(argc, argv);
 
Quoted 7 lines

linux-user/main.c int main(int argc, char **argv, char **envp) · line 749

Quoted 5 lines
linux-user/main.c +5 -0
+    if (qemu_dup_for_children) {
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);

g_strdup.

ack

Quoted 5 lines

linux-user/main.c int main(int argc, char **argv, char **envp) · line 750

Quoted 2 lines
linux-user/main.c +1 -0
+    bool through_qemu = dirfd == AT_FDCWD && qemu_dup_for_children;

Why is this limited to AT_FDCWD? Why not for execvat too?

We could, initially it was because AFAICT qemu doesn't support executing a program relative to another dir, but it would be simple enough to just join the relative program path and path dirfd points to.

Want me to add support?

Quoted 28 lines
Quoted 5 lines
linux-user/main.c +0 -1
@@ -8628,9 +8631,16 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
      }
 
      const char *exe = p;
-    if (is_proc_myself(p, "exe")) {

linux-user/main.c static int do_execv(CPUArchState *cpu_env, int dirfd, · line 8643

Quoted 10 lines
linux-user/main.c +8 -0
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    } else if (is_proc_myself(p, "exe")) {
          exe = exec_path;
      }
+

You still need to handle is_proc_myself, for the guest binary.

I wonder if those two cases are related. Do we need to also add an argument so that we can pass the executable to the next qemu via file descriptor? I.e. execvat becomes

f = openat()
execv(qemu, "-execfd", f)

and is_proc_myself uses execfd, which we already have open.

r~

#28 lore raw
Quoted 79 lines · Noah Goldstein
Quoted 65 lines · Richard Henderson
Quoted 63 lines · Noah Goldstein

The new option '-qemu-children' makes it so that on execve the child process will be launch by the same qemu executable that is currently running along with its current commandline arguments.

The motivation for the change is to make it so that plugins running through qemu can continue to run on children. Why not just binfmt?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling binfmt for all natively runnable elf files may not be desirable.

---
  linux-user/main.c                             | 21 ++++++
  linux-user/syscall.c                          | 21 ++++--
  linux-user/user-internals.h                   |  4 ++
  tests/tcg/multiarch/Makefile.target           |  8 +++
  .../linux/linux-execve-qemu-children.c        | 68 +++++++++++++++++++
  5 files changed, 117 insertions(+), 5 deletions(-)

create mode 100644 tests/tcg/multiarch/linux/linux-execve-qemu-children.c

linux-user/main.c +13 -0
index 8143a0d4b0..5e3d41dc2b 100644
@@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
  uintptr_t guest_base;
  bool have_guest_base;
 
+bool qemu_dup_for_children;
+int qemu_argc;
+char **qemu_argv;
+
  /*
   * Used to implement backwards-compatibility for the `-strace`, and
   * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
@@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
      perf_enable_jitdump();
  }
 
+static void handle_arg_qemu_children(const char *arg)
+{
+    qemu_dup_for_children = true;
+}
+
  static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
  #ifdef CONFIG_PLUGIN
@@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
       "",           "Generate a /tmp/perf-${pid}.map file for perf"},
      {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
       "",           "Generate a jit-${pid}.dump file for perf"},
+    {"qemu-children",
+                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
+     "",           "Run child processes (created with execve) with qemu "
+                   "(as instantiated for the parent)"},
      {NULL, NULL, false, NULL, NULL, NULL}
  };
 
@@ -729,6 +742,14 @@ int main(int argc, char **argv, char **envp)
 
      optind = parse_args(argc, argv);
 
Quoted 7 lines

linux-user/main.c int main(int argc, char **argv, char **envp) · line 749

Quoted 5 lines
linux-user/main.c +5 -0
+    if (qemu_dup_for_children) {
+        qemu_argc = optind;
+        qemu_argv = g_new0(char *, qemu_argc);
+        for (i = 0; i < optind; ++i) {
+            qemu_argv[i] = strdup(argv[i]);

g_strdup.

ack

Quoted 3 lines
Quoted 2 lines
linux-user/main.c +1 -0
+    bool through_qemu = dirfd == AT_FDCWD && qemu_dup_for_children;
Quoted 25 lines
Quoted 2 lines

Why is this limited to AT_FDCWD? Why not for execvat too?

We could, initially it was because AFAICT qemu doesn't support executing a program relative to another dir, but it would be simple enough to just join the relative program path and path dirfd points to.

Want me to add support?

Quoted 17 lines
Quoted 5 lines
linux-user/main.c +0 -1
@@ -8628,9 +8631,16 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
      }
 
      const char *exe = p;
-    if (is_proc_myself(p, "exe")) {

linux-user/main.c static int do_execv(CPUArchState *cpu_env, int dirfd, · line 8643

Quoted 10 lines
linux-user/main.c +8 -0
+    if (through_qemu) {
+        int i;
+        for (i = 0; i < argp_offset; ++i) {
+            argp[i] = qemu_argv[i];
+        }
+        exe = qemu_argv[0];
+    } else if (is_proc_myself(p, "exe")) {
          exe = exec_path;
      }
+

You still need to handle is_proc_myself, for the guest binary.

Would this by handled by basically do:

```
if (is_proc_myself(p, "exe")) {
        exe = exec_path;
        if (through_qemu)
            argp[argp_offset] = exec_path;
}
```

Or am I missing something?

Quoted 8 lines
Quoted 8 lines

I wonder if those two cases are related. Do we need to also add an argument so that we can pass the executable to the next qemu via file descriptor? I.e. execvat becomes

f = openat()
execv(qemu, "-execfd", f)

and is_proc_myself uses execfd, which we already have open.

How does passing a fd from one process to another work?

Quoted 3 lines
Quoted 3 lines

r~

#29 lore raw
Quoted 4 lines · Noah Goldstein
Quoted 1 line
Quoted 1 line

You still need to handle is_proc_myself, for the guest binary.

Would this by handled by basically do:

Quoted 8 lines
```
if (is_proc_myself(p, "exe")) {
         exe = exec_path;
         if (through_qemu)
             argp[argp_offset] = exec_path;
}
```

Or am I missing something?

Something like that, yes.

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

I wonder if those two cases are related. Do we need to also add an argument so that we can pass the executable to the next qemu via file descriptor? I.e. execvat becomes

f = openat()
execv(qemu, "-execfd", f)

and is_proc_myself uses execfd, which we already have open.

How does passing a fd from one process to another work?

As long as the fd is not marked O_CLOEXEC, it stays open in the new process. Providing the number via command-line, or whatever, is sufficient for the new process to know what is going on.

I now realize this is necessary for the AT_EMPTY_PATH flag, where we only have the file descriptor.

r~

#30 lore raw
Quoted 23 lines · Richard Henderson
Quoted 4 lines · Noah Goldstein
Quoted 1 line
Quoted 1 line

You still need to handle is_proc_myself, for the guest binary.

Would this by handled by basically do:

Quoted 8 lines
```
if (is_proc_myself(p, "exe")) {
         exe = exec_path;
         if (through_qemu)
             argp[argp_offset] = exec_path;
}
```

Or am I missing something?

Something like that, yes.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

I wonder if those two cases are related. Do we need to also add an argument so that we can pass the executable to the next qemu via file descriptor? I.e. execvat becomes

f = openat()
execv(qemu, "-execfd", f)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

and is_proc_myself uses execfd, which we already have open.

How does passing a fd from one process to another work?

As long as the fd is not marked O_CLOEXEC, it stays open in the new process. Providing the number via command-line, or whatever, is sufficient for the new process to know what is going on.

Err I guess I was thinking its a bit weird having an option that is only really applicable
if qemu is a child process. I.e the -execfd argument is not usable from commandline.

Quoted 3 lines

I now realize this is necessary for the AT_EMPTY_PATH flag, where we only have the file descriptor.

We could also do something along the lines of:

```
fd = openat(dirfd, exe);
char new_exe[PATH_MAX];
char fd_path[PATH_MAX];
sprintf(fd_path, "/proc/self/fd/%d", fd);
readlink(fd_path, new_exe, PATH_MAX);
exe = new_exe;
```
Quoted 3 lines

r~

#31 lore raw
Quoted 28 lines · Noah Goldstein
Quoted 26 lines · Richard Henderson
Quoted 4 lines · Noah Goldstein
Quoted 1 line
Quoted 1 line

You still need to handle is_proc_myself, for the guest binary.

Would this by handled by basically do:

Quoted 8 lines
```
if (is_proc_myself(p, "exe")) {
          exe = exec_path;
          if (through_qemu)
              argp[argp_offset] = exec_path;
}
```

Or am I missing something?

Something like that, yes.

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

I wonder if those two cases are related. Do we need to also add an argument so that we can pass the executable to the next qemu via file descriptor? I.e. execvat becomes

f = openat()
execv(qemu, "-execfd", f)

and is_proc_myself uses execfd, which we already have open.

How does passing a fd from one process to another work?

Quoted 8 lines
Quoted 3 lines

As long as the fd is not marked O_CLOEXEC, it stays open in the new process. Providing the number via command-line, or whatever, is sufficient for the new process to know what is going on.

Err I guess I was thinking its a bit weird having an option that is only really applicable
if qemu is a child process. I.e the -execfd argument is not usable from commandline.

qemu-foo -execfd 3 3< /some/file

Or perhaps opened via other tooling.

Quoted 11 lines
Quoted 2 lines

I now realize this is necessary for the AT_EMPTY_PATH flag, where we only have the file descriptor.

We could also do something along the lines of:

```
fd = openat(dirfd, exe);
char new_exe[PATH_MAX];
char fd_path[PATH_MAX];
sprintf(fd_path, "/proc/self/fd/%d", fd);
readlink(fd_path, new_exe, PATH_MAX);

Reading the link doesn't always work.
Reading or passing the link means AT_SYMLINK_NOFOLLOW isn't honored.

r~

#32 lore raw
Quoted 55 lines · Richard Henderson
Quoted 28 lines · Noah Goldstein
Quoted 26 lines · Richard Henderson
Quoted 4 lines · Noah Goldstein
Quoted 1 line
Quoted 1 line

You still need to handle is_proc_myself, for the guest binary.

Would this by handled by basically do:

Quoted 8 lines
```
if (is_proc_myself(p, "exe")) {
          exe = exec_path;
          if (through_qemu)
              argp[argp_offset] = exec_path;
}
```

Or am I missing something?

Something like that, yes.

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

I wonder if those two cases are related. Do we need to also add an argument so that we can pass the executable to the next qemu via file descriptor? I.e. execvat becomes

f = openat()
execv(qemu, "-execfd", f)

and is_proc_myself uses execfd, which we already have open.

How does passing a fd from one process to another work?

Quoted 8 lines
Quoted 3 lines

As long as the fd is not marked O_CLOEXEC, it stays open in the new process. Providing the number via command-line, or whatever, is sufficient for the new process to know what is going on.

Err I guess I was thinking its a bit weird having an option that is only really applicable
if qemu is a child process. I.e the -execfd argument is not usable from commandline.

qemu-foo -execfd 3 3< /some/file

Or perhaps opened via other tooling.

Quoted 11 lines
Quoted 2 lines

I now realize this is necessary for the AT_EMPTY_PATH flag, where we only have the file descriptor.

We could also do something along the lines of:

```
fd = openat(dirfd, exe);
char new_exe[PATH_MAX];
char fd_path[PATH_MAX];
sprintf(fd_path, "/proc/self/fd/%d", fd);
readlink(fd_path, new_exe, PATH_MAX);

Quoted 2 lines

Reading the link doesn't always work.
Reading or passing the link means AT_SYMLINK_NOFOLLOW isn't honored.

Okay, fair enough, I will get started on adding -execfd

Quoted 3 lines

r~

#33 lore raw
Quoted 58 lines · Noah Goldstein
Quoted 55 lines · Richard Henderson
Quoted 28 lines · Noah Goldstein
Quoted 26 lines · Richard Henderson
Quoted 4 lines · Noah Goldstein
Quoted 1 line
Quoted 1 line

You still need to handle is_proc_myself, for the guest binary.

Would this by handled by basically do:

Quoted 8 lines
```
if (is_proc_myself(p, "exe")) {
          exe = exec_path;
          if (through_qemu)
              argp[argp_offset] = exec_path;
}
```

Or am I missing something?

Something like that, yes.

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

I wonder if those two cases are related. Do we need to also add an argument so that we can pass the executable to the next qemu via file descriptor? I.e. execvat becomes

f = openat()
execv(qemu, "-execfd", f)

and is_proc_myself uses execfd, which we already have open.

How does passing a fd from one process to another work?

Quoted 8 lines
Quoted 3 lines

As long as the fd is not marked O_CLOEXEC, it stays open in the new process. Providing the number via command-line, or whatever, is sufficient for the new process to know what is going on.

Err I guess I was thinking its a bit weird having an option that is only really applicable
if qemu is a child process. I.e the -execfd argument is not usable from commandline.

qemu-foo -execfd 3 3< /some/file

Or perhaps opened via other tooling.

Quoted 11 lines
Quoted 2 lines

I now realize this is necessary for the AT_EMPTY_PATH flag, where we only have the file descriptor.

We could also do something along the lines of:

```
fd = openat(dirfd, exe);
char new_exe[PATH_MAX];
char fd_path[PATH_MAX];
sprintf(fd_path, "/proc/self/fd/%d", fd);
readlink(fd_path, new_exe, PATH_MAX);

Quoted 4 lines
Quoted 2 lines

Reading the link doesn't always work.
Reading or passing the link means AT_SYMLINK_NOFOLLOW isn't honored.

Okay, fair enough, I will get started on adding -execfd

Question about impl regarding handling of -execfd with/without a program name.

1) -execfd + program name ie: qemu -execfd <some_fd> ls -a.
2) -execfd without program name i.e: qemu -execfd <some_fd> -a.

Do you want to allow both of these? If you want to allow (1), what should we use for argv[0]/exec_path. The program pass ("ls") or readlink(<some_fd>)?

#34 lore raw
Quoted 8 lines · Noah Goldstein

Question about impl regarding handling of -execfd with/without a program name.

1) -execfd + program name ie: qemu -execfd <some_fd> ls -a.
2) -execfd without program name i.e: qemu -execfd <some_fd> -a.

Do you want to allow both of these? If you want to allow (1), what should we use for argv[0]/exec_path. The program pass ("ls") or readlink(<some_fd>)?

The canonical response is, examine the kernel source.
We're not implementing this in a vacuum, you're replicating execveat(2).

I suspect the answer is (1), to be compared with

    syscall(__NR_execveat, some_fd, "", &["ls", "-a"], env, AT_EMPTY_PATH);

r~

#35 lore raw
Quoted 11 lines · Richard Henderson
Quoted 8 lines · Noah Goldstein

Question about impl regarding handling of -execfd with/without a program name.

1) -execfd + program name ie: qemu -execfd <some_fd> ls -a.
2) -execfd without program name i.e: qemu -execfd <some_fd> -a.

Do you want to allow both of these? If you want to allow (1), what should we use for argv[0]/exec_path. The program pass ("ls") or readlink(<some_fd>)?

Quoted 6 lines

The canonical response is, examine the kernel source.
We're not implementing this in a vacuum, you're replicating execveat(2).

I suspect the answer is (1), to be compared with

     syscall(__NR_execveat, some_fd, "", &["ls", "-a"], env, AT_EMPTY_PATH);

Err, I think the reference for '-execfd' is fexecve:
https://man7.org/linux/man-pages/man3/fexecve.3.html

Which doesn't take a path. So I guess we just interpret the "ls" as argv[0] but not
as "exec_path".

Quoted 3 lines

r~

#36 lore raw
Quoted 19 lines · Noah Goldstein
Quoted 16 lines · Richard Henderson
Quoted 8 lines · Noah Goldstein

Question about impl regarding handling of -execfd with/without a program name.

1) -execfd + program name ie: qemu -execfd <some_fd> ls -a.
2) -execfd without program name i.e: qemu -execfd <some_fd> -a.

Do you want to allow both of these? If you want to allow (1), what should we use for argv[0]/exec_path. The program pass ("ls") or readlink(<some_fd>)?

The canonical response is, examine the kernel source.
We're not implementing this in a vacuum, you're replicating execveat(2).

I suspect the answer is (1), to be compared with

Quoted 8 lines
Quoted 1 line
     syscall(__NR_execveat, some_fd, "", &["ls", "-a"], env, AT_EMPTY_PATH);

Err, I think the reference for '-execfd' is fexecve:
https://man7.org/linux/man-pages/man3/fexecve.3.html

Which doesn't take a path. So I guess we just interpret the "ls" as argv[0] but not
as "exec_path".

One more point, what should the behavior be if we have AT_EXECFD from binfmt-misc?

Quoted 3 lines
Quoted 3 lines

r~

#37 lore raw
Quoted 25 lines · Noah Goldstein
Quoted 24 lines · Noah Goldstein
Quoted 11 lines · Richard Henderson
Quoted 8 lines · Noah Goldstein

Question about impl regarding handling of -execfd with/without a program name.

1) -execfd + program name ie: qemu -execfd <some_fd> ls -a.
2) -execfd without program name i.e: qemu -execfd <some_fd> -a.

Do you want to allow both of these? If you want to allow (1), what should we use for argv[0]/exec_path. The program pass ("ls") or readlink(<some_fd>)?

Quoted 6 lines

The canonical response is, examine the kernel source.
We're not implementing this in a vacuum, you're replicating execveat(2).

I suspect the answer is (1), to be compared with

      syscall(__NR_execveat, some_fd, "", &["ls", "-a"], env, AT_EMPTY_PATH);

Err, I think the reference for '-execfd' is fexecve:
https://man7.org/linux/man-pages/man3/fexecve.3.html

Quoted 6 lines
Quoted 3 lines

Which doesn't take a path. So I guess we just interpret the "ls" as argv[0] but not
as "exec_path".

One more point, what should the behavior be if we have AT_EXECFD from binfmt-misc?

You mean precedence of AT_EXECFD vs the command-line option?

Arbitrary, since it would be a usage error to have both. You'd have to do something silly with the binfmt-misc rule for that to happen.

Perhaps

static int execfd = -1;
// option processing
// main

    if (execfd < 0) {
        errno = 0;
        execfd = qemu_getauxval(AT_EXECFD);
        if (errno != 0) {
            execfd = open(...);
        }
    }

just because that's a simple change to what's currently present.

r~

#38 lore raw
In reply to Noah Goldstein
Quoted 16 lines · Noah Goldstein
Quoted 14 lines · Richard Henderson
Quoted 8 lines · Noah Goldstein

Question about impl regarding handling of -execfd with/without a program name.

1) -execfd + program name ie: qemu -execfd <some_fd> ls -a.
2) -execfd without program name i.e: qemu -execfd <some_fd> -a.

Do you want to allow both of these? If you want to allow (1), what should we use for argv[0]/exec_path. The program pass ("ls") or readlink(<some_fd>)?

The canonical response is, examine the kernel source.
We're not implementing this in a vacuum, you're replicating execveat(2).

Quoted 6 lines
Quoted 3 lines

I suspect the answer is (1), to be compared with

      syscall(__NR_execveat, some_fd, "", &["ls", "-a"], env, AT_EMPTY_PATH);

Err, I think the reference for '-execfd' is fexecve:
https://man7.org/linux/man-pages/man3/fexecve.3.html

No, fexecve(3) is a glibc function which (nowadays) uses the execveat(2) syscall that we want to emulate.

Quoted 1 line

Which doesn't take a path...

... corresponding to the "" and AT_EMPTY_PATH above.

Quoted 1 line

So I guess we just interpret the "ls" as argv[0] but not as "exec_path".

But your conclusion is correct.

r~