wintun/api
Jason A. Donenfeld 005af4a9c7 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 <Jason@zx2c4.com>
2021-07-09 17:08:28 +02:00
..
adapter.c api: use SuggestedInstanceId instead of NetSetupAnticipatedInstanceId 2021-07-09 17:08:28 +02:00
adapter.h global: bump copyright 2021-01-30 16:45:26 +01:00
api.vcxproj api: use SuggestedInstanceId instead of NetSetupAnticipatedInstanceId 2021-07-09 17:08:28 +02:00
api.vcxproj.filters api: use SuggestedInstanceId instead of NetSetupAnticipatedInstanceId 2021-07-09 17:08:28 +02:00
entry.c api: don't auto-elevate 2021-06-25 16:18:03 +02:00
entry.h api: don't auto-elevate 2021-06-25 16:18:03 +02:00
exports.def api: remove WintunOpenAdapterDeviceObject 2020-11-05 16:58:43 +01:00
extract-driverver.js global: bump copyright 2021-01-30 16:45:26 +01:00
logger.c api: fix fallback log line printf template 2021-04-10 13:37:22 +02:00
logger.h api: upgrade logging 2021-02-16 04:19:21 +01:00
namespace.c api: don't auto-elevate 2021-06-25 16:18:03 +02:00
namespace.h global: bump copyright 2021-01-30 16:45:26 +01:00
nci.def api: make nci.lib work on x86 2020-11-02 11:38:02 +01:00
nci.h global: bump copyright 2021-01-30 16:45:26 +01:00
ntdll.h api: upgrade logging 2021-02-16 04:19:21 +01:00
pch.c global: bump copyright 2021-01-30 16:45:26 +01:00
registry.c api: clean up NetSetup2 GUIDs 2021-05-05 11:17:39 +02:00
registry.h api: clean up NetSetup2 GUIDs 2021-05-05 11:17:39 +02:00
resource.c api: upgrade logging 2021-02-16 04:19:21 +01:00
resource.h global: bump copyright 2021-01-30 16:45:26 +01:00
resources.rc Fix © in resources 2021-03-19 12:04:23 +01:00
rundll32_i.c api: don't auto-elevate 2021-06-25 16:18:03 +02:00
rundll32.c global: bump copyright 2021-01-30 16:45:26 +01:00
session.c api: use SuggestedInstanceId instead of NetSetupAnticipatedInstanceId 2021-07-09 17:08:28 +02:00
wintun.h api: make .h filenames lowercase for building with MinGW on Linux 2021-03-16 10:11:00 +01:00