api: serialize driver installation

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-11-02 12:07:05 +01:00
parent 724508d61f
commit 007db8bd94
3 changed files with 43 additions and 7 deletions

View File

@ -621,7 +621,7 @@ WintunGetAdapter(_In_z_ const WCHAR *Pool, _In_z_ const WCHAR *Name, _Out_ WINTU
return LOG(WINTUN_LOG_ERR, L"Failed to impersonate SYSTEM user"), ERROR_ACCESS_DENIED; return LOG(WINTUN_LOG_ERR, L"Failed to impersonate SYSTEM user"), ERROR_ACCESS_DENIED;
DWORD Result; DWORD Result;
HANDLE Mutex = NamespaceTakeMutex(Pool); HANDLE Mutex = NamespaceTakePoolMutex(Pool);
if (!Mutex) if (!Mutex)
{ {
Result = ERROR_INVALID_HANDLE; Result = ERROR_INVALID_HANDLE;
@ -1029,7 +1029,7 @@ CreateAdapter(
LOG(WINTUN_LOG_INFO, L"Creating adapter"); LOG(WINTUN_LOG_INFO, L"Creating adapter");
DWORD Result; DWORD Result;
HANDLE Mutex = NamespaceTakeMutex(Pool); HANDLE Mutex = NamespaceTakePoolMutex(Pool);
if (!Mutex) if (!Mutex)
return ERROR_INVALID_HANDLE; return ERROR_INVALID_HANDLE;
@ -1428,10 +1428,16 @@ static BOOL EnsureWintunUnloaded(VOID)
static WINTUN_STATUS static WINTUN_STATUS
InstallDriver(_Out_writes_z_(MAX_PATH) WCHAR InfStorePath[MAX_PATH], _Inout_ BOOL *RebootRequired) InstallDriver(_Out_writes_z_(MAX_PATH) WCHAR InfStorePath[MAX_PATH], _Inout_ BOOL *RebootRequired)
{ {
HANDLE DriverInstallationLock = NamespaceTakeDriverInstallationMutex();
if (!DriverInstallationLock)
return LOG_LAST_ERROR(L"Failed to take driver installation mutex");
DWORD Result; DWORD Result;
WCHAR RandomTempSubDirectory[MAX_PATH]; WCHAR RandomTempSubDirectory[MAX_PATH];
if ((Result = CreateTemporaryDirectory(RandomTempSubDirectory)) != ERROR_SUCCESS) if ((Result = CreateTemporaryDirectory(RandomTempSubDirectory)) != ERROR_SUCCESS)
return LOG(WINTUN_LOG_ERR, L"Failed to create temporary folder"), Result; {
LOG(WINTUN_LOG_ERR, L"Failed to create temporary folder");
goto cleanupDriverInstallationLock;
}
WCHAR CatPath[MAX_PATH] = { 0 }; WCHAR CatPath[MAX_PATH] = { 0 };
WCHAR SysPath[MAX_PATH] = { 0 }; WCHAR SysPath[MAX_PATH] = { 0 };
@ -1518,6 +1524,8 @@ cleanupDelete:
DeleteFileW(InfPath); DeleteFileW(InfPath);
cleanupDirectory: cleanupDirectory:
RemoveDirectoryW(RandomTempSubDirectory); RemoveDirectoryW(RandomTempSubDirectory);
cleanupDriverInstallationLock:
NamespaceReleaseMutex(DriverInstallationLock);
return Result; return Result;
} }
@ -1730,7 +1738,7 @@ cleanupDirectory:
static WINTUN_STATUS static WINTUN_STATUS
GetAdapter(_In_z_ const WCHAR *Pool, _In_ const GUID *CfgInstanceID, _Out_ WINTUN_ADAPTER **Adapter) GetAdapter(_In_z_ const WCHAR *Pool, _In_ const GUID *CfgInstanceID, _Out_ WINTUN_ADAPTER **Adapter)
{ {
HANDLE Mutex = NamespaceTakeMutex(Pool); HANDLE Mutex = NamespaceTakePoolMutex(Pool);
if (!Mutex) if (!Mutex)
return ERROR_INVALID_HANDLE; return ERROR_INVALID_HANDLE;
HDEVINFO DevInfo; HDEVINFO DevInfo;
@ -1940,7 +1948,7 @@ cleanupToken:
WINTUN_STATUS WINAPI WINTUN_STATUS WINAPI
WintunEnumAdapters(_In_z_ const WCHAR *Pool, _In_ WINTUN_ENUM_CALLBACK_FUNC Func, _In_ LPARAM Param) WintunEnumAdapters(_In_z_ const WCHAR *Pool, _In_ WINTUN_ENUM_CALLBACK_FUNC Func, _In_ LPARAM Param)
{ {
HANDLE Mutex = NamespaceTakeMutex(Pool); HANDLE Mutex = NamespaceTakePoolMutex(Pool);
if (!Mutex) if (!Mutex)
return ERROR_INVALID_HANDLE; return ERROR_INVALID_HANDLE;
DWORD Result = ERROR_SUCCESS; DWORD Result = ERROR_SUCCESS;

View File

@ -102,7 +102,7 @@ cleanupLeaveCriticalSection:
_Check_return_ _Check_return_
HANDLE HANDLE
NamespaceTakeMutex(_In_z_ const WCHAR *Pool) NamespaceTakePoolMutex(_In_z_ const WCHAR *Pool)
{ {
HANDLE Mutex = NULL; HANDLE Mutex = NULL;
@ -147,6 +147,30 @@ cleanupSha256:
return Mutex; return Mutex;
} }
_Check_return_
HANDLE
NamespaceTakeDriverInstallationMutex(void)
{
HANDLE Mutex = NULL;
if (NamespaceRuntimeInit() != ERROR_SUCCESS)
return NULL;
Mutex = CreateMutexW(&SecurityAttributes, FALSE, L"Wintun\\Wintun-Driver-Installation-Mutex");
if (!Mutex)
return NULL;
switch (WaitForSingleObject(Mutex, INFINITE))
{
case WAIT_OBJECT_0:
case WAIT_ABANDONED:
goto out;
}
CloseHandle(Mutex);
Mutex = NULL;
out:
return Mutex;
}
void void
NamespaceReleaseMutex(_In_ HANDLE Mutex) NamespaceReleaseMutex(_In_ HANDLE Mutex)
{ {

View File

@ -9,7 +9,11 @@
_Check_return_ _Check_return_
HANDLE HANDLE
NamespaceTakeMutex(_In_z_ const WCHAR *Pool); NamespaceTakePoolMutex(_In_z_ const WCHAR *Pool);
_Check_return_
HANDLE
NamespaceTakeDriverInstallationMutex(void);
void void
NamespaceReleaseMutex(_In_ HANDLE Mutex); NamespaceReleaseMutex(_In_ HANDLE Mutex);