Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
b0198dab
Commit
b0198dab
authored
Oct 14, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update Zygisk logging
parent
b75ec099
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
25 deletions
+35
-25
entry.cpp
native/jni/zygisk/entry.cpp
+3
-3
hook.cpp
native/jni/zygisk/hook.cpp
+22
-22
zygisk.hpp
native/jni/zygisk/zygisk.hpp
+10
-0
No files found.
native/jni/zygisk/entry.cpp
View file @
b0198dab
...
@@ -29,7 +29,7 @@ static void zygisk_logging() {
...
@@ -29,7 +29,7 @@ static void zygisk_logging() {
}
}
void
self_unload
()
{
void
self_unload
()
{
LOGD
(
"zygisk:
Request to self unload
\n
"
);
ZLOGD
(
"
Request to self unload
\n
"
);
// If unhooking failed, do not unload or else it will cause SIGSEGV
// If unhooking failed, do not unload or else it will cause SIGSEGV
if
(
!
unhook_functions
())
if
(
!
unhook_functions
())
return
;
return
;
...
@@ -78,7 +78,7 @@ static void second_stage_entry(void *handle, char *path) {
...
@@ -78,7 +78,7 @@ static void second_stage_entry(void *handle, char *path) {
unsetenv
(
SECOND_STAGE_PTR
);
unsetenv
(
SECOND_STAGE_PTR
);
zygisk_logging
();
zygisk_logging
();
LOGD
(
"zygisk:
inject 2nd stage
\n
"
);
ZLOGD
(
"
inject 2nd stage
\n
"
);
hook_functions
();
hook_functions
();
// Register signal handler to unload 1st stage
// Register signal handler to unload 1st stage
...
@@ -100,7 +100,7 @@ static void second_stage_entry(void *handle, char *path) {
...
@@ -100,7 +100,7 @@ static void second_stage_entry(void *handle, char *path) {
static
void
first_stage_entry
()
{
static
void
first_stage_entry
()
{
android_logging
();
android_logging
();
LOGD
(
"zygisk:
inject 1st stage
\n
"
);
ZLOGD
(
"
inject 1st stage
\n
"
);
char
*
ld
=
getenv
(
"LD_PRELOAD"
);
char
*
ld
=
getenv
(
"LD_PRELOAD"
);
char
*
path
;
char
*
path
;
...
...
native/jni/zygisk/hook.cpp
View file @
b0198dab
...
@@ -16,8 +16,8 @@ using jni_hook::tree_map;
...
@@ -16,8 +16,8 @@ using jni_hook::tree_map;
using
xstring
=
jni_hook
::
string
;
using
xstring
=
jni_hook
::
string
;
// Extreme verbose logging
// Extreme verbose logging
#define
VLOG(...)
LOGD(__VA_ARGS__)
#define
ZLOGV(...) Z
LOGD(__VA_ARGS__)
//#define
VLOG
(...)
//#define
ZLOGV
(...)
namespace
{
namespace
{
...
@@ -83,7 +83,7 @@ if (methods[i].name == #method##sv) { \
...
@@ -83,7 +83,7 @@ if (methods[i].name == #method##sv) { \
jni_hook_list->try_emplace(className).first->second.push_back(methods[i]); \
jni_hook_list->try_emplace(className).first->second.push_back(methods[i]); \
method##_orig = methods[i].fnPtr; \
method##_orig = methods[i].fnPtr; \
newMethods[i] = method##_methods[j]; \
newMethods[i] = method##_methods[j]; \
LOGI("zygisk: replaced %s#" #method "\n", className);
\
ZLOGI("replaced %s#" #method "\n", className);
\
--hook_cnt; \
--hook_cnt; \
break; \
break; \
} \
} \
...
@@ -122,14 +122,14 @@ ret new_##func(__VA_ARGS__)
...
@@ -122,14 +122,14 @@ ret new_##func(__VA_ARGS__)
jint
env_RegisterNatives
(
jint
env_RegisterNatives
(
JNIEnv
*
env
,
jclass
clazz
,
const
JNINativeMethod
*
methods
,
jint
numMethods
)
{
JNIEnv
*
env
,
jclass
clazz
,
const
JNINativeMethod
*
methods
,
jint
numMethods
)
{
auto
className
=
get_class_name
(
env
,
clazz
);
auto
className
=
get_class_name
(
env
,
clazz
);
VLOG
(
"zygisk:
JNIEnv->RegisterNatives [%s]
\n
"
,
className
.
data
());
ZLOGV
(
"
JNIEnv->RegisterNatives [%s]
\n
"
,
className
.
data
());
auto
newMethods
=
hookAndSaveJNIMethods
(
className
.
data
(),
methods
,
numMethods
);
auto
newMethods
=
hookAndSaveJNIMethods
(
className
.
data
(),
methods
,
numMethods
);
return
old_functions
->
RegisterNatives
(
env
,
clazz
,
newMethods
.
get
()
?:
methods
,
numMethods
);
return
old_functions
->
RegisterNatives
(
env
,
clazz
,
newMethods
.
get
()
?:
methods
,
numMethods
);
}
}
DCL_HOOK_FUNC
(
int
,
jniRegisterNativeMethods
,
DCL_HOOK_FUNC
(
int
,
jniRegisterNativeMethods
,
JNIEnv
*
env
,
const
char
*
className
,
const
JNINativeMethod
*
methods
,
int
numMethods
)
{
JNIEnv
*
env
,
const
char
*
className
,
const
JNINativeMethod
*
methods
,
int
numMethods
)
{
VLOG
(
"zygisk:
jniRegisterNativeMethods [%s]
\n
"
,
className
);
ZLOGV
(
"
jniRegisterNativeMethods [%s]
\n
"
,
className
);
auto
newMethods
=
hookAndSaveJNIMethods
(
className
,
methods
,
numMethods
);
auto
newMethods
=
hookAndSaveJNIMethods
(
className
,
methods
,
numMethods
);
return
old_jniRegisterNativeMethods
(
env
,
className
,
newMethods
.
get
()
?:
methods
,
numMethods
);
return
old_jniRegisterNativeMethods
(
env
,
className
,
newMethods
.
get
()
?:
methods
,
numMethods
);
}
}
...
@@ -144,7 +144,7 @@ DCL_HOOK_FUNC(int, selinux_android_setcontext,
...
@@ -144,7 +144,7 @@ DCL_HOOK_FUNC(int, selinux_android_setcontext,
uid_t
uid
,
int
isSystemServer
,
const
char
*
seinfo
,
const
char
*
pkgname
)
{
uid_t
uid
,
int
isSystemServer
,
const
char
*
seinfo
,
const
char
*
pkgname
)
{
if
(
g_ctx
&&
g_ctx
->
flags
[
UNMOUNT_FLAG
])
{
if
(
g_ctx
&&
g_ctx
->
flags
[
UNMOUNT_FLAG
])
{
if
(
remote_request_unmount
()
==
0
)
{
if
(
remote_request_unmount
()
==
0
)
{
LOGD
(
"zygisk:
mount namespace cleaned up
\n
"
);
ZLOGD
(
"
mount namespace cleaned up
\n
"
);
}
}
}
}
return
old_selinux_android_setcontext
(
uid
,
isSystemServer
,
seinfo
,
pkgname
);
return
old_selinux_android_setcontext
(
uid
,
isSystemServer
,
seinfo
,
pkgname
);
...
@@ -167,7 +167,7 @@ void **gAppRuntimeVTable;
...
@@ -167,7 +167,7 @@ void **gAppRuntimeVTable;
// This method is a trampoline for hooking JNIEnv->RegisterNatives
// This method is a trampoline for hooking JNIEnv->RegisterNatives
void
onVmCreated
(
void
*
self
,
JNIEnv
*
env
)
{
void
onVmCreated
(
void
*
self
,
JNIEnv
*
env
)
{
LOGD
(
"zygisk:
AppRuntime::onVmCreated
\n
"
);
ZLOGD
(
"
AppRuntime::onVmCreated
\n
"
);
// Restore virtual table
// Restore virtual table
auto
new_table
=
*
reinterpret_cast
<
void
***>
(
self
);
auto
new_table
=
*
reinterpret_cast
<
void
***>
(
self
);
...
@@ -199,7 +199,7 @@ DCL_HOOK_FUNC(void, setArgv0, void *self, const char *argv0, bool setProcName) {
...
@@ -199,7 +199,7 @@ DCL_HOOK_FUNC(void, setArgv0, void *self, const char *argv0, bool setProcName) {
return
;
return
;
}
}
LOGD
(
"zygisk:
AndroidRuntime::setArgv0
\n
"
);
ZLOGD
(
"
AndroidRuntime::setArgv0
\n
"
);
// We don't know which entry is onVmCreated, so overwrite every one
// We don't know which entry is onVmCreated, so overwrite every one
// We also don't know the size of the vtable, but 8 is more than enough
// We also don't know the size of the vtable, but 8 is more than enough
...
@@ -347,14 +347,14 @@ void HookContext::nativeSpecializeAppProcess_pre() {
...
@@ -347,14 +347,14 @@ void HookContext::nativeSpecializeAppProcess_pre() {
flags
[
APP_SPECIALIZE
]
=
true
;
flags
[
APP_SPECIALIZE
]
=
true
;
process
=
env
->
GetStringUTFChars
(
args
->
nice_name
,
nullptr
);
process
=
env
->
GetStringUTFChars
(
args
->
nice_name
,
nullptr
);
if
(
flags
[
FORK_AND_SPECIALIZE
])
{
if
(
flags
[
FORK_AND_SPECIALIZE
])
{
VLOG
(
"zygisk:
pre forkAndSpecialize [%s]
\n
"
,
process
);
ZLOGV
(
"
pre forkAndSpecialize [%s]
\n
"
,
process
);
}
else
{
}
else
{
VLOG
(
"zygisk:
pre specialize [%s]
\n
"
,
process
);
ZLOGV
(
"
pre specialize [%s]
\n
"
,
process
);
}
}
auto
module_fds
=
remote_get_info
(
args
->
uid
,
process
,
&
info
);
auto
module_fds
=
remote_get_info
(
args
->
uid
,
process
,
&
info
);
if
(
info
.
on_denylist
)
{
if
(
info
.
on_denylist
)
{
LOGI
(
"zygisk:
[%s] is on the denylist
\n
"
,
process
);
ZLOGI
(
"
[%s] is on the denylist
\n
"
,
process
);
toggle_unmount
();
toggle_unmount
();
}
else
{
}
else
{
run_modules_pre
(
module_fds
);
run_modules_pre
(
module_fds
);
...
@@ -363,9 +363,9 @@ void HookContext::nativeSpecializeAppProcess_pre() {
...
@@ -363,9 +363,9 @@ void HookContext::nativeSpecializeAppProcess_pre() {
void
HookContext
::
nativeSpecializeAppProcess_post
()
{
void
HookContext
::
nativeSpecializeAppProcess_post
()
{
if
(
flags
[
FORK_AND_SPECIALIZE
])
{
if
(
flags
[
FORK_AND_SPECIALIZE
])
{
VLOG
(
"zygisk:
post forkAndSpecialize [%s]
\n
"
,
process
);
ZLOGV
(
"
post forkAndSpecialize [%s]
\n
"
,
process
);
}
else
{
}
else
{
VLOG
(
"zygisk:
post specialize [%s]
\n
"
,
process
);
ZLOGV
(
"
post specialize [%s]
\n
"
,
process
);
}
}
env
->
ReleaseStringUTFChars
(
args
->
nice_name
,
process
);
env
->
ReleaseStringUTFChars
(
args
->
nice_name
,
process
);
...
@@ -388,7 +388,7 @@ void HookContext::nativeForkSystemServer_pre() {
...
@@ -388,7 +388,7 @@ void HookContext::nativeForkSystemServer_pre() {
fork_pre
();
fork_pre
();
flags
[
SERVER_SPECIALIZE
]
=
true
;
flags
[
SERVER_SPECIALIZE
]
=
true
;
if
(
pid
==
0
)
{
if
(
pid
==
0
)
{
VLOG
(
"zygisk:
pre forkSystemServer
\n
"
);
ZLOGV
(
"
pre forkSystemServer
\n
"
);
run_modules_pre
(
remote_get_info
(
1000
,
"system_server"
,
&
info
));
run_modules_pre
(
remote_get_info
(
1000
,
"system_server"
,
&
info
));
close_fds
();
close_fds
();
}
}
...
@@ -397,7 +397,7 @@ void HookContext::nativeForkSystemServer_pre() {
...
@@ -397,7 +397,7 @@ void HookContext::nativeForkSystemServer_pre() {
void
HookContext
::
nativeForkSystemServer_post
()
{
void
HookContext
::
nativeForkSystemServer_post
()
{
if
(
pid
==
0
)
{
if
(
pid
==
0
)
{
android_logging
();
android_logging
();
VLOG
(
"zygisk:
post forkSystemServer
\n
"
);
ZLOGV
(
"
post forkSystemServer
\n
"
);
run_modules_post
();
run_modules_post
();
}
}
fork_post
();
fork_post
();
...
@@ -446,10 +446,10 @@ void HookContext::fork_post() {
...
@@ -446,10 +446,10 @@ void HookContext::fork_post() {
static
bool
hook_refresh
()
{
static
bool
hook_refresh
()
{
if
(
xhook_refresh
(
0
)
==
0
)
{
if
(
xhook_refresh
(
0
)
==
0
)
{
xhook_clear
();
xhook_clear
();
LOGI
(
"zygisk:
xhook success
\n
"
);
ZLOGI
(
"
xhook success
\n
"
);
return
true
;
return
true
;
}
else
{
}
else
{
LOGE
(
"zygisk:
xhook failed
\n
"
);
ZLOGE
(
"
xhook failed
\n
"
);
return
false
;
return
false
;
}
}
}
}
...
@@ -457,7 +457,7 @@ static bool hook_refresh() {
...
@@ -457,7 +457,7 @@ static bool hook_refresh() {
static
int
hook_register
(
const
char
*
path
,
const
char
*
symbol
,
void
*
new_func
,
void
**
old_func
)
{
static
int
hook_register
(
const
char
*
path
,
const
char
*
symbol
,
void
*
new_func
,
void
**
old_func
)
{
int
ret
=
xhook_register
(
path
,
symbol
,
new_func
,
old_func
);
int
ret
=
xhook_register
(
path
,
symbol
,
new_func
,
old_func
);
if
(
ret
!=
0
)
{
if
(
ret
!=
0
)
{
LOGE
(
"hook:
Failed to register hook
\"
%s
\"\n
"
,
symbol
);
ZLOGE
(
"
Failed to register hook
\"
%s
\"\n
"
,
symbol
);
return
ret
;
return
ret
;
}
}
xhook_list
->
emplace_back
(
path
,
symbol
,
old_func
);
xhook_list
->
emplace_back
(
path
,
symbol
,
old_func
);
...
@@ -495,7 +495,7 @@ void hook_functions() {
...
@@ -495,7 +495,7 @@ void hook_functions() {
xhook_list
->
end
());
xhook_list
->
end
());
if
(
old_jniRegisterNativeMethods
==
nullptr
)
{
if
(
old_jniRegisterNativeMethods
==
nullptr
)
{
LOGD
(
"zygisk:
jniRegisterNativeMethods not hooked, using fallback
\n
"
);
ZLOGD
(
"
jniRegisterNativeMethods not hooked, using fallback
\n
"
);
// android::AndroidRuntime::setArgv0(const char*, bool)
// android::AndroidRuntime::setArgv0(const char*, bool)
XHOOK_REGISTER_SYM
(
APP_PROCESS
,
"_ZN7android14AndroidRuntime8setArgv0EPKcb"
,
setArgv0
);
XHOOK_REGISTER_SYM
(
APP_PROCESS
,
"_ZN7android14AndroidRuntime8setArgv0EPKcb"
,
setArgv0
);
...
@@ -530,7 +530,7 @@ bool unhook_functions() {
...
@@ -530,7 +530,7 @@ bool unhook_functions() {
for
(
const
auto
&
[
clz
,
methods
]
:
*
jni_hook_list
)
{
for
(
const
auto
&
[
clz
,
methods
]
:
*
jni_hook_list
)
{
if
(
!
methods
.
empty
()
&&
old_jniRegisterNativeMethods
(
if
(
!
methods
.
empty
()
&&
old_jniRegisterNativeMethods
(
g_ctx
->
env
,
clz
.
data
(),
methods
.
data
(),
methods
.
size
())
!=
0
)
{
g_ctx
->
env
,
clz
.
data
(),
methods
.
data
(),
methods
.
size
())
!=
0
)
{
LOGE
(
"zygisk:
Failed to restore JNI hook of class [%s]
\n
"
,
clz
.
data
());
ZLOGE
(
"
Failed to restore JNI hook of class [%s]
\n
"
,
clz
.
data
());
success
=
false
;
success
=
false
;
}
}
}
}
...
@@ -539,13 +539,13 @@ bool unhook_functions() {
...
@@ -539,13 +539,13 @@ bool unhook_functions() {
// Unhook xhook
// Unhook xhook
for
(
const
auto
&
[
path
,
sym
,
old_func
]
:
*
xhook_list
)
{
for
(
const
auto
&
[
path
,
sym
,
old_func
]
:
*
xhook_list
)
{
if
(
xhook_register
(
path
,
sym
,
*
old_func
,
nullptr
)
!=
0
)
{
if
(
xhook_register
(
path
,
sym
,
*
old_func
,
nullptr
)
!=
0
)
{
LOGE
(
"zygisk:
Failed to register xhook [%s]
\n
"
,
sym
);
ZLOGE
(
"
Failed to register xhook [%s]
\n
"
,
sym
);
success
=
false
;
success
=
false
;
}
}
}
}
delete
xhook_list
;
delete
xhook_list
;
if
(
!
hook_refresh
())
{
if
(
!
hook_refresh
())
{
LOGE
(
"zygisk:
Failed to restore xhook
\n
"
);
ZLOGE
(
"
Failed to restore xhook
\n
"
);
success
=
false
;
success
=
false
;
}
}
...
...
native/jni/zygisk/zygisk.hpp
View file @
b0198dab
...
@@ -14,6 +14,16 @@ enum : int {
...
@@ -14,6 +14,16 @@ enum : int {
ZYGISK_GET_LOG_PIPE
,
ZYGISK_GET_LOG_PIPE
,
};
};
#if defined(__LP64__)
#define ZLOGD(...) LOGD("zygisk64: " __VA_ARGS__)
#define ZLOGE(...) LOGE("zygisk64: " __VA_ARGS__)
#define ZLOGI(...) LOGI("zygisk64: " __VA_ARGS__)
#else
#define ZLOGD(...) LOGD("zygisk32: " __VA_ARGS__)
#define ZLOGE(...) LOGE("zygisk32: " __VA_ARGS__)
#define ZLOGI(...) LOGI("zygisk32: " __VA_ARGS__)
#endif
// Unmap all pages matching the name
// Unmap all pages matching the name
void
unmap_all
(
const
char
*
name
);
void
unmap_all
(
const
char
*
name
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment