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

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: greg's 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 // The number of bytes made to agree (between memory and disk) when the
csharp 2014/07/30 15:22:08 nit: I'd add a blank line above this comment
krstnmnlsn 2014/07/30 18:36:07 Done.
22 // relocations are accounted for.
23 int bytes_corrected_by_reloc;
24 // Set true if the relocation table contains a reloc of type that we don't
csharp 2014/07/30 15:22:08 nit: I'd add a blank line above this comment
krstnmnlsn 2014/07/30 18:36:07 Done.
25 // currently handle.
26 bool unknown_reloc_type;
27
28 DISALLOW_COPY_AND_ASSIGN(RelocEnumerationState);
29 };
30
31 RelocEnumerationState::RelocEnumerationState(HMODULE hModule)
32 : disk_peimage(hModule),
33 bytes_corrected_by_reloc(0),
34 unknown_reloc_type(false) {
35 }
36
37 RelocEnumerationState::~RelocEnumerationState() {
38 }
39
40 int CountBytesDiffInMemory(uint8_t* disk_code_start,
41 uint8_t* memory_code_start,
42 uint32_t code_size) {
43 int counter = 0;
44 for (int i = 0; i < static_cast<int>(code_size); ++i) {
45 if (*(disk_code_start + i) != *(memory_code_start + i))
46 ++counter;
47 }
48 return counter;
49 }
50
51 bool AddrIsInCodeSection(void* address,
52 uint8_t* code_addr,
53 uint32_t code_size) {
54 return (code_addr <= address && address < code_addr + code_size);
55 }
56
57 bool EnumRelocsCallback(const base::win::PEImage& mem_peimage,
58 WORD type,
59 void* address,
60 void* cookie) {
61 RelocEnumerationState* reloc_enum_state =
62 reinterpret_cast<RelocEnumerationState*>(cookie);
63 base::win::PEImageAsData disk_peimage = reloc_enum_state->disk_peimage;
csharp 2014/07/30 15:22:08 Probably worth making this a constant pointer (con
krstnmnlsn 2014/07/30 18:36:06 Done.
64 uint8_t* mem_code_addr = NULL;
65 uint8_t* disk_code_addr = NULL;
66 uint32_t code_size = 0;
67 if (!GetCodeAddrsAndSize(mem_peimage,
68 disk_peimage,
69 &mem_code_addr,
70 &disk_code_addr,
71 &code_size))
72 return false;
73
74 switch (type) {
75 case IMAGE_REL_BASED_HIGHLOW: {
76 // If not in the code section return true to continue to the next reloc.
77 if (!AddrIsInCodeSection(address, mem_code_addr, code_size))
78 return true;
79
80 uint8_t* preferred_image_base = reinterpret_cast<uint8_t*>(
81 disk_peimage.GetNTHeaders()->OptionalHeader.ImageBase);
82 uintptr_t delta = preferred_image_base -
83 reinterpret_cast<uint8_t*>(mem_peimage.module());
84 uint8_t* new_value = (*reinterpret_cast<uint8_t**>(address)) + delta;
csharp 2014/07/30 15:22:08 new_value->new_address (maybe?)
krstnmnlsn 2014/07/30 18:36:07 Yeah so I was debating this. I worried it would b
85 int bytes_corrected =
86 CountBytesDiffInPtr(reinterpret_cast<uintptr_t>(new_value),
87 *reinterpret_cast<uintptr_t*>(address));
88
89 // Check that the adding delta did correct the value to agree with disk.
csharp 2014/07/30 15:22:08 nit: "that the adding delta did correct" -> "that
krstnmnlsn 2014/07/30 18:36:07 Done.
90 uint8_t** disk_address = reinterpret_cast<uint8_t**>(
91 reinterpret_cast<uint8_t*>(address) - mem_code_addr + disk_code_addr);
92 if (new_value == *disk_address) {
93 reloc_enum_state->bytes_corrected_by_reloc += bytes_corrected;
94 }
95 break;
96 }
97 case IMAGE_REL_BASED_ABSOLUTE:
98 break;
csharp 2014/07/30 15:22:08 Could you add a brief comment explaining why we ca
krstnmnlsn 2014/07/30 18:36:07 I meant to, thanks!
99 default: {
100 // TODO(krstnmnlsn): Find a reliable description of the behaviour of the
101 // remaining types of relocation and handle them.
102 reloc_enum_state->unknown_reloc_type = true;
103 break;
104 }
105 }
106 return true;
107 }
108
109 } // namespace
110
111 bool GetCodeAddrsAndSize(const base::win::PEImage& mem_peimage,
112 const base::win::PEImageAsData& disk_peimage,
113 uint8_t** mem_code_addr,
114 uint8_t** disk_code_addr,
115 uint32_t* code_size) {
116 DWORD base_of_code = mem_peimage.GetNTHeaders()->OptionalHeader.BaseOfCode;
117
118 // Get the address and size of the code section in the loaded module image.
119 PIMAGE_SECTION_HEADER mem_code_header =
120 mem_peimage.GetImageSectionFromAddr(mem_peimage.RVAToAddr(base_of_code));
121 if (mem_code_header == NULL)
122 return false;
123 *mem_code_addr = reinterpret_cast<uint8_t*>(
124 mem_peimage.RVAToAddr(mem_code_header->VirtualAddress));
125 *code_size = mem_code_header->Misc.VirtualSize;
126
127 // Get the address of the code section in the module mapped as data from disk.
128 // Section size will be the same as in the loaded module.
129 DWORD disk_code_offset = 0;
130 if (!mem_peimage.ImageAddrToOnDiskOffset(
131 reinterpret_cast<void*>(*mem_code_addr), &disk_code_offset))
132 return false;
133 *disk_code_addr =
134 reinterpret_cast<uint8_t*>(disk_peimage.module()) + disk_code_offset;
135 return true;
136 }
137
138 int CountBytesDiffInPtr(uintptr_t num_a, uintptr_t num_b) {
139 int num_bytes = 0;
140 for (int i = 0; i < sizeof(num_a); ++i) {
141 if ((num_a & 0xFF) != (num_b & 0xFF))
142 ++num_bytes;
143 num_a >>= 8;
144 num_b >>= 8;
145 }
146 return num_bytes;
147 }
148
149 ModuleState VerifyModule(const wchar_t* module_name) {
150 HMODULE module_handle = NULL;
151 if (!GetModuleHandleEx(0, module_name, &module_handle))
152 return MODULE_STATE_UNKNOWN;
153 base::ScopedNativeLibrary native_library(module_handle);
154
155 WCHAR module_path[MAX_PATH] = {};
156 DWORD length =
157 GetModuleFileName(module_handle, module_path, arraysize(module_path));
158 if (!length || length == arraysize(module_path))
159 return MODULE_STATE_UNKNOWN;
160
161 base::MemoryMappedFile mapped_module;
162 if (!mapped_module.Initialize(base::FilePath(module_path)))
163 return MODULE_STATE_UNKNOWN;
164 RelocEnumerationState reloc_enum_state(
165 reinterpret_cast<HMODULE>(const_cast<uint8*>(mapped_module.data())));
166
167 base::win::PEImage mem_peimage(module_handle);
168 if (!mem_peimage.VerifyMagic() ||
169 !reloc_enum_state.disk_peimage.VerifyMagic())
170 return MODULE_STATE_UNKNOWN;
171
172 uint8_t* mem_code_addr = NULL;
173 uint8_t* disk_code_addr = NULL;
174 uint32_t code_size = 0;
175 if (!GetCodeAddrsAndSize(mem_peimage,
176 reloc_enum_state.disk_peimage,
177 &mem_code_addr,
178 &disk_code_addr,
179 &code_size))
180 return MODULE_STATE_UNKNOWN;
181
182 int num_bytes_different =
csharp 2014/07/30 15:22:08 Can this be moved to line 187? (I don't need we ne
krstnmnlsn 2014/07/30 18:36:07 Sure.
183 CountBytesDiffInMemory(disk_code_addr, mem_code_addr, code_size);
184 mem_peimage.EnumRelocs(EnumRelocsCallback, &reloc_enum_state);
185 if (reloc_enum_state.unknown_reloc_type)
186 return MODULE_STATE_UNKNOWN;
187
188 if (num_bytes_different == reloc_enum_state.bytes_corrected_by_reloc)
189 return MODULE_STATE_UNMODIFIED;
190 return MODULE_STATE_MODIFIED;
191 }
192
193 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698