diff options
| author | David Lönnhager <david.l@mullvad.net> | 2021-02-17 12:15:57 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2021-02-17 13:03:21 +0100 |
| commit | 1d9bbc38fb5664ee488c1ab4910cf57936701d3d (patch) | |
| tree | a0262c01618849e4115a6becc7b3e628760122cb /windows/nsis-plugins/src/osinfo/update.cpp | |
| parent | b36a32c4fe8247f032b55dd0dce23d114d6e4e73 (diff) | |
| download | mullvadvpn-1d9bbc38fb5664ee488c1ab4910cf57936701d3d.tar.xz mullvadvpn-1d9bbc38fb5664ee488c1ab4910cf57936701d3d.zip | |
Rename os plugin to osinfo
Diffstat (limited to 'windows/nsis-plugins/src/osinfo/update.cpp')
| -rw-r--r-- | windows/nsis-plugins/src/osinfo/update.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/windows/nsis-plugins/src/osinfo/update.cpp b/windows/nsis-plugins/src/osinfo/update.cpp new file mode 100644 index 0000000000..4f33310236 --- /dev/null +++ b/windows/nsis-plugins/src/osinfo/update.cpp @@ -0,0 +1,97 @@ +#include <stdafx.h> +#include "update.h" +#include <libcommon/error.h> +#include <libcommon/filesystem.h> +#include <libcommon/memory.h> +#include <algorithm> +#include <fstream> +#include <filesystem> + +namespace +{ + +// Jason found this to be a reliable marker of the signature check fix: +// https://git.zx2c4.com/wireguard-windows/tree/installer/customactions.c#n145 +const char PATCH_MARKER[] = "Signature Hash"; + +} + +namespace update +{ + +bool HasSetupApiSha2Fix() +{ + common::memory::ScopeDestructor destructor; + + common::fs::ScopedNativeFileSystem nativeFileSystem; + + const auto systemDir = common::fs::GetKnownFolderPath(FOLDERID_System, KF_FLAG_DEFAULT, NULL); + const auto setupApiPath = std::filesystem::path(systemDir).append(L"setupapi.dll"); + + const auto setupApiHandle = CreateFileW( + setupApiPath.c_str(), + GENERIC_READ, + FILE_SHARE_READ, + nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + nullptr + ); + + if (INVALID_HANDLE_VALUE == setupApiHandle) + { + THROW_WINDOWS_ERROR(GetLastError(), "CreateFileW"); + } + + destructor += [=]() { + CloseHandle(setupApiHandle); + }; + + const auto mapping = CreateFileMappingW(setupApiHandle, nullptr, PAGE_READONLY, 0, 0, nullptr); + + if (nullptr == mapping) + { + THROW_WINDOWS_ERROR(GetLastError(), "CreateFileMappingW"); + } + + destructor += [=]() { + CloseHandle(mapping); + }; + + const auto bytes = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0); + + if (nullptr == bytes) + { + THROW_WINDOWS_ERROR(GetLastError(), "MapViewOfFile"); + } + + destructor += [=]() { + UnmapViewOfFile(bytes); + }; + + MEMORY_BASIC_INFORMATION meminfo; + + if (0 == VirtualQuery(bytes, &meminfo, sizeof(meminfo))) + { + THROW_WINDOWS_ERROR(GetLastError(), "VirtualQuery"); + } + + constexpr auto PATCH_MARKER_SIZE = sizeof(PATCH_MARKER) - 1; + + if (meminfo.RegionSize < PATCH_MARKER_SIZE) + { + return false; + } + + for (size_t i = 0; i <= meminfo.RegionSize - PATCH_MARKER_SIZE; i++) + { + if (0 == memcmp((void*)((char*)bytes + i), PATCH_MARKER, PATCH_MARKER_SIZE)) + { + return true; + } + } + + return false; +} + +} |
