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

Windows: correctly handle WIN32 LastError when mounting. Harmonize file access checks between GUI and console. Skip ERROR_SHARING_VIOLATION in primary check in order to let the driver handle it more thoroughly.

This commit is contained in:
Mounir IDRASSI
2015-03-19 00:13:56 +01:00
parent f397f70fbe
commit d3db2548b5
2 changed files with 79 additions and 55 deletions

View File

@@ -6317,6 +6317,7 @@ typedef struct
MOUNT_STRUCT* pmount; MOUNT_STRUCT* pmount;
BOOL* pbResult; BOOL* pbResult;
DWORD* pdwResult; DWORD* pdwResult;
DWORD dwLastError;
} MountThreadParam; } MountThreadParam;
void CALLBACK MountWaitThreadProc(void* pArg, HWND ) void CALLBACK MountWaitThreadProc(void* pArg, HWND )
@@ -6325,6 +6326,8 @@ void CALLBACK MountWaitThreadProc(void* pArg, HWND )
*(pThreadParam->pbResult) = DeviceIoControl (hDriver, TC_IOCTL_MOUNT_VOLUME, pThreadParam->pmount, *(pThreadParam->pbResult) = DeviceIoControl (hDriver, TC_IOCTL_MOUNT_VOLUME, pThreadParam->pmount,
sizeof (MOUNT_STRUCT),pThreadParam->pmount, sizeof (MOUNT_STRUCT), pThreadParam->pdwResult, NULL); sizeof (MOUNT_STRUCT),pThreadParam->pmount, sizeof (MOUNT_STRUCT), pThreadParam->pdwResult, NULL);
pThreadParam->dwLastError = GetLastError ();
} }
/************************************************************************/ /************************************************************************/
@@ -6353,7 +6356,7 @@ int MountVolume (HWND hwndDlg,
BOOL bReportWrongPassword) BOOL bReportWrongPassword)
{ {
MOUNT_STRUCT mount; MOUNT_STRUCT mount;
DWORD dwResult; DWORD dwResult, dwLastError = ERROR_SUCCESS;
BOOL bResult, bDevice; BOOL bResult, bDevice;
char root[MAX_PATH]; char root[MAX_PATH];
int favoriteMountOnArrivalRetryCount = 0; int favoriteMountOnArrivalRetryCount = 0;
@@ -6484,13 +6487,17 @@ retry:
mountThreadParam.pmount = &mount; mountThreadParam.pmount = &mount;
mountThreadParam.pbResult = &bResult; mountThreadParam.pbResult = &bResult;
mountThreadParam.pdwResult = &dwResult; mountThreadParam.pdwResult = &dwResult;
mountThreadParam.dwLastError = ERROR_SUCCESS;
ShowWaitDialog (hwndDlg, FALSE, MountWaitThreadProc, &mountThreadParam); ShowWaitDialog (hwndDlg, FALSE, MountWaitThreadProc, &mountThreadParam);
dwLastError = mountThreadParam.dwLastError;
} }
else else
{ {
bResult = DeviceIoControl (hDriver, TC_IOCTL_MOUNT_VOLUME, &mount, bResult = DeviceIoControl (hDriver, TC_IOCTL_MOUNT_VOLUME, &mount,
sizeof (mount), &mount, sizeof (mount), &dwResult, NULL); sizeof (mount), &mount, sizeof (mount), &dwResult, NULL);
dwLastError = GetLastError ();
} }
burn (&mount.VolumePassword, sizeof (mount.VolumePassword)); burn (&mount.VolumePassword, sizeof (mount.VolumePassword));
@@ -6499,6 +6506,7 @@ retry:
burn (&mount.bTrueCryptMode, sizeof (mount.bTrueCryptMode)); burn (&mount.bTrueCryptMode, sizeof (mount.bTrueCryptMode));
burn (&mount.ProtectedHidVolPkcs5Prf, sizeof (mount.ProtectedHidVolPkcs5Prf)); burn (&mount.ProtectedHidVolPkcs5Prf, sizeof (mount.ProtectedHidVolPkcs5Prf));
SetLastError (dwLastError);
if (bResult == FALSE) if (bResult == FALSE)
{ {
// Volume already open by another process // Volume already open by another process
@@ -10270,7 +10278,16 @@ BOOL VolumePathExists (const char *volumePath)
return TRUE; return TRUE;
} }
return _access (volumePath, 0) == 0; if (_access (volumePath, 0) == 0)
return TRUE;
else
{
DWORD dwResult = GetLastError ();
if (dwResult == ERROR_SHARING_VIOLATION)
return TRUE;
else
return FALSE;
}
} }

