Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sun, 9 Jul 2017 11:23:23 +0200
From: u-uy74@...ey.se
To: musl@...ts.openwall.com
Subject: Re: Question about setting argv[0] when manually using
 dynamic linker

On Tue, Jul 04, 2017 at 04:58:11PM -0400, Rich Felker wrote:
> On Sun, Jul 02, 2017 at 07:36:19PM +0200, u-uy74@...ey.se wrote:
> > On Wed, May 17, 2017 at 12:24:28PM -0400, Rich Felker wrote:
> > > Perhaps adding an option like --argv0=foo would be
> > > appropriate.
> > 
> > Is this option being considered to introduce?

> Yes, adding it.

Thanks again, it makes some "unsolvable" cases work as needed.

OTOH when applying this in practice, I noticed that a slightly
different behaviour would be very handy: if the linker could
supply its own argv[0] as the one for the program to run.

This would fit nicely into the framework where we already set argv[0]
anyway, in the same way for both statically and dynamically linked
programs. (The only exception so far would be a hypothetical need to
pass on the value "ldd" as argv[0], then --argv0=ldd would save the day)

Otherwise we have to set the dynloader --argv0 argument per binary
which is of course possible but remarkably less convenient,
among others because this setting is dynloader-specific, while
otherwise our tools are libc- and dynamic vs static agnostic.

IOW as long as the loader itself does not rely on its argv[0]
too much, plainly passing on argv[0] is a very practical means to
handle the programs like busybox and gcc transparently.

What about always passing on the loader argv[0] unless --argv0 is present?
This will not matter for programs which do not analyze argv[0]
and will not make it worse for programs which do.

--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -1388,7 +1388,7 @@
                kernel_mapped_dso(&app);
        } else {
                int fd;
-               char *ldname = argv[0];
+               char *ldname = replace_argv0 = argv[0];
                size_t l = strlen(ldname);
                if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
                argv++;

or even

--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -1322,7 +1322,7 @@
        size_t aux[AUX_CNT], *auxv;
        size_t i;
        char *env_preload=0;
-       char *replace_argv0=0;
+       char *replace_argv0=argv[0];
        size_t vdso_base;
        int argc = *sp;
        char **argv = (void *)(sp+1);
@@ -1567,7 +1567,7 @@
        debug.state = 0;
        _dl_debug_state();

-       if (replace_argv0) argv[0] = replace_argv0;
+       argv[0] = replace_argv0;

        errno = 0;
 

Otherwise may be something like this:

--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -1409,6 +1409,7 @@
                                else if (*argv) env_preload = *argv++;
                        } else if (!memcmp(opt, "argv0", 5)) {
                                if (opt[5]=='=') replace_argv0 = opt+6;
+                               else if (opt[5]=='^') replace_argv0 = ldname;
                                else if (opt[5]) *argv = 0;
                                else if (*argv) replace_argv0 = *argv++;
                        } else {

to instead introduce an extra --argv0^ option ?

Regards,
Rune

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.