1
0
mirror of https://github.com/veracrypt/VeraCrypt.git synced 2025-11-11 11:08:02 -06:00

Windows: handle rare case where call to SwitchDestop fails when trying to display password dialog in our secure desktop. This can happen for example if VeraCrypt process is launched early when user session is opened and it tries to load favorites before user workstation becomes visible.

This commit is contained in:
Mounir IDRASSI
2018-03-25 21:40:07 +02:00
parent b206988531
commit f38c4cfee8

View File

@@ -13211,15 +13211,32 @@ static DWORD WINAPI SecureDesktopThread(LPVOID lpThreadParameter)
unsigned int monitoringThreadID = 0; unsigned int monitoringThreadID = 0;
SecureDesktopThreadParam* pParam = (SecureDesktopThreadParam*) lpThreadParameter; SecureDesktopThreadParam* pParam = (SecureDesktopThreadParam*) lpThreadParameter;
SecureDesktopMonitoringThreadParam monitorParam; SecureDesktopMonitoringThreadParam monitorParam;
HDESK hOriginalDesk = GetThreadDesktop (GetCurrentThreadId ());
BOOL bNewDesktopSet = FALSE;
int counter = 0;
SetThreadDesktop (pParam->hDesk); // wait for SwitchDesktop to succeed before using it for current thread
SwitchDesktop (pParam->hDesk); // we wait a maximum of 5 seconds
for (counter = 0; counter < 10; counter++)
{
if (SwitchDesktop (pParam->hDesk))
{
bNewDesktopSet = TRUE;
break;
}
Sleep (SECUREDESKTOP_MONOTIR_PERIOD);
}
// create the thread that will ensure that VeraCrypt secure desktop has always user input if (bNewDesktopSet)
monitorParam.szVCDesktopName = pParam->szDesktopName; {
monitorParam.hVcDesktop = pParam->hDesk; SetThreadDesktop (pParam->hDesk);
monitorParam.pbStopMonitoring = &bStopMonitoring;
hMonitoringThread = (HANDLE) _beginthreadex (NULL, 0, SecureDesktopMonitoringThread, (LPVOID) &monitorParam, 0, &monitoringThreadID); // create the thread that will ensure that VeraCrypt secure desktop has always user input
monitorParam.szVCDesktopName = pParam->szDesktopName;
monitorParam.hVcDesktop = pParam->hDesk;
monitorParam.pbStopMonitoring = &bStopMonitoring;
hMonitoringThread = (HANDLE) _beginthreadex (NULL, 0, SecureDesktopMonitoringThread, (LPVOID) &monitorParam, 0, &monitoringThreadID);
}
pParam->retValue = DialogBoxParamW (pParam->hInstance, pParam->lpTemplateName, pParam->retValue = DialogBoxParamW (pParam->hInstance, pParam->lpTemplateName,
NULL, pParam->lpDialogFunc, pParam->dwInitParam); NULL, pParam->lpDialogFunc, pParam->dwInitParam);
@@ -13232,6 +13249,12 @@ static DWORD WINAPI SecureDesktopThread(LPVOID lpThreadParameter)
CloseHandle (hMonitoringThread); CloseHandle (hMonitoringThread);
} }
if (bNewDesktopSet)
{
SetThreadDesktop (hOriginalDesk);
SwitchDesktop (hOriginalDesk);
}
return 0; return 0;
} }
@@ -13290,7 +13313,6 @@ INT_PTR SecureDesktopDialogBoxParam(
hSecureDesk = CreateDesktop (szDesktopName, NULL, NULL, 0, desktopAccess, NULL); hSecureDesk = CreateDesktop (szDesktopName, NULL, NULL, 0, desktopAccess, NULL);
if (hSecureDesk) if (hSecureDesk)
{ {
HDESK hOriginalDesk = GetThreadDesktop (GetCurrentThreadId ());
SecureDesktopThreadParam param; SecureDesktopThreadParam param;
param.hDesk = hSecureDesk; param.hDesk = hSecureDesk;
@@ -13307,9 +13329,6 @@ INT_PTR SecureDesktopDialogBoxParam(
WaitForSingleObject (hThread, INFINITE); WaitForSingleObject (hThread, INFINITE);
CloseHandle (hThread); CloseHandle (hThread);
SwitchDesktop (hOriginalDesk);
SetThreadDesktop (hOriginalDesk);
retValue = param.retValue; retValue = param.retValue;
bSuccess = TRUE; bSuccess = TRUE;
} }