Skip to contentSkip to conversation

linux-user: remove useless variable

PATCH Applied Laurent Vivier 4 participants 8 messages Jul 14, 2019 – Aug 23, 2019 exec and binfmt

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 linux-user: remove useless variable

filename is only used to open the file if AT_EXECFD is not provided. But exec_path already contains the path of the file to open. Remove filename as it is only used in main.c whereas exec_path is also used in syscall.c.

Fixes d088d664f201 ("linux-user: identify running binary in /proc/self/exe")
---
 linux-user/main.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
linux-user/main.c +4 -6
index a59ae9439de1..ef8e8cb10eba 100644
@@ -48,7 +48,6 @@
 char *exec_path;
 
 int singlestep;
-static const char *filename;
 static const char *argv0;
 static int gdbstub_port;
 static envlist_t *envlist;
@@ -580,7 +579,6 @@ static int parse_args(int argc, char **argv)
         exit(EXIT_FAILURE);
     }
 
-    filename = argv[optind];
     exec_path = argv[optind];
 
     return optind;
@@ -651,9 +649,9 @@ int main(int argc, char **argv, char **envp)
 
     execfd = qemu_getauxval(AT_EXECFD);
     if (execfd == 0) {
-        execfd = open(filename, O_RDONLY);
+        execfd = open(exec_path, O_RDONLY);
         if (execfd < 0) {
-            printf("Error while loading %s: %s\n", filename, strerror(errno));
+            printf("Error while loading %s: %s\n", exec_path, strerror(errno));
             _exit(EXIT_FAILURE);
         }
     }
@@ -778,10 +776,10 @@ int main(int argc, char **argv, char **envp)
     cpu->opaque = ts;
     task_settid(ts);
 
-    ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
+    ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
         info, &bprm);
     if (ret != 0) {
-        printf("Error while loading %s: %s\n", filename, strerror(-ret));
+        printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
         _exit(EXIT_FAILURE);
     }
 
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/2 linux-user: manage binfmt-misc preserve-arg[0] flag

Add --preserve-arg0 in qemu-binfmt-conf.sh to configure the preserve-arg0 flag.

Now, if QEMU is started with -0 or QEMU_ARGV0 and an empty parameter argv[0] (the full pathname provided by binfmt-misc) is removed and replaced by argv[1] (the original argv[0] provided by binfmt-misc when 'P'/preserve-arg[0] is set)

For instance:

  $ sudo QEMU_ARGV0= chroot m68k-chroot sh -c 'echo $0'
  sh

without this patch:

  $ sudo chroot m68k-chroot sh -c 'echo $0'
  /usr/bin/sh
---
 linux-user/main.c           | 18 ++++++++++++++-
 scripts/qemu-binfmt-conf.sh | 44 +++++++++++++++++++++++--------------
 2 files changed, 44 insertions(+), 18 deletions(-)
linux-user/main.c +17 -1
index ef8e8cb10eba..eeaba4a7b914 100644
@@ -647,6 +647,9 @@ int main(int argc, char **argv, char **envp)
 
     init_qemu_uname_release();
 
+    /*
+     * Manage binfmt-misc open-binary flag
+     */
     execfd = qemu_getauxval(AT_EXECFD);
     if (execfd == 0) {
         execfd = open(exec_path, O_RDONLY);
@@ -656,6 +659,19 @@ int main(int argc, char **argv, char **envp)
         }
     }
 
+    /*
+     * Manage binfmt-misc preserve-arg[0] flag
+     *    argv[optind]     full path to the binary
+     *    argv[optind + 1] original argv[0]
+     */
+    if (optind + 1 < argc && argv0 != NULL && argv0[0] == 0) {
+        /*
+         * argv0 with an empty string will set argv[optind + 1]
+         * as target_argv[0]
+         */
+        optind++;
+    }
+
     if (cpu_model == NULL) {
         cpu_model = cpu_get_model(get_elf_eflags(execfd));
     }
@@ -760,7 +776,7 @@ int main(int argc, char **argv, char **envp)
      * argv[0] pointer with the given one.
      */
     i = 0;
