public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Andreas Fink <finkandreas@web.de>
To: libc-help@sourceware.org
Subject: Re: Hooking execve for an LD_PRELOAD library
Date: Sun, 17 Jan 2021 12:28:03 +0100	[thread overview]
Message-ID: <0LgHf2-1lmY0O29NE-00ngvl@smtp.web.de> (raw)
In-Reply-To: <878s8srmnh.fsf@oldenburg.str.redhat.com>

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

  reply	other threads:[~2021-01-17 11:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-17  7:07 Andreas Fink
2021-01-17  8:30 ` Florian Weimer
2021-01-17 11:28   ` Andreas Fink [this message]
2021-01-18 10:39     ` Florian Weimer
2021-01-20 11:00       ` Andreas Fink

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0LgHf2-1lmY0O29NE-00ngvl@smtp.web.de \
    --to=finkandreas@web.de \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).