From 7efe4e4f2a99450f3e6a15447a2ea816f9be848d Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Wed, 24 Feb 2021 21:42:20 +0100 Subject: Windows: Fix freeze when password dialog displayed in secure desktop and try to access token keyfiles protected by PIN --- src/Common/Dlgcode.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/Common/Dlgcode.c') diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c index 14c57f3e..8e1e0070 100644 --- a/src/Common/Dlgcode.c +++ b/src/Common/Dlgcode.c @@ -235,7 +235,8 @@ static std::vector rawHostDeviceList; CRITICAL_SECTION csSecureDesktop; /* Boolean that indicates if our Secure Desktop is active and being used or not */ -BOOL bSecureDesktopOngoing = FALSE; +volatile BOOL bSecureDesktopOngoing = FALSE; +TCHAR SecureDesktopName[65]; HINSTANCE hInst = NULL; HCURSOR hCursor = NULL; @@ -12214,6 +12215,35 @@ BOOL CALLBACK SecurityTokenKeyfileDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam return 0; } +extern "C" BOOL IsThreadInSecureDesktop(DWORD dwThreadID) +{ + BOOL bRet = FALSE; + if (bSecureDesktopOngoing) + { + HDESK currentDesk = GetThreadDesktop (dwThreadID); + if (currentDesk) + { + LPWSTR szName = NULL; + DWORD dwLen = 0; + if (!GetUserObjectInformation (currentDesk, UOI_NAME, NULL, 0, &dwLen)) + { + szName = (LPWSTR) malloc (dwLen); + if (szName) + { + if (GetUserObjectInformation (currentDesk, UOI_NAME, szName, dwLen, &dwLen)) + { + if (0 == _wcsicmp (szName, SecureDesktopName)) + bRet = TRUE; + } + free (szName); + } + } + } + } + + return bRet; +} + BOOL InitSecurityTokenLibrary (HWND hwndDlg) { @@ -12238,6 +12268,8 @@ BOOL InitSecurityTokenLibrary (HWND hwndDlg) HWND hParent = IsWindow (m_hwnd)? m_hwnd : GetActiveWindow(); if (!hParent) hParent = GetForegroundWindow (); + if (IsThreadInSecureDesktop(GetCurrentThreadId()) && !IsThreadInSecureDesktop(GetWindowThreadProcessId(hParent, NULL))) + hParent = GetActiveWindow (); if (SecureDesktopDialogBoxParam (hInst, MAKEINTRESOURCEW (IDD_TOKEN_PASSWORD), hParent, (DLGPROC) SecurityTokenPasswordDlgProc, (LPARAM) &str) == IDCANCEL) throw UserAbort (SRC_POS); } @@ -13847,7 +13879,7 @@ INT_PTR SecureDesktopDialogBoxParam( INT_PTR retValue = 0; BOOL bEffectiveUseSecureDesktop = bCmdUseSecureDesktopValid? bCmdUseSecureDesktop : bUseSecureDesktop; - if (bEffectiveUseSecureDesktop) + if (bEffectiveUseSecureDesktop && !IsThreadInSecureDesktop(GetCurrentThreadId())) { EnterCriticalSection (&csSecureDesktop); bSecureDesktopOngoing = TRUE; @@ -13893,6 +13925,8 @@ INT_PTR SecureDesktopDialogBoxParam( HANDLE hThread = ::CreateThread (NULL, 0, SecureDesktopThread, (LPVOID) ¶m, 0, NULL); if (hThread) { + StringCbCopy(SecureDesktopName, sizeof (SecureDesktopName), szDesktopName); + WaitForSingleObject (hThread, INFINITE); CloseHandle (hThread); -- cgit v1.2.3