public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Peng Yu <pengyu.ut@gmail.com>
To: tomas@tuxteam.de
Cc: libc-help <libc-help@sourceware.org>
Subject: Re: When is it OK to pass NULLs to the 2nd and 3rd args of execve()?
Date: Fri, 9 Apr 2021 12:20:50 -0500	[thread overview]
Message-ID: <CABrM6wkFgO6wBFxVoh2xuWJ7rrBrh=tZrn8irE53ztyg3RCGAQ@mail.gmail.com> (raw)
In-Reply-To: <20210409170317.GB13192@tuxteam.de>

On Fri, Apr 9, 2021 at 12:03 PM <tomas@tuxteam.de> wrote:
>
> On Fri, Apr 09, 2021 at 11:53:39AM -0500, Peng Yu via Libc-help wrote:
> > Hi,
> >
> > I am wondering when (all possible legitimate cases) it is OK to pass
> > NULLs to execve's 2nd and 3rd args.
> >
> > #include <unistd.h>
> > #include <stdio.h>
> >
> > int main(int argc, char *argv[]) {
> >       if(execve(argv[1], NULL, NULL) == -1) {
> >               perror("execvp()");
> >               return 1;
> >       }
> >       return 0;
> > }
> >
> >
> > I tried the above program. It works when it is called with `/bin/sh`.
> > My guess is that when the program being exec'ed don't use its args and
> > envs, then then 2nd and 3rd args of execve() can be NULLs. Could
> > anybody correct me if I am wrong?
>
> I wouldn't count on that. It seems to depend on the operating
> system. I quote the Linux Programmer's Manual execve(2) man page
> from my box:
>
>   On Linux, argv and envp can be specified as NULL.  In both
>   cases, this has the same effect as specifying the argument
>   as a pointer to a list containing a single null pointer.
>   **Do not take  advantage of this nonstandard and nonportable
>   misfeature!**  On many other UNIX systems, specifying argv
>   as NULL will result in an error (EFAULT).  Some other UNIX
>   systems treat the envp==NULL case the same as Linux.

The above paragraph also reads awkwardly. Why it first mentions `On
many other ... argv as NULL ... Some other UNIX ... envp==NULL`? It
seems to be contrasting two cases. But then it should be either argv
or envp but not mentioning both. So it is confusing.

What systems have glibc been ported to? On all these systems, are
there specifics on what systems argv=NULL causes EFAULT?

Does the above paragraph mean envp=NULL can also cause EFAULT? I don't
get what error could envp=NULL cause?

> So... I wouldn't rely on it :-)

Thanks.
--
Regards,
Peng

  reply	other threads:[~2021-04-09 17:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-09 16:53 Peng Yu
2021-04-09 17:03 ` tomas
2021-04-09 17:20   ` Peng Yu [this message]
2021-04-09 17:32     ` tomas
2021-04-09 22:06 ` Mike Frysinger

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='CABrM6wkFgO6wBFxVoh2xuWJ7rrBrh=tZrn8irE53ztyg3RCGAQ@mail.gmail.com' \
    --to=pengyu.ut@gmail.com \
    --cc=libc-help@sourceware.org \
    --cc=tomas@tuxteam.de \
    /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).