Chromium Code Reviews| 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/module_integrity_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/scoped_native_library.h" | |
| 13 #include "base/win/pe_image.h" | |
| 14 | |
| 15 namespace safe_browsing { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // The number of bytes made to agree (between memory and disk) when the | |
| 20 // relocations are accounted for. | |
| 21 int bytes_corrected_by_reloc = 0; | |
|
grt (UTC plus 2)
2014/07/29 19:32:22
instead of using global state (which makes concurr
krstnmnlsn
2014/07/30 14:34:35
Okay yes, I was asking earlier if I should do this
| |
| 22 | |
| 23 // This is true if the relocation table contains a reloc of type we don't | |
| 24 // currently handle. | |
| 25 bool unknown_reloc_type = false; | |
| 26 | |
| 27 int CountBytesDiffInMemory(BYTE* disk_code_start, | |
| 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; | |
| 36 } | |
| 37 | |
| 38 bool AddrIsInCodeSection(void* address, BYTE* code_addr, SIZE_T code_size) { | |
| 39 return (code_addr <= address && address < code_addr + code_size); | |
| 40 } | |
| 41 | |
| 42 bool EnumRelocsCallback(const base::win::PEImage& mem_peimage, | |
| 43 WORD type, | |
| 44 void* address, | |
| 45 void* cookie) { | |
| 46 base::win::PEImageAsData disk_peimage = | |
| 47 *reinterpret_cast<base::win::PEImageAsData*>(cookie); | |
| 48 BYTE* mem_code_addr = NULL; | |
| 49 BYTE* disk_code_addr = NULL; | |
| 50 SIZE_T code_size = 0; | |
| 51 if (!GetCodeAddrsAndSize(mem_peimage, disk_peimage, | |
| 52 &mem_code_addr, &disk_code_addr, &code_size)) | |
| 53 return false; | |
| 54 | |
| 55 switch (type) { | |
| 56 case IMAGE_REL_BASED_HIGHLOW: { | |
| 57 // If not in the code section return true to continue to the next reloc. | |
| 58 if (!AddrIsInCodeSection(address, mem_code_addr, code_size)) | |
| 59 return true; | |
| 60 | |
| 61 BYTE* preferred_image_base = reinterpret_cast<BYTE*>( | |
| 62 disk_peimage.GetNTHeaders()->OptionalHeader.ImageBase); | |
| 63 intptr_t delta = | |
| 64 preferred_image_base - reinterpret_cast<BYTE*>(mem_peimage.module()); | |
| 65 BYTE* new_value = (*reinterpret_cast<BYTE**>(address)) + delta; | |
| 66 int bytes_corrected = | |
| 67 CountBytesDiffInPtr(reinterpret_cast<intptr_t>(new_value), | |
| 68 *reinterpret_cast<intptr_t*>(address)); | |
| 69 | |
| 70 // Check that the adding delta did correct the value to agree with disk. | |
| 71 BYTE** disk_address = reinterpret_cast<BYTE**>( | |
| 72 reinterpret_cast<BYTE*>(address) - mem_code_addr + disk_code_addr); | |
| 73 if (new_value == *disk_address) { | |
| 74 bytes_corrected_by_reloc += bytes_corrected; | |
| 75 } | |
| 76 break; | |
| 77 } | |
| 78 case IMAGE_REL_BASED_ABSOLUTE: | |
| 79 break; | |
| 80 default: { | |
| 81 // TODO(krstnmnlsn): Find a reliable description of the behaviour of the | |
| 82 // remaining types of relocation and handle them. | |
| 83 unknown_reloc_type = true; | |
| 84 break; | |
| 85 } | |
| 86 } | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 } // namespace | |
| 91 | |
| 92 bool GetCodeAddrsAndSize(const base::win::PEImage& mem_peimage, | |
| 93 const base::win::PEImageAsData& disk_peimage, | |
| 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. | |
|
grt (UTC plus 2)
2014/07/29 19:32:22
is this necessarily true? VirtualSize (size of the
krstnmnlsn
2014/07/30 14:34:35
Ah, I misunderstood that. It should be safe/suffi
grt (UTC plus 2)
2014/07/31 17:03:33
I believe so, yes.
krstnmnlsn
2014/08/04 15:18:14
Done.
| |
| 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)) | |
| 136 return MODULE_STATE_UNKNOWN; | |
| 137 base::ScopedNativeLibrary native_library(module_handle); | |
| 138 | |
| 139 WCHAR module_path[MAX_PATH] = {}; | |
| 140 DWORD length = | |
| 141 GetModuleFileName(module_handle, module_path, arraysize(module_path)); | |
| 142 if (length == arraysize(module_path)) | |
|
grt (UTC plus 2)
2014/07/29 19:32:22
this function returns 0 on failure, so:
if (!len
krstnmnlsn
2014/07/30 14:34:35
Done.
| |
| 143 return MODULE_STATE_UNKNOWN; | |
| 144 | |
| 145 base::MemoryMappedFile mapped_module; | |
| 146 mapped_module.Initialize(base::FilePath(module_path)); | |
|
grt (UTC plus 2)
2014/07/29 19:32:22
returns false if the file cannot be mapped
krstnmnlsn
2014/07/30 14:34:35
Done.
| |
| 147 base::win::PEImageAsData disk_peimage( | |
| 148 reinterpret_cast<HMODULE>(const_cast<uint8*>(mapped_module.data()))); | |
| 149 | |
| 150 base::win::PEImage mem_peimage(module_handle); | |
| 151 if (!mem_peimage.VerifyMagic() || !disk_peimage.VerifyMagic()) | |
| 152 return MODULE_STATE_UNKNOWN; | |
| 153 | |
| 154 BYTE* mem_code_addr = NULL; | |
| 155 BYTE* disk_code_addr = NULL; | |
| 156 SIZE_T code_size = 0; | |
| 157 if (!GetCodeAddrsAndSize(mem_peimage, | |
| 158 disk_peimage, | |
| 159 &mem_code_addr, | |
| 160 &disk_code_addr, | |
| 161 &code_size)) | |
| 162 return MODULE_STATE_UNKNOWN; | |
| 163 | |
| 164 int num_bytes_different = | |
| 165 CountBytesDiffInMemory(disk_code_addr, mem_code_addr, code_size); | |
| 166 mem_peimage.EnumRelocs(EnumRelocsCallback, &disk_peimage); | |
| 167 if (unknown_reloc_type) | |
| 168 return MODULE_STATE_UNKNOWN; | |
| 169 | |
| 170 if (num_bytes_different == bytes_corrected_by_reloc) | |
| 171 return MODULE_STATE_UNMODIFIED; | |
| 172 return MODULE_STATE_MODIFIED; | |
| 173 } | |
| 174 | |
| 175 } // namespace safe_browsing | |
| OLD | NEW |