From 5f8ba93b78d1877e46a381e4ec991c796caaa820 Mon Sep 17 00:00:00 2001 From: dail8859 Date: Mon, 26 Sep 2016 17:46:01 -0400 Subject: [PATCH] Check plugin architecture before loading Closes #2348, Closes #2350 --- .../MISC/PluginsManager/PluginsManager.cpp | 55 ++++++++++++++++++- PowerEditor/visual.net/notepadPlus.vcxproj | 8 +-- .../visual.net/notepadPlus.vs2015.vcxproj | 8 +-- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp b/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp index 663cce54..03200698 100644 --- a/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp +++ b/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp @@ -27,15 +27,23 @@ #include +#include #include #include "PluginsManager.h" #include "resource.h" using namespace std; -const TCHAR * USERMSG = TEXT("This plugin is not compatible with current version of Notepad++.\n\n\ -Do you want to remove this plugin from plugins directory to prevent this message from the next launch time?"); +const TCHAR * USERMSG = TEXT(" is not compatible with the current version of Notepad++.\n\n\ +Do you want to remove this plugin from the plugins directory to prevent this message from the next launch?"); +#ifdef _WIN64 +#define ARCH_TYPE IMAGE_FILE_MACHINE_AMD64 +const TCHAR *ARCH_ERR_MSG = TEXT("Cannot load 32-bit plugin."); +#else +#define ARCH_TYPE IMAGE_FILE_MACHINE_I386 +const TCHAR *ARCH_ERR_MSG = TEXT("Cannot load 64-bit plugin."); +#endif @@ -82,6 +90,44 @@ static std::wstring GetLastErrorAsString() return message; } +static WORD GetBinaryArchitectureType(const TCHAR *filePath) +{ + WORD machine_type = IMAGE_FILE_MACHINE_UNKNOWN; + HANDLE hMapping = NULL; + LPVOID addrHeader = NULL; + + HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); + if (hFile == INVALID_HANDLE_VALUE) + goto cleanup; + + hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL); + if (hMapping == NULL) + goto cleanup; + + addrHeader = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); + if (addrHeader == NULL) + goto cleanup; // couldn't memory map the file + + PIMAGE_NT_HEADERS peHdr = ImageNtHeader(addrHeader); + if (peHdr == NULL) + goto cleanup; // couldn't read the header + + // Found the binary and architecture type + machine_type = peHdr->FileHeader.Machine; + +cleanup: // release all of our handles + if (addrHeader != NULL) + UnmapViewOfFile(addrHeader); + + if (hMapping != NULL) + CloseHandle(hMapping); + + if (hFile != INVALID_HANDLE_VALUE) + CloseHandle(hFile); + + return machine_type; +} + int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector & dll2Remove) { const TCHAR *pluginFileName = ::PathFindFileName(pluginFilePath); @@ -95,6 +141,9 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector_moduleName = PathFindFileName(pluginFilePath); + if (GetBinaryArchitectureType(pluginFilePath) != ARCH_TYPE) + throw generic_string(ARCH_ERR_MSG); + pi->_hLib = ::LoadLibrary(pluginFilePath); if (!pi->_hLib) { @@ -230,6 +279,7 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector /fixed:no %(AdditionalOptions) - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -142,7 +142,7 @@ /fixed:no %(AdditionalOptions) - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -182,7 +182,7 @@ true - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -231,7 +231,7 @@ copy ..\src\contextMenu.xml ..\bin\contextMenu.xml true - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 diff --git a/PowerEditor/visual.net/notepadPlus.vs2015.vcxproj b/PowerEditor/visual.net/notepadPlus.vs2015.vcxproj index bbdb3c05..8cad6e57 100644 --- a/PowerEditor/visual.net/notepadPlus.vs2015.vcxproj +++ b/PowerEditor/visual.net/notepadPlus.vs2015.vcxproj @@ -108,7 +108,7 @@ /fixed:no %(AdditionalOptions) - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -144,7 +144,7 @@ /fixed:no %(AdditionalOptions) - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -185,7 +185,7 @@ 4091;4456;4457;4459 - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -236,7 +236,7 @@ copy ..\src\contextMenu.xml ..\bin\contextMenu.xml 4091;4456;4457;4459 - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0