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.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/files/memory_mapped_file.h" | |
9 #include "base/scoped_native_library.h" | |
10 #include "base/win/pe_image.h" | |
11 | |
12 namespace safe_browsing { | |
13 | |
14 namespace { | |
15 | |
16 struct RelocEnumerationState { | |
17 explicit RelocEnumerationState(HMODULE hModule); | |
18 ~RelocEnumerationState(); | |
19 | |
20 base::win::PEImageAsData disk_peimage; | |
21 | |
22 // The number of bytes made equivalent between memory and disk due to | |
23 // relocations. | |
24 int bytes_corrected_by_reloc; | |
25 | |
26 // Set true if the relocation table contains a reloc of type that we don't | |
27 // currently handle. | |
28 bool unknown_reloc_type; | |
29 | |
grt (UTC plus 2)
2014/07/31 17:03:33
private:
krstnmnlsn
2014/08/04 15:18:15
Done.
| |
30 DISALLOW_COPY_AND_ASSIGN(RelocEnumerationState); | |
31 }; | |
32 | |
33 RelocEnumerationState::RelocEnumerationState(HMODULE hModule) | |
34 : disk_peimage(hModule), | |
35 bytes_corrected_by_reloc(0), | |
36 unknown_reloc_type(false) { | |
37 } | |
38 | |
39 RelocEnumerationState::~RelocEnumerationState() { | |
40 } | |
41 | |
42 int CountBytesDiffInMemory(uint8_t* disk_code_start, | |
43 uint8_t* memory_code_start, | |
44 uint32_t code_size) { | |
45 int counter = 0; | |
46 for (int i = 0; i < static_cast<int>(code_size); ++i) { | |
grt (UTC plus 2)
2014/07/31 17:03:33
unsigned -> signed is implementation defined if th
krstnmnlsn
2014/08/04 15:18:14
Right, had tried to fix this in the other branch w
| |
47 if (*(disk_code_start + i) != *(memory_code_start + i)) | |
48 ++counter; | |
49 } | |
50 return counter; | |
51 } | |
52 | |
53 bool AddrIsInCodeSection(void* address, | |
54 uint8_t* code_addr, | |
55 uint32_t code_size) { | |
56 return (code_addr <= address && address < code_addr + code_size); | |
57 } | |
58 | |
59 bool EnumRelocsCallback(const base::win::PEImage& mem_peimage, | |
60 WORD type, | |
61 void* address, | |
62 void* cookie) { | |
63 RelocEnumerationState* reloc_enum_state = | |
64 reinterpret_cast<RelocEnumerationState*>(cookie); | |
65 const base::win::PEImageAsData* disk_peimage_ptr = | |
66 &reloc_enum_state->disk_peimage; | |
67 uint8_t* mem_code_addr = NULL; | |
68 uint8_t* disk_code_addr = NULL; | |
69 uint32_t code_size = 0; | |
70 if (!GetCodeAddrsAndSize(mem_peimage, | |
71 *disk_peimage_ptr, | |
72 &mem_code_addr, | |
73 &disk_code_addr, | |
74 &code_size)) | |
grt (UTC plus 2)
2014/07/31 17:03:34
i thought chromium style called for braces around
krstnmnlsn
2014/08/04 15:18:15
Don't see anything. They do say we can add them i
grt (UTC plus 2)
2014/08/05 15:51:54
Naw, if cl-format is happy, I'm happy.
| |
75 return false; | |
76 | |
77 // If not in the code section return true to continue to the next reloc. | |
78 if (!AddrIsInCodeSection(address, mem_code_addr, code_size)) | |
79 return true; | |
80 | |
81 switch (type) { | |
82 case IMAGE_REL_BASED_HIGHLOW: { | |
83 uint8_t* preferred_image_base = reinterpret_cast<uint8_t*>( | |
84 disk_peimage_ptr->GetNTHeaders()->OptionalHeader.ImageBase); | |
85 uintptr_t delta = preferred_image_base - | |
86 reinterpret_cast<uint8_t*>(mem_peimage.module()); | |
grt (UTC plus 2)
2014/07/31 17:03:33
why cast to uint8_t* and then assign to uintptr_t?
krstnmnlsn
2014/08/04 15:18:15
The original plan was to store pointers as pointer
grt (UTC plus 2)
2014/08/05 15:51:55
Ah, I misread this before. Deltas are often signed
krstnmnlsn
2014/08/05 19:17:47
Right, that sounds perfect to me. This code has b
| |
87 uint8_t* new_value = (*reinterpret_cast<uint8_t**>(address)) + delta; | |
88 int bytes_corrected = | |
89 CountBytesDiffInPtr(reinterpret_cast<uintptr_t>(new_value), | |
90 *reinterpret_cast<uintptr_t*>(address)); | |
91 | |
92 // Check that the adding delta corrected the value to agree with disk. | |
93 uint8_t** disk_address = reinterpret_cast<uint8_t**>( | |
94 reinterpret_cast<uint8_t*>(address) - mem_code_addr + disk_code_addr); | |
95 if (new_value == *disk_address) { | |
grt (UTC plus 2)
2014/07/31 17:03:34
nit: remove braces
krstnmnlsn
2014/08/04 15:18:14
Done.
| |
96 reloc_enum_state->bytes_corrected_by_reloc += bytes_corrected; | |
97 } | |
98 break; | |
99 } | |
100 case IMAGE_REL_BASED_ABSOLUTE: | |
101 // Absolute type relocations are a noop, sometimes used to pad a section | |
102 // of relocations. | |
103 break; | |
104 default: { | |
grt (UTC plus 2)
2014/07/31 17:03:33
nit: remove braces
krstnmnlsn
2014/08/04 15:18:15
Done.
| |
105 // TODO(krstnmnlsn): Find a reliable description of the behaviour of the | |
106 // remaining types of relocation and handle them. | |
107 reloc_enum_state->unknown_reloc_type = true; | |
grt (UTC plus 2)
2014/07/31 17:03:33
consider putting:
UMA_HISTOGRAM_ENUMERATION("Saf
krstnmnlsn
2014/08/04 15:18:14
I like this idea! Added a histogram, I made the v
grt (UTC plus 2)
2014/08/05 15:51:55
Can you clarify the question? What are the machine
krstnmnlsn
2014/08/05 19:17:47
So in my enum I have things like
"<int value="5"
grt (UTC plus 2)
2014/08/06 01:52:49
No, just storing the values is fine. We have CPU i
| |
108 break; | |
109 } | |
110 } | |
111 return true; | |
112 } | |
113 | |
114 } // namespace | |
115 | |
116 bool GetCodeAddrsAndSize(const base::win::PEImage& mem_peimage, | |
117 const base::win::PEImageAsData& disk_peimage, | |
118 uint8_t** mem_code_addr, | |
119 uint8_t** disk_code_addr, | |
120 uint32_t* code_size) { | |
121 DWORD base_of_code = mem_peimage.GetNTHeaders()->OptionalHeader.BaseOfCode; | |
122 | |
123 // Get the address and size of the code section in the loaded module image. | |
124 PIMAGE_SECTION_HEADER mem_code_header = | |
125 mem_peimage.GetImageSectionFromAddr(mem_peimage.RVAToAddr(base_of_code)); | |
126 if (mem_code_header == NULL) | |
127 return false; | |
128 *mem_code_addr = reinterpret_cast<uint8_t*>( | |
129 mem_peimage.RVAToAddr(mem_code_header->VirtualAddress)); | |
130 *code_size = mem_code_header->Misc.VirtualSize; | |
131 | |
132 // Get the address of the code section in the module mapped as data from disk. | |
133 // Section size will be the same as in the loaded module. | |
134 DWORD disk_code_offset = 0; | |
135 if (!mem_peimage.ImageAddrToOnDiskOffset( | |
136 reinterpret_cast<void*>(*mem_code_addr), &disk_code_offset)) | |
137 return false; | |
138 *disk_code_addr = | |
139 reinterpret_cast<uint8_t*>(disk_peimage.module()) + disk_code_offset; | |
140 return true; | |
141 } | |
142 | |
143 int CountBytesDiffInPtr(uintptr_t num_a, uintptr_t num_b) { | |
144 int num_bytes = 0; | |
145 for (int i = 0; i < sizeof(num_a); ++i) { | |
grt (UTC plus 2)
2014/07/31 17:03:33
is this equivalent?
for (uintptr_t delta = num_a
krstnmnlsn
2014/08/04 15:18:14
right, why not take the difference right away. th
| |
146 if ((num_a & 0xFF) != (num_b & 0xFF)) | |
147 ++num_bytes; | |
148 num_a >>= 8; | |
149 num_b >>= 8; | |
150 } | |
151 return num_bytes; | |
152 } | |
153 | |
154 ModuleState VerifyModule(const wchar_t* module_name) { | |
155 HMODULE module_handle = NULL; | |
156 if (!GetModuleHandleEx(0, module_name, &module_handle)) | |
157 return MODULE_STATE_UNKNOWN; | |
158 base::ScopedNativeLibrary native_library(module_handle); | |
159 | |
160 WCHAR module_path[MAX_PATH] = {}; | |
161 DWORD length = | |
162 GetModuleFileName(module_handle, module_path, arraysize(module_path)); | |
163 if (!length || length == arraysize(module_path)) | |
164 return MODULE_STATE_UNKNOWN; | |
165 | |
166 base::MemoryMappedFile mapped_module; | |
167 if (!mapped_module.Initialize(base::FilePath(module_path))) | |
168 return MODULE_STATE_UNKNOWN; | |
169 RelocEnumerationState reloc_enum_state( | |
170 reinterpret_cast<HMODULE>(const_cast<uint8*>(mapped_module.data()))); | |
171 | |
172 base::win::PEImage mem_peimage(module_handle); | |
173 if (!mem_peimage.VerifyMagic() || | |
174 !reloc_enum_state.disk_peimage.VerifyMagic()) | |
175 return MODULE_STATE_UNKNOWN; | |
176 | |
177 uint8_t* mem_code_addr = NULL; | |
178 uint8_t* disk_code_addr = NULL; | |
179 uint32_t code_size = 0; | |
180 if (!GetCodeAddrsAndSize(mem_peimage, | |
181 reloc_enum_state.disk_peimage, | |
182 &mem_code_addr, | |
183 &disk_code_addr, | |
184 &code_size)) | |
185 return MODULE_STATE_UNKNOWN; | |
186 | |
187 mem_peimage.EnumRelocs(EnumRelocsCallback, &reloc_enum_state); | |
188 if (reloc_enum_state.unknown_reloc_type) | |
189 return MODULE_STATE_UNKNOWN; | |
190 | |
191 int num_bytes_different = | |
192 CountBytesDiffInMemory(disk_code_addr, mem_code_addr, code_size); | |
193 if (num_bytes_different == reloc_enum_state.bytes_corrected_by_reloc) | |
194 return MODULE_STATE_UNMODIFIED; | |
195 return MODULE_STATE_MODIFIED; | |
196 } | |
197 | |
198 } // namespace safe_browsing | |
OLD | NEW |