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
6f9c3c4f
Commit
6f9c3c4f
authored
Aug 19, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor hook.cpp
parent
9b3efffb
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
219 additions
and
168 deletions
+219
-168
misc.hpp
native/jni/utils/misc.hpp
+2
-2
entry.cpp
native/jni/zygisk/entry.cpp
+4
-4
gen_jni_hooks.py
native/jni/zygisk/gen_jni_hooks.py
+8
-6
hook.cpp
native/jni/zygisk/hook.cpp
+113
-94
jni_hooks.hpp
native/jni/zygisk/jni_hooks.hpp
+92
-62
No files found.
native/jni/utils/misc.hpp
View file @
6f9c3c4f
...
@@ -33,10 +33,10 @@ template <class Func>
...
@@ -33,10 +33,10 @@ template <class Func>
class
run_finally
{
class
run_finally
{
DISALLOW_COPY_AND_MOVE
(
run_finally
)
DISALLOW_COPY_AND_MOVE
(
run_finally
)
public
:
public
:
explicit
run_finally
(
const
Func
&
fn
)
:
fn
(
fn
)
{}
explicit
run_finally
(
Func
&&
fn
)
:
fn
(
std
::
move
(
fn
)
)
{}
~
run_finally
()
{
fn
();
}
~
run_finally
()
{
fn
();
}
private
:
private
:
const
Func
&
fn
;
Func
fn
;
};
};
template
<
typename
T
>
template
<
typename
T
>
...
...
native/jni/zygisk/entry.cpp
View file @
6f9c3c4f
...
@@ -16,8 +16,8 @@ static void *self_handle = nullptr;
...
@@ -16,8 +16,8 @@ static void *self_handle = nullptr;
static
atomic
<
int
>
active_threads
=
-
1
;
static
atomic
<
int
>
active_threads
=
-
1
;
void
self_unload
()
{
void
self_unload
()
{
LOGD
(
"
hoo
k: Request to self unload
\n
"
);
LOGD
(
"
zygis
k: Request to self unload
\n
"
);
// If
unhook
failed, do not unload or else it will cause SIGSEGV
// If
deny
failed, do not unload or else it will cause SIGSEGV
if
(
!
unhook_functions
())
if
(
!
unhook_functions
())
return
;
return
;
new_daemon_thread
(
reinterpret_cast
<
thread_entry
>
(
&
dlclose
),
self_handle
);
new_daemon_thread
(
reinterpret_cast
<
thread_entry
>
(
&
dlclose
),
self_handle
);
...
@@ -81,7 +81,7 @@ __attribute__((constructor))
...
@@ -81,7 +81,7 @@ __attribute__((constructor))
static
void
inject_init
()
{
static
void
inject_init
()
{
if
(
char
*
env
=
getenv
(
INJECT_ENV_2
))
{
if
(
char
*
env
=
getenv
(
INJECT_ENV_2
))
{
android_logging
();
android_logging
();
LOGD
(
"zyg
ote
: inject 2nd stage
\n
"
);
LOGD
(
"zyg
isk
: inject 2nd stage
\n
"
);
active_threads
=
1
;
active_threads
=
1
;
unsetenv
(
INJECT_ENV_2
);
unsetenv
(
INJECT_ENV_2
);
...
@@ -100,7 +100,7 @@ static void inject_init() {
...
@@ -100,7 +100,7 @@ static void inject_init() {
new_daemon_thread
(
&
unload_first_stage
,
env
);
new_daemon_thread
(
&
unload_first_stage
,
env
);
}
else
if
(
getenv
(
INJECT_ENV_1
))
{
}
else
if
(
getenv
(
INJECT_ENV_1
))
{
android_logging
();
android_logging
();
LOGD
(
"zyg
ote
: inject 1st stage
\n
"
);
LOGD
(
"zyg
isk
: inject 1st stage
\n
"
);
char
*
ld
=
getenv
(
"LD_PRELOAD"
);
char
*
ld
=
getenv
(
"LD_PRELOAD"
);
char
*
path
;
char
*
path
;
...
...
native/jni/zygisk/gen_jni_hooks.py
View file @
6f9c3c4f
...
@@ -128,7 +128,7 @@ spec_r = Method('r', [uid, gid, gids, runtime_flags, rlimits, mount_external, se
...
@@ -128,7 +128,7 @@ spec_r = Method('r', [uid, gid, gids, runtime_flags, rlimits, mount_external, se
spec_samsung_q
=
Method
(
'samsung_q'
,
[
uid
,
gid
,
gids
,
runtime_flags
,
rlimits
,
mount_external
,
spec_samsung_q
=
Method
(
'samsung_q'
,
[
uid
,
gid
,
gids
,
runtime_flags
,
rlimits
,
mount_external
,
se_info
,
i1
,
i2
,
nice_name
,
is_child_zygote
,
instruction_set
,
app_data_dir
])
se_info
,
i1
,
i2
,
nice_name
,
is_child_zygote
,
instruction_set
,
app_data_dir
])
server_
m
=
Method
(
'm
'
,
[
uid
,
gid
,
gids
,
runtime_flags
,
rlimits
,
server_
l
=
Method
(
'l
'
,
[
uid
,
gid
,
gids
,
runtime_flags
,
rlimits
,
permitted_capabilities
,
effective_capabilities
])
permitted_capabilities
,
effective_capabilities
])
server_samsung_q
=
Method
(
'samsung_q'
,
[
uid
,
gid
,
gids
,
runtime_flags
,
i1
,
i2
,
rlimits
,
server_samsung_q
=
Method
(
'samsung_q'
,
[
uid
,
gid
,
gids
,
runtime_flags
,
i1
,
i2
,
rlimits
,
...
@@ -151,7 +151,6 @@ def gen_definitions(methods, base_name):
...
@@ -151,7 +151,6 @@ def gen_definitions(methods, base_name):
for
m
in
methods
:
for
m
in
methods
:
func_name
=
f
'{base_name}_{m.name}'
func_name
=
f
'{base_name}_{m.name}'
decl
+=
ind
(
0
)
+
f
'{cpp_ret} {func_name}(JNIEnv *env, jclass clazz, {m.cpp()}) {{'
decl
+=
ind
(
0
)
+
f
'{cpp_ret} {func_name}(JNIEnv *env, jclass clazz, {m.cpp()}) {{'
decl
+=
ind
(
1
)
+
'HookContext ctx{};'
if
base_name
==
'nativeForkSystemServer'
:
if
base_name
==
'nativeForkSystemServer'
:
decl
+=
ind
(
1
)
+
'ForkSystemServerArgs args(uid, gid, gids, runtime_flags, permitted_capabilities, effective_capabilities);'
decl
+=
ind
(
1
)
+
'ForkSystemServerArgs args(uid, gid, gids, runtime_flags, permitted_capabilities, effective_capabilities);'
else
:
else
:
...
@@ -159,12 +158,15 @@ def gen_definitions(methods, base_name):
...
@@ -159,12 +158,15 @@ def gen_definitions(methods, base_name):
for
a
in
m
.
args
:
for
a
in
m
.
args
:
if
a
.
set_arg
:
if
a
.
set_arg
:
decl
+=
ind
(
1
)
+
f
'args.{a.name} = &{a.name};'
decl
+=
ind
(
1
)
+
f
'args.{a.name} = &{a.name};'
decl
+=
ind
(
1
)
+
'HookContext ctx;'
decl
+=
ind
(
1
)
+
'ctx.env = env;'
decl
+=
ind
(
1
)
+
'ctx.clazz = clazz;'
decl
+=
ind
(
1
)
+
'ctx.raw_args = &args;'
decl
+=
ind
(
1
)
+
'ctx.raw_args = &args;'
decl
+=
ind
(
1
)
+
f
'
{base_name}_pre(&ctx, env, clazz
);'
decl
+=
ind
(
1
)
+
f
'
ctx.{base_name}_pre(
);'
decl
+=
ind
(
1
)
+
f
'reinterpret_cast<decltype(&{func_name})>({base_name}_orig)('
decl
+=
ind
(
1
)
+
f
'reinterpret_cast<decltype(&{func_name})>(
HookContext::
{base_name}_orig)('
decl
+=
ind
(
2
)
+
f
'env, clazz, {m.name_list()}'
decl
+=
ind
(
2
)
+
f
'env, clazz, {m.name_list()}'
decl
+=
ind
(
1
)
+
');'
decl
+=
ind
(
1
)
+
');'
decl
+=
ind
(
1
)
+
f
'
{base_name}_post(&ctx, env, clazz
);'
decl
+=
ind
(
1
)
+
f
'
ctx.{base_name}_post(
);'
decl
+=
ret_stat
decl
+=
ret_stat
decl
+=
ind
(
0
)
+
'}'
decl
+=
ind
(
0
)
+
'}'
...
@@ -189,7 +191,7 @@ def gen_spec():
...
@@ -189,7 +191,7 @@ def gen_spec():
return
gen_definitions
(
methods
,
'nativeSpecializeAppProcess'
)
return
gen_definitions
(
methods
,
'nativeSpecializeAppProcess'
)
def
gen_server
():
def
gen_server
():
methods
=
[
server_
m
,
server_samsung_q
]
methods
=
[
server_
l
,
server_samsung_q
]
return
gen_definitions
(
methods
,
'nativeForkSystemServer'
)
return
gen_definitions
(
methods
,
'nativeForkSystemServer'
)
with
open
(
'jni_hooks.hpp'
,
'w'
)
as
f
:
with
open
(
'jni_hooks.hpp'
,
'w'
)
as
f
:
...
...
native/jni/zygisk/hook.cpp
View file @
6f9c3c4f
This diff is collapsed.
Click to expand it.
native/jni/zygisk/jni_hooks.hpp
View file @
6f9c3c4f
This diff is collapsed.
Click to expand it.
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