NetBSD-Bugs archive

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

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



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. */





Home | Main Index | Thread Index | Old Index