Skip to content

Commit 6714fe5

Browse files
committed
Add runtime_flags and service.sh arguments for late injection
Complicated modules, such as LSPosed / Vector, need to be inform about the injection mode, since many of their functions could reply on the exact launching sequence of system services.
1 parent 5b2ad41 commit 6714fe5

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

loader/src/injector/module.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,12 @@ void ZygiskContext::run_modules_post() {
320320
if (m.tryUnload()) modules_unloaded++;
321321
}
322322

323-
if (modules.size() > 0) {
323+
if (modules.size() > 0 && g_hook != nullptr) {
324324
LOGV("modules unloaded: %zu/%zu", modules_unloaded, modules.size());
325325
if (modules.size() == modules_unloaded) clean_libc_trace();
326326
clean_linker_trace("jit-cache-zygisk", modules.size(), modules_unloaded, true);
327-
if (g_hook != nullptr)
328-
g_hook->should_spoof_maps =
329-
(flags & APP_SPECIALIZE) && (modules.size() - modules_unloaded) > 0;
327+
g_hook->should_spoof_maps =
328+
(flags & APP_SPECIALIZE) && (modules.size() - modules_unloaded) > 0;
330329
}
331330
}
332331

loader/src/injector/system_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void trigger_system_server_hooks() {
147147
jint uid = static_cast<jint>(getuid());
148148
jint gid = static_cast<jint>(getgid());
149149
jintArray gids = fetch_gids(env);
150-
jint runtime_flags = 0;
150+
jint runtime_flags = RuntimeFlags::LATE_INJECT;
151151
jlong permitted_capabilities = 0;
152152
jlong effective_capabilities = 0;
153153

loader/src/injector/system_server.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <cstdint>
34
/**
45
* @brief Triggers Zygisk module hooks for system_server in late-injection scenarios.
56
*
@@ -8,3 +9,9 @@
89
* system_server_specialize.
910
*/
1011
void trigger_system_server_hooks();
12+
13+
enum RuntimeFlags : uint32_t {
14+
// Safely out of the way of AOSP's flags (Bits 0, 14-26)
15+
// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/jni/com_android_internal_os_Zygote.cpp;
16+
LATE_INJECT = 1 << 30,
17+
};

module/src/service.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@ fi
99

1010
cd "$MODDIR"
1111

12+
system_server_pid=$(pidof system_server)
13+
1214
if [ "$(which magisk)" ]; then
1315
for file in ../*; do
1416
if [ -d "$file" ] && [ -d "$file/zygisk" ] && ! [ -f "$file/disable" ]; then
1517
if [ -f "$file/service.sh" ]; then
1618
cd "$file"
1719
log -p i -t "zygisk-sh" "Manually trigger service.sh for $file"
18-
sh "$(realpath ./service.sh)" &
20+
if [ -z $system_server_pid ]; then
21+
sh "$(realpath ./service.sh)" &
22+
else
23+
sh "$(realpath ./service.sh)" --late-inject &
24+
fi
1925
cd "$MODDIR"
2026
fi
2127
fi
2228
done
2329
fi
2430

25-
if [ ! -z $(pidof system_server) ]; then
26-
log -p i -t "zygisk-sh" "Maually inject into system_server $(pidof system_server)"
27-
./bin/zygisk-ptrace64 trace $(pidof system_server) --system_server
31+
if [ ! -z $system_server_pid ]; then
32+
log -p i -t "zygisk-sh" "Maually inject into system_server $system_server_pid"
33+
./bin/zygisk-ptrace64 trace $system_server_pid --system_server
2834
fi

0 commit comments

Comments
 (0)