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
be573950
Commit
be573950
authored
Apr 21, 2017
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Isolate root daemon from requests
parent
38c867ea
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
51 additions
and
33 deletions
+51
-33
daemon.c
jni/daemon/daemon.c
+14
-4
log_monitor.c
jni/daemon/log_monitor.c
+2
-1
list_manager.c
jni/magiskhide/list_manager.c
+4
-4
magiskhide.c
jni/magiskhide/magiskhide.c
+25
-17
magiskhide.h
jni/magiskhide/magiskhide.h
+2
-2
proc_monitor.c
jni/magiskhide/proc_monitor.c
+4
-5
No files found.
jni/daemon/daemon.c
View file @
be573950
...
...
@@ -23,7 +23,9 @@
pthread_t
sepol_patch
;
static
void
request_handler
(
int
client
)
{
static
void
*
request_handler
(
void
*
args
)
{
int
client
=
*
((
int
*
)
args
);
free
(
args
);
client_request
req
=
read_int
(
client
);
char
*
s
;
int
pid
,
status
,
code
;
...
...
@@ -68,6 +70,7 @@ static void request_handler(int client) {
close
(
client
);
break
;
}
return
NULL
;
}
/* Setup the address and return socket fd */
...
...
@@ -112,12 +115,12 @@ void start_daemon() {
xsetsid
();
xsetcon
(
"u:r:su:s0"
);
// Patch selinux with medium patch
, block
ing
// Patch selinux with medium patch
before we do anyth
ing
load_policydb
(
"/sys/fs/selinux/policy"
);
sepol_med_rules
();
dump_policydb
(
"/sys/fs/selinux/load"
);
// Continue the larger patch in another thread, w
ill
join later
// Continue the larger patch in another thread, w
e will need to
join later
pthread_create
(
&
sepol_patch
,
NULL
,
large_sepol_patch
,
NULL
);
struct
sockaddr_un
sun
;
...
...
@@ -144,11 +147,18 @@ void start_daemon() {
xmount
(
NULL
,
"/"
,
NULL
,
MS_REMOUNT
,
NULL
);
create_links
(
NULL
,
"/sbin"
);
chmod
(
"/sbin"
,
0755
);
mkdir
(
"/magisk"
,
0755
);
chmod
(
"/magisk"
,
0755
);
xmount
(
NULL
,
"/"
,
NULL
,
MS_REMOUNT
|
MS_RDONLY
,
NULL
);
// Loop forever to listen to requests
while
(
1
)
{
request_handler
(
xaccept
(
fd
,
NULL
,
NULL
));
int
*
client
=
xmalloc
(
sizeof
(
int
));
*
client
=
xaccept
(
fd
,
NULL
,
NULL
);
pthread_t
thread
;
xpthread_create
(
&
thread
,
NULL
,
request_handler
,
client
);
// Detach the thread, we will never join it
pthread_detach
(
thread
);
}
}
...
...
jni/daemon/log_monitor.c
View file @
be573950
...
...
@@ -32,5 +32,6 @@ static void *logger_thread(void *args) {
/* Start a new thread to monitor logcat and dump to logfile */
void
monitor_logs
()
{
pthread_t
log_monitor_thread
;
pthread_create
(
&
log_monitor_thread
,
NULL
,
logger_thread
,
NULL
);
xpthread_create
(
&
log_monitor_thread
,
NULL
,
logger_thread
,
NULL
);
pthread_detach
(
log_monitor_thread
);
}
jni/magiskhide/list_manager.c
View file @
be573950
...
...
@@ -37,9 +37,9 @@ int add_list(char *proc) {
ps_filter_proc_name
(
proc
,
kill_proc
);
// Critical region
pthread_mutex_lock
(
&
lock
);
pthread_mutex_lock
(
&
hide_
lock
);
hide_list
=
new_list
;
pthread_mutex_unlock
(
&
lock
);
pthread_mutex_unlock
(
&
hide_
lock
);
// Free old list
vec_destroy
(
temp
);
...
...
@@ -74,9 +74,9 @@ int rm_list(char *proc) {
LOGI
(
"hide_list rm: [%s]
\n
"
,
proc
);
ps_filter_proc_name
(
proc
,
kill_proc
);
// Critical region
pthread_mutex_lock
(
&
lock
);
pthread_mutex_lock
(
&
hide_
lock
);
hide_list
=
new_list
;
pthread_mutex_unlock
(
&
lock
);
pthread_mutex_unlock
(
&
hide_
lock
);
if
(
vector_to_file
(
HIDELIST
,
hide_list
))
return
1
;
}
...
...
jni/magiskhide/magiskhide.c
View file @
be573950
...
...
@@ -23,7 +23,7 @@ struct vector *hide_list = NULL;
int
hideEnabled
=
0
;
static
pthread_t
proc_monitor_thread
;
pthread_mutex_t
lock
;
pthread_mutex_t
hide_
lock
;
void
kill_proc
(
int
pid
)
{
kill
(
pid
,
SIGTERM
);
...
...
@@ -43,12 +43,11 @@ static void usage(char *arg0) {
}
void
launch_magiskhide
(
int
client
)
{
if
(
hideEnabled
)
goto
success
;
/*
* The setns system call do not support multithread processes
* We have to fork a new process, and communicate with pipe
*/
if
(
hideEnabled
)
{
write_int
(
client
,
0
);
close
(
client
);
return
;
}
LOGI
(
"* Starting MagiskHide
\n
"
);
...
...
@@ -60,7 +59,11 @@ void launch_magiskhide(int client) {
if
(
socketpair
(
AF_LOCAL
,
SOCK_STREAM
,
0
,
sv
)
==
-
1
)
goto
error
;
// Launch the hide daemon
/*
* The setns system call do not support multithread processes
* We have to fork a new process, and communicate with sockets
*/
if
(
hide_daemon
())
goto
error
;
...
...
@@ -73,15 +76,18 @@ void launch_magiskhide(int client) {
// Add SafetyNet by default
add_list
(
strdup
(
"com.google.android.gms.unstable"
));
// Start a new thread to monitor processes
pthread_mutex_init
(
&
lock
,
NULL
);
if
(
xpthread_create
(
&
proc_monitor_thread
,
NULL
,
proc_monitor
,
NULL
))
goto
error
;
// Initialize the mutex lock
pthread_mutex_init
(
&
hide_lock
,
NULL
);
success:
write_int
(
client
,
0
);
close
(
client
);
// Get thread reference
proc_monitor_thread
=
pthread_self
();
// Start monitoring
proc_monitor
();
return
;
error:
hideEnabled
=
0
;
write_int
(
client
,
1
);
...
...
@@ -98,16 +104,18 @@ error:
}
void
stop_magiskhide
(
int
client
)
{
if
(
!
hideEnabled
)
if
(
!
hideEnabled
)
{
write_int
(
client
,
0
);
close
(
client
);
return
;
}
LOGI
(
"* Stopping MagiskHide
\n
"
);
pthread_kill
(
proc_monitor_thread
,
SIGUSR1
);
pthread_join
(
proc_monitor_thread
,
NULL
);
hideEnabled
=
0
;
setprop
(
"persist.magisk.hide"
,
"0"
);
pthread_kill
(
proc_monitor_thread
,
SIGUSR1
);
write_int
(
client
,
0
);
close
(
client
);
}
...
...
jni/magiskhide/magiskhide.h
View file @
be573950
...
...
@@ -15,7 +15,7 @@ void kill_proc(int pid);
int
hide_daemon
();
// Process monitor
void
*
proc_monitor
(
void
*
args
);
void
proc_monitor
(
);
// Preprocess
void
manage_selinux
();
...
...
@@ -30,6 +30,6 @@ int destroy_list();
extern
int
sv
[
2
],
hide_pid
,
hideEnabled
;
extern
struct
vector
*
hide_list
;
extern
pthread_mutex_t
lock
;
extern
pthread_mutex_t
hide_
lock
;
#endif
jni/magiskhide/proc_monitor.c
View file @
be573950
...
...
@@ -44,7 +44,7 @@ static void quit_pthread(int sig) {
write
(
sv
[
0
],
&
kill
,
sizeof
(
kill
));
close
(
sv
[
0
]);
waitpid
(
hide_pid
,
NULL
,
0
);
pthread_mutex_destroy
(
&
lock
);
pthread_mutex_destroy
(
&
hide_
lock
);
LOGD
(
"proc_monitor: terminating...
\n
"
);
pthread_exit
(
NULL
);
}
...
...
@@ -63,7 +63,7 @@ static void proc_monitor_err() {
quit_pthread
(
SIGUSR1
);
}
void
*
proc_monitor
(
void
*
args
)
{
void
proc_monitor
(
)
{
// Register the cancel signal
signal
(
SIGUSR1
,
quit_pthread
);
// The error handler should only exit the thread, not the whole process
...
...
@@ -120,7 +120,7 @@ void *proc_monitor(void *args) {
ret
=
0
;
// Critical region
pthread_mutex_lock
(
&
lock
);
pthread_mutex_lock
(
&
hide_
lock
);
vec_for_each
(
hide_list
,
line
)
{
if
(
strcmp
(
processName
,
line
)
==
0
)
{
read_namespace
(
pid
,
buffer
,
32
);
...
...
@@ -152,7 +152,7 @@ void *proc_monitor(void *args) {
break
;
}
}
pthread_mutex_unlock
(
&
lock
);
pthread_mutex_unlock
(
&
hide_
lock
);
if
(
ret
)
{
// Wait hide process to kill itself
...
...
@@ -163,5 +163,4 @@ void *proc_monitor(void *args) {
// Should never be here
quit_pthread
(
SIGUSR1
);
return
NULL
;
}
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