From cd5384f13e131369ab565668615127204953f1ee Mon Sep 17 00:00:00 2001 From: LoveSy Date: Tue, 22 Nov 2022 09:43:23 +0800 Subject: [PATCH] Fix crashes whenever a zygisk module has ver > 4 --- native/src/zygisk/hook.cpp | 17 +++++++++++------ native/src/zygisk/module.hpp | 3 +++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/native/src/zygisk/hook.cpp b/native/src/zygisk/hook.cpp index d33677af2..8bd1728f1 100644 --- a/native/src/zygisk/hook.cpp +++ b/native/src/zygisk/hook.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -54,7 +55,7 @@ struct HookContext { } args; const char *process; - vector modules; + list modules; int pid; bitset flags; @@ -485,10 +486,6 @@ void HookContext::fork_post() { } void HookContext::run_modules_pre(const vector &fds) { - // Because the data structure stored in the vector is self referencing, in order to prevent - // dangling pointers, the vector has to be pre-allocated to ensure reallocation does not occur - modules.reserve(fds.size()); - for (int i = 0; i < fds.size(); ++i) { struct stat s{}; if (fstat(fds[i], &s) != 0 || !S_ISREG(s.st_mode)) { @@ -509,8 +506,16 @@ void HookContext::run_modules_pre(const vector &fds) { close(fds[i]); } + for (auto it = modules.begin(); it != modules.end();) { + it->onLoad(env); + if (it->valid()) { + ++it; + } else { + it = modules.erase(it); + } + } + for (auto &m : modules) { - m.onLoad(env); if (flags[APP_SPECIALIZE]) { m.preAppSpecialize(args.app); } else if (flags[SERVER_FORK_AND_SPECIALIZE]) { diff --git a/native/src/zygisk/module.hpp b/native/src/zygisk/module.hpp index 39144b78a..9ee41a498 100644 --- a/native/src/zygisk/module.hpp +++ b/native/src/zygisk/module.hpp @@ -180,6 +180,9 @@ struct ZygiskModule { void postServerSpecialize(const ServerSpecializeArgs_v1 *args) const { mod.v1->postServerSpecialize(mod.v1->impl, args); } + bool valid() const { + return entry.fn && mod.api_version; + } int connectCompanion() const; int getModuleDir() const;