-
Notifications
You must be signed in to change notification settings - Fork 156
/
plthook_elf.c
927 lines (858 loc) · 26.6 KB
/
plthook_elf.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/* -*- indent-tabs-mode: nil -*-
*
* plthook_elf.c -- implementation of plthook for ELF format
*
* URL: https://github.com/kubo/plthook
*
* ------------------------------------------------------
*
* Copyright 2013-2019 Kubo Takehiro <kubo@jiubao.org>
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of the authors.
*
*/
#if defined(__sun) && defined(_XOPEN_SOURCE) && !defined(__EXTENSIONS__)
#define __EXTENSIONS__
#endif
#if defined(__linux__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <sys/mman.h>
#include <errno.h>
#include <dlfcn.h>
#ifdef __sun
#include <sys/auxv.h>
#include <procfs.h>
#define ELF_TARGET_ALL
#endif /* __sun */
#ifdef __FreeBSD__
#include <sys/types.h>
#include <sys/user.h>
#include <libutil.h>
#endif
#include <elf.h>
#include <link.h>
#include "plthook.h"
#if defined __UCLIBC__ && !defined RTLD_NOLOAD
#define RTLD_NOLOAD 0
#endif
#ifndef __GNUC__
#define __attribute__(arg)
#endif
#if defined __FreeBSD__ && defined __i386__ && __ELF_WORD_SIZE == 64
#error 32-bit application on 64-bit OS is not supported.
#endif
#if !defined(R_X86_64_JUMP_SLOT) && defined(R_X86_64_JMP_SLOT)
#define R_X86_64_JUMP_SLOT R_X86_64_JMP_SLOT
#endif
#if defined __x86_64__ || defined __x86_64
#define R_JUMP_SLOT R_X86_64_JUMP_SLOT
#define R_GLOBAL_DATA R_X86_64_GLOB_DAT
#elif defined __i386__ || defined __i386
#define R_JUMP_SLOT R_386_JMP_SLOT
#define R_GLOBAL_DATA R_386_GLOB_DAT
#define USE_REL
#elif defined __arm__ || defined __arm
#define R_JUMP_SLOT R_ARM_JUMP_SLOT
#define R_GLOBAL_DATA R_ARM_GLOB_DAT
#define USE_REL
#elif defined __aarch64__ || defined __aarch64 /* ARM64 */
#define R_JUMP_SLOT R_AARCH64_JUMP_SLOT
#define R_GLOBAL_DATA R_AARCH64_GLOB_DAT
#elif defined __powerpc64__
#define R_JUMP_SLOT R_PPC64_JMP_SLOT
#define R_GLOBAL_DATA R_PPC64_GLOB_DAT
#elif defined __powerpc__
#define R_JUMP_SLOT R_PPC_JMP_SLOT
#define R_GLOBAL_DATA R_PPC_GLOB_DAT
#elif defined __riscv
#define R_JUMP_SLOT R_RISCV_JUMP_SLOT
#if __riscv_xlen == 32
#define R_GLOBAL_DATA R_RISCV_32
#elif __riscv_xlen == 64
#define R_GLOBAL_DATA R_RISCV_64
#else
#error unsupported RISCV implementation
#endif
#elif 0 /* disabled because not tested */ && (defined __sparcv9 || defined __sparc_v9__)
#define R_JUMP_SLOT R_SPARC_JMP_SLOT
#elif 0 /* disabled because not tested */ && (defined __sparc || defined __sparc__)
#define R_JUMP_SLOT R_SPARC_JMP_SLOT
#elif 0 /* disabled because not tested */ && (defined __ia64 || defined __ia64__)
#define R_JUMP_SLOT R_IA64_IPLTMSB
#else
#error unsupported OS
#endif
#ifdef USE_REL
#define Elf_Plt_Rel Elf_Rel
#define PLT_DT_REL DT_REL
#define PLT_DT_RELSZ DT_RELSZ
#define PLT_DT_RELENT DT_RELENT
#else
#define Elf_Plt_Rel Elf_Rela
#define PLT_DT_REL DT_RELA
#define PLT_DT_RELSZ DT_RELASZ
#define PLT_DT_RELENT DT_RELAENT
#endif
#if defined __LP64__
#ifndef ELF_CLASS
#define ELF_CLASS ELFCLASS64
#endif
#define SIZE_T_FMT "lu"
#define ELF_WORD_FMT "u"
#ifdef __ANDROID__
#define ELF_XWORD_FMT "llu"
#else
#define ELF_XWORD_FMT "lu"
#endif
#define ELF_SXWORD_FMT "ld"
#define Elf_Half Elf64_Half
#define Elf_Xword Elf64_Xword
#define Elf_Sxword Elf64_Sxword
#define Elf_Ehdr Elf64_Ehdr
#define Elf_Phdr Elf64_Phdr
#define Elf_Sym Elf64_Sym
#define Elf_Dyn Elf64_Dyn
#define Elf_Rel Elf64_Rel
#define Elf_Rela Elf64_Rela
#ifndef ELF_R_SYM
#define ELF_R_SYM ELF64_R_SYM
#endif
#ifndef ELF_R_TYPE
#define ELF_R_TYPE ELF64_R_TYPE
#endif
#else /* __LP64__ */
#ifndef ELF_CLASS
#define ELF_CLASS ELFCLASS32
#endif
#define SIZE_T_FMT "u"
#ifdef __sun
#define ELF_WORD_FMT "lu"
#define ELF_XWORD_FMT "lu"
#define ELF_SXWORD_FMT "ld"
#else
#define ELF_WORD_FMT "u"
#define ELF_XWORD_FMT "u"
#define ELF_SXWORD_FMT "d"
#endif
#define Elf_Half Elf32_Half
#define Elf_Xword Elf32_Word
#define Elf_Sxword Elf32_Sword
#define Elf_Ehdr Elf32_Ehdr
#define Elf_Phdr Elf32_Phdr
#define Elf_Sym Elf32_Sym
#define Elf_Dyn Elf32_Dyn
#define Elf_Rel Elf32_Rel
#define Elf_Rela Elf32_Rela
#ifndef ELF_R_SYM
#define ELF_R_SYM ELF32_R_SYM
#endif
#ifndef ELF_R_TYPE
#define ELF_R_TYPE ELF32_R_TYPE
#endif
#endif /* __LP64__ */
typedef struct mem_prot {
size_t start;
size_t end;
int prot;
} mem_prot_t;
#define NUM_MEM_PROT 20
struct plthook {
const Elf_Sym *dynsym;
const char *dynstr;
size_t dynstr_size;
const char *plt_addr_base;
const Elf_Plt_Rel *rela_plt;
size_t rela_plt_cnt;
#ifdef R_GLOBAL_DATA
const Elf_Plt_Rel *rela_dyn;
size_t rela_dyn_cnt;
#endif
mem_prot_t mem_prot[NUM_MEM_PROT];
};
static char errmsg[512];
static size_t page_size;
#define ALIGN_ADDR(addr) ((void*)((size_t)(addr) & ~(page_size - 1)))
static int plthook_open_executable(plthook_t **plthook_out);
static int plthook_open_shared_library(plthook_t **plthook_out, const char *filename);
static const Elf_Dyn *find_dyn_by_tag(const Elf_Dyn *dyn, Elf_Sxword tag);
typedef struct mem_prot_iter mem_prot_iter_t;
static int mem_prot_begin(mem_prot_iter_t *iter);
static int mem_prot_next(mem_prot_iter_t *iter, mem_prot_t *mem_prot);
static void mem_prot_end(mem_prot_iter_t *iter);
static int plthook_open_real(plthook_t **plthook_out, struct link_map *lmap);
static int plthook_set_mem_prot(plthook_t *plthook);
static int plthook_get_mem_prot(plthook_t *plthook, void *addr);
#if defined __FreeBSD__ || defined __sun
static int check_elf_header(const Elf_Ehdr *ehdr);
#endif
static void set_errmsg(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
#if defined __ANDROID__ || defined __UCLIBC__
struct dl_iterate_data {
char* addr;
struct link_map lmap;
};
static int dl_iterate_cb(struct dl_phdr_info *info, size_t size, void *cb_data)
{
struct dl_iterate_data *data = (struct dl_iterate_data*)cb_data;
Elf_Half idx = 0;
for (idx = 0; idx < info->dlpi_phnum; ++idx) {
const Elf_Phdr *phdr = &info->dlpi_phdr[idx];
char* base = (char*)info->dlpi_addr + phdr->p_vaddr;
if (base <= data->addr && data->addr < base + phdr->p_memsz) {
break;
}
}
if (idx == info->dlpi_phnum) {
return 0;
}
for (idx = 0; idx < info->dlpi_phnum; ++idx) {
const Elf_Phdr *phdr = &info->dlpi_phdr[idx];
if (phdr->p_type == PT_DYNAMIC) {
data->lmap.l_addr = info->dlpi_addr;
data->lmap.l_ld = (Elf_Dyn*)(info->dlpi_addr + phdr->p_vaddr);
return 1;
}
}
return 0;
}
#endif
int plthook_open(plthook_t **plthook_out, const char *filename)
{
*plthook_out = NULL;
if (filename == NULL) {
return plthook_open_executable(plthook_out);
} else {
return plthook_open_shared_library(plthook_out, filename);
}
}
int plthook_open_by_handle(plthook_t **plthook_out, void *hndl)
{
#if defined __ANDROID__ || defined __UCLIBC__
const static char *symbols[] = {
"__INIT_ARRAY__",
"_end",
"_start"
};
size_t i;
if (hndl == NULL) {
set_errmsg("NULL handle");
return PLTHOOK_FILE_NOT_FOUND;
}
for (i = 0; i < sizeof(symbols)/sizeof(symbols[0]); i++) {
char *addr = dlsym(hndl, symbols[i]);
if (addr != NULL) {
return plthook_open_by_address(plthook_out, addr - 1);
}
}
set_errmsg("Could not find an address in the specified handle.");
return PLTHOOK_INTERNAL_ERROR;
#else
struct link_map *lmap = NULL;
if (hndl == NULL) {
set_errmsg("NULL handle");
return PLTHOOK_FILE_NOT_FOUND;
}
if (dlinfo(hndl, RTLD_DI_LINKMAP, &lmap) != 0) {
set_errmsg("dlinfo error");
return PLTHOOK_FILE_NOT_FOUND;
}
return plthook_open_real(plthook_out, lmap);
#endif
}
int plthook_open_by_address(plthook_t **plthook_out, void *address)
{
#if defined __FreeBSD__
return PLTHOOK_NOT_IMPLEMENTED;
#elif defined __ANDROID__ || defined __UCLIBC__
struct dl_iterate_data data = {0,};
data.addr = address;
dl_iterate_phdr(dl_iterate_cb, &data);
if (data.lmap.l_ld == NULL) {
set_errmsg("Could not find memory region containing address %p", address);
return PLTHOOK_INTERNAL_ERROR;
}
return plthook_open_real(plthook_out, &data.lmap);
#else
Dl_info info;
struct link_map *lmap = NULL;
*plthook_out = NULL;
if (dladdr1(address, &info, (void**)&lmap, RTLD_DL_LINKMAP) == 0) {
set_errmsg("dladdr error");
return PLTHOOK_FILE_NOT_FOUND;
}
return plthook_open_real(plthook_out, lmap);
#endif
}
static int plthook_open_executable(plthook_t **plthook_out)
{
#if defined __ANDROID__ || defined __UCLIBC__
return plthook_open_shared_library(plthook_out, NULL);
#elif defined __linux__
return plthook_open_real(plthook_out, _r_debug.r_map);
#elif defined __sun
const char *auxv_file = "/proc/self/auxv";
#define NUM_AUXV_CNT 10
FILE *fp = fopen(auxv_file, "r");
auxv_t auxv;
struct r_debug *r_debug = NULL;
if (fp == NULL) {
set_errmsg("Could not open %s: %s", auxv_file,
strerror(errno));
return PLTHOOK_INTERNAL_ERROR;
}
while (fread(&auxv, sizeof(auxv_t), 1, fp) == 1) {
if (auxv.a_type == AT_SUN_LDDATA) {
r_debug = (struct r_debug *)auxv.a_un.a_ptr;
break;
}
}
fclose(fp);
if (r_debug == NULL) {
set_errmsg("Could not find r_debug");
return PLTHOOK_INTERNAL_ERROR;
}
return plthook_open_real(plthook_out, r_debug->r_map);
#elif defined __FreeBSD__
return plthook_open_shared_library(plthook_out, NULL);
#else
set_errmsg("Opening the main program is not supported on this platform.");
return PLTHOOK_NOT_IMPLEMENTED;
#endif
}
static int plthook_open_shared_library(plthook_t **plthook_out, const char *filename)
{
void *hndl = dlopen(filename, RTLD_LAZY | RTLD_NOLOAD);
#if defined __ANDROID__ || defined __UCLIBC__
int rv;
#else
struct link_map *lmap = NULL;
#endif
if (hndl == NULL) {
set_errmsg("dlopen error: %s", dlerror());
return PLTHOOK_FILE_NOT_FOUND;
}
#if defined __ANDROID__ || defined __UCLIBC__
rv = plthook_open_by_handle(plthook_out, hndl);
dlclose(hndl);
return rv;
#else
if (dlinfo(hndl, RTLD_DI_LINKMAP, &lmap) != 0) {
set_errmsg("dlinfo error");
dlclose(hndl);
return PLTHOOK_FILE_NOT_FOUND;
}
dlclose(hndl);
return plthook_open_real(plthook_out, lmap);
#endif
}
static const Elf_Dyn *find_dyn_by_tag(const Elf_Dyn *dyn, Elf_Sxword tag)
{
while (dyn->d_tag != DT_NULL) {
if (dyn->d_tag == tag) {
return dyn;
}
dyn++;
}
return NULL;
}
#ifdef __linux__
struct mem_prot_iter {
FILE *fp;
};
static int mem_prot_begin(mem_prot_iter_t *iter)
{
iter->fp = fopen("/proc/self/maps", "r");
if (iter->fp == NULL) {
set_errmsg("failed to open /proc/self/maps");
return -1;
}
return 0;
}
static int mem_prot_next(mem_prot_iter_t *iter, mem_prot_t *mem_prot)
{
char buf[PATH_MAX];
char perms[5];
int bol = 1; /* beginnng of line */
while (fgets(buf, PATH_MAX, iter->fp) != NULL) {
unsigned long start, end;
int eol = (strchr(buf, '\n') != NULL); /* end of line */
if (bol) {
/* The fgets reads from the beginning of a line. */
if (!eol) {
/* The next fgets reads from the middle of the same line. */
bol = 0;
}
} else {
/* The fgets reads from the middle of a line. */
if (eol) {
/* The next fgets reads from the beginning of a line. */
bol = 1;
}
continue;
}
if (sscanf(buf, "%lx-%lx %4s", &start, &end, perms) != 3) {
continue;
}
mem_prot->start = start;
mem_prot->end = end;
mem_prot->prot = 0;
if (perms[0] == 'r') {
mem_prot->prot |= PROT_READ;
}
if (perms[1] == 'w') {
mem_prot->prot |= PROT_WRITE;
}
if (perms[2] == 'x') {
mem_prot->prot |= PROT_EXEC;
}
return 0;
}
return -1;
}
static void mem_prot_end(mem_prot_iter_t *iter)
{
if (iter->fp != NULL) {
fclose(iter->fp);
}
}
#elif defined __FreeBSD__
struct mem_prot_iter {
struct kinfo_vmentry *kve;
int idx;
int num;
};
static int mem_prot_begin(mem_prot_iter_t *iter)
{
iter->kve = kinfo_getvmmap(getpid(), &iter->num);
if (iter->kve == NULL) {
set_errmsg("failed to call kinfo_getvmmap()\n");
return -1;
}
iter->idx = 0;
return 0;
}
static int mem_prot_next(mem_prot_iter_t *iter, mem_prot_t *mem_prot)
{
if (iter->idx >= iter->num) {
return -1;
}
struct kinfo_vmentry *kve = &iter->kve[iter->idx++];
mem_prot->start = kve->kve_start;
mem_prot->end = kve->kve_end;
mem_prot->prot = 0;
if (kve->kve_protection & KVME_PROT_READ) {
mem_prot->prot |= PROT_READ;
}
if (kve->kve_protection & KVME_PROT_WRITE) {
mem_prot->prot |= PROT_WRITE;
}
if (kve->kve_protection & KVME_PROT_EXEC) {
mem_prot->prot |= PROT_EXEC;
}
return 0;
}
static void mem_prot_end(mem_prot_iter_t *iter)
{
if (iter->kve != NULL) {
free(iter->kve);
}
}
#elif defined(__sun)
struct mem_prot_iter {
FILE *fp;
prmap_t maps[20];
size_t idx;
size_t num;
};
static int mem_prot_begin(mem_prot_iter_t *iter)
{
iter->fp = fopen("/proc/self/map", "r");
if (iter->fp == NULL) {
set_errmsg("failed to open /proc/self/map");
return -1;
}
iter->idx = iter->num = 0;
return 0;
}
static int mem_prot_next(mem_prot_iter_t *iter, mem_prot_t *mem_prot)
{
prmap_t *map;
if (iter->idx == iter->num) {
iter->num = fread(iter->maps, sizeof(iter->maps[0]), sizeof(iter->maps) / sizeof(iter->maps[0]), iter->fp);
if (iter->num == 0) {
return -1;
}
iter->idx = 0;
}
map = &iter->maps[iter->idx++];
mem_prot->start = map->pr_vaddr;
mem_prot->end = map->pr_vaddr + map->pr_size;
mem_prot->prot = 0;
if (map->pr_mflags & MA_READ) {
mem_prot->prot |= PROT_READ;
}
if (map->pr_mflags & MA_WRITE) {
mem_prot->prot |= PROT_WRITE;
}
if (map->pr_mflags & MA_EXEC) {
mem_prot->prot |= PROT_EXEC;
}
return 0;
}
static void mem_prot_end(mem_prot_iter_t *iter)
{
if (iter->fp != NULL) {
fclose(iter->fp);
}
}
#else
#error Unsupported platform
#endif
static int plthook_open_real(plthook_t **plthook_out, struct link_map *lmap)
{
plthook_t plthook = {NULL,};
const Elf_Dyn *dyn;
const char *dyn_addr_base = NULL;
if (page_size == 0) {
page_size = sysconf(_SC_PAGESIZE);
}
#if defined __linux__
plthook.plt_addr_base = (char*)lmap->l_addr;
#if defined __riscv
const Elf_Ehdr *ehdr = (const Elf_Ehdr*)lmap->l_addr;
if (ehdr->e_type == ET_DYN) {
dyn_addr_base = (const char*)lmap->l_addr;
}
#endif
#if defined __ANDROID__ || defined __UCLIBC__
dyn_addr_base = (const char*)lmap->l_addr;
#endif
#elif defined __FreeBSD__ || defined __sun
#if __FreeBSD__ >= 13
const Elf_Ehdr *ehdr = (const Elf_Ehdr*)lmap->l_base;
#else
const Elf_Ehdr *ehdr = (const Elf_Ehdr*)lmap->l_addr;
#endif
int rv_ = check_elf_header(ehdr);
if (rv_ != 0) {
return rv_;
}
if (ehdr->e_type == ET_DYN) {
dyn_addr_base = (const char*)lmap->l_addr;
plthook.plt_addr_base = (const char*)lmap->l_addr;
}
#else
#error unsupported OS
#endif
/* get .dynsym section */
dyn = find_dyn_by_tag(lmap->l_ld, DT_SYMTAB);
if (dyn == NULL) {
set_errmsg("failed to find DT_SYMTAB");
return PLTHOOK_INTERNAL_ERROR;
}
plthook.dynsym = (const Elf_Sym*)(dyn_addr_base + dyn->d_un.d_ptr);
/* Check sizeof(Elf_Sym) */
dyn = find_dyn_by_tag(lmap->l_ld, DT_SYMENT);
if (dyn == NULL) {
set_errmsg("failed to find DT_SYMTAB");
return PLTHOOK_INTERNAL_ERROR;
}
if (dyn->d_un.d_val != sizeof(Elf_Sym)) {
set_errmsg("DT_SYMENT size %" ELF_XWORD_FMT " != %" SIZE_T_FMT, dyn->d_un.d_val, sizeof(Elf_Sym));
return PLTHOOK_INTERNAL_ERROR;
}
/* get .dynstr section */
dyn = find_dyn_by_tag(lmap->l_ld, DT_STRTAB);
if (dyn == NULL) {
set_errmsg("failed to find DT_STRTAB");
return PLTHOOK_INTERNAL_ERROR;
}
plthook.dynstr = dyn_addr_base + dyn->d_un.d_ptr;
/* get .dynstr size */
dyn = find_dyn_by_tag(lmap->l_ld, DT_STRSZ);
if (dyn == NULL) {
set_errmsg("failed to find DT_STRSZ");
return PLTHOOK_INTERNAL_ERROR;
}
plthook.dynstr_size = dyn->d_un.d_val;
/* get .rela.plt or .rel.plt section */
dyn = find_dyn_by_tag(lmap->l_ld, DT_JMPREL);
if (dyn != NULL) {
plthook.rela_plt = (const Elf_Plt_Rel *)(dyn_addr_base + dyn->d_un.d_ptr);
dyn = find_dyn_by_tag(lmap->l_ld, DT_PLTRELSZ);
if (dyn == NULL) {
set_errmsg("failed to find DT_PLTRELSZ");
return PLTHOOK_INTERNAL_ERROR;
}
plthook.rela_plt_cnt = dyn->d_un.d_val / sizeof(Elf_Plt_Rel);
}
#ifdef R_GLOBAL_DATA
/* get .rela.dyn or .rel.dyn section */
dyn = find_dyn_by_tag(lmap->l_ld, PLT_DT_REL);
if (dyn != NULL) {
size_t total_size, elem_size;
plthook.rela_dyn = (const Elf_Plt_Rel *)(dyn_addr_base + dyn->d_un.d_ptr);
dyn = find_dyn_by_tag(lmap->l_ld, PLT_DT_RELSZ);
if (dyn == NULL) {
set_errmsg("failed to find PLT_DT_RELSZ");
return PLTHOOK_INTERNAL_ERROR;
}
total_size = dyn->d_un.d_ptr;
dyn = find_dyn_by_tag(lmap->l_ld, PLT_DT_RELENT);
if (dyn == NULL) {
set_errmsg("failed to find PLT_DT_RELENT");
return PLTHOOK_INTERNAL_ERROR;
}
elem_size = dyn->d_un.d_ptr;
plthook.rela_dyn_cnt = total_size / elem_size;
}
#endif
#ifdef R_GLOBAL_DATA
if (plthook.rela_plt == NULL && plthook.rela_dyn == NULL) {
set_errmsg("failed to find either of DT_JMPREL and DT_REL");
return PLTHOOK_INTERNAL_ERROR;
}
#else
if (plthook.rela_plt == NULL) {
set_errmsg("failed to find DT_JMPREL");
return PLTHOOK_INTERNAL_ERROR;
}
#endif
if (plthook_set_mem_prot(&plthook)) {
return PLTHOOK_INTERNAL_ERROR;
}
*plthook_out = malloc(sizeof(plthook_t));
if (*plthook_out == NULL) {
set_errmsg("failed to allocate memory: %" SIZE_T_FMT " bytes", sizeof(plthook_t));
return PLTHOOK_OUT_OF_MEMORY;
}
**plthook_out = plthook;
return 0;
}
static int plthook_set_mem_prot(plthook_t *plthook)
{
unsigned int pos = 0;
const char *name;
void **addr;
size_t start = (size_t)-1;
size_t end = 0;
mem_prot_iter_t iter;
mem_prot_t mem_prot;
int idx = 0;
while (plthook_enum(plthook, &pos, &name, &addr) == 0) {
if (start > (size_t)addr) {
start = (size_t)addr;
}
if (end < (size_t)addr) {
end = (size_t)addr;
}
}
end++;
if (mem_prot_begin(&iter) != 0) {
return PLTHOOK_INTERNAL_ERROR;
}
while (mem_prot_next(&iter, &mem_prot) == 0 && idx < NUM_MEM_PROT) {
if (mem_prot.prot != 0 && mem_prot.start < end && start < mem_prot.end) {
plthook->mem_prot[idx++] = mem_prot;
}
}
mem_prot_end(&iter);
return 0;
}
static int plthook_get_mem_prot(plthook_t *plthook, void *addr)
{
mem_prot_t *ptr = plthook->mem_prot;
mem_prot_t *end = ptr + NUM_MEM_PROT;
while (ptr < end && ptr->prot != 0) {
if (ptr->start <= (size_t)addr && (size_t)addr < ptr->end) {
return ptr->prot;
}
++ptr;
}
return 0;
}
#if defined __FreeBSD__ || defined __sun
static int check_elf_header(const Elf_Ehdr *ehdr)
{
static const unsigned short s = 1;
/* Check endianness at runtime. */
unsigned char elfdata = (*(const char*)&s) ? ELFDATA2LSB : ELFDATA2MSB;
if (ehdr == NULL) {
set_errmsg("invalid elf header address: NULL");
return PLTHOOK_INTERNAL_ERROR;
}
if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
set_errmsg("invalid file signature: 0x%02x,0x%02x,0x%02x,0x%02x",
ehdr->e_ident[0], ehdr->e_ident[1], ehdr->e_ident[2], ehdr->e_ident[3]);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (ehdr->e_ident[EI_CLASS] != ELF_CLASS) {
set_errmsg("invalid elf class: 0x%02x", ehdr->e_ident[EI_CLASS]);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (ehdr->e_ident[EI_DATA] != elfdata) {
set_errmsg("invalid elf data: 0x%02x", ehdr->e_ident[EI_DATA]);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
set_errmsg("invalid elf version: 0x%02x", ehdr->e_ident[EI_VERSION]);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
set_errmsg("invalid file type: 0x%04x", ehdr->e_type);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (ehdr->e_version != EV_CURRENT) {
set_errmsg("invalid object file version: %" ELF_WORD_FMT, ehdr->e_version);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (ehdr->e_ehsize != sizeof(Elf_Ehdr)) {
set_errmsg("invalid elf header size: %u", ehdr->e_ehsize);
return PLTHOOK_INVALID_FILE_FORMAT;
}
if (ehdr->e_phentsize != sizeof(Elf_Phdr)) {
set_errmsg("invalid program header table entry size: %u", ehdr->e_phentsize);
return PLTHOOK_INVALID_FILE_FORMAT;
}
return 0;
}
#endif
static int check_rel(const plthook_t *plthook, const Elf_Plt_Rel *plt, Elf_Xword r_type, const char **name_out, void ***addr_out)
{
if (ELF_R_TYPE(plt->r_info) == r_type) {
size_t idx = ELF_R_SYM(plt->r_info);
idx = plthook->dynsym[idx].st_name;
if (idx + 1 > plthook->dynstr_size) {
set_errmsg("too big section header string table index: %" SIZE_T_FMT, idx);
return PLTHOOK_INVALID_FILE_FORMAT;
}
*name_out = plthook->dynstr + idx;
*addr_out = (void**)(plthook->plt_addr_base + plt->r_offset);
return 0;
}
return -1;
}
int plthook_enum(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out)
{
return plthook_enum_with_prot(plthook, pos, name_out, addr_out, NULL);
}
int plthook_enum_with_prot(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out, int *prot)
{
while (*pos < plthook->rela_plt_cnt) {
const Elf_Plt_Rel *plt = plthook->rela_plt + *pos;
int rv = check_rel(plthook, plt, R_JUMP_SLOT, name_out, addr_out);
(*pos)++;
if (rv >= 0) {
if (rv == 0 && prot != NULL) {
*prot = plthook_get_mem_prot(plthook, *addr_out);
}
return rv;
}
}
#ifdef R_GLOBAL_DATA
while (*pos < plthook->rela_plt_cnt + plthook->rela_dyn_cnt) {
const Elf_Plt_Rel *plt = plthook->rela_dyn + (*pos - plthook->rela_plt_cnt);
int rv = check_rel(plthook, plt, R_GLOBAL_DATA, name_out, addr_out);
(*pos)++;
if (rv >= 0) {
if (rv == 0 && prot != NULL) {
*prot = plthook_get_mem_prot(plthook, *addr_out);
}
return rv;
}
}
#endif
*name_out = NULL;
*addr_out = NULL;
return EOF;
}
int plthook_replace(plthook_t *plthook, const char *funcname, void *funcaddr, void **oldfunc)
{
size_t funcnamelen = strlen(funcname);
unsigned int pos = 0;
const char *name;
void **addr;
int rv;
if (plthook == NULL) {
set_errmsg("invalid argument: The first argument is null.");
return PLTHOOK_INVALID_ARGUMENT;
}
while ((rv = plthook_enum(plthook, &pos, &name, &addr)) == 0) {
if (strncmp(name, funcname, funcnamelen) == 0) {
if (name[funcnamelen] == '\0' || name[funcnamelen] == '@') {
int prot = plthook_get_mem_prot(plthook, addr);
if (prot == 0) {
set_errmsg("Could not get the process memory permission at %p",
ALIGN_ADDR(addr));
return PLTHOOK_INTERNAL_ERROR;
}
if (!(prot & PROT_WRITE)) {
if (mprotect(ALIGN_ADDR(addr), page_size, PROT_READ | PROT_WRITE) != 0) {
set_errmsg("Could not change the process memory permission at %p: %s",
ALIGN_ADDR(addr), strerror(errno));
return PLTHOOK_INTERNAL_ERROR;
}
}
if (oldfunc) {
*oldfunc = *addr;
}
*addr = funcaddr;
if (!(prot & PROT_WRITE)) {
mprotect(ALIGN_ADDR(addr), page_size, prot);
}
return 0;
}
}
}
if (rv == EOF) {
set_errmsg("no such function: %s", funcname);
rv = PLTHOOK_FUNCTION_NOT_FOUND;
}
return rv;
}
void plthook_close(plthook_t *plthook)
{
if (plthook != NULL) {
free(plthook);
}
}
const char *plthook_error(void)
{
return errmsg;
}
static void set_errmsg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(errmsg, sizeof(errmsg) - 1, fmt, ap);
va_end(ap);
}