NetBSD Problem Report #61083

From www@netbsd.org  Thu Nov 26 22:49:59 2026
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits)
	 client-signature RSA-PSS (2048 bits))
	(Client CN "mail.netbsd.org", Issuer "YR1" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 86B751A923B
	for <gnats-bugs@gnats.NetBSD.org>; Thu, 26 Nov 2026 22:49:59 +0000 (UTC)
Message-Id: <20261126224958.6C5651A923D@mollari.NetBSD.org>
Date: Thu, 26 Nov 2026 22:49:58 +0000 (UTC)
From: contact+netbsd.org@hacktivis.me
Reply-To: contact+netbsd.org@hacktivis.me
To: gnats-bugs@NetBSD.org
Subject: fexecve fails on sh-based /usr/bin/true
X-Send-Pr-Version: www-1.0
X-From4GNATS: "contact+netbsd.org@hacktivis.me via gnats" <gnats-admin@NetBSD.org>

>Number:         61083
>Category:       bin
>Synopsis:       fexecve fails on sh-based /usr/bin/true
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kre
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 26 22:50:00 +0000 2026
>Last-Modified:  Fri Nov 27 21:40:01 +0000 2026
>Originator:     Haelwenn (lanodan) Monnier
>Release:        11.0
>Organization:
>Environment:
NetBSD build 11.0 NetBSD 11.0 (GENERIC) #0: Thu Jul 30 15:23:12 UTC 2026  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
When a file-descriptor for sh-based "/usr/bin/true" is passed to fexecve(), /bin/sh then fails with "/bin/sh: Can't open true".
Strangely if "/usr/bin/true" is copied into the home, it then works without any issues.
>How-To-Repeat:
$ cat fexec.c                                                                                                                                                                                                                                                                                                                  
#define _POSIX_C_SOURCE 200809L

#include <errno.h>
#include <fcntl.h> 
#include <unistd.h>
#include <string.h>
#include <stdio.h>

extern char **environ;

int
main(int argc, char *argv[]) {
	argc--,argv++;
	int fd = open(argv[0], O_EXEC | O_CLOEXEC | O_NOCTTY); 
	if(fd < 0) {
		fprintf(stderr, "fexec: error: open \"%s\": %s\n", argv[0], strerror(errno));
		return 1;
	}

	fexecve(fd, argv, environ);

	fprintf(stderr, "fexec: error: fexecve \"%s\": %s\n", argv[0], strerror(errno));
	close(fd);
	return 128;
}
$ make fexec                                                                                                                                                                                                                                                                                                                   
cc -O2    -o fexec fexec.c 
$ ./fexec `which uname` -a        
NetBSD build 11.0 NetBSD 11.0 (GENERIC) #0: Thu Jul 30 15:23:12 UTC 2026  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
$ which true
/usr/bin/true
$ ./fexec `which true`     
/bin/sh: Can't open true
$ file `which true`    
/usr/bin/true: POSIX shell script, ASCII text executable
$ cp /usr/bin/true exit0.sh
$ ./fexec $PWD/exit0.sh                                                                                                                                                                                                                                                                                                        
$ $PWD/exit0.sh
$ ed hello.sh
hello.sh: No such file or directory
a
#!/bin/sh
echo "Hello World!"
exit 0
.
w
37
q
$ ./fexec $PWD/hello.sh
fexec: error: open "/home/build/hello.sh": Permission denied
$ chmod +x $PWD/hello.sh
$ ./fexec $PWD/hello.sh  
Hello World!
$ ls -l
total 64
-r-xr-xr-x   1 build  users     18 Nov 26 22:38 exit0.sh
-rwxr-xr-x   1 build  users  23352 Nov 26 22:31 fexec
-rw-r--r--   1 build  users    513 Nov 26 22:31 fexec.c
-rwxr-xr-x   1 build  users     37 Nov 26 22:40 hello.sh
$ ls -l /usr/bin/true
-r-xr-xr-x  1 root  wheel  18 Jul 30 15:23 /usr/bin/true
$
>Fix:

>Release-Note:

>Audit-Trail:
From: RVP <rvp@SDF.ORG>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: bin/61083: fexecve fails on sh-based /usr/bin/true
Date: Fri, 27 Nov 2026 09:40:36 +0000 (UTC)

 On Thu, 26 Nov 2026, contact+netbsd.org@hacktivis.me via gnats wrote:

 >> Description:
 > When a file-descriptor for sh-based "/usr/bin/true" is passed to fexecve(), /bin/sh then fails with "/bin/sh: Can't open true".
 > Strangely if "/usr/bin/true" is copied into the home, it then works without any issues.
 >
 > [...]
 >
 > $ ls -l
 > total 64
 > -r-xr-xr-x   1 build  users     18 Nov 26 22:38 exit0.sh
 > -rwxr-xr-x   1 build  users  23352 Nov 26 22:31 fexec
 > -rw-r--r--   1 build  users    513 Nov 26 22:31 fexec.c
 > -rwxr-xr-x   1 build  users     37 Nov 26 22:40 hello.sh
 > $ ls -l /usr/bin/true
 > -r-xr-xr-x  1 root  wheel  18 Jul 30 15:23 /usr/bin/true
 > $
 >

 This is a kernel issue. In the fexecve()+script case, the kernel strips off the
 directory components, so the script has to always be in the current directory:

 https://github.com/NetBSD/src/blob/trunk/sys/kern/kern_exec.c#L340

 `/usr/bin/true' should also run if you `cd /usr/bin' first.

 -RVP

Responsible-Changed-From-To: bin-bug-people->kre
Responsible-Changed-By: kre@NetBSD.org
Responsible-Changed-When: Fri, 27 Nov 2026 10:36:24 +0000
Responsible-Changed-Why:
I will look into this.

I am suspecting a kernel problem (hence this probably should be
kern/61083) - where the full path name is being lost somewhere.

Note it isn't the cp that changes things, it is that the script
to be run ends up in the current directory of the process doing
the fexeve ...  ie:
	cd /usr/bin; $OLDPWD/fexec $(which true)
works fine, but:
	cd -; cd /tmp; $OLDPWD/fexec $OLDPWD/exit0.sh
does not.

This suggests to me that on an fexecve() the full path name is not
being passed through (which the shell's "cannot exec true" suggests
as well, or it would say "cannot exec /usr/bin/true").   Similarly
I suspect that if /usr/bin/true were copied to ./true instead of
./exit0.sh then it would have appeared to work without issues.

Whether this is (reasonably) fixable is yet to be determined, but
I suspect it should be possible.


From: Robert Elz <kre@munnari.OZ.AU>
To: gnats-bugs@netbsd.org, netbsd-bugs@netbsd.org
Cc: contact+netbsd.org@hacktivis.me
Subject: Re: bin/61083 (fexecve fails on sh-based /usr/bin/true)
Date: Sat, 28 Nov 2026 04:34:14 +0700

 The following (small) kernel patch seems to work for me, with some caveats.

 First, the O_CLOEXEC flag (or any other way of setting close on exec)
 must not be set on the fd passed to fexecve() if the "binary" to be exec'd
 is a #! script (easy to check, just read the first two bytes from it,
 and look...)

 Second, to fexecve() a binary that is a #! script, either there needs to be
 a union mount of the fdesc filesystem on /dev or /dev/fd needs to be populated
 with enough entries to include the file descriptor for the fd being used ...
 "MAKEDEV fd" (executed in /dev) will make enough entries for up to fd 63,
 so in general, you'd want to keep the fd under that.   I haven't tested the
 fdesc filesystem union mount method, but there's no reason to believe that
 would not be equivalent.

 Third, this scheme won't work with script interpreters that close open
 fd's (above 2 usually) before they start processing the script.  Once the
 interpreter has opened the script, it could then safely close the fd that
 was used for the fexecve(), but not before then (I haven't used csh at all
 in many decades, but it is rumoured to have (or once had) that problem.)

 With these restrictions this patch seems to work (I haven't tested it, yet
 anyway, on -11 sources, but I doubt it will be a problem there).

 Before this, I doubt that fexecve() has ever (properly) worked on a binary
 that is a #! - the issue is that #!'s require the path name passed to exec
 for the script, in order to hand that off to the interpreter (sh, or awk, 
 sed, python, ...) but with fexecve() there is no pathname, just the open fd.

 Before this patch, the kernel used to cobble up a name to use based upon what
 was being passed as argv[0] to the application being exec'd - just the part
 of that after the last "/" of that.   That was never correct, argv[0] doesn't
 even need to exist, let alone be related to the binary being exec'd in any
 way.  If that was considered adequate, there would be no point using fexecve()
 since the kernel was just going to (effectively) ignore the fd passed, and use
 argv[0] as the script to run - may as well just evecve(argv[0], argv, environ)
 instead in that case (before this patch the fd would be used only to find the
 script to locate which interpreter to use, when the binary is a #! type.)

 For now anyway, I believe the following is about as good as we're likely
 to get it in the near future.

 I'm not going to commit it just yet, to allow some time for others to
 review this change, and see whether or not it might break something
 (which it shouldn't, all it does is replace the (bad) kernel made
 argv[0] substring with /dev/fd/N as the file for the interpreter to read,
 when it is an fexecve() of an interpreted script), or perhaps suggest a
 better method.

 kre

 Index: exec_script.c
 ===================================================================
 RCS file: /cvsroot/src/sys/kern/exec_script.c,v
 retrieving revision 1.86
 diff -u -r1.86 exec_script.c
 --- exec_script.c	28 Aug 2026 06:33:50 -0000	1.86
 +++ exec_script.c	27 Nov 2026 21:24:08 -0000
 @@ -257,20 +257,21 @@
  	}
  	tmpsap->fa_len = MAXPATHLEN;
  	tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
 +	if (epp->ep_xfd == -1
  #ifdef FDSCRIPTS
 -	if ((epp->ep_flags & EXEC_HASFD) == 0) {
 +	     && (epp->ep_flags & EXEC_HASFD) == 0
  #endif
 +	) {
  		/* normally can't fail, but check for it if diagnostic */
  		error = copystr(epp->ep_kname, tmpsap->fa_arg, MAXPATHLEN,
  		    NULL);
  		KASSERT(error == 0);
  		tmpsap++;
 -#ifdef FDSCRIPTS
  	} else {
 -		snprintf(tmpsap->fa_arg, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
 +		snprintf(tmpsap->fa_arg, MAXPATHLEN, "/dev/fd/%d",
 +		    epp->ep_xfd == -1 ? epp->ep_fd : epp->ep_xfd);
  		tmpsap++;
  	}
 -#endif
  	tmpsap->fa_arg = NULL;

  	/* Save the old vnode so we can clean it up later. */


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.