Skip to content

Commit

Permalink
[REGEDIT] Avoid buffer overflow in SelectNode. CORE-18602
Browse files Browse the repository at this point in the history
  • Loading branch information
ThFabba committed Mar 8, 2023
1 parent 910822b commit 1ee9ea4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions base/applications/regedit/regedit.h
Expand Up @@ -9,6 +9,7 @@
#include <stdio.h>
#include <aclapi.h>
#include <shellapi.h>
#include <strsafe.h>

#include "main.h"
#include "hexedit.h"
Expand Down
25 changes: 17 additions & 8 deletions base/applications/regedit/treeview.c
Expand Up @@ -781,7 +781,7 @@ BOOL SelectNode(HWND hwndTV, LPCWSTR keyPath)

/* Load "My Computer" string... */
LoadStringW(hInst, IDS_MY_COMPUTER, szBuffer, ARRAY_SIZE(szBuffer));
wcscat(szBuffer, L"\\");
StringCbCatW(szBuffer, sizeof(szBuffer), L"\\");

/* ... and remove it from the key path */
if (!_wcsnicmp(keyPath, szBuffer, wcslen(szBuffer)))
Expand All @@ -795,24 +795,33 @@ BOOL SelectNode(HWND hwndTV, LPCWSTR keyPath)

while(keyPath[0])
{
size_t copyLength;
s = wcschr(keyPath, L'\\');
lstrcpynW(szPathPart, keyPath, s ? s - keyPath + 1 : wcslen(keyPath) + 1);
if (s != NULL)
{
copyLength = (s - keyPath) * sizeof(WCHAR);
}
else
{
copyLength = sizeof(szPathPart);
}
StringCbCopyNW(szPathPart, sizeof(szPathPart), keyPath, copyLength);

/* Special case for root to expand root key abbreviations */
if (hItem == hRoot)
{
if (!_wcsicmp(szPathPart, L"HKCR"))
wcscpy(szPathPart, L"HKEY_CLASSES_ROOT");
StringCbCopyW(szPathPart, sizeof(szPathPart), L"HKEY_CLASSES_ROOT");
else if (!_wcsicmp(szPathPart, L"HKCU"))
wcscpy(szPathPart, L"HKEY_CURRENT_USER");
StringCbCopyW(szPathPart, sizeof(szPathPart), L"HKEY_CURRENT_USER");
else if (!_wcsicmp(szPathPart, L"HKLM"))
wcscpy(szPathPart, L"HKEY_LOCAL_MACHINE");
StringCbCopyW(szPathPart, sizeof(szPathPart), L"HKEY_LOCAL_MACHINE");
else if (!_wcsicmp(szPathPart, L"HKU"))
wcscpy(szPathPart, L"HKEY_USERS");
StringCbCopyW(szPathPart, sizeof(szPathPart), L"HKEY_USERS");
else if (!_wcsicmp(szPathPart, L"HKCC"))
wcscpy(szPathPart, L"HKEY_CURRENT_CONFIG");
StringCbCopyW(szPathPart, sizeof(szPathPart), L"HKEY_CURRENT_CONFIG");
else if (!_wcsicmp(szPathPart, L"HKDD"))
wcscpy(szPathPart, L"HKEY_DYN_DATA");
StringCbCopyW(szPathPart, sizeof(szPathPart), L"HKEY_DYN_DATA");
}

for (hChildItem = TreeView_GetChild(hwndTV, hItem); hChildItem;
Expand Down

0 comments on commit 1ee9ea4

Please sign in to comment.