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
ef98eaed
Commit
ef98eaed
authored
Jan 07, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Proper injection entry and unloading
parent
2a257f32
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
20 deletions
+24
-20
entry.cpp
native/jni/inject/entry.cpp
+24
-20
No files found.
native/jni/inject/entry.cpp
View file @
ef98eaed
...
@@ -50,7 +50,7 @@ static void *unload_first_stage(void *) {
...
@@ -50,7 +50,7 @@ static void *unload_first_stage(void *) {
// Setup 1ms
// Setup 1ms
timespec
ts
=
{
.
tv_sec
=
0
,
.
tv_nsec
=
1000000L
};
timespec
ts
=
{
.
tv_sec
=
0
,
.
tv_nsec
=
1000000L
};
while
(
getenv
(
INJECT_ENV_
2
))
while
(
getenv
(
INJECT_ENV_
1
))
nanosleep
(
&
ts
,
nullptr
);
nanosleep
(
&
ts
,
nullptr
);
// Wait another 1ms to make sure all threads left our code
// Wait another 1ms to make sure all threads left our code
...
@@ -67,6 +67,8 @@ static void sanitize_environ() {
...
@@ -67,6 +67,8 @@ static void sanitize_environ() {
static
string
env
;
static
string
env
;
for
(
int
i
=
0
;
environ
[
i
];
++
i
)
{
for
(
int
i
=
0
;
environ
[
i
];
++
i
)
{
if
(
str_starts
(
environ
[
i
],
INJECT_ENV_1
"="
))
continue
;
env
+=
environ
[
i
];
env
+=
environ
[
i
];
env
+=
'\0'
;
env
+=
'\0'
;
}
}
...
@@ -83,37 +85,39 @@ static void sanitize_environ() {
...
@@ -83,37 +85,39 @@ static void sanitize_environ() {
__attribute__
((
constructor
))
__attribute__
((
constructor
))
static
void
inject_init
()
{
static
void
inject_init
()
{
inject_logging
();
inject_logging
();
if
(
char
*
env
=
getenv
(
INJECT_ENV_1
))
{
LOGD
(
"zygote: inject 1st stage
\n
"
);
if
(
env
[
0
]
==
'1'
)
unsetenv
(
"LD_PRELOAD"
);
else
setenv
(
"LD_PRELOAD"
,
env
,
1
);
// Restore original LD_PRELOAD
unsetenv
(
INJECT_ENV_1
);
// Setup second stage
if
(
getenv
(
INJECT_ENV_2
))
{
setenv
(
INJECT_ENV_2
,
"1"
,
1
);
cp_afc
(
INJECT_LIB_1
,
INJECT_LIB_2
);
dlopen
(
INJECT_LIB_2
,
RTLD_LAZY
);
}
else
if
(
getenv
(
INJECT_ENV_2
))
{
LOGD
(
"zygote: inject 2nd stage
\n
"
);
LOGD
(
"zygote: inject 2nd stage
\n
"
);
active_threads
=
1
;
active_threads
=
1
;
unsetenv
(
INJECT_ENV_2
);
// Get our own handle
// Get our own handle
self_handle
=
dlopen
(
INJECT_LIB_2
,
RTLD_LAZY
);
self_handle
=
dlopen
(
INJECT_LIB_2
,
RTLD_LAZY
);
dlclose
(
self_handle
);
dlclose
(
self_handle
);
// Cleanup 1st stage maps
// TODO: actually inject stuffs here
active_threads
++
;
new_daemon_thread
(
&
unload_first_stage
);
unsetenv
(
INJECT_ENV_2
);
// Some cleanup
sanitize_environ
();
sanitize_environ
();
active_threads
++
;
new_daemon_thread
(
&
unload_first_stage
);
//
TODO: actually inject stuffs, for now we demonstrate clean self unloading
//
Demonstrate self unloading 2nd stage
self_unload
();
self_unload
();
}
else
if
(
char
*
env
=
getenv
(
INJECT_ENV_1
))
{
LOGD
(
"zygote: inject 1st stage
\n
"
);
if
(
env
[
0
]
==
'1'
)
unsetenv
(
"LD_PRELOAD"
);
else
setenv
(
"LD_PRELOAD"
,
env
,
1
);
// Restore original LD_PRELOAD
// Setup second stage
setenv
(
INJECT_ENV_2
,
"1"
,
1
);
cp_afc
(
INJECT_LIB_1
,
INJECT_LIB_2
);
dlopen
(
INJECT_LIB_2
,
RTLD_LAZY
);
unsetenv
(
INJECT_ENV_1
);
}
}
}
}
...
...
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