api: use proper instance id bounds

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-10-14 06:37:50 +00:00
parent 80aab77c19
commit 470bdf3e26
5 changed files with 9 additions and 9 deletions

View File

@ -523,7 +523,7 @@ DeviceCreateCallback(
SW_DEVICE_CREATE_CTX *Ctx = Context; SW_DEVICE_CREATE_CTX *Ctx = Context;
Ctx->CreateResult = CreateResult; Ctx->CreateResult = CreateResult;
if (DeviceInstanceId) if (DeviceInstanceId)
wcsncpy_s(Ctx->DeviceInstanceId, MAX_INSTANCE_ID, DeviceInstanceId, _TRUNCATE); wcsncpy_s(Ctx->DeviceInstanceId, MAX_DEVICE_ID_LEN, DeviceInstanceId, _TRUNCATE);
SetEvent(Ctx->Triggered); SetEvent(Ctx->Triggered);
} }

View File

@ -8,9 +8,9 @@
#include "wintun.h" #include "wintun.h"
#include <IPExport.h> #include <IPExport.h>
#include <SetupAPI.h> #include <SetupAPI.h>
#include <cfgmgr32.h>
#include <Windows.h> #include <Windows.h>
#define MAX_INSTANCE_ID MAX_PATH /* TODO: Is MAX_PATH always enough? */
#define WINTUN_HWID L"Wintun" #define WINTUN_HWID L"Wintun"
#define WINTUN_ENUMERATOR (IsWindows7 ? L"ROOT\\" WINTUN_HWID : L"SWD\\" WINTUN_HWID) #define WINTUN_ENUMERATOR (IsWindows7 ? L"ROOT\\" WINTUN_HWID : L"SWD\\" WINTUN_HWID)
@ -28,7 +28,7 @@ typedef struct _WINTUN_ADAPTER
SP_DEVINFO_DATA DevInfoData; SP_DEVINFO_DATA DevInfoData;
WCHAR *InterfaceFilename; WCHAR *InterfaceFilename;
GUID CfgInstanceID; GUID CfgInstanceID;
WCHAR DevInstanceID[MAX_INSTANCE_ID]; WCHAR DevInstanceID[MAX_DEVICE_ID_LEN];
DWORD LuidIndex; DWORD LuidIndex;
DWORD IfType; DWORD IfType;
DWORD IfIndex; DWORD IfIndex;

View File

@ -311,7 +311,7 @@ InvokeClassInstaller(_In_ LPCWSTR Action, _In_ LPCWSTR Function, _In_ HDEVINFO D
{ {
LOG(WINTUN_LOG_INFO, L"Spawning native process to %s instance", Action); LOG(WINTUN_LOG_INFO, L"Spawning native process to %s instance", Action);
WCHAR InstanceId[MAX_INSTANCE_ID]; WCHAR InstanceId[MAX_DEVICE_ID_LEN];
DWORD RequiredChars = _countof(InstanceId); DWORD RequiredChars = _countof(InstanceId);
if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, InstanceId, RequiredChars, &RequiredChars)) if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, InstanceId, RequiredChars, &RequiredChars))
{ {
@ -374,7 +374,7 @@ CreateInstanceWin7ViaRundll32(LPWSTR InstanceId)
LOG(WINTUN_LOG_INFO, L"Spawning native process to create instance"); LOG(WINTUN_LOG_INFO, L"Spawning native process to create instance");
DWORD LastError; DWORD LastError;
WCHAR Response[MAX_INSTANCE_ID + 1]; WCHAR Response[MAX_DEVICE_ID_LEN + 1];
if (!ExecuteRunDll32(L"CreateInstanceWin7", L"", Response, _countof(Response))) if (!ExecuteRunDll32(L"CreateInstanceWin7", L"", Response, _countof(Response)))
{ {
LastError = LOG_LAST_ERROR(L"Error executing worker process"); LastError = LOG_LAST_ERROR(L"Error executing worker process");
@ -389,7 +389,7 @@ CreateInstanceWin7ViaRundll32(LPWSTR InstanceId)
} }
LastError = wcstoul(Argv[0], NULL, 16); LastError = wcstoul(Argv[0], NULL, 16);
if (LastError == ERROR_SUCCESS) if (LastError == ERROR_SUCCESS)
wcsncpy_s(InstanceId, MAX_INSTANCE_ID, Argv[1], _TRUNCATE); wcsncpy_s(InstanceId, MAX_DEVICE_ID_LEN, Argv[1], _TRUNCATE);
cleanupArgv: cleanupArgv:
LocalFree(Argv); LocalFree(Argv);
cleanup: cleanup:

View File

@ -23,4 +23,4 @@ DisableInstanceViaRundll32(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevInfoD
_Return_type_success_(return != FALSE) _Return_type_success_(return != FALSE)
BOOL BOOL
CreateInstanceWin7ViaRundll32(_Out_writes_z_(MAX_INSTANCE_ID) LPWSTR InstanceId); CreateInstanceWin7ViaRundll32(_Out_writes_z_(MAX_DEVICE_ID_LEN) LPWSTR InstanceId);

View File

@ -4,8 +4,8 @@
*/ */
#include <devguid.h> #include <devguid.h>
#include <cfgmgr32.h>
#define MAX_INSTANCE_ID MAX_PATH /* TODO: Is MAX_PATH always enough? */
#define WINTUN_HWID L"Wintun" #define WINTUN_HWID L"Wintun"
VOID __stdcall CreateInstanceWin7(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) VOID __stdcall CreateInstanceWin7(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
@ -13,7 +13,7 @@ VOID __stdcall CreateInstanceWin7(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine,
#pragma EXPORT #pragma EXPORT
DWORD LastError = ERROR_SUCCESS; DWORD LastError = ERROR_SUCCESS;
WCHAR InstanceId[MAX_INSTANCE_ID] = { 0 }; WCHAR InstanceId[MAX_DEVICE_ID_LEN] = { 0 };
HDEVINFO DevInfo = SetupDiCreateDeviceInfoListExW(&GUID_DEVCLASS_NET, NULL, NULL, NULL); HDEVINFO DevInfo = SetupDiCreateDeviceInfoListExW(&GUID_DEVCLASS_NET, NULL, NULL, NULL);
if (DevInfo == INVALID_HANDLE_VALUE) if (DevInfo == INVALID_HANDLE_VALUE)