-
Notifications
You must be signed in to change notification settings - Fork 86
/
elf_hook.c
478 lines (388 loc) · 13.4 KB
/
elf_hook.c
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <elf.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
//rename standart types for convenience
#if defined __x86_64 || defined __aarch64__
#define Elf_Ehdr Elf64_Ehdr
#define Elf_Shdr Elf64_Shdr
#define Elf_Sym Elf64_Sym
#define Elf_Rel Elf64_Rela
#define ELF_R_SYM ELF64_R_SYM
#define REL_DYN ".rela.dyn"
#define REL_PLT ".rela.plt"
#else
#define Elf_Ehdr Elf32_Ehdr
#define Elf_Shdr Elf32_Shdr
#define Elf_Sym Elf32_Sym
#define Elf_Rel Elf32_Rel
#define ELF_R_SYM ELF32_R_SYM
#define REL_DYN ".rel.dyn"
#define REL_PLT ".rel.plt"
#endif
//==================================================================================================
static int read_header(int d, Elf_Ehdr **header)
{
*header = (Elf_Ehdr *)malloc(sizeof(Elf_Ehdr));
if(NULL == *header)
{
return errno;
}
if (lseek(d, 0, SEEK_SET) < 0)
{
free(*header);
return errno;
}
if (read(d, *header, sizeof(Elf_Ehdr)) <= 0)
{
free(*header);
return errno = EINVAL;
}
return 0;
}
//--------------------------------------------------------------------------------------------------
static int read_section_table(int d, Elf_Ehdr const *header, Elf_Shdr **table)
{
size_t size;
if (NULL == header)
return EINVAL;
size = header->e_shnum * sizeof(Elf_Shdr);
*table = (Elf_Shdr *)malloc(size);
if(NULL == *table)
{
return errno;
}
if (lseek(d, header->e_shoff, SEEK_SET) < 0)
{
free(*table);
return errno;
}
if (read(d, *table, size) <= 0)
{
free(*table);
return errno = EINVAL;
}
return 0;
}
//--------------------------------------------------------------------------------------------------
static int read_string_table(int d, Elf_Shdr const *section, char const **strings)
{
if (NULL == section)
return EINVAL;
*strings = (char const *)malloc(section->sh_size);
if(NULL == *strings)
{
return errno;
}
if (lseek(d, section->sh_offset, SEEK_SET) < 0)
{
free((void *)*strings);
return errno;
}
if (read(d, (char *)*strings, section->sh_size) <= 0)
{
free((void *)*strings);
return errno = EINVAL;
}
return 0;
}
//--------------------------------------------------------------------------------------------------
static int read_symbol_table(int d, Elf_Shdr const *section, Elf_Sym **table)
{
if (NULL == section)
return EINVAL;
*table = (Elf_Sym *)malloc(section->sh_size);
if(NULL == *table)
{
return errno;
}
if (lseek(d, section->sh_offset, SEEK_SET) < 0)
{
free(*table);
return errno;
}
if (read(d, *table, section->sh_size) <= 0)
{
free(*table);
return errno = EINVAL;
}
return 0;
}
//--------------------------------------------------------------------------------------------------
static int section_by_index(int d, size_t const index, Elf_Shdr **section)
{
Elf_Ehdr *header = NULL;
Elf_Shdr *sections = NULL;
*section = NULL;
if (
read_header(d, &header) ||
read_section_table(d, header, §ions)
)
return errno;
if (index < header->e_shnum)
{
*section = (Elf_Shdr *)malloc(sizeof(Elf_Shdr));
if (NULL == *section)
{
free(header);
free(sections);
return errno;
}
memcpy(*section, sections + index, sizeof(Elf_Shdr));
}
else
return EINVAL;
free(header);
free(sections);
return 0;
}
//--------------------------------------------------------------------------------------------------
static int section_by_type(int d, size_t const section_type, Elf_Shdr **section)
{
Elf_Ehdr *header = NULL;
Elf_Shdr *sections = NULL;
size_t i;
*section = NULL;
if (
read_header(d, &header) ||
read_section_table(d, header, §ions)
)
return errno;
for (i = 0; i < header->e_shnum; ++i)
if (section_type == sections[i].sh_type)
{
*section = (Elf_Shdr *)malloc(sizeof(Elf_Shdr));
if (NULL == *section)
{
free(header);
free(sections);
return errno;
}
memcpy(*section, sections + i, sizeof(Elf_Shdr));
break;
}
free(header);
free(sections);
return 0;
}
//--------------------------------------------------------------------------------------------------
static int section_by_name(int d, char const *section_name, Elf_Shdr **section)
{
Elf_Ehdr *header = NULL;
Elf_Shdr *sections = NULL;
char const *strings = NULL;
size_t i;
*section = NULL;
if (
read_header(d, &header) ||
read_section_table(d, header, §ions) ||
read_string_table(d, §ions[header->e_shstrndx], &strings)
)
return errno;
for (i = 0; i < header->e_shnum; ++i)
if (!strcmp(section_name, &strings[sections[i].sh_name]))
{
*section = (Elf_Shdr *)malloc(sizeof(Elf_Shdr));
if (NULL == *section)
{
free(header);
free(sections);
free((void *)strings);
return errno;
}
memcpy(*section, sections + i, sizeof(Elf_Shdr));
break;
}
free(header);
free(sections);
free((void *)strings);
return 0;
}
//--------------------------------------------------------------------------------------------------
static int symbol_by_name(int d, Elf_Shdr *section, char const *name, Elf_Sym **symbol, size_t *index)
{
Elf_Shdr *strings_section = NULL;
char const *strings = NULL;
Elf_Sym *symbols = NULL;
size_t i, amount;
*symbol = NULL;
*index = 0;
if (
section_by_index(d, section->sh_link, &strings_section) ||
read_string_table(d, strings_section, &strings) ||
read_symbol_table(d, section, &symbols)
)
return errno;
amount = section->sh_size / sizeof(Elf_Sym);
for (i = 0; i < amount; ++i)
if (!strcmp(name, &strings[symbols[i].st_name]))
{
*symbol = (Elf_Sym *)malloc(sizeof(Elf_Sym));
if (NULL == *symbol)
{
free(strings_section);
free((void *)strings);
free(symbols);
return errno;
}
memcpy(*symbol, symbols + i, sizeof(Elf_Sym));
*index = i;
break;
}
free(strings_section);
free((void *)strings);
free(symbols);
return 0;
}
//--------------------------------------------------------------------------------------------------
int get_module_base_address(char const *module_filename, void *handle, void **base)
{
int descriptor; //file descriptor of shared module
Elf_Shdr *dynsym = NULL, *strings_section = NULL;
char const *strings = NULL;
Elf_Sym *symbols = NULL;
size_t i, amount;
Elf_Sym *found = NULL;
*base = NULL;
descriptor = open(module_filename, O_RDONLY);
if (descriptor < 0)
return errno;
if (section_by_type(descriptor, SHT_DYNSYM, &dynsym) || //get ".dynsym" section
section_by_index(descriptor, dynsym->sh_link, &strings_section) ||
read_string_table(descriptor, strings_section, &strings) ||
read_symbol_table(descriptor, dynsym, &symbols))
{
free(strings_section);
free((void *)strings);
free(symbols);
free(dynsym);
close(descriptor);
return errno;
}
amount = dynsym->sh_size / sizeof(Elf_Sym);
/* Trick to get the module base address in a portable way:
* Find the first GLOBAL or WEAK symbol in the symbol table,
* look this up with dlsym, then return the difference as the base address
*/
for (i = 0; i < amount; ++i)
{
switch(ELF32_ST_BIND(symbols[i].st_info)) {
case STB_GLOBAL:
case STB_WEAK:
found = &symbols[i];
break;
default: // Not interested in this symbol
break;
}
}
if(found != NULL)
{
const char *name = &strings[found->st_name];
void *sym = dlsym(handle, name);
if(sym != NULL)
*base = (void*)((size_t)sym - found->st_value);
}
free(strings_section);
free((void *)strings);
free(symbols);
free(dynsym);
close(descriptor);
return *base == NULL;
}
//--------------------------------------------------------------------------------------------------
#ifdef __cplusplus
extern "C"
{
#endif
void *elf_hook(char const *module_filename, void const *module_address, char const *name, void const *substitution)
{
static size_t pagesize;
int descriptor; //file descriptor of shared module
Elf_Shdr
*dynsym = NULL, // ".dynsym" section header
*rel_plt = NULL, // ".rel.plt" section header
*rel_dyn = NULL; // ".rel.dyn" section header
Elf_Sym
*symbol = NULL; //symbol table entry for symbol named "name"
Elf_Rel
*rel_plt_table = NULL, //array with ".rel.plt" entries
*rel_dyn_table = NULL; //array with ".rel.dyn" entries
size_t
i,
name_index, //index of symbol named "name" in ".dyn.sym"
rel_plt_amount, // amount of ".rel.plt" entries
rel_dyn_amount, // amount of ".rel.dyn" entries
*name_address = NULL; //address of relocation for symbol named "name"
void *original = NULL; //address of the symbol being substituted
if (NULL == module_address || NULL == name || NULL == substitution)
return original;
if (!pagesize)
pagesize = sysconf(_SC_PAGESIZE);
descriptor = open(module_filename, O_RDONLY);
if (descriptor < 0)
return original;
if (
section_by_type(descriptor, SHT_DYNSYM, &dynsym) || //get ".dynsym" section
symbol_by_name(descriptor, dynsym, name, &symbol, &name_index) || //actually, we need only the index of symbol named "name" in the ".dynsym" table
section_by_name(descriptor, REL_PLT, &rel_plt) || //get ".rel.plt" (for 32-bit) or ".rela.plt" (for 64-bit) section
section_by_name(descriptor, REL_DYN, &rel_dyn) //get ".rel.dyn" (for 32-bit) or ".rela.dyn" (for 64-bit) section
)
{ //if something went wrong
free(dynsym);
free(rel_plt);
free(rel_dyn);
free(symbol);
close(descriptor);
return original;
}
//release the data used
free(dynsym);
free(symbol);
rel_plt_table = (Elf_Rel *)(((size_t)module_address) + rel_plt->sh_addr); //init the ".rel.plt" array
rel_plt_amount = rel_plt->sh_size / sizeof(Elf_Rel); //and get its size
rel_dyn_table = (Elf_Rel *)(((size_t)module_address) + rel_dyn->sh_addr); //init the ".rel.dyn" array
rel_dyn_amount = rel_dyn->sh_size / sizeof(Elf_Rel); //and get its size
//release the data used
free(rel_plt);
free(rel_dyn);
//and descriptor
close(descriptor);
//now we've got ".rel.plt" (needed for PIC) table and ".rel.dyn" (for non-PIC) table and the symbol's index
for (i = 0; i < rel_plt_amount; ++i) //lookup the ".rel.plt" table
if (ELF_R_SYM(rel_plt_table[i].r_info) == name_index) //if we found the symbol to substitute in ".rel.plt"
{
original = (void *)*(size_t *)(((size_t)module_address) + rel_plt_table[i].r_offset); //save the original function address
*(size_t *)(((size_t)module_address) + rel_plt_table[i].r_offset) = (size_t)substitution; //and replace it with the substitutional
break; //the target symbol appears in ".rel.plt" only once
}
if (original)
return original;
//we will get here only with 32-bit non-PIC module
for (i = 0; i < rel_dyn_amount; ++i) //lookup the ".rel.dyn" table
if (ELF_R_SYM(rel_dyn_table[i].r_info) == name_index) //if we found the symbol to substitute in ".rel.dyn"
{
name_address = (size_t *)(((size_t)module_address) + rel_dyn_table[i].r_offset); //get the relocation address (address of a relative CALL (0xE8) instruction's argument)
if (!original)
original = (void *)(*name_address + (size_t)name_address + sizeof(size_t)); //calculate an address of the original function by a relative CALL (0xE8) instruction's argument
if(mprotect((void *)(((size_t)name_address) & (((size_t)-1) ^ (pagesize - 1))), pagesize, PROT_READ | PROT_WRITE) < 0) //mark a memory page that contains the relocation as writable
{
return NULL;
}
*name_address = (size_t)substitution - (size_t)name_address - sizeof(size_t); //calculate a new relative CALL (0xE8) instruction's argument for the substitutional function and write it down
if(mprotect((void *)(((size_t)name_address) & (((size_t)-1) ^ (pagesize - 1))), pagesize, PROT_READ | PROT_EXEC) < 0) //mark a memory page that contains the relocation back as executable
{
*name_address = (size_t)original - (size_t)name_address - sizeof(size_t); //if something went wrong then restore the original function address
return NULL;
}
}
return original;
}
#ifdef __cplusplus
}
#endif
//==================================================================================================