Bug 294780 - fexecve(2) only works for scripts if /dev/fd is mounted with the non-default nodup option.
Summary: fexecve(2) only works for scripts if /dev/fd is mounted with the non-default ...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 15.0-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: Kyle Evans
URL: https://reviews.freebsd.org/D56704
Keywords:
Depends on:
Blocks:
 
Reported: 2026-04-25 12:45 UTC by Jan Bramkamp
Modified: 2026-06-10 04:03 UTC (History)
2 users (show)

See Also:
kevans: mfc-stable15+
kevans: mfc-stable14+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Bramkamp 2026-04-25 12:45:40 UTC
I'm trying to use fexecve(2) to run a script, but open(path, O_RDONLY | O_EXEC) results in a file descriptor that's only opened for execution, but not for reading. This means that while the kernel can fexecve() it any interpreter specified on the shebang line (e.g. /bin/sh) can't use the file descriptor e.g. /dev/fd/6 to read the script source because by default it has dup-like instead of open-like semantics.

This means that as far as I can tell fexecve(2) is unusable for scripts unless root has mounted /dev/fd with the non-default nodup option.

Is there a reason why the kernel clears the O_RDONLY (FREAD) when opening with O_EXEC? Would it be possible to allow the combination of O_RDONLY | O_EXEC (FREAD | FEXEC) to allow both direct execution and interpreters to work with dup semantics?
Comment 1 Kyle Evans freebsd_committer freebsd_triage 2026-04-28 18:49:41 UTC
I only spent a minute looking at the fexecve(2) implementation, so CC kib@, but I think the official response is probably: just don't do that.  I note the following comment:

```
                /*                                                                                      
                 * If the descriptors was not opened with O_PATH, then                                  
                 * we require that it was opened with O_EXEC or                                         
                 * O_RDONLY.  In either case, exec_check_permissions()                                  
                 * below checks _current_ file access mode regardless                                   
                 * of the permissions additionally checked at the                                       
                 * open(2).                                                                             
                 */            
```

You're not benefiting yourself by opening it O_EXEC because it will be re-checked, you might as well just open it O_RDONLY and use faccessat(2) with AT_EMPTY_PATH to see if you should even try fexecve(2) since your userland checks are racey anyways, given the above.  I think that solves your problem, and I don't know if we want to try and do anything else here.
Comment 2 Jan Bramkamp 2026-04-28 19:13:38 UTC
I wanted to avoid reresolving the name, by opening it once for reading, then reopening it for execution with O_EMPTY_PATH. This works if and only if /dev/fd is mounted with the non-default "nodup" option. In this configuration the descriptor gets reopened both my code with O_EXEC and later by the interpreter as O_RDONLY.

