Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/linux/init/WSLAInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
OneBlue marked this conversation as resolved.
}
Comment thread
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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)};
Expand Down