Skip to content

Commit 005af4a

Browse files
committed
api: use SuggestedInstanceId instead of NetSetupAnticipatedInstanceId
All was well with NetSetupAnticipatedInstanceId, until a bug crept into recent Windows builds that caused old GUIDs not to be properly removed, resulting in subsequent adapter creations to fail, because NetSetup AnticipatedInstanceId considers it fatal when the target GUID already exists, even if in diminished form. The initial solution was to detect cruft, and then steal a TrustedInstaller token and sleuth around the registry cleaning things up. The horror! Uncomfortable with this, I reopened IDA and had a look around with fresh eyes, three years after the original discovery of NetSetupAnticipated InstanceId. There, I found some interesting behavior in NetSetupSvcDeviceManager::InstallNetworkInterfaces, which amounts to something like: if (IsSet("RetiredNetCfgInstanceId") { if (IsSet("NetSetupAnticipatedInstanceId") DeleteAdapter(GetValue("RetiredNetCfgInstanceId")); else Set("NetSetupAnticipatedInstanceId", GetValue("RetiredNetCfgInstanceId")); Delete("RetiredNetCfgInstanceId"); } CreateAdapter = TRUE; if (IsSet("NetSetupAnticipatedInstanceId")) { Guid = GetValue("NetSetupAnticipatedInstanceId"); if (AdapterAlreadyExists(Guid)) CreateAdapter = FALSE; else SetGuidOfNewAdapter(Guid); Delete("NetSetupAnticipatedInstanceId"); } else if (IsSet("SuggestedInstanceId")) { Guid = GetValue("SuggestedInstanceId"); if (!AdapterAlreadyExists(Guid)) SetGuidOfNewAdapter(Guid); Delete("SuggestedInstanceId"); } Thus, one appealing strategy would be to set both NetSetupAnticipated InstanceId and RetiredInstanceId to the same value, and let the service handle deleting the old one for us before creating the new one. However, the cleanup of the old adapter winds up being quasi- asynchronous, and thus we still wind up in the CreateAdapter = FALSE case. So, the remaining strategy is to simply use SuggestedInstanceId instead. This has the behavior that if there's an adapter already in use, it'll use a new random GUID. The result is that adapter creation won't fail. That's not great, but the docs have always made it clear that "requested" is a best-effort sort of thing. Plus, hopefully the creation of the new adapter will help nudge the bug a bit and cleanup the old cruft. In some ways, transitioning from our old strategy of "cudgel the registry until we get the GUID we want" to "ask politely and accept no for an answer" is a disappointing regression in functionality. But it also means we don't need to keep crazy token stealing code around, or fish around in the registry dangerously. This probably also increases the likelihood that an adapter will be created during edge cases, which means fewer errors for users, which could be a good thing. On the downside, we have the perpetual tensions caused by a system that now "fails open" instead of "fails closed". But so it goes in Windows land. Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent cf6e441 commit 005af4a

File tree

7 files changed

+158
-466
lines changed

7 files changed

+158
-466
lines changed

api/adapter.c

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <devpkey.h>
2121

2222
#include "adapter.h"
23-
#include "elevate.h"
2423
#include "entry.h"
2524
#include "logger.h"
2625
#include "namespace.h"
@@ -1421,62 +1420,6 @@ static _Return_type_success_(return != NULL) WINTUN_ADAPTER *CreateAdapter(
14211420
if (!IsWindows10())
14221421
RequestedGUID = NULL;
14231422

1424-
if (RequestedGUID)
1425-
{
1426-
WCHAR RegPath[MAX_REG_PATH];
1427-
WCHAR RequestedGUIDStr[MAX_GUID_STRING_LEN];
1428-
int GuidStrLen = StringFromGUID2(RequestedGUID, RequestedGUIDStr, _countof(RequestedGUIDStr)) * sizeof(WCHAR);
1429-
if (_snwprintf_s(
1430-
RegPath,
1431-
MAX_REG_PATH,
1432-
_TRUNCATE,
1433-
L"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\%.*s\\Connection",
1434-
GuidStrLen,
1435-
RequestedGUIDStr) == -1)
1436-
goto guidIsFresh;
1437-
HKEY Key;
1438-
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegPath, 0, KEY_QUERY_VALUE, &Key) != ERROR_SUCCESS)
1439-
goto guidIsFresh;
1440-
WCHAR *InstanceID = RegistryQueryString(Key, L"PnPInstanceId", FALSE);
1441-
RegCloseKey(Key);
1442-
if (!InstanceID)
1443-
goto guidIsFresh;
1444-
int Ret = _snwprintf_s(RegPath, MAX_REG_PATH, _TRUNCATE, L"SYSTEM\\CurrentControlSet\\Enum\\%s", InstanceID);
1445-
Free(InstanceID);
1446-
if (Ret == -1)
1447-
goto guidIsFresh;
1448-
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegPath, 0, KEY_QUERY_VALUE, &Key) == ERROR_SUCCESS)
1449-
{
1450-
RegCloseKey(Key);
1451-
MIB_IF_ROW2 IfRow = { 0 };
1452-
if (ConvertInterfaceGuidToLuid(RequestedGUID, &IfRow.InterfaceLuid) == NO_ERROR &&
1453-
GetIfEntry2(&IfRow) == NO_ERROR && IfRow.OperStatus != IfOperStatusNotPresent)
1454-
{
1455-
SetLastError(
1456-
LOG_ERROR(ERROR_ALREADY_EXISTS, L"Requested GUID is already in use: %s", RequestedGUIDStr));
1457-
return NULL;
1458-
}
1459-
}
1460-
LOG(WINTUN_LOG_WARN, L"Requested GUID %s has leftover residue", RequestedGUIDStr);
1461-
HANDLE OriginalToken;
1462-
if (!ImpersonateService(L"NetSetupSvc", &OriginalToken))
1463-
{
1464-
LOG_LAST_ERROR(L"Unable to impersonate NetSetupSvc");
1465-
goto guidIsFresh; // non-fatal
1466-
}
1467-
if (_snwprintf_s(
1468-
RegPath,
1469-
MAX_REG_PATH,
1470-
_TRUNCATE,
1471-
L"SYSTEM\\CurrentControlSet\\Control\\NetworkSetup2\\Interfaces\\%.*s",
1472-
GuidStrLen,
1473-
RequestedGUIDStr) == -1 ||
1474-
!RegistryDeleteKeyRecursive(HKEY_LOCAL_MACHINE, RegPath))
1475-
LOG_LAST_ERROR(L"Unable to delete NetworkSetup2 registry key"); // non-fatal
1476-
RestoreToken(OriginalToken);
1477-
guidIsFresh:;
1478-
}
1479-
14801423
HDEVINFO DevInfo = SetupDiCreateDeviceInfoListExW(&GUID_DEVCLASS_NET, NULL, NULL, NULL);
14811424
if (DevInfo == INVALID_HANDLE_VALUE)
14821425
{
@@ -1566,19 +1509,13 @@ static _Return_type_success_(return != NULL) WINTUN_ADAPTER *CreateAdapter(
15661509
}
15671510
if (RequestedGUID)
15681511
{
1569-
WCHAR RequestedGUIDStr[MAX_GUID_STRING_LEN];
15701512
LastError = RegSetValueExW(
1571-
NetDevRegKey,
1572-
L"NetSetupAnticipatedInstanceId",
1573-
0,
1574-
REG_SZ,
1575-
(const BYTE *)RequestedGUIDStr,
1576-
StringFromGUID2(RequestedGUID, RequestedGUIDStr, _countof(RequestedGUIDStr)) * sizeof(WCHAR));
1513+
NetDevRegKey, L"SuggestedInstanceId", 0, REG_BINARY, (const BYTE *)RequestedGUID, sizeof(*RequestedGUID));
15771514
if (LastError != ERROR_SUCCESS)
15781515
{
15791516
WCHAR RegPath[MAX_REG_PATH];
15801517
LoggerGetRegistryKeyPath(NetDevRegKey, RegPath);
1581-
LOG_ERROR(LastError, L"Failed to set %.*s\\NetSetupAnticipatedInstanceId", MAX_REG_PATH, RegPath);
1518+
LOG_ERROR(LastError, L"Failed to set %.*s\\SuggestedInstanceId", MAX_REG_PATH, RegPath);
15821519
goto cleanupNetDevRegKey;
15831520
}
15841521
}

