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
12e98735
Commit
12e98735
authored
Sep 22, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update zygisk entry implementation
parent
f7c0e407
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
51 deletions
+62
-51
entry.cpp
native/jni/zygisk/entry.cpp
+62
-51
No files found.
native/jni/zygisk/entry.cpp
View file @
12e98735
...
@@ -39,17 +39,13 @@ void self_unload() {
...
@@ -39,17 +39,13 @@ void self_unload() {
}
}
static
void
*
unload_first_stage
(
void
*
v
)
{
static
void
*
unload_first_stage
(
void
*
v
)
{
//
Setup 1ms
//
Wait 1ms to make sure first stage is completely done
timespec
ts
=
{
.
tv_sec
=
0
,
.
tv_nsec
=
1000000L
};
timespec
ts
=
{
.
tv_sec
=
0
,
.
tv_nsec
=
1000000L
};
while
(
getenv
(
INJECT_ENV_1
))
nanosleep
(
&
ts
,
nullptr
);
nanosleep
(
&
ts
,
nullptr
);
// Wait another 1ms to make sure all threads left our code
auto
path
=
static_cast
<
const
char
*>
(
v
);
nanosleep
(
&
ts
,
nullptr
);
char
*
path
=
static_cast
<
char
*>
(
v
);
unmap_all
(
path
);
unmap_all
(
path
);
free
(
v
);
active_threads
--
;
active_threads
--
;
return
nullptr
;
return
nullptr
;
}
}
...
@@ -60,13 +56,7 @@ static void sanitize_environ() {
...
@@ -60,13 +56,7 @@ static void sanitize_environ() {
char
*
cur
=
environ
[
0
];
char
*
cur
=
environ
[
0
];
for
(
int
i
=
0
;
environ
[
i
];
++
i
)
{
for
(
int
i
=
0
;
environ
[
i
];
++
i
)
{
if
(
str_starts
(
environ
[
i
],
INJECT_ENV_1
"="
))
{
// Copy all env onto the original stack
// This specific env has to live in heap
environ
[
i
]
=
strdup
(
environ
[
i
]);
continue
;
}
// Copy all filtered env onto the original stack
int
len
=
strlen
(
environ
[
i
]);
int
len
=
strlen
(
environ
[
i
]);
memmove
(
cur
,
environ
[
i
],
len
+
1
);
memmove
(
cur
,
environ
[
i
],
len
+
1
);
environ
[
i
]
=
cur
;
environ
[
i
]
=
cur
;
...
@@ -77,7 +67,7 @@ static void sanitize_environ() {
...
@@ -77,7 +67,7 @@ static void sanitize_environ() {
}
}
__attribute__
((
destructor
))
__attribute__
((
destructor
))
static
void
inject
_cleanup_wait
()
{
static
void
zygisk
_cleanup_wait
()
{
if
(
active_threads
<
0
)
if
(
active_threads
<
0
)
return
;
return
;
...
@@ -92,49 +82,70 @@ static void inject_cleanup_wait() {
...
@@ -92,49 +82,70 @@ static void inject_cleanup_wait() {
nanosleep
(
&
ts
,
nullptr
);
nanosleep
(
&
ts
,
nullptr
);
}
}
__attribute__
((
constructor
))
#define SECOND_STAGE_PTR "ZYGISK_PTR"
static
void
inject_init
()
{
if
(
char
*
env
=
getenv
(
INJECT_ENV_2
))
{
static
decltype
(
&
unload_first_stage
)
second_stage_entry
(
void
*
handle
)
{
self_handle
=
handle
;
unsetenv
(
INJECT_ENV_2
);
unsetenv
(
SECOND_STAGE_PTR
);
zygisk_logging
();
zygisk_logging
();
LOGD
(
"zygisk: inject 2nd stage
\n
"
);
LOGD
(
"zygisk: inject 2nd stage
\n
"
);
active_threads
=
1
;
active_threads
=
1
;
unsetenv
(
INJECT_ENV_2
);
// Get our own handle
self_handle
=
dlopen
(
env
,
RTLD_LAZY
);
dlclose
(
self_handle
);
hook_functions
();
hook_functions
();
// Update path to 1st stage lib
*
(
strrchr
(
env
,
'.'
)
-
1
)
=
'1'
;
active_threads
++
;
active_threads
++
;
new_daemon_thread
(
&
unload_first_stage
,
env
);
return
&
unload_first_stage
;
}
else
if
(
getenv
(
INJECT_ENV_1
))
{
}
static
void
first_stage_entry
()
{
android_logging
();
android_logging
();
LOGD
(
"zygisk: inject 1st stage
\n
"
);
LOGD
(
"zygisk: inject 1st stage
\n
"
);
string
ld
=
getenv
(
"LD_PRELOAD"
);
char
*
ld
=
getenv
(
"LD_PRELOAD"
);
char
*
path
;
char
*
path
;
if
(
char
*
c
=
strrchr
(
ld
.
data
()
,
':'
))
{
if
(
char
*
c
=
strrchr
(
ld
,
':'
))
{
*
c
=
'\0'
;
*
c
=
'\0'
;
setenv
(
"LD_PRELOAD"
,
ld
.
data
()
,
1
);
// Restore original LD_PRELOAD
setenv
(
"LD_PRELOAD"
,
ld
,
1
);
// Restore original LD_PRELOAD
path
=
c
+
1
;
path
=
strdup
(
c
+
1
)
;
}
else
{
}
else
{
unsetenv
(
"LD_PRELOAD"
);
unsetenv
(
"LD_PRELOAD"
);
path
=
ld
.
data
(
);
path
=
strdup
(
ld
);
}
}
unsetenv
(
INJECT_ENV_1
);
sanitize_environ
();
sanitize_environ
();
// Update path to 2nd stage lib
// Update path to 2nd stage lib
*
(
strrchr
(
path
,
'.'
)
-
1
)
=
'2'
;
*
(
strrchr
(
path
,
'.'
)
-
1
)
=
'2'
;
// Setup
second stage
// Load
second stage
setenv
(
INJECT_ENV_2
,
path
,
1
);
setenv
(
INJECT_ENV_2
,
"1"
,
1
);
dlopen
(
path
,
RTLD_LAZY
);
void
*
handle
=
dlopen
(
path
,
RTLD_LAZY
);
unsetenv
(
INJECT_ENV_1
);
// Revert path to 1st stage lib
*
(
strrchr
(
path
,
'.'
)
-
1
)
=
'1'
;
// Run second stage entry
char
*
env
=
getenv
(
SECOND_STAGE_PTR
);
decltype
(
&
second_stage_entry
)
second_stage
;
sscanf
(
env
,
"%p"
,
&
second_stage
);
auto
unload
=
second_stage
(
handle
);
// Schedule to unload 1st stage
new_daemon_thread
(
unload
,
path
);
}
__attribute__
((
constructor
))
static
void
zygisk_init
()
{
if
(
getenv
(
INJECT_ENV_2
))
{
// Return function pointer to first stage
char
buf
[
128
];
snprintf
(
buf
,
sizeof
(
buf
),
"%p"
,
&
second_stage_entry
);
setenv
(
SECOND_STAGE_PTR
,
buf
,
1
);
}
else
if
(
getenv
(
INJECT_ENV_1
))
{
first_stage_entry
();
}
}
}
}
...
...
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