@samitouri / QOSAMI-WSL / commits / 299d05d4

Grant VM access to a caller-supplied root VHD override as the user (#40895)

The WSLC boot path attached the root VHD without granting VM access, so a caller-supplied RootVhdOverride was opened by the VM worker process with no check that the user can actually access the file. Grant access while impersonating the user (as the WSL2 boot path does), which both authorizes the disk and ensures the override targets a file within the user's reach. Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Ben Hillis committed Jun 25, 2026 at 09:33 UTC 299d05d4afa823025fab4d73b596f52ee1a27023
1 file changed +12 -3
src/windows/service/exe/HcsVirtualMachine.cpp
+12 -3
@@ -259,7 +259,7 @@ HcsVirtualMachine::HcsVirtualMachine(_In_ const WSLCSessionSettings* Settings)
259
260 // Setup boot VHDs
261 hcs::Scsi scsiController{};
262 - auto attachScsiDisk = [&](PCWSTR path) {
262 + auto attachScsiDisk = [&](PCWSTR path, bool grantUserAccess) {
263 const ULONG lun = AllocateLun();
264 hcs::Attachment disk{};
265 disk.Type = hcs::AttachmentType::VirtualDisk;
@@ -269,12 +269,21 @@ HcsVirtualMachine::HcsVirtualMachine(_In_ const WSLCSessionSettings* Settings)
269 disk.AlwaysAllowSparseFiles = true;
270 disk.SupportEncryptedFiles = true;
271 scsiController.Attachments[std::to_string(lun)] = std::move(disk);
272 +
273 DiskInfo diskInfo{path};
274 +
275 + if (grantUserAccess)
276 + {
277 + auto runAsUser = wil::impersonate_token(m_userToken.get());
278 + hcs::GrantVmAccess(m_vmIdString.c_str(), path);
279 + diskInfo.AccessGranted = true;
280 + }
281 +
282 m_attachedDisks.emplace(lun, std::move(diskInfo));
283 };
284
276 - attachScsiDisk(rootVhdPath.c_str());
277 - attachScsiDisk(kernelModulesPath.c_str());
285 + attachScsiDisk(rootVhdPath.c_str(), Settings->RootVhdOverride != nullptr);
286 + attachScsiDisk(kernelModulesPath.c_str(), false);
287
288 vmSettings.Devices.Scsi["0"] = std::move(scsiController);
289