Hooking execve for an LD_PRELOAD library

Andreas Fink finkandreas@web.de
Sun Jan 17 11:28:03 GMT 2021


On Sun, 17 Jan 2021 09:30:10 +0100
Florian Weimer <fweimer@redhat.com> wrote:

> * Andreas Fink via Libc-help:
>
> > Now I would like the same for execvp to happen. Reading the man page of
> > execvp it is mentioned that exec-family functions are just
> > frontends to execve, so I replaced in my executable source code the
> > explicit call to execve with a call to execvp. I expected that this
> > would just work, as execvp would in turn call execve and this would be
> > caught by the hook, then logged and forwarded to the real
> > implementation. But to my surprise no such thing happened. execvp would
> > run successfully, but my hook would never be called.
> > Why is the hook not called, what did I miss?
>
> Most internal function calls are implemented as direct function call,
> and it is not possible to use symbol interposition to redirect them.
> An exception is the malloc family of functions:
>
>   Replacing malloc
>   <http://www.gnu.org/software/libc/manual/html_node/Replacing-malloc.html>
>
> If you want to modify the behavior of execve, you should consider a
> kernel-based mechanism.  This will likely make it easier to achieve
> correct behavior after vfork, too.
>
> Thanks,
> Florian


Ok, if I understood you correctly this is the expected behaviour. I was
afraid of it, but ok I can live with it.
Assuming I want to hook the whole exec-family, it seems easy to just
add hooks for the execv* functions while forwarding all arguments to
the corresponding glibc implementation. Forwarding the execl*
functions seems a bit more involved, as I would have to bend the
va_list to an array, or is there some way to forward the arguments to
the glibc function without unwrapping the va_list?

Kernel-based mechanism (in my case that's Linux) sounds also interesting
as it is one level lower I guess, but honestly speaking I have no idea
where to start to look at. Do you know of an example (not necesserily
execve, but any system call where this is done)? I guess glibc must do
this of course, but I could not directly find for example the real
implementation of execve. Only the stub one in posix/execve.c, but this
is certainly not the real implementation, as it returns always an error
;)

I think for my real use case I can get away with hooking all functions
on top of glibc, still interested to learn new things.

Best
Andreas


More information about the Libc-help mailing list