OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/module_integrity_verifier_win.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/memory_mapped_file.h" |
| 9 #include "base/native_library.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/scoped_native_library.h" |
| 12 #include "base/win/pe_image.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace safe_browsing { |
| 16 |
| 17 namespace { |
| 18 |
| 19 const wchar_t kTestDllName[] = L"verifier_test_dll.dll"; |
| 20 const char kTestExportName[] = "DummyExport"; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 class SafeBrowsingModuleVerifierWinTest : public testing::Test { |
| 25 protected: |
| 26 void SetUpTestDllAndPEImages() { |
| 27 LoadModule(); |
| 28 HMODULE mem_handle; |
| 29 GetMemModuleHandle(&mem_handle); |
| 30 mem_peimage_ptr_.reset(new base::win::PEImage(mem_handle)); |
| 31 ASSERT_TRUE(mem_peimage_ptr_->VerifyMagic()); |
| 32 |
| 33 LoadDLLAsFile(); |
| 34 HMODULE disk_handle; |
| 35 GetDiskModuleHandle(&disk_handle); |
| 36 disk_peimage_ptr_.reset(new base::win::PEImageAsData(disk_handle)); |
| 37 ASSERT_TRUE(disk_peimage_ptr_->VerifyMagic()); |
| 38 } |
| 39 |
| 40 void LoadModule() { |
| 41 HMODULE mem_dll_handle = |
| 42 LoadNativeLibrary(base::FilePath(kTestDllName), NULL); |
| 43 ASSERT_NE(static_cast<HMODULE>(NULL), mem_dll_handle) |
| 44 << "GLE=" << GetLastError(); |
| 45 mem_dll_handle_.Reset(mem_dll_handle); |
| 46 ASSERT_TRUE(mem_dll_handle_.is_valid()); |
| 47 } |
| 48 |
| 49 void GetMemModuleHandle(HMODULE* mem_handle) { |
| 50 *mem_handle = GetModuleHandle(kTestDllName); |
| 51 ASSERT_NE(static_cast<HMODULE>(NULL), *mem_handle); |
| 52 } |
| 53 |
| 54 void LoadDLLAsFile() { |
| 55 // Use the module handle to find the it on disk, then load as a file. |
| 56 HMODULE module_handle; |
| 57 GetMemModuleHandle(&module_handle); |
| 58 |
| 59 WCHAR module_path[MAX_PATH] = {}; |
| 60 DWORD length = |
| 61 GetModuleFileName(module_handle, module_path, arraysize(module_path)); |
| 62 ASSERT_NE(arraysize(module_path), length); |
| 63 ASSERT_TRUE(disk_dll_handle_.Initialize(base::FilePath(module_path))); |
| 64 } |
| 65 |
| 66 void GetDiskModuleHandle(HMODULE* disk_handle) { |
| 67 *disk_handle = |
| 68 reinterpret_cast<HMODULE>(const_cast<uint8*>(disk_dll_handle_.data())); |
| 69 } |
| 70 |
| 71 // Edits the first byte of the single function exported by the test dll. |
| 72 void EditExport() { |
| 73 HMODULE mem_handle; |
| 74 GetMemModuleHandle(&mem_handle); |
| 75 uint8_t* export_addr = |
| 76 reinterpret_cast<uint8_t*>(GetProcAddress(mem_handle, kTestExportName)); |
| 77 EXPECT_NE(reinterpret_cast<uint8_t*>(NULL), export_addr); |
| 78 |
| 79 // Edit the first byte of the function. |
| 80 uint8_t new_val = (*export_addr) + 1; |
| 81 SIZE_T bytes_written = 0; |
| 82 WriteProcessMemory(GetCurrentProcess(), |
| 83 export_addr, |
| 84 reinterpret_cast<void*>(&new_val), |
| 85 1, |
| 86 &bytes_written); |
| 87 EXPECT_EQ(1, bytes_written); |
| 88 } |
| 89 |
| 90 base::ScopedNativeLibrary mem_dll_handle_; |
| 91 base::MemoryMappedFile disk_dll_handle_; |
| 92 scoped_ptr<base::win::PEImageAsData> disk_peimage_ptr_; |
| 93 scoped_ptr<base::win::PEImage> mem_peimage_ptr_; |
| 94 }; |
| 95 |
| 96 TEST_F(SafeBrowsingModuleVerifierWinTest, VerifyModuleUnmodified) { |
| 97 std::set<std::string> modified_exports; |
| 98 // Call VerifyModule before the module has been loaded, should fail. |
| 99 EXPECT_EQ(MODULE_STATE_UNKNOWN, |
| 100 VerifyModule(kTestDllName, &modified_exports)); |
| 101 EXPECT_EQ(0, modified_exports.size()); |
| 102 |
| 103 // On loading, the module should be identical (up to relocations) in memory as |
| 104 // on disk. |
| 105 SetUpTestDllAndPEImages(); |
| 106 EXPECT_EQ(MODULE_STATE_UNMODIFIED, |
| 107 VerifyModule(kTestDllName, &modified_exports)); |
| 108 EXPECT_EQ(0, modified_exports.size()); |
| 109 } |
| 110 |
| 111 TEST_F(SafeBrowsingModuleVerifierWinTest, VerifyModuleModified) { |
| 112 std::set<std::string> modified_exports; |
| 113 // Confirm the module is identical in memory as on disk before we begin. |
| 114 SetUpTestDllAndPEImages(); |
| 115 EXPECT_EQ(MODULE_STATE_UNMODIFIED, |
| 116 VerifyModule(kTestDllName, &modified_exports)); |
| 117 |
| 118 uint8_t* mem_code_addr = NULL; |
| 119 uint8_t* disk_code_addr = NULL; |
| 120 uint32_t code_size = 0; |
| 121 EXPECT_TRUE(GetCodeAddrsAndSize(*mem_peimage_ptr_, |
| 122 *disk_peimage_ptr_, |
| 123 &mem_code_addr, |
| 124 &disk_code_addr, |
| 125 &code_size)); |
| 126 |
| 127 // Edit the first byte of the code section of the module (this may be before |
| 128 // the address of any export). |
| 129 uint8_t new_val = (*mem_code_addr) + 1; |
| 130 SIZE_T bytes_written = 0; |
| 131 WriteProcessMemory(GetCurrentProcess(), |
| 132 mem_code_addr, |
| 133 reinterpret_cast<void*>(&new_val), |
| 134 1, |
| 135 &bytes_written); |
| 136 EXPECT_EQ(1, bytes_written); |
| 137 |
| 138 // VerifyModule should detect the change. |
| 139 EXPECT_EQ(MODULE_STATE_MODIFIED, |
| 140 VerifyModule(kTestDllName, &modified_exports)); |
| 141 } |
| 142 |
| 143 TEST_F(SafeBrowsingModuleVerifierWinTest, VerifyModuleExportModified) { |
| 144 std::set<std::string> modified_exports; |
| 145 // Confirm the module is identical in memory as on disk before we begin. |
| 146 SetUpTestDllAndPEImages(); |
| 147 EXPECT_EQ(MODULE_STATE_UNMODIFIED, |
| 148 VerifyModule(kTestDllName, &modified_exports)); |
| 149 modified_exports.clear(); |
| 150 |
| 151 // Edit the exported function, VerifyModule should now return the function |
| 152 // name in modified_exports. |
| 153 EditExport(); |
| 154 EXPECT_EQ(MODULE_STATE_MODIFIED, |
| 155 VerifyModule(kTestDllName, &modified_exports)); |
| 156 EXPECT_EQ(1, modified_exports.size()); |
| 157 EXPECT_EQ(0, std::string(kTestExportName).compare(*modified_exports.begin())); |
| 158 } |
| 159 |
| 160 } // namespace safe_browsing |
OLD | NEW |