Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: chrome/browser/safe_browsing/module_integrity_verifier_unittest.cc

Issue 434163002: Changes to module_integrity_verifier.cc that allow the function VerifyModule (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@patchHunting2
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/safe_browsing/module_integrity_verifier.h" 5 #include "chrome/browser/safe_browsing/module_integrity_verifier.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/memory_mapped_file.h" 8 #include "base/files/memory_mapped_file.h"
9 #include "base/native_library.h" 9 #include "base/native_library.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/scoped_native_library.h" 11 #include "base/scoped_native_library.h"
12 #include "base/win/pe_image.h" 12 #include "base/win/pe_image.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace safe_browsing { 15 namespace safe_browsing {
16 16
17 const wchar_t kTestDllName[] = L"verifier_test_dll.dll"; 17 const wchar_t kTestDllName[] = L"verifier_test_dll.dll";
18 const char kTestExportName[] = "DummyExport";
18 19
19 class SafeBrowsingModuleVerifierTest : public testing::Test { 20 class SafeBrowsingModuleVerifierTest : public testing::Test {
20 protected: 21 protected:
21 SafeBrowsingModuleVerifierTest() {} 22 SafeBrowsingModuleVerifierTest() {}
22 virtual ~SafeBrowsingModuleVerifierTest() {} 23 virtual ~SafeBrowsingModuleVerifierTest() {}
23 24
24 base::ScopedNativeLibrary mem_dll_handle_; 25 base::ScopedNativeLibrary mem_dll_handle_;
25 base::MemoryMappedFile disk_dll_handle_; 26 base::MemoryMappedFile disk_dll_handle_;
26 scoped_ptr<base::win::PEImageAsData> disk_peimage_ptr_; 27 scoped_ptr<base::win::PEImageAsData> disk_peimage_ptr_;
27 scoped_ptr<base::win::PEImage> mem_peimage_ptr_; 28 scoped_ptr<base::win::PEImage> mem_peimage_ptr_;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 WCHAR module_path[MAX_PATH] = {}; 61 WCHAR module_path[MAX_PATH] = {};
61 DWORD length = 62 DWORD length =
62 GetModuleFileName(module_handle, module_path, arraysize(module_path)); 63 GetModuleFileName(module_handle, module_path, arraysize(module_path));
63 ASSERT_NE(length, arraysize(module_path)); 64 ASSERT_NE(length, arraysize(module_path));
64 65
65 ASSERT_TRUE(disk_dll_handle_.Initialize(base::FilePath(module_path))); 66 ASSERT_TRUE(disk_dll_handle_.Initialize(base::FilePath(module_path)));
66 *disk_handle = 67 *disk_handle =
67 reinterpret_cast<HMODULE>(const_cast<uint8*>(disk_dll_handle_.data())); 68 reinterpret_cast<HMODULE>(const_cast<uint8*>(disk_dll_handle_.data()));
68 } 69 }
69 70
70 private: 71 void EditExport(const char* export_name) {
71 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingModuleVerifierTest); 72 HMODULE mem_handle;
73 GetMemModuleHandle(&mem_handle);
74 uint8_t* export_addr =
75 reinterpret_cast<uint8_t*>(GetProcAddress(mem_handle, export_name));
76 EXPECT_TRUE(NULL != export_addr);
77
78 export_addr = export_addr;
79
80 // Edit the first byte of the function.
81 uint8_t new_val = (*export_addr) + 1;
82 SIZE_T bytes_written = 0;
83 WriteProcessMemory(GetCurrentProcess(),
84 export_addr,
85 reinterpret_cast<void*>(&new_val),
86 1,
87 &bytes_written);
88 EXPECT_EQ(1, bytes_written);
89 }
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingModuleVerifierTest);
72 }; 93 };
73 94
74 TEST_F(SafeBrowsingModuleVerifierTest, CountBytesDiffInPtr) {
75 // Construct test pointers and try with CountBytesDiffInPtr.
76 // The first Bytes differ.
77 uintptr_t num_a = 0xFF;
78 uintptr_t num_b = 0x00;
79
80 // Any inbetween Bytes are identical.
81 int num_bytes_to_add = sizeof(num_a) - 2;
82 for (int i = 0; i < num_bytes_to_add; ++i) {
83 num_a <<= 8;
84 num_b <<= 8;
85 num_a += 0xFF;
86 num_b += 0xFF;
87 }
88
89 // The last Bytes differ.
90 num_a <<= 8;
91 num_b <<= 8;
92 num_a += 0x0F;
93 num_b += 0xFF;
94
95 EXPECT_EQ(2, CountBytesDiffInPtr(num_a, num_b));
96 EXPECT_EQ(2, CountBytesDiffInPtr(num_b, num_a));
97 EXPECT_EQ(0, CountBytesDiffInPtr(num_a, num_a));
98 }
99
100 TEST_F(SafeBrowsingModuleVerifierTest, VerifyModuleUnmodified) { 95 TEST_F(SafeBrowsingModuleVerifierTest, VerifyModuleUnmodified) {
96 std::set<std::string> modified_exports;
101 // Call VerifyModule before the module has been loaded, should fail. 97 // Call VerifyModule before the module has been loaded, should fail.
102 EXPECT_EQ(MODULE_STATE_UNKNOWN, VerifyModule(kTestDllName)); 98 EXPECT_EQ(MODULE_STATE_UNKNOWN,
99 VerifyModule(kTestDllName, &modified_exports));
100 EXPECT_EQ(0, modified_exports.size());
103 101
104 // On loading, the module should be identical (up to relocations) in memory as 102 // On loading, the module should be identical (up to relocations) in memory as
105 // on disk. 103 // on disk.
106 SetUpTestDllAndPEImages(); 104 SetUpTestDllAndPEImages();
107 EXPECT_EQ(MODULE_STATE_UNMODIFIED, VerifyModule(kTestDllName)); 105 EXPECT_EQ(MODULE_STATE_UNMODIFIED,
106 VerifyModule(kTestDllName, &modified_exports));
107 EXPECT_EQ(0, modified_exports.size());
108 } 108 }
109 109
110 TEST_F(SafeBrowsingModuleVerifierTest, VerifyModuleModified) { 110 TEST_F(SafeBrowsingModuleVerifierTest, VerifyModuleModified) {
111 std::set<std::string> modified_exports;
111 // Confirm the module is identical in memory as on disk before we begin. 112 // Confirm the module is identical in memory as on disk before we begin.
112 SetUpTestDllAndPEImages(); 113 SetUpTestDllAndPEImages();
113 EXPECT_EQ(MODULE_STATE_UNMODIFIED, VerifyModule(kTestDllName)); 114 EXPECT_EQ(MODULE_STATE_UNMODIFIED,
115 VerifyModule(kTestDllName, &modified_exports));
114 116
115 uint8_t* mem_code_addr = NULL; 117 uint8_t* mem_code_addr = NULL;
116 uint8_t* disk_code_addr = NULL; 118 uint8_t* disk_code_addr = NULL;
117 uint32_t code_size = 0; 119 uint32_t code_size = 0;
118 EXPECT_TRUE(GetCodeAddrsAndSize(*mem_peimage_ptr_, 120 EXPECT_TRUE(GetCodeAddrsAndSize(*mem_peimage_ptr_,
119 *disk_peimage_ptr_, 121 *disk_peimage_ptr_,
120 &mem_code_addr, 122 &mem_code_addr,
121 &disk_code_addr, 123 &disk_code_addr,
122 &code_size)); 124 &code_size));
123 125
124 // Edit the first byte of the code section of the module. 126 // Edit the first byte of the code section of the module.
125 uint8_t new_val = (*mem_code_addr) + 1; 127 uint8_t new_val = (*mem_code_addr) + 1;
126 SIZE_T bytes_written = 0; 128 SIZE_T bytes_written = 0;
127 WriteProcessMemory(GetCurrentProcess(), 129 WriteProcessMemory(GetCurrentProcess(),
128 mem_code_addr, 130 mem_code_addr,
129 reinterpret_cast<void*>(&new_val), 131 reinterpret_cast<void*>(&new_val),
130 1, 132 1,
131 &bytes_written); 133 &bytes_written);
132 EXPECT_EQ(1, bytes_written); 134 EXPECT_EQ(1, bytes_written);
133 135
134 // VerifyModule should detect the change. 136 // VerifyModule should detect the change.
135 EXPECT_EQ(MODULE_STATE_MODIFIED, VerifyModule(kTestDllName)); 137 EXPECT_EQ(MODULE_STATE_MODIFIED,
138 VerifyModule(kTestDllName, &modified_exports));
139 }
140
141 TEST_F(SafeBrowsingModuleVerifierTest, VerifyModuleExportModified) {
142 std::set<std::string> modified_exports;
143 // Confirm the module is identical in memory as on disk before we begin.
144 SetUpTestDllAndPEImages();
145 EXPECT_EQ(MODULE_STATE_UNMODIFIED,
146 VerifyModule(kTestDllName, &modified_exports));
147 modified_exports.clear();
148
149 EditExport(kTestExportName);
150 EXPECT_EQ(MODULE_STATE_MODIFIED,
151 VerifyModule(kTestDllName, &modified_exports));
152 EXPECT_EQ(1, modified_exports.size());
136 } 153 }
137 154
138 } // namespace safe_browsing 155 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698