-
Notifications
You must be signed in to change notification settings - Fork 237
/
gumandroid.c
1777 lines (1429 loc) · 42.3 KB
/
gumandroid.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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010-2024 Ole André Vadla Ravnås <oleavr@nowsecure.com>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
#include "gum/gumandroid.h"
#include "gum-init.h"
#include "gum/gumlinux.h"
#include <dlfcn.h>
#include <elf.h>
#include <link.h>
#include <pthread.h>
#include <string.h>
#include <sys/system_properties.h>
#if (defined (HAVE_I386) && GLIB_SIZEOF_VOID_P == 4) || defined (HAVE_ARM)
# define GUM_ANDROID_LEGACY_SOINFO 1
#endif
#define GUM_ANDROID_VDSO_MODULE_NAME "linux-vdso.so.1"
#if GLIB_SIZEOF_VOID_P == 4
# define GUM_LIBCXX_TINY_STRING_CAPACITY 11
#else
# define GUM_LIBCXX_TINY_STRING_CAPACITY 23
#endif
typedef struct _GumGetModuleHandleContext GumGetModuleHandleContext;
typedef struct _GumEnsureModuleInitializedContext
GumEnsureModuleInitializedContext;
typedef struct _GumEnumerateModulesContext GumEnumerateModulesContext;
typedef struct _GumSoinfoDetails GumSoinfoDetails;
typedef gboolean (* GumFoundSoinfoFunc) (const GumSoinfoDetails * details,
gpointer user_data);
typedef struct _GumLinkerApi GumLinkerApi;
typedef struct _GumSoinfo GumSoinfo;
typedef struct _GumSoinfoHead GumSoinfoHead;
typedef struct _GumSoinfoBody GumSoinfoBody;
typedef struct _GumSoinfoExtrasPre33 GumSoinfoExtrasPre33;
typedef struct _GumSoinfoExtrasPost33 GumSoinfoExtrasPost33;
typedef struct _GumSoinfoModern GumSoinfoModern;
typedef struct _GumSoinfoLegacy23 GumSoinfoLegacy23;
typedef struct _GumSoinfoLegacy GumSoinfoLegacy;
typedef guint32 GumSoinfoFlags;
typedef struct _GumSoinfoListModern GumSoinfoListModern;
typedef struct _GumSoinfoListLegacy GumSoinfoListLegacy;
typedef struct _GumSoinfoListHeader GumSoinfoListHeader;
typedef struct _GumSoinfoListEntry GumSoinfoListEntry;
typedef struct _GumFindDlopenApiContext GumFindDlopenApiContext;
typedef struct _GumFindDlMutexContext GumFindDlMutexContext;
typedef struct _GumFindFunctionSignatureContext GumFindFunctionSignatureContext;
typedef struct _GumFunctionSignature GumFunctionSignature;
typedef union _GumLibcxxString GumLibcxxString;
typedef struct _GumLibcxxTinyString GumLibcxxTinyString;
typedef struct _GumLibcxxHugeString GumLibcxxHugeString;
struct _GumGetModuleHandleContext
{
const gchar * name;
void * module;
};
struct _GumEnsureModuleInitializedContext
{
const gchar * name;
gboolean success;
};
struct _GumEnumerateModulesContext
{
GumFoundModuleFunc func;
gpointer user_data;
};
struct _GumSoinfoDetails
{
const gchar * path;
GumSoinfo * si;
GumSoinfoBody * body;
GumLinkerApi * api;
};
struct _GumLinkerApi
{
GumAndroidDlopenImpl dlopen;
GumAndroidDlsymImpl dlsym;
gpointer trusted_caller;
void * (* do_dlopen) (const char * filename, int flags, const void * extinfo,
void * caller_addr);
guint8 (* do_dlsym) (void * handle, const char * sym_name,
const char * sym_ver, void * caller_addr, void ** symbol);
pthread_mutex_t * dl_mutex;
GumSoinfo * (* solist_get_head) (void);
GumSoinfo ** solist;
GumSoinfo * libdl_info;
GumSoinfo * (* solist_get_somain) (void);
GumSoinfo ** somain;
GumSoinfo * somain_node;
const char * (* soinfo_get_path) (GumSoinfo * si);
};
struct _GumSoinfoListModern
{
GumSoinfoListHeader * header;
};
struct _GumSoinfoListLegacy
{
GumSoinfoListEntry * head;
GumSoinfoListEntry * tail;
};
struct _GumSoinfoListHeader
{
GumSoinfoListEntry * head;
GumSoinfoListEntry * tail;
};
struct _GumSoinfoListEntry
{
GumSoinfoListEntry * next;
GumSoinfo * element;
};
struct _GumLibcxxTinyString
{
guint8 size;
gchar data[GUM_LIBCXX_TINY_STRING_CAPACITY];
};
struct _GumLibcxxHugeString
{
gsize capacity;
gsize size;
gchar * data;
};
union _GumLibcxxString
{
GumLibcxxTinyString tiny;
GumLibcxxHugeString huge;
};
struct _GumSoinfoHead
{
#ifdef GUM_ANDROID_LEGACY_SOINFO
gchar old_name[128];
#endif
const ElfW(Phdr) * phdr;
gsize phnum;
};
struct _GumSoinfoExtrasPre33
{
GumSoinfoListLegacy children;
GumSoinfoListLegacy parents;
/* version >= 1 */
off64_t file_offset;
guint32 rtld_flags;
guint32 dt_flags_1;
gsize strtab_size;
/* version >= 2 */
gsize gnu_nbucket;
guint32 * gnu_bucket;
guint32 * gnu_chain;
guint32 gnu_maskwords;
guint32 gnu_shift2;
ElfW(Addr) * gnu_bloom_filter;
GumSoinfo * local_group_root;
guint8 * android_relocs;
gsize android_relocs_size;
const gchar * soname;
GumLibcxxString realpath;
const ElfW(Versym) * versym;
ElfW(Addr) verdef_ptr;
gsize verdef_cnt;
ElfW(Addr) verneed_ptr;
gsize verneed_cnt;
gint target_sdk_version;
/* For now we don't need anything from version >= 3. */
};
struct _GumSoinfoExtrasPost33
{
GumSoinfoListModern children;
GumSoinfoListModern parents;
off64_t file_offset;
guint32 rtld_flags;
guint32 dt_flags_1;
gsize strtab_size;
gsize gnu_nbucket;
guint32 * gnu_bucket;
guint32 * gnu_chain;
guint32 gnu_maskwords;
guint32 gnu_shift2;
ElfW(Addr) * gnu_bloom_filter;
GumSoinfo * local_group_root;
guint8 * android_relocs;
gsize android_relocs_size;
GumLibcxxString soname;
GumLibcxxString realpath;
const ElfW(Versym) * versym;
ElfW(Addr) verdef_ptr;
gsize verdef_cnt;
ElfW(Addr) verneed_ptr;
gsize verneed_cnt;
gint target_sdk_version;
};
struct _GumSoinfoBody
{
#ifdef GUM_ANDROID_LEGACY_SOINFO
ElfW(Addr) unused0;
#endif
ElfW(Addr) base;
gsize size;
#ifdef GUM_ANDROID_LEGACY_SOINFO
guint32 unused1;
#endif
ElfW(Dyn) * dynamic;
#ifdef GUM_ANDROID_LEGACY_SOINFO
guint32 unused2;
guint32 unused3;
#endif
GumSoinfo * next;
GumSoinfoFlags flags;
const gchar * strtab;
ElfW(Sym) * symtab;
gsize nbucket;
gsize nchain;
guint32 * bucket;
guint32 * chain;
#if GLIB_SIZEOF_VOID_P == 4
ElfW(Addr) ** plt_got;
#endif
gpointer plt_relx;
gsize plt_relx_count;
gpointer relx;
gsize relx_count;
gpointer * preinit_array;
gsize preinit_array_count;
gpointer * init_array;
gsize init_array_count;
gpointer * fini_array;
gsize fini_array_count;
gpointer init_func;
gpointer fini_func;
#if defined (HAVE_ARM)
guint32 * arm_exidx;
gsize arm_exidx_count;
#elif defined (HAVE_MIPS)
guint32 mips_symtabno;
guint32 mips_local_gotno;
guint32 mips_gotsym;
#endif
gsize ref_count;
struct link_map link_map_head;
guint8 constructors_called;
ElfW(Addr) load_bias;
#if GLIB_SIZEOF_VOID_P == 4
guint8 has_text_relocations;
#endif
guint8 has_dt_symbolic;
/* Next part of structure only present when NEW_FORMAT is in flags. */
guint32 version;
/* version >= 0 */
dev_t st_dev;
ino_t st_ino;
union
{
GumSoinfoExtrasPre33 pre33;
GumSoinfoExtrasPost33 post33;
}
extras;
};
struct _GumSoinfoModern
{
GumSoinfoHead head;
GumSoinfoBody body;
};
struct _GumSoinfoLegacy23
{
GumSoinfoHead head;
#ifndef GUM_ANDROID_LEGACY_SOINFO
ElfW(Addr) entry;
#endif
GumSoinfoBody body;
};
struct _GumSoinfoLegacy
{
#ifndef GUM_ANDROID_LEGACY_SOINFO
gchar name[128];
#endif
GumSoinfoLegacy23 legacy23;
};
struct _GumSoinfo
{
union
{
GumSoinfoModern modern;
GumSoinfoLegacy23 legacy23;
GumSoinfoLegacy legacy;
};
};
enum _GumSoinfoFlags
{
GUM_SOINFO_LINKED = 0x00000001,
GUM_SOINFO_EXE = 0x00000004,
GUM_SOINFO_GNU_HASH = 0x00000040,
GUM_SOINFO_NEW_FORMAT = 0x40000000,
};
struct _GumFindDlopenApiContext
{
GumElfModule * linker;
const GumFunctionSignature * dlopen_signatures;
gpointer dlopen;
const GumFunctionSignature * dlsym_signatures;
gpointer dlsym;
};
struct _GumFindDlMutexContext
{
GumElfModule * linker;
pthread_mutex_t * dl_mutex;
};
struct _GumFindFunctionSignatureContext
{
GumAddress match;
guint num_matches;
};
struct _GumFunctionSignature
{
const gchar * signature;
gint displacement;
};
static const GumModuleDetails * gum_try_init_linker_details (void);
static void gum_deinit_linker_details (void);
static gchar * gum_find_linker_path (void);
static GRegex * gum_find_linker_path_pattern (void);
static gboolean gum_try_parse_linker_proc_maps_line (const gchar * line,
const gchar * linker_path, const GRegex * linker_path_pattern,
GumModuleDetails * module, GumMemoryRange * range);
static gboolean gum_store_module_handle_if_name_matches (
const GumSoinfoDetails * details, GumGetModuleHandleContext * ctx);
static gboolean gum_emit_module_from_soinfo (const GumSoinfoDetails * details,
GumEnumerateModulesContext * ctx);
static void gum_enumerate_soinfo (GumFoundSoinfoFunc func, gpointer user_data);
static void gum_init_soinfo_details (GumSoinfoDetails * details, GumSoinfo * si,
GumLinkerApi * api, GHashTable ** ranges);
static const gchar * gum_resolve_soinfo_path (GumSoinfo * si,
GumLinkerApi * api, GHashTable ** ranges);
static GumLinkerApi * gum_linker_api_get (void);
static GumLinkerApi * gum_linker_api_try_init (void);
static gboolean gum_store_linker_symbol_if_needed (
const GumElfSymbolDetails * details, guint * pending);
static gboolean gum_try_find_dlopen_api245_forensically (GumElfModule * linker,
GumLinkerApi * api);
static gboolean gum_try_find_dlopen_api26p_forensically (GumElfModule * linker,
GumLinkerApi * api);
static gboolean gum_store_dlopen_api_if_found_in_section (
const GumElfSectionDetails * details, GumFindDlopenApiContext * ctx);
static gboolean gum_try_find_dl_mutex_forensically (GumElfModule * linker,
pthread_mutex_t ** dl_mutex);
static gboolean gum_store_dl_mutex_pointer_if_found_in_section (
const GumElfSectionDetails * details, GumFindDlMutexContext * ctx);
static gboolean gum_try_find_libdl_info_forensically (GumElfModule * linker,
GumSoinfo ** libdl_info);
static gboolean gum_store_libdl_info_pointer_if_found_in_section (
const GumElfSectionDetails * details, GumSoinfo ** libdl_info);
static gboolean gum_try_find_somain_forensically (GumLinkerApi * api);
static gpointer gum_find_function_by_signature (GumAddress address, gsize size,
const GumFunctionSignature * signatures);
static gboolean gum_store_function_signature_match (GumAddress address,
gsize size, GumFindFunctionSignatureContext * ctx);
static gboolean gum_store_first_scan_match (GumAddress address, gsize size,
gpointer user_data);
static GumSoinfo * gum_solist_get_head_fallback (void);
static GumSoinfo * gum_solist_get_somain_fallback (void);
#ifdef GUM_ANDROID_LEGACY_SOINFO
static GumSoinfoHead * gum_soinfo_get_head (GumSoinfo * self);
#endif
static GumSoinfoBody * gum_soinfo_get_body (GumSoinfo * self);
static gboolean gum_soinfo_is_linker (GumSoinfo * self);
static GumSoinfo * gum_soinfo_get_parent (GumSoinfo * self);
static guint32 gum_soinfo_get_rtld_flags (GumSoinfo * self);
static const gchar * gum_soinfo_get_realpath (GumSoinfo * self);
static const char * gum_soinfo_get_path_fallback (GumSoinfo * self);
static void * gum_call_inner_dlopen (const char * filename, int flags);
static void * gum_call_inner_dlsym (void * handle, const char * symbol);
static const char * gum_libcxx_string_get_data (const GumLibcxxString * self);
static gboolean gum_android_is_vdso_module_name (const gchar * name);
static gchar * gum_dl_path;
static GumModuleDetails gum_dl_module;
static GumMemoryRange gum_dl_range;
static GumLinkerApi gum_dl_api;
static const gchar * gum_magic_linker_export_names_pre_api_level_26[] =
{
"dlopen",
"dlsym",
"dlclose",
"dlerror",
NULL
};
static const gchar * gum_magic_linker_export_names_post_api_level_26[] =
{
NULL
};
/*
* The following signatures have been tested on:
*
* - Xiaomi iRedmi Note 3 running LineageOS 14.1 (Android 7.1.2)
*/
static const GumFunctionSignature gum_dlopen_signatures_api245[] =
{
#ifdef HAVE_ARM
{
"93 46 " /* mov r11, r2 */
"0c 46 " /* mov r4, r1 */
"78 44 " /* add r0, pc */
"05 68", /* ldr r5, [r0] */
-12 + 1
},
#endif
#ifdef HAVE_ARM64
{
"f4 4f 04 a9 " /* stp x20, x19, [sp, #0x40] */
"fd 7b 05 a9 " /* stp x29, x30, [sp, #0x50] */
"fd 43 01 91 " /* add x29, sp, #0x50 */
"ff c3 04 d1 " /* sub sp, sp, #0x130 */
"?? ?? ?? ?? " /* adrp x8, #0xb1000 */
"?? ?? ?? ?? " /* ldr x21, [x8, #0x688] */
"f4 03 02 aa", /* mov x20, x2 */
-16
},
#endif
{ NULL, 0 }
};
static const GumFunctionSignature gum_dlsym_signatures_api245[] =
{
#ifdef HAVE_ARM
{
"14 46 " /* mov r4, r2 */
"88 46 " /* mov r8, r1 */
"?? ?? " /* cbz r6, loc_52a6 */
"b8 f1 00 0f", /* cmp.w r8, #0 */
-8 + 1
},
#endif
#ifdef HAVE_ARM64
{
"ff c3 01 d1 " /* sub sp, sp, #0x70 */
"f3 03 04 aa " /* mov x19, x4 */
"f4 03 02 aa " /* mov x20, x2 */
"f5 03 01 aa " /* mov x21, x1 */
"e8 03 00 aa", /* mov x8, x0 */
-16
},
#endif
{ NULL, 0 }
};
/*
* The following signatures have been tested on:
*
* - Pixel 3 running Android 9.0
*/
static const GumFunctionSignature gum_dlopen_signatures_api26p[] =
{
#ifdef HAVE_ARM
{
"0d 46 " /* mov r5, r1 */
"78 44 " /* add r0, pc */
"?? ?? ?? ?? " /* bl __dl_pthread_mutex_lock */
"?? ?? " /* ldr r0, =0xbd62a */
"78 44", /* add r0, pc */
-8 + 1
},
#endif
#ifdef HAVE_ARM64
{
"f3 03 02 aa " /* mov x19, x2 */
"f4 03 01 2a " /* mov w20, w1 */
"f5 03 00 aa " /* mov x21, x0 */
"?? ?? ?? ?? " /* adrp x0, #0x150000 */
"?? ?? ?? ?? " /* add x0, x0, #0 */
"?? ?? ?? ?4 " /* bl __dl_pthread_mutex_lock */
"?? ?? ?? ?? " /* adrp x0, #0x14f000 */
"?? ?? ?? ?? " /* ldr x0, [x0, #0x840] */
"?? ?? ?? ?4 " /* bl __dl__ZN12LinkerLogger10ResetStateEv */
"e0 03 15 aa ", /* mov x0, x21 */
-16
},
#endif
{ NULL, 0 }
};
static const GumFunctionSignature gum_dlsym_signatures_api26p[] =
{
#ifdef HAVE_ARM
{
"1c 46 " /* mov r4, r3 */
"15 46 " /* mov r5, r2 */
"0e 46 " /* mov r6, r1 */
"78 44 " /* add r0, pc */
"?? ?? ?? ?? " /* bl __dl_pthread_mutex_lock */
"?? ?? " /* ldr r0, =0xbd5ce */
"78 44 " /* add r0, pc */
"00 68 " /* ldr r0, [r0] */
"?? ?? ?? ?? " /* bl __dl__ZN12LinkerLogger10ResetStateEv */
"02 a8", /* add r0, sp, #8 */
-8 + 1
},
#endif
#ifdef HAVE_ARM64
{
"fd c3 00 91 " /* add x29, sp, #0x30 */
"f3 03 03 aa " /* mov x19, x3 */
"f4 03 02 aa " /* mov x20, x2 */
"f5 03 01 aa " /* mov x21, x1 */
"f6 03 00 aa " /* mov x22, x0 */
"?? ?? ?? ?? " /* adrp x0, #0x150000 */
"?? ?? ?? ?? " /* add x0, x0, #0 */
"?? ?? ?? ?4 " /* bl __dl_pthread_mutex_lock */
"?? ?? ?? ?? " /* adrp x0, #0x14f000 */
"?? ?? ?? ?? " /* ldr x0, [x0, #0x840] */
"?? ?? ?? ?4 " /* bl __dl__ZN12LinkerLogger10ResetStateEv */
"e4 23 00 91", /* add x4, sp, #8 */
-16
},
#endif
{ NULL, 0 }
};
GumAndroidLinkerFlavor
gum_android_get_linker_flavor (void)
{
#if defined (HAVE_ARM) || defined (HAVE_ARM64)
static GumAndroidLinkerFlavor cached_flavor = -1;
if (cached_flavor == -1)
{
gchar * info = NULL;
g_file_get_contents ("/sys/devices/system/cpu/modalias", &info, NULL, NULL);
cached_flavor = (info != NULL && strstr (info, "x86") != NULL)
? GUM_ANDROID_LINKER_EMULATED
: GUM_ANDROID_LINKER_NATIVE;
g_free (info);
}
return cached_flavor;
#else
return GUM_ANDROID_LINKER_NATIVE;
#endif
}
guint
gum_android_get_api_level (void)
{
static guint cached_api_level = G_MAXUINT;
if (cached_api_level == G_MAXUINT)
{
gchar sdk_version[PROP_VALUE_MAX];
__system_property_get ("ro.build.version.sdk", sdk_version);
cached_api_level = atoi (sdk_version);
}
return cached_api_level;
}
static gboolean
gum_android_is_api33_or_newer (void)
{
static gboolean is_api33_or_newer;
static gboolean initialized = FALSE;
if (!initialized)
{
if (gum_android_get_api_level () >= 33)
{
is_api33_or_newer = TRUE;
}
else
{
gchar codename[PROP_VALUE_MAX];
__system_property_get ("ro.build.version.codename", codename);
is_api33_or_newer = strcmp (codename, "Tiramisu") == 0;
}
initialized = TRUE;
}
return is_api33_or_newer;
}
gboolean
gum_android_is_linker_module_name (const gchar * name)
{
const GumModuleDetails * linker;
linker = gum_android_get_linker_module_details ();
if (name[0] != '/')
return strcmp (name, linker->name) == 0;
return strcmp (name, linker->path) == 0;
}
const GumModuleDetails *
gum_android_get_linker_module_details (void)
{
static GOnce once = G_ONCE_INIT;
g_once (&once, (GThreadFunc) gum_try_init_linker_details, NULL);
if (once.retval == NULL)
gum_panic ("Unable to locate the Android linker; please file a bug");
return once.retval;
}
static const GumModuleDetails *
gum_try_init_linker_details (void)
{
const GumModuleDetails * result = NULL;
gchar * linker_path;
GRegex * linker_path_pattern;
gchar * maps, ** lines;
gint num_lines, vdso_index, i;
linker_path = gum_find_linker_path ();
linker_path_pattern = gum_find_linker_path_pattern ();
/*
* Using /proc/self/maps means there might be false positives, as the
* application – or even Frida itself – may have mmap()ed the module.
*
* Knowing that the linker is mapped right around the vdso, with no
* empty space between, we just have to find the vdso, and we can
* count on the the next or previous linker mapping being the actual
* linker.
*/
g_file_get_contents ("/proc/self/maps", &maps, NULL, NULL);
lines = g_strsplit (maps, "\n", 0);
num_lines = g_strv_length (lines);
vdso_index = -1;
for (i = 0; i != num_lines; i++)
{
const gchar * line = lines[i];
if (g_str_has_suffix (line, " [vdso]"))
{
vdso_index = i;
break;
}
}
if (vdso_index == -1)
goto no_vdso;
for (i = vdso_index + 1; i != num_lines; i++)
{
if (gum_try_parse_linker_proc_maps_line (lines[i], linker_path,
linker_path_pattern, &gum_dl_module, &gum_dl_range))
{
result = &gum_dl_module;
goto beach;
}
}
for (i = vdso_index - 1; i >= 0; i--)
{
if (gum_try_parse_linker_proc_maps_line (lines[i], linker_path,
linker_path_pattern, &gum_dl_module, &gum_dl_range))
{
result = &gum_dl_module;
goto beach;
}
}
goto beach;
no_vdso:
for (i = num_lines - 1; i >= 0; i--)
{
if (gum_try_parse_linker_proc_maps_line (lines[i], linker_path,
linker_path_pattern, &gum_dl_module, &gum_dl_range))
{
result = &gum_dl_module;
goto beach;
}
}
beach:
if (result != NULL)
{
gum_dl_path = linker_path;
_gum_register_destructor (gum_deinit_linker_details);
}
else
{
g_free (linker_path);
}
g_strfreev (lines);
g_free (maps);
g_regex_unref (linker_path_pattern);
return result;
}
static void
gum_deinit_linker_details (void)
{
g_free (gum_dl_path);
}
static gchar *
gum_find_linker_path (void)
{
const gchar * traditional_path, * modern_path, * path;
traditional_path = (sizeof (gpointer) == 4)
? "/system/bin/linker"
: "/system/bin/linker64";
modern_path = (sizeof (gpointer) == 4)
? "/apex/com.android.runtime/bin/linker"
: "/apex/com.android.runtime/bin/linker64";
path = (gum_android_get_api_level () >= 29)
? modern_path
: traditional_path;
return g_strdup (path);
}
static GRegex *
gum_find_linker_path_pattern (void)
{
const gchar * pattern;
if (gum_android_get_api_level () >= 29)
{
pattern = (sizeof (gpointer) == 4)
? "/apex/com.android.runtime[^/]*/bin/linker$"
: "/apex/com.android.runtime[^/]*/bin/linker64$";
}
else
{
pattern = (sizeof (gpointer) == 4)
? "/system/bin/linker$"
: "/system/bin/linker64$";
}
return g_regex_new (pattern, 0, 0, NULL);
}
static gboolean
gum_try_parse_linker_proc_maps_line (const gchar * line,
const gchar * linker_path,
const GRegex * linker_path_pattern,
GumModuleDetails * module,
GumMemoryRange * range)
{
GumAddress start, end;
gchar perms[5] = { 0, };
gchar path[PATH_MAX];
gint n;
const guint8 elf_magic[] = { 0x7f, 'E', 'L', 'F' };
n = sscanf (line,
"%" G_GINT64_MODIFIER "x-%" G_GINT64_MODIFIER "x "
"%4c "
"%*x %*s %*d "
"%s",
&start, &end,
perms,
path);
if (n != 4)
return FALSE;
if (!g_regex_match (linker_path_pattern, path, 0, NULL))
return FALSE;
if (perms[0] != 'r')
return FALSE;
if (memcmp (GSIZE_TO_POINTER (start), elf_magic, sizeof (elf_magic)) != 0)
return FALSE;
module->name = strrchr (linker_path, '/') + 1;
module->range = range;
module->path = linker_path;
range->base_address = start;
range->size = end - start;
return TRUE;
}
const gchar **
gum_android_get_magic_linker_export_names (void)
{
return (gum_android_get_api_level () < 26)
? gum_magic_linker_export_names_pre_api_level_26
: gum_magic_linker_export_names_post_api_level_26;
}
gboolean
gum_android_try_resolve_magic_export (const gchar * module_name,
const gchar * symbol_name,
GumAddress * result)
{
const gchar ** magic_exports;
guint i;
magic_exports = gum_android_get_magic_linker_export_names ();
if (magic_exports[0] == NULL)
return FALSE;
if (module_name == NULL || !gum_android_is_linker_module_name (module_name))
return FALSE;
for (i = 0; magic_exports[i] != NULL; i++)
{
if (strcmp (symbol_name, magic_exports[i]) == 0)
{
*result = GUM_ADDRESS (dlsym (RTLD_DEFAULT, symbol_name));
return TRUE;
}
}
return FALSE;
}
GumElfModule *
gum_android_open_linker_module (void)
{
const GumModuleDetails * linker;
linker = gum_android_get_linker_module_details ();
return gum_elf_module_new_from_memory (linker->path,
linker->range->base_address, NULL);
}
void *
gum_android_get_module_handle (const gchar * name)
{
GumGetModuleHandleContext ctx;
ctx.name = name;
ctx.module = NULL;
gum_enumerate_soinfo (
(GumFoundSoinfoFunc) gum_store_module_handle_if_name_matches, &ctx);
return ctx.module;
}
static gboolean
gum_store_module_handle_if_name_matches (const GumSoinfoDetails * details,
GumGetModuleHandleContext * ctx)
{
GumLinkerApi * api = details->api;
if (gum_linux_module_path_matches (details->path, ctx->name))
{
GumSoinfoBody * sb = details->body;
int flags = RTLD_LAZY;
void * caller_addr = GSIZE_TO_POINTER (sb->base);
if (gum_android_is_vdso_module_name (details->path))
return FALSE;
if ((sb->flags & GUM_SOINFO_NEW_FORMAT) != 0)
{
GumSoinfo * parent;
parent = gum_soinfo_get_parent (details->si);
if (parent != NULL)
{
caller_addr = GSIZE_TO_POINTER (gum_soinfo_get_body (parent)->base);
}
if (sb->version >= 1)
{
flags = gum_soinfo_get_rtld_flags (details->si);
}
}
if (gum_android_get_api_level () >= 21)
{
flags |= RTLD_NOLOAD;
}
if (api->dlopen != NULL)
{
/* API level >= 26 (Android >= 8.0) */
ctx->module = api->dlopen (details->path, flags, caller_addr);
}
else if (api->do_dlopen != NULL)
{
/* API level >= 24 (Android >= 7.0) */
ctx->module = api->do_dlopen (details->path, flags, NULL, caller_addr);
}
else
{
ctx->module = dlopen (details->path, flags);
}
return FALSE;