From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by sourceware.org (Postfix) with ESMTPS id 0E043388A43B for ; Fri, 9 Apr 2021 17:21:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0E043388A43B Received: by mail-io1-xd29.google.com with SMTP id y4so6695693ioy.3 for ; Fri, 09 Apr 2021 10:21:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vsG1JzatHWs1+QjrDlR7qEnfPPlQNBLp0VjlD90uD9U=; b=aSz7LF+6xNS5f1UoPMg9oCA2s7qahD47PXEumhZoi6VP7kj20DLDdJXexAlbnrx+y+ cbJdlGhJNsY3wVD36k4MqCxZctKSOz8eZpvPo8FATh5/9ukuxjwVP3YtAxy3FcM98dRX e1G7XcBho+UzKVgPZIyqf+2TekBkZDdH3euH3Nbq6GoVWE//8x0dcbaNcHsswqAG+iPK /Do+4O2MElkdDNVyqiP4eEtUf1JgcGtOAdAJKyPQMQjMN6ZSJD0Zq2V/vGVUSt6SWFCd xEyFHDKdAgGOza8FSWzFP/ja1pIaU1OXebAJ9x1d/iTZJd5w4bmkQAT4FeySJZa5muta 1b3A== X-Gm-Message-State: AOAM530w5yor0QMZcmFixBjA/lnArNHpvNAE9uN75Py1hPpozfDvp8aH ZKRwdp4S03OpTBclApXcCdGX//prY2PWQH+craEkXtwGk6STkg== X-Google-Smtp-Source: ABdhPJyVGRSe3GLgazHVdsfjm2wIdl6J2nCZzpFCSJa2opG91o+2a2mntIGxFWjT51Wc8vPVxufaKGu8TMkdiXuki7k= X-Received: by 2002:a02:cbaf:: with SMTP id v15mr15236682jap.118.1617988861561; Fri, 09 Apr 2021 10:21:01 -0700 (PDT) MIME-Version: 1.0 References: <20210409170317.GB13192@tuxteam.de> In-Reply-To: <20210409170317.GB13192@tuxteam.de> From: Peng Yu Date: Fri, 9 Apr 2021 12:20:50 -0500 Message-ID: Subject: Re: When is it OK to pass NULLs to the 2nd and 3rd args of execve()? To: tomas@tuxteam.de Cc: libc-help Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, 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:21:03 -0000 On Fri, Apr 9, 2021 at 12:03 PM 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 > > #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? > > 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