From 5925f914e4c8a41acbae5b347afea7b47da0f87c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 24 Jul 2020 09:39:02 +0200 Subject: [PATCH] api: add support for WoW64 Some functions of SetupAPI only work when invoked from a native process. Registry and filesystem reflection makes them fail on WoW64. For WoW64 processes, a minimum set of rundll32 functions are provided. Signed-off-by: Simon Rozman --- .gitignore | 10 +++--- api/api.vcxproj | 29 +++++++++++++++++ api/api.vcxproj.filters | 3 ++ api/rundll32.c | 62 +++++++++++++++++++++++++++++++++++++ installer/installer.vcxproj | 2 ++ installer/installer.wxs | 20 ++++++++---- wintun.proj | 3 +- wintun.props | 3 +- wintun.sln | 10 ++++++ 9 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 api/rundll32.c diff --git a/.gitignore b/.gitignore index 7db1686..d7468b9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,12 +4,14 @@ # Build Output /dist -/x86/Release -/x86/Debug -/amd64/Release /amd64/Debug -/arm64/Release +/amd64/Release +/arm/Debug +/arm/Release /arm64/Debug +/arm64/Release +/x86/Debug +/x86/Release # Static Driver Verifier Output /sdv diff --git a/api/api.vcxproj b/api/api.vcxproj index 8383455..89cc0a6 100644 --- a/api/api.vcxproj +++ b/api/api.vcxproj @@ -1,6 +1,10 @@ + + Debug + ARM + Debug ARM64 @@ -9,6 +13,10 @@ Debug Win32 + + Release + ARM + Release ARM64 @@ -62,6 +70,12 @@ v142 true + + DynamicLibrary + Unicode + v142 + true + DynamicLibrary true @@ -75,6 +89,12 @@ Unicode v142 + + DynamicLibrary + true + Unicode + v142 + @@ -96,6 +116,10 @@ + + + + @@ -104,6 +128,10 @@ + + + + ..\$(WintunPlatform)\$(Configuration)\ @@ -169,6 +197,7 @@ Create + diff --git a/api/api.vcxproj.filters b/api/api.vcxproj.filters index 2016316..6f8c334 100644 --- a/api/api.vcxproj.filters +++ b/api/api.vcxproj.filters @@ -45,6 +45,9 @@ Source Files + + Source Files + Source Files diff --git a/api/rundll32.c b/api/rundll32.c new file mode 100644 index 0000000..56234e0 --- /dev/null +++ b/api/rundll32.c @@ -0,0 +1,62 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. + */ + +#include "pch.h" + +#if defined(_M_AMD64) || defined(_M_ARM64) + +__declspec(dllexport) VOID __stdcall CreateAdapter(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) +{ + UNREFERENCED_PARAMETER(hwnd); + UNREFERENCED_PARAMETER(hinst); + UNREFERENCED_PARAMETER(lpszCmdLine); + UNREFERENCED_PARAMETER(nCmdShow); + + int Argc; + LPWSTR *Argv = CommandLineToArgvW(GetCommandLineW(), &Argc); + if (Argc < 4) + goto cleanupArgv; + + if (wcslen(Argv[2]) >= MAX_POOL) + goto cleanupArgv; + if (wcslen(Argv[3]) >= MAX_ADAPTER_NAME) + goto cleanupArgv; + GUID RequestedGUID; + if (Argc > 4 && FAILED(CLSIDFromString(Argv[4], &RequestedGUID))) + goto cleanupArgv; + WINTUN_ADAPTER *Adapter; + BOOL RebootRequired = FALSE; + DWORD Result = WintunCreateAdapter(Argv[2], Argv[3], Argc > 4 ? &RequestedGUID : NULL, &Adapter, &RebootRequired); + if (Result != ERROR_SUCCESS) + goto cleanupArgv; + + WintunFreeAdapter(Adapter); +cleanupArgv: + LocalFree(Argv); +} + +__declspec(dllexport) VOID __stdcall DeleteAdapter(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) +{ + UNREFERENCED_PARAMETER(hwnd); + UNREFERENCED_PARAMETER(hinst); + UNREFERENCED_PARAMETER(lpszCmdLine); + UNREFERENCED_PARAMETER(nCmdShow); + + int Argc; + LPWSTR *Argv = CommandLineToArgvW(GetCommandLineW(), &Argc); + if (Argc < 3) + goto cleanupArgv; + + WINTUN_ADAPTER Adapter = { 0 }; + if (FAILED(CLSIDFromString(Argv[2], &Adapter.CfgInstanceID))) + goto cleanupArgv; + BOOL RebootRequired = FALSE; + WintunDeleteAdapter(&Adapter, &RebootRequired); + +cleanupArgv: + LocalFree(Argv); +} + +#endif diff --git a/installer/installer.vcxproj b/installer/installer.vcxproj index 3bbd5fd..582a1b1 100644 --- a/installer/installer.vcxproj +++ b/installer/installer.vcxproj @@ -122,6 +122,8 @@ arm64 $(WixCandleFlags) -nologo -arch $(WixArch) -dWINTUN_PLATFORM=$(WintunPlatform) -dWINTUN_VERSION=$(WintunVersion) -dINSTALLER_VERSION_MIN=$(InstallerVersionMin) -sw1086 $(WixLightFlags) -nologo -b output_dir="$(OutDir.TrimEnd('\'))" -spdb -sw1076 -sw1079 + $(WixLightFlags) -b output_dir_wow64="..\x86\$(Configuration)" + $(WixLightFlags) -b output_dir_wow64="..\arm\$(Configuration)" $(OutDir) wintun .msm diff --git a/installer/installer.wxs b/installer/installer.wxs index 281b397..b9f5c1a 100644 --- a/installer/installer.wxs +++ b/installer/installer.wxs @@ -5,11 +5,6 @@ Copyright (C) 2018-2019 WireGuard LLC. All Rights Reserved. --> - - - - - - + + + + + + + + + + + + + + diff --git a/wintun.proj b/wintun.proj index eec1f01..918c241 100644 --- a/wintun.proj +++ b/wintun.proj @@ -15,9 +15,10 @@ - + + diff --git a/wintun.props b/wintun.props index 5b31926..cb71aa9 100644 --- a/wintun.props +++ b/wintun.props @@ -12,9 +12,10 @@ $(WintunVersionMaj).$(WintunVersionMin) + arm + arm64 x86 amd64 - arm64 dist\ sdv\ diff --git a/wintun.sln b/wintun.sln index 2caf2b6..538a570 100644 --- a/wintun.sln +++ b/wintun.sln @@ -22,45 +22,55 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|amd64 = Debug|amd64 + Debug|arm = Debug|arm Debug|arm64 = Debug|arm64 Debug|x86 = Debug|x86 Release|amd64 = Release|amd64 + Release|arm = Release|arm Release|arm64 = Release|arm64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Debug|amd64.ActiveCfg = Debug|x64 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Debug|amd64.Build.0 = Debug|x64 + {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Debug|arm.ActiveCfg = Debug|ARM + {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Debug|arm.Build.0 = Debug|ARM {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Debug|arm64.ActiveCfg = Debug|ARM64 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Debug|arm64.Build.0 = Debug|ARM64 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Debug|x86.ActiveCfg = Debug|Win32 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Debug|x86.Build.0 = Debug|Win32 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Release|amd64.ActiveCfg = Release|x64 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Release|amd64.Build.0 = Release|x64 + {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Release|arm.ActiveCfg = Release|ARM + {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Release|arm.Build.0 = Release|ARM {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Release|arm64.ActiveCfg = Release|ARM64 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Release|arm64.Build.0 = Release|ARM64 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Release|x86.ActiveCfg = Release|Win32 {897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}.Release|x86.Build.0 = Release|Win32 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Debug|amd64.ActiveCfg = Debug|x64 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Debug|amd64.Build.0 = Debug|x64 + {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Debug|arm.ActiveCfg = Debug|Win32 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Debug|arm64.ActiveCfg = Debug|ARM64 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Debug|arm64.Build.0 = Debug|ARM64 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Debug|x86.ActiveCfg = Debug|Win32 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Debug|x86.Build.0 = Debug|Win32 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|amd64.ActiveCfg = Release|x64 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|amd64.Build.0 = Release|x64 + {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|arm.ActiveCfg = Release|Win32 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|arm64.ActiveCfg = Release|ARM64 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|arm64.Build.0 = Release|ARM64 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|x86.ActiveCfg = Release|Win32 {F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|x86.Build.0 = Release|Win32 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Debug|amd64.ActiveCfg = Debug|x64 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Debug|amd64.Build.0 = Debug|x64 + {D19E6354-A643-4ACC-82D5-B2780BB83475}.Debug|arm.ActiveCfg = Debug|Win32 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Debug|arm64.ActiveCfg = Debug|ARM64 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Debug|arm64.Build.0 = Debug|ARM64 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Debug|x86.ActiveCfg = Debug|Win32 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Debug|x86.Build.0 = Debug|Win32 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Release|amd64.ActiveCfg = Release|x64 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Release|amd64.Build.0 = Release|x64 + {D19E6354-A643-4ACC-82D5-B2780BB83475}.Release|arm.ActiveCfg = Release|Win32 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Release|arm64.ActiveCfg = Release|ARM64 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Release|arm64.Build.0 = Release|ARM64 {D19E6354-A643-4ACC-82D5-B2780BB83475}.Release|x86.ActiveCfg = Release|Win32