From 937eb447278c2bc1b30852905c7740bcdedb534d Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 31 Oct 2020 11:55:26 +0100 Subject: api: get rid of pch and make headers sane Signed-off-by: Jason A. Donenfeld --- api/adapter.c | 23 +++++++++++- api/adapter.h | 2 + api/api.c | 85 ------------------------------------------ api/api.h | 26 ------------- api/api.vcxproj | 12 ++---- api/api.vcxproj.filters | 18 +++------ api/elevate.c | 5 ++- api/elevate.h | 2 + api/entry.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ api/entry.h | 26 +++++++++++++ api/logger.c | 3 +- api/logger.h | 1 + api/namespace.c | 7 +++- api/nci.c | 3 +- api/ntldr.h | 3 ++ api/pch.h | 43 --------------------- api/registry.c | 6 ++- api/registry.h | 1 + api/resource.c | 5 ++- api/rundll32.c | 6 ++- api/session.c | 7 +++- 21 files changed, 199 insertions(+), 184 deletions(-) delete mode 100644 api/api.c delete mode 100644 api/api.h create mode 100644 api/entry.c create mode 100644 api/entry.h delete mode 100644 api/pch.h diff --git a/api/adapter.c b/api/adapter.c index c0df8e9..bc166c6 100644 --- a/api/adapter.c +++ b/api/adapter.c @@ -3,7 +3,28 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "adapter.h" +#include "elevate.h" +#include "entry.h" +#include "logger.h" +#include "namespace.h" +#include "nci.h" +#include "ntldr.h" +#include "registry.h" +#include "resource.h" + +#include +#include +#define _NTDEF_ //TODO: figure out how to include ntsecapi and winternal together without requiring this +#include +#include +#include +#include +#include +#include +#include +#include +#include #pragma warning(disable : 4221) /* nonstandard: address of automatic in initializer */ diff --git a/api/adapter.h b/api/adapter.h index e7480c6..628299a 100644 --- a/api/adapter.h +++ b/api/adapter.h @@ -6,7 +6,9 @@ #pragma once #include "wintun.h" +#include #include +#include #define MAX_INSTANCE_ID MAX_PATH /* TODO: Is MAX_PATH always enough? */ #define WINTUN_HWID L"Wintun" diff --git a/api/api.c b/api/api.c deleted file mode 100644 index 7f820ae..0000000 --- a/api/api.c +++ /dev/null @@ -1,85 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 - * - * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. - */ - -#include "pch.h" - -HINSTANCE ResourceModule; -HANDLE ModuleHeap; -SECURITY_ATTRIBUTES SecurityAttributes = { .nLength = sizeof(SECURITY_ATTRIBUTES) }; - -WINTUN_STATUS WINAPI -WintunGetVersion( - _Out_ DWORD *DriverVersionMaj, - _Out_ DWORD *DriverVersionMin, - _Out_ DWORD *NdisVersionMaj, - _Out_ DWORD *NdisVersionMin) -{ - HKEY Key; - DWORD Result = - RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\Wintun", 0, KEY_QUERY_VALUE, &Key); - if (Result != ERROR_SUCCESS) - return LOG_ERROR(L"Failed to open registry key", Result); - if (RegistryQueryDWORD(Key, L"DriverMajorVersion", DriverVersionMaj, FALSE) != ERROR_SUCCESS || - RegistryQueryDWORD(Key, L"DriverMinorVersion", DriverVersionMin, FALSE) != ERROR_SUCCESS) - { - /* TODO: Drop the fallback to WINTUN_VERSION_MAJ & WINTUN_VERSION_MIN when Windows 7 support is discontinued. */ - *DriverVersionMaj = WINTUN_VERSION_MAJ; - *DriverVersionMin = WINTUN_VERSION_MIN; - } - Result = RegistryQueryDWORD(Key, L"NdisMajorVersion", NdisVersionMaj, TRUE); - if (Result != ERROR_SUCCESS) - { - LOG(WINTUN_LOG_ERR, L"Failed to query NdisMajorVersion value"); - goto cleanupKey; - } - Result = RegistryQueryDWORD(Key, L"NdisMinorVersion", NdisVersionMin, TRUE); - if (Result != ERROR_SUCCESS) - LOG(WINTUN_LOG_ERR, L"Failed to query NdisMinorVersion value"); -cleanupKey: - RegCloseKey(Key); - return Result; -} - -static FARPROC WINAPI DelayedLoadLibraryHook(unsigned dliNotify, PDelayLoadInfo pdli) -{ - if (dliNotify != dliNotePreLoadLibrary) - return NULL; - HMODULE Library = LoadLibraryExA(pdli->szDll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); - if (!Library) - abort(); - return (FARPROC)Library; -} - -const PfnDliHook __pfnDliNotifyHook2 = DelayedLoadLibraryHook; - -BOOL APIENTRY -DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved) -{ - UNREFERENCED_PARAMETER(lpvReserved); - - switch (fdwReason) - { - case DLL_PROCESS_ATTACH: - ResourceModule = hinstDLL; - ModuleHeap = HeapCreate(0, 0, 0); - if (!ModuleHeap) - return FALSE; - ConvertStringSecurityDescriptorToSecurityDescriptorW( - L"O:SYD:P(A;;GA;;;SY)", SDDL_REVISION_1, &SecurityAttributes.lpSecurityDescriptor, NULL); - AdapterInit(); - NamespaceInit(); - if (NciInit() != ERROR_SUCCESS) - return FALSE; - break; - - case DLL_PROCESS_DETACH: - NciCleanup(); - NamespaceCleanup(); - LocalFree(SecurityAttributes.lpSecurityDescriptor); - HeapDestroy(ModuleHeap); - break; - } - return TRUE; -} diff --git a/api/api.h b/api/api.h deleted file mode 100644 index 0e0e3a6..0000000 --- a/api/api.h +++ /dev/null @@ -1,26 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 - * - * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. - */ - -#pragma once - -#include - -#ifndef __L -# define __L(x) L##x -#endif -#ifndef _L -# define _L(x) __L(x) -#endif - -#if defined(_M_IX86) || defined(_M_ARM) -#define MAYBE_WOW64 -#endif -#if defined(_M_AMD64) || defined(_M_ARM64) || defined(_DEBUG) -#define ACCEPT_WOW64 -#endif - -extern HINSTANCE ResourceModule; -extern HANDLE ModuleHeap; -extern SECURITY_ATTRIBUTES SecurityAttributes; diff --git a/api/api.vcxproj b/api/api.vcxproj index 3c1f58d..0883de8 100644 --- a/api/api.vcxproj +++ b/api/api.vcxproj @@ -104,8 +104,6 @@ _WINDOWS;_USRDLL;%(PreprocessorDefinitions) HAVE_WHQL;%(PreprocessorDefinitions) - Use - pch.h /volatile:iso %(AdditionalOptions) @@ -153,28 +151,24 @@ - + - - + - - Create - @@ -197,4 +191,4 @@ - + \ No newline at end of file diff --git a/api/api.vcxproj.filters b/api/api.vcxproj.filters index c65b099..90061fb 100644 --- a/api/api.vcxproj.filters +++ b/api/api.vcxproj.filters @@ -25,12 +25,6 @@ - - Header Files - - - Header Files - Header Files @@ -58,11 +52,11 @@ Header Files + + Header Files + - - Source Files - Source Files @@ -75,9 +69,6 @@ Source Files - - Source Files - Source Files @@ -93,5 +84,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/api/elevate.c b/api/elevate.c index 9baaa97..e9bab7f 100644 --- a/api/elevate.c +++ b/api/elevate.c @@ -3,7 +3,10 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "elevate.h" + +#include +#include BOOL ElevateToSystem(void) diff --git a/api/elevate.h b/api/elevate.h index 5a3b70c..ec054ba 100644 --- a/api/elevate.h +++ b/api/elevate.h @@ -5,5 +5,7 @@ #pragma once +#include + BOOL ElevateToSystem(void); diff --git a/api/entry.c b/api/entry.c new file mode 100644 index 0000000..df24ff9 --- /dev/null +++ b/api/entry.c @@ -0,0 +1,99 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. + */ + +#include "adapter.h" +#include "entry.h" +#include "logger.h" +#include "registry.h" +#include "namespace.h" +#include "nci.h" +#include "wintun.h" + +#include +#pragma warning(push) +#pragma warning(disable : 4201) +/* nonstandard extension used: nameless struct/union */ +#include +#pragma warning(pop) +#include + +HINSTANCE ResourceModule; +HANDLE ModuleHeap; +SECURITY_ATTRIBUTES SecurityAttributes = { .nLength = sizeof(SECURITY_ATTRIBUTES) }; + +WINTUN_STATUS WINAPI +WintunGetVersion( + _Out_ DWORD *DriverVersionMaj, + _Out_ DWORD *DriverVersionMin, + _Out_ DWORD *NdisVersionMaj, + _Out_ DWORD *NdisVersionMin) +{ + HKEY Key; + DWORD Result = + RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\Wintun", 0, KEY_QUERY_VALUE, &Key); + if (Result != ERROR_SUCCESS) + return LOG_ERROR(L"Failed to open registry key", Result); + if (RegistryQueryDWORD(Key, L"DriverMajorVersion", DriverVersionMaj, FALSE) != ERROR_SUCCESS || + RegistryQueryDWORD(Key, L"DriverMinorVersion", DriverVersionMin, FALSE) != ERROR_SUCCESS) + { + /* TODO: Drop the fallback to WINTUN_VERSION_MAJ & WINTUN_VERSION_MIN when Windows 7 support is discontinued. */ + *DriverVersionMaj = WINTUN_VERSION_MAJ; + *DriverVersionMin = WINTUN_VERSION_MIN; + } + Result = RegistryQueryDWORD(Key, L"NdisMajorVersion", NdisVersionMaj, TRUE); + if (Result != ERROR_SUCCESS) + { + LOG(WINTUN_LOG_ERR, L"Failed to query NdisMajorVersion value"); + goto cleanupKey; + } + Result = RegistryQueryDWORD(Key, L"NdisMinorVersion", NdisVersionMin, TRUE); + if (Result != ERROR_SUCCESS) + LOG(WINTUN_LOG_ERR, L"Failed to query NdisMinorVersion value"); +cleanupKey: + RegCloseKey(Key); + return Result; +} + +static FARPROC WINAPI DelayedLoadLibraryHook(unsigned dliNotify, PDelayLoadInfo pdli) +{ + if (dliNotify != dliNotePreLoadLibrary) + return NULL; + HMODULE Library = LoadLibraryExA(pdli->szDll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (!Library) + abort(); + return (FARPROC)Library; +} + +const PfnDliHook __pfnDliNotifyHook2 = DelayedLoadLibraryHook; + +BOOL APIENTRY +DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved) +{ + UNREFERENCED_PARAMETER(lpvReserved); + + switch (fdwReason) + { + case DLL_PROCESS_ATTACH: + ResourceModule = hinstDLL; + ModuleHeap = HeapCreate(0, 0, 0); + if (!ModuleHeap) + return FALSE; + ConvertStringSecurityDescriptorToSecurityDescriptorW( + L"O:SYD:P(A;;GA;;;SY)", SDDL_REVISION_1, &SecurityAttributes.lpSecurityDescriptor, NULL); + AdapterInit(); + NamespaceInit(); + if (NciInit() != ERROR_SUCCESS) + return FALSE; + break; + + case DLL_PROCESS_DETACH: + NciCleanup(); + NamespaceCleanup(); + LocalFree(SecurityAttributes.lpSecurityDescriptor); + HeapDestroy(ModuleHeap); + break; + } + return TRUE; +} diff --git a/api/entry.h b/api/entry.h new file mode 100644 index 0000000..0e0e3a6 --- /dev/null +++ b/api/entry.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. + */ + +#pragma once + +#include + +#ifndef __L +# define __L(x) L##x +#endif +#ifndef _L +# define _L(x) __L(x) +#endif + +#if defined(_M_IX86) || defined(_M_ARM) +#define MAYBE_WOW64 +#endif +#if defined(_M_AMD64) || defined(_M_ARM64) || defined(_DEBUG) +#define ACCEPT_WOW64 +#endif + +extern HINSTANCE ResourceModule; +extern HANDLE ModuleHeap; +extern SECURITY_ATTRIBUTES SecurityAttributes; diff --git a/api/logger.c b/api/logger.c index 468e22d..6de25c8 100644 --- a/api/logger.c +++ b/api/logger.c @@ -3,7 +3,8 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "logger.h" +#include static BOOL CALLBACK NopLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *LogLine) diff --git a/api/logger.h b/api/logger.h index 76d98f8..e126748 100644 --- a/api/logger.h +++ b/api/logger.h @@ -6,6 +6,7 @@ #pragma once #include "wintun.h" +#include extern WINTUN_LOGGER_FUNC Logger; diff --git a/api/namespace.c b/api/namespace.c index 6eff910..baee8a2 100644 --- a/api/namespace.c +++ b/api/namespace.c @@ -3,7 +3,12 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "entry.h" +#include "logger.h" +#include "namespace.h" + +#include +#include static BOOL HasInitialized = FALSE; static CRITICAL_SECTION Initializing; diff --git a/api/nci.c b/api/nci.c index 48bab92..e4dea9d 100644 --- a/api/nci.c +++ b/api/nci.c @@ -3,7 +3,8 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "nci.h" +#include static HMODULE NciModule; diff --git a/api/ntldr.h b/api/ntldr.h index dca0e78..3511fd8 100644 --- a/api/ntldr.h +++ b/api/ntldr.h @@ -31,3 +31,6 @@ typedef struct _RTL_PROCESS_MODULES ULONG NumberOfModules; RTL_PROCESS_MODULE_INFORMATION Modules[1]; } RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES; + + +#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) //TODO: #include instead of this diff --git a/api/pch.h b/api/pch.h deleted file mode 100644 index 4defd82..0000000 --- a/api/pch.h +++ /dev/null @@ -1,43 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 - * - * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. - */ - -#pragma once - -#include "adapter.h" -#include "api.h" -#include "elevate.h" -#include "logger.h" -#include "namespace.h" -#include "nci.h" -#include "ntldr.h" -#include "registry.h" -#include "resource.h" -#include "wintun.h" - -#pragma warning(push) -#pragma warning(disable: 4201) /* nonstandard extension used: nameless struct/union */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#define _NTDEF_ //TODO: find a better way to include both ntsecapi.h and winternl.h or include ntdef.h for real somehow -#include -#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) //TODO: #include instead of this -#pragma warning(pop) \ No newline at end of file diff --git a/api/registry.c b/api/registry.c index c0ebe93..1428165 100644 --- a/api/registry.c +++ b/api/registry.c @@ -3,7 +3,11 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "entry.h" +#include "logger.h" +#include "registry.h" +#include +#include static WINTUN_STATUS OpenKeyWait(_In_ HKEY Key, _Inout_z_ WCHAR *Path, _In_ DWORD Access, _In_ ULONGLONG Deadline, _Out_ HKEY *KeyOut) diff --git a/api/registry.h b/api/registry.h index a8e5f5f..0ba7279 100644 --- a/api/registry.h +++ b/api/registry.h @@ -6,6 +6,7 @@ #pragma once #include "wintun.h" +#include #define MAX_REG_PATH \ 256 /* Maximum registry path length \ diff --git a/api/resource.c b/api/resource.c index 17bed6a..94dc80b 100644 --- a/api/resource.c +++ b/api/resource.c @@ -3,7 +3,10 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "entry.h" +#include "logger.h" +#include "resource.h" +#include WINTUN_STATUS ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ const void **Address, _Out_ DWORD *Size) diff --git a/api/rundll32.c b/api/rundll32.c index bdec3b6..40ec3fe 100644 --- a/api/rundll32.c +++ b/api/rundll32.c @@ -3,7 +3,11 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "adapter.h" +#include "logger.h" +#include "wintun.h" + +#include #define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__) diff --git a/api/session.c b/api/session.c index eb71147..67772f0 100644 --- a/api/session.c +++ b/api/session.c @@ -3,7 +3,12 @@ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. */ -#include "pch.h" +#include "adapter.h" +#include "elevate.h" +#include "entry.h" +#include "logger.h" +#include "wintun.h" +#include #pragma warning(disable : 4200) /* nonstandard: zero-sized array in struct/union */ -- cgit v1.2.3-59-g8ed1b