Check plugin architecture before loading

Closes #2348, Closes #2350
pull/2303/merge
dail8859 8 years ago
parent d64a525f3e
commit 5f8ba93b78

@ -27,15 +27,23 @@
#include <shlwapi.h> #include <shlwapi.h>
#include <DbgHelp.h>
#include <algorithm> #include <algorithm>
#include "PluginsManager.h" #include "PluginsManager.h"
#include "resource.h" #include "resource.h"
using namespace std; using namespace std;
const TCHAR * USERMSG = TEXT("This plugin is not compatible with current version of Notepad++.\n\n\ const TCHAR * USERMSG = TEXT(" is not compatible with the 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?"); 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; 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<generic_string> & dll2Remove) int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector<generic_string> & dll2Remove)
{ {
const TCHAR *pluginFileName = ::PathFindFileName(pluginFilePath); const TCHAR *pluginFileName = ::PathFindFileName(pluginFilePath);
@ -95,6 +141,9 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector<generic_strin
{ {
pi->_moduleName = PathFindFileName(pluginFilePath); pi->_moduleName = PathFindFileName(pluginFilePath);
if (GetBinaryArchitectureType(pluginFilePath) != ARCH_TYPE)
throw generic_string(ARCH_ERR_MSG);
pi->_hLib = ::LoadLibrary(pluginFilePath); pi->_hLib = ::LoadLibrary(pluginFilePath);
if (!pi->_hLib) if (!pi->_hLib)
{ {
@ -230,6 +279,7 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector<generic_strin
catch (generic_string s) catch (generic_string s)
{ {
s += TEXT("\n\n"); s += TEXT("\n\n");
s += pluginFileName;
s += USERMSG; s += USERMSG;
if (::MessageBox(NULL, s.c_str(), pluginFilePath, MB_YESNO) == IDYES) if (::MessageBox(NULL, s.c_str(), pluginFilePath, MB_YESNO) == IDYES)
{ {
@ -242,6 +292,7 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector<generic_strin
{ {
generic_string msg = TEXT("Failed to load"); generic_string msg = TEXT("Failed to load");
msg += TEXT("\n\n"); msg += TEXT("\n\n");
msg += pluginFileName;
msg += USERMSG; msg += USERMSG;
if (::MessageBox(NULL, msg.c_str(), pluginFilePath, MB_YESNO) == IDYES) if (::MessageBox(NULL, msg.c_str(), pluginFilePath, MB_YESNO) == IDYES)
{ {

@ -107,7 +107,7 @@
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalOptions>/fixed:no %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/fixed:no %(AdditionalOptions)</AdditionalOptions>
<AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerboseLib</ShowProgress> <ShowProgress>LinkVerboseLib</ShowProgress>
<OutputFile>$(OutDir)notepad++.exe</OutputFile> <OutputFile>$(OutDir)notepad++.exe</OutputFile>
<Version>1.0</Version> <Version>1.0</Version>
@ -142,7 +142,7 @@
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalOptions>/fixed:no %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/fixed:no %(AdditionalOptions)</AdditionalOptions>
<AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerboseLib</ShowProgress> <ShowProgress>LinkVerboseLib</ShowProgress>
<OutputFile>$(OutDir)notepad++.exe</OutputFile> <OutputFile>$(OutDir)notepad++.exe</OutputFile>
<Version>1.0</Version> <Version>1.0</Version>
@ -182,7 +182,7 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation> <MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerboseLib</ShowProgress> <ShowProgress>LinkVerboseLib</ShowProgress>
<OutputFile>$(OutDir)notepad++.exe</OutputFile> <OutputFile>$(OutDir)notepad++.exe</OutputFile>
<Version>1.0</Version> <Version>1.0</Version>
@ -231,7 +231,7 @@ copy ..\src\contextMenu.xml ..\bin\contextMenu.xml
<MultiProcessorCompilation>true</MultiProcessorCompilation> <MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerboseLib</ShowProgress> <ShowProgress>LinkVerboseLib</ShowProgress>
<OutputFile>$(OutDir)notepad++.exe</OutputFile> <OutputFile>$(OutDir)notepad++.exe</OutputFile>
<Version>1.0</Version> <Version>1.0</Version>

@ -108,7 +108,7 @@
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalOptions>/fixed:no %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/fixed:no %(AdditionalOptions)</AdditionalOptions>
<AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerboseLib</ShowProgress> <ShowProgress>LinkVerboseLib</ShowProgress>
<OutputFile>$(OutDir)notepad++.exe</OutputFile> <OutputFile>$(OutDir)notepad++.exe</OutputFile>
<Version>1.0</Version> <Version>1.0</Version>
@ -144,7 +144,7 @@
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalOptions>/fixed:no %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/fixed:no %(AdditionalOptions)</AdditionalOptions>
<AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerboseLib</ShowProgress> <ShowProgress>LinkVerboseLib</ShowProgress>
<OutputFile>$(OutDir)notepad++.exe</OutputFile> <OutputFile>$(OutDir)notepad++.exe</OutputFile>
<Version>1.0</Version> <Version>1.0</Version>
@ -185,7 +185,7 @@
<DisableSpecificWarnings>4091;4456;4457;4459</DisableSpecificWarnings> <DisableSpecificWarnings>4091;4456;4457;4459</DisableSpecificWarnings>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerboseLib</ShowProgress> <ShowProgress>LinkVerboseLib</ShowProgress>
<OutputFile>$(OutDir)notepad++.exe</OutputFile> <OutputFile>$(OutDir)notepad++.exe</OutputFile>
<Version>1.0</Version> <Version>1.0</Version>
@ -236,7 +236,7 @@ copy ..\src\contextMenu.xml ..\bin\contextMenu.xml
<DisableSpecificWarnings>4091;4456;4457;4459</DisableSpecificWarnings> <DisableSpecificWarnings>4091;4456;4457;4459</DisableSpecificWarnings>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerboseLib</ShowProgress> <ShowProgress>LinkVerboseLib</ShowProgress>
<OutputFile>$(OutDir)notepad++.exe</OutputFile> <OutputFile>$(OutDir)notepad++.exe</OutputFile>
<Version>1.0</Version> <Version>1.0</Version>

Loading…
Cancel
Save