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 SafeBrowsingModuleVerifierTest : public testing::Test { | |
25 protected: | |
26 base::ScopedNativeLibrary mem_dll_handle_; | |
grt (UTC plus 2)
2014/08/05 15:51:56
data members come below methods (http://google-sty
krstnmnlsn
2014/08/05 19:17:49
Done.
| |
27 base::MemoryMappedFile disk_dll_handle_; | |
28 scoped_ptr<base::win::PEImageAsData> disk_peimage_ptr_; | |
29 scoped_ptr<base::win::PEImage> mem_peimage_ptr_; | |
30 | |
31 void SetUpTestDllAndPEImages() { | |
32 LoadModule(); | |
33 HMODULE mem_handle; | |
34 GetMemModuleHandle(&mem_handle); | |
35 mem_peimage_ptr_.reset(new base::win::PEImage(mem_handle)); | |
36 ASSERT_TRUE(mem_peimage_ptr_->VerifyMagic()); | |
37 | |
38 LoadDLLAsFile(); | |
39 HMODULE disk_handle; | |
40 GetDiskModuleHandle(&disk_handle); | |
41 disk_peimage_ptr_.reset(new base::win::PEImageAsData(disk_handle)); | |
42 ASSERT_TRUE(disk_peimage_ptr_->VerifyMagic()); | |
43 } | |
44 | |
45 void LoadModule() { | |
46 mem_dll_handle_.Reset( | |
47 LoadNativeLibrary(base::FilePath(kTestDllName), NULL)); | |
48 ASSERT_TRUE(mem_dll_handle_.is_valid()); | |
49 } | |
50 | |
51 void GetMemModuleHandle(HMODULE* mem_handle) { | |
52 *mem_handle = GetModuleHandle(kTestDllName); | |
53 ASSERT_NE(static_cast<HMODULE>(NULL), *mem_handle); | |
54 } | |
55 | |
56 void LoadDLLAsFile() { | |
57 // Use the module handle to find the it on disk, then load as a file. | |
58 HMODULE module_handle; | |
59 GetMemModuleHandle(&module_handle); | |
60 | |
61 WCHAR module_path[MAX_PATH] = {}; | |
62 DWORD length = | |
63 GetModuleFileName(module_handle, module_path, arraysize(module_path)); | |
64 ASSERT_NE(arraysize(module_path), length); | |
65 ASSERT_TRUE(disk_dll_handle_.Initialize(base::FilePath(module_path))); | |
66 } | |
67 | |
68 void GetDiskModuleHandle(HMODULE* disk_handle) { | |
69 *disk_handle = | |
70 reinterpret_cast<HMODULE>(const_cast<uint8*>(disk_dll_handle_.data())); | |
71 } | |
72 | |
73 // Edits the first byte of the single function exported by the test dll. | |
74 void EditExport() { | |
75 HMODULE mem_handle; | |
76 GetMemModuleHandle(&mem_handle); | |
77 uint8_t* export_addr = | |
78 reinterpret_cast<uint8_t*>(GetProcAddress(mem_handle, kTestExportName)); | |
79 EXPECT_NE(reinterpret_cast<uint8_t*>(NULL), export_addr); | |
80 | |
81 // Edit the first byte of the function. | |
82 uint8_t new_val = (*export_addr) + 1; | |
83 SIZE_T bytes_written = 0; | |
84 WriteProcessMemory(GetCurrentProcess(), | |
85 export_addr, | |
86 reinterpret_cast<void*>(&new_val), | |
87 1, | |
88 &bytes_written); | |
89 EXPECT_EQ(1, bytes_written); | |
90 } | |
91 }; | |
92 | |
93 TEST_F(SafeBrowsingModuleVerifierTest, VerifyModuleUnmodified) { | |
94 std::set<std::string> modified_exports; | |
95 // Call VerifyModule before the module has been loaded, should fail. | |
96 EXPECT_EQ(MODULE_STATE_UNKNOWN, | |
97 VerifyModule(kTestDllName, &modified_exports)); | |
98 EXPECT_EQ(0, modified_exports.size()); | |
99 | |
100 // On loading, the module should be identical (up to relocations) in memory as | |
101 // on disk. | |
102 SetUpTestDllAndPEImages(); | |
103 EXPECT_EQ(MODULE_STATE_UNMODIFIED, | |
104 VerifyModule(kTestDllName, &modified_exports)); | |
105 EXPECT_EQ(0, modified_exports.size()); | |
106 } | |
107 | |
108 TEST_F(SafeBrowsingModuleVerifierTest, VerifyModuleModified) { | |
109 std::set<std::string> modified_exports; | |
110 // Confirm the module is identical in memory as on disk before we begin. | |
111 SetUpTestDllAndPEImages(); | |
112 EXPECT_EQ(MODULE_STATE_UNMODIFIED, | |
113 VerifyModule(kTestDllName, &modified_exports)); | |
114 | |
115 uint8_t* mem_code_addr = NULL; | |
116 uint8_t* disk_code_addr = NULL; | |
117 uint32_t code_size = 0; | |
118 EXPECT_TRUE(GetCodeAddrsAndSize(*mem_peimage_ptr_, | |
119 *disk_peimage_ptr_, | |
120 &mem_code_addr, | |
121 &disk_code_addr, | |
122 &code_size)); | |
123 | |
124 // Edit the first byte of the code section of the module (this may be before | |
125 // the address of any export). | |
126 uint8_t new_val = (*mem_code_addr) + 1; | |
127 SIZE_T bytes_written = 0; | |
128 WriteProcessMemory(GetCurrentProcess(), | |
129 mem_code_addr, | |
130 reinterpret_cast<void*>(&new_val), | |
131 1, | |
132 &bytes_written); | |
133 EXPECT_EQ(1, bytes_written); | |
134 | |
135 // VerifyModule should detect the change. | |
136 EXPECT_EQ(MODULE_STATE_MODIFIED, | |
137 VerifyModule(kTestDllName, &modified_exports)); | |
138 } | |
139 | |
140 TEST_F(SafeBrowsingModuleVerifierTest, VerifyModuleExportModified) { | |
141 std::set<std::string> modified_exports; | |
142 // Confirm the module is identical in memory as on disk before we begin. | |
143 SetUpTestDllAndPEImages(); | |
144 EXPECT_EQ(MODULE_STATE_UNMODIFIED, | |
145 VerifyModule(kTestDllName, &modified_exports)); | |
146 modified_exports.clear(); | |
147 | |
148 // Edit the exported function, VerifyModule should now return the function | |
149 // name in modified_exports. | |
150 EditExport(); | |
151 EXPECT_EQ(MODULE_STATE_MODIFIED, | |
152 VerifyModule(kTestDllName, &modified_exports)); | |
153 EXPECT_EQ(1, modified_exports.size()); | |
154 EXPECT_EQ(0, std::string(kTestExportName).compare(*modified_exports.begin())); | |
155 } | |
156 | |
157 } // namespace safe_browsing | |
OLD | NEW |