Message ID | 4a157fa95462503bd1b6c3218644a0334ae52325.1649854695.git.szabolcs.nagy@arm.com |
---|---|
State | New |
Headers | show |
Series | Args adjustment with ./ld.so exe [BZ #23293] | expand |
* Szabolcs Nagy via Libc-alpha: > When an executable is invoked as > > ./ld.so [ld.so-args] ./exe [exe-args] > > then the argv is adujusted in ld.so before calling the entry point of > the executable so ld.so args are not visible to it. On most targets > this requires moving argv, env and auxv on the stack to ensure correct > stack alignment at the entry point. This had several issues: > > - The code for this adjustment on the stack is written in asm as part > of the target specific ld.so _start code which is hard to maintain. > > - The adjustment is done after _dl_start returns, where it's too late > to update GLRO(dl_auxv), as it is already readonly, so it points to > memory that was clobbered by the adjustment. This is bug 23293. > > - _environ is also wrong in ld.so after the adjustment, but it is > likely not used after _dl_start returns so this is not user visible. > > - _dl_argv was updated, but for this it was moved out of relro, which > changes security properties across targets unnecessarily. > > This patch introduces a generic _dl_start_args_adjust function that > handles the argument adjustments after ld.so processed its own args > and before relro protection is applied. It sets _dl_skip_args to 0 so > the existing adjustment in asm is not invoked. Each target has to > opt-in to use this new adjustment since some targets don't need it. > Once all targets are updated, _dl_argv declaration can be simplified. > > A new _dl_start_argptr was introduced because the original sp is not > passed to dl_main which now has to do the adjustments. > > -- > v2: > - use p != NULL, and a_type != AT_NULL > - remove the confusing paragraph from the commit message. Looks good to me. Reviewed-by: Florian Weimer <fweimer@redhat.com> Thanks, Florian
The 05/03/2022 13:53, Florian Weimer via Libc-alpha wrote: > * Szabolcs Nagy via Libc-alpha: > > > When an executable is invoked as > > > > ./ld.so [ld.so-args] ./exe [exe-args] > > > > then the argv is adujusted in ld.so before calling the entry point of > > the executable so ld.so args are not visible to it. On most targets > > this requires moving argv, env and auxv on the stack to ensure correct > > stack alignment at the entry point. This had several issues: > > > > - The code for this adjustment on the stack is written in asm as part > > of the target specific ld.so _start code which is hard to maintain. > > > > - The adjustment is done after _dl_start returns, where it's too late > > to update GLRO(dl_auxv), as it is already readonly, so it points to > > memory that was clobbered by the adjustment. This is bug 23293. > > > > - _environ is also wrong in ld.so after the adjustment, but it is > > likely not used after _dl_start returns so this is not user visible. > > > > - _dl_argv was updated, but for this it was moved out of relro, which > > changes security properties across targets unnecessarily. > > > > This patch introduces a generic _dl_start_args_adjust function that > > handles the argument adjustments after ld.so processed its own args > > and before relro protection is applied. It sets _dl_skip_args to 0 so > > the existing adjustment in asm is not invoked. Each target has to > > opt-in to use this new adjustment since some targets don't need it. > > Once all targets are updated, _dl_argv declaration can be simplified. > > > > A new _dl_start_argptr was introduced because the original sp is not > > passed to dl_main which now has to do the adjustments. > > > > -- > > v2: > > - use p != NULL, and a_type != AT_NULL > > - remove the confusing paragraph from the commit message. > > Looks good to me. > > Reviewed-by: Florian Weimer <fweimer@redhat.com> thanks, meanwhile i started working on v4. that always does the new adjustment (even on targets that don't require it) since ld.so invocation does not have to be optimal but it's better to have consistent behaviour. then the bug is fixed everywhere (not just opt-in). is that an acceptable approach?
* Szabolcs Nagy: > The 05/03/2022 13:53, Florian Weimer via Libc-alpha wrote: >> * Szabolcs Nagy via Libc-alpha: >> >> > When an executable is invoked as >> > >> > ./ld.so [ld.so-args] ./exe [exe-args] >> > >> > then the argv is adujusted in ld.so before calling the entry point of >> > the executable so ld.so args are not visible to it. On most targets >> > this requires moving argv, env and auxv on the stack to ensure correct >> > stack alignment at the entry point. This had several issues: >> > >> > - The code for this adjustment on the stack is written in asm as part >> > of the target specific ld.so _start code which is hard to maintain. >> > >> > - The adjustment is done after _dl_start returns, where it's too late >> > to update GLRO(dl_auxv), as it is already readonly, so it points to >> > memory that was clobbered by the adjustment. This is bug 23293. >> > >> > - _environ is also wrong in ld.so after the adjustment, but it is >> > likely not used after _dl_start returns so this is not user visible. >> > >> > - _dl_argv was updated, but for this it was moved out of relro, which >> > changes security properties across targets unnecessarily. >> > >> > This patch introduces a generic _dl_start_args_adjust function that >> > handles the argument adjustments after ld.so processed its own args >> > and before relro protection is applied. It sets _dl_skip_args to 0 so >> > the existing adjustment in asm is not invoked. Each target has to >> > opt-in to use this new adjustment since some targets don't need it. >> > Once all targets are updated, _dl_argv declaration can be simplified. >> > >> > A new _dl_start_argptr was introduced because the original sp is not >> > passed to dl_main which now has to do the adjustments. >> > >> > -- >> > v2: >> > - use p != NULL, and a_type != AT_NULL >> > - remove the confusing paragraph from the commit message. >> >> Looks good to me. >> >> Reviewed-by: Florian Weimer <fweimer@redhat.com> > > thanks, meanwhile i started working on v4. > > that always does the new adjustment (even on targets that don't > require it) since ld.so invocation does not have to be optimal > but it's better to have consistent behaviour. > > then the bug is fixed everywhere (not just opt-in). > > is that an acceptable approach? Sorry, I would have to see the patch. But I generally appreciate consistency. Thanks, Florian
diff --git a/elf/rtld.c b/elf/rtld.c index 19e328f89e..bc4d59f5d6 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1311,6 +1311,60 @@ rtld_setup_main_map (struct link_map *main_map) return has_interp; } +#ifdef DL_NEED_START_ARGS_ADJUST +static void +_dl_start_args_adjust (void) +{ + void **sp; + void **p; + long argc; + char **argv; + ElfW(auxv_t) *auxv; + + if (_dl_skip_args == 0) + return; + + sp = _dl_start_argptr; + + /* Adjust argc on stack. */ + argc = (long) sp[0] - _dl_skip_args; + sp[0] = (void *) argc; + + argv = (char **) (sp + 1); /* Necessary aliasing violation. */ + p = sp + _dl_skip_args; + /* Shuffle argv down. */ + do + *++sp = *++p; + while (*p != NULL); + + /* Shuffle envp down. */ + do + *++sp = *++p; + while (*p != NULL); + + auxv = (ElfW(auxv_t) *) (sp + 1); /* Necessary aliasing violation. */ + /* Shuffle auxv down. */ + void *a, *b; /* Use a pair of pointers for an auxv entry. */ + unsigned long a_type; + do + { + a_type = ((ElfW(auxv_t) *) (p + 1))->a_type; + a = *++p; + b = *++p; + *++sp = a; + *++sp = b; + } + while (a_type != AT_NULL); + + /* Update globals in rtld. */ + _dl_argv = argv; + _environ = argv + argc + 1; + GLRO(dl_auxv) = auxv; + /* No longer need to skip args. */ + _dl_skip_args = 0; +} +#endif + static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum, @@ -1615,6 +1669,10 @@ dl_main (const ElfW(Phdr) *phdr, /* Set the argv[0] string now that we've processed the executable. */ if (argv0 != NULL) _dl_argv[0] = argv0; +#ifdef DL_NEED_START_ARGS_ADJUST + /* Adjust arguments for the application entry point. */ + _dl_start_args_adjust (); +#endif } else { diff --git a/sysdeps/aarch64/dl-sysdep.h b/sysdeps/aarch64/dl-sysdep.h index 667786671c..1df4c2c528 100644 --- a/sysdeps/aarch64/dl-sysdep.h +++ b/sysdeps/aarch64/dl-sysdep.h @@ -20,6 +20,6 @@ /* _dl_argv cannot be attribute_relro, because _dl_start_user might write into it after _dl_start returns. */ -#define DL_ARGV_NOT_RELRO 1 +#define DL_NEED_START_ARGS_ADJUST 1 #define DL_EXTERN_PROTECTED_DATA diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 29f005499b..f322d36570 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -785,6 +785,9 @@ extern unsigned int _dl_skip_args attribute_hidden attribute_relro # endif ; +# ifdef DL_NEED_START_ARGS_ADJUST +extern void **_dl_start_argptr attribute_hidden attribute_relro; +# endif #endif #define rtld_progname _dl_argv[0] diff --git a/sysdeps/unix/sysv/linux/dl-sysdep.c b/sysdeps/unix/sysv/linux/dl-sysdep.c index c90f109b11..66f003e2a3 100644 --- a/sysdeps/unix/sysv/linux/dl-sysdep.c +++ b/sysdeps/unix/sysv/linux/dl-sysdep.c @@ -58,6 +58,12 @@ void *__libc_stack_end attribute_relro = NULL; rtld_hidden_data_def(__libc_stack_end) void *_dl_random attribute_relro = NULL; +#ifdef DL_NEED_START_ARGS_ADJUST +/* Original sp at ELF entry, used when rtld is executed explicitly + and needs to adjust arg components for the actual application. */ +void **_dl_start_argptr attribute_hidden attribute_relro = NULL; +#endif + #ifndef DL_STACK_END # define DL_STACK_END(cookie) ((void *) (cookie)) #endif @@ -114,6 +120,10 @@ _dl_sysdep_start (void **start_argptr, __brk (0); /* Initialize the break. */ +#ifdef DL_NEED_START_ARGS_ADJUST + _dl_start_argptr = start_argptr; +#endif + #ifdef DL_PLATFORM_INIT DL_PLATFORM_INIT; #endif