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

Side by Side Diff: chrome/browser/safe_browsing/module_integrity_verifier.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: chris' fixes 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.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 to agree (between memory and disk) when the
csharp 2014/07/30 19:29:42 This comment confused me a bit, maybe just say "Th
krstnmnlsn 2014/07/30 21:03:58 Done.
23 // relocations are accounted for.
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
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) {
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))
75 return false;
76
77 switch (type) {
78 case IMAGE_REL_BASED_HIGHLOW: {
79 // If not in the code section return true to continue to the next reloc.
80 if (!AddrIsInCodeSection(address, mem_code_addr, code_size))
81 return true;
csharp 2014/07/30 19:29:42 Does it make sense to pull this check out above th
krstnmnlsn 2014/07/30 21:03:58 They would care yes. Thanks!
82
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());
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) {
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: {
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;
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) {
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698