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

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: Copying over changes so that modified exported functions are returned by VerifyModule 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 <set>
grt (UTC plus 2) 2014/08/05 15:51:55 #include "base/containers/hash_tables.h"
krstnmnlsn 2014/08/05 19:17:48 Done.
8
9 #include "base/files/file_path.h"
10 #include "base/files/memory_mapped_file.h"
11 #include "base/metrics/histogram.h"
12 #include "base/scoped_native_library.h"
13 #include "base/win/pe_image.h"
14
15 namespace safe_browsing {
16
17 struct ModuleVerificationState {
18 explicit ModuleVerificationState(HMODULE hModule);
19 ~ModuleVerificationState();
20
21 base::win::PEImageAsData disk_peimage;
22
23 // The module's preferred base address minus the base address it actually
24 // loaded at.
25 uintptr_t image_base_delta;
26
27 // The location of the disk_peimage module's code section minus that of the
28 // mem_peimage module's code section.
29 uintptr_t code_section_delta;
30
31 // The bytes corrected by relocs.
32 base::hash_set<uintptr_t> reloc_addr;
33
34 // Set true if the relocation table contains a reloc of type that we don't
35 // currently handle.
36 bool unknown_reloc_type;
37
38 private:
39 DISALLOW_COPY_AND_ASSIGN(ModuleVerificationState);
40 };
41
42 ModuleVerificationState::ModuleVerificationState(HMODULE hModule)
43 : disk_peimage(hModule),
44 image_base_delta(0),
45 code_section_delta(0),
46 reloc_addr(),
47 unknown_reloc_type(false) {
48 }
49
50 ModuleVerificationState::~ModuleVerificationState() {
51 }
52
53 namespace {
54
55 struct Export {
56 Export(void* addr, std::string name);
grt (UTC plus 2) 2014/08/05 15:51:56 const std::string&
krstnmnlsn 2014/08/05 19:17:48 Done.
57 ~Export();
58
59 bool operator<(const Export& other) const;
60
61 void* addr;
62 std::string name;
63 };
64
65 Export::Export(void* addr, std::string name) : addr(addr), name(name) {
66 }
67
68 Export::~Export() {
69 }
70
71 bool Export::operator<(const Export& other) const {
72 return addr < other.addr;
73 }
74
75 bool ByteAccountedForByReloc(uint8_t* byte_addr,
76 ModuleVerificationState* state) {
77 return ((state->reloc_addr.count(reinterpret_cast<uintptr_t>(byte_addr))) >
78 0);
79 }
80
81 // Checks each byte in the module's code section again the corresponding byte on
grt (UTC plus 2) 2014/08/05 15:51:55 please add to the doc comment that |exports| must
krstnmnlsn 2014/08/05 19:17:48 Done.
82 // disk. Returns a list of 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 std::vector<Export> exports,
grt (UTC plus 2) 2014/08/05 15:51:55 const std::vector<Export>&
krstnmnlsn 2014/08/05 19:17:48 Done.
87 ModuleVerificationState* state,
88 std::set<std::string>* modified_exports) {
89 int bytes_different = 0;
90 std::vector<Export>::iterator export_it = exports.begin();
grt (UTC plus 2) 2014/08/05 15:51:55 const_iterator
krstnmnlsn 2014/08/05 19:17:48 Done.
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 =
grt (UTC plus 2) 2014/08/05 15:51:56 Export addr(reinterpret_cast<void*>(mem_code_start
krstnmnlsn 2014/08/05 19:17:49 Done.
100 Export(reinterpret_cast<void*>(mem_code_start), std::string());
101 std::vector<Export>::iterator modified_export_it =
102 std::upper_bound(export_it, exports.end(), addr);
103
104 if (modified_export_it != exports.begin())
105 modified_exports->insert((modified_export_it - 1)->name);
106 ++bytes_different;
107
108 // No later byte can belong to an earlier export.
109 export_it = modified_export_it;
110 }
111 }
112 return bytes_different;
113 }
114
115 // Adds to |state->reloc_addr| the bytes of the pointer at |address| that are
116 // corrected by adding |image_base_delta|.
117 void AddBytesCorrectedByReloc(ModuleVerificationState* state,
grt (UTC plus 2) 2014/08/05 15:51:55 swap args since |state| is modified
krstnmnlsn 2014/08/05 19:17:49 Done.
118 uintptr_t address) {
119 uintptr_t orig_mem_value = *reinterpret_cast<uintptr_t*>(address);
120 uintptr_t fixed_mem_value = orig_mem_value + state->image_base_delta;
121 uintptr_t disk_value =
122 *reinterpret_cast<uintptr_t*>(address + state->code_section_delta);
123
124 uintptr_t diff_before = orig_mem_value ^ disk_value;
125 uintptr_t shared_after = ~(fixed_mem_value ^ disk_value);
126 int i = 0;
127 for (uintptr_t fixed = diff_before & shared_after; fixed; fixed >>= 8) {
128 if (fixed & 0xFF) {
grt (UTC plus 2) 2014/08/05 15:51:56 nit: no braces
krstnmnlsn 2014/08/05 19:17:48 Done.
129 state->reloc_addr.insert(address + i);
grt (UTC plus 2) 2014/08/05 15:51:55 this depends on the endianness of the machine, doe
krstnmnlsn 2014/08/05 19:17:49 Oh hmm. I do care if windows ever stores big endi
grt (UTC plus 2) 2014/08/06 01:52:49 maybe something like: #if defined(ARCH_CPU_LITTLE_
130 }
131 ++i;
grt (UTC plus 2) 2014/08/05 15:51:55 put this in the for loop (fixed >>= 8, ++i)? i thi
krstnmnlsn 2014/08/05 19:17:48 With (one) space to spare!
132 }
133 }
134
135 bool AddrIsInCodeSection(void* address,
136 uint8_t* code_addr,
137 uint32_t code_size) {
138 return (code_addr <= address && address < code_addr + code_size);
139 }
140
141 bool EnumRelocsCallback(const base::win::PEImage& mem_peimage,
142 WORD type,
143 void* address,
144 void* cookie) {
145 ModuleVerificationState* state =
146 reinterpret_cast<ModuleVerificationState*>(cookie);
147
148 uint8_t* mem_code_addr = NULL;
149 uint8_t* disk_code_addr = NULL;
150 uint32_t code_size = 0;
151 if (!GetCodeAddrsAndSize(mem_peimage,
152 state->disk_peimage,
153 &mem_code_addr,
154 &disk_code_addr,
155 &code_size))
156 return false;
157
158 // If not in the code section return true to continue to the next reloc.
159 if (!AddrIsInCodeSection(address, mem_code_addr, code_size))
160 return true;
161
162 switch (type) {
163 case IMAGE_REL_BASED_HIGHLOW: {
164 AddBytesCorrectedByReloc(state, reinterpret_cast<uintptr_t>(address));
165 break;
166 }
167 case IMAGE_REL_BASED_ABSOLUTE:
168 // Absolute type relocations are a noop, sometimes used to pad a section
169 // of relocations.
170 break;
171 default: {
172 // TODO(krstnmnlsn): Find a reliable description of the behaviour of the
173 // remaining types of relocation and handle them.
174 UMA_HISTOGRAM_ENUMERATION(
grt (UTC plus 2) 2014/08/05 15:51:56 if this is done on line 161, then you'll be able t
krstnmnlsn 2014/08/05 19:17:48 That would probably give a better picture yes.
175 "SafeBrowsing.ModuleBaseRelocation", type, IMAGE_REL_BASED_DIR64 + 1);
Alexei Svitkine (slow) 2014/08/05 14:51:21 How do you know |type| will always be in range? P
grt (UTC plus 2) 2014/08/05 15:51:55 If it's out of range, the PE image is malformed in
krstnmnlsn 2014/08/05 19:17:48 If it doesn't have an overflow bucket I could add
Alexei Svitkine (slow) 2014/08/05 19:27:07 Yes, there will be an overflow bucket by default.
176 state->unknown_reloc_type = true;
177 break;
178 }
179 }
180 return true;
181 }
182
183 bool EnumExportsCallback(const base::win::PEImage& mem_peimage,
184 DWORD ordinal,
185 DWORD hint,
186 LPCSTR name,
187 PVOID function_addr,
188 LPCSTR forward,
189 PVOID cookie) {
190 std::vector<Export>* exports = reinterpret_cast<std::vector<Export>*>(cookie);
191 if (name) {
grt (UTC plus 2) 2014/08/05 15:51:56 nit: no braces
krstnmnlsn 2014/08/05 19:17:48 Done.
192 exports->push_back(Export(function_addr, std::string(name)));
193 }
grt (UTC plus 2) 2014/08/05 15:51:55 do you care about handling exports-by-ordinal?
krstnmnlsn 2014/08/05 19:17:48 I'm not sure what you mean? Currently, the code i
grt (UTC plus 2) 2014/08/06 01:52:49 Functions can be exported/imported by ordinal rath
194 return true;
195 }
196
197 } // namespace
198
199 bool GetCodeAddrsAndSize(const base::win::PEImage& mem_peimage,
200 const base::win::PEImageAsData& disk_peimage,
201 uint8_t** mem_code_addr,
202 uint8_t** disk_code_addr,
203 uint32_t* code_size) {
204 DWORD base_of_code = mem_peimage.GetNTHeaders()->OptionalHeader.BaseOfCode;
205
206 // Get the address and size of the code section in the loaded module image.
207 PIMAGE_SECTION_HEADER mem_code_header =
208 mem_peimage.GetImageSectionFromAddr(mem_peimage.RVAToAddr(base_of_code));
209 if (mem_code_header == NULL)
210 return false;
211 *mem_code_addr = reinterpret_cast<uint8_t*>(
212 mem_peimage.RVAToAddr(mem_code_header->VirtualAddress));
213 // If the section is padded with zeros when mapped then |VirtualSize| can be
214 // larger. Alternatively, |SizeOfRawData| can be rounded up to align
215 // according to OptionalHeader.FileAlignment.
216 *code_size = std::min(mem_code_header->Misc.VirtualSize,
217 mem_code_header->SizeOfRawData);
218
219 // Get the address of the code section in the module mapped as data from disk.
220 DWORD disk_code_offset = 0;
221 if (!mem_peimage.ImageAddrToOnDiskOffset(
222 reinterpret_cast<void*>(*mem_code_addr), &disk_code_offset))
223 return false;
224 *disk_code_addr =
225 reinterpret_cast<uint8_t*>(disk_peimage.module()) + disk_code_offset;
226 return true;
227 }
228
229 ModuleState VerifyModule(const wchar_t* module_name,
230 std::set<std::string>* modified_exports) {
231 // Get module handle, load a copy from disk as data and create PEImages.
232 HMODULE module_handle = NULL;
233 if (!GetModuleHandleEx(0, module_name, &module_handle))
234 return MODULE_STATE_UNKNOWN;
235 base::ScopedNativeLibrary native_library(module_handle);
236
237 WCHAR module_path[MAX_PATH] = {};
238 DWORD length =
239 GetModuleFileName(module_handle, module_path, arraysize(module_path));
240 if (!length || length == arraysize(module_path))
241 return MODULE_STATE_UNKNOWN;
242
243 base::MemoryMappedFile mapped_module;
244 if (!mapped_module.Initialize(base::FilePath(module_path)))
245 return MODULE_STATE_UNKNOWN;
246 ModuleVerificationState state(
247 reinterpret_cast<HMODULE>(const_cast<uint8*>(mapped_module.data())));
248
249 base::win::PEImage mem_peimage(module_handle);
250 if (!mem_peimage.VerifyMagic() || !state.disk_peimage.VerifyMagic())
251 return MODULE_STATE_UNKNOWN;
252
253 // Get the list of exports.
254 std::vector<Export> exports;
255 mem_peimage.EnumExports(EnumExportsCallback, &exports);
256 std::sort(exports.begin(), exports.end());
257
258 // Get the addresses of the code sections then calculate |code_section_delta|
259 // and |image_base_delta|.
260 uint8_t* mem_code_addr = NULL;
261 uint8_t* disk_code_addr = NULL;
262 uint32_t code_size = 0;
263 if (!GetCodeAddrsAndSize(mem_peimage,
264 state.disk_peimage,
265 &mem_code_addr,
266 &disk_code_addr,
267 &code_size))
268 return MODULE_STATE_UNKNOWN;
269
270 state.code_section_delta = disk_code_addr - mem_code_addr;
271
272 uint8_t* preferred_image_base = reinterpret_cast<uint8_t*>(
273 state.disk_peimage.GetNTHeaders()->OptionalHeader.ImageBase);
274 state.image_base_delta =
275 preferred_image_base - reinterpret_cast<uint8_t*>(mem_peimage.module());
276
277 // Get the relocations.
278 mem_peimage.EnumRelocs(EnumRelocsCallback, &state);
279 if (state.unknown_reloc_type)
280 return MODULE_STATE_UNKNOWN;
281
282 // Count the modified bytes (after accounting for relocs) and get the set of
283 // modified functions.
284 int num_bytes_different = ExamineBytesDiffInMemory(disk_code_addr,
285 mem_code_addr,
286 code_size,
287 exports,
288 &state,
289 modified_exports);
290
291 if (num_bytes_different == 0)
grt (UTC plus 2) 2014/08/05 15:51:55 return num_bytes_different ? MODULE_STATE_MODIFIED
krstnmnlsn 2014/08/05 19:17:48 Done.
292 return MODULE_STATE_UNMODIFIED;
293 return MODULE_STATE_MODIFIED;
294 }
295
296 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698