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

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

Issue 406043003: Adding the VerifyModule function (and helpers) to safe browsing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
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
(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/containers/hash_tables.h"
8 #include "base/files/file_path.h"
9 #include "base/files/memory_mapped_file.h"
10 #include "base/metrics/histogram.h"
11 #include "base/scoped_native_library.h"
12 #include "base/win/pe_image.h"
13
14 namespace safe_browsing {
15
16 struct ModuleVerificationState {
17 explicit ModuleVerificationState(HMODULE hModule);
18 ~ModuleVerificationState();
19
20 base::win::PEImageAsData disk_peimage;
21
22 // The module's preferred base address minus the base address it actually
23 // loaded at.
24 intptr_t image_base_delta;
25
26 // The location of the disk_peimage module's code section minus that of the
27 // mem_peimage module's code section.
28 intptr_t code_section_delta;
29
30 // The bytes corrected by relocs.
31 base::hash_set<uintptr_t> reloc_addr;
32
33 // Set true if the relocation table contains a reloc of type that we don't
34 // currently handle.
35 bool unknown_reloc_type;
36
37 private:
38 DISALLOW_COPY_AND_ASSIGN(ModuleVerificationState);
39 };
40
41 ModuleVerificationState::ModuleVerificationState(HMODULE hModule)
42 : disk_peimage(hModule),
43 image_base_delta(0),
44 code_section_delta(0),
45 reloc_addr(),
46 unknown_reloc_type(false) {
47 }
48
49 ModuleVerificationState::~ModuleVerificationState() {
50 }
51
52 namespace {
53
54 struct Export {
55 Export(void* addr, const std::string& name);
56 ~Export();
57
58 bool operator<(const Export& other) const;
59
60 void* addr;
61 std::string name;
62 };
63
64 Export::Export(void* addr, const std::string& name) : addr(addr), name(name) {
65 }
66
67 Export::~Export() {
68 }
69
70 bool Export::operator<(const Export& other) const {
71 return addr < other.addr;
72 }
73
74 bool ByteAccountedForByReloc(uint8_t* byte_addr,
75 ModuleVerificationState* state) {
76 return ((state->reloc_addr.count(reinterpret_cast<uintptr_t>(byte_addr))) >
77 0);
78 }
79
80 // Checks each byte in the module's code section again the corresponding byte on
81 // disk. The list |exports| must be sorted. Returns a list of
grt (UTC plus 2) 2014/08/06 15:07:08 similarly, "the list |exports|" could be simply "|
grt (UTC plus 2) 2014/08/06 15:07:08 comment nitpick: consider something like "Adds the
grt (UTC plus 2) 2014/08/06 15:07:08 "...on disk, returning the number of bytes differi
krstnmnlsn 2014/08/06 18:19:07 Triple Done
82 // the functions who may have been modified.
83 int ExamineBytesDiffInMemory(uint8_t* disk_code_start,
84 uint8_t* mem_code_start,
85 uint32_t code_size,
86 const std::vector<Export>& exports,
87 ModuleVerificationState* state,
grt (UTC plus 2) 2014/08/06 15:07:08 const ModuleVerificationState& state here and in B
krstnmnlsn 2014/08/06 18:19:07 Done.
88 std::set<std::string>* modified_exports) {
89 int bytes_different = 0;
90 std::vector<Export>::const_iterator export_it = exports.begin();
91
92 for (uint8_t* end = mem_code_start + code_size; mem_code_start != end;
93 ++mem_code_start) {
94 if ((*disk_code_start++ != *mem_code_start) &&
95 !ByteAccountedForByReloc(mem_code_start, state)) {
96 // We get the largest export address still smaller than |addr|. It is
97 // possible that |addr| belongs to some nonexported function located
98 // between this export and the following one.
99 Export addr(reinterpret_cast<void*>(mem_code_start), std::string());
100 std::vector<Export>::const_iterator modified_export_it =
101 std::upper_bound(export_it, exports.end(), addr);
102
103 if (modified_export_it != exports.begin())
104 modified_exports->insert((modified_export_it - 1)->name);
105 ++bytes_different;
106
107 // No later byte can belong to an earlier export.
108 export_it = modified_export_it;
109 }
110 }
111 return bytes_different;
112 }
113
114 // Adds to |state->reloc_addr| the bytes of the pointer at |address| that are
115 // corrected by adding |image_base_delta|.
116 void AddBytesCorrectedByReloc(uintptr_t address,
117 ModuleVerificationState* state) {
118 #if __BYTE_ORDER == __LITTLE_ENDIAN
grt (UTC plus 2) 2014/08/06 15:07:08 awesome. i think it's better to use the chromium m
krstnmnlsn 2014/08/06 18:19:07 Done.
119 # define OFFSET(i) i
120 #elif __BYTE_ORDER == __BIG_ENDIAN
121 # define OFFSET(i) (sizeof(uintptr_t) - i)
122 #endif
123
124 uintptr_t orig_mem_value = *reinterpret_cast<uintptr_t*>(address);
125 uintptr_t fixed_mem_value = orig_mem_value + state->image_base_delta;
126 uintptr_t disk_value =
127 *reinterpret_cast<uintptr_t*>(address + state->code_section_delta);
128
129 uintptr_t diff_before = orig_mem_value ^ disk_value;
130 uintptr_t shared_after = ~(fixed_mem_value ^ disk_value);
131 int i = 0;
132 for (uintptr_t fixed = diff_before & shared_after; fixed; fixed >>= 8, ++i) {
133 if (fixed & 0xFF)
134 state->reloc_addr.insert(address + OFFSET(i));
135 }
136 #undef OFFSET
137 }
138
139 bool AddrIsInCodeSection(void* address,
140 uint8_t* code_addr,
141 uint32_t code_size) {
142 return (code_addr <= address && address < code_addr + code_size);
143 }
144
145 bool EnumRelocsCallback(const base::win::PEImage& mem_peimage,
146 WORD type,
147 void* address,
148 void* cookie) {
149 ModuleVerificationState* state =
150 reinterpret_cast<ModuleVerificationState*>(cookie);
151
152 uint8_t* mem_code_addr = NULL;
153 uint8_t* disk_code_addr = NULL;
154 uint32_t code_size = 0;
155 if (!GetCodeAddrsAndSize(mem_peimage,
156 state->disk_peimage,
157 &mem_code_addr,
158 &disk_code_addr,
159 &code_size))
160 return false;
161
162 // If not in the code section return true to continue to the next reloc.
163 if (!AddrIsInCodeSection(address, mem_code_addr, code_size))
164 return true;
165
166 UMA_HISTOGRAM_ENUMERATION(
167 "SafeBrowsing.ModuleBaseRelocation", type, IMAGE_REL_BASED_DIR64 + 1);
168
169 switch (type) {
170 case IMAGE_REL_BASED_HIGHLOW: {
171 AddBytesCorrectedByReloc(reinterpret_cast<uintptr_t>(address), state);
172 break;
173 }
174 case IMAGE_REL_BASED_ABSOLUTE:
175 // Absolute type relocations are a noop, sometimes used to pad a section
176 // of relocations.
177 break;
178 default: {
179 // TODO(krstnmnlsn): Find a reliable description of the behaviour of the
180 // remaining types of relocation and handle them.
181 state->unknown_reloc_type = true;
182 break;
183 }
184 }
185 return true;
186 }
187
188 bool EnumExportsCallback(const base::win::PEImage& mem_peimage,
189 DWORD ordinal,
190 DWORD hint,
191 LPCSTR name,
192 PVOID function_addr,
193 LPCSTR forward,
194 PVOID cookie) {
195 std::vector<Export>* exports = reinterpret_cast<std::vector<Export>*>(cookie);
196 if (name)
197 exports->push_back(Export(function_addr, std::string(name)));
198 return true;
199 }
200
201 } // namespace
202
203 bool GetCodeAddrsAndSize(const base::win::PEImage& mem_peimage,
204 const base::win::PEImageAsData& disk_peimage,
205 uint8_t** mem_code_addr,
206 uint8_t** disk_code_addr,
207 uint32_t* code_size) {
208 DWORD base_of_code = mem_peimage.GetNTHeaders()->OptionalHeader.BaseOfCode;
209
210 // Get the address and size of the code section in the loaded module image.
211 PIMAGE_SECTION_HEADER mem_code_header =
212 mem_peimage.GetImageSectionFromAddr(mem_peimage.RVAToAddr(base_of_code));
213 if (mem_code_header == NULL)
214 return false;
215 *mem_code_addr = reinterpret_cast<uint8_t*>(
216 mem_peimage.RVAToAddr(mem_code_header->VirtualAddress));
217 // If the section is padded with zeros when mapped then |VirtualSize| can be
218 // larger. Alternatively, |SizeOfRawData| can be rounded up to align
219 // according to OptionalHeader.FileAlignment.
220 *code_size = std::min(mem_code_header->Misc.VirtualSize,
221 mem_code_header->SizeOfRawData);
222
223 // Get the address of the code section in the module mapped as data from disk.
224 DWORD disk_code_offset = 0;
225 if (!mem_peimage.ImageAddrToOnDiskOffset(
226 reinterpret_cast<void*>(*mem_code_addr), &disk_code_offset))
227 return false;
228 *disk_code_addr =
229 reinterpret_cast<uint8_t*>(disk_peimage.module()) + disk_code_offset;
230 return true;
231 }
232
233 ModuleState VerifyModule(const wchar_t* module_name,
234 std::set<std::string>* modified_exports) {
235 // Get module handle, load a copy from disk as data and create PEImages.
236 HMODULE module_handle = NULL;
237 if (!GetModuleHandleEx(0, module_name, &module_handle))
238 return MODULE_STATE_UNKNOWN;
239 base::ScopedNativeLibrary native_library(module_handle);
240
241 WCHAR module_path[MAX_PATH] = {};
242 DWORD length =
243 GetModuleFileName(module_handle, module_path, arraysize(module_path));
244 if (!length || length == arraysize(module_path))
245 return MODULE_STATE_UNKNOWN;
246
247 base::MemoryMappedFile mapped_module;
248 if (!mapped_module.Initialize(base::FilePath(module_path)))
249 return MODULE_STATE_UNKNOWN;
250 ModuleVerificationState state(
251 reinterpret_cast<HMODULE>(const_cast<uint8*>(mapped_module.data())));
252
253 base::win::PEImage mem_peimage(module_handle);
254 if (!mem_peimage.VerifyMagic() || !state.disk_peimage.VerifyMagic())
255 return MODULE_STATE_UNKNOWN;
256
257 // Get the list of exports.
258 std::vector<Export> exports;
259 mem_peimage.EnumExports(EnumExportsCallback, &exports);
260 std::sort(exports.begin(), exports.end());
261
262 // Get the addresses of the code sections then calculate |code_section_delta|
263 // and |image_base_delta|.
264 uint8_t* mem_code_addr = NULL;
265 uint8_t* disk_code_addr = NULL;
266 uint32_t code_size = 0;
267 if (!GetCodeAddrsAndSize(mem_peimage,
268 state.disk_peimage,
269 &mem_code_addr,
270 &disk_code_addr,
271 &code_size))
272 return MODULE_STATE_UNKNOWN;
273
274 state.code_section_delta = disk_code_addr - mem_code_addr;
275
276 uint8_t* preferred_image_base = reinterpret_cast<uint8_t*>(
277 state.disk_peimage.GetNTHeaders()->OptionalHeader.ImageBase);
278 state.image_base_delta =
279 preferred_image_base - reinterpret_cast<uint8_t*>(mem_peimage.module());
280
281 // Get the relocations.
282 mem_peimage.EnumRelocs(EnumRelocsCallback, &state);
283 if (state.unknown_reloc_type)
284 return MODULE_STATE_UNKNOWN;
285
286 // Count the modified bytes (after accounting for relocs) and get the set of
287 // modified functions.
288 int num_bytes_different = ExamineBytesDiffInMemory(disk_code_addr,
289 mem_code_addr,
290 code_size,
291 exports,
292 &state,
293 modified_exports);
294
295 return num_bytes_different ? MODULE_STATE_MODIFIED : MODULE_STATE_UNMODIFIED;
296 }
297
298 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698