api: only force close handles if requested

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-10-30 14:34:40 +01:00 committed by Simon Rozman
parent a332f54a1b
commit 0a8bf9d1ff
4 changed files with 19 additions and 13 deletions

View File

@ -1657,16 +1657,17 @@ cleanupToken:
#ifdef MAYBE_WOW64 #ifdef MAYBE_WOW64
static WINTUN_STATUS static WINTUN_STATUS
DeleteAdapterNatively(_In_ const WINTUN_ADAPTER *Adapter, _Inout_ BOOL *RebootRequired) DeleteAdapterNatively(_In_ const WINTUN_ADAPTER *Adapter, _In_ BOOL ForceCloseSessions, _Inout_ BOOL *RebootRequired)
{ {
LOG(WINTUN_LOG_INFO, L"Spawning native process"); LOG(WINTUN_LOG_INFO, L"Spawning native process");
WCHAR GuidStr[MAX_GUID_STRING_LEN]; WCHAR GuidStr[MAX_GUID_STRING_LEN];
WCHAR Arguments[14 + MAX_GUID_STRING_LEN + 1]; WCHAR Arguments[16 + MAX_GUID_STRING_LEN + 1];
if (_snwprintf_s( if (_snwprintf_s(
Arguments, Arguments,
_countof(Arguments), _countof(Arguments),
_TRUNCATE, _TRUNCATE,
L"DeleteAdapter %.*s", L"DeleteAdapter %d %.*s",
ForceCloseSessions ? 1 : 0,
StringFromGUID2(&Adapter->CfgInstanceID, GuidStr, _countof(GuidStr)), StringFromGUID2(&Adapter->CfgInstanceID, GuidStr, _countof(GuidStr)),
GuidStr) == -1) GuidStr) == -1)
return LOG(WINTUN_LOG_ERR, L"Command line too long"), ERROR_INVALID_PARAMETER; return LOG(WINTUN_LOG_ERR, L"Command line too long"), ERROR_INVALID_PARAMETER;
@ -1693,7 +1694,7 @@ cleanupArgv:
#endif #endif
WINTUN_STATUS WINAPI WINTUN_STATUS WINAPI
WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _Inout_ BOOL *RebootRequired) WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _In_ BOOL ForceCloseSessions, _Inout_ BOOL *RebootRequired)
{ {
if (!ElevateToSystem()) if (!ElevateToSystem())
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;
@ -1702,7 +1703,7 @@ WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _Inout_ BOOL *RebootRequ
#ifdef MAYBE_WOW64 #ifdef MAYBE_WOW64
if (NativeMachine != IMAGE_FILE_PROCESS) if (NativeMachine != IMAGE_FILE_PROCESS)
{ {
Result = DeleteAdapterNatively(Adapter, RebootRequired); Result = DeleteAdapterNatively(Adapter, ForceCloseSessions, RebootRequired);
RevertToSelf(); RevertToSelf();
return Result; return Result;
} }
@ -1722,7 +1723,7 @@ WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _Inout_ BOOL *RebootRequ
goto cleanupToken; goto cleanupToken;
} }
if (ForceCloseWintunAdapterHandle(DevInfo, &DevInfoData) != ERROR_SUCCESS) if (ForceCloseSessions && ForceCloseWintunAdapterHandle(DevInfo, &DevInfoData) != ERROR_SUCCESS)
LOG(WINTUN_LOG_WARN, L"Failed to force close adapter handles"); LOG(WINTUN_LOG_WARN, L"Failed to force close adapter handles");
SetQuietInstall(DevInfo, &DevInfoData); SetQuietInstall(DevInfo, &DevInfoData);

View File

@ -115,4 +115,4 @@ WintunCreateAdapter(
* @copydoc WINTUN_DELETE_ADAPTER_FUNC * @copydoc WINTUN_DELETE_ADAPTER_FUNC
*/ */
WINTUN_STATUS WINAPI WINTUN_STATUS WINAPI
WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _Inout_ BOOL *RebootRequired); WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _In_ BOOL ForceCloseSessions, _Inout_ BOOL *RebootRequired);

View File

@ -121,10 +121,11 @@ VOID __stdcall DeleteAdapter(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int
goto cleanup; goto cleanup;
WINTUN_ADAPTER Adapter = { 0 }; WINTUN_ADAPTER Adapter = { 0 };
if (FAILED(CLSIDFromString(Argv[2], &Adapter.CfgInstanceID))) BOOL ForceCloseSessions = wcstoul(Argv[2], NULL, 10);
if (FAILED(CLSIDFromString(Argv[3], &Adapter.CfgInstanceID)))
goto cleanup; goto cleanup;
BOOL RebootRequired = FALSE; BOOL RebootRequired = FALSE;
WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X! %2!X!", WintunDeleteAdapter(&Adapter, &RebootRequired), RebootRequired); WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X! %2!X!", WintunDeleteAdapter(&Adapter, ForceCloseSessions, &RebootRequired), RebootRequired);
cleanup: cleanup:
Done(); Done();

View File

@ -54,13 +54,17 @@ typedef WINTUN_STATUS(WINAPI *WINTUN_CREATE_ADAPTER_FUNC)(
* *
* @param Adapter Adapter handle obtained with WintunGetAdapter or WintunCreateAdapter. * @param Adapter Adapter handle obtained with WintunGetAdapter or WintunCreateAdapter.
* *
* @param ForceCloseSessions Force close adapter handles that may be in use by other processes. Only set this to TRUE
* with extreme care, as this is resource intensive and may put processes into an undefined
* or unpredictable state. Most users should set this to FALSE.
*
* @param RebootRequired Pointer to a boolean flag to be set to TRUE in case SetupAPI suggests a reboot. Must be * @param RebootRequired Pointer to a boolean flag to be set to TRUE in case SetupAPI suggests a reboot. Must be
* initialised to FALSE manually before this function is called. * initialised to FALSE manually before this function is called.
* *
* @return ERROR_SUCCESS on success or the adapter was not found; Win32 error code otherwise. * @return ERROR_SUCCESS on success or the adapter was not found; Win32 error code otherwise.
*/ */
typedef WINTUN_STATUS( typedef WINTUN_STATUS(
WINAPI *WINTUN_DELETE_ADAPTER_FUNC)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _Inout_ BOOL *RebootRequired); WINAPI *WINTUN_DELETE_ADAPTER_FUNC)(_In_ WINTUN_ADAPTER_HANDLE Adapter, _In_ BOOL ForceCloseSessions, _Inout_ BOOL *RebootRequired);
/** /**
* Called by WintunEnumAdapters for each adapter in the pool. * Called by WintunEnumAdapters for each adapter in the pool.