api/api.vcxproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@
156156
<ItemGroup>
157157
<ClInclude Include="entry.h" />
158158
<ClInclude Include="adapter.h" />
159-
<ClInclude Include="elevate.h" />
160159
<ClInclude Include="rundll32_i.c">
161160
<ExcludedFromBuild>true</ExcludedFromBuild>
162161
</ClInclude>
@@ -171,7 +170,6 @@
171170
<ItemGroup>
172171
<ClCompile Include="entry.c" />
173172
<ClCompile Include="adapter.c" />
174-
<ClCompile Include="elevate.c" />
175173
<ClCompile Include="logger.c" />
176174
<ClCompile Include="namespace.c" />
177175
<ClCompile Include="registry.c" />

api/api.vcxproj.filters

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@
4949
<ClInclude Include="wintun.h">
5050
<Filter>Header Files</Filter>
5151
</ClInclude>
52-
<ClInclude Include="elevate.h">
53-
<Filter>Header Files</Filter>
54-
</ClInclude>
5552
<ClInclude Include="entry.h">
5653
<Filter>Header Files</Filter>
5754
</ClInclude>
@@ -84,11 +81,8 @@
8481
<ClCompile Include="session.c">
8582
<Filter>Source Files</Filter>
8683
</ClCompile>
87-
<ClCompile Include="elevate.c">
88-
<Filter>Source Files</Filter>
89-
</ClCompile>
9084
<ClCompile Include="entry.c">
9185
<Filter>Source Files</Filter>
9286
</ClCompile>
9387
</ItemGroup>
94-
</Project>
88+
</Project>

api/elevate.c

Lines changed: 0 additions & 248 deletions
This file was deleted.

api/elevate.h

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)