Next I wanted to make my code work with the default /dev/fd mounted without any options (other than "rw"). For any interpreter to be able to run a script it needs to read it (which doesn't count as execution), but to launch the script with fexecve() the descriptor needs to be executable. For fexecve() to be usable with scripts **at all** and the default mount option a single file descriptor has to have both read and execute capability.

For now I accepted the mostly theoretical race condition and use execve(2) instead of fexecve(2). Still the fexecve(2) system call should support running scripts just like execve(2). If not this limitation must be clearly documented to safe future would be users from wasting time debugging this problem again.
Comment 3 Kyle Evans freebsd_committer freebsd_triage 2026-04-28 19:22:09 UTC
(In reply to Jan Bramkamp from comment #2)

> to launch the script with fexecve() the descriptor needs to be executable.

The comment I quoted disagrees with you there, and my reading of the following path agrees:

```
                error = fgetvp_exec(td, args->fd, &cap_fexecve_rights,                                  
                    &newtextvp);                                                                        
```

```
int                                                                                                     
fgetvp_exec(struct thread *td, int fd, const cap_rights_t *rightsp,                                     
    struct vnode **vpp)                                                                                 
{                                                                                                       
                                                                                                        
        return (_fgetvp(td, fd, FEXEC, rightsp, vpp));                                                  
}      
```

Follow that through to _fget:

```
...
        case FEXEC:                                                                                     
                if (fp->f_ops != &path_fileops &&                                                       
                    ((fp->f_flag & (FREAD | FEXEC)) == 0 ||                                             
                    (fp->f_flag & FWRITE) != 0))                                                        
                        error = EBADF;                                                                  
...
```

O_RDONLY is sufficient for fexecve(2), the vnode will be re-checked for executability after the above call.
Comment 4 Jan Bramkamp 2026-04-28 19:26:11 UTC
I'll try again tomorrow and report back.
Comment 5 Kyle Evans freebsd_committer freebsd_triage 2026-04-28 19:29:58 UTC
(In reply to Jan Bramkamp from comment #4)

Thanks.  In the meantime, looking at the fexecve(2) docs:

     The fexecve() ignores the file offset of fd.  Since execute permission is
     checked by fexecve(), the file descriptor fd need not have been opened
     with the O_EXEC flag.  However, if the file to be executed denies read
     permission for the process preparing to do the exec, the only way to
     provide the fd to fexecve() is to use the O_EXEC flag when opening fd.
     Note that the file to be executed can not be open for writing.

I agree that we could seize the opportunity to explicitly mention the interpreter problem here as a reason one might not want O_EXEC.
Comment 6 Jan Bramkamp 2026-04-28 19:47:15 UTC
I couldn't sleep and you're right. I had a stupid bug in my faccessat(2) based attempt. I used !! instead of a single ! to convert the return value to bool.

Opening the file for reading, checking if it should be executeable with faccessat(O_EMPTY_PATH) then using fexecve() with a read only descriptor works. 

Sorry for taking up your time with my mistakes. No change to the mutally exclusive nature of O_RDONLY/O_WRONLY/O_RDWR/O_EXEC is needed because the kernel accepts both a O_RDONLY and O_EXEC. A hint not to use O_EXEC unless you really have an executable that isn't also readable to the process opening it would be helpful.
Comment 7 Konstantin Belousov freebsd_committer freebsd_triage 2026-04-28 23:21:18 UTC
Access modes are not mixable.  As an additional twist, O_RDONLY is 0, so if
any mode is or-ed with O_RDONLY, it means just that mode.
Comment 8 Kyle Evans freebsd_committer freebsd_triage 2026-04-29 03:47:14 UTC
I don't usually argue with someone that wants to declare their bug isn't one, but I think it's still useful to track the documentation addition here in case someone stumbles upon it -- I anticipate closing it again within the week, though.
Comment 9 commit-hook freebsd_committer freebsd_triage 2026-05-01 03:03:28 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=9c18d55a768a3e60ecaba1325e9a3e00a25dee26

commit 9c18d55a768a3e60ecaba1325e9a3e00a25dee26
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-05-01 03:02:55 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-05-01 03:02:55 +0000

    fexecve(2): call out a scenario where you want !O_EXEC

    We note a reason why you might need it, but there's an equally important
    reason you may need to omit it: interpreted programs.  Add a note
    accordingly, along with the workaround configuration if there's reason
    you can't help it.

    PR:             294780
    Reviewed by:    Jan Bramkamp <crest_freebsd_rlwinm.de>, kib
    Differential Revision:  https://reviews.freebsd.org/D56704

 lib/libsys/execve.2 | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
Comment 10 commit-hook freebsd_committer freebsd_triage 2026-06-10 04:01:28 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=0de1f3a8aa7f5e99aa63bcd76dfa3fd7e017d295

commit 0de1f3a8aa7f5e99aa63bcd76dfa3fd7e017d295
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-05-01 03:02:55 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-06-10 04:00:46 +0000

    fexecve(2): call out a scenario where you want !O_EXEC

    We note a reason why you might need it, but there's an equally important
    reason you may need to omit it: interpreted programs.  Add a note
    accordingly, along with the workaround configuration if there's reason
    you can't help it.

    PR:             294780
    Reviewed by:    Jan Bramkamp <crest_freebsd_rlwinm.de>, kib

    (cherry picked from commit 9c18d55a768a3e60ecaba1325e9a3e00a25dee26)

 lib/libc/sys/execve.2 | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
Comment 11 commit-hook freebsd_committer freebsd_triage 2026-06-10 04:02:32 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=dc0cc22a898198a21bd58109351d2c2b2ec5410f

commit dc0cc22a898198a21bd58109351d2c2b2ec5410f
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-05-01 03:02:55 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-06-10 04:00:36 +0000

    fexecve(2): call out a scenario where you want !O_EXEC

    We note a reason why you might need it, but there's an equally important
    reason you may need to omit it: interpreted programs.  Add a note
    accordingly, along with the workaround configuration if there's reason
    you can't help it.

    PR:             294780
    Reviewed by:    Jan Bramkamp <crest_freebsd_rlwinm.de>, kib

    (cherry picked from commit 9c18d55a768a3e60ecaba1325e9a3e00a25dee26)

 lib/libsys/execve.2 | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)