public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* When is it OK to pass NULLs to the 2nd and 3rd args of execve()?
@ 2021-04-09 16:53 Peng Yu
  2021-04-09 17:03 ` tomas
  2021-04-09 22:06 ` Mike Frysinger
  0 siblings, 2 replies; 5+ messages in thread
From: Peng Yu @ 2021-04-09 16:53 UTC (permalink / raw)
  To: libc-help

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?

-- 
Regards,
Peng

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: When is it OK to pass NULLs to the 2nd and 3rd args of execve()?
  2021-04-09 16:53 When is it OK to pass NULLs to the 2nd and 3rd args of execve()? Peng Yu
@ 2021-04-09 17:03 ` tomas
  2021-04-09 17:20   ` Peng Yu
  2021-04-09 22:06 ` Mike Frysinger
  1 sibling, 1 reply; 5+ messages in thread
From: tomas @ 2021-04-09 17:03 UTC (permalink / raw)
  To: Peng Yu; +Cc: libc-help

[-- Attachment #1: Type: text/plain, Size: 1280 bytes --]

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.

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

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: When is it OK to pass NULLs to the 2nd and 3rd args of execve()?
  2021-04-09 17:03 ` tomas
@ 2021-04-09 17:20   ` Peng Yu
  2021-04-09 17:32     ` tomas
  0 siblings, 1 reply; 5+ messages in thread
From: Peng Yu @ 2021-04-09 17:20 UTC (permalink / raw)
  To: tomas; +Cc: libc-help

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: When is it OK to pass NULLs to the 2nd and 3rd args of execve()?
  2021-04-09 17:20   ` Peng Yu
@ 2021-04-09 17:32     ` tomas
  0 siblings, 0 replies; 5+ messages in thread
From: tomas @ 2021-04-09 17:32 UTC (permalink / raw)
  To: Peng Yu; +Cc: libc-help

[-- Attachment #1: Type: text/plain, Size: 854 bytes --]

On Fri, Apr 09, 2021 at 12:20:50PM -0500, Peng Yu wrote:

[man page]

> 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.

I don't know. I read it as "anything can happen, so don't do it",
for any of argv and envp or both.

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

I don't know :-)

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

Perhaps. It seems to be the kernel's realm and libc just passes things
around, so you'd have to look into the respective kernel documentation.

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: When is it OK to pass NULLs to the 2nd and 3rd args of execve()?
  2021-04-09 16:53 When is it OK to pass NULLs to the 2nd and 3rd args of execve()? Peng Yu
  2021-04-09 17:03 ` tomas
@ 2021-04-09 22:06 ` Mike Frysinger
  1 sibling, 0 replies; 5+ messages in thread
From: Mike Frysinger @ 2021-04-09 22:06 UTC (permalink / raw)
  To: Peng Yu; +Cc: libc-help

On 09 Apr 2021 11:53, Peng Yu via Libc-help wrote:
> 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?

never do this.  POSIX doesn't allow it, and no programs account for it.
if you were try to report it as such, you'd (rightly) be told no.
i recall coreutils for example segfaults when it tries to deref argv[0].

https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
-mike

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-04-09 22:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-09 16:53 When is it OK to pass NULLs to the 2nd and 3rd args of execve()? Peng Yu
2021-04-09 17:03 ` tomas
2021-04-09 17:20   ` Peng Yu
2021-04-09 17:32     ` tomas
2021-04-09 22:06 ` Mike Frysinger

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).