Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 96f23b0

Browse files
committed
generate a random name for libmemtrack_real to temporarily make SafetyNet happy
1 parent b4fc4af commit 96f23b0

File tree

6 files changed

+93
-27
lines changed

6 files changed

+93
-27
lines changed

module/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def apiVersion = 6
99
def moduleProp = [
1010
id : "riru-core",
1111
name : "Riru (Riru - Core)",
12-
version : "v21.0",
13-
versionCode: "33",
12+
version : "v21.1",
13+
versionCode: "34",
1414
author : "Rikka",
1515
description: "Inject zygote process by replace libmemtrack.so, provide interface to other Riru modules."
1616
]

module/src/main/cpp/redirect_memtrack.cpp

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
#include <dlfcn.h>
22
#include <sys/types.h>
3+
#include <fcntl.h>
4+
#include <zconf.h>
5+
#include <unistd.h>
6+
#include <cstring>
7+
#include <cstdio>
8+
#include "logging.h"
39

410
#ifdef __LP64__
5-
#define MEMTRACK_LIBRARY "/system/lib64/libmemtrack_real.so"
11+
#define LIB_PATH "/system/lib64"
612
#else
7-
#define MEMTRACK_LIBRARY "/system/lib/libmemtrack_real.so"
13+
#define LIB_PATH "/system/lib"
814
#endif
915

16+
static void *init() {
17+
char path[PATH_MAX] = {0}, buf[64] = {0};
18+
19+
int fd = open("/data/adb/riru/random_name", O_RDONLY);
20+
if (fd > 0 && read(fd, buf, 64)) {
21+
read(fd, buf, 64);
22+
snprintf(path, PATH_MAX, "%s/lib%s.so", LIB_PATH, buf);
23+
close(fd);
24+
if (access(path, F_OK) == 0) {
25+
return dlopen(path, RTLD_NOW | RTLD_GLOBAL);
26+
}
27+
}
28+
return nullptr;
29+
}
30+
31+
static void *handle = init();
32+
1033
extern "C" {
11-
static void *handle = dlopen(MEMTRACK_LIBRARY, RTLD_NOW | RTLD_GLOBAL);
1234

1335
#define FUNC_DEF(NAME, RET, ...) \
1436
static void* sym_##NAME = handle ? dlsym(handle, #NAME) : NULL; \
@@ -161,56 +183,66 @@ FUNC_DEF(_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9a
161183
if (!sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEm)
162184
return;
163185

164-
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEm_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEm)(a1, a2);
186+
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEm_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEm)(
187+
a1, a2);
165188
}
166189

167190
FUNC_DEF(_ZNSt3__114__split_bufferIN7android8hardware8memtrack4V1_014MemtrackRecordERNS_9allocatorIS5_EEEC2EjjS8_, int, uint a1, uint a2, void *a3) {
168191
if (!sym__ZNSt3__114__split_bufferIN7android8hardware8memtrack4V1_014MemtrackRecordERNS_9allocatorIS5_EEEC2EjjS8_)
169192
return 0;
170193

171-
return ((_ZNSt3__114__split_bufferIN7android8hardware8memtrack4V1_014MemtrackRecordERNS_9allocatorIS5_EEEC2EjjS8__t) sym__ZNSt3__114__split_bufferIN7android8hardware8memtrack4V1_014MemtrackRecordERNS_9allocatorIS5_EEEC2EjjS8_)(a1, a2, a3);
194+
return ((_ZNSt3__114__split_bufferIN7android8hardware8memtrack4V1_014MemtrackRecordERNS_9allocatorIS5_EEEC2EjjS8__t) sym__ZNSt3__114__split_bufferIN7android8hardware8memtrack4V1_014MemtrackRecordERNS_9allocatorIS5_EEEC2EjjS8_)(
195+
a1, a2, a3);
172196
}
173197

174198
FUNC_DEF(_ZNSt3__113__vector_baseIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEED2Ev, void, void **a1) {
175199
if (!sym__ZNSt3__113__vector_baseIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEED2Ev)
176200
return;
177201

178-
return ((_ZNSt3__113__vector_baseIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEED2Ev_t) sym__ZNSt3__113__vector_baseIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEED2Ev)(a1);
202+
return ((_ZNSt3__113__vector_baseIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEED2Ev_t) sym__ZNSt3__113__vector_baseIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEED2Ev)(
203+
a1);
179204
}
180205

