Recommitting: https://codereview.chromium.org/406043003/

(originally reverted because the buildbots weren't pointed 
to verifier_test_dll.dll by unit_tests.isolate)

=========================================================
Adding the VerifyModule function (and helpers) to safe browsing.

This function allows us to compare a module in memory with 
its original on disk, and identify any differences (after 
accounting for relocations).  The state of the module 
(MODULE_UNKNOWN, MODULE_UNMODIFIED or MODULE_MODIFIED) is 
returned along with a list of the possibly modified exported 
functions of the module.


BUG=401854
TBR= noelutz@google.com, robertshield@chromium.org, csharp@chromium.org, asvitkine@chromium.org, grt@chromium.org
NOTRY=True

Review URL: https://codereview.chromium.org/451893003

Cr-Commit-Position: refs/heads/master@{#288417}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288417 0039d316-1c4b-4281-b951-d872f2087c98
parent f1ad9c7b
This diff is collapsed.
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SAFE_BROWSING_MODULE_INTEGRITY_VERIFIER_WIN_H_
#define CHROME_BROWSER_SAFE_BROWSING_MODULE_INTEGRITY_VERIFIER_WIN_H_
#include <stdint.h>
#include <set>
#include <string>
namespace base {
namespace win {
class PEImage;
class PEImageAsData;
} // namespace win
} // namespace base
namespace safe_browsing {
// This enum defines the possible module states VerifyModule can return.
enum ModuleState {
MODULE_STATE_UNKNOWN,
MODULE_STATE_UNMODIFIED,
MODULE_STATE_MODIFIED,
};
// Helper to grab the addresses and size of the code section of a PEImage.
// Returns two addresses: one for the dll loaded as a library, the other for the
// dll loaded as data.
bool GetCodeAddrsAndSize(const base::win::PEImage& mem_peimage,
const base::win::PEImageAsData& disk_peimage,
uint8_t** mem_code_addr,
uint8_t** disk_code_addr,
uint32_t* code_size);
// Examines the code section of the given module in memory and on disk, looking
// for unexpected differences. Returns a ModuleState and and a set of the
// possibly modified exports.
ModuleState VerifyModule(const wchar_t* module_name,
std::set<std::string>* modified_exports);
} // namespace safe_browsing
#endif // CHROME_BROWSER_SAFE_BROWSING_MODULE_INTEGRITY_VERIFIER_WIN_H_
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/safe_browsing/module_integrity_verifier_win.h"
#include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h"
#include "base/native_library.h"
#include "base/path_service.h"
#include "base/scoped_native_library.h"
#include "base/win/pe_image.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace safe_browsing {
namespace {
const wchar_t kTestDllName[] = L"verifier_test_dll.dll";
const char kTestExportName[] = "DummyExport";
} // namespace
class SafeBrowsingModuleVerifierWinTest : public testing::Test {
protected:
void SetUpTestDllAndPEImages() {
LoadModule();
HMODULE mem_handle;
GetMemModuleHandle(&mem_handle);
mem_peimage_ptr_.reset(new base::win::PEImage(mem_handle));
ASSERT_TRUE(mem_peimage_ptr_->VerifyMagic());
LoadDLLAsFile();
HMODULE disk_handle;
GetDiskModuleHandle(&disk_handle);
disk_peimage_ptr_.reset(new base::win::PEImageAsData(disk_handle));
ASSERT_TRUE(disk_peimage_ptr_->VerifyMagic());
}
void LoadModule() {
HMODULE mem_dll_handle =
LoadNativeLibrary(base::FilePath(kTestDllName), NULL);
ASSERT_NE(static_cast<HMODULE>(NULL), mem_dll_handle)
<< "GLE=" << GetLastError();
mem_dll_handle_.Reset(mem_dll_handle);
ASSERT_TRUE(mem_dll_handle_.is_valid());
}
void GetMemModuleHandle(HMODULE* mem_handle) {
*mem_handle = GetModuleHandle(kTestDllName);
ASSERT_NE(static_cast<HMODULE>(NULL), *mem_handle);
}
void LoadDLLAsFile() {
// Use the module handle to find the it on disk, then load as a file.
HMODULE module_handle;
GetMemModuleHandle(&module_handle);
WCHAR module_path[MAX_PATH] = {};
DWORD length =
GetModuleFileName(module_handle, module_path, arraysize(module_path));
ASSERT_NE(arraysize(module_path), length);
ASSERT_TRUE(disk_dll_handle_.Initialize(base::FilePath(module_path)));
}
void GetDiskModuleHandle(HMODULE* disk_handle) {
*disk_handle =
reinterpret_cast<HMODULE>(const_cast<uint8*>(disk_dll_handle_.data()));
}
// Edits the first byte of the single function exported by the test dll.
void EditExport() {
HMODULE mem_handle;
GetMemModuleHandle(&mem_handle);
uint8_t* export_addr =
reinterpret_cast<uint8_t*>(GetProcAddress(mem_handle, kTestExportName));
EXPECT_NE(reinterpret_cast<uint8_t*>(NULL), export_addr);
// Edit the first byte of the function.
uint8_t new_val = (*export_addr) + 1;
SIZE_T bytes_written = 0;
WriteProcessMemory(GetCurrentProcess(),
export_addr,
reinterpret_cast<void*>(&new_val),
1,
&bytes_written);
EXPECT_EQ(1, bytes_written);
}
base::ScopedNativeLibrary mem_dll_handle_;
base::MemoryMappedFile disk_dll_handle_;
scoped_ptr<base::win::PEImageAsData> disk_peimage_ptr_;
scoped_ptr<base::win::PEImage> mem_peimage_ptr_;
};
TEST_F(SafeBrowsingModuleVerifierWinTest, VerifyModuleUnmodified) {
std::set<std::string> modified_exports;
// Call VerifyModule before the module has been loaded, should fail.
EXPECT_EQ(MODULE_STATE_UNKNOWN,
VerifyModule(kTestDllName, &modified_exports));
EXPECT_EQ(0, modified_exports.size());
// On loading, the module should be identical (up to relocations) in memory as
// on disk.
SetUpTestDllAndPEImages();
EXPECT_EQ(MODULE_STATE_UNMODIFIED,
VerifyModule(kTestDllName, &modified_exports));
EXPECT_EQ(0, modified_exports.size());
}
TEST_F(SafeBrowsingModuleVerifierWinTest, VerifyModuleModified) {
std::set<std::string> modified_exports;
// Confirm the module is identical in memory as on disk before we begin.
SetUpTestDllAndPEImages();
EXPECT_EQ(MODULE_STATE_UNMODIFIED,
VerifyModule(kTestDllName, &modified_exports));
uint8_t* mem_code_addr = NULL;
uint8_t* disk_code_addr = NULL;
uint32_t code_size = 0;
EXPECT_TRUE(GetCodeAddrsAndSize(*mem_peimage_ptr_,
*disk_peimage_ptr_,
&mem_code_addr,
&disk_code_addr,
&code_size));
// Edit the first byte of the code section of the module (this may be before
// the address of any export).
uint8_t new_val = (*mem_code_addr) + 1;
SIZE_T bytes_written = 0;
WriteProcessMemory(GetCurrentProcess(),
mem_code_addr,
reinterpret_cast<void*>(&new_val),
1,
&bytes_written);
EXPECT_EQ(1, bytes_written);
// VerifyModule should detect the change.
EXPECT_EQ(MODULE_STATE_MODIFIED,
VerifyModule(kTestDllName, &modified_exports));
}
TEST_F(SafeBrowsingModuleVerifierWinTest, VerifyModuleExportModified) {
std::set<std::string> modified_exports;
// Confirm the module is identical in memory as on disk before we begin.
SetUpTestDllAndPEImages();
EXPECT_EQ(MODULE_STATE_UNMODIFIED,
VerifyModule(kTestDllName, &modified_exports));
modified_exports.clear();
// Edit the exported function, VerifyModule should now return the function
// name in modified_exports.
EditExport();
EXPECT_EQ(MODULE_STATE_MODIFIED,
VerifyModule(kTestDllName, &modified_exports));
EXPECT_EQ(1, modified_exports.size());
EXPECT_EQ(0, std::string(kTestExportName).compare(*modified_exports.begin()));
}
} // namespace safe_browsing
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <windows.h>
extern "C" {
// Have a dummy export so that the module gets an export table entry.
void DummyExport() {
}
}
BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) {
return true;
}
; Copyright 2014 The Chromium Authors. All rights reserved.
; Use of this source code is governed by a BSD-style license that can be
; found in the LICENSE file.
LIBRARY "verifier_test_dll.dll"
EXPORTS
DummyExport
{
'targets': [
{
'target_name': 'verifier_test_dll',
'type': 'loadable_module',
'sources': [
'verifier_test_dll.cc',
'verifier_test_dll.def',
],
},
],
}
\ No newline at end of file
......@@ -2672,6 +2672,8 @@
'browser/safe_browsing/incident_report_uploader_impl.h',
'browser/safe_browsing/last_download_finder.cc',
'browser/safe_browsing/last_download_finder.h',
'browser/safe_browsing/module_integrity_verifier_win.cc',
'browser/safe_browsing/module_integrity_verifier_win.h',
'browser/safe_browsing/path_sanitizer.cc',
'browser/safe_browsing/path_sanitizer.h',
'browser/safe_browsing/pe_image_reader_win.cc',
......
......@@ -1216,6 +1216,7 @@
'browser/safe_browsing/last_download_finder_unittest.cc',
'browser/safe_browsing/local_two_phase_testserver.cc',
'browser/safe_browsing/malware_details_unittest.cc',
'browser/safe_browsing/module_integrity_verifier_win_unittest.cc',
'browser/safe_browsing/path_sanitizer_unittest.cc',
'browser/safe_browsing/pe_image_reader_win_unittest.cc',
'browser/safe_browsing/ping_manager_unittest.cc',
......@@ -2463,6 +2464,7 @@
}],
['OS=="win"', {
'dependencies': [
'browser/safe_browsing/verifier_test/verifier_unittest.gyp:verifier_test_dll',
'chrome_version_resources',
'installer_util_strings',
'../chrome_elf/chrome_elf.gyp:blacklist_test_dll_1',
......
......@@ -106,6 +106,7 @@
'<(PRODUCT_DIR)/ffmpegsumo.dll',
'<(PRODUCT_DIR)/libexif.dll',
'<(PRODUCT_DIR)/osmesa.dll',
'<(PRODUCT_DIR)/verifier_test_dll.dll',
],
'isolate_dependency_untracked': [
'../ppapi/lib/gl/include/KHR/',
......
......@@ -25729,6 +25729,15 @@ Therefore, the affected-histogram name has to have at least one dot in it.
</summary>
</histogram>
<histogram name="SafeBrowsing.ModuleBaseRelocation" units="BaseRelocationType">
<owner>csharp@chromium.org</owner>
<owner>krstnmnlsn@chromium.org</owner>
<summary>
A windows only historgram. Records when an unknown base relocation type is
encountered while reading the reloc table of a loaded module.
</summary>
</histogram>
<histogram name="SB.BloomFilter" units="milliseconds">
<obsolete>
Has not been generated for years (7/8/14).
......@@ -36832,6 +36841,20 @@ Therefore, the affected-histogram name has to have at least one dot in it.
<int value="2" label="Failure"/>
</enum>
<enum name="BaseRelocationType" type="int">
<int value="0" label="IMAGE_REL_BASED_ABSOLUTE"/>
<int value="1" label="IMAGE_REL_BASED_HIGH"/>
<int value="2" label="IMAGE_REL_BASED_LOW"/>
<int value="3" label="IMAGE_REL_BASED_HIGHLOW"/>
<int value="4" label="IMAGE_REL_BASED_HIGHADJ"/>
<int value="5" label="IMAGE_REL_BASED_MACHINE_SPECIFIC_5"/>
<int value="6" label="IMAGE_REL_BASED_RESERVED"/>
<int value="7" label="IMAGE_REL_BASED_MACHINE_SPECIFIC_7"/>
<int value="8" label="IMAGE_REL_BASED_MACHINE_SPECIFIC_8"/>
<int value="9" label="IMAGE_REL_BASED_MACHINE_SPECIFIC_9"/>
<int value="10" label="IMAGE_REL_BASED_DIR64"/>
</enum>
<enum name="BatteryInfoSampleResult" type="int">
<int value="0" label="Read"/>
<int value="1" label="Good"/>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment