Skip to content

Commit f85bf3b

Browse files
committed
api: check netsetup2 key when trying to determine if GUID is duplicated
Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 0d96b90 commit f85bf3b

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

api/adapter.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,7 @@ static _Return_type_success_(return != NULL) WINTUN_ADAPTER *CreateAdapter(
14271427

14281428
if (RequestedGUID)
14291429
{
1430+
WCHAR *NetworkInstanceId = NULL, *NetSetupInstanceId = NULL;
14301431
WCHAR RegPath[MAX_REG_PATH];
14311432
WCHAR RequestedGUIDStr[MAX_GUID_STRING_LEN];
14321433
int GuidStrLen = StringFromGUID2(RequestedGUID, RequestedGUIDStr, _countof(RequestedGUIDStr)) * sizeof(WCHAR);
@@ -1441,32 +1442,48 @@ static _Return_type_success_(return != NULL) WINTUN_ADAPTER *CreateAdapter(
14411442
HKEY Key;
14421443
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegPath, 0, KEY_QUERY_VALUE, &Key) != ERROR_SUCCESS)
14431444
goto guidIsFresh;
1444-
WCHAR *InstanceID = RegistryQueryString(Key, L"PnPInstanceId", FALSE);
1445+
NetworkInstanceId = RegistryQueryString(Key, L"PnPInstanceId", FALSE);
14451446
RegCloseKey(Key);
1446-
if (!InstanceID)
1447+
if (!NetworkInstanceId)
14471448
goto guidIsFresh;
1448-
int Ret = _snwprintf_s(RegPath, MAX_REG_PATH, _TRUNCATE, L"SYSTEM\\CurrentControlSet\\Enum\\%s", InstanceID);
1449-
Free(InstanceID);
1449+
int Ret = _snwprintf_s(RegPath, MAX_REG_PATH, _TRUNCATE, L"SYSTEM\\CurrentControlSet\\Enum\\%s", NetworkInstanceId);
14501450
if (Ret == -1)
14511451
goto guidIsFresh;
1452+
BOOL MightExist = FALSE;
14521453
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegPath, 0, KEY_QUERY_VALUE, &Key) == ERROR_SUCCESS)
14531454
{
14541455
RegCloseKey(Key);
1455-
NET_LUID Luid;
1456-
if (ConvertInterfaceGuidToLuid(RequestedGUID, &Luid) == NO_ERROR)
1457-
{
1458-
SetLastError(
1459-
LOG_ERROR(ERROR_ALREADY_EXISTS, L"Requested GUID is already in use: %s", RequestedGUIDStr));
1460-
return NULL;
1461-
}
1456+
MightExist = TRUE;
14621457
}
1463-
LOG(WINTUN_LOG_WARN, L"Requested GUID %s has leftover residue", RequestedGUIDStr);
1458+
if (MightExist && _snwprintf_s(
1459+
RegPath,
1460+
MAX_REG_PATH,
1461+
_TRUNCATE,
1462+
L"SYSTEM\\CurrentControlSet\\Control\\NetworkSetup2\\Interfaces\\%.*s\\Properties\\{a111f1f4-5923-47c0-9a68-d0bafb577901}\\0032",
1463+
GuidStrLen,
1464+
RequestedGUIDStr) == -1)
1465+
goto guidIsFresh;
14641466
HANDLE OriginalToken;
14651467
if (!ImpersonateService(L"NetSetupSvc", &OriginalToken))
14661468
{
14671469
LOG_LAST_ERROR(L"Unable to impersonate NetSetupSvc");
14681470
goto guidIsFresh; // non-fatal
14691471
}
1472+
if (MightExist && RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegPath, 0, KEY_QUERY_VALUE, &Key) == ERROR_SUCCESS)
1473+
{
1474+
NetSetupInstanceId = RegistryQueryString(Key, NULL, FALSE);
1475+
RegCloseKey(Key);
1476+
if (NetSetupInstanceId && !_wcsicmp(NetSetupInstanceId, NetworkInstanceId))
1477+
{
1478+
Free(NetSetupInstanceId);
1479+
Free(NetworkInstanceId);
1480+
RestoreToken(OriginalToken);
1481+
SetLastError(
1482+
LOG_ERROR(ERROR_ALREADY_EXISTS, L"Requested GUID is already in use: %s", RequestedGUIDStr));
1483+
return NULL;
1484+
}
1485+
}
1486+
LOG(WINTUN_LOG_WARN, L"Requested GUID %s has leftover residue", RequestedGUIDStr);
14701487
if (_snwprintf_s(
14711488
RegPath,
14721489
MAX_REG_PATH,
@@ -1477,7 +1494,9 @@ static _Return_type_success_(return != NULL) WINTUN_ADAPTER *CreateAdapter(
14771494
!RegistryDeleteKeyRecursive(HKEY_LOCAL_MACHINE, RegPath))
14781495
LOG_LAST_ERROR(L"Unable to delete NetworkSetup2 registry key"); // non-fatal
14791496
RestoreToken(OriginalToken);
1480-
guidIsFresh:;
1497+
guidIsFresh:
1498+
Free(NetworkInstanceId);
1499+
Free(NetSetupInstanceId);
14811500
}
14821501

14831502
HDEVINFO DevInfo = SetupDiCreateDeviceInfoListExW(&GUID_DEVCLASS_NET, NULL, NULL, NULL);

api/registry.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ _Return_type_success_(
237237
return NULL;
238238
switch (ValueType)
239239
{
240+
case 0xffff0012:
240241
case REG_SZ:
241242
case REG_EXPAND_SZ:
242243
case REG_MULTI_SZ:

0 commit comments

Comments
 (0)