181206
FUNC_DEF(_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEj, int, int a1, uint a2) {
182207
if (!sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEj)
183208
return 0;
184209

185-
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEj_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEj)(a1, a2);
210+
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEj_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8__appendEj)(
211+
a1, a2);
186212
}
187213

188-
FUNC_DEF(_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE26__swap_out_circular_bufferERNS_14__split_bufferIS5_RS7_EE, int, int *a1, void *a2) {
214+
FUNC_DEF(
215+
_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE26__swap_out_circular_bufferERNS_14__split_bufferIS5_RS7_EE,
216+
int, int *a1, void *a2) {
189217
if (!sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE26__swap_out_circular_bufferERNS_14__split_bufferIS5_RS7_EE)
190218
return 0;
191219

192-
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE26__swap_out_circular_bufferERNS_14__split_bufferIS5_RS7_EE_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE26__swap_out_circular_bufferERNS_14__split_bufferIS5_RS7_EE)(a1, a2);
220+
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE26__swap_out_circular_bufferERNS_14__split_bufferIS5_RS7_EE_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE26__swap_out_circular_bufferERNS_14__split_bufferIS5_RS7_EE)(
221+
a1, a2);
193222
}
194223

195224
FUNC_DEF(_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8allocateEj, int, void *a1, uint a2) {
196225
if (!sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8allocateEj)
197226
return 0;
198227

199-
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8allocateEj_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8allocateEj)(a1, a2);
228+
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8allocateEj_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE8allocateEj)(
229+
a1, a2);
200230
}
201231

202232
FUNC_DEF(_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE6resizeEj, int, int a1, uint a2) {
203233
if (!sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE6resizeEj)
204234
return 0;
205235

206-
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE6resizeEj_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE6resizeEj)(a1, a2);
236+
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE6resizeEj_t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEE6resizeEj)(
237+
a1, a2);
207238
}
208239

209240
FUNC_DEF(_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEEC2ERKS8_, int, void *a1, void *a2) {
210241
if (!sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEEC2ERKS8_)
211242
return 0;
212243

213-
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEEC2ERKS8__t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEEC2ERKS8_)(a1, a2);
244+
return ((_ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEEC2ERKS8__t) sym__ZNSt3__16vectorIN7android8hardware8memtrack4V1_014MemtrackRecordENS_9allocatorIS5_EEEC2ERKS8_)(
245+
a1, a2);
214246
}
215247

216248
struct memtrack_proc;

module/src/main/cpp/zygote_restart.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#include "pmparser.h"
1010

1111
#ifdef __LP64__
12-
#define CHECK_LIB_NAME "/system/lib64/libmemtrack_real.so"
12+
#define LIB_PATH "/system/lib64"
1313
#define ZYGOTE_NAME "zygote64"
1414
#define RESTART_NAME "zygote_secondary"
1515
#else
16-
#define CHECK_LIB_NAME "/system/lib/libmemtrack_real.so"
16+
#define LIB_PATH "/system/lib"
1717
#define ZYGOTE_NAME "zygote"
1818
#define RESTART_NAME "zygote"
1919
#endif
@@ -117,7 +117,7 @@ static int is_path_in_maps(int pid, const char *path) {
117117
return false;
118118
}
119119

