From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.tuxteam.de (mail.tuxteam.de [5.199.139.25]) by sourceware.org (Postfix) with ESMTPS id 0D4EB39DC4D5 for ; Fri, 9 Apr 2021 17:03:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0D4EB39DC4D5 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tuxteam.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tomas@tuxteam.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=From:In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:Date; bh=2xLYfrACv9GO5wWLDevLuK8e+K3PpIXL2xfW7byeqX8=; b=cI9jANo2uK5dpT0hosAxgQXOg0HKVBLdq6avExt6+Dp1tvfE+XCEWZfHtxjXI3R/Y74POBYv40ZeG0EpvjDfnQEB317GlA/mGo7ssrNqUOK9BjcFFTbeAxUL5Iv3sI59QhfmuzsJk4v4c997Cyhklyk4nKI/SCNhv2UQHPNA1V7WAtAHt4c45BUW9dPxbk+ferHmzDwDGXR9wPGu9NGuwlOpMREhbm3qVBKwIw8lt3A1gSCLO5Cy6Js1bJhiNBgFGJqUOwZv7t3BrzO5zDE+860id92cIRduQn8vlP3uKS3qknLz52q61zVAFLneto2fHcPQLUwWTA1uccJV9LS+Uw==; Received: from tomas by mail.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1lUuXN-0003mN-Kt; Fri, 09 Apr 2021 19:03:17 +0200 Date: Fri, 9 Apr 2021 19:03:17 +0200 To: Peng Yu Cc: libc-help Subject: Re: When is it OK to pass NULLs to the 2nd and 3rd args of execve()? Message-ID: <20210409170317.GB13192@tuxteam.de> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="IiVenqGWf+H9Y6IX" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) From: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 09 Apr 2021 17:03:20 -0000 --IiVenqGWf+H9Y6IX Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Apr 09, 2021 at 11:53:39AM -0500, Peng Yu via Libc-help wrote: > Hi, >=20 > I am wondering when (all possible legitimate cases) it is OK to pass > NULLs to execve's 2nd and 3rd args. >=20 > #include > #include >=20 > int main(int argc, char *argv[]) { > if(execve(argv[1], NULL, NULL) =3D=3D -1) { > perror("execvp()"); > return 1; > } > return 0; > } >=20 >=20 > 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 =66rom 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=3D=3DNULL case the same as Linux. So... I wouldn't rely on it :-) Cheers - t --IiVenqGWf+H9Y6IX Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAmBwiNUACgkQBcgs9XrR2kbAKQCggkeaJBqChvUdlbnKlgcknxv+ zGIAn3Hqd8Ty4GCb4WOKySm7Zh+76XOA =gUGD -----END PGP SIGNATURE----- --IiVenqGWf+H9Y6IX--