Skip to content

Commit c01a09d

Browse files
committed
add: injection hardening
This commit adds injection hardening mechanism to ReLSPosed, where it will not proceed with the processing/binder-based communication.
1 parent 6b03cbd commit c01a09d

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

magisk-loader/src/main/jni/api/zygisk_main.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
#include "zygisk.hpp"
2828

2929
namespace lspd {
30+
3031
int allow_unload = 0;
3132
int *allowUnload = &allow_unload;
33+
bool should_ignore = false;
3234

3335
class ZygiskModule : public zygisk::ModuleBase {
3436
JNIEnv *env_;
@@ -42,12 +44,60 @@ class ZygiskModule : public zygisk::ModuleBase {
4244
}
4345

4446
void preAppSpecialize(zygisk::AppSpecializeArgs *args) override {
47+
int cfd = api_->connectCompanion();
48+
if (cfd < 0) {
49+
LOGE("Failed to connect to companion: %s", strerror(errno));
50+
51+
return;
52+
}
53+
54+
uint8_t injection_hardening_disabled = 0;
55+
if (read(cfd, &injection_hardening_disabled, sizeof(injection_hardening_disabled)) < 0) {
56+
LOGE("Failed to read from companion socket: %s", strerror(errno));
57+
}
58+
59+
close(cfd);
60+
61+
if (!injection_hardening_disabled) {
62+
uint32_t flags = api_->getFlags();
63+
if ((flags & zygisk::PROCESS_ON_DENYLIST) == 0) goto bypass_denylist;
64+
65+
const char *name = env_->GetStringUTFChars(args->nice_name, nullptr);
66+
if (strcmp(name, "com.android.shell") == 0) {
67+
LOGD("Process is com.android.shell, bypassing denylist check");
68+
69+
env_->ReleaseStringUTFChars(args->nice_name, name);
70+
71+
goto bypass_denylist;
72+
}
73+
74+
LOGE("Process %s is on denylist, cannot specialize", name);
75+
76+
env_->ReleaseStringUTFChars(args->nice_name, name);
77+
78+
should_ignore = true;
79+
80+
return;
81+
} else {
82+
LOGD("Injection hardening is disabled");
83+
}
84+
85+
bypass_denylist:
86+
4587
MagiskLoader::GetInstance()->OnNativeForkAndSpecializePre(
4688
env_, args->uid, args->gids, args->nice_name,
4789
args->is_child_zygote ? *args->is_child_zygote : false, args->app_data_dir);
4890
}
4991

5092
void postAppSpecialize(const zygisk::AppSpecializeArgs *args) override {
93+
if (should_ignore) {
94+
LOGD("Ignoring postAppSpecialize due to injection hardening being enabled");
95+
96+
api_->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
97+
98+
return;
99+
}
100+
51101
MagiskLoader::GetInstance()->OnNativeForkAndSpecializePost(env_, args->nice_name,
52102
args->app_data_dir);
53103
if (*allowUnload) api_->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
@@ -72,4 +122,22 @@ class ZygiskModule : public zygisk::ModuleBase {
72122
};
73123
} // namespace lspd
74124

125+
void relsposed_companion(int lib_fd) {
126+
/* INFO: The only current task we do in companion now is to check if
127+
/data/adb/disable_injection_hardening file exists. */
128+
uint8_t file_exists = 0;
129+
if (access("/data/adb/disable_injection_hardening", F_OK) == 0) {
130+
LOGD("Found /data/adb/disable_injection_hardening, disabling injection hardening");
131+
132+
file_exists = 1;
133+
}
134+
135+
if (write(lib_fd, &file_exists, sizeof(file_exists)) < 0) {
136+
LOGE("Failed to write to companion socket: %s", strerror(errno));
137+
}
138+
139+
close(lib_fd);
140+
}
141+
75142
REGISTER_ZYGISK_MODULE(lspd::ZygiskModule);
143+
REGISTER_ZYGISK_COMPANION(relsposed_companion);

0 commit comments

Comments
 (0)