From 74b82118d5f77116ec5f4a1e2438cdc77cead40f Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Mon, 15 May 2017 16:22:48 +0200 Subject: Windows: use IOCTL_DISK_GET_DRIVE_GEOMETRY_EX instead of the deprecated IOCTL_DISK_GET_DRIVE_GEOMETRY in order to get accurate disk size value. --- src/Common/Apidrvr.h | 11 ++++++++ src/Common/BootEncryption.cpp | 20 +++++++------- src/Common/BootEncryption.h | 2 +- src/Common/Dlgcode.c | 62 ++++++++++++++++++++++++++----------------- src/Common/Dlgcode.h | 2 +- src/Common/Password.c | 7 +++-- src/Common/Volumes.c | 24 ++++++++--------- 7 files changed, 76 insertions(+), 52 deletions(-) (limited to 'src/Common') diff --git a/src/Common/Apidrvr.h b/src/Common/Apidrvr.h index 07a8448c..28202e61 100644 --- a/src/Common/Apidrvr.h +++ b/src/Common/Apidrvr.h @@ -119,6 +119,9 @@ #define TC_IOCTL_REREAD_DRIVER_CONFIG TC_IOCTL (37) #define TC_IOCTL_GET_SYSTEM_DRIVE_DUMP_CONFIG TC_IOCTL (38) #define VC_IOCTL_GET_BOOT_LOADER_FINGERPRINT TC_IOCTL (39) +// result IOCTL_DISK_GET_DRIVE_GEOMETRY_EX +// IN OUT - DISK_GEOMETRY_EX_STRUCT +#define VC_IOCTL_GET_DRIVE_GEOMETRY_EX TC_IOCTL (40) // Legacy IOCTLs used before version 5.0 #define TC_IOCTL_LEGACY_GET_DRIVER_VERSION 466968 @@ -238,6 +241,14 @@ typedef struct } DISK_GEOMETRY_STRUCT; +typedef struct +{ + WCHAR deviceName[TC_MAX_PATH]; + DISK_GEOMETRY diskGeometry; + LARGE_INTEGER DiskSize; +} +DISK_GEOMETRY_EX_STRUCT; + typedef struct { WCHAR DeviceName[TC_MAX_PATH]; diff --git a/src/Common/BootEncryption.cpp b/src/Common/BootEncryption.cpp index cd7ea79a..0b684e49 100644 --- a/src/Common/BootEncryption.cpp +++ b/src/Common/BootEncryption.cpp @@ -1053,16 +1053,18 @@ namespace VeraCrypt } - DISK_GEOMETRY BootEncryption::GetDriveGeometry (int driveNumber) +#ifndef SETUP + + DISK_GEOMETRY_EX BootEncryption::GetDriveGeometry (int driveNumber) { wstringstream devName; devName << L"\\Device\\Harddisk" << driveNumber << L"\\Partition0"; - DISK_GEOMETRY geometry; + DISK_GEOMETRY_EX geometry; throw_sys_if (!::GetDriveGeometry (devName.str().c_str(), &geometry)); return geometry; } - +#endif // !SETUP wstring BootEncryption::GetWindowsDirectory () { @@ -3999,9 +4001,9 @@ namespace VeraCrypt if (config.InitialUnallocatedSpace < TC_BOOT_LOADER_AREA_SIZE) throw ErrorException ("NO_SPACE_FOR_BOOT_LOADER", SRC_POS); - DISK_GEOMETRY geometry = GetDriveGeometry (config.DriveNumber); + DISK_GEOMETRY_EX geometry = GetDriveGeometry (config.DriveNumber); - if (geometry.BytesPerSector != TC_SECTOR_SIZE_BIOS) + if (geometry.Geometry.BytesPerSector != TC_SECTOR_SIZE_BIOS) throw ErrorException ("SYSENC_UNSUPPORTED_SECTOR_SIZE_BIOS", SRC_POS); bool activePartitionFound = false; @@ -4425,17 +4427,17 @@ namespace VeraCrypt // Some chipset drivers may prevent access to the last sector of the drive if (!systemPartitionOnly) { - DISK_GEOMETRY geometry = GetDriveGeometry (config.DriveNumber); - if ((geometry.BytesPerSector > 0) && (geometry.BytesPerSector < TC_MAX_VOLUME_SECTOR_SIZE)) + DISK_GEOMETRY_EX geometry = GetDriveGeometry (config.DriveNumber); + if ((geometry.Geometry.BytesPerSector > 0) && (geometry.Geometry.BytesPerSector < TC_MAX_VOLUME_SECTOR_SIZE)) { - Buffer sector (geometry.BytesPerSector); + Buffer sector (geometry.Geometry.BytesPerSector); Device device (config.DevicePath); device.CheckOpened (SRC_POS); try { - device.SeekAt (config.DrivePartition.Info.PartitionLength.QuadPart - geometry.BytesPerSector); + device.SeekAt (config.DrivePartition.Info.PartitionLength.QuadPart - geometry.Geometry.BytesPerSector); device.Read (sector.Ptr(), (DWORD) sector.Size()); } catch (SystemException &e) diff --git a/src/Common/BootEncryption.h b/src/Common/BootEncryption.h index f7f2ec1a..4071a7f5 100644 --- a/src/Common/BootEncryption.h +++ b/src/Common/BootEncryption.h @@ -310,7 +310,7 @@ namespace VeraCrypt void CreateVolumeHeader (uint64 volumeSize, uint64 encryptedAreaStart, Password *password, int ea, int mode, int pkcs5, int pim); wstring GetSystemLoaderBackupPath (); uint32 GetChecksum (byte *data, size_t size); - DISK_GEOMETRY GetDriveGeometry (int driveNumber); + DISK_GEOMETRY_EX GetDriveGeometry (int driveNumber); PartitionList GetDrivePartitions (int driveNumber); wstring GetRemarksOnHiddenOS (); wstring GetWindowsDirectory (); diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c index 73eabfaf..d09e0044 100644 --- a/src/Common/Dlgcode.c +++ b/src/Common/Dlgcode.c @@ -3102,6 +3102,7 @@ BOOL GetDriveLabel (int driveNo, wchar_t *label, int labelSize) return GetVolumeInformationW (root, label, labelSize / 2, NULL, NULL, &fileSystemFlags, NULL, 0); } +#ifndef SETUP /* Stores the device path of the system partition in SysPartitionDevicePath and the device path of the system drive in SysDriveDevicePath. @@ -3254,6 +3255,7 @@ int IsNonSysPartitionOnSysDrive (const wchar_t *path) } } +#endif //!SETUP wstring GetSysEncryptionPretestInfo2String (void) { @@ -3477,6 +3479,7 @@ char * GetLegalNotices () return buf; } +#ifndef SETUP BOOL CALLBACK RawDevicesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { @@ -3835,6 +3838,7 @@ BOOL CALLBACK RawDevicesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM l return 0; } +#endif //!SETUP BOOL DoDriverInstall (HWND hwndDlg) { @@ -7266,6 +7270,8 @@ BOOL GetPhysicalDriveAlignment(UINT nDriveNumber, STORAGE_ACCESS_ALIGNMENT_DESCR return TRUE; } +#ifndef SETUP + /************************************************************/ // implementation of the generic wait dialog mechanism @@ -7980,6 +7986,7 @@ BOOL UnmountVolumeAfterFormatExCall (HWND hwndDlg, int nDosDriveNo) return UnmountVolumeBase (hwndDlg, nDosDriveNo, FALSE, TRUE); } +#endif //!SETUP BOOL IsPasswordCacheEmpty (void) { @@ -8176,29 +8183,31 @@ BOOL GetDeviceInfo (const wchar_t *deviceName, DISK_PARTITION_INFO_STRUCT *info) return DeviceIoControl (hDriver, TC_IOCTL_GET_DRIVE_PARTITION_INFO, info, sizeof (*info), info, sizeof (*info), &dwResult, NULL); } - -BOOL GetDriveGeometry (const wchar_t *deviceName, PDISK_GEOMETRY diskGeometry) +#ifndef SETUP +BOOL GetDriveGeometry (const wchar_t *deviceName, PDISK_GEOMETRY_EX diskGeometry) { BOOL bResult; DWORD dwResult; - DISK_GEOMETRY_STRUCT dg; + DISK_GEOMETRY_EX_STRUCT dg; memset (&dg, 0, sizeof(dg)); StringCbCopyW ((PWSTR) &dg.deviceName, sizeof(dg.deviceName), deviceName); - bResult = DeviceIoControl (hDriver, TC_IOCTL_GET_DRIVE_GEOMETRY, &dg, + bResult = DeviceIoControl (hDriver, VC_IOCTL_GET_DRIVE_GEOMETRY_EX, &dg, sizeof (dg), &dg, sizeof (dg), &dwResult, NULL); if (bResult && (dwResult == sizeof (dg)) && dg.diskGeometry.BytesPerSector) { - memcpy (diskGeometry, &dg.diskGeometry, sizeof (DISK_GEOMETRY)); + ZeroMemory (diskGeometry, sizeof (PDISK_GEOMETRY_EX)); + memcpy (&diskGeometry->Geometry, &dg.diskGeometry, sizeof (DISK_GEOMETRY)); + diskGeometry->DiskSize.QuadPart = dg.DiskSize.QuadPart; return TRUE; } else return FALSE; } -BOOL GetPhysicalDriveGeometry (int driveNumber, PDISK_GEOMETRY diskGeometry) +BOOL GetPhysicalDriveGeometry (int driveNumber, PDISK_GEOMETRY_EX diskGeometry) { HANDLE hDev; BOOL bResult = FALSE; @@ -8210,11 +8219,11 @@ BOOL GetPhysicalDriveGeometry (int driveNumber, PDISK_GEOMETRY diskGeometry) { DWORD bytesRead = 0; - ZeroMemory (diskGeometry, sizeof (DISK_GEOMETRY)); + ZeroMemory (diskGeometry, sizeof (DISK_GEOMETRY_EX)); - if ( DeviceIoControl (hDev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, diskGeometry, sizeof (DISK_GEOMETRY), &bytesRead, NULL) - && (bytesRead == sizeof (DISK_GEOMETRY)) - && diskGeometry->BytesPerSector) + if ( DeviceIoControl (hDev, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, diskGeometry, sizeof (DISK_GEOMETRY_EX), &bytesRead, NULL) + && (bytesRead == sizeof (DISK_GEOMETRY_EX)) + && diskGeometry->Geometry.BytesPerSector) { bResult = TRUE; } @@ -8224,7 +8233,7 @@ BOOL GetPhysicalDriveGeometry (int driveNumber, PDISK_GEOMETRY diskGeometry) return bResult; } - +#endif // Returns drive letter number assigned to device (-1 if none) int GetDiskDeviceDriveLetter (PWSTR deviceName) @@ -10534,7 +10543,7 @@ int OpenVolume (OpenVolumeContext *context, const wchar_t *volumePath, Password char buffer[TC_VOLUME_HEADER_EFFECTIVE_SIZE]; LARGE_INTEGER headerOffset; DWORD dwResult; - DISK_GEOMETRY deviceGeometry; + DISK_GEOMETRY_EX deviceGeometry; context->VolumeIsOpen = FALSE; context->CryptoInfo = NULL; @@ -10602,15 +10611,15 @@ int OpenVolume (OpenVolumeContext *context, const wchar_t *volumePath, Password } else { - DISK_GEOMETRY driveInfo; + DISK_GEOMETRY_EX driveInfo; - if (!DeviceIoControl (context->HostFileHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &driveInfo, sizeof (driveInfo), &dwResult, NULL)) + if (!DeviceIoControl (context->HostFileHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &driveInfo, sizeof (driveInfo), &dwResult, NULL)) { status = ERR_OS_ERROR; goto error; } - context->HostSize = driveInfo.Cylinders.QuadPart * driveInfo.BytesPerSector * driveInfo.SectorsPerTrack * driveInfo.TracksPerCylinder; + context->HostSize = driveInfo.DiskSize.QuadPart; } if (context->HostSize == 0) @@ -10798,10 +10807,10 @@ BOOL IsPagingFileActive (BOOL checkNonWindowsPartitionsOnly) if (handle == INVALID_HANDLE_VALUE) continue; - DISK_GEOMETRY driveInfo; + DISK_GEOMETRY_EX driveInfo; DWORD dwResult; - if (!DeviceIoControl (handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &driveInfo, sizeof (driveInfo), &dwResult, NULL)) + if (!DeviceIoControl (handle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &driveInfo, sizeof (driveInfo), &dwResult, NULL)) { CloseHandle (handle); continue; @@ -11398,8 +11407,6 @@ BOOL InitSecurityTokenLibrary (HWND hwndDlg) return TRUE; } -#endif // !SETUP - std::vector GetAvailableHostDevices (bool noDeviceProperties, bool singleList, bool noFloppy, bool detectUnencryptedFilesystems) { vector devices; @@ -11436,14 +11443,13 @@ std::vector GetAvailableHostDevices (bool noDeviceProperties, bool } else { - // retrieve size using DISK_GEOMETRY - DISK_GEOMETRY deviceGeometry = {0}; + // retrieve size using DISK_GEOMETRY_EX + DISK_GEOMETRY_EX deviceGeometry = {0}; if ( GetDriveGeometry (devPath, &deviceGeometry) || ((partNumber == 0) && GetPhysicalDriveGeometry (devNumber, &deviceGeometry)) ) { - device.Size = deviceGeometry.Cylinders.QuadPart * (LONGLONG) deviceGeometry.BytesPerSector - * (LONGLONG) deviceGeometry.SectorsPerTrack * (LONGLONG) deviceGeometry.TracksPerCylinder; + device.Size = (uint64) deviceGeometry.DiskSize.QuadPart; } } @@ -11451,7 +11457,7 @@ std::vector GetAvailableHostDevices (bool noDeviceProperties, bool if (!noDeviceProperties) { - DISK_GEOMETRY geometry; + DISK_GEOMETRY_EX geometry; int driveNumber = GetDiskDeviceDriveLetter ((wchar_t *) devPathStr.c_str()); @@ -11469,7 +11475,7 @@ std::vector GetAvailableHostDevices (bool noDeviceProperties, bool } if (partNumber == 0 && GetDriveGeometry (devPath, &geometry)) - device.Removable = (geometry.MediaType == RemovableMedia); + device.Removable = (geometry.Geometry.MediaType == RemovableMedia); } if (partNumber == 0) @@ -11601,6 +11607,8 @@ wstring FindDeviceByVolumeID (const BYTE volumeID [VOLUME_ID_SIZE]) return L""; } +#endif // !SETUP + BOOL FileHasReadOnlyAttribute (const wchar_t *path) { DWORD attributes = GetFileAttributes (path); @@ -12207,6 +12215,8 @@ BOOL IsRepeatedByteArray (byte value, const byte* buffer, size_t bufferSize) return FALSE; } +#ifndef SETUP + BOOL TranslateVolumeID (HWND hwndDlg, wchar_t* pathValue, size_t cchPathValue) { BOOL bRet = TRUE; @@ -12243,6 +12253,8 @@ BOOL TranslateVolumeID (HWND hwndDlg, wchar_t* pathValue, size_t cchPathValue) return bRet; } +#endif + BOOL CopyTextToClipboard (LPCWSTR txtValue) { size_t txtLen = wcslen(txtValue); diff --git a/src/Common/Dlgcode.h b/src/Common/Dlgcode.h index d902d3cc..be04bd39 100644 --- a/src/Common/Dlgcode.h +++ b/src/Common/Dlgcode.h @@ -463,7 +463,7 @@ BOOL SelectMultipleFilesNext (wchar_t *lpszFileName, size_t cbFileName); void OpenOnlineHelp (); BOOL GetPartitionInfo (const wchar_t *deviceName, PPARTITION_INFORMATION rpartInfo); BOOL GetDeviceInfo (const wchar_t *deviceName, DISK_PARTITION_INFO_STRUCT *info); -BOOL GetDriveGeometry (const wchar_t *deviceName, PDISK_GEOMETRY diskGeometry); +BOOL GetDriveGeometry (const wchar_t *deviceName, PDISK_GEOMETRY_EX diskGeometry); BOOL GetPhysicalDriveGeometry (int driveNumber, PDISK_GEOMETRY diskGeometry); BOOL IsVolumeDeviceHosted (const wchar_t *lpszDiskFile); int CompensateXDPI (int val); diff --git a/src/Common/Password.c b/src/Common/Password.c index 1c9083a3..b51870f9 100644 --- a/src/Common/Password.c +++ b/src/Common/Password.c @@ -186,7 +186,7 @@ int ChangePwd (const wchar_t *lpszVolume, Password *oldPassword, int old_pkcs5, BOOL bTimeStampValid = FALSE; LARGE_INTEGER headerOffset; BOOL backupHeader; - DISK_GEOMETRY driveInfo; + DISK_GEOMETRY_EX driveInfo; if (oldPassword->Length == 0 || newPassword->Length == 0) return -1; @@ -239,7 +239,7 @@ int ChangePwd (const wchar_t *lpszVolume, Password *oldPassword, int old_pkcs5, DWORD dwResult; BOOL bResult; - bResult = DeviceIoControl (dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, + bResult = DeviceIoControl (dev, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &driveInfo, sizeof (driveInfo), &dwResult, NULL); if (!bResult) @@ -253,8 +253,7 @@ int ChangePwd (const wchar_t *lpszVolume, Password *oldPassword, int old_pkcs5, } else { - hostSize = driveInfo.Cylinders.QuadPart * driveInfo.BytesPerSector * - driveInfo.SectorsPerTrack * driveInfo.TracksPerCylinder; + hostSize = driveInfo.DiskSize.QuadPart; } if (hostSize == 0) diff --git a/src/Common/Volumes.c b/src/Common/Volumes.c index d2321769..762adc2b 100644 --- a/src/Common/Volumes.c +++ b/src/Common/Volumes.c @@ -1155,21 +1155,21 @@ BOOL ReadEffectiveVolumeHeader (BOOL device, HANDLE fileHandle, byte *header, DW #endif byte sectorBuffer[TC_MAX_VOLUME_SECTOR_SIZE]; - DISK_GEOMETRY geometry; + DISK_GEOMETRY_EX geometry; if (!device) return ReadFile (fileHandle, header, TC_VOLUME_HEADER_EFFECTIVE_SIZE, bytesRead, NULL); - if (!DeviceIoControl (fileHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geometry, sizeof (geometry), bytesRead, NULL)) + if (!DeviceIoControl (fileHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &geometry, sizeof (geometry), bytesRead, NULL)) return FALSE; - if (geometry.BytesPerSector > sizeof (sectorBuffer) || geometry.BytesPerSector < TC_MIN_VOLUME_SECTOR_SIZE) + if (geometry.Geometry.BytesPerSector > sizeof (sectorBuffer) || geometry.Geometry.BytesPerSector < TC_MIN_VOLUME_SECTOR_SIZE) { SetLastError (ERROR_INVALID_PARAMETER); return FALSE; } - if (!ReadFile (fileHandle, sectorBuffer, max (TC_VOLUME_HEADER_EFFECTIVE_SIZE, geometry.BytesPerSector), bytesRead, NULL)) + if (!ReadFile (fileHandle, sectorBuffer, max (TC_VOLUME_HEADER_EFFECTIVE_SIZE, geometry.Geometry.BytesPerSector), bytesRead, NULL)) return FALSE; memcpy (header, sectorBuffer, min (*bytesRead, TC_VOLUME_HEADER_EFFECTIVE_SIZE)); @@ -1189,7 +1189,7 @@ BOOL WriteEffectiveVolumeHeader (BOOL device, HANDLE fileHandle, byte *header) byte sectorBuffer[TC_MAX_VOLUME_SECTOR_SIZE]; DWORD bytesDone; - DISK_GEOMETRY geometry; + DISK_GEOMETRY_EX geometry; if (!device) { @@ -1205,23 +1205,23 @@ BOOL WriteEffectiveVolumeHeader (BOOL device, HANDLE fileHandle, byte *header) return TRUE; } - if (!DeviceIoControl (fileHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geometry, sizeof (geometry), &bytesDone, NULL)) + if (!DeviceIoControl (fileHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &geometry, sizeof (geometry), &bytesDone, NULL)) return FALSE; - if (geometry.BytesPerSector > sizeof (sectorBuffer) || geometry.BytesPerSector < TC_MIN_VOLUME_SECTOR_SIZE) + if (geometry.Geometry.BytesPerSector > sizeof (sectorBuffer) || geometry.Geometry.BytesPerSector < TC_MIN_VOLUME_SECTOR_SIZE) { SetLastError (ERROR_INVALID_PARAMETER); return FALSE; } - if (geometry.BytesPerSector != TC_VOLUME_HEADER_EFFECTIVE_SIZE) + if (geometry.Geometry.BytesPerSector != TC_VOLUME_HEADER_EFFECTIVE_SIZE) { LARGE_INTEGER seekOffset; - if (!ReadFile (fileHandle, sectorBuffer, geometry.BytesPerSector, &bytesDone, NULL)) + if (!ReadFile (fileHandle, sectorBuffer, geometry.Geometry.BytesPerSector, &bytesDone, NULL)) return FALSE; - if (bytesDone != geometry.BytesPerSector) + if (bytesDone != geometry.Geometry.BytesPerSector) { SetLastError (ERROR_INVALID_PARAMETER); return FALSE; @@ -1234,10 +1234,10 @@ BOOL WriteEffectiveVolumeHeader (BOOL device, HANDLE fileHandle, byte *header) memcpy (sectorBuffer, header, TC_VOLUME_HEADER_EFFECTIVE_SIZE); - if (!WriteFile (fileHandle, sectorBuffer, geometry.BytesPerSector, &bytesDone, NULL)) + if (!WriteFile (fileHandle, sectorBuffer, geometry.Geometry.BytesPerSector, &bytesDone, NULL)) return FALSE; - if (bytesDone != geometry.BytesPerSector) + if (bytesDone != geometry.Geometry.BytesPerSector) { SetLastError (ERROR_INVALID_PARAMETER); return FALSE; -- cgit v1.2.3