-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Unshare the FD table when creating threads in init to avoid fd leaks #14231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ac881b5
65576b8
f2fda9a
d693da2
8e5d470
f056117
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -446,13 +446,25 @@ void HandleMessageImpl(wsl::shared::SocketChannel& Channel, const WSLA_FORK& Mes | |
| std::promise<pid_t> childPid; | ||
|
|
||
| { | ||
| auto childLogic = [ListenSocket = std::move(ListenSocket), &SocketAddress, &Channel, &Message, &childPid]() mutable { | ||
| auto childLogic = [ListenSocketFd = ListenSocket.get(), &SocketAddress, &Channel, &Message, &childPid]() mutable { | ||
| wil::unique_fd ListenSocket; | ||
|
|
||
| // Close parent channel | ||
| if (Message.ForkType == WSLA_FORK::Process || Message.ForkType == WSLA_FORK::Pty) | ||
| { | ||
| Channel.Close(); | ||
| } | ||
|
|
||
| if (Message.ForkType == WSLA_FORK::Thread) | ||
| { | ||
| // If this is a thread, detach from the process' fd table. | ||
| // This prevents other threads from creating child processes that could inherit fds that this thread could create. | ||
| // N.B. This needs to happen before childPid is signalled to ensure that ListenSocket() is not closed by the parent before getting duplicated in the child's fd table. | ||
| THROW_LAST_ERROR_IF(unshare(CLONE_FILES) < 0); | ||
| } | ||
|
OneBlue marked this conversation as resolved.
|
||
|
|
||
| // ListenSocket should only be assigned after this thread is guaranteed to have its own fd table (either via unshare() or a child process). | ||
| ListenSocket.reset(ListenSocketFd); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn’t seem right to me. Both this thread and the parent have this file descriptor.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is correct since the file descriptor here is in a separate table from the parent.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I couldn't quite follow the logic, this seems right to me. |
||
| childPid.set_value(getpid()); | ||
|
|
||
| wil::unique_fd ProcessSocket{UtilAcceptVsock(ListenSocket.get(), SocketAddress, SESSION_LEADER_ACCEPT_TIMEOUT_MS)}; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.