From ba733dd032a44eb38653abe922fc6905413bcac4 Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Mon, 14 Jul 2014 17:20:32 +0200 Subject: Use Safe String functions in Registry.c and add a unicode version of WriteLocalMachineRegistryDword function to avoid doing conversions when used. --- src/Common/Registry.c | 33 +++++++++++++++++++++++++++++---- src/Common/Registry.h | 3 ++- 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Common/Registry.c b/src/Common/Registry.c index c7496471..a68e5f18 100644 --- a/src/Common/Registry.c +++ b/src/Common/Registry.c @@ -8,6 +8,7 @@ #include "Tcdefs.h" #include "Registry.h" +#include BOOL ReadLocalMachineRegistryDword (char *subKey, char *name, DWORD *value) { @@ -105,13 +106,13 @@ char *ReadRegistryString (char *subKey, char *name, char *defaultValue, char *st DWORD size = sizeof (value); str[maxLen-1] = 0; - strncpy (str, defaultValue, maxLen-1); + StringCbCopyA (str, maxLen, defaultValue); ZeroMemory (value, sizeof value); if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey, 0, KEY_READ, &hkey) == ERROR_SUCCESS) if (RegQueryValueEx (hkey, name, 0, 0, (LPBYTE) value, &size) == ERROR_SUCCESS) - strncpy (str, value, maxLen-1); + StringCbCopyA (str, maxLen,value); RegCloseKey (hkey); return str; @@ -169,6 +170,30 @@ BOOL WriteLocalMachineRegistryDword (char *subKey, char *name, DWORD value) return TRUE; } +BOOL WriteLocalMachineRegistryDwordW (WCHAR *subKey, WCHAR *name, DWORD value) +{ + HKEY hkey = 0; + DWORD disp; + LONG status; + + if ((status = RegCreateKeyExW (HKEY_LOCAL_MACHINE, subKey, + 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp)) != ERROR_SUCCESS) + { + SetLastError (status); + return FALSE; + } + + if ((status = RegSetValueExW (hkey, name, 0, REG_DWORD, (BYTE *) &value, sizeof value)) != ERROR_SUCCESS) + { + RegCloseKey (hkey); + SetLastError (status); + return FALSE; + } + + RegCloseKey (hkey); + return TRUE; +} + BOOL WriteLocalMachineRegistryMultiString (char *subKey, char *name, char *multiString, DWORD size) { HKEY hkey = 0; @@ -279,9 +304,9 @@ void DeleteRegistryValue (char *subKey, char *name) } -void GetStartupRegKeyName (char *regk) +void GetStartupRegKeyName (char *regk, size_t cbRegk) { // The string is split in order to prevent some antivirus packages from falsely reporting // TrueCrypt.exe to contain a possible Trojan horse because of this string (heuristic scan). - sprintf (regk, "%s%s", "Software\\Microsoft\\Windows\\Curren", "tVersion\\Run"); + StringCbPrintfA (regk, cbRegk,"%s%s", "Software\\Microsoft\\Windows\\Curren", "tVersion\\Run"); } diff --git a/src/Common/Registry.h b/src/Common/Registry.h index a0e2e732..693ddc0e 100644 --- a/src/Common/Registry.h +++ b/src/Common/Registry.h @@ -19,13 +19,14 @@ char *ReadRegistryString (char *subKey, char *name, char *defaultValue, char *st DWORD ReadRegistryBytes (char *path, char *name, char *value, int maxLen); void WriteRegistryInt (char *subKey, char *name, int value); BOOL WriteLocalMachineRegistryDword (char *subKey, char *name, DWORD value); +BOOL WriteLocalMachineRegistryDwordW (WCHAR *subKey, WCHAR *name, DWORD value); BOOL WriteLocalMachineRegistryMultiString (char *subKey, char *name, char *multiString, DWORD size); BOOL WriteLocalMachineRegistryString (char *subKey, char *name, char *str, BOOL expandable); void WriteRegistryString (char *subKey, char *name, char *str); BOOL WriteRegistryBytes (char *path, char *name, char *str, DWORD size); BOOL DeleteLocalMachineRegistryKey (char *parentKey, char *subKeyToDelete); void DeleteRegistryValue (char *subKey, char *name); -void GetStartupRegKeyName (char *regk); +void GetStartupRegKeyName (char *regk, size_t cbRegk); #ifdef __cplusplus } -- cgit v1.2.3