diff --git a/src/linux/init/DnsServer.h b/src/linux/init/DnsServer.h index 41cc2ccaad..d4729bfbb7 100644 --- a/src/linux/init/DnsServer.h +++ b/src/linux/init/DnsServer.h @@ -44,8 +44,8 @@ class DnsServer wil::unique_fd m_tcpConnection; // Offset in m_currentDnsRequest indicating how much of the current DNS request on - // the TCP connection has been read. Using 2 bytes to represent the offset as the request length is represented using 2 bytes. - uint16_t m_currentRequestOffset = 0; + // the TCP connection has been read. + size_t m_currentRequestOffset = 0; // Buffer containing the current DNS request received on the TCP connection. std::vector m_currentDnsRequest; diff --git a/src/linux/init/init.cpp b/src/linux/init/init.cpp index 5bd4c793e4..035b0f7815 100644 --- a/src/linux/init/init.cpp +++ b/src/linux/init/init.cpp @@ -2572,23 +2572,33 @@ Return Value: continue; } - int Status{}; - auto Pid = waitpid(-1, &Status, WNOHANG); - if (Pid == 0) - { - continue; - } - else if (Pid > 0) + bool distroInitExited = false; + for (;;) { - if (Pid == distroInitPid.value()) + int Status{}; + auto Pid = waitpid(-1, &Status, WNOHANG); + if (Pid == 0) + { + break; + } + else if (Pid > 0) + { + distroInitExited |= (Pid == distroInitPid.value()); + } + else if (errno == ECHILD) { - LOG_ERROR("Init has exited. Terminating distribution"); break; } + else + { + FATAL_ERROR("waitpid failed {}", errno); + } } - else if (errno != ECHILD) + + if (distroInitExited) { - FATAL_ERROR("waitpid failed {}", errno); + LOG_ERROR("Init has exited. Terminating distribution"); + break; } } } diff --git a/src/linux/init/util.cpp b/src/linux/init/util.cpp index 966d0f3d9c..9b9f94ef60 100644 --- a/src/linux/init/util.cpp +++ b/src/linux/init/util.cpp @@ -3494,7 +3494,7 @@ int ProcessCreateProcessMessage(wsl::shared::Transaction& Transaction, gsl::span { execResult = 0; } - else if (execResult == sizeof(execResult)) + else if (ReadResult == sizeof(execResult)) { // Otherwise, return the error code to the service execResult = abs(execResult); diff --git a/src/windows/common/WslCoreMessageQueue.h b/src/windows/common/WslCoreMessageQueue.h index d8f3b8d43d..11b75a5b6e 100644 --- a/src/windows/common/WslCoreMessageQueue.h +++ b/src/windows/common/WslCoreMessageQueue.h @@ -171,10 +171,11 @@ class WslCoreMessageQueue const auto queueLock = m_lock.lock_exclusive(); THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_CANCELLED), m_isCanceled); m_workItems.emplace_back(new_result); + + // always maintain a 1:1 ratio for calls to submit_with_results() and ::SubmitThreadpoolWork + SubmitThreadpoolWork(m_tpHandle.get()); } - // always maintain a 1:1 ratio for calls to SubmitWorkWithResults() and ::SubmitThreadpoolWork - SubmitThreadpoolWork(m_tpHandle.get()); return new_result; } catch (...) @@ -194,10 +195,11 @@ class WslCoreMessageQueue const auto queueLock = m_lock.lock_exclusive(); THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_CANCELLED), m_isCanceled); m_workItems.emplace_back(std::forward(functor)); + + // always maintain a 1:1 ratio for calls to submit() and ::SubmitThreadpoolWork + SubmitThreadpoolWork(m_tpHandle.get()); } - // always maintain a 1:1 ratio for calls to SubmitWork() and ::SubmitThreadpoolWork - SubmitThreadpoolWork(m_tpHandle.get()); return true; } catch (...) diff --git a/src/windows/service/exe/LxssUserSession.cpp b/src/windows/service/exe/LxssUserSession.cpp index ef4cfd7044..dc9286fadb 100644 --- a/src/windows/service/exe/LxssUserSession.cpp +++ b/src/windows/service/exe/LxssUserSession.cpp @@ -891,11 +891,10 @@ HRESULT LxssUserSessionImpl::MountDisk( _Out_ int* Step, _Out_ LPWSTR* MountName) { - ExecutionContext context(Context::MountDisk); - std::lock_guard lock(m_instanceLock); return wil::ResultFromException([&]() { _CreateVm(); + ExecutionContext context(Context::MountDisk); const auto MountDiskType = WI_IsFlagSet(Flags, LXSS_ATTACH_MOUNT_FLAGS_VHD) ? WslCoreVm::DiskType::VHD : WslCoreVm::DiskType::PassThrough; const auto MountResult = m_utilityVm->MountDisk(Disk, MountDiskType, PartitionIndex, Name, Type, Options); const auto MountNameWide = wsl::shared::string::MultiByteToWide(MountResult.MountPointName); @@ -967,8 +966,6 @@ HRESULT LxssUserSessionImpl::MoveDistribution(_In_ LPCGUID DistroGuid, _In_ LPCW ::SetSecurityInfo(vhdHandle.get(), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, originalOwner, nullptr, nullptr, nullptr)); }; - setVhdOwner(newVhdPath); - auto revert = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { THROW_IF_WIN32_BOOL_FALSE(MoveFileEx( newVhdPath.c_str(), distro.VhdFilePath.c_str(), MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)); @@ -980,6 +977,8 @@ HRESULT LxssUserSessionImpl::MoveDistribution(_In_ LPCGUID DistroGuid, _In_ LPCW registration.Write(Property::BasePath, distro.BasePath.c_str()); }); + setVhdOwner(newVhdPath); + // Update the registry location registration.Write(Property::BasePath, Location); registration.Write(Property::VhdFileName, newVhdPath.filename().c_str()); diff --git a/src/windows/service/exe/WslCoreVm.cpp b/src/windows/service/exe/WslCoreVm.cpp index 03ffba1983..8c3070d1a1 100644 --- a/src/windows/service/exe/WslCoreVm.cpp +++ b/src/windows/service/exe/WslCoreVm.cpp @@ -1000,7 +1000,7 @@ ULONG WslCoreVm::AttachDiskLockHeld( if (WI_IsFlagSet(diskFlags, DiskStateFlags::Online)) { const auto diskHandle = wsl::windows::common::disk::OpenDevice(Disk, GENERIC_READ | GENERIC_WRITE, m_vmConfig.MountDeviceTimeout); - wsl::windows::common::disk::SetOnline(diskHandle.get(), false, m_vmConfig.MountDeviceTimeout); + wsl::windows::common::disk::SetOnline(diskHandle.get(), true, m_vmConfig.MountDeviceTimeout); } }); @@ -2752,8 +2752,9 @@ std::string WslCoreVm::s_GetMountTargetName(_In_ PCWSTR Disk, _In_opt_ PCWSTR Na if (ARGUMENT_PRESENT(Name)) { auto mountName = wsl::shared::string::WideToMultiByte(Name); - // Throw if the name contains '/' since it is a linux path separator - THROW_HR_IF(WSL_E_VM_MODE_INVALID_MOUNT_NAME, mountName.find('/') != std::string::npos); + THROW_HR_IF( + WSL_E_VM_MODE_INVALID_MOUNT_NAME, + mountName.empty() || mountName == "." || mountName == ".." || mountName.find('/') != std::string::npos); return mountName; } diff --git a/test/windows/MountTests.cpp b/test/windows/MountTests.cpp index fbf5676648..3c40454c5d 100644 --- a/test/windows/MountTests.cpp +++ b/test/windows/MountTests.cpp @@ -341,6 +341,23 @@ class MountTests WaitForDiskReady(); } + WSL2_TEST_METHOD(SpecifyInvalidMountName) + { + SKIP_UNSUPPORTED_ARM64_MOUNT_TEST(); + + FormatDisk({L"ext4"}, true); + + for (const auto* name : {L"\"\"", L".", L"..", L"foo/bar"}) + { + const auto mountCommand = std::format(L"--mount {} --vhd --name {} --partition 1", VhdDevice, name); + VERIFY_ARE_NOT_EQUAL(LxsstuLaunchWsl(mountCommand), (DWORD)0, name); + } + + const auto disk = GetBlockDeviceInWsl(); + VERIFY_IS_TRUE(IsBlockDevicePresent(disk)); + VERIFY_IS_FALSE(GetBlockDeviceMount(disk + L"1").has_value()); + } + // Test ensuring that name collision detection works in --mount --name WSL2_TEST_METHOD(SpecifyMountNameCollision) {