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 #include "util/numeric/checked_address_range.h" | |
24 | |
25 namespace crashpad { | |
26 | |
27 PEImageReader::PEImageReader() | |
28 : process_reader_(nullptr), address_(0), module_name_(), initialized_() { | |
Mark Mentovai
2015/04/30 20:58:35
size, module_range
scottmg
2015/04/30 22:09:44
Done.
| |
29 } | |
30 | |
31 PEImageReader::~PEImageReader() { | |
32 } | |
33 | |
34 bool PEImageReader::Initialize(ProcessReaderWin* process_reader, | |
35 uintptr_t address, | |
36 uintptr_t size, | |
37 const std::string& module_name) { | |
38 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); | |
39 | |
40 process_reader_ = process_reader; | |
41 address_ = address; | |
42 size_ = size; | |
43 module_range_.SetRange(process_reader_->Is64Bit(), address_, size_); | |
Mark Mentovai
2015/04/30 20:58:35
Check the validity of module_range_ once you set i
scottmg
2015/04/30 22:09:44
Done.
| |
44 module_name_ = module_name; | |
45 | |
46 INITIALIZATION_STATE_SET_VALID(initialized_); | |
47 return true; | |
48 } | |
49 | |
50 bool PEImageReader::GetCrashpadInfo( | |
51 process_types::CrashpadInfo* crashpad_info) const { | |
52 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | |
53 | |
54 IMAGE_SECTION_HEADER section; | |
55 if (!GetSectionByName("CPADinfo", §ion)) | |
56 return false; | |
57 | |
58 if (section.Misc.VirtualSize < sizeof(process_types::CrashpadInfo)) { | |
59 LOG(WARNING) << "small crashpad info section size " | |
60 << section.Misc.VirtualSize << ", " << module_name_; | |
61 return false; | |
62 } | |
63 | |
64 uintptr_t crashpad_info_address = address_ + section.VirtualAddress; | |
65 if (!module_range_.ContainsRange( | |
66 CheckedAddressRange(process_reader_->Is64Bit(), | |
Mark Mentovai
2015/04/30 20:58:35
I think that you need to check the new CheckedAddr
scottmg
2015/04/30 22:09:43
Done.
| |
67 crashpad_info_address, | |
68 sizeof(process_types::CrashpadInfo)))) { | |
Mark Mentovai
2015/04/30 20:58:35
This should be section.Misc.VirtualSize, since you
scottmg
2015/04/30 22:09:43
Done.
| |
69 LOG(WARNING) << "invalid address for crashpad info " | |
Mark Mentovai
2015/04/30 20:58:35
This message is a little misleading, because it ch
scottmg
2015/04/30 22:09:43
Done.
| |
70 << crashpad_info_address; | |
71 return false; | |
72 } | |
73 | |
74 // TODO(scottmg): process_types for cross-bitness. | |
75 if (!process_reader_->ReadMemory(crashpad_info_address, | |
76 sizeof(process_types::CrashpadInfo), | |
77 crashpad_info)) { | |
78 LOG(WARNING) << "could not read crashpad info " << module_name_; | |
79 return false; | |
80 } | |
81 | |
82 if (crashpad_info->signature != CrashpadInfo::kSignature || | |
83 crashpad_info->version < 1) { | |
84 LOG(WARNING) << "unexpected crashpad info data " << module_name_; | |
85 return false; | |
86 } | |
87 | |
88 return true; | |
89 } | |
90 | |
91 bool PEImageReader::GetSectionByName(const std::string& name, | |
92 IMAGE_SECTION_HEADER* section) const { | |
93 if (name.size() > sizeof(section->Name)) { | |
94 LOG(WARNING) << "supplied section name too long " << name; | |
95 return false; | |
96 } | |
97 | |
98 IMAGE_DOS_HEADER dos_header; | |
99 if (!CheckedReadMemory(address_, sizeof(IMAGE_DOS_HEADER), &dos_header)) { | |
100 LOG(WARNING) << "could not read dos header of " << module_name_; | |
101 return false; | |
102 } | |
103 | |
104 if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) { | |
105 LOG(WARNING) << "invalid e_magic in dos header of " << module_name_; | |
106 return false; | |
107 } | |
108 | |
109 // TODO(scottmg): This is reading a same-bitness sized structure. | |
110 IMAGE_NT_HEADERS nt_headers; | |
111 uintptr_t nt_headers_address = address_ + dos_header.e_lfanew; | |
112 if (!CheckedReadMemory( | |
113 nt_headers_address, sizeof(IMAGE_NT_HEADERS), &nt_headers)) { | |
114 LOG(WARNING) << "could not read nt headers of " << module_name_; | |
115 return false; | |
116 } | |
117 | |
118 if (nt_headers.Signature != IMAGE_NT_SIGNATURE) { | |
119 LOG(WARNING) << "invalid signature in nt headers of " << module_name_; | |
120 return false; | |
121 } | |
122 | |
123 uintptr_t first_section_address = | |
124 nt_headers_address + offsetof(IMAGE_NT_HEADERS, OptionalHeader) + | |
125 nt_headers.FileHeader.SizeOfOptionalHeader; | |
126 for (DWORD i = 0; i < nt_headers.FileHeader.NumberOfSections; ++i) { | |
127 uintptr_t section_address = | |
128 first_section_address + sizeof(IMAGE_SECTION_HEADER) * i; | |
129 if (!CheckedReadMemory( | |
130 section_address, sizeof(IMAGE_SECTION_HEADER), section)) { | |
131 LOG(WARNING) << "could not read section " << i << " of " << module_name_; | |
132 return false; | |
133 } | |
134 if (strncmp(reinterpret_cast<const char*>(section->Name), | |
135 name.c_str(), | |
136 sizeof(section->Name)) == 0) { | |
137 return true; | |
138 } | |
139 } | |
140 | |
141 return false; | |
142 } | |
143 | |
144 bool PEImageReader::CheckedReadMemory(uintptr_t address, | |
145 uintptr_t size, | |
146 void* into) const { | |
147 if (!module_range_.ContainsRange( | |
148 CheckedAddressRange(process_reader_->Is64Bit(), address, size))) { | |
149 LOG(WARNING) << base::StringPrintf( | |
150 "invalid read range 0x%llx + 0x%llx in %s", | |
151 address, | |
152 size, | |
153 module_name_.c_str()); | |
154 return false; | |
155 } | |
156 return process_reader_->ReadMemory(address, size, into); | |
157 } | |
158 | |
159 } // namespace crashpad | |
OLD | NEW |