aboutsummaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-11-03 02:09:00 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2020-11-03 02:25:57 +0100
commit1201c9f346f1e9b2bc00b0a9854fc6e182e851ef (patch)
tree7a404f1a14c3fbb258bb11b464c72dcb15f319d3 /api
parentapi: begin to separate rundll32 jumps (diff)
downloadwintun-1201c9f346f1e9b2bc00b0a9854fc6e182e851ef.tar.xz
wintun-1201c9f346f1e9b2bc00b0a9854fc6e182e851ef.zip
api: ensure more code compiles by using dead code elimination
It'd be nicer to do this via if (is_defined(HAVE_WHATEVER)) But MSVC won't work with the linux kernel macros for this. Ongoing research. Nevertheless, this makes most of the program always pass through the compiler's type checker, only to have dead code removed later. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'api')
-rw-r--r--api/adapter.c35
-rw-r--r--api/entry.c2
-rw-r--r--api/entry.h18
-rw-r--r--api/rundll32.c7
4 files changed, 33 insertions, 29 deletions
diff --git a/api/adapter.c b/api/adapter.c
index 0579e8b..642f842 100644
--- a/api/adapter.c
+++ b/api/adapter.c
@@ -340,7 +340,8 @@ EnableAllOurAdapters(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA_LIST *AdaptersT
void
AdapterInit(void)
{
-#ifdef MAYBE_WOW64
+ if (!MAYBE_WOW64)
+ return;
typedef BOOL(WINAPI * IsWow64Process2_t)(
_In_ HANDLE hProcess, _Out_ USHORT * pProcessMachine, _Out_opt_ USHORT * pNativeMachine);
HANDLE Kernel32;
@@ -354,7 +355,6 @@ AdapterInit(void)
NativeMachine =
IsWow64Process(GetCurrentProcess(), &IsWoW64) && IsWoW64 ? IMAGE_FILE_MACHINE_AMD64 : IMAGE_FILE_PROCESS;
}
-#endif
}
static BOOL
@@ -801,13 +801,13 @@ RtlGetNtVersionNumbers(_Out_opt_ DWORD *MajorVersion, _Out_opt_ DWORD *MinorVers
static BOOL
HaveWHQL(void)
{
-#if defined(HAVE_WHQL)
- DWORD MajorVersion;
- RtlGetNtVersionNumbers(&MajorVersion, NULL, NULL);
- return MajorVersion >= 10;
-#else
+ if (HAVE_WHQL)
+ {
+ DWORD MajorVersion;
+ RtlGetNtVersionNumbers(&MajorVersion, NULL, NULL);
+ return MajorVersion >= 10;
+ }
return FALSE;
-#endif
}
static WINTUN_STATUS
@@ -1682,15 +1682,10 @@ WintunCreateAdapter(
RebootRequired = &DummyRebootRequired;
*RebootRequired = FALSE;
DWORD Result;
-#ifdef MAYBE_WOW64
- if (NativeMachine != IMAGE_FILE_PROCESS)
- {
+ if (MAYBE_WOW64 && NativeMachine != IMAGE_FILE_PROCESS)
Result = CreateAdapterViaRundll32(Pool, Name, RequestedGUID, Adapter, RebootRequired);
- RevertToSelf();
- return Result;
- }
-#endif
- Result = CreateAdapter(Pool, Name, RequestedGUID, Adapter, RebootRequired);
+ else
+ Result = CreateAdapter(Pool, Name, RequestedGUID, Adapter, RebootRequired);
RevertToSelf();
return Result;
}
@@ -1706,14 +1701,12 @@ WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _In_ BOOL ForceCloseSess
RebootRequired = &DummyRebootRequired;
*RebootRequired = FALSE;
DWORD Result;
-#ifdef MAYBE_WOW64
- if (NativeMachine != IMAGE_FILE_PROCESS)
+ if (MAYBE_WOW64 && NativeMachine != IMAGE_FILE_PROCESS)
{
Result = DeleteAdapterViaRundll32(Adapter, ForceCloseSessions, RebootRequired);
RevertToSelf();
return Result;
}
-#endif
HDEVINFO DevInfo;
SP_DEVINFO_DATA DevInfoData;
@@ -1796,14 +1789,12 @@ WintunDeleteDriver(void)
DWORD Result = ERROR_SUCCESS;
-#ifdef MAYBE_WOW64
- if (NativeMachine != IMAGE_FILE_PROCESS)
+ if (MAYBE_WOW64 && NativeMachine != IMAGE_FILE_PROCESS)
{
Result = DeleteDriverViaRundll32();
RevertToSelf();
return Result;
}
-#endif
/* DeleteAllOurAdapters(); */
HANDLE DriverInstallationLock = NamespaceTakeDriverInstallationMutex();
diff --git a/api/entry.c b/api/entry.c
index 3dd42bb..4b56a12 100644
--- a/api/entry.c
+++ b/api/entry.c
@@ -33,7 +33,7 @@ static FARPROC WINAPI DelayedLoadLibraryHook(unsigned dliNotify, PDelayLoadInfo
}
const PfnDliHook __pfnDliNotifyHook2 = DelayedLoadLibraryHook;
-
+#define NOT 1
BOOL APIENTRY
DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved)
{
diff --git a/api/entry.h b/api/entry.h
index 0e0e3a6..e60dd71 100644
--- a/api/entry.h
+++ b/api/entry.h
@@ -14,12 +14,24 @@
# define _L(x) __L(x)
#endif
+/* TODO: Replace with is_defined. MSVC has issues with the linux kernel varadic macro trick for this. */
#if defined(_M_IX86) || defined(_M_ARM)
-#define MAYBE_WOW64
+# define MAYBE_WOW64 1
+#else
+# define MAYBE_WOW64 0
#endif
-#if defined(_M_AMD64) || defined(_M_ARM64) || defined(_DEBUG)
-#define ACCEPT_WOW64
+#if defined(_M_AMD64) || defined(_M_ARM64)
+# define ACCEPT_WOW64 1
+#else
+# define ACCEPT_WOW64 0
#endif
+#ifdef HAVE_WHQL
+# undef HAVE_WHQL
+# define HAVE_WHQL 1
+#else
+# define HAVE_WHQL 0
+#endif
+#pragma warning(disable : 4127) /* conditional expression is constant */
extern HINSTANCE ResourceModule;
extern HANDLE ModuleHeap;
diff --git a/api/rundll32.c b/api/rundll32.c
index 85121a5..8bab711 100644
--- a/api/rundll32.c
+++ b/api/rundll32.c
@@ -3,6 +3,10 @@
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
*/
+#include "entry.h"
+
+#if ACCEPT_WOW64 == 1
+
#include "adapter.h"
#include "logger.h"
#include "wintun.h"
@@ -14,8 +18,6 @@
#define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)
-#if defined(ACCEPT_WOW64) || defined(_DEBUG)
-
static DWORD
WriteFormatted(_In_ DWORD StdHandle, _In_z_ const WCHAR *Template, ...)
{
@@ -147,5 +149,4 @@ VOID __stdcall DeleteDriver(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int n
WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X!", WintunDeleteDriver());
Done();
}
-
#endif