diff options
author | 2021-07-28 13:50:40 +0200 | |
---|---|---|
committer | 2021-07-28 20:22:18 +0200 | |
commit | 7dffa4be72c77bd7322039ffdc558a842abba8af (patch) | |
tree | 1712373ccfbd56596959206cb6b48c8d02d395ca /api/main.c | |
parent | driver: workaround SDV failure with code analysis (diff) | |
download | wintun-7dffa4be72c77bd7322039ffdc558a842abba8af.tar.xz wintun-7dffa4be72c77bd7322039ffdc558a842abba8af.zip |
vs: move shared configuration to wintun.props and upgrade
Remember to rename wintun.vcxproj.user file in your local working folder
to wintun.props.user manually.
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'api/main.c')
-rw-r--r-- | api/main.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/api/main.c b/api/main.c new file mode 100644 index 0000000..16d3b7c --- /dev/null +++ b/api/main.c @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved. + */ + +#include "adapter.h" +#include "logger.h" +#include "registry.h" +#include "namespace.h" +#include "wintun.h" + +#include <Windows.h> +#pragma warning(push) +#pragma warning(disable : 4201) +/* nonstandard extension used: nameless struct/union */ +#include <delayimp.h> +#pragma warning(pop) +#include <sddl.h> +#include <winefs.h> +#include <stdlib.h> + +HINSTANCE ResourceModule; +HANDLE ModuleHeap; +SECURITY_ATTRIBUTES SecurityAttributes = { .nLength = sizeof(SECURITY_ATTRIBUTES) }; +BOOL IsLocalSystem; + +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; + +static BOOL +InitializeSecurityObjects(void) +{ + BYTE LocalSystemSid[MAX_SID_SIZE]; + DWORD RequiredBytes = sizeof(LocalSystemSid); + HANDLE CurrentProcessToken; + struct + { + TOKEN_USER MaybeLocalSystem; + CHAR LargeEnoughForLocalSystem[MAX_SID_SIZE]; + } TokenUserBuffer; + BOOL Ret = FALSE; + + if (!CreateWellKnownSid(WinLocalSystemSid, NULL, LocalSystemSid, &RequiredBytes)) + return FALSE; + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &CurrentProcessToken)) + return FALSE; + + if (!GetTokenInformation(CurrentProcessToken, TokenUser, &TokenUserBuffer, sizeof(TokenUserBuffer), &RequiredBytes)) + goto cleanupProcessToken; + + IsLocalSystem = EqualSid(TokenUserBuffer.MaybeLocalSystem.User.Sid, LocalSystemSid); + Ret = ConvertStringSecurityDescriptorToSecurityDescriptorW( + IsLocalSystem ? L"O:SYD:P(A;;GA;;;SY)(A;;GA;;;BA)S:(ML;;NWNRNX;;;HI)" + : L"O:BAD:P(A;;GA;;;SY)(A;;GA;;;BA)S:(ML;;NWNRNX;;;HI)", + SDDL_REVISION_1, + &SecurityAttributes.lpSecurityDescriptor, + NULL); + +cleanupProcessToken: + CloseHandle(CurrentProcessToken); + return Ret; +} + +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; + if (!InitializeSecurityObjects()) + { + HeapDestroy(ModuleHeap); + return FALSE; + } + AdapterInit(); + NamespaceInit(); + break; + + case DLL_PROCESS_DETACH: + NamespaceDone(); + LocalFree(SecurityAttributes.lpSecurityDescriptor); + HeapDestroy(ModuleHeap); + break; + } + return TRUE; +} |