diff options
Diffstat (limited to 'src/Format/InPlace.c')
-rw-r--r-- | src/Format/InPlace.c | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/Format/InPlace.c b/src/Format/InPlace.c index c3d09fb3..228e2e5f 100644 --- a/src/Format/InPlace.c +++ b/src/Format/InPlace.c @@ -17,40 +17,42 @@ IMPORTANT: Due to this issue, functions in this file must not directly interact # define _WIN32_WINNT 0x0600
#endif
#include <stdlib.h>
#include <string.h>
#include <string>
#include "Tcdefs.h"
#include "Platform/Finally.h"
#include "Common.h"
#include "Crc.h"
#include "Dlgcode.h"
#include "Language.h"
#include "Tcformat.h"
#include "Volumes.h"
#include "InPlace.h"
+#include <Strsafe.h>
+
using namespace std;
using namespace VeraCrypt;
#define TC_MAX_NONSYS_INPLACE_ENC_WORK_CHUNK_SIZE (2048 * BYTES_PER_KB)
#define TC_INITIAL_NTFS_CONCEAL_PORTION_SIZE (2 * TC_MAX_VOLUME_SECTOR_SIZE)
#define TC_NTFS_CONCEAL_CONSTANT 0xFF
#define TC_NONSYS_INPLACE_ENC_HEADER_UPDATE_INTERVAL (64 * BYTES_PER_MB)
#define TC_NONSYS_INPLACE_ENC_MIN_VOL_SIZE (TC_TOTAL_VOLUME_HEADERS_SIZE + TC_MIN_NTFS_FS_SIZE * 2)
// If the returned value is greater than 0, it is the desired volume size in NTFS sectors (not in bytes)
// after shrinking has been performed. If there's any error, returns -1.
static __int64 NewFileSysSizeAfterShrink (HANDLE dev, const char *devicePath, int64 *totalClusterCount, DWORD *bytesPerCluster, BOOL silent)
{
NTFS_VOLUME_DATA_BUFFER ntfsVolData;
DWORD nBytesReturned;
__int64 fileSysSize, desiredNbrSectors;
// Filesystem size and sector size
@@ -134,49 +136,49 @@ BOOL CheckRequirementsForNonSysInPlaceEnc (const char *devicePath, BOOL silent) }
/* Admin rights */
if (!IsAdmin())
{
// We rely on the wizard process to call us only when the whole wizard process has been elevated (so UAC
// status can be ignored). In case the IsAdmin() detection somehow fails, we allow the user to continue.
if (!silent)
Warning ("ADMIN_PRIVILEGES_WARN_DEVICES");
}
/* ---------- Checks that may require admin rights ----------- */
/* Access to the partition */
- strcpy ((char *) devPath, devicePath);
- ToUNICODE ((char *) devPath);
+ StringCbCopyA ((char *) devPath, sizeof(devPath), devicePath);
+ ToUNICODE ((char *) devPath, sizeof(devPath));
driveLetterNo = GetDiskDeviceDriveLetter (devPath);
if (driveLetterNo >= 0)
szRootPath[0] = (char) driveLetterNo + 'A';
- if (FakeDosNameForDevice (devicePath, dosDev, devName, FALSE) != 0)
+ if (FakeDosNameForDevice (devicePath, dosDev, sizeof(dosDev), devName, sizeof(devName),FALSE) != 0)
{
if (!silent)
{
handleWin32Error (MainDlg);
Error ("INPLACE_ENC_CANT_ACCESS_OR_GET_INFO_ON_VOL");
}
return FALSE;
}
dev = OpenPartitionVolume (devName,
FALSE, // Do not require exclusive access
TRUE, // Require shared access (must be TRUE; otherwise, volume properties will not be possible to obtain)
FALSE, // Do not ask the user to confirm shared access (if exclusive fails)
FALSE, // Do not append alternative instructions how to encrypt the data (to applicable error messages)
silent); // Silent mode
if (dev == INVALID_HANDLE_VALUE)
return FALSE;
@@ -331,47 +333,47 @@ int EncryptPartitionInPlaceBegin (volatile FORMAT_VOL_PARAMETERS *volParams, vol VirtualLock (header, TC_VOLUME_HEADER_EFFECTIVE_SIZE);
deviceSize = GetDeviceSize (volParams->volumePath);
if (deviceSize < 0)
{
// Cannot determine the size of the partition
nStatus = ERR_PARAMETER_INCORRECT;
goto closing_seq;
}
if (deviceSize < TC_NONSYS_INPLACE_ENC_MIN_VOL_SIZE)
{
ShowInPlaceEncErrMsgWAltSteps ("PARTITION_TOO_SMALL_FOR_NONSYS_INPLACE_ENC", TRUE);
nStatus = ERR_DONT_REPORT;
goto closing_seq;
}
dataAreaSize = GetVolumeDataAreaSize (volParams->hiddenVol, deviceSize);
- strcpy ((char *)deviceName, volParams->volumePath);
- ToUNICODE ((char *)deviceName);
+ StringCbCopyA ((char *)deviceName, sizeof(deviceName), volParams->volumePath);
+ ToUNICODE ((char *)deviceName, sizeof(deviceName));
driveLetter = GetDiskDeviceDriveLetter (deviceName);
- if (FakeDosNameForDevice (volParams->volumePath, dosDev, devName, FALSE) != 0)
+ if (FakeDosNameForDevice (volParams->volumePath, dosDev, sizeof(dosDev),devName, sizeof(devName),FALSE) != 0)
{
nStatus = ERR_OS_ERROR;
goto closing_seq;
}
if (IsDeviceMounted (devName))
{
dev = OpenPartitionVolume (devName,
FALSE, // Do not require exclusive access (must be FALSE; otherwise, it will not be possible to dismount the volume or obtain its properties and FSCTL_ALLOW_EXTENDED_DASD_IO will fail too)
TRUE, // Require shared access (must be TRUE; otherwise, it will not be possible to dismount the volume or obtain its properties and FSCTL_ALLOW_EXTENDED_DASD_IO will fail too)
FALSE, // Do not ask the user to confirm shared access (if exclusive fails)
FALSE, // Do not append alternative instructions how to encrypt the data (to applicable error messages)
FALSE); // Non-silent mode
if (dev == INVALID_HANDLE_VALUE)
{
nStatus = ERR_DONT_REPORT;
goto closing_seq;
}
}
@@ -693,44 +695,44 @@ int EncryptPartitionInPlaceResume (HANDLE dev, }
headerCryptoInfo = crypto_open();
if (headerCryptoInfo == NULL)
{
nStatus = ERR_OUTOFMEMORY;
goto closing_seq;
}
deviceSize = GetDeviceSize (devicePath);
if (deviceSize < 0)
{
// Cannot determine the size of the partition
nStatus = ERR_OS_ERROR;
goto closing_seq;
}
if (dev == INVALID_HANDLE_VALUE)
{
- strcpy ((char *)deviceName, devicePath);
- ToUNICODE ((char *)deviceName);
+ StringCbCopyA ((char *)deviceName, sizeof(deviceName), devicePath);
+ ToUNICODE ((char *)deviceName, sizeof(deviceName));
- if (FakeDosNameForDevice (devicePath, dosDev, devName, FALSE) != 0)
+ if (FakeDosNameForDevice (devicePath, dosDev, sizeof(dosDev),devName, sizeof(devName),FALSE) != 0)
{
nStatus = ERR_OS_ERROR;
goto closing_seq;
}
dev = OpenPartitionVolume (devName,
FALSE, // Do not require exclusive access
FALSE, // Do not require shared access
TRUE, // Ask the user to confirm shared access (if exclusive fails)
FALSE, // Do not append alternative instructions how to encrypt the data (to applicable error messages)
FALSE); // Non-silent mode
if (dev == INVALID_HANDLE_VALUE)
{
nStatus = ERR_DONT_REPORT;
goto closing_seq;
}
}
// This should never be needed, but is still performed for extra safety (without checking the result)
@@ -1068,43 +1070,43 @@ closing_seq: }
if (buf != NULL)
TCfree (buf);
if (header != NULL)
{
burn (header, TC_VOLUME_HEADER_EFFECTIVE_SIZE);
VirtualUnlock (header, TC_VOLUME_HEADER_EFFECTIVE_SIZE);
TCfree (header);
}
if (wipeBuffer != NULL)
TCfree (wipeBuffer);
if (zeroedSectorCount > 0)
{
wchar_t msg[30000] = {0};
wchar_t sizeStr[500] = {0};
- GetSizeString (zeroedSectorCount * sectorSize, sizeStr);
+ GetSizeString (zeroedSectorCount * sectorSize, sizeStr, sizeof(sizeStr));
- wsprintfW (msg,
+ StringCbPrintfW (msg, sizeof(msg),
GetString ("ZEROED_BAD_SECTOR_COUNT"),
zeroedSectorCount,
sizeStr);
WarningDirect (msg);
}
if (nStatus != ERR_SUCCESS && nStatus != ERR_USER_ABORT)
SetLastError (dwError);
return nStatus;
}
int FastVolumeHeaderUpdate (HANDLE dev, CRYPTO_INFO *headerCryptoInfo, CRYPTO_INFO *masterCryptoInfo, __int64 deviceSize)
{
LARGE_INTEGER offset;
DWORD n;
int nStatus = ERR_SUCCESS;
byte *header;
@@ -1352,44 +1354,44 @@ static int ConcealNTFS (HANDLE dev) do
{
Sleep (1);
}
while (SetFilePointerEx (dev, offset, NULL, FILE_BEGIN) == 0
|| WriteFile (dev, buf, TC_INITIAL_NTFS_CONCEAL_PORTION_SIZE, &nbrBytesProcessed2, NULL) == 0);
SetLastError (dwError);
return ERR_OS_ERROR;
}
return ERR_SUCCESS;
}
void ShowInPlaceEncErrMsgWAltSteps (char *iniStrId, BOOL bErr)
{
wchar_t msg[30000];
- wcscpy (msg, GetString (iniStrId));
+ StringCbCopyW (msg, sizeof(msg), GetString (iniStrId));
- wcscat (msg, L"\n\n\n");
- wcscat (msg, GetString ("INPLACE_ENC_ALTERNATIVE_STEPS"));
+ StringCbCatW (msg, sizeof(msg), L"\n\n\n");
+ StringCbCatW (msg, sizeof(msg), GetString ("INPLACE_ENC_ALTERNATIVE_STEPS"));
if (bErr)
ErrorDirect (msg);
else
WarningDirect (msg);
}
static void ExportProgressStats (__int64 bytesDone, __int64 totalSize)
{
NonSysInplaceEncBytesDone = bytesDone;
NonSysInplaceEncTotalSize = totalSize;
}
void SetNonSysInplaceEncUIStatus (int nonSysInplaceEncStatus)
{
NonSysInplaceEncStatus = nonSysInplaceEncStatus;
}
@@ -1397,50 +1399,50 @@ void SetNonSysInplaceEncUIStatus (int nonSysInplaceEncStatus) BOOL SaveNonSysInPlaceEncSettings (int delta, WipeAlgorithmId newWipeAlgorithm)
{
int count;
char str[32];
WipeAlgorithmId savedWipeAlgorithm = TC_WIPE_NONE;
if (delta == 0)
return TRUE;
count = LoadNonSysInPlaceEncSettings (&savedWipeAlgorithm) + delta;
if (count < 1)
{
RemoveNonSysInPlaceEncNotifications();
return TRUE;
}
else
{
if (newWipeAlgorithm != TC_WIPE_NONE)
{
- sprintf (str, "%d", (int) newWipeAlgorithm);
+ StringCbPrintfA (str, sizeof(str), "%d", (int) newWipeAlgorithm);
SaveBufferToFile (str, GetConfigPath (TC_APPD_FILENAME_NONSYS_INPLACE_ENC_WIPE), strlen(str), FALSE);
}
else if (FileExists (GetConfigPath (TC_APPD_FILENAME_NONSYS_INPLACE_ENC_WIPE)))
{
remove (GetConfigPath (TC_APPD_FILENAME_NONSYS_INPLACE_ENC_WIPE));
}
- sprintf (str, "%d", count);
+ StringCbPrintfA (str, sizeof(str), "%d", count);
return SaveBufferToFile (str, GetConfigPath (TC_APPD_FILENAME_NONSYS_INPLACE_ENC), strlen(str), FALSE);
}
}
// Repairs damaged sectors (i.e. those with read errors) by zeroing them.
// Note that this operating fails if there are any write errors.
int ZeroUnreadableSectors (HANDLE dev, LARGE_INTEGER startOffset, int64 size, int sectorSize, uint64 *zeroedSectorCount)
{
int nStatus;
DWORD n;
int64 sectorCount;
LARGE_INTEGER workOffset;
byte *sectorBuffer = NULL;
DWORD dwError;
workOffset.QuadPart = startOffset.QuadPart;
sectorBuffer = (byte *) TCalloc (sectorSize);
|