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=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,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 09EE825496 for ; Sat, 10 Feb 2024 17:33:18 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1707582798; b=owtjwiZJ4f6J8UZSiEzCLkAmCkIT4tOpWKOnAwDZzYBzMZLx/0I9NhGWi+XwhwTtvSWzuQQeTY Yz+SEmgjOejibliiTyHT+TsA1NRDJIa1W9MzpoWQjOY13hM83YAnqMjG5DXlBc4pSF/RGcHO9l sgUkncOBGLoyaVqy69QSmPZwettKQrxnyCoSaHDN9diEe/+Yil2DQmzpvmF+/1+kg3dmKb36Zf ZWECDHxUbHg2kw2VOa3f2ZMplxcsTJX7Vnn2MxDfg+9JAFMadFcCIwW0a3kEmOqMILrUEgqIbZ 8/S5QcXjqBCnw3qJLmbkcHTzSQAN/4cGL8Ki09+vYDDrQA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay6-d.mail.gandi.net) smtp.remote-ip=217.70.183.198; 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=1707582798; bh=gkEjGpHkJizGX/w7aRRwAna2gioCOmeBgo1b3TZYrT8=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:To:From:Date:DKIM-Signature; b=e1tWcB+ycjbfdz995fTI/ywYbxP2ENp06WKkFTEj2/RaQEQGLSCM6z+EiUUgsNrJi453xob+eT NO9yHokMqA3rdPRb0YwcaN4Bs4W0EzVnvsdVLaZ3H5d7D/4EoswmeH/SJj5Gg0HyN42qmq/46A ZH2Ysn8ChCNe3L8lz+oVesytrMPSKA002/8Ef34GN9I523OB+R5Dn0RG2M6OWsEDGhUi/0Ahz/ tWmw9s4/+aM1BbYz3d5L6LMS4znwjAzI8IAucN03joWZ3033VpF7mqn6mxWkx2bu/QscFUv5jt T9eY+WGr6a5lk+A8wB0CuiOumoZ3gILrbwSjGqAnV+LKUw==; 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:In-Reply-To:Content-Type:MIME-Version :References: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; bh=aMHFZSdQ3fE//UJ/0QPIWx5k3INb6prdPQJR50fJZp8=; b=TeZeXl23ZQWVeIVvA9nkEu9zan eeN+ZVkoYN7gpPVTOuuOaLpV4DLpzFoUvrnfbWl70SNId8s/noIf5SaLWyH+ipaF2abokGIiLUH7C HgdPZ8naTx9IHXHO6IZWJvpODDxrxIvTCiIu0cPSzYqccFm/i9h1pRbMuTGYaXBrXJYOJyGZE3WyY 2ftIhpmL1w+xqn8Ij559ynst7d8eWfggUd62++SK8CgpF+2r+ZKyXbG54eS4ApFEXcbw6qbt27rb8 YMhOMs9nbmjlNpxxlvVW6Z9UQaQyN/yatrWVDoFM+jUSxDz9p3XOTy8+Kkkmjv0feGR/8L+A0NxiE TYk7XLlg==; Received: by zero.zsh.org with local id 1rYqI1-0003CL-3g; Sat, 10 Feb 2024 16:33:17 +0000 Authentication-Results: zsh.org; iprev=pass (relay6-d.mail.gandi.net) smtp.remote-ip=217.70.183.198; dmarc=none header.from=chazelas.org; arc=none Received: from relay6-d.mail.gandi.net ([217.70.183.198]:44431) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1rYqHh-0002tW-Oc; Sat, 10 Feb 2024 16:32:58 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id E092AC0003 for ; Sat, 10 Feb 2024 16:32:56 +0000 (UTC) Date: Sat, 10 Feb 2024 16:32:56 +0000 From: Stephane Chazelas To: Zsh hackers list Subject: Re: [PATCH][nitpick-BUG] shebang less scripts and paths starting with -/+ Message-ID: <20240210163256.lz7xfkmpw4kkzdk5@chazelas.org> Mail-Followup-To: Zsh hackers list References: <20240204090627.6lpx2ocx36bw5nco@chazelas.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240204090627.6lpx2ocx36bw5nco@chazelas.org> X-GND-Sasl: stephane@chazelas.org X-Seq: 52527 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: 2024-02-04 09:06:27 +0000, Stephane Chazelas: [...] > $ 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 > ++ [...] Sorry, forgot to add a test case: diff --git a/Src/exec.c b/Src/exec.c index 7d8135266..9c8bbb458 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 diff --git a/Test/A05execution.ztst b/Test/A05execution.ztst index bcadc6d56..07a24f9c8 100644 --- a/Test/A05execution.ztst +++ b/Test/A05execution.ztst @@ -2,7 +2,7 @@ storepath=($path) - mkdir command.tmp command.tmp/dir1 command.tmp/dir2 + mkdir command.tmp command.tmp/dir{1,2} command.tmp/{+,-}dir cd command.tmp @@ -21,7 +21,10 @@ print '#!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxnyyy' >tstcmd-interp-too-long print "#!${sh}\necho should not execute; exit 1" >xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn - chmod 755 tstcmd dir1/tstcmd dir2/tstcmd + print 'echo no shebang in -dir' > -dir/tstcmd + print 'echo no shebang in +dir' > +dir/tstcmd + + chmod 755 tstcmd dir{1,2}/tstcmd ./{-,+}dir/tstcmd chmod 755 tstcmd-slashless tstcmd-arg tstcmd-interp-too-long chmod 755 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn @@ -422,3 +425,12 @@ F:anonymous function, and a descriptor leak when backgrounding a pipeline (exit 4); repeat 0 do done 0:'repeat 0' resets lastval + -dir/tstcmd + +dir/tstcmd + PATH=-dir tstcmd + PATH=+dir tstcmd +0:shebang-less scripts are to be run by sh even when their file paths start with - or + (workers/52515) +>no shebang in -dir +>no shebang in +dir +>no shebang in -dir +>no shebang in +dir