Skip to content

Commit 11d04f2

Browse files
committed
Fixed a bug that failed when hooking functions in .symtab in Android 5.x.
1 parent 3a6b18a commit 11d04f2

File tree

7 files changed

+51
-22
lines changed

7 files changed

+51
-22
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# android-inline-hook
22

33
![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)
4-
![](https://img.shields.io/badge/release-1.0.2-red.svg?style=flat)
4+
![](https://img.shields.io/badge/release-1.0.3-red.svg?style=flat)
55
![](https://img.shields.io/badge/Android-4.1%20--%2012-blue.svg?style=flat)
66
![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a-blue.svg?style=flat)
77

@@ -55,7 +55,7 @@ android {
5555
}
5656
5757
dependencies {
58-
implementation 'com.bytedance.android:shadowhook:1.0.2'
58+
implementation 'com.bytedance.android:shadowhook:1.0.3'
5959
}
6060
```
6161

README.zh-CN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# android-inline-hook
22

33
![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)
4-
![](https://img.shields.io/badge/release-1.0.2-red.svg?style=flat)
4+
![](https://img.shields.io/badge/release-1.0.3-red.svg?style=flat)
55
![](https://img.shields.io/badge/Android-4.1%20--%2012-blue.svg?style=flat)
66
![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a-blue.svg?style=flat)
77

@@ -55,7 +55,7 @@ android {
5555
}
5656
5757
dependencies {
58-
implementation 'com.bytedance.android:shadowhook:1.0.2'
58+
implementation 'com.bytedance.android:shadowhook:1.0.3'
5959
}
6060
```
6161

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ ext {
2323
abiFilters = "armeabi-v7a,arm64-v8a"
2424
useASAN = false
2525
dependencyOnLocalLibrary = true
26-
shadowhookVersion = "1.0.2"
26+
shadowhookVersion = "1.0.3"
2727

2828
POM_GROUP_ID = "com.bytedance.android"
2929
POM_ARTIFACT_ID = "shadowhook"
30-
POM_VERSION_NAME = "1.0.2"
30+
POM_VERSION_NAME = "1.0.3"
3131

3232
POM_NAME = "shadowhook"
3333
POM_DESCRIPTION = "shadowhook is an inline hook library for Android apps."

shadowhook/src/main/cpp/third_party/xdl/xdl.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
#include "xdl_lzma.h"
4747
#include "xdl_util.h"
4848

49+
#ifndef __LP64__
50+
#define XDL_LIB_PATH "/system/lib"
51+
#else
52+
#define XDL_LIB_PATH "/system/lib64"
53+
#endif
54+
4955
#define XDL_DYNSYM_IS_EXPORT_SYM(shndx) (SHN_UNDEF != (shndx))
5056
#define XDL_SYMTAB_IS_EXPORT_SYM(shndx) \
5157
(SHN_UNDEF != (shndx) && !((shndx) >= SHN_LORESERVE && (shndx) <= SHN_HIRESERVE))
@@ -282,6 +288,8 @@ static int xdl_symtab_load_from_debugdata(xdl_t *self, int file_fd, size_t file_
282288

283289
// load from disk and memory
284290
static int xdl_symtab_load(xdl_t *self) {
291+
if ('[' == self->pathname[0]) return -1;
292+
285293
int r = -1;
286294
ElfW(Shdr) *shdrs = NULL;
287295
char *shstrtab = NULL;
@@ -298,7 +306,21 @@ static int xdl_symtab_load(xdl_t *self) {
298306
self->base = self->load_bias + vaddr_min;
299307

300308
// open file
301-
int file_fd = open(self->pathname, O_RDONLY | O_CLOEXEC);
309+
int flags = O_RDONLY | O_CLOEXEC;
310+
int file_fd;
311+
if ('/' == self->pathname[0]) {
312+
file_fd = open(self->pathname, flags);
313+
} else {
314+
char full_pathname[1024];
315+
// try the fast method
316+
snprintf(full_pathname, sizeof(full_pathname), "%s/%s", XDL_LIB_PATH, self->pathname);
317+
file_fd = open(full_pathname, flags);
318+
if (file_fd < 0) {
319+
// try the slow method
320+
if (0 != xdl_iterate_get_full_pathname(self->base, full_pathname, sizeof(full_pathname))) return -1;
321+
file_fd = open(full_pathname, flags);
322+
}
323+
}
302324
if (file_fd < 0) return -1;
303325
struct stat st;
304326
if (0 != fstat(file_fd, &st)) goto end;

shadowhook/src/main/cpp/third_party/xdl/xdl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// Created by caikelun on 2020-10-04.
2323

2424
//
25-
// xDL version: 1.1.2
25+
// xDL version: 1.1.3
2626
//
2727
// xDL is an enhanced implementation of the Android DL series functions.
2828
// For more information, documentation, and the latest version please check:

shadowhook/src/main/cpp/third_party/xdl/xdl_iterate.c

+19-14
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,9 @@ static int xdl_iterate_open_or_rewind_maps(FILE **maps) {
8181
return 0;
8282
}
8383

84-
static uintptr_t xdl_iterate_get_pathname_from_maps(struct dl_phdr_info *info, char *buf, size_t buf_len,
85-
FILE **maps) {
86-
// get base address
87-
uintptr_t min_vaddr = xdl_iterate_get_min_vaddr(info);
88-
if (UINTPTR_MAX == min_vaddr) return 0; // failed
89-
uintptr_t base = (uintptr_t)(info->dlpi_addr + min_vaddr);
90-
84+
static int xdl_iterate_get_pathname_from_maps(uintptr_t base, char *buf, size_t buf_len, FILE **maps) {
9185
// open or rewind maps-file
92-
if (0 != xdl_iterate_open_or_rewind_maps(maps)) return 0; // failed
86+
if (0 != xdl_iterate_open_or_rewind_maps(maps)) return -1; // failed
9387

9488
char line[1024];
9589
while (fgets(line, sizeof(line), *maps)) {
@@ -106,10 +100,10 @@ static uintptr_t xdl_iterate_get_pathname_from_maps(struct dl_phdr_info *info, c
106100

107101
// found it
108102
strlcpy(buf, pathname, buf_len);
109-
return (uintptr_t)buf; // OK
103+
return 0; // OK
110104
}
111105

112-
return 0; // failed
106+
return -1; // failed
113107
}
114108

115109
static int xdl_iterate_by_linker_cb(struct dl_phdr_info *info, size_t size, void *arg) {
@@ -142,11 +136,15 @@ static int xdl_iterate_by_linker_cb(struct dl_phdr_info *info, size_t size, void
142136

143137
// fix dlpi_name (from /proc/self/maps)
144138
if ('/' != info->dlpi_name[0] && '[' != info->dlpi_name[0] && (0 != (flags & XDL_FULL_PATHNAME))) {
145-
char buf[512];
146-
uintptr_t pathname = xdl_iterate_get_pathname_from_maps(info, buf, sizeof(buf), maps);
147-
if (0 == pathname) return 0; // ignore this ELF
139+
// get base address
140+
uintptr_t min_vaddr = xdl_iterate_get_min_vaddr(info);
141+
if (UINTPTR_MAX == min_vaddr) return 0; // ignore this ELF
142+
uintptr_t base = (uintptr_t)(info->dlpi_addr + min_vaddr);
143+
144+
char buf[1024];
145+
if (0 != xdl_iterate_get_pathname_from_maps(base, buf, sizeof(buf), maps)) return 0; // ignore this ELF
148146

149-
info->dlpi_name = (const char *)pathname;
147+
info->dlpi_name = (const char *)buf;
150148
}
151149

152150
// callback
@@ -245,3 +243,10 @@ int xdl_iterate_phdr_impl(xdl_iterate_phdr_cb_t cb, void *cb_arg, int flags) {
245243
// iterate by dl_iterate_phdr()
246244
return xdl_iterate_by_linker(cb, cb_arg, flags);
247245
}
246+
247+
int xdl_iterate_get_full_pathname(uintptr_t base, char *buf, size_t buf_len) {
248+
FILE *maps = NULL;
249+
int r = xdl_iterate_get_pathname_from_maps(base, buf, buf_len, &maps);
250+
if (NULL != maps) fclose(maps);
251+
return r;
252+
}

shadowhook/src/main/cpp/third_party/xdl/xdl_iterate.h

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ extern "C" {
3434
typedef int (*xdl_iterate_phdr_cb_t)(struct dl_phdr_info *info, size_t size, void *arg);
3535
int xdl_iterate_phdr_impl(xdl_iterate_phdr_cb_t cb, void *cb_arg, int flags);
3636

37+
int xdl_iterate_get_full_pathname(uintptr_t base, char *buf, size_t buf_len);
38+
3739
#ifdef __cplusplus
3840
}
3941
#endif

0 commit comments

Comments
 (0)