VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common')
-rw-r--r--src/Common/Dlgcode.c16
-rw-r--r--src/Common/Random.c2
2 files changed, 12 insertions, 6 deletions
diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c
index 7b3d2d45..2c707f5d 100644
--- a/src/Common/Dlgcode.c
+++ b/src/Common/Dlgcode.c
@@ -14515,71 +14515,74 @@ static bool RunAsDesktopUser(
{
HANDLE hThreadToken = NULL, hShellProcess = NULL, hShellProcessToken = NULL, hPrimaryToken = NULL;
HWND hwnd = NULL;
DWORD dwPID = 0;
BOOL ret;
DWORD dwLastErr;
STARTUPINFOW si;
PROCESS_INFORMATION pi;
bool retval = false;
SecureZeroMemory(&si, sizeof(si));
SecureZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
// locate CreateProcessWithTokenW in Advapi32.dll
if (!CreateProcessWithTokenWPtr)
{
return false;
}
if (!ImpersonateSelf (SecurityImpersonation))
{
return false;
}
if (!OpenThreadToken (GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, TRUE, &hThreadToken))
{
return false;
}
else
{
TOKEN_PRIVILEGES tkp;
tkp.PrivilegeCount = 1;
LookupPrivilegeValueW(NULL, SE_INCREASE_QUOTA_NAME, &tkp.Privileges[0].Luid);
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- SetThreadToken (NULL, NULL);
+ if (!SetThreadToken(NULL, NULL))
+ {
+ goto cleanup;
+ }
AdjustTokenPrivileges(hThreadToken, FALSE, &tkp, 0, NULL, NULL);
dwLastErr = GetLastError();
if (ERROR_SUCCESS != dwLastErr)
{
goto cleanup;
}
}
// From this point down, we have handles to close, so make sure to clean up.
// Get an HWND representing the desktop shell.
// CAVEATS: This will fail if the shell is not running (crashed or terminated), or the default shell has been
// replaced with a custom shell. This also won't return what you probably want if Explorer has been terminated and
// restarted elevated.
hwnd = GetShellWindow();
if (NULL == hwnd)
{
dwLastErr = GetLastError();
goto cleanup;
}
// Get the PID of the desktop shell process.
GetWindowThreadProcessId(hwnd, &dwPID);
if (0 == dwPID)
{
dwLastErr = GetLastError();
goto cleanup;
}
// Open the desktop shell process in order to query it (get the token)
hShellProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPID);
if (!hShellProcess)
{
dwLastErr = GetLastError();
@@ -14601,71 +14604,74 @@ static bool RunAsDesktopUser(
if (!ret)
{
dwLastErr = GetLastError();
goto cleanup;
}
// Start the target process with the new token.
ret = CreateProcessWithTokenWPtr(
hPrimaryToken,
0,
szApp,
szCmdLine,
0,
NULL,
NULL,
&si,
&pi);
if (!ret)
{
dwLastErr = GetLastError();
goto cleanup;
}
// Make sure to close HANDLEs return in the PROCESS_INFORMATION.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
retval = true;
cleanup:
// Clean up resources
if (hShellProcessToken) CloseHandle(hShellProcessToken);
if (hPrimaryToken) CloseHandle(hPrimaryToken);
if (hShellProcess) CloseHandle(hShellProcess);
if (hThreadToken) CloseHandle(hThreadToken);
- RevertToSelf ();
+
+ if (!RevertToSelf())
+ return false;
+
if (!retval)
SetLastError (dwLastErr);
return retval;
}
// This function checks if the process is running with elevated privileges or not
BOOL IsElevated()
{
DWORD dwSize = 0;
HANDLE hToken = NULL;
TOKEN_ELEVATION tokenInformation;
BOOL bReturn = FALSE;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
if(GetTokenInformation(hToken, TokenElevation, &tokenInformation, sizeof(TOKEN_ELEVATION), &dwSize))
{
if (tokenInformation.TokenIsElevated)
bReturn = TRUE;
}
CloseHandle(hToken);
}
return bReturn;
}
// Based on code from:
// https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/Win7Samples/winui/shell/appplatform/ExecInExplorer/ExecInExplorer.cpp
HRESULT GetShellViewForDesktop(REFIID riid, void **ppv)
{
*ppv = NULL;
IShellWindows *psw;
HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw));
if (SUCCEEDED(hr))
@@ -15009,71 +15015,71 @@ HRESULT GenericDropTarget::QueryInterface(REFIID iid, void **ppvObject)
}
// compare guids fast and dirty
if (IsEqualGUID (iid, IID_IDropTarget))
{
AddRef();
(*ppvObject) = this;
return S_OK;
}
return E_FAIL;
}
ULONG GenericDropTarget::AddRef(void)
{
return (ULONG) InterlockedIncrement (&m_dwRefCount);
}
ULONG GenericDropTarget::Release(void)
{
if (InterlockedDecrement (&m_dwRefCount) == 0)
{
delete this;
return 0;
}
else
return (ULONG) m_dwRefCount;
}
//*************************************************************
// Register
// Called by whom implements us so we can serve
//*************************************************************
BOOL GenericDropTarget::Register(HWND hWnd)
{
if(NULL == hWnd)
- return E_FAIL;
+ return FALSE;
OleInitialize(NULL);
// required: these MUST be strong locked
CoLockObjectExternal(this, TRUE, 0);
// this is ok, we have it
DWORD hRes = ::RegisterDragDrop(hWnd, this);
if(SUCCEEDED(hRes))
{
// keep
m_DropTargetWnd = hWnd;
return TRUE;
}
// unlock
CoLockObjectExternal(this, FALSE, 0);
// bye bye COM
OleUninitialize();
// wont accept data now
return FALSE;
}
//*************************************************************
// Revoke
// Unregister us as a target
//*************************************************************
void GenericDropTarget::Revoke()
{
if(NULL == m_DropTargetWnd)
return;
RevokeDragDrop(m_DropTargetWnd);
@@ -15230,96 +15236,96 @@ DWORD GenericDropTarget::GotEnter(void)
// PasswordEditDropTarget
// Constructor
// ************************************************************
PasswordEditDropTarget::PasswordEditDropTarget() : GenericDropTarget (g_supportedFormats, ARRAYSIZE (g_supportedFormats))
{
}
// ************************************************************
// GotDrag
// ************************************************************
DWORD PasswordEditDropTarget::GotDrag(void)
{
return GotEnter();
}
// ************************************************************
// GotLeave
// ************************************************************
void PasswordEditDropTarget::GotLeave(void)
{
}
// ************************************************************
// GotEnter
// ************************************************************
DWORD PasswordEditDropTarget::GotEnter(void)
{
TCHAR szClassName[64];
DWORD dwStyles;
int maxLen;
HWND hChild = WindowFromPoint (m_DropPoint);
// check that we are on password edit control (we use maximum length to correctly identify password fields since they don't always have ES_PASSWORD style (if the the user checked show password)
if (hChild && GetClassName (hChild, szClassName, ARRAYSIZE (szClassName)) && (0 == _tcsicmp (szClassName, _T("EDIT")))
- && (dwStyles = GetWindowLong (hChild, GWL_STYLE)) && !(dwStyles & ES_NUMBER)
+ && (dwStyles = GetWindowLongPtr (hChild, GWL_STYLE)) && !(dwStyles & ES_NUMBER)
&& (maxLen = (int) SendMessage (hChild, EM_GETLIMITTEXT, 0, 0)) && (maxLen == MAX_PASSWORD || maxLen == MAX_LEGACY_PASSWORD)
)
{
return DROPEFFECT_COPY;
}
return DROPEFFECT_LINK;
}
// ************************************************************
// GotDrop
// Called if we have a drop text drop here.
//
// ************************************************************
void PasswordEditDropTarget::GotDrop(CLIPFORMAT format)
{
// value contains the material itself
if(m_Data)
{
TCHAR szClassName[64];
DWORD dwStyles;
int maxLen;
HWND hChild = WindowFromPoint (m_DropPoint);
if (hChild && GetClassName (hChild, szClassName, ARRAYSIZE (szClassName)) && (0 == _tcsicmp (szClassName, _T("EDIT")))
- && (dwStyles = GetWindowLong (hChild, GWL_STYLE)) && !(dwStyles & ES_NUMBER)
+ && (dwStyles = GetWindowLongPtr (hChild, GWL_STYLE)) && !(dwStyles & ES_NUMBER)
&& (maxLen = (int) SendMessage (hChild, EM_GETLIMITTEXT, 0, 0)) && (maxLen == MAX_PASSWORD || maxLen == MAX_LEGACY_PASSWORD)
)
{
WCHAR* wszText;
int wlen;
bool bFree = false;
// get the text
if (format == CF_UNICODETEXT)
{
wszText = (WCHAR *)m_Data;
}
else
{
char *iText = (char *)m_Data;
wlen = MultiByteToWideChar ((format == CF_OEMTEXT)? CP_OEMCP : CP_ACP, 0, iText, -1, NULL, 0);
wszText = new WCHAR[wlen];
if (wszText)
{
wlen = MultiByteToWideChar (CP_ACP, 0, iText, -1, wszText, wlen);
bFree = true;
}
}
WCHAR* pchData = wszText;
int txtlen = 0;
bool bTruncated = false;
// remove any appended \r or \n
while (*pchData)
{
if (*pchData == '\r' || *pchData == '\n')
break;
else
{
txtlen++;
diff --git a/src/Common/Random.c b/src/Common/Random.c
index c44c69d7..d5c09848 100644
--- a/src/Common/Random.c
+++ b/src/Common/Random.c
@@ -840,71 +840,71 @@ BOOL FastPoll (void)
capture */
RandaddIntPtr (GetClipboardOwner ()); /* Handle of clipboard owner */
RandaddIntPtr (GetClipboardViewer ()); /* Handle of start of
clpbd.viewer list */
RandaddIntPtr (GetCurrentProcess ()); /* Pseudohandle of current
process */
RandaddInt32 (GetCurrentProcessId ()); /* Current process ID */
RandaddIntPtr (GetCurrentThread ()); /* Pseudohandle of current
thread */
RandaddInt32 (GetCurrentThreadId ()); /* Current thread ID */
RandaddInt32 (GetCurrentTime ()); /* Milliseconds since Windows
started */
RandaddIntPtr (GetDesktopWindow ()); /* Handle of desktop window */
RandaddIntPtr (GetFocus ()); /* Handle of window with kb.focus */
RandaddInt32 (GetInputState ()); /* Whether sys.queue has any events */
RandaddInt32 (GetMessagePos ()); /* Cursor pos.for last message */
RandaddInt32 (GetMessageTime ()); /* 1 ms time for last message */
RandaddIntPtr (GetOpenClipboardWindow ()); /* Handle of window with
clpbd.open */
RandaddIntPtr (GetProcessHeap ()); /* Handle of process heap */
RandaddIntPtr (GetProcessWindowStation ()); /* Handle of procs
window station */
RandaddInt32 (GetQueueStatus (QS_ALLEVENTS)); /* Types of events in
input queue */
/* Get multiword system information */
GetCaretPos (&point); /* Current caret position */
RandaddBuf ((unsigned char *) &point, sizeof (POINT));
GetCursorPos (&point); /* Current mouse cursor position */
RandaddBuf ((unsigned char *) &point, sizeof (POINT));
/* Get percent of memory in use, bytes of physical memory, bytes of
free physical memory, bytes in paging file, free bytes in paging
file, user bytes of address space, and free user bytes */
memoryStatus.dwLength = sizeof (MEMORYSTATUS);
- GlobalMemoryStatus (&memoryStatus);
+ GlobalMemoryStatusEx (&memoryStatus);
RandaddBuf ((unsigned char *) &memoryStatus, sizeof (MEMORYSTATUS));
/* Get thread and process creation time, exit time, time in kernel
mode, and time in user mode in 100ns intervals */
handle = GetCurrentThread ();
GetThreadTimes (handle, &creationTime, &exitTime, &kernelTime, &userTime);
RandaddBuf ((unsigned char *) &creationTime, sizeof (FILETIME));
RandaddBuf ((unsigned char *) &exitTime, sizeof (FILETIME));
RandaddBuf ((unsigned char *) &kernelTime, sizeof (FILETIME));
RandaddBuf ((unsigned char *) &userTime, sizeof (FILETIME));
handle = GetCurrentProcess ();
GetProcessTimes (handle, &creationTime, &exitTime, &kernelTime, &userTime);
RandaddBuf ((unsigned char *) &creationTime, sizeof (FILETIME));
RandaddBuf ((unsigned char *) &exitTime, sizeof (FILETIME));
RandaddBuf ((unsigned char *) &kernelTime, sizeof (FILETIME));
RandaddBuf ((unsigned char *) &userTime, sizeof (FILETIME));
/* Get the minimum and maximum working set size for the current
process */
GetProcessWorkingSetSize (handle, &minimumWorkingSetSize,
&maximumWorkingSetSize);
RandaddIntPtr (minimumWorkingSetSize);
RandaddIntPtr (maximumWorkingSetSize);
/* The following are fixed for the lifetime of the process so we only
add them once */
if (addedFixedItems == 0)
{
STARTUPINFO startupInfo;
/* Get name of desktop, console window title, new window
position and size, window flags, and handles for stdin,
stdout, and stderr */
startupInfo.cb = sizeof (STARTUPINFO);
GetStartupInfo (&startupInfo);