@samitouri / QOSAMI-WSL / commits / d02e0d0c

wslcsession: use DuplicateHandle idiom for self-process handle in GetProcessHandle (#40748)

Replace OpenProcess(GetCurrentProcessId()) in IWSLCSessionFactory::GetProcessHandle and IWSLCSession::GetProcessHandle with a duplicate of the GetCurrentProcess() pseudo-handle via wslutil::DuplicateHandle. A process should not need an access check to obtain a handle to itself; duplicating the pseudo-handle is the canonical idiom and avoids OpenProcess's access check against the calling thread's (possibly impersonated) effective token. An explicit desired-access mask keeps the handle's rights identical (PROCESS_SET_QUOTA | PROCESS_TERMINATE), so the marshaled handle and downstream usage are unchanged. This is a cleanup/hardening change. It does not fix the 0x80070005 seen after hibernate/resume, which originates from AssignProcessToJobObject in wslservice.exe (WSLCSessionManager::AddSessionProcessToJobObject), not from GetProcessHandle. Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Ben Hillis committed Jun 11, 2026 at 12:18 UTC d02e0d0c8d4da2d59bc3488e6c56736931bd1850
2 files changed +3 -9
src/windows/wslcsession/WSLCSession.cpp
+2 -5
@@ -281,12 +281,9 @@ UserCOMCallback::~UserCOMCallback() noexcept
281 HRESULT WSLCSession::GetProcessHandle(_Out_ HANDLE* ProcessHandle)
282 try
283 {
284 - RETURN_HR_IF(E_POINTER, ProcessHandle == nullptr);
284 + RETURN_HR_IF_NULL(E_POINTER, ProcessHandle);
285
286 - wil::unique_handle process{OpenProcess(PROCESS_SET_QUOTA | PROCESS_TERMINATE, FALSE, GetCurrentProcessId())};
287 - THROW_LAST_ERROR_IF(!process);
288 -
289 - *ProcessHandle = process.release();
286 + *ProcessHandle = wslutil::DuplicateHandle(GetCurrentProcess(), PROCESS_SET_QUOTA | PROCESS_TERMINATE);
287 return S_OK;
288 }
289 CATCH_RETURN();
src/windows/wslcsession/WSLCSessionFactory.cpp
+1 -4
@@ -76,10 +76,7 @@ try
76 {
77 RETURN_HR_IF_NULL(E_POINTER, ProcessHandle);
78
79 - wil::unique_handle process{OpenProcess(PROCESS_SET_QUOTA | PROCESS_TERMINATE, FALSE, GetCurrentProcessId())};
80 - RETURN_LAST_ERROR_IF(!process);
81 -
82 - *ProcessHandle = process.release();
79 + *ProcessHandle = wslutil::DuplicateHandle(GetCurrentProcess(), PROCESS_SET_QUOTA | PROCESS_TERMINATE);
80 return S_OK;
81 }
82 CATCH_RETURN()