diff options
author | 2021-10-01 04:36:10 +0000 | |
---|---|---|
committer | 2021-10-06 05:16:14 +0000 | |
commit | 12993b13641a2ee608b0c8906cc280465e2a6423 (patch) | |
tree | bcd34aa8ac9446c704900f6f4f6b43cfe841affb | |
parent | api: adapter: check for errors before waiting on reg keys (diff) | |
download | wireguard-nt-12993b13641a2ee608b0c8906cc280465e2a6423.tar.xz wireguard-nt-12993b13641a2ee608b0c8906cc280465e2a6423.zip |
api: adapter: treat reboot required as real error
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | api/adapter.c | 45 | ||||
-rw-r--r-- | api/rundll32.c | 33 | ||||
-rw-r--r-- | api/rundll32.h | 7 | ||||
-rw-r--r-- | api/wireguard.h | 12 | ||||
-rw-r--r-- | example/example.c | 6 |
5 files changed, 40 insertions, 63 deletions
diff --git a/api/adapter.c b/api/adapter.c index c386df7..42b191f 100644 --- a/api/adapter.c +++ b/api/adapter.c @@ -1341,16 +1341,11 @@ cleanup: _Use_decl_annotations_ WIREGUARD_ADAPTER_HANDLE WINAPI -WireGuardCreateAdapter(LPCWSTR Pool, LPCWSTR Name, const GUID *RequestedGUID, BOOL *RebootRequired) +WireGuardCreateAdapter(LPCWSTR Pool, LPCWSTR Name, const GUID *RequestedGUID) { - BOOL DummyRebootRequired; - if (!RebootRequired) - RebootRequired = &DummyRebootRequired; - *RebootRequired = FALSE; - #ifdef MAYBE_WOW64 if (NativeMachine != IMAGE_FILE_PROCESS) - return CreateAdapterViaRundll32(Pool, Name, RequestedGUID, RebootRequired); + return CreateAdapterViaRundll32(Pool, Name, RequestedGUID); #endif DWORD LastError = ERROR_SUCCESS; @@ -1487,7 +1482,11 @@ WireGuardCreateAdapter(LPCWSTR Pool, LPCWSTR Name, const GUID *RequestedGUID, BO LastError = LOG_LAST_ERROR(L"Failed to install adapter %u device", Adapter->DevInfoData.DevInst); goto cleanupNetDevRegKey; } - *RebootRequired = *RebootRequired || CheckReboot(Adapter->DevInfo, &Adapter->DevInfoData); + if (CheckReboot(Adapter->DevInfo, &Adapter->DevInfoData)) + { + LastError = ERROR_PNP_REBOOT_REQUIRED; + goto cleanupNetDevRegKey; + } if (!SetupDiSetDevicePropertyW( Adapter->DevInfo, @@ -1617,9 +1616,8 @@ cleanupDevice: Adapter->DevInfo, &Adapter->DevInfoData, &RemoveDeviceParams.ClassInstallHeader, - sizeof(RemoveDeviceParams)) && - SetupDiCallClassInstaller(DIF_REMOVE, Adapter->DevInfo, &Adapter->DevInfoData)) - *RebootRequired = *RebootRequired || CheckReboot(Adapter->DevInfo, &Adapter->DevInfoData); + sizeof(RemoveDeviceParams))) + SetupDiCallClassInstaller(DIF_REMOVE, Adapter->DevInfo, &Adapter->DevInfoData); } NamespaceReleaseMutex(Mutex); cleanupDriverInfoList: @@ -1633,17 +1631,13 @@ cleanupAdapter: _Use_decl_annotations_ BOOL WINAPI -WireGuardDeleteAdapter(WIREGUARD_ADAPTER *Adapter, BOOL *RebootRequired) +WireGuardDeleteAdapter(WIREGUARD_ADAPTER *Adapter) { WireGuardSetAdapterLogging(Adapter, WIREGUARD_ADAPTER_LOG_OFF); - BOOL DummyRebootRequired; - if (!RebootRequired) - RebootRequired = &DummyRebootRequired; - *RebootRequired = FALSE; #ifdef MAYBE_WOW64 if (NativeMachine != IMAGE_FILE_PROCESS) - return DeleteAdapterViaRundll32(Adapter, RebootRequired); + return DeleteAdapterViaRundll32(Adapter); #endif DWORD LastError = ERROR_SUCCESS; @@ -1678,7 +1672,8 @@ WireGuardDeleteAdapter(WIREGUARD_ADAPTER *Adapter, BOOL *RebootRequired) GetLastError() != ERROR_NO_SUCH_DEVINST) LastError = LOG_LAST_ERROR(L"Failed to remove adapter %u", Adapter->DevInfoData.DevInst); - *RebootRequired = *RebootRequired || CheckReboot(Adapter->DevInfo, &Adapter->DevInfoData); + if (CheckReboot(Adapter->DevInfo, &Adapter->DevInfoData)) + LastError = LastError == ERROR_SUCCESS ? ERROR_SUCCESS_REBOOT_REQUIRED : ERROR_FAIL_REBOOT_REQUIRED; cleanupMutex: NamespaceReleaseMutex(Mutex); @@ -1737,23 +1732,19 @@ cleanupMutex: _Use_decl_annotations_ BOOL WINAPI -WireGuardDeletePoolDriver(LPCWSTR Pool, BOOL *RebootRequired) +WireGuardDeletePoolDriver(LPCWSTR Pool) { - BOOL DummyRebootRequired; - if (!RebootRequired) - RebootRequired = &DummyRebootRequired; - *RebootRequired = FALSE; - DWORD LastError = ERROR_SUCCESS; #ifdef MAYBE_WOW64 if (NativeMachine != IMAGE_FILE_PROCESS) { - LastError = DeletePoolDriverViaRundll32(Pool, RebootRequired) ? ERROR_SUCCESS : GetLastError(); + LastError = DeletePoolDriverViaRundll32(Pool) ? ERROR_SUCCESS : GetLastError(); goto cleanup; } #endif - if (!DeleteAllOurAdapters(Pool, RebootRequired)) + BOOL RebootRequired = FALSE; + if (!DeleteAllOurAdapters(Pool, &RebootRequired)) { LastError = GetLastError(); goto cleanup; @@ -1800,6 +1791,8 @@ WireGuardDeletePoolDriver(LPCWSTR Pool, BOOL *RebootRequired) } Free(DriverDetail); } + if (RebootRequired) + LastError = LastError == ERROR_SUCCESS ? ERROR_SUCCESS_REBOOT_REQUIRED : ERROR_FAIL_REBOOT_REQUIRED; SetupDiDestroyDriverInfoList(DeviceInfoSet, NULL, SPDIT_CLASSDRIVER); cleanupDeviceInfoSet: SetupDiDestroyDeviceInfoList(DeviceInfoSet); diff --git a/api/rundll32.c b/api/rundll32.c index 3a8fb84..b4fe912 100644 --- a/api/rundll32.c +++ b/api/rundll32.c @@ -82,12 +82,11 @@ VOID __stdcall CreateAdapter(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int if (Argc > 4 && FAILED(CLSIDFromString(Argv[4], &RequestedGUID))) goto cleanup; - BOOL RebootRequired; WIREGUARD_ADAPTER *Adapter = - WireGuardCreateAdapter(Argv[2], Argv[3], Argc > 4 ? &RequestedGUID : NULL, &RebootRequired); + WireGuardCreateAdapter(Argv[2], Argv[3], Argc > 4 ? &RequestedGUID : NULL); DWORD LastError = Adapter ? ERROR_SUCCESS : GetLastError(); WriteFormatted( - STD_OUTPUT_HANDLE, L"%1!X! %2!s! %3!X!", LastError, Adapter ? Adapter->DevInstanceID : L"\"\"", RebootRequired); + STD_OUTPUT_HANDLE, L"%1!X! %2!s!", LastError, Adapter ? Adapter->DevInstanceID : L"\"\""); if (Adapter) WireGuardFreeAdapter(Adapter); @@ -107,17 +106,16 @@ VOID __stdcall DeleteAdapter(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int goto cleanup; DWORD LastError; - BOOL RebootRequired = FALSE; WIREGUARD_ADAPTER *Adapter = AdapterOpenFromDevInstanceId(Argv[2], Argv[3]); if (!Adapter) { LastError = GetLastError(); goto write; } - LastError = WireGuardDeleteAdapter(Adapter, &RebootRequired) ? ERROR_SUCCESS : GetLastError(); + LastError = WireGuardDeleteAdapter(Adapter) ? ERROR_SUCCESS : GetLastError(); WireGuardFreeAdapter(Adapter); write: - WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X! %2!X!", LastError, RebootRequired); + WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X!", LastError); cleanup: LocalFree(Argv); @@ -134,9 +132,8 @@ VOID __stdcall DeletePoolDriver(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, i if (Argc < 2) goto cleanup; - BOOL RebootRequired; - DWORD LastError = WireGuardDeletePoolDriver(Argv[2], &RebootRequired) ? ERROR_SUCCESS : GetLastError(); - WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X! %2!X!", LastError, RebootRequired); + DWORD LastError = WireGuardDeletePoolDriver(Argv[2]) ? ERROR_SUCCESS : GetLastError(); + WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X!", LastError); cleanup: LocalFree(Argv); @@ -435,7 +432,7 @@ cleanupDirectory: _Use_decl_annotations_ WIREGUARD_ADAPTER * -CreateAdapterViaRundll32(LPCWSTR Pool, LPCWSTR Name, const GUID *RequestedGUID, BOOL *RebootRequired) +CreateAdapterViaRundll32(LPCWSTR Pool, LPCWSTR Name, const GUID *RequestedGUID) { LOG(WIREGUARD_LOG_INFO, L"Spawning native process"); LPWSTR Arguments = NULL; @@ -464,7 +461,7 @@ CreateAdapterViaRundll32(LPCWSTR Pool, LPCWSTR Name, const GUID *RequestedGUID, } int Argc; LPWSTR *Argv = CommandLineToArgvW(Response, &Argc); - if (Argc < 3) + if (Argc < 2) { LOG(WIREGUARD_LOG_ERR, L"Incomplete response: %s", Response); LastError = ERROR_INVALID_PARAMETER; @@ -476,8 +473,6 @@ CreateAdapterViaRundll32(LPCWSTR Pool, LPCWSTR Name, const GUID *RequestedGUID, LOG(WIREGUARD_LOG_ERR, L"Failed to get adapter %s", Argv[1]); LastError = ERROR_FILE_NOT_FOUND; } - if (wcstoul(Argv[2], NULL, 16)) - *RebootRequired = TRUE; cleanupArgv: LocalFree(Argv); cleanupArguments: @@ -488,7 +483,7 @@ cleanupArguments: _Use_decl_annotations_ BOOL -DeleteAdapterViaRundll32(const WIREGUARD_ADAPTER *Adapter, BOOL *RebootRequired) +DeleteAdapterViaRundll32(const WIREGUARD_ADAPTER *Adapter) { LOG(WIREGUARD_LOG_INFO, L"Spawning native process"); LPWSTR Arguments = ArgvToCommandLineW(2, Adapter->Pool, Adapter->DevInstanceID); @@ -508,15 +503,13 @@ DeleteAdapterViaRundll32(const WIREGUARD_ADAPTER *Adapter, BOOL *RebootRequired) } int Argc; LPWSTR *Argv = CommandLineToArgvW(Response, &Argc); - if (Argc < 2) + if (Argc < 1) { LOG(WIREGUARD_LOG_ERR, L"Incomplete response: %s", Response); LastError = ERROR_INVALID_PARAMETER; goto cleanupArgv; } LastError = wcstoul(Argv[0], NULL, 16); - if (wcstoul(Argv[1], NULL, 16)) - *RebootRequired = TRUE; cleanupArgv: LocalFree(Argv); cleanupArguments: @@ -526,7 +519,7 @@ cleanupArguments: _Use_decl_annotations_ BOOL -DeletePoolDriverViaRundll32(LPCWSTR Pool, BOOL *RebootRequired) +DeletePoolDriverViaRundll32(LPCWSTR Pool) { LOG(WIREGUARD_LOG_INFO, L"Spawning native process"); LPWSTR Arguments = ArgvToCommandLineW(1, Pool); @@ -546,15 +539,13 @@ DeletePoolDriverViaRundll32(LPCWSTR Pool, BOOL *RebootRequired) } int Argc; LPWSTR *Argv = CommandLineToArgvW(Response, &Argc); - if (Argc < 2) + if (Argc < 1) { LOG(WIREGUARD_LOG_ERR, L"Incomplete response: %s", Response); LastError = ERROR_INVALID_PARAMETER; goto cleanupArgv; } LastError = wcstoul(Argv[0], NULL, 16); - if (wcstoul(Argv[1], NULL, 16)) - *RebootRequired = TRUE; cleanupArgv: LocalFree(Argv); cleanupArguments: diff --git a/api/rundll32.h b/api/rundll32.h index 03a88cb..2b0e0a4 100644 --- a/api/rundll32.h +++ b/api/rundll32.h @@ -14,13 +14,12 @@ WIREGUARD_ADAPTER * CreateAdapterViaRundll32( _In_z_ LPCWSTR Pool, _In_z_ LPCWSTR Name, - _In_opt_ const GUID *RequestedGUID, - _Inout_ BOOL *RebootRequired); + _In_opt_ const GUID *RequestedGUID); _Return_type_success_(return != FALSE) BOOL -DeleteAdapterViaRundll32(_In_ const WIREGUARD_ADAPTER *Adapter, _Inout_ BOOL *RebootRequired); +DeleteAdapterViaRundll32(_In_ const WIREGUARD_ADAPTER *Adapter); _Return_type_success_(return != FALSE) BOOL -DeletePoolDriverViaRundll32(_In_z_ LPCWSTR Pool, _Inout_ BOOL *RebootRequired); +DeletePoolDriverViaRundll32(_In_z_ LPCWSTR Pool); diff --git a/api/wireguard.h b/api/wireguard.h index f820711..27477e9 100644 --- a/api/wireguard.h +++ b/api/wireguard.h @@ -56,8 +56,6 @@ typedef struct _WIREGUARD_ADAPTER *WIREGUARD_ADAPTER_HANDLE; * created for each new adapter. It is called "requested" GUID because the API it uses is * completely undocumented, and so there could be minor interesting complications with its usage. * - * @param RebootRequired Optional pointer to a boolean flag to be set to TRUE in case SetupAPI suggests a reboot. - * * @return If the function succeeds, the return value is the adapter handle. Must be released with WireGuardFreeAdapter. * If the function fails, the return value is NULL. To get extended error information, call GetLastError. */ @@ -65,7 +63,7 @@ typedef _Must_inspect_result_ _Return_type_success_(return != NULL) _Post_maybenull_ WIREGUARD_ADAPTER_HANDLE(WINAPI WIREGUARD_CREATE_ADAPTER_FUNC) -(_In_z_ LPCWSTR Pool, _In_z_ LPCWSTR Name, _In_opt_ const GUID *RequestedGUID, _Out_opt_ BOOL *RebootRequired); +(_In_z_ LPCWSTR Pool, _In_z_ LPCWSTR Name, _In_opt_ const GUID *RequestedGUID); /** * Opens an existing WireGuard adapter. @@ -89,14 +87,12 @@ WIREGUARD_ADAPTER_HANDLE(WINAPI WIREGUARD_OPEN_ADAPTER_FUNC)(_In_z_ LPCWSTR Pool * * @param Adapter Adapter handle obtained with WireGuardOpenAdapter or WireGuardCreateAdapter. * - * @param RebootRequired Optional pointer to a boolean flag to be set to TRUE in case SetupAPI suggests a reboot. - * * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To * get extended error information, call GetLastError. */ typedef _Return_type_success_(return != FALSE) BOOL(WINAPI WIREGUARD_DELETE_ADAPTER_FUNC) -(_In_ WIREGUARD_ADAPTER_HANDLE Adapter, _Out_opt_ BOOL *RebootRequired); +(_In_ WIREGUARD_ADAPTER_HANDLE Adapter); /** * Called by WireGuardEnumAdapters for each adapter in the pool. @@ -139,13 +135,11 @@ typedef VOID(WINAPI WIREGUARD_FREE_ADAPTER_FUNC)(_In_opt_ WIREGUARD_ADAPTER_HAND * * @param Pool Name of the adapter pool. Zero-terminated string of up to WIREGUARD_MAX_POOL-1 characters. * - * @param RebootRequired Optional pointer to a boolean flag to be set to TRUE in case SetupAPI suggests a reboot. - * * @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To * get extended error information, call GetLastError. */ typedef _Return_type_success_(return != FALSE) -BOOL(WINAPI WIREGUARD_DELETE_POOL_DRIVER_FUNC)(_In_z_ LPCWSTR Pool, _Out_opt_ BOOL *RebootRequired); +BOOL(WINAPI WIREGUARD_DELETE_POOL_DRIVER_FUNC)(_In_z_ LPCWSTR Pool); /** * Returns the LUID of the adapter. diff --git a/example/example.c b/example/example.c index 360d4e8..23ef91f 100644 --- a/example/example.c +++ b/example/example.c @@ -371,14 +371,14 @@ int __cdecl main(void) GUID ExampleGuid = { 0xdeadc001, 0xbeef, 0xbabe, { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef } };
WIREGUARD_ADAPTER_HANDLE Adapter = WireGuardOpenAdapter(L"Example", L"Demo");
- if (Adapter && !WireGuardDeleteAdapter(Adapter, NULL))
+ if (Adapter && !WireGuardDeleteAdapter(Adapter))
{
LastError = GetLastError();
LogError(L"Failed to delete already existing adapter", LastError);
goto cleanupQuit;
}
WireGuardFreeAdapter(Adapter);
- Adapter = WireGuardCreateAdapter(L"Example", L"Demo", &ExampleGuid, NULL);
+ Adapter = WireGuardCreateAdapter(L"Example", L"Demo", &ExampleGuid);
if (!Adapter)
{
LastError = GetLastError();
@@ -466,7 +466,7 @@ int __cdecl main(void) } while (WaitForSingleObject(QuitEvent, 1000) == WAIT_TIMEOUT);
cleanupAdapter:
- WireGuardDeleteAdapter(Adapter, NULL);
+ WireGuardDeleteAdapter(Adapter);
WireGuardFreeAdapter(Adapter);
cleanupQuit:
SetConsoleCtrlHandler(CtrlHandler, FALSE);
|