From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by sourceware.org (Postfix) with ESMTP id B3F1B3858038 for ; Fri, 9 Apr 2021 22:06:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B3F1B3858038 Received: from vapier (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 286FD335D06; Fri, 9 Apr 2021 22:06:38 +0000 (UTC) Date: Fri, 9 Apr 2021 18:06:37 -0400 From: Mike Frysinger 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: Mail-Followup-To: Peng Yu , libc-help References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no 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 22:06:40 -0000 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 > #include > > 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