-    if (argv0 != NULL) {
+    if (argv0 != NULL && argv0[0] != 0) {
         target_argv[i++] = strdup(argv0);
     }
     for (; i < target_argc; i++) {
scripts/qemu-binfmt-conf.sh +27 -17

73 lines not on this page · raw message

Signature · 1 line
2.21.0
#2 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag

Hi!

Quoted 12 lines · Laurent Vivier

Add --preserve-arg0 in qemu-binfmt-conf.sh to configure the preserve-arg0 flag.

Now, if QEMU is started with -0 or QEMU_ARGV0 and an empty parameter argv[0] (the full pathname provided by binfmt-misc) is removed and replaced by argv[1] (the original argv[0] provided by binfmt-misc when 'P'/preserve-arg[0] is set)

For instance:

Quoted 7 lines
 $ sudo QEMU_ARGV0= chroot m68k-chroot sh -c 'echo $0'
 sh

without this patch:

 $ sudo chroot m68k-chroot sh -c 'echo $0'
 /usr/bin/sh

As a regular user of qemu-user (we’re using qemu-user to run Debian’s buildds for m68k and sh4), I would like to add that the idea of having to pass additional environment variables to make qemu behave as expected, i.e. as the real hardware, is sub-optimal.

I would prefer that enabling the preserve flag with the qemu-binfmt.sh script would make qemu-user behave correctly.

If I understand correctly, the current design with the environment variable was chosen because my preferred approach would break compatibility in certain cases. However, I think that correct emulation is more important than compatibility to an old broken behavior and I would therefore be in favor to make the correct behavior default.

This will also be necessary when using qemu-user with Debian’s sbuild to “cross”-build packages with qemu-user. This particular bug was actually discovered while building Debian packages for m68k and sh4 using qemu-user.

Thanks,
Adrian

PS: I have disabled receiving messages for this list, so please keep me CC’ed.

#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag

Le 14/07/2019 à 18:19, John Paul Adrian Glaubitz a écrit :

Quoted 17 lines

Hi!

Quoted 15 lines · Laurent Vivier

Add --preserve-arg0 in qemu-binfmt-conf.sh to configure the preserve-arg0 flag.

Now, if QEMU is started with -0 or QEMU_ARGV0 and an empty parameter argv[0] (the full pathname provided by binfmt-misc) is removed and replaced by argv[1] (the original argv[0] provided by binfmt-misc when 'P'/preserve-arg[0] is set)

For instance:

 $ sudo QEMU_ARGV0= chroot m68k-chroot sh -c 'echo $0'
 sh
Quoted 8 lines
Quoted 4 lines

without this patch:

 $ sudo chroot m68k-chroot sh -c 'echo $0'
 /usr/bin/sh

As a regular user of qemu-user (we’re using qemu-user to run Debian’s buildds for m68k and sh4), I would like to add that the idea of having to pass additional environment variables to make qemu behave as expected, i.e. as the real hardware, is sub-optimal.

I would prefer that enabling the preserve flag with the qemu-binfmt.sh script would make qemu-user behave correctly.

QEMU is not able to detect if it has been started by binfmt_misc with the preserve-arg[0] enabled or not, so it can't adapt the args analysis to get the correct list.

Quoted 4 lines

If I understand correctly, the current design with the environment variable was chosen because my preferred approach would break compatibility in certain cases. However, I think that correct emulation is more important than compatibility to an old broken behavior and I would therefore be in favor to make the correct behavior default.

This will also be necessary when using qemu-user with Debian’s sbuild to “cross”-build packages with qemu-user. This particular bug was actually discovered while building Debian packages for m68k and sh4 using qemu-user.

The problem we have here is we don't know how qemu-user is used in the wild. In my knowledge you are the most involved user, but you're not the only one reporting problem via launchpad. Moreover, distros provide qemu-user statically linked and binfmt configuration files, so we can guess we have other users.

And I don't like to break existing things...

What I can propose:

1- modify this patch to add a configure option:

by default qemu will need the QEMU_ARGV0 but we will be able to
define at configure time it always runs with preserve-arg[0] flag enabled (something like "--enable-preserve-arg0")

[So debian will be able to provide qemu-user-static with this enabled by default if you're not afraid to break debian users environment]

2- try (again) to push in the kernel the binfmt_misc namespace that allows to have per chroot basis binfmt configuration

3- once 3 done, enable preserve-arg[0] by default

Thanks,
Laurent

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag

Hi!

Sorry for the late reply!

Quoted 4 lines · Laurent Vivier

And I don't like to break existing things...

What I can propose:

Quoted 8 lines

1- modify this patch to add a configure option:

by default qemu will need the QEMU_ARGV0 but we will be able to
define at configure time it always runs with preserve-arg[0] flag enabled (something like "--enable-preserve-arg0")

[So debian will be able to provide qemu-user-static with this enabled by default if you're not afraid to break debian users environment]

This sounds like a reasonable approach for the time being, I agree with that. I could just pass that option to configure whenever I build new static qemu binaries for the buildds and I can drop your customized patch locally.

Quoted 2 lines

2- try (again) to push in the kernel the binfmt_misc namespace that allows to have per chroot basis binfmt configuration

That would be cool, too. Has there been some discussion on this already?

Quoted 1 line

3- once 3 done, enable preserve-arg[0] by default

You mean once "2 done"?

Adrian

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag

In reply to Laurent Vivier
Quoted 3 lines · Laurent Vivier

QEMU is not able to detect if it has been started by binfmt_misc with the preserve-arg[0] enabled or not, so it can't adapt the args analysis to get the correct list.

If the kernel provided a more useful interface (for instance telling us what it was doing via the ELF auxv so we knew how to interpret the command line arguments) this would help. Of course you still have to wait until that kernel change actually gets into the hands of users...

thanks
-- PMM

Reviewed-by on v1 1/2
#6 lore raw
In reply to Laurent Vivier
Quoted 47 lines · Laurent Vivier

filename is only used to open the file if AT_EXECFD is not provided. But exec_path already contains the path of the file to open. Remove filename as it is only used in main.c whereas exec_path is also used in syscall.c.

Fixes d088d664f201 ("linux-user: identify running binary in /proc/self/exe")
---
 linux-user/main.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
linux-user/main.c +2 -4
index a59ae9439de1..ef8e8cb10eba 100644
@@ -48,7 +48,6 @@
 char *exec_path;
 
 int singlestep;
-static const char *filename;
 static const char *argv0;
 static int gdbstub_port;
 static envlist_t *envlist;
@@ -580,7 +579,6 @@ static int parse_args(int argc, char **argv)
         exit(EXIT_FAILURE);
     }
 
-    filename = argv[optind];
     exec_path = argv[optind];
 
     return optind;
@@ -651,9 +649,9 @@ int main(int argc, char **argv, char **envp)
 
     execfd = qemu_getauxval(AT_EXECFD);
     if (execfd == 0) {
-        execfd = open(filename, O_RDONLY);
+        execfd = open(exec_path, O_RDONLY);
         if (execfd < 0) {
-            printf("Error while loading %s: %s\n", filename, strerror(errno));
+            printf("Error while loading %s: %s\n", exec_path, strerror(errno));
             _exit(EXIT_FAILURE);
         }
     }
@@ -778,10 +776,10 @@ int main(int argc, char **argv, char **envp)
     cpu->opaque = ts;
     task_settid(ts);

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

Quoted 10 lines
linux-user/main.c +2 -2
 
-    ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
+    ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
         info, &bprm);
     if (ret != 0) {
-        printf("Error while loading %s: %s\n", filename, strerror(-ret));
+        printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
         _exit(EXIT_FAILURE);
     }
 
author Applied
#7 lore raw
In reply to Laurent Vivier

Le 14/07/2019 à 15:40, Laurent Vivier a écrit :

Quoted 47 lines

filename is only used to open the file if AT_EXECFD is not provided. But exec_path already contains the path of the file to open. Remove filename as it is only used in main.c whereas exec_path is also used in syscall.c.

Fixes d088d664f201 ("linux-user: identify running binary in /proc/self/exe")
---
 linux-user/main.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
linux-user/main.c +2 -4
index a59ae9439de1..ef8e8cb10eba 100644
@@ -48,7 +48,6 @@
 char *exec_path;
 
 int singlestep;
-static const char *filename;
 static const char *argv0;
 static int gdbstub_port;
 static envlist_t *envlist;
@@ -580,7 +579,6 @@ static int parse_args(int argc, char **argv)
         exit(EXIT_FAILURE);
     }
 
-    filename = argv[optind];
     exec_path = argv[optind];
 
     return optind;
@@ -651,9 +649,9 @@ int main(int argc, char **argv, char **envp)
 
     execfd = qemu_getauxval(AT_EXECFD);
     if (execfd == 0) {
-        execfd = open(filename, O_RDONLY);
+        execfd = open(exec_path, O_RDONLY);
         if (execfd < 0) {
-            printf("Error while loading %s: %s\n", filename, strerror(errno));
+            printf("Error while loading %s: %s\n", exec_path, strerror(errno));
             _exit(EXIT_FAILURE);
         }
     }
@@ -778,10 +776,10 @@ int main(int argc, char **argv, char **envp)
     cpu->opaque = ts;
     task_settid(ts);

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

Quoted 10 lines
linux-user/main.c +2 -2
 
-    ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
+    ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
         info, &bprm);
     if (ret != 0) {
-        printf("Error while loading %s: %s\n", filename, strerror(-ret));
+        printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
         _exit(EXIT_FAILURE);
     }
 

Applied to my linux-user branch.

Thanks,
Laurent