Failing to use close-on-exec #4980
Labels
Bug: Platform
A code bug in platform/TLS specific code.
external
Proposed by non-MSFT
OS: Linux
OS: Windows (User)
Milestone
Describe the bug
Most code paths in MsQuic don't create close-on-exec sockets. This is a problem because it means that MsQuic sockets will be inherited by subprocesses if the containing application calls
fork
+execve
,posix_spawn
or one of theCreateProcess
(with inherit handles mode) system calls.This can theoretically be a security issue, but the circumstances would be rather unlikely in practice. Imagine a privileged server application that spawns a child process of lower privilege. The child process would inherit privileged sockets. Many services like this are aware that they need to close all but intended handles in the child.
More likely, it's a reliability issue with keeping sockets open like that. QUIC is less affected by it than TCP, though.
Setting this flag needs to be done atomically with the creation of the socket, because you never know when another thread is going to spawn a child process.
Affected OS
Additional OS information
No response
MsQuic version
main
Steps taken to reproduce bug
This isn't a reproducible bug in the normal sense.
Expected behavior
This isn't a reproducible bug in the normal sense.
Actual outcome
This isn't a reproducible bug in the normal sense.
Additional details
How this can be fixed:
Linux:
Various APIs have ways of specifying that the new handle should be close-on-exec.
epoll_create
eventfd
open
fopen
(e
extendedfopen
flag in glibc)accept
(asaccept4
)socketpair
Windows:
WinSock supports this by using
WSASocketW
instead ofsocket
to create the socket. Specify theWSA_FLAG_NO_HANDLE_INHERIT
flag. Note that you'll also needWSA_FLAG_OVERLAPPPED
, becausesocket
specifies that flag internally.accept
's new socket inherits the inheritance flag from the listener socket.macOS:
Unfortunately, macOS does not have a way to atomically create sockets with
FD_CLOEXEC
already set.It might not be worthwhile to do this, and instead have a warning in the documentation. UNIX-based programs can close handles that shouldn't be inherited in the child of
fork
. Windows-based programs can either tellCreateProcess*
to not have the child inherit handles, or usePROC_THREAD_ATTRIBUTE_HANDLE_LIST
to explicitly specify handles to be inherited.The text was updated successfully, but these errors were encountered: