Skip to content

Commit

Permalink
Remove some traces left by LSPosed
Browse files Browse the repository at this point in the history
1. Avoid changing `system.prop`: there is already no need to add
   system-wise `dex2oat` flags, since LSPosed provides a wrapper for it,
   see https://nullptr.icu/index.php/archives/53/ for detailed
   explanation;
2. Postpone initialization of LSPlant: initialization of `initInfo`
   during the `onLoad` Zygisk api will change the order of parsed files
   in memory, especially bring the item `libart.so` forward;
3. Close opened virtual map file: this should no longer be a problem
   after the second point is applied since it is no longer opened during
   `onLoad`, but let us close it as a good practice;
4. Remove /data/resource-cache mount: introduced in
   LSPosed#1627, most likely designed to
   solve problems in early versions of root solutions and being reverted
   does not change the function of deamon since we will have to wait
   sufficient time in `waitSystemService` of `LSPosedService` manager.
   Of course, more tests are neeeded on different devices to see if
   parasitic notification manager will work as expected.
  • Loading branch information
JingMatrix committed Dec 2, 2024
1 parent 984bb1c commit 156c6ae
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
2 changes: 0 additions & 2 deletions magisk-loader/magisk_module/daemon
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ if [ $debug = "true" ]; then
fi
fi

mount tmpfs -t tmpfs /data/resource-cache

if [ ! -S "/dev/socket/zygote" ]; then
timeout 0.5 inotifyd - /dev/socket:near | while read -r line; do
$debug && log -p v -t "LSPosed" "inotify: $line"
Expand Down
1 change: 0 additions & 1 deletion magisk-loader/magisk_module/system.prop

This file was deleted.

6 changes: 3 additions & 3 deletions magisk-loader/src/main/jni/api/zygisk_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ZygiskModule : public zygisk::ModuleBase {
void onLoad(zygisk::Api *api, JNIEnv *env) override {
env_ = env;
api_ = api;
MagiskLoader::Init(api);
MagiskLoader::Init();
ConfigImpl::Init();
}

Expand All @@ -51,7 +51,7 @@ class ZygiskModule : public zygisk::ModuleBase {
}

void postAppSpecialize(const zygisk::AppSpecializeArgs *args) override {
MagiskLoader::GetInstance()->OnNativeForkAndSpecializePost(env_, args->nice_name,
MagiskLoader::GetInstance()->OnNativeForkAndSpecializePost(env_, api_, args->nice_name,
args->app_data_dir);
if (*allowUnload) api_->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
}
Expand All @@ -69,7 +69,7 @@ class ZygiskModule : public zygisk::ModuleBase {
env_->DeleteLocalRef(name);
env_->DeleteLocalRef(process);
}
MagiskLoader::GetInstance()->OnNativeForkSystemServerPost(env_);
MagiskLoader::GetInstance()->OnNativeForkSystemServerPost(env_, api_);
if (*allowUnload) api_->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
}
};
Expand Down
16 changes: 11 additions & 5 deletions magisk-loader/src/main/jni/src/magisk_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ std::vector<MapInfo> MapInfo::Scan(std::string_view pid) {
constexpr static auto kMapEntry = 7;
std::vector<MapInfo> info;
auto path = "/proc/" + std::string{pid} + "/maps";
auto maps = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "r"), &fclose};
auto maps = fopen(path.c_str(), "r");
if (maps) {
char *line = nullptr;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, maps.get())) > 0) {
while ((read = getline(&line, &len, maps)) > 0) {
line[read - 1] = '\0';
uintptr_t start = 0;
uintptr_t end = 0;
Expand All @@ -89,10 +89,12 @@ std::vector<MapInfo> MapInfo::Scan(std::string_view pid) {
}
free(line);
}
fclose(maps);
return info;
}

void MagiskLoader::InitializeZygiskApi(zygisk::Api *api) {
void MagiskLoader::InitializeLSPlant(zygisk::Api *api) {
if (lsplant_initilized) return;
std::vector<std::pair<const char *, void **>> plt_hook_saved = {};

const std::string libArtPath = GetArt()->name();
Expand Down Expand Up @@ -151,6 +153,7 @@ void MagiskLoader::InitializeZygiskApi(zygisk::Api *api) {
.art_symbol_prefix_resolver =
[](auto symbol) { return GetArt()->getSymbPrefixFirstAddress(symbol); },
.is_plt_hook = true};
lsplant_initilized = true;
}

void MagiskLoader::LoadDex(JNIEnv *env, PreloadedDex &&dex) {
Expand Down Expand Up @@ -195,7 +198,7 @@ void MagiskLoader::OnNativeForkSystemServerPre(JNIEnv *env) {
setAllowUnload(skip_);
}

void MagiskLoader::OnNativeForkSystemServerPost(JNIEnv *env) {
void MagiskLoader::OnNativeForkSystemServerPost(JNIEnv *env, zygisk::Api *api) {
if (!skip_) {
auto *instance = Service::instance();
auto system_server_binder = instance->RequestSystemServerBinder(env);
Expand All @@ -218,6 +221,7 @@ void MagiskLoader::OnNativeForkSystemServerPost(JNIEnv *env) {
instance->HookBridge(*this, env);

// always inject into system server
InitializeLSPlant(api);
InitArtHooker(env, initInfo);
InitHooks(env);
SetupEntryClass(env);
Expand Down Expand Up @@ -272,7 +276,8 @@ void MagiskLoader::OnNativeForkAndSpecializePre(JNIEnv *env, jint uid, jintArray
setAllowUnload(skip_);
}

void MagiskLoader::OnNativeForkAndSpecializePost(JNIEnv *env, jstring nice_name, jstring app_dir) {
void MagiskLoader::OnNativeForkAndSpecializePost(JNIEnv *env, zygisk::Api *api, jstring nice_name,
jstring app_dir) {
const JUTFString process_name(env, nice_name);
auto *instance = Service::instance();
if (is_parasitic_manager) nice_name = JNI_NewStringUTF(env, "org.lsposed.manager").release();
Expand All @@ -284,6 +289,7 @@ void MagiskLoader::OnNativeForkAndSpecializePost(JNIEnv *env, jstring nice_name,
ConfigBridge::GetInstance()->obfuscation_map(std::move(obfs_map));
LoadDex(env, PreloadedDex(dex_fd, size));
close(dex_fd);
InitializeLSPlant(api);
InitArtHooker(env, initInfo);
InitHooks(env);
SetupEntryClass(env);
Expand Down
15 changes: 7 additions & 8 deletions magisk-loader/src/main/jni/src/magisk_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
namespace lspd {
class MagiskLoader : public Context {
public:
inline static void Init(zygisk::Api *api) {
instance_ = std::make_unique<MagiskLoader>();
GetInstance()->InitializeZygiskApi(api);
}
inline static void Init() { instance_ = std::make_unique<MagiskLoader>(); }

inline static MagiskLoader *GetInstance() {
return static_cast<MagiskLoader *>(instance_.get());
Expand All @@ -41,22 +38,24 @@ class MagiskLoader : public Context {
void OnNativeForkAndSpecializePre(JNIEnv *env, jint uid, jintArray &gids, jstring &nice_name,
jboolean is_child_zygote, jstring app_data_dir);

void OnNativeForkAndSpecializePost(JNIEnv *env, jstring nice_name, jstring app_dir);

void OnNativeForkSystemServerPost(JNIEnv *env);
void OnNativeForkAndSpecializePost(JNIEnv *env, zygisk::Api *api, jstring nice_name,
jstring app_dir);

void OnNativeForkSystemServerPre(JNIEnv *env);

void OnNativeForkSystemServerPost(JNIEnv *env, zygisk::Api *api);

protected:
void LoadDex(JNIEnv *env, PreloadedDex &&dex) override;

void SetupEntryClass(JNIEnv *env) override;

private:
bool skip_ = false;
bool lsplant_initilized = false;
lsplant::InitInfo initInfo;

void InitializeZygiskApi(zygisk::Api *api);
void InitializeLSPlant(zygisk::Api *api);
static void setAllowUnload(bool unload);
};

Expand Down

2 comments on commit 156c6ae

@HuskyDG
Copy link

@HuskyDG HuskyDG commented on 156c6ae Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. if [ "$API" -ge 29 ]; then

@JingMatrix
Copy link
Owner Author

@JingMatrix JingMatrix commented on 156c6ae Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HuskyDG Thank you for the information, I will add commits for the compatibility. Didn't notice it before.

Please sign in to comment.