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 reply was made to PR bin/61083; it has been noted by GNATS.

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

 The following amended patch addresses:
 
  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...)
 
 not by making it possible use O_CLOEXEC with a fexecve() of a #! script,
 but by failing the fexecve() instead of allowing it to (appear to) work
 correctly, but then for the interpreter (sh or whatever is) to be unable
 to open the script file (which is passed in this case as /dev/fd/N which
 only works as intended if fd N is open to the script).
 
 Note that it would be possible (with a bunch of extra work) to kind of
 do a fake close on exec in these circumstances - that is, while the
 fexecve is in progress, disable close on exec (restore it if fexecve
 fails and returns some other error).   Instead set some other flag on
 the fd (not settable via any other means) which would cause fd N to
 automatically close, the first time it is opened via /dev/fd/N (unless
 N happens to be the lowest available fd, the one an open() would return
 at this point if the close on exec had worked, in which case the open()
 call would simply return N, leave close-on-exec clear (unless the new open
 call requests it) and continue.
 
 That isn't close on exec - the fd would be open in the new process until
 it is ready to open its script, which (might) affect the fd numbers returned
 from other open (dup, etc) sys calls, and could be closed by code which simply
 assumes that fd's <= 2 should never be open when the process in question 
 starts.   But for most purposes, it would work in a similar fashion (not 
 leaving an "unknown" fd open to be passed from process to process as more
 of them are exec'd) which is essentially what will happen now if fexecve()
 is used on a #! script (that is, once it is made to work properly) since that
 requires close on exec to be off.
 
 I'm not sure the "fake close on exec" is worth the effort though (fexecve()
 is quite rare, and using it on a #! even rarer) so I don't currently plan on
 implementing any of that.
 
 But unless anyone has any objections to the rest of this, as it is now, or
 suggestions for improvements, I plan on committing it this coming weekend.
 (It has been tested, and does work, including generating an error if close
 on exec is set, in the #! case only).
 
 kre
 
 ps: part of this change (the last hunk of the diff) is just an (IMO)
 cleaner way of cleaning up after an error, which I ran into when an earlier
 version of this updated patch wasn't quite perfect...  (that part of the
 change isn't really required).
 
 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	3 Dec 2026 12:11:57 -0000
 @@ -247,31 +247,42 @@
  	tmpsap = shellargp;
  	tmpsap->fa_len = shellnamelen + 1;
  	tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
 -	strlcpy(tmpsap->fa_arg, shellname, tmpsap->fa_len);
 +	strlcpy(tmpsap->fa_arg, shellname, tmpsap->fa_len);	/* argv[0] */
  	tmpsap++;
  	if (shellarg != NULL) {
  		tmpsap->fa_len = shellarglen + 1;
  		tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
 -		strlcpy(tmpsap->fa_arg, shellarg, tmpsap->fa_len);
 +		strlcpy(tmpsap->fa_arg, shellarg, tmpsap->fa_len); /* argv[1] */
  		tmpsap++;
  	}
  	tmpsap->fa_len = MAXPATHLEN;
 -	tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
 +	tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);	/* argv[1|2] */
 +	tmpsap[1].fa_arg = NULL;				/* argv[2|3] */
 +	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);
 +		if (epp->ep_xfd != -1) {
 +			fdtab_t *dt;
 +
 +			dt = atomic_load_consume(&l->l_fd->fd_dt);
 +			if (dt->dt_ff[epp->ep_xfd]->ff_exclose) {
 +				error = SET_ERROR(ENOTSUP);
 +				scriptvp = NULL;
 +				goto fail;
 +			}
 +		}
 +		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. */
  	scriptvp = epp->ep_vp;
 @@ -328,9 +339,7 @@
  		return (0);
  	}
  
 -#ifdef FDSCRIPTS
 -fail:
 -#endif
 + fail:;
  
  	/*
  	 * We should only reach this point if we have failed to load an
 @@ -352,10 +361,12 @@
  	}
  
  	/* free the fake arg list, because we're not returning it */
 -	if ((tmpsap = shellargp) != NULL) {
 -		while (tmpsap->fa_arg != NULL) {
 -			kmem_free(tmpsap->fa_arg, tmpsap->fa_len);
 -			tmpsap++;
 +	if (shellargp != NULL) {
 +		struct exec_fakearg *fap;
 +
 +		for (fap = shellargp; fap < tmpsap; fap++) {
 +			if (fap->fa_arg != NULL)
 +				kmem_free(fap->fa_arg, fap->fa_len);
  		}
  		kmem_free(shellargp, shellargp_len);
  	}
 
 



Home | Main Index | Thread Index | Old Index