improve: dynamically retrieve libc.so path

This commit makes ptracer dynamically get "libc.so" library path, avoiding errors when errors happen in environments where libc.so from zygote doesn't come from "/system/lib" or "/system/lib64".
This commit is contained in:
ThePedroo
2025-06-15 15:20:42 -03:00
parent a0ab02cedc
commit f432550f07

View File

@@ -160,6 +160,7 @@ bool inject_on_main(int pid, const char *lib_path) {
LOGD("libc return addr %p", libc_return_addr);
const char *libdl_path = NULL;
const char *libc_path = NULL;
for (size_t i = 0; i < local_map->size; i++) {
if (local_map->maps[i].path == NULL) continue;
@@ -168,7 +169,19 @@ bool inject_on_main(int pid, const char *lib_path) {
if (strcmp(filename, "libdl.so") == 0) {
libdl_path = local_map->maps[i].path;
break;
/* INFO: If we had found libc.so too, no need to continue searching */
if (libc_path) break;
continue;
}
if (strcmp(filename, "libc.so") == 0) {
libc_path = local_map->maps[i].path;
/* INFO: If we had found libdl.so too, no need to continue searching */
if (libdl_path) break;
continue;
}
}
@@ -238,19 +251,19 @@ bool inject_on_main(int pid, const char *lib_path) {
LOGE("dlerror str is null");
free(args);
free_maps(local_map);
free_maps(map);
return false;
}
#ifdef __LP64__
void *strlen_addr = find_func_addr(local_map, map, "/system/lib64/libc.so", "strlen");
#else
void *strlen_addr = find_func_addr(local_map, map, "/system/lib/libc.so", "strlen");
#endif
void *strlen_addr = find_func_addr(local_map, map, libc_path, "strlen");
if (strlen_addr == NULL) {
LOGE("find strlen");
free(args);
free_maps(local_map);
free_maps(map);
return false;
}
@@ -262,6 +275,8 @@ bool inject_on_main(int pid, const char *lib_path) {
LOGE("dlerror len <= 0");
free(args);
free_maps(local_map);
free_maps(map);
return false;
}
@@ -271,6 +286,8 @@ bool inject_on_main(int pid, const char *lib_path) {
LOGE("malloc err");
free(args);
free_maps(local_map);
free_maps(map);
return false;
}
@@ -282,6 +299,9 @@ bool inject_on_main(int pid, const char *lib_path) {
free(err);
free(args);
free_maps(local_map);
free_maps(map);
return false;
}