Skip to content

Commit

Permalink
Fix bug electron#50: Update the PE checksum after committing changes
Browse files Browse the repository at this point in the history
(And fixes duplicate bug electron#101: Triggers false positive for windows defender golang)
  • Loading branch information
rversteegen authored and 2128506 committed Nov 3, 2021
1 parent c2eb4dc commit bee8b20
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
3 changes: 3 additions & 0 deletions rcedit.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
'src/rcedit.rc',
'src/version.h',
],
'libraries': [
'imagehlp.lib',
],
'msvs_settings': {
'VCLinkerTool': {
'SubSystem': 1, # console executable
Expand Down
3 changes: 1 addition & 2 deletions rcedit.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<AdditionalDependencies>
</AdditionalDependencies>
<AdditionalDependencies>imagehlp.lib</AdditionalDependencies>
<OutputFile>$(OutDir)$(ProjectName)$(TargetExt)</OutputFile>
<SubSystem>Console</SubSystem>
</Link>
Expand Down
66 changes: 54 additions & 12 deletions src/rescle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <assert.h>
#include <atlstr.h>
#include <imagehlp.h>
#include <sstream> // wstringstream
#include <iomanip> // setw, setfill
#include <fstream>
Expand Down Expand Up @@ -93,8 +94,8 @@ std::wstring ReadFileToString(const wchar_t* filename) {

class ScopedFile {
public:
ScopedFile(const WCHAR* path)
: file_(CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) {}
ScopedFile(const WCHAR* path, DWORD access = GENERIC_READ)
: file_(CreateFileW(path, access, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) {}
~ScopedFile() { CloseHandle(file_); }

operator HANDLE() { return file_; }
Expand Down Expand Up @@ -568,8 +569,8 @@ bool ResourceUpdater::ChangeRcData(UINT id, const WCHAR* pathToResource) {

wchar_t abspath[MAX_PATH] = { 0 };
const auto filePath = _wfullpath(abspath, pathToResource, MAX_PATH) ? abspath : pathToResource;
ScopedFile newRcDataFile(filePath);
if (newRcDataFile == INVALID_HANDLE_VALUE) {
ScopedFile newRcDataFile(filePath);
if (newRcDataFile == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Cannot open new data file '%ws'\n", filePath);
return false;
}
Expand All @@ -581,13 +582,13 @@ bool ResourceUpdater::ChangeRcData(UINT id, const WCHAR* pathToResource) {
}

auto& rcData = rcDataLngPairIt->second[id];
rcData.clear();
rcData.clear();
rcData.resize(dwFileSize);

DWORD dwBytesRead{ 0 };

DWORD dwBytesRead{ 0 };
if (!ReadFile(newRcDataFile, rcData.data(), dwFileSize, &dwBytesRead, NULL)) {
fprintf(stderr, "Cannot read file '%ws'\n", filePath);
return false;
return false;
}

return true;
Expand Down Expand Up @@ -701,7 +702,7 @@ bool ResourceUpdater::Commit() {
FreeLibrary(module_);
module_ = NULL;

ScopedResourceUpdater ru(filename_.c_str(), false);
ScopedResourceUpdater ru(filename_, false);
if (ru.Get() == NULL) {
return false;
}
Expand Down Expand Up @@ -979,8 +980,8 @@ BOOL CALLBACK ResourceUpdater::OnEnumResourceManifest(HMODULE hModule, LPCTSTR l
return TRUE; // Keep going
}

ScopedResourceUpdater::ScopedResourceUpdater(const WCHAR* filename, bool deleteOld)
: handle_(BeginUpdateResourceW(filename, deleteOld)) {
ScopedResourceUpdater::ScopedResourceUpdater(std::wstring filename, bool deleteOld)
: filename_(filename), handle_(BeginUpdateResourceW(filename.c_str(), deleteOld)) {
}

ScopedResourceUpdater::~ScopedResourceUpdater() {
Expand All @@ -995,7 +996,7 @@ HANDLE ScopedResourceUpdater::Get() const {

bool ScopedResourceUpdater::Commit() {
commited_ = true;
return EndUpdate(true);
return EndUpdate(true) && UpdateChecksum();
}

bool ScopedResourceUpdater::EndUpdate(bool doesCommit) {
Expand All @@ -1005,4 +1006,45 @@ bool ScopedResourceUpdater::EndUpdate(bool doesCommit) {
return bResult ? true : false;
}

bool ScopedResourceUpdater::UpdateChecksum() {
const WCHAR* path = filename_.c_str();
ScopedFile binFile(path, GENERIC_READ|GENERIC_WRITE);
if (binFile == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Cannot open '%ws'\n", path);
return false;
}
DWORD fileSize = GetFileSize(binFile, NULL);
if (fileSize == INVALID_FILE_SIZE) {
fprintf(stderr, "Cannot get file size for '%ws'\n", path);
return false;
}

HANDLE hFilemap = CreateFileMapping(binFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (!hFilemap) {
fprintf(stderr, "CreateFileMapping(%ws) failed\n", path);
return false;
}

void* view = MapViewOfFile(hFilemap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (!view) {
fprintf(stderr, "Cannot map '%ws' into memory\n", path);
CloseHandle(hFilemap);
return false;
}

bool ret = false;
DWORD originalSum, checkSum;
IMAGE_NT_HEADERS *ntHeader = CheckSumMappedFile(view, fileSize, &originalSum, &checkSum);
if (!ntHeader) {
fprintf(stderr, "CheckSumMappedFile failed\n");
} else {
ntHeader->OptionalHeader.CheckSum = checkSum;
ret = true;
}

UnmapViewOfFile(view);
CloseHandle(hFilemap);
return ret;
}

} // namespace rescle
4 changes: 3 additions & 1 deletion src/rescle.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,17 @@ class ResourceUpdater {

class ScopedResourceUpdater {
public:
ScopedResourceUpdater(const WCHAR* filename, bool deleteOld);
ScopedResourceUpdater(std::wstring filename, bool deleteOld);
~ScopedResourceUpdater();

HANDLE Get() const;
bool Commit();

private:
bool EndUpdate(bool doesCommit);
bool UpdateChecksum();

std::wstring filename_;
HANDLE handle_;
bool commited_ = false;
};
Expand Down

0 comments on commit bee8b20

Please sign in to comment.