120-
static bool should_restart() {
120+
static bool should_restart(const char* name) {
121121
// It is said that some wired devices (Samsung? or other) have multiply zygote, get all processes called zygote
122122
std::vector<pid_t> pids;
123123
while ((pids = grep_pid(ZYGOTE_NAME, 0)).empty()) {
@@ -132,7 +132,7 @@ static bool should_restart() {
132132

133133
int riru_count = 0;
134134
for (auto pid : pids) {
135-
if (!is_path_in_maps(pid, CHECK_LIB_NAME)) {
135+
if (!is_path_in_maps(pid, name)) {
136136
LOGW("no Riru found in %s (pid=%d), restart required", ZYGOTE_NAME, pid);
137137
} else {
138138
LOGI("found Riru in %s (pid=%d)", ZYGOTE_NAME, pid);
@@ -143,9 +143,9 @@ static bool should_restart() {
143143
return riru_count != count;
144144
}
145145

146-
static bool should_restart(int retries) {
146+
static bool should_restart(const char* name, int retries) {
147147
for (int i = 0; i < retries; ++i) {
148-
if (should_restart())
148+
if (should_restart(name))
149149
return true;
150150

151151
if (i != retries - 1)
@@ -160,17 +160,27 @@ int main(int argc, char **argv) {
160160
if (fork() != 0)
161161
return 1;
162162

163-
if (!should_restart(3))
164-
return 0;
163+
// read random name
164+
char name[PATH_MAX] = {0}, buf[64] = {0};
165+
int fd = open("/data/adb/riru/random_name", O_RDONLY);
166+
if (fd > 0 && read(fd, buf, 64)) {
167+
read(fd, buf, 64);
168+
snprintf(name, PATH_MAX, "%s/lib%s.so", LIB_PATH, buf);
169+
close(fd);
170+
LOGI("libmemtrack: %s", name);
171+
}
165172

166173
// wait for magisk mount
167-
while (access(CHECK_LIB_NAME, F_OK) != 0) {
174+
if (!should_restart(name, 3))
175+
return 0;
176+
177+
while (access(name, F_OK) != 0) {
168178
LOGV("not mounted, wait 1s");
169179
sleep(1);
170180
}
171181

172182
// check again
173-
if (!should_restart(3)) {
183+
if (!should_restart(name, 3)) {
174184
LOGI("found Riru, abort restart");
175185
return 0;
176186
}

template/magisk_module/customize.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ui_print "- Extracting Magisk files"
3232
extract "$ZIPFILE" 'module.prop' "$MODPATH"
3333
extract "$ZIPFILE" 'post-fs-data.sh' "$MODPATH"
3434
extract "$ZIPFILE" 'uninstall.sh' "$MODPATH"
35+
extract "$ZIPFILE" 'sepolicy.rule' "$MODPATH"
3536

3637
if [ "$ARCH" = "x86" ] || [ "$ARCH" = "x64" ]; then
3738
ui_print "- Extracting x86 libraries"

template/magisk_module/post-fs-data.sh

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,34 @@ move_new_file "$RIRU_PATH/api_version"
1414
move_new_file "$RIRU_PATH/version_name"
1515
move_new_file "$RIRU_PATH/version_code"
1616

17+
# generate a random name
18+
RANDOM_NAME_FILE="/data/adb/riru/random_name"
19+
RANDOM_NAME=""
20+
if [ -f "$RANDOM_NAME_FILE" ]; then
21+
RANDOM_NAME=$(cat "$RANDOM_NAME_FILE")
22+
else
23+
while true; do
24+
RANDOM_NAME=$(mktemp -u XXXXXXXX)
25+
[ -f "/system/lib/lib$RANDOM_NAME.so" ] || break
26+
done
27+
mkdir "/data/adb/riru"
28+
printf "%s" "$RANDOM_NAME" > "$RANDOM_NAME_FILE"
29+
fi
30+
31+
# use magisk contextr
32+
chcon -R u:object_r:magisk_file:s0 "/data/adb/riru"
33+
34+
# remove old libmemtrack_real
35+
rm "$MODDIR/system/lib64/libmemtrack_real.so"
36+
rm "$MODDIR/system/lib/libmemtrack_real.so"
37+
1738
# Copy libmemtrack.so
18-
cp -f "/system/lib/libmemtrack.so" "$MODDIR/system/lib/libmemtrack_real.so"
19-
[ -f "/system/lib64/libmemtrack.so" ] && cp -f "/system/lib64/libmemtrack.so" "$MODDIR/system/lib64/libmemtrack_real.so"
39+
cp -f "/system/lib/libmemtrack.so" "$MODDIR/system/lib/lib$RANDOM_NAME.so"
40+
[ -f "/system/lib64/libmemtrack.so" ] && cp -f "/system/lib64/libmemtrack.so" "$MODDIR/system/lib64/lib$RANDOM_NAME.so"
2041

2142
# Reset context in case
2243
chcon -R u:object_r:system_file:s0 "$MODDIR"
2344

2445
# Restart zygote if needed
2546
ZYGOTE_RESTART=$RIRU_PATH/bin/zygote_restart
26-
[ ! -f "$RIRU_PATH/config/disable_auto_restart" ] && $ZYGOTE_RESTART
47+
[ ! -f "$RIRU_PATH/config/disable_auto_restart" ] && $ZYGOTE_RESTART

template/magisk_module/sepolicy.rule

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# allow zygote to enter /data/adb
2+
allow zygote adb_data_file dir search

0 commit comments

Comments
 (0)