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/verifier.h" | |
6 | |
7 #include <windows.h> | |
8 #include <psapi.h> | |
9 | |
10 #include "base/files/file_path.h" | |
11 #include "base/files/memory_mapped_file.h" | |
12 #include "base/win/pe_image.h" | |
13 | |
14 namespace safe_browsing { | |
15 | |
16 namespace { | |
17 | |
18 // The number of bytes made to agree (between memory and disk) when the | |
19 // relocations are accounted for. | |
20 int bytes_corrected_by_reloc = 0; | |
21 | |
22 // This is true if the relocation table contains a reloc of type we don't | |
23 // currently handle. | |
24 bool unknown_reloc_type = false; | |
25 | |
26 bool AddrIsInCodeSection(void* address, BYTE* code_addr, SIZE_T code_size) { | |
27 return (code_addr <= address && address <= code_addr + code_size); | |
28 } | |
29 | |
30 bool GetCodeAddrsAndSize(const base::win::PEImage mem_peimage, | |
grt (UTC plus 2)
2014/07/22 17:39:49
const base::win::PEImage&
krstnmnlsn
2014/07/25 00:15:42
Done.
| |
31 base::win::PEImageAsData disk_peimage, | |
32 BYTE*& mem_code_addr, | |
grt (UTC plus 2)
2014/07/22 17:39:50
chromium passes out params by pointer rather than
krstnmnlsn
2014/07/25 00:15:43
done. The page on smartpointers is saying to use
| |
33 BYTE*& disk_code_addr, | |
34 SIZE_T& code_size) { | |
35 DWORD base_of_code = mem_peimage.GetNTHeaders()->OptionalHeader.BaseOfCode; | |
36 | |
37 // Get the address and size of the code section in the loaded module image. | |
38 PIMAGE_SECTION_HEADER mem_code_header = | |
39 mem_peimage.GetImageSectionFromAddr(mem_peimage.RVAToAddr(base_of_code)); | |
40 if (mem_code_header == NULL) | |
41 return false; | |
42 mem_code_addr = reinterpret_cast<BYTE*>( | |
43 mem_peimage.RVAToAddr(mem_code_header->VirtualAddress)); | |
44 code_size = mem_code_header->Misc.VirtualSize; | |
45 | |
46 // Get the address of the code section in the module mapped as data from disk. | |
47 // Section size will be the same as in the loaded module. | |
48 DWORD disk_code_offset = 0; | |
49 if (!mem_peimage.ImageAddrToOnDiskOffset( | |
50 reinterpret_cast<void*>(mem_code_addr), &disk_code_offset)) | |
51 return false; | |
52 disk_code_addr = | |
53 reinterpret_cast<BYTE*>(disk_peimage.module()) + disk_code_offset; | |
54 return true; | |
55 } | |
56 | |
57 int CountBytesDiffInPtr(intptr_t num_a, intptr_t num_b) { | |
58 int num_bytes = 0; | |
59 for (int i = 0; i < sizeof(num_a); ++i) { | |
60 if ((num_a & 0xFF) != (num_b & 0xFF)) | |
61 ++num_bytes; | |
62 num_a >>= 8; | |
63 num_b >>= 8; | |
64 } | |
65 return num_bytes; | |
66 } | |
67 | |
68 bool EnumRelocsCallback(const base::win::PEImage& mem_peimage, | |
69 WORD type, | |
70 void* address, | |
71 void* cookie) { | |
72 base::win::PEImageAsData disk_peimage = | |
73 *reinterpret_cast<base::win::PEImageAsData*>(cookie); | |
74 BYTE* mem_code_addr = NULL; | |
75 BYTE* disk_code_addr = NULL; | |
76 SIZE_T code_size = 0; | |
77 if (!GetCodeAddrsAndSize( | |
78 mem_peimage, disk_peimage, mem_code_addr, disk_code_addr, code_size)) | |
79 return false; | |
80 | |
81 switch (type) { | |
82 case IMAGE_REL_BASED_HIGHLOW: { | |
83 // If not in the code section return true to continue to the next reloc. | |
84 if (!AddrIsInCodeSection(address, mem_code_addr, code_size)) | |
85 return true; | |
86 | |
87 BYTE* preferred_image_base = reinterpret_cast<BYTE*>( | |
88 disk_peimage.GetNTHeaders()->OptionalHeader.ImageBase); | |
89 intptr_t delta = | |
90 preferred_image_base - reinterpret_cast<BYTE*>(mem_peimage.module()); | |
91 BYTE* new_value = (*reinterpret_cast<BYTE**>(address)) + delta; | |
92 int bytes_corrected = | |
93 CountBytesDiffInPtr(reinterpret_cast<intptr_t>(new_value), | |
94 *reinterpret_cast<intptr_t*>(address)); | |
95 | |
96 // Check that the adding delta did correct the value to agree with disk. | |
97 BYTE** disk_address = reinterpret_cast<BYTE**>( | |
98 reinterpret_cast<BYTE*>(address) - mem_code_addr + disk_code_addr); | |
99 if (new_value == *disk_address) { | |
100 bytes_corrected_by_reloc += bytes_corrected; | |
101 } | |
102 break; | |
103 } | |
104 case IMAGE_REL_BASED_ABSOLUTE: | |
105 break; | |
106 default: { | |
107 // TODO(krstnmnlsn): Find a reliable description of the behaviour of the | |
108 // remaining types of relocation and handle them. | |
109 unknown_reloc_type = true; | |
110 break; | |
111 } | |
112 } | |
113 return true; | |
114 } | |
115 | |
116 int CountBytesDiffInMemory(BYTE* disk_code_start, | |
117 BYTE* memory_code_start, | |
118 SIZE_T code_size) { | |
119 int counter = 0; | |
120 for (int i = 0; i < static_cast<int>(code_size); ++i) { | |
121 if (*(disk_code_start + i) != *(memory_code_start + i)) | |
122 ++counter; | |
123 } | |
124 return counter; | |
125 } | |
126 | |
127 } // namespace | |
128 | |
129 ModuleState VerifyModule(wchar_t* module_name) { | |
grt (UTC plus 2)
2014/07/22 17:39:49
const wchar_t*
krstnmnlsn
2014/07/25 00:15:42
Done.
| |
130 HMODULE module_handle = GetModuleHandle(module_name); | |
grt (UTC plus 2)
2014/07/22 17:39:49
the module could be unloaded immediately after thi
krstnmnlsn
2014/07/25 00:15:42
Cool! I was wondering about this, and thought (/w
| |
131 if (module_handle == NULL) | |
132 return MODULE_STATE_UNKNOWN; | |
133 | |
134 WCHAR module_path[MAX_PATH] = {0}; | |
grt (UTC plus 2)
2014/07/22 17:39:50
nit: {0} -> {}
krstnmnlsn
2014/07/25 00:15:42
Done.
| |
135 if (GetModuleFileNameEx( | |
grt (UTC plus 2)
2014/07/22 17:39:49
check the return value. if the buffer is too small
grt (UTC plus 2)
2014/07/22 17:39:50
msdn says: "To retrieve the name of a module in th
krstnmnlsn
2014/07/25 00:15:42
Resolution: trusting in all reasonable cases MAX_P
krstnmnlsn
2014/07/25 00:15:43
Done.
| |
136 GetCurrentProcess(), module_handle, module_path, MAX_PATH) == 0) | |
grt (UTC plus 2)
2014/07/22 17:39:49
MAX_PATH -> arraysize(module_path)
krstnmnlsn
2014/07/25 00:15:43
right, thanks.
| |
137 return MODULE_STATE_UNKNOWN; | |
138 | |
139 base::MemoryMappedFile mapped_module; | |
140 mapped_module.Initialize(base::FilePath(module_path)); | |
141 base::win::PEImageAsData disk_peimage( | |
grt (UTC plus 2)
2014/07/22 17:39:49
it would be safer to use safe_browsing::PeImageRea
krstnmnlsn
2014/07/25 00:15:42
Continuing to use PEImageAsData for now, I could a
| |
142 reinterpret_cast<HMODULE>(const_cast<uint8*>(mapped_module.data()))); | |
143 | |
144 base::win::PEImage mem_peimage(module_handle); | |
145 if (!mem_peimage.VerifyMagic() || !disk_peimage.VerifyMagic()) | |
146 return MODULE_STATE_UNKNOWN; | |
147 | |
148 BYTE* mem_code_addr = NULL; | |
149 BYTE* disk_code_addr = NULL; | |
150 SIZE_T code_size = 0; | |
151 if (!GetCodeAddrsAndSize( | |
152 mem_peimage, disk_peimage, mem_code_addr, disk_code_addr, code_size)) | |
153 return MODULE_STATE_UNKNOWN; | |
154 | |
155 int num_bytes_different = | |
156 CountBytesDiffInMemory(disk_code_addr, mem_code_addr, code_size); | |
157 mem_peimage.EnumRelocs( | |
158 (base::win::PEImage::EnumRelocsFunction) & EnumRelocsCallback, | |
159 &disk_peimage); | |
160 if (unknown_reloc_type) | |
161 return MODULE_STATE_UNKNOWN; | |
162 | |
163 if (num_bytes_different - bytes_corrected_by_reloc == 0) | |
grt (UTC plus 2)
2014/07/22 17:39:49
?num_bytes_different == bytes_corrected_by_reloc?
krstnmnlsn
2014/07/25 00:15:42
Done. :P
| |
164 return MODULE_STATE_UNMODIFIED; | |
165 return MODULE_STATE_MODIFIED; | |
166 } | |
167 | |
168 } // namespace safe_browsing | |
OLD | NEW |