LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
ChainedPointerAnalysis.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_MACHO_CHAINED_PTR_ANALYSIS_H
17#define LIEF_MACHO_CHAINED_PTR_ANALYSIS_H
18#include <memory>
19#include <ostream>
20#include <functional>
21
23#include "LIEF/errors.hpp"
24#include "LIEF/visibility.h"
25
26namespace LIEF {
27class BinaryStream;
28namespace MachO {
30 public:
31 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
32 struct dyld_chained_ptr_arm64e_rebase_t
33 {
34 uint64_t target : 43,
35 high8 : 8,
36 next : 11,
37 bind : 1,
38 auth : 1;
39
40 friend LIEF_API
41 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_rebase_t& chain);
42
43 uint64_t unpack_target() const {
44 return uint64_t(high8) | target;
45 }
46 };
47
48 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
49 struct dyld_chained_ptr_arm64e_bind_t
50 {
51 uint64_t ordinal : 16,
52 zero : 16,
53 addend : 19,
54 next : 11,
55 bind : 1,
56 auth : 1;
57
58 friend LIEF_API
59 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_bind_t& chain);
60 };
61
62 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
63 struct dyld_chained_ptr_arm64e_auth_rebase_t
64 {
65 uint64_t target : 32,
66 diversity : 16,
67 addr_div : 1,
68 key : 2,
69 next : 11,
70 bind : 1,
71 auth : 1;
72
73 friend LIEF_API
74 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_auth_rebase_t& chain);
75 };
76
77 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
78 struct dyld_chained_ptr_arm64e_auth_bind_t
79 {
80 uint64_t ordinal : 16,
81 zero : 16,
82 diversity : 16,
83 addr_div : 1,
84 key : 2,
85 next : 11,
86 bind : 1,
87 auth : 1;
88
89 friend LIEF_API
90 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_auth_bind_t& chain);
91 };
92
93 // DYLD_CHAINED_PTR_FORMAT::PTR_64 & DYLD_CHAINED_PTR_FORMAT::PTR_64_OFFSET
94 struct dyld_chained_ptr_64_rebase_t
95 {
96 uint64_t target : 36,
97 high8 : 8,
98 reserved : 7,
99 next : 12,
100 bind : 1;
101
102 friend LIEF_API
103 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_64_rebase_t& chain);
104
105 uint64_t unpack_target() const {
106 return uint64_t(high8) | target;
107 }
108 };
109
110 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24
111 struct dyld_chained_ptr_arm64e_bind24_t
112 {
113 uint64_t ordinal : 24,
114 zero : 8,
115 addend : 19,
116 next : 11,
117 bind : 1,
118 auth : 1;
119
120 friend LIEF_API
121 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_bind24_t& chain);
122 };
123
124 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24
125 struct dyld_chained_ptr_arm64e_auth_bind24_t
126 {
127 uint64_t ordinal : 24,
128 zero : 8,
129 diversity : 16,
130 addr_div : 1,
131 key : 2,
132 next : 11,
133 bind : 1,
134 auth : 1;
135
136 friend LIEF_API
137 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_auth_bind24_t& chain);
138 };
139
140 // DYLD_CHAINED_PTR_FORMAT::PTR_64
141 struct dyld_chained_ptr_64_bind_t
142 {
143 uint64_t ordinal : 24,
144 addend : 8,
145 reserved : 19,
146 next : 12,
147 bind : 1;
148
149 friend LIEF_API
150 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_64_bind_t& chain);
151 };
152
153 // DYLD_CHAINED_PTR_FORMAT::PTR_64_KERNEL_CACHE
154 struct dyld_chained_ptr_64_kernel_cache_rebase_t
155 {
156 uint64_t target : 30,
157 cache_level : 2,
158 diversity : 16,
159 addr_div : 1,
160 key : 2,
161 next : 12,
162 is_auth : 1;
163
164 friend LIEF_API
165 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_64_kernel_cache_rebase_t& chain);
166 };
167
168 // DYLD_CHAINED_PTR_FORMAT::PTR_32
169 struct dyld_chained_ptr_32_rebase_t
170 {
171 uint32_t target : 26,
172 next : 5,
173 bind : 1;
174
175 friend LIEF_API
176 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_32_rebase_t& chain);
177 };
178
179 // DYLD_CHAINED_PTR_FORMAT::PTR_32
180 struct dyld_chained_ptr_32_bind_t
181 {
182 uint32_t ordinal : 20,
183 addend : 6,
184 next : 5,
185 bind : 1;
186
187 friend LIEF_API
188 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_32_bind_t& chain);
189 };
190
191 // DYLD_CHAINED_PTR_FORMAT::PTR_32_CACHE
192 struct dyld_chained_ptr_32_cache_rebase_t
193 {
194 uint32_t target : 30,
195 next : 2;
196
197 friend LIEF_API
198 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_32_cache_rebase_t& chain);
199 };
200
201 // DYLD_CHAINED_PTR_FORMAT::PTR_32_FIRMWARE
202 struct dyld_chained_ptr_32_firmware_rebase_t
203 {
204 uint32_t target : 26,
205 next : 6;
206
207 friend LIEF_API
208 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_32_firmware_rebase_t& chain);
209 };
210
211 enum class PTR_TYPE {
213 DYLD_CHAINED_PTR_ARM64E_REBASE,
214 DYLD_CHAINED_PTR_ARM64E_BIND,
215 DYLD_CHAINED_PTR_ARM64E_AUTH_REBASE,
216 DYLD_CHAINED_PTR_ARM64E_AUTH_BIND,
217 DYLD_CHAINED_PTR_64_REBASE,
218 DYLD_CHAINED_PTR_ARM64E_BIND24,
219 DYLD_CHAINED_PTR_ARM64E_AUTH_BIND24,
220 DYLD_CHAINED_PTR_64_BIND,
221 DYLD_CHAINED_PTR_64_KERNEL_CACHE_REBASE,
222 DYLD_CHAINED_PTR_32_REBASE,
223 DYLD_CHAINED_PTR_32_BIND,
224 DYLD_CHAINED_PTR_32_CACHE_REBASE,
225 DYLD_CHAINED_PTR_32_FIRMWARE_REBASE
226 };
227
228 static std::unique_ptr<ChainedPointerAnalysis> from_value(uint64_t value,
229 size_t size)
230 {
231 return std::unique_ptr<ChainedPointerAnalysis>(
232 new ChainedPointerAnalysis(value, size));
233 }
234
235 static size_t stride(DYLD_CHAINED_PTR_FORMAT fmt) {
236 switch (fmt) {
237 case DYLD_CHAINED_PTR_FORMAT::NONE:
238 return 0;
239 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E:
240 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND:
241 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24:
242 return 8;
243
244 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_KERNEL:
245 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_FIRMWARE:
246 case DYLD_CHAINED_PTR_FORMAT::PTR_32_FIRMWARE:
247 case DYLD_CHAINED_PTR_FORMAT::PTR_64:
248 case DYLD_CHAINED_PTR_FORMAT::PTR_64_OFFSET:
249 case DYLD_CHAINED_PTR_FORMAT::PTR_32:
250 case DYLD_CHAINED_PTR_FORMAT::PTR_32_CACHE:
251 case DYLD_CHAINED_PTR_FORMAT::PTR_64_KERNEL_CACHE:
252 return 4;
253
254 case DYLD_CHAINED_PTR_FORMAT::PTR_X86_64_KERNEL_CACHE:
255 return 1;
256 }
257 return 0;
258 }
259
260 static size_t ptr_size(DYLD_CHAINED_PTR_FORMAT fmt) {
261 switch (fmt) {
262 case DYLD_CHAINED_PTR_FORMAT::NONE:
263 return 0;
264 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E:
265 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND:
266 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24:
267 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_KERNEL:
268 case DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_FIRMWARE:
269 case DYLD_CHAINED_PTR_FORMAT::PTR_64:
270 case DYLD_CHAINED_PTR_FORMAT::PTR_64_OFFSET:
271 case DYLD_CHAINED_PTR_FORMAT::PTR_64_KERNEL_CACHE:
272 case DYLD_CHAINED_PTR_FORMAT::PTR_X86_64_KERNEL_CACHE:
273 return sizeof(uint64_t);
274
275 case DYLD_CHAINED_PTR_FORMAT::PTR_32_FIRMWARE:
276 case DYLD_CHAINED_PTR_FORMAT::PTR_32:
277 case DYLD_CHAINED_PTR_FORMAT::PTR_32_CACHE:
278 return sizeof(uint32_t);
279 }
280 return 0;
281 }
282
283 ChainedPointerAnalysis(uint64_t value, size_t size) :
284 value_(value),
285 size_(size)
286 {}
287
288 ChainedPointerAnalysis(const ChainedPointerAnalysis&) = default;
289 ChainedPointerAnalysis& operator=(const ChainedPointerAnalysis&) = default;
290
291 ChainedPointerAnalysis(ChainedPointerAnalysis&&) noexcept = default;
292 ChainedPointerAnalysis& operator=(ChainedPointerAnalysis&&) noexcept = default;
293
294 ~ChainedPointerAnalysis() = default;
295
296 uint64_t value() const {
297 return value_;
298 }
299
300 size_t size() const {
301 return size_;
302 }
303
304 const dyld_chained_ptr_arm64e_rebase_t dyld_chained_ptr_arm64e_rebase() const {
305 return *reinterpret_cast<const dyld_chained_ptr_arm64e_rebase_t*>(&value_);
306 }
307
308 const dyld_chained_ptr_arm64e_bind_t& dyld_chained_ptr_arm64e_bind() const {
309 return *reinterpret_cast<const dyld_chained_ptr_arm64e_bind_t*>(&value_);
310 }
311
312 const dyld_chained_ptr_arm64e_auth_rebase_t dyld_chained_ptr_arm64e_auth_rebase() const {
313 return *reinterpret_cast<const dyld_chained_ptr_arm64e_auth_rebase_t*>(&value_);
314 }
315
316 const dyld_chained_ptr_arm64e_auth_bind_t dyld_chained_ptr_arm64e_auth_bind() const {
317 return *reinterpret_cast<const dyld_chained_ptr_arm64e_auth_bind_t*>(&value_);
318 }
319
320 const dyld_chained_ptr_64_rebase_t dyld_chained_ptr_64_rebase() const {
321 return *reinterpret_cast<const dyld_chained_ptr_64_rebase_t*>(&value_);
322 }
323
324 const dyld_chained_ptr_arm64e_bind24_t dyld_chained_ptr_arm64e_bind24() const {
325 return *reinterpret_cast<const dyld_chained_ptr_arm64e_bind24_t*>(&value_);
326 }
327
328 const dyld_chained_ptr_arm64e_auth_bind24_t dyld_chained_ptr_arm64e_auth_bind24() const {
329 return *reinterpret_cast<const dyld_chained_ptr_arm64e_auth_bind24_t*>(&value_);
330 }
331
332 const dyld_chained_ptr_64_bind_t dyld_chained_ptr_64_bind() const {
333 return *reinterpret_cast<const dyld_chained_ptr_64_bind_t*>(&value_);
334 }
335
336 const dyld_chained_ptr_64_kernel_cache_rebase_t dyld_chained_ptr_64_kernel_cache_rebase() const {
337 return *reinterpret_cast<const dyld_chained_ptr_64_kernel_cache_rebase_t*>(&value_);
338 }
339
340 const dyld_chained_ptr_32_rebase_t dyld_chained_ptr_32_rebase() const {
341 return *reinterpret_cast<const dyld_chained_ptr_32_rebase_t*>(&value_);
342 }
343
344 const dyld_chained_ptr_32_bind_t dyld_chained_ptr_32_bind() const {
345 return *reinterpret_cast<const dyld_chained_ptr_32_bind_t*>(&value_);
346 }
347
348 const dyld_chained_ptr_32_cache_rebase_t dyld_chained_ptr_32_cache_rebase() const {
349 return *reinterpret_cast<const dyld_chained_ptr_32_cache_rebase_t*>(&value_);
350 }
351
352 const dyld_chained_ptr_32_firmware_rebase_t dyld_chained_ptr_32_firmware_rebase() const {
353 return *reinterpret_cast<const dyld_chained_ptr_32_firmware_rebase_t*>(&value_);
354 }
355
356 struct union_pointer_t {
357 PTR_TYPE type = PTR_TYPE::UNKNOWN;
358 union {
359 dyld_chained_ptr_arm64e_rebase_t arm64e_rebase;
360 dyld_chained_ptr_arm64e_bind_t arm64e_bind;
361 dyld_chained_ptr_arm64e_auth_rebase_t arm64e_auth_rebase;
362 dyld_chained_ptr_arm64e_auth_bind_t arm64e_auth_bind;
363 dyld_chained_ptr_64_rebase_t ptr_64_rebase;
364 dyld_chained_ptr_arm64e_bind24_t arm64e_bind24;
365 dyld_chained_ptr_arm64e_auth_bind24_t arm64e_auth_bind24;
366 dyld_chained_ptr_64_bind_t ptr_64_bind;
367 dyld_chained_ptr_64_kernel_cache_rebase_t ptr_64_kernel_cache_rebase;
368 dyld_chained_ptr_32_rebase_t ptr_32_rebase;
369 dyld_chained_ptr_32_bind_t ptr_32_bind;
370 dyld_chained_ptr_32_cache_rebase_t ptr_32_cache_rebase;
371 dyld_chained_ptr_32_firmware_rebase_t ptr_32_firmware_rebase;
372 uint64_t raw;
373 };
374 uint32_t next() const;
375
376 result<uint32_t> ordinal() const;
377 result<uint64_t> target() const;
378
379 bool is_bind() const {
380 return (bool)ordinal();
381 }
382 bool is_auth() const;
383
384 friend LIEF_API
385 std::ostream& operator<<(std::ostream& os, const union_pointer_t& ptr);
386 };
387
388 static_assert(sizeof(union_pointer_t) == 16);
389
390 union_pointer_t get_as(DYLD_CHAINED_PTR_FORMAT fmt) const;
391
392 static uint64_t walk_chain(
393 BinaryStream& stream, DYLD_CHAINED_PTR_FORMAT format,
394 const std::function<int(uint64_t, const union_pointer_t& ptr)>& callback);
395
396 private:
397 uint64_t value_ = 0;
398 size_t size_ = 0;
399};
400}
401}
402#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
Definition ChainedPointerAnalysis.hpp:29
union_pointer_t get_as(DYLD_CHAINED_PTR_FORMAT fmt) const
ChainedPointerAnalysis(uint64_t value, size_t size)
Definition ChainedPointerAnalysis.hpp:283
const dyld_chained_ptr_32_bind_t dyld_chained_ptr_32_bind() const
Definition ChainedPointerAnalysis.hpp:344
const dyld_chained_ptr_64_bind_t dyld_chained_ptr_64_bind() const
Definition ChainedPointerAnalysis.hpp:332
const dyld_chained_ptr_64_rebase_t dyld_chained_ptr_64_rebase() const
Definition ChainedPointerAnalysis.hpp:320
size_t size() const
Definition ChainedPointerAnalysis.hpp:300
const dyld_chained_ptr_arm64e_rebase_t dyld_chained_ptr_arm64e_rebase() const
Definition ChainedPointerAnalysis.hpp:304
const dyld_chained_ptr_arm64e_auth_rebase_t dyld_chained_ptr_arm64e_auth_rebase() const
Definition ChainedPointerAnalysis.hpp:312
const dyld_chained_ptr_64_kernel_cache_rebase_t dyld_chained_ptr_64_kernel_cache_rebase() const
Definition ChainedPointerAnalysis.hpp:336
ChainedPointerAnalysis & operator=(const ChainedPointerAnalysis &)=default
const dyld_chained_ptr_arm64e_bind24_t dyld_chained_ptr_arm64e_bind24() const
Definition ChainedPointerAnalysis.hpp:324
const dyld_chained_ptr_arm64e_bind_t & dyld_chained_ptr_arm64e_bind() const
Definition ChainedPointerAnalysis.hpp:308
const dyld_chained_ptr_32_rebase_t dyld_chained_ptr_32_rebase() const
Definition ChainedPointerAnalysis.hpp:340
static size_t stride(DYLD_CHAINED_PTR_FORMAT fmt)
Definition ChainedPointerAnalysis.hpp:235
const dyld_chained_ptr_32_firmware_rebase_t dyld_chained_ptr_32_firmware_rebase() const
Definition ChainedPointerAnalysis.hpp:352
const dyld_chained_ptr_arm64e_auth_bind_t dyld_chained_ptr_arm64e_auth_bind() const
Definition ChainedPointerAnalysis.hpp:316
ChainedPointerAnalysis(ChainedPointerAnalysis &&) noexcept=default
static std::unique_ptr< ChainedPointerAnalysis > from_value(uint64_t value, size_t size)
Definition ChainedPointerAnalysis.hpp:228
const dyld_chained_ptr_32_cache_rebase_t dyld_chained_ptr_32_cache_rebase() const
Definition ChainedPointerAnalysis.hpp:348
static size_t ptr_size(DYLD_CHAINED_PTR_FORMAT fmt)
Definition ChainedPointerAnalysis.hpp:260
ChainedPointerAnalysis(const ChainedPointerAnalysis &)=default
const dyld_chained_ptr_arm64e_auth_bind24_t dyld_chained_ptr_arm64e_auth_bind24() const
Definition ChainedPointerAnalysis.hpp:328
static uint64_t walk_chain(BinaryStream &stream, DYLD_CHAINED_PTR_FORMAT format, const std::function< int(uint64_t, const union_pointer_t &ptr)> &callback)
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
@ UNKNOWN
Definition MachO/enums.hpp:25
LIEF namespace.
Definition Abstract/Binary.hpp:36
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_bind_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_cache_rebase_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_firmware_rebase_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_rebase_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_bind_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_kernel_cache_rebase_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_rebase_t &chain)
uint64_t unpack_target() const
Definition ChainedPointerAnalysis.hpp:105
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_bind24_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_bind_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_rebase_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_bind24_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_bind_t &chain)
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_rebase_t &chain)
uint64_t unpack_target() const
Definition ChainedPointerAnalysis.hpp:43
friend std::ostream & operator<<(std::ostream &os, const union_pointer_t &ptr)
bool is_bind() const
Definition ChainedPointerAnalysis.hpp:379
#define LIEF_API
Definition visibility.h:41