OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "snapshot/win/pe_image_reader.h" |
| 16 |
| 17 #include <string.h> |
| 18 |
| 19 #include "base/logging.h" |
| 20 #include "base/strings/stringprintf.h" |
| 21 #include "client/crashpad_info.h" |
| 22 #include "snapshot/win/process_reader_win.h" |
| 23 |
| 24 namespace crashpad { |
| 25 |
| 26 namespace { |
| 27 |
| 28 std::string RangeToString(const CheckedWinAddressRange& range) { |
| 29 return base::StringPrintf("[0x%llx + 0x%llx (%s)]", |
| 30 range.Base(), |
| 31 range.Size(), |
| 32 range.Is64Bit() ? "64" : "32"); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 PEImageReader::PEImageReader() |
| 38 : process_reader_(nullptr), |
| 39 module_range_(), |
| 40 module_name_(), |
| 41 initialized_() { |
| 42 } |
| 43 |
| 44 PEImageReader::~PEImageReader() { |
| 45 } |
| 46 |
| 47 bool PEImageReader::Initialize(ProcessReaderWin* process_reader, |
| 48 WinVMAddress address, |
| 49 WinVMSize size, |
| 50 const std::string& module_name) { |
| 51 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); |
| 52 |
| 53 process_reader_ = process_reader; |
| 54 module_range_.SetRange(process_reader_->Is64Bit(), address, size); |
| 55 if (!module_range_.IsValid()) { |
| 56 LOG(WARNING) << "invalid module range for " << module_name << ": " |
| 57 << RangeToString(module_range_); |
| 58 return false; |
| 59 } |
| 60 module_name_ = module_name; |
| 61 |
| 62 INITIALIZATION_STATE_SET_VALID(initialized_); |
| 63 return true; |
| 64 } |
| 65 |
| 66 bool PEImageReader::GetCrashpadInfo( |
| 67 process_types::CrashpadInfo* crashpad_info) const { |
| 68 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 69 |
| 70 IMAGE_SECTION_HEADER section; |
| 71 if (!GetSectionByName("CPADinfo", §ion)) |
| 72 return false; |
| 73 |
| 74 if (section.Misc.VirtualSize < sizeof(process_types::CrashpadInfo)) { |
| 75 LOG(WARNING) << "small crashpad info section size " |
| 76 << section.Misc.VirtualSize << ", " << module_name_; |
| 77 return false; |
| 78 } |
| 79 |
| 80 WinVMAddress crashpad_info_address = Address() + section.VirtualAddress; |
| 81 CheckedWinAddressRange crashpad_info_range(process_reader_->Is64Bit(), |
| 82 crashpad_info_address, |
| 83 section.Misc.VirtualSize); |
| 84 if (!crashpad_info_range.IsValid()) { |
| 85 LOG(WARNING) << "invalid range for crashpad info: " |
| 86 << RangeToString(crashpad_info_range); |
| 87 return false; |
| 88 } |
| 89 |
| 90 if (!module_range_.ContainsRange(crashpad_info_range)) { |
| 91 LOG(WARNING) << "crashpad info does not fall inside module " |
| 92 << module_name_; |
| 93 return false; |
| 94 } |
| 95 |
| 96 // TODO(scottmg): process_types for cross-bitness. |
| 97 if (!process_reader_->ReadMemory(crashpad_info_address, |
| 98 sizeof(process_types::CrashpadInfo), |
| 99 crashpad_info)) { |
| 100 LOG(WARNING) << "could not read crashpad info " << module_name_; |
| 101 return false; |
| 102 } |
| 103 |
| 104 if (crashpad_info->signature != CrashpadInfo::kSignature || |
| 105 crashpad_info->version < 1) { |
| 106 LOG(WARNING) << "unexpected crashpad info data " << module_name_; |
| 107 return false; |
| 108 } |
| 109 |
| 110 return true; |
| 111 } |
| 112 |
| 113 bool PEImageReader::GetSectionByName(const std::string& name, |
| 114 IMAGE_SECTION_HEADER* section) const { |
| 115 if (name.size() > sizeof(section->Name)) { |
| 116 LOG(WARNING) << "supplied section name too long " << name; |
| 117 return false; |
| 118 } |
| 119 |
| 120 IMAGE_DOS_HEADER dos_header; |
| 121 if (!CheckedReadMemory(Address(), sizeof(IMAGE_DOS_HEADER), &dos_header)) { |
| 122 LOG(WARNING) << "could not read dos header of " << module_name_; |
| 123 return false; |
| 124 } |
| 125 |
| 126 if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) { |
| 127 LOG(WARNING) << "invalid e_magic in dos header of " << module_name_; |
| 128 return false; |
| 129 } |
| 130 |
| 131 // TODO(scottmg): This is reading a same-bitness sized structure. |
| 132 IMAGE_NT_HEADERS nt_headers; |
| 133 WinVMAddress nt_headers_address = Address() + dos_header.e_lfanew; |
| 134 if (!CheckedReadMemory( |
| 135 nt_headers_address, sizeof(IMAGE_NT_HEADERS), &nt_headers)) { |
| 136 LOG(WARNING) << "could not read nt headers of " << module_name_; |
| 137 return false; |
| 138 } |
| 139 |
| 140 if (nt_headers.Signature != IMAGE_NT_SIGNATURE) { |
| 141 LOG(WARNING) << "invalid signature in nt headers of " << module_name_; |
| 142 return false; |
| 143 } |
| 144 |
| 145 WinVMAddress first_section_address = |
| 146 nt_headers_address + offsetof(IMAGE_NT_HEADERS, OptionalHeader) + |
| 147 nt_headers.FileHeader.SizeOfOptionalHeader; |
| 148 for (DWORD i = 0; i < nt_headers.FileHeader.NumberOfSections; ++i) { |
| 149 WinVMAddress section_address = |
| 150 first_section_address + sizeof(IMAGE_SECTION_HEADER) * i; |
| 151 if (!CheckedReadMemory( |
| 152 section_address, sizeof(IMAGE_SECTION_HEADER), section)) { |
| 153 LOG(WARNING) << "could not read section " << i << " of " << module_name_; |
| 154 return false; |
| 155 } |
| 156 if (strncmp(reinterpret_cast<const char*>(section->Name), |
| 157 name.c_str(), |
| 158 sizeof(section->Name)) == 0) { |
| 159 return true; |
| 160 } |
| 161 } |
| 162 |
| 163 return false; |
| 164 } |
| 165 |
| 166 bool PEImageReader::CheckedReadMemory(WinVMAddress address, |
| 167 WinVMSize size, |
| 168 void* into) const { |
| 169 CheckedWinAddressRange read_range(process_reader_->Is64Bit(), address, size); |
| 170 if (!read_range.IsValid()) { |
| 171 LOG(WARNING) << "invalid read range: " << RangeToString(read_range); |
| 172 return false; |
| 173 } |
| 174 if (!module_range_.ContainsRange(read_range)) { |
| 175 LOG(WARNING) << "attempt to read outside of module " << module_name_ |
| 176 << " at range: " << RangeToString(read_range); |
| 177 return false; |
| 178 } |
| 179 return process_reader_->ReadMemory(address, size, into); |
| 180 } |
| 181 |
| 182 } // namespace crashpad |
OLD | NEW |