From 5281e2d3b9adea8dff1730d78fe94af85582aea8 Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Wed, 9 Jul 2014 05:32:14 +0200 Subject: Static Code Analysis : fix resource leakage by ensuring that all Windows handles are released properly --- src/Common/Dlgcode.c | 28 ++++++++++++++++++++-------- src/Mount/Mount.c | 23 +++++++++++++++++++---- src/Setup/SelfExtract.c | 11 +++++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c index 54b848dc..f645fdec 100644 --- a/src/Common/Dlgcode.c +++ b/src/Common/Dlgcode.c @@ -1349,7 +1349,7 @@ HBITMAP RenderBitmap (char *resource, HWND hwndDest, int x, int y, int nWidth, i HDC hdcDest = GetDC (hwndDest); BitBlt (hdcDest, x, y, nWidth, nHeight, hdcRescaled, 0, 0, SRCCOPY); - DeleteDC (hdcDest); + ReleaseDC (hwndDest, hdcDest); } else { @@ -3492,6 +3492,8 @@ load: bPortableModeConfirmed = TRUE; + if (hDriver != INVALID_HANDLE_VALUE) + CloseHandle (hDriver); hDriver = CreateFile (WIN32_ROOT_PREFIX, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); } @@ -7484,16 +7486,19 @@ __int64 GetFileSize64 (const char *path) { HANDLE h = CreateFile (path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); LARGE_INTEGER size; + __int64 retSize = -1; - if (h == INVALID_HANDLE_VALUE) - return -1; - - if (GetFileSizeEx (h, &size) == 0) - return -1; + if (h) + { + if (GetFileSizeEx (h, &size)) + { + retSize = size.QuadPart; + } - CloseHandle (h); + CloseHandle (h); + } - return size.QuadPart; + return retSize; } @@ -8214,12 +8219,19 @@ BOOL RestartComputer (void) AdjustTokenPrivileges (hTkn, false, &tokenPrivil, 0, (PTOKEN_PRIVILEGES) NULL, 0); if (GetLastError() != ERROR_SUCCESS) + { + CloseHandle(hTkn); return false; + } if (!ExitWindowsEx (EWX_REBOOT, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED)) + { + CloseHandle(hTkn); return false; + } + CloseHandle(hTkn); return true; } diff --git a/src/Mount/Mount.c b/src/Mount/Mount.c index 370c5f57..eee282a8 100644 --- a/src/Mount/Mount.c +++ b/src/Mount/Mount.c @@ -7001,7 +7001,11 @@ BOOL TaskBarIconAdd (HWND hwnd) TaskBarIconMutex = CreateMutex (NULL, TRUE, "VeraCryptTaskBarIcon"); if (TaskBarIconMutex == NULL || GetLastError () == ERROR_ALREADY_EXISTS) { - TaskBarIconMutex = NULL; + if (TaskBarIconMutex) + { + CloseHandle(TaskBarIconMutex); + TaskBarIconMutex = NULL; + } return FALSE; } @@ -8692,7 +8696,12 @@ void AnalyzeKernelMiniDump (HWND hwndDlg) NormalCursor(); DWORD exitCode; - if (!GetExitCodeProcess (procInfo.hProcess, &exitCode) || exitCode != 0) + bool bExitCheck = (!GetExitCodeProcess (procInfo.hProcess, &exitCode) || exitCode != 0); + + CloseHandle(procInfo.hProcess); + CloseHandle(procInfo.hThread); + + if (bExitCheck) return; } @@ -8764,6 +8773,10 @@ void AnalyzeKernelMiniDump (HWND hwndDlg) { handleWin32Error (hwndDlg); Error ("DEBUGGER_NOT_FOUND"); + CloseHandle (procInfo.hProcess); + CloseHandle (procInfo.hThread); + CloseHandle (hChildStdoutRead); + CloseHandle (hChildStdoutWrite); return; } @@ -8787,6 +8800,8 @@ void AnalyzeKernelMiniDump (HWND hwndDlg) output.insert (output.size(), pipeBuffer, bytesReceived); } + CloseHandle (hChildStdoutRead); + NormalCursor(); bool otherDriver = (StringToUpperCase (output).find (StringToUpperCase (TC_APP_NAME)) == string::npos); @@ -8868,7 +8883,7 @@ void AnalyzeKernelMiniDump (HWND hwndDlg) retAddrs.push_back (s); } - /* +/* char url[MAX_URL_LENGTH]; sprintf (url, TC_APPLINK_SECURE "&dest=syserr-report&os=%s&osver=%d.%d.%d&arch=%s&err=%I64x&arg1=%I64x&arg2=%I64x&arg3=%I64x&arg4=%I64x&flag=%s&drv=%s", GetWindowsEdition().c_str(), @@ -8943,7 +8958,7 @@ void AnalyzeKernelMiniDump (HWND hwndDlg) if (AskYesNoString (msg.c_str()) == IDYES) ShellExecute (NULL, "open", urlStr.c_str(), NULL, NULL, SW_SHOWNORMAL); - */ +*/ } diff --git a/src/Setup/SelfExtract.c b/src/Setup/SelfExtract.c index 5be9cd21..a14a0db4 100644 --- a/src/Setup/SelfExtract.c +++ b/src/Setup/SelfExtract.c @@ -150,6 +150,8 @@ static int CompressBuffer (char *out, char *in, int len) if (!CreatePipe (&hChildStdinRead, &((HANDLE) hChildStdinWrite), &securityAttrib, 0)) { PkgError ("Cannot create STDIN pipe."); + CloseHandle(hChildStdoutWrite); + CloseHandle(hChildStdoutRead); return 0; } SetHandleInformation (hChildStdinWrite, HANDLE_FLAG_INHERIT, 0); @@ -166,6 +168,10 @@ static int CompressBuffer (char *out, char *in, int len) if (!CreateProcess (NULL, "gzip --best", NULL, NULL, TRUE, 0, NULL, NULL, &startupInfo, &procInfo)) { PkgError ("Error: Cannot run gzip.\n\nBefore you can create a self-extracting VeraCrypt package, you need to have the open-source 'gzip' compression tool placed in any directory in the search path for executable files (for example, in 'C:\\Windows\\').\n\nNote: gzip can be freely downloaded e.g. from www.gzip.org"); + CloseHandle(hChildStdoutWrite); + CloseHandle(hChildStdoutRead); + CloseHandle(hChildStdinRead); + CloseHandle(hChildStdinWrite); return 0; } @@ -179,6 +185,8 @@ static int CompressBuffer (char *out, char *in, int len) if (!CloseHandle (hChildStdoutWrite)) { PkgError ("Cannot close STDOUT write"); + CloseHandle(hChildStdoutRead); + CloseHandle(hChildStdinRead); return 0; } @@ -198,6 +206,9 @@ static int CompressBuffer (char *out, char *in, int len) else bGzipHeaderRead = TRUE; // Skip the 10-byte gzip header } + + CloseHandle(hChildStdoutRead); + CloseHandle(hChildStdinRead); return res_len - 8; // A gzip stream ends with a CRC-32 hash and a 32-bit size (those 8 bytes need to be chopped off) } -- cgit v1.2.3