From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id C85B020551 for ; Sun, 4 Feb 2024 10:07:13 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1707037633; b=a7WG8fM2Y8lHaFMtcTYIAYB0j48xpIdYbBUW1wGOmt9MFqPPmLKrRLDzw0mn85SsMhv96Tgnsd U1sSl3OMhFRAgWyj9E7J5OKTAcWucokzAH1C2BqZdyYUbo1HSEk2tpqt3ATw7ZGWsbeseUXyu0 /UANZ8kt3L6KYRTbtOK2qJZoeWZ4RlwO7csR6cnXGGrVJL4ORI38KdI2MN6xtleIepLgl0AZUF xWS4ZlZ/g8Toffqnha7DuC9fiWql/hngrjwcmb3ognbbVH0TvjpMw1hZ3JoH3oKWQpC0Eaie3/ 98ixScxPCMrZe6MwPjK5+BC3LiB+nouqOGYmV/rxYFyDKQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay4-d.mail.gandi.net) smtp.remote-ip=217.70.183.196; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1707037633; bh=LTIGEfeOIRhyB8i2fGWdeILwch1HL51fVZ/x0nCr9po=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:MIME-Version:Message-ID:Subject:To:From:Date: DKIM-Signature; b=RvarOP23tBjfSQp/zoLCBwQXrse/MBTWkoj7phGwzFdjYdg2AyBEqeHqECPi8fC+IBHK7J82HG sG9SV0/gTzTtp5ErqlZCT/75aMtX3QD2Vpab4LCsva7wdli3wx1VBf+48wrHGe+363suxzD4qr 2Qj9s66e56I2BDAR6QBYpIdEJAGoXu3AZem/TuwnQAqqW/egFNUJqWAeu6O+jY1r5+SFAKEJIG FrEZiP5GOxfUBtYB7DSRRnb7te8b+pozSw/rVnRaAUMhrTilZFDRuUShmJrzy6sjiMtVrvpvMX bpUQtYz92yFXn1ULC6jw0MUfY4l4GhHN2m06KN/hZGk3EQ==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:MIME-Version:Message-ID: Subject:To:From:Date:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References; bh=KTYwkRJnM+HD4Lu4maIakV3dKIhB9P+qMSOTyMHW5RY=; b=ObaIUDo6/wxdfJvtD5n0lZQgXx ODVq4tXz4MkWE7pfKn70qSfdpw23LkcWSds3JFLHbSPQne72p4VSDUgj/Ad3mYoUuVs6jwPJBiwYI 5MFbmnpDNcNuVz4o/LO41P5tq0l/HEz/9GcxTM/2OEsVGuk/CaNZZg+K4JkJ7l2dRDejTKx5ol4iH WIgdsbWKI59fD3u8jxTh6xgaVd/ENYSPEKInkjq6s87WW73Ra0lcXmZKSbmVah0OQN05peLBLyfmm HaxqMqBbq44Qm3Ps3JBWrEb/5u1kxTRVDC+Y6hW/NshbCtqX4NB33zxfSP+CxhLrGWMttPVB9k4mH FOyOaMNg==; Received: by zero.zsh.org with local id 1rWYT1-0004xZ-1v; Sun, 04 Feb 2024 09:07:11 +0000 Authentication-Results: zsh.org; iprev=pass (relay4-d.mail.gandi.net) smtp.remote-ip=217.70.183.196; dmarc=none header.from=chazelas.org; arc=none Received: from relay4-d.mail.gandi.net ([217.70.183.196]:55039) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1rWYSS-0004dX-35; Sun, 04 Feb 2024 09:06:37 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 2ECB1E0005 for ; Sun, 4 Feb 2024 09:06:34 +0000 (UTC) Date: Sun, 4 Feb 2024 09:06:27 +0000 From: Stephane Chazelas To: Zsh hackers list Subject: [PATCH][nitpick-BUG] shebang less scripts and paths starting with -/+ Message-ID: <20240204090627.6lpx2ocx36bw5nco@chazelas.org> Mail-Followup-To: Zsh hackers list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-GND-Sasl: stephane@chazelas.org X-Seq: 52515 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: Hello, $ echo echo ++ > ++ $ chmod +x ++ $ strace -qqfe /exec zsh -c 'PATH=; ++' execve("/usr/bin/zsh", ["zsh", "-c", "PATH=; ++"], 0x7fffadb8e870 /* 55 vars */) = 0 execve("++", ["++"], 0x7ffc57750de8 /* 55 vars */) = -1 ENOEXEC (Exec format error) execve("/bin/sh", ["sh", "++"], 0x7ffc57750de8 /* 55 vars */) = 0 sh: 0: Illegal option -+ The path of the script was passed as an option to sh rather than an operand. The patch below changes it to: $ strace -qqfe /exec Src/zsh -c 'PATH=; ++' execve("Src/zsh", ["Src/zsh", "-c", "PATH=; ++"], 0x7ffd2a923100 /* 55 vars */) = 0 execve("++", ["++"], 0x7ffd8d292448 /* 55 vars */) = -1 ENOEXEC (Exec format error) execve("/bin/sh", ["sh", "-", "++"], 0x7ffd8d292448 /* 55 vars */) = 0 ++ yash is the only other shell I've found so far that guards against it. It does add the "-" unconditionally. Here I add it only if the path starts with - or + which would be extremely rare to avoid getting closer to the E2BIG limit in the normal case. Also note (related though this patch doesn't do anything about it other than adding the "-"): $ strace -qqfe /exec zsh -c 'PATH=.; ++' execve("/usr/bin/zsh", ["zsh", "-c", "PATH=.; ++"], 0x7ffcf4504b50 /* 55 vars */) = 0 execve("++", ["++"], 0x7ffc2a099678 /* 55 vars */) = -1 ENOEXEC (Exec format error) execve("/bin/sh", ["sh", "++"], 0x7ffc2a099678 /* 55 vars */) = 0 sh: 0: Illegal option -+ Is that intentional? I'd have expected execve("./++", ["++"]...) as $path contains the "." relative path there. $ strace -qqfe /exec zsh -c 'PATH=./.; ++' execve("/usr/bin/zsh", ["zsh", "-c", "PATH=./.; ++"], 0x7ffddeba1840 /* 55 vars */) = 0 execve("././++", ["++"], 0x7ffc4dfe9ae8 /* 55 vars */) = -1 ENOEXEC (Exec format error) execve("/bin/sh", ["sh", "././++"], 0x7ffc4dfe9ae8 /* 55 vars */) = 0 ++ There are a number of scripts in the source distribution whose shebang is missing the "-", and which this patch doesn't address. See also https://unix.stackexchange.com/questions/351729/why-the-in-the-bin-sh-shebang diff --git a/Src/exec.c b/Src/exec.c index 7d8135266..294587802 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -612,9 +612,22 @@ zexecve(char *pth, char **argv, char **newenvp) } } if (!isbinary) { - argv[-1] = "sh"; + char** args = argv; + if (argv[0][0] == '-' || argv[0][0] == '+') { + /* + * guard against +foo or -bar script paths being + * taken as options. POSIX says the script path + * must be passed as an *operand*. "--" would also + * make sure the next argument is treated as an + * operand with POSIX compliant sh implementations + * but "-" is more portable (to the Bourne shell in + * particular) and shorter. + */ + *--args = "-"; + } + *--args = "sh"; winch_unblock(); - execve("/bin/sh", argv - 1, newenvp); + execve("/bin/sh", args, newenvp); } } } else