NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

bin/61083: fexecve fails on sh-based /usr/bin/true



>Number:         61083
>Category:       bin
>Synopsis:       fexecve fails on sh-based /usr/bin/true
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 26 22:50:00 +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@localhost:/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@localhost:/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:




Home | Main Index | Thread Index | Old Index