forked from GJDuck/e9patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe9patch.h
More file actions
401 lines (353 loc) · 10.2 KB
/
e9patch.h
File metadata and controls
401 lines (353 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* e9patch.h
* Copyright (C) 2020 National University of Singapore
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __E9PATCH_H
#define __E9PATCH_H
#include <cassert>
#include <cstdarg>
#include <cstdint>
#include <cstring>
#include <elf.h>
#include <map>
#include <vector>
#define NO_RETURN __attribute__((__noreturn__))
#define NO_INLINE __attribute__((__noinline__))
#define PAGE_SIZE ((size_t)4096)
/*
* States of each virtual memory byte.
*/
#define STATE_UNKNOWN 0x0 // Unknown or "don't care"
#define STATE_INSTRUCTION 0x1 // Used by an instruction.
#define STATE_PATCHED 0x2 // Used by a patched instruction.
#define STATE_FREE 0x3 // Was used by instruction, now free.
#define STATE_OVERFLOW 0x4 // Byte is past the end-of-file.
#define STATE_LOCKED 0x10 // Byte is locked (read-only).
/*
* C-string comparator.
*/
struct CStrCmp
{
bool operator()(const char* a, const char* b) const
{
return (strcmp(a, b) < 0);
}
};
/*
* Buffer.
*/
struct Buffer
{
unsigned i = 0;
const unsigned max;
uint8_t * const bytes;
Buffer(uint8_t * bytes, unsigned max = UINT32_MAX) : bytes(bytes), max(max)
{
;
}
void push(uint8_t b)
{
assert(i < max);
if (bytes != nullptr)
bytes[i] = b;
i++;
}
void push(const uint8_t *data, unsigned len)
{
assert(i + len <= max);
if (bytes != nullptr)
memcpy(bytes + i, data, len);
i += len;
}
size_t size()
{
return (size_t)i;
}
};
/*
* Bounds.
*/
struct Bounds
{
intptr_t lb; // Lower bound.
intptr_t ub; // Upper bound.
};
/*
* Trampoline template entry kind.
*/
enum EntryKind
{
ENTRY_BYTES,
ENTRY_ZEROES,
ENTRY_LABEL,
ENTRY_MACRO,
ENTRY_REL8,
ENTRY_REL32,
ENTRY_INT8,
ENTRY_INT16,
ENTRY_INT32,
ENTRY_INT64,
ENTRY_INSTRUCTION,
ENTRY_INSTRUCTION_BYTES,
ENTRY_CONTINUE,
ENTRY_TAKEN,
};
/*
* Trampoline template entry.
*/
struct Entry
{
EntryKind kind; // Entry kind
union
{
unsigned length; // Entry length
bool use_label; // Use label for rel8/rel32?
};
union
{
const uint8_t *bytes; // Raw bytes
const char *label; // Label name
const char *macro; // Macro name
uint8_t uint8; // 8bit integer constant
uint16_t uint16; // 16bit integer constant
uint32_t uint32; // 32bit integer constant
uint64_t uint64; // 64bit integer constant
};
};
/*
* A trampoline template.
*/
struct Trampoline
{
const char *name; // Name (if applicable)
int prot:31; // Protections.
int preload:1; // Pre-load trampoline?
unsigned num_entries; // Number of entries.
// Entries.
Entry entries[];
};
/*
* The default evictee trampoline template.
*/
extern const Trampoline *evicteeTrampoline;
/*
* Metadata representation.
*/
struct Metadata
{
size_t num_entries; // Number of entries.
Trampoline *entries[]; // Entries.
};
/*
* Instruction representation.
*/
struct Instr
{
const size_t offset:48; // The instruction offset
const size_t size:4; // The instruction size (bytes)
const size_t pcrel32_idx:4; // 32bit PC-relative imm idx (or 0)
const size_t pcrel8_idx:4; // 8bit PC-relative imm idx (or 0)
const size_t pic:1; // PIC? (stored here for convenience)
const intptr_t addr; // The address of the instruction
intptr_t trampoline = INTPTR_MIN; // The address of any trampoline
const struct Original
{
const uint8_t * const bytes; // The (unmodified) instruction
Original(const uint8_t *bytes) : bytes(bytes)
{
;
}
} original;
const struct Patched
{
uint8_t * const bytes; // The (modified/patched) instruction
uint8_t * const state; // The instruction state
Patched(uint8_t *bytes, uint8_t *state) : bytes(bytes), state(state)
{
;
}
} patched;
const Metadata *metadata = nullptr; // The instruction metadata.
Instr *prev = nullptr; // The previous instruction.
Instr *next = nullptr; // The next instruction.
Instr(off_t offset, intptr_t addr, size_t size, const uint8_t *original,
uint8_t *bytes, uint8_t *state, size_t pcrel32_idx,
size_t pcrel8_idx, bool pic) :
offset((size_t)offset), addr(addr), size(size), original(original),
patched(bytes, state), pcrel32_idx(pcrel32_idx),
pcrel8_idx(pcrel8_idx), pic(pic)
{
;
}
};
/*
* Virtual address space allocation.
*/
struct Alloc
{
intptr_t lb; // Allocation lower bound
intptr_t ub; // Allocation upper bound
const Instr *I; // Instruction.
const Trampoline *T; // Trampoline.
};
/*
* Interval tree.
*/
struct Node;
struct Tree
{
Node *root; // Interval tree root
};
/*
* Virtual address space allocator.
*/
struct Allocator
{
Tree tree; // Interval tree
/*
* Iterators.
*/
struct iterator
{
Node *node = nullptr;
iterator() = default;
iterator(const iterator &i) : node(i.node)
{
;
}
iterator(Node *node) : node(node)
{
;
}
const Alloc *operator*();
void operator++();
bool operator!=(const iterator &i)
{
return (node != i.node);
}
bool operator==(const iterator &i)
{
return (node == i.node);
}
void operator=(const iterator &i)
{
node = i.node;
}
};
iterator begin() const;
static iterator end()
{
iterator i;
return i;
}
iterator find(intptr_t addr) const;
Allocator()
{
tree.root = nullptr;
}
};
/*
* The (minimal) ELF info needed for rewriting.
*/
struct ElfInfo
{
Elf64_Ehdr *ehdr; // EHDR (Elf header)
Elf64_Phdr *phdr_note; // PHDR PT_NOTE to be used for loader.
Elf64_Phdr *phdr_dynamic; // PHDR PT_DYNAMIC else nullptr.
bool pic; // Position independent?
};
/*
* Supported binary modes.
*/
enum Mode
{
MODE_EXECUTABLE, // Binary is an executable.
MODE_SHARED_OBJECT // Binary is a shared object.
};
/*
* Binary representation.
*/
typedef std::map<off_t, Instr *> InstrSet;
typedef std::map<const char *, Trampoline *, CStrCmp> TrampolineSet;
typedef std::vector<intptr_t> InitSet;
struct Binary
{
const char *filename; // The binary's path.
size_t size; // The binary's size.
ElfInfo elf; // ELF information.
Mode mode; // Binary mode.
struct
{
const uint8_t *bytes; // The original binary bytes.
int fd; // The original binary file descr.
} original;
struct
{
uint8_t *bytes; // The patched binary bytes.
uint8_t *state; // The patched binary state.
size_t size; // The patched binary size.
} patched;
InstrSet Is; // All (known) instructions.
TrampolineSet Ts; // All current trampoline templates.
Allocator allocator; // Virtual address allocation.
InitSet inits; // Initialization functions.
intptr_t mmap = INTPTR_MIN; // Mmap function.
};
/*
* Global options.
*/
extern bool option_is_tty;
extern bool option_debug;
extern bool option_disable_B1;
extern bool option_disable_B2;
extern bool option_disable_T1;
extern bool option_disable_T2;
extern bool option_disable_T3;
extern bool option_static_loader;
extern bool option_same_page;
extern bool option_use_stack;
extern intptr_t option_lb;
extern intptr_t option_ub;
/*
* Global statistics.
*/
extern size_t stat_num_patched;
extern size_t stat_num_failed;
extern size_t stat_num_B1;
extern size_t stat_num_B2;
extern size_t stat_num_T1;
extern size_t stat_num_T2;
extern size_t stat_num_T3;
extern size_t stat_num_virtual_mappings;
extern size_t stat_num_physical_mappings;
extern size_t stat_num_virtual_bytes;
extern size_t stat_num_physical_bytes;
extern size_t stat_input_file_size;
extern size_t stat_output_file_size;
extern void NO_RETURN error(const char *msg, ...);
extern void warning(const char *msg, ...);
extern void debugImpl(const char *msg, ...);
#define debug(msg, ...) \
do { \
if (__builtin_expect(option_debug, false)) \
debugImpl((msg), ##__VA_ARGS__); \
} while (false)
#define ADDRESS_FORMAT "%s%s0x%lx"
#define ADDRESS(p) \
(IS_ABSOLUTE(p)? "[absolute] ": ""), \
((p) < 0? "-": ""), \
std::abs(BASE_ADDRESS(p))
#endif