2020-07-03 16:49:47 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "entry.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "namespace.h"
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <bcrypt.h>
|
2020-07-03 16:49:47 +02:00
|
|
|
|
|
|
|
static BOOL HasInitialized = FALSE;
|
|
|
|
static CRITICAL_SECTION Initializing;
|
|
|
|
static BCRYPT_ALG_HANDLE AlgProvider;
|
|
|
|
|
2020-07-21 18:19:15 +02:00
|
|
|
static WCHAR *
|
|
|
|
NormalizeStringAlloc(_In_ NORM_FORM NormForm, _In_z_ const WCHAR *Source)
|
2020-07-03 16:49:47 +02:00
|
|
|
{
|
|
|
|
int Len = NormalizeString(NormForm, Source, -1, NULL, 0);
|
2020-10-15 12:38:05 +02:00
|
|
|
for (;;)
|
2020-07-03 16:49:47 +02:00
|
|
|
{
|
2020-10-24 22:12:47 +02:00
|
|
|
WCHAR *Str = HeapAlloc(ModuleHeap, 0, sizeof(WCHAR) * Len);
|
2020-10-15 12:38:05 +02:00
|
|
|
if (!Str)
|
|
|
|
return LOG(WINTUN_LOG_ERR, L"Out of memory"), NULL;
|
|
|
|
Len = NormalizeString(NormForm, Source, -1, Str, Len);
|
2020-07-03 16:49:47 +02:00
|
|
|
if (Len > 0)
|
2020-10-15 12:38:05 +02:00
|
|
|
return Str;
|
|
|
|
DWORD Result = GetLastError();
|
2020-10-24 22:12:47 +02:00
|
|
|
HeapFree(ModuleHeap, 0, Str);
|
2020-10-15 12:38:05 +02:00
|
|
|
if (Result != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
return LOG_ERROR(L"Failed", Result), NULL;
|
2020-07-03 16:49:47 +02:00
|
|
|
Len = -Len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-07-21 18:19:15 +02:00
|
|
|
Bin2Hex(_In_bytecount_(Size) const void *Source, size_t Size, _Out_capcount_(Size * 2) WCHAR *Destination)
|
2020-07-03 16:49:47 +02:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < Size; ++i)
|
|
|
|
{
|
|
|
|
static const WCHAR nibble[] = L"0123456789ABCDEF";
|
|
|
|
*(Destination++) = nibble[(((unsigned char *)Source)[i] & 0xf0) >> 4];
|
|
|
|
*(Destination++) = nibble[(((unsigned char *)Source)[i] & 0x0f)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
static WINTUN_STATUS
|
2020-10-30 06:03:21 +01:00
|
|
|
NamespaceRuntimeInit(void)
|
2020-07-03 16:49:47 +02:00
|
|
|
{
|
|
|
|
DWORD Result;
|
|
|
|
|
|
|
|
EnterCriticalSection(&Initializing);
|
|
|
|
if (HasInitialized)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&Initializing);
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&AlgProvider, BCRYPT_SHA256_ALGORITHM, NULL, 0)))
|
|
|
|
{
|
|
|
|
Result = ERROR_GEN_FAILURE;
|
|
|
|
goto cleanupLeaveCriticalSection;
|
|
|
|
}
|
|
|
|
|
|
|
|
BYTE Sid[MAX_SID_SIZE];
|
|
|
|
DWORD SidSize = MAX_SID_SIZE;
|
|
|
|
if (!CreateWellKnownSid(WinLocalSystemSid, NULL, Sid, &SidSize))
|
|
|
|
{
|
|
|
|
Result = GetLastError();
|
2020-10-15 15:34:31 +02:00
|
|
|
goto cleanupBCryptCloseAlgorithmProvider;
|
2020-07-03 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE Boundary = CreateBoundaryDescriptorW(L"Wintun", 0);
|
|
|
|
if (!Boundary)
|
|
|
|
{
|
|
|
|
Result = GetLastError();
|
2020-10-15 15:34:31 +02:00
|
|
|
goto cleanupBCryptCloseAlgorithmProvider;
|
2020-07-03 16:49:47 +02:00
|
|
|
}
|
|
|
|
if (!AddSIDToBoundaryDescriptor(&Boundary, Sid))
|
|
|
|
{
|
|
|
|
Result = GetLastError();
|
2020-10-15 15:34:31 +02:00
|
|
|
goto cleanupBCryptCloseAlgorithmProvider;
|
2020-07-03 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2020-10-30 13:26:36 +01:00
|
|
|
if (CreatePrivateNamespaceW(&SecurityAttributes, Boundary, L"Wintun"))
|
2020-07-03 16:49:47 +02:00
|
|
|
break;
|
|
|
|
Result = GetLastError();
|
|
|
|
if (Result == ERROR_ALREADY_EXISTS)
|
|
|
|
{
|
|
|
|
if (OpenPrivateNamespaceW(Boundary, L"Wintun"))
|
|
|
|
break;
|
|
|
|
Result = GetLastError();
|
|
|
|
if (Result == ERROR_PATH_NOT_FOUND)
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-15 15:34:31 +02:00
|
|
|
goto cleanupBCryptCloseAlgorithmProvider;
|
2020-07-03 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HasInitialized = TRUE;
|
|
|
|
Result = ERROR_SUCCESS;
|
|
|
|
goto cleanupLeaveCriticalSection;
|
|
|
|
|
|
|
|
cleanupBCryptCloseAlgorithmProvider:
|
|
|
|
BCryptCloseAlgorithmProvider(AlgProvider, 0);
|
|
|
|
cleanupLeaveCriticalSection:
|
|
|
|
LeaveCriticalSection(&Initializing);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Check_return_
|
|
|
|
HANDLE
|
2020-10-15 11:32:06 +02:00
|
|
|
NamespaceTakeMutex(_In_z_ const WCHAR *Pool)
|
2020-07-03 16:49:47 +02:00
|
|
|
{
|
|
|
|
HANDLE Mutex = NULL;
|
|
|
|
|
|
|
|
if (NamespaceRuntimeInit() != ERROR_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BCRYPT_HASH_HANDLE Sha256 = NULL;
|
|
|
|
if (!BCRYPT_SUCCESS(BCryptCreateHash(AlgProvider, &Sha256, NULL, 0, NULL, 0, 0)))
|
|
|
|
return NULL;
|
2020-10-30 16:31:41 +01:00
|
|
|
static const WCHAR mutex_label[] = L"Wintun Adapter Name Mutex Stable Suffix v1 jason@zx2c4.com";
|
|
|
|
if (!BCRYPT_SUCCESS(BCryptHashData(Sha256, (PUCHAR)mutex_label, sizeof(mutex_label) /* Including NULL 2 bytes */, 0)))
|
2020-07-03 16:49:47 +02:00
|
|
|
goto cleanupSha256;
|
2020-07-21 18:19:15 +02:00
|
|
|
WCHAR *PoolNorm = NormalizeStringAlloc(NormalizationC, Pool);
|
2020-07-03 16:49:47 +02:00
|
|
|
if (!PoolNorm)
|
|
|
|
goto cleanupSha256;
|
2020-10-30 16:31:41 +01:00
|
|
|
if (!BCRYPT_SUCCESS(BCryptHashData(Sha256, (PUCHAR)PoolNorm, (int)wcslen(PoolNorm) + 2 /* Add in NULL 2 bytes */, 0)))
|
2020-07-03 16:49:47 +02:00
|
|
|
goto cleanupPoolNorm;
|
|
|
|
BYTE Hash[32];
|
|
|
|
if (!BCRYPT_SUCCESS(BCryptFinishHash(Sha256, Hash, sizeof(Hash), 0)))
|
|
|
|
goto cleanupPoolNorm;
|
|
|
|
static const WCHAR MutexNamePrefix[] = L"Wintun\\Wintun-Name-Mutex-";
|
|
|
|
WCHAR MutexName[_countof(MutexNamePrefix) /*<= incl. terminator*/ + sizeof(Hash) * 2];
|
|
|
|
memcpy(MutexName, MutexNamePrefix, sizeof(MutexNamePrefix) - sizeof(WCHAR));
|
|
|
|
Bin2Hex(Hash, sizeof(Hash), MutexName + _countof(MutexNamePrefix) - 1);
|
|
|
|
MutexName[_countof(MutexName) - 1] = 0;
|
2020-10-30 13:26:36 +01:00
|
|
|
Mutex = CreateMutexW(&SecurityAttributes, FALSE, MutexName);
|
2020-07-03 16:49:47 +02:00
|
|
|
if (!Mutex)
|
|
|
|
goto cleanupPoolNorm;
|
|
|
|
switch (WaitForSingleObject(Mutex, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
case WAIT_ABANDONED:
|
|
|
|
goto cleanupPoolNorm;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(Mutex);
|
|
|
|
Mutex = NULL;
|
|
|
|
cleanupPoolNorm:
|
2020-10-24 22:12:47 +02:00
|
|
|
HeapFree(ModuleHeap, 0, PoolNorm);
|
2020-07-03 16:49:47 +02:00
|
|
|
cleanupSha256:
|
|
|
|
BCryptDestroyHash(Sha256);
|
|
|
|
return Mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-15 11:32:06 +02:00
|
|
|
NamespaceReleaseMutex(_In_ HANDLE Mutex)
|
2020-07-03 16:49:47 +02:00
|
|
|
{
|
|
|
|
ReleaseMutex(Mutex);
|
|
|
|
CloseHandle(Mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-30 06:03:21 +01:00
|
|
|
NamespaceInit(void)
|
2020-07-03 16:49:47 +02:00
|
|
|
{
|
|
|
|
InitializeCriticalSection(&Initializing);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-30 06:03:21 +01:00
|
|
|
NamespaceCleanup(void)
|
2020-07-03 16:49:47 +02:00
|
|
|
{
|
|
|
|
EnterCriticalSection(&Initializing);
|
|
|
|
if (HasInitialized)
|
|
|
|
{
|
|
|
|
BCryptCloseAlgorithmProvider(AlgProvider, 0);
|
|
|
|
HasInitialized = FALSE;
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&Initializing);
|
|
|
|
DeleteCriticalSection(&Initializing);
|
|
|
|
}
|