View File

@@ -5182,74 +5182,81 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa
if (szFileName[0] != 0 && !IsMountedVolume (szFileName)) if (szFileName[0] != 0 && !IsMountedVolume (szFileName))
{ {
BOOL mounted; BOOL mounted = FALSE;
int EffectiveVolumePkcs5 = CmdVolumePkcs5; int EffectiveVolumePkcs5 = CmdVolumePkcs5;
BOOL EffectiveVolumeTrueCryptMode = CmdVolumeTrueCryptMode; BOOL EffectiveVolumeTrueCryptMode = CmdVolumeTrueCryptMode;
/* Priority is given to command line parameters if (!VolumePathExists (szFileName))
* Default values used only when nothing specified in command line
*/
if (EffectiveVolumePkcs5 == 0)
EffectiveVolumePkcs5 = DefaultVolumePkcs5;
if (!EffectiveVolumeTrueCryptMode)
EffectiveVolumeTrueCryptMode = DefaultVolumeTrueCryptMode;
// Cached password
mounted = MountVolume (hwndDlg, szDriveLetter[0] - 'A', szFileName, NULL, EffectiveVolumePkcs5, EffectiveVolumeTrueCryptMode, bCacheInDriver, bForceMount, &mountOptions, Silent, FALSE);
// Command line password or keyfiles
if (!mounted && (CmdVolumePassword.Length != 0 || FirstCmdKeyFile))
{ {
BOOL reportBadPasswd = CmdVolumePassword.Length > 0; handleWin32Error (hwndDlg);
if (FirstCmdKeyFile)
KeyFilesApply (hwndDlg, &CmdVolumePassword, FirstCmdKeyFile);
mounted = MountVolume (hwndDlg, szDriveLetter[0] - 'A',
szFileName, &CmdVolumePassword, EffectiveVolumePkcs5, EffectiveVolumeTrueCryptMode, bCacheInDriver, bForceMount,
&mountOptions, Silent, reportBadPasswd);
burn (&CmdVolumePassword, sizeof (CmdVolumePassword));
} }
else
if (FirstCmdKeyFile)
{ {
FirstKeyFile = FirstCmdKeyFile; /* Priority is given to command line parameters
KeyFilesEnable = TRUE; * Default values used only when nothing specified in command line
} */
if (EffectiveVolumePkcs5 == 0)
EffectiveVolumePkcs5 = DefaultVolumePkcs5;
if (!EffectiveVolumeTrueCryptMode)
EffectiveVolumeTrueCryptMode = DefaultVolumeTrueCryptMode;
// Ask user for password // Cached password
while (!mounted && !Silent) mounted = MountVolume (hwndDlg, szDriveLetter[0] - 'A', szFileName, NULL, EffectiveVolumePkcs5, EffectiveVolumeTrueCryptMode, bCacheInDriver, bForceMount, &mountOptions, Silent, FALSE);
{
int GuiPkcs5 = EffectiveVolumePkcs5;
BOOL GuiTrueCryptMode = EffectiveVolumeTrueCryptMode;
VolumePassword.Length = 0;
StringCbCopyA (PasswordDlgVolume, sizeof(PasswordDlgVolume),szFileName); // Command line password or keyfiles
if (!AskVolumePassword (hwndDlg, &VolumePassword, &GuiPkcs5, &GuiTrueCryptMode, NULL, TRUE)) if (!mounted && (CmdVolumePassword.Length != 0 || FirstCmdKeyFile))
break;
else
{ {
VolumePkcs5 = GuiPkcs5; BOOL reportBadPasswd = CmdVolumePassword.Length > 0;
VolumeTrueCryptMode = GuiTrueCryptMode;
burn (&GuiPkcs5, sizeof(GuiPkcs5)); if (FirstCmdKeyFile)
burn (&GuiTrueCryptMode, sizeof(GuiTrueCryptMode)); KeyFilesApply (hwndDlg, &CmdVolumePassword, FirstCmdKeyFile);
mounted = MountVolume (hwndDlg, szDriveLetter[0] - 'A',
szFileName, &CmdVolumePassword, EffectiveVolumePkcs5, EffectiveVolumeTrueCryptMode, bCacheInDriver, bForceMount,
&mountOptions, Silent, reportBadPasswd);
burn (&CmdVolumePassword, sizeof (CmdVolumePassword));
} }
WaitCursor (); if (FirstCmdKeyFile)
{
FirstKeyFile = FirstCmdKeyFile;
KeyFilesEnable = TRUE;
}
if (KeyFilesEnable && FirstKeyFile) // Ask user for password
KeyFilesApply (hwndDlg, &VolumePassword, FirstKeyFile); while (!mounted && !Silent)
{
int GuiPkcs5 = EffectiveVolumePkcs5;
BOOL GuiTrueCryptMode = EffectiveVolumeTrueCryptMode;
VolumePassword.Length = 0;
mounted = MountVolume (hwndDlg, szDriveLetter[0] - 'A', szFileName, &VolumePassword, VolumePkcs5, VolumeTrueCryptMode, bCacheInDriver, bForceMount, &mountOptions, FALSE, TRUE); StringCbCopyA (PasswordDlgVolume, sizeof(PasswordDlgVolume),szFileName);
if (!AskVolumePassword (hwndDlg, &VolumePassword, &GuiPkcs5, &GuiTrueCryptMode, NULL, TRUE))
break;
else
{
VolumePkcs5 = GuiPkcs5;
VolumeTrueCryptMode = GuiTrueCryptMode;
burn (&GuiPkcs5, sizeof(GuiPkcs5));
burn (&GuiTrueCryptMode, sizeof(GuiTrueCryptMode));
}
burn (&VolumePassword, sizeof (VolumePassword)); WaitCursor ();
burn (&VolumePkcs5, sizeof (VolumePkcs5));
burn (&VolumeTrueCryptMode, sizeof (VolumeTrueCryptMode));
burn (&mountOptions.ProtectedHidVolPassword, sizeof (mountOptions.ProtectedHidVolPassword));
burn (&mountOptions.ProtectedHidVolPkcs5Prf, sizeof (mountOptions.ProtectedHidVolPkcs5Prf));
NormalCursor (); if (KeyFilesEnable && FirstKeyFile)
KeyFilesApply (hwndDlg, &VolumePassword, FirstKeyFile);
mounted = MountVolume (hwndDlg, szDriveLetter[0] - 'A', szFileName, &VolumePassword, VolumePkcs5, VolumeTrueCryptMode, bCacheInDriver, bForceMount, &mountOptions, FALSE, TRUE);
burn (&VolumePassword, sizeof (VolumePassword));
burn (&VolumePkcs5, sizeof (VolumePkcs5));
burn (&VolumeTrueCryptMode, sizeof (VolumeTrueCryptMode));
burn (&mountOptions.ProtectedHidVolPassword, sizeof (mountOptions.ProtectedHidVolPassword));
burn (&mountOptions.ProtectedHidVolPkcs5Prf, sizeof (mountOptions.ProtectedHidVolPkcs5Prf));
NormalCursor ();
}
} }
if (UsePreferences) if (UsePreferences)