OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/safe_browsing/verifier.h" | 5 #include "chrome/browser/safe_browsing/module_integrity_verifier.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <psapi.h> | 8 #include <psapi.h> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/files/memory_mapped_file.h" | 11 #include "base/files/memory_mapped_file.h" |
12 #include "base/scoped_native_library.h" | |
12 #include "base/win/pe_image.h" | 13 #include "base/win/pe_image.h" |
13 | 14 |
14 namespace safe_browsing { | 15 namespace safe_browsing { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 // The number of bytes made to agree (between memory and disk) when the | 19 // The number of bytes made to agree (between memory and disk) when the |
19 // relocations are accounted for. | 20 // relocations are accounted for. |
20 int bytes_corrected_by_reloc = 0; | 21 int bytes_corrected_by_reloc = 0; |
csharp
2014/07/29 14:28:15
This seems to be only used in VerifyModule, so it
krstnmnlsn
2014/07/29 17:30:25
It's used in the EnumRelocsCallback (hidden in som
| |
21 | 22 |
22 // This is true if the relocation table contains a reloc of type we don't | 23 // This is true if the relocation table contains a reloc of type we don't |
23 // currently handle. | 24 // currently handle. |
24 bool unknown_reloc_type = false; | 25 bool unknown_reloc_type = false; |
csharp
2014/07/29 14:28:15
This probably should also be a local variable in V
krstnmnlsn
2014/07/29 17:30:25
Do you think it would be better to make some sort
| |
25 | 26 |
26 bool AddrIsInCodeSection(void* address, BYTE* code_addr, SIZE_T code_size) { | 27 int CountBytesDiffInMemory(BYTE* disk_code_start, |
27 return (code_addr <= address && address <= code_addr + code_size); | 28 BYTE* memory_code_start, |
29 SIZE_T code_size) { | |
30 int counter = 0; | |
31 for (int i = 0; i < static_cast<int>(code_size); ++i) { | |
32 if (*(disk_code_start + i) != *(memory_code_start + i)) | |
33 ++counter; | |
34 } | |
35 return counter; | |
28 } | 36 } |
29 | 37 |
30 bool GetCodeAddrsAndSize(const base::win::PEImage mem_peimage, | 38 bool AddrIsInCodeSection(void* address, BYTE* code_addr, SIZE_T code_size) { |
31 base::win::PEImageAsData disk_peimage, | 39 return (code_addr <= address && address < code_addr + code_size); |
32 BYTE*& mem_code_addr, | |
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 } | 40 } |
67 | 41 |
68 bool EnumRelocsCallback(const base::win::PEImage& mem_peimage, | 42 bool EnumRelocsCallback(const base::win::PEImage& mem_peimage, |
69 WORD type, | 43 WORD type, |
70 void* address, | 44 void* address, |
71 void* cookie) { | 45 void* cookie) { |
72 base::win::PEImageAsData disk_peimage = | 46 base::win::PEImageAsData disk_peimage = |
73 *reinterpret_cast<base::win::PEImageAsData*>(cookie); | 47 *reinterpret_cast<base::win::PEImageAsData*>(cookie); |
74 BYTE* mem_code_addr = NULL; | 48 BYTE* mem_code_addr = NULL; |
75 BYTE* disk_code_addr = NULL; | 49 BYTE* disk_code_addr = NULL; |
76 SIZE_T code_size = 0; | 50 SIZE_T code_size = 0; |
77 if (!GetCodeAddrsAndSize( | 51 if (!GetCodeAddrsAndSize(mem_peimage, disk_peimage, |
78 mem_peimage, disk_peimage, mem_code_addr, disk_code_addr, code_size)) | 52 &mem_code_addr, &disk_code_addr, &code_size)) |
79 return false; | 53 return false; |
80 | 54 |
81 switch (type) { | 55 switch (type) { |
82 case IMAGE_REL_BASED_HIGHLOW: { | 56 case IMAGE_REL_BASED_HIGHLOW: { |
83 // If not in the code section return true to continue to the next reloc. | 57 // If not in the code section return true to continue to the next reloc. |
84 if (!AddrIsInCodeSection(address, mem_code_addr, code_size)) | 58 if (!AddrIsInCodeSection(address, mem_code_addr, code_size)) |
85 return true; | 59 return true; |
86 | 60 |
87 BYTE* preferred_image_base = reinterpret_cast<BYTE*>( | 61 BYTE* preferred_image_base = reinterpret_cast<BYTE*>( |
88 disk_peimage.GetNTHeaders()->OptionalHeader.ImageBase); | 62 disk_peimage.GetNTHeaders()->OptionalHeader.ImageBase); |
(...skipping 17 matching lines...) Expand all Loading... | |
106 default: { | 80 default: { |
107 // TODO(krstnmnlsn): Find a reliable description of the behaviour of the | 81 // TODO(krstnmnlsn): Find a reliable description of the behaviour of the |
108 // remaining types of relocation and handle them. | 82 // remaining types of relocation and handle them. |
109 unknown_reloc_type = true; | 83 unknown_reloc_type = true; |
110 break; | 84 break; |
111 } | 85 } |
112 } | 86 } |
113 return true; | 87 return true; |
114 } | 88 } |
115 | 89 |
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 | 90 } // namespace |
128 | 91 |
129 ModuleState VerifyModule(wchar_t* module_name) { | 92 bool GetCodeAddrsAndSize(const base::win::PEImage& mem_peimage, |
130 HMODULE module_handle = GetModuleHandle(module_name); | 93 const base::win::PEImageAsData& disk_peimage, |
131 if (module_handle == NULL) | 94 BYTE** mem_code_addr, |
95 BYTE** disk_code_addr, | |
96 SIZE_T* code_size) { | |
97 DWORD base_of_code = mem_peimage.GetNTHeaders()->OptionalHeader.BaseOfCode; | |
98 | |
99 // Get the address and size of the code section in the loaded module image. | |
100 PIMAGE_SECTION_HEADER mem_code_header = | |
101 mem_peimage.GetImageSectionFromAddr(mem_peimage.RVAToAddr(base_of_code)); | |
102 if (mem_code_header == NULL) | |
103 return false; | |
104 *mem_code_addr = reinterpret_cast<BYTE*>( | |
105 mem_peimage.RVAToAddr(mem_code_header->VirtualAddress)); | |
106 *code_size = mem_code_header->Misc.VirtualSize; | |
107 | |
108 // Get the address of the code section in the module mapped as data from disk. | |
109 // Section size will be the same as in the loaded module. | |
110 DWORD disk_code_offset = 0; | |
111 if (!mem_peimage.ImageAddrToOnDiskOffset( | |
112 reinterpret_cast<void*>(*mem_code_addr), &disk_code_offset)) | |
113 return false; | |
114 *disk_code_addr = | |
115 reinterpret_cast<BYTE*>(disk_peimage.module()) + disk_code_offset; | |
116 return true; | |
117 } | |
118 | |
119 int CountBytesDiffInPtr(intptr_t num_a, intptr_t num_b) { | |
120 int num_bytes = 0; | |
121 for (int i = 0; i < sizeof(num_a); ++i) { | |
122 if ((num_a & 0xFF) != (num_b & 0xFF)) | |
123 ++num_bytes; | |
124 num_a >>= 8; | |
125 num_b >>= 8; | |
126 } | |
127 return num_bytes; | |
128 } | |
129 | |
130 ModuleState VerifyModule(const wchar_t* module_name) { | |
131 bytes_corrected_by_reloc = 0; | |
132 unknown_reloc_type = false; | |
133 | |
134 HMODULE module_handle = NULL; | |
135 if (!GetModuleHandleEx(0, module_name, &module_handle)) | |
132 return MODULE_STATE_UNKNOWN; | 136 return MODULE_STATE_UNKNOWN; |
137 base::ScopedNativeLibrary native_library(module_handle); | |
133 | 138 |
134 WCHAR module_path[MAX_PATH] = {0}; | 139 WCHAR module_path[MAX_PATH] = {}; |
135 if (GetModuleFileNameEx( | 140 DWORD length = |
136 GetCurrentProcess(), module_handle, module_path, MAX_PATH) == 0) | 141 GetModuleFileName(module_handle, module_path, arraysize(module_path)); |
142 if (length == arraysize(module_path)) | |
137 return MODULE_STATE_UNKNOWN; | 143 return MODULE_STATE_UNKNOWN; |
138 | 144 |
139 base::MemoryMappedFile mapped_module; | 145 base::MemoryMappedFile mapped_module; |
140 mapped_module.Initialize(base::FilePath(module_path)); | 146 mapped_module.Initialize(base::FilePath(module_path)); |
141 base::win::PEImageAsData disk_peimage( | 147 base::win::PEImageAsData disk_peimage( |
142 reinterpret_cast<HMODULE>(const_cast<uint8*>(mapped_module.data()))); | 148 reinterpret_cast<HMODULE>(const_cast<uint8*>(mapped_module.data()))); |
143 | 149 |
144 base::win::PEImage mem_peimage(module_handle); | 150 base::win::PEImage mem_peimage(module_handle); |
145 if (!mem_peimage.VerifyMagic() || !disk_peimage.VerifyMagic()) | 151 if (!mem_peimage.VerifyMagic() || !disk_peimage.VerifyMagic()) |
146 return MODULE_STATE_UNKNOWN; | 152 return MODULE_STATE_UNKNOWN; |
147 | 153 |
148 BYTE* mem_code_addr = NULL; | 154 BYTE* mem_code_addr = NULL; |
149 BYTE* disk_code_addr = NULL; | 155 BYTE* disk_code_addr = NULL; |
150 SIZE_T code_size = 0; | 156 SIZE_T code_size = 0; |
151 if (!GetCodeAddrsAndSize( | 157 if (!GetCodeAddrsAndSize(mem_peimage, |
152 mem_peimage, disk_peimage, mem_code_addr, disk_code_addr, code_size)) | 158 disk_peimage, |
159 &mem_code_addr, | |
160 &disk_code_addr, | |
161 &code_size)) | |
153 return MODULE_STATE_UNKNOWN; | 162 return MODULE_STATE_UNKNOWN; |
154 | 163 |
155 int num_bytes_different = | 164 int num_bytes_different = |
156 CountBytesDiffInMemory(disk_code_addr, mem_code_addr, code_size); | 165 CountBytesDiffInMemory(disk_code_addr, mem_code_addr, code_size); |
157 mem_peimage.EnumRelocs( | 166 mem_peimage.EnumRelocs(EnumRelocsCallback, &disk_peimage); |
158 (base::win::PEImage::EnumRelocsFunction) & EnumRelocsCallback, | |
159 &disk_peimage); | |
160 if (unknown_reloc_type) | 167 if (unknown_reloc_type) |
161 return MODULE_STATE_UNKNOWN; | 168 return MODULE_STATE_UNKNOWN; |
162 | 169 |
163 if (num_bytes_different - bytes_corrected_by_reloc == 0) | 170 if (num_bytes_different == bytes_corrected_by_reloc) |
164 return MODULE_STATE_UNMODIFIED; | 171 return MODULE_STATE_UNMODIFIED; |
165 return MODULE_STATE_MODIFIED; | 172 return MODULE_STATE_MODIFIED; |
166 } | 173 } |
167 | 174 |
168 } // namespace safe_browsing | 175 } // namespace safe_browsing |
OLD | NEW |