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
b36e6d98
Commit
b36e6d98
authored
Jan 11, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reorganize MagiskHide code
Prepare for zygote injection hiding
parent
53c3dd5e
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
77 deletions
+73
-77
hide_utils.cpp
native/jni/magiskhide/hide_utils.cpp
+43
-18
magiskhide.hpp
native/jni/magiskhide/magiskhide.hpp
+3
-4
proc_monitor.cpp
native/jni/magiskhide/proc_monitor.cpp
+13
-49
misc.cpp
native/jni/utils/misc.cpp
+13
-6
misc.hpp
native/jni/utils/misc.hpp
+1
-0
No files found.
native/jni/magiskhide/hide_utils.cpp
View file @
b36e6d98
...
@@ -15,11 +15,43 @@
...
@@ -15,11 +15,43 @@
using
namespace
std
;
using
namespace
std
;
static
pthread_t
proc_
monitor_thread
;
static
pthread_t
monitor_thread
;
static
bool
hide_state
=
false
;
static
bool
hide_state
=
false
;
static
set
<
pair
<
string
,
string
>>
hide_set
;
/* set of <pkg, process> pair */
// This locks the 2 variables above
map
<
int
,
vector
<
string_view
>>
uid_proc_map
;
/* uid -> list of process */
static
pthread_mutex_t
hide_state_lock
=
PTHREAD_MUTEX_INITIALIZER
;
// Locks the variables above
pthread_mutex_t
hide_state_lock
=
PTHREAD_MUTEX_INITIALIZER
;
void
update_uid_map
()
{
mutex_guard
lock
(
hide_state_lock
);
uid_proc_map
.
clear
();
string
data_path
(
APP_DATA_DIR
);
size_t
len
=
data_path
.
length
();
auto
dir
=
open_dir
(
APP_DATA_DIR
);
bool
first_iter
=
true
;
for
(
dirent
*
entry
;
(
entry
=
xreaddir
(
dir
.
get
()));)
{
data_path
.
resize
(
len
);
data_path
+=
'/'
;
data_path
+=
entry
->
d_name
;
// multiuser user id
data_path
+=
'/'
;
size_t
user_len
=
data_path
.
length
();
struct
stat
st
;
for
(
auto
&
hide
:
hide_set
)
{
if
(
hide
.
first
==
ISOLATED_MAGIC
)
{
if
(
!
first_iter
)
continue
;
// Setup isolated processes
uid_proc_map
[
-
1
].
emplace_back
(
hide
.
second
);
}
data_path
.
resize
(
user_len
);
data_path
+=
hide
.
first
;
if
(
stat
(
data_path
.
data
(),
&
st
))
continue
;
uid_proc_map
[
st
.
st_uid
].
emplace_back
(
hide
.
second
);
}
first_iter
=
false
;
}
}
// Leave /proc fd opened as we're going to read from it repeatedly
// Leave /proc fd opened as we're going to read from it repeatedly
static
DIR
*
procfp
;
static
DIR
*
procfp
;
...
@@ -43,11 +75,6 @@ bool hide_enabled() {
...
@@ -43,11 +75,6 @@ bool hide_enabled() {
return
hide_state
;
return
hide_state
;
}
}
void
set_hide_state
(
bool
state
)
{
mutex_guard
g
(
hide_state_lock
);
hide_state
=
state
;
}
template
<
bool
str_op
(
string_view
,
string_view
)
>
template
<
bool
str_op
(
string_view
,
string_view
)
>
static
bool
proc_name_match
(
int
pid
,
const
char
*
name
)
{
static
bool
proc_name_match
(
int
pid
,
const
char
*
name
)
{
char
buf
[
4019
];
char
buf
[
4019
];
...
@@ -124,7 +151,7 @@ static int add_list(const char *pkg, const char *proc) {
...
@@ -124,7 +151,7 @@ static int add_list(const char *pkg, const char *proc) {
{
{
// Critical region
// Critical region
mutex_guard
lock
(
monitor
_lock
);
mutex_guard
lock
(
hide_state
_lock
);
add_hide_set
(
pkg
,
proc
);
add_hide_set
(
pkg
,
proc
);
}
}
...
@@ -146,7 +173,7 @@ static int rm_list(const char *pkg, const char *proc) {
...
@@ -146,7 +173,7 @@ static int rm_list(const char *pkg, const char *proc) {
bool
remove
=
false
;
bool
remove
=
false
;
{
{
// Critical region
// Critical region
mutex_guard
lock
(
monitor
_lock
);
mutex_guard
lock
(
hide_state
_lock
);
for
(
auto
it
=
hide_set
.
begin
();
it
!=
hide_set
.
end
();)
{
for
(
auto
it
=
hide_set
.
begin
();
it
!=
hide_set
.
end
();)
{
if
(
it
->
first
==
pkg
&&
(
proc
[
0
]
==
'\0'
||
it
->
second
==
proc
))
{
if
(
it
->
first
==
pkg
&&
(
proc
[
0
]
==
'\0'
||
it
->
second
==
proc
))
{
remove
=
true
;
remove
=
true
;
...
@@ -256,9 +283,6 @@ int launch_magiskhide() {
...
@@ -256,9 +283,6 @@ int launch_magiskhide() {
LOGI
(
"* Starting MagiskHide
\n
"
);
LOGI
(
"* Starting MagiskHide
\n
"
);
// Initialize the mutex lock
pthread_mutex_init
(
&
monitor_lock
,
nullptr
);
// Initialize the hide list
// Initialize the hide list
if
(
!
init_list
())
if
(
!
init_list
())
return
DAEMON_ERROR
;
return
DAEMON_ERROR
;
...
@@ -268,8 +292,7 @@ int launch_magiskhide() {
...
@@ -268,8 +292,7 @@ int launch_magiskhide() {
hide_late_sensitive_props
();
hide_late_sensitive_props
();
// Start monitoring
// Start monitoring
void
*
(
*
start
)(
void
*
)
=
[](
void
*
)
->
void
*
{
proc_monitor
();
};
if
(
new_daemon_thread
(
&
proc_monitor
))
if
(
xpthread_create
(
&
proc_monitor_thread
,
nullptr
,
start
,
nullptr
))
return
DAEMON_ERROR
;
return
DAEMON_ERROR
;
hide_state
=
true
;
hide_state
=
true
;
...
@@ -282,7 +305,9 @@ int stop_magiskhide() {
...
@@ -282,7 +305,9 @@ int stop_magiskhide() {
if
(
hide_state
)
{
if
(
hide_state
)
{
LOGI
(
"* Stopping MagiskHide
\n
"
);
LOGI
(
"* Stopping MagiskHide
\n
"
);
pthread_kill
(
proc_monitor_thread
,
SIGTERMTHRD
);
uid_proc_map
.
clear
();
hide_set
.
clear
();
pthread_kill
(
monitor_thread
,
SIGTERMTHRD
);
}
}
hide_state
=
false
;
hide_state
=
false
;
...
@@ -292,7 +317,7 @@ int stop_magiskhide() {
...
@@ -292,7 +317,7 @@ int stop_magiskhide() {
void
auto_start_magiskhide
()
{
void
auto_start_magiskhide
()
{
if
(
hide_enabled
())
{
if
(
hide_enabled
())
{
pthread_kill
(
proc_
monitor_thread
,
SIGALRM
);
pthread_kill
(
monitor_thread
,
SIGALRM
);
hide_late_sensitive_props
();
hide_late_sensitive_props
();
}
else
if
(
SDK_INT
>=
19
)
{
}
else
if
(
SDK_INT
>=
19
)
{
db_settings
dbs
;
db_settings
dbs
;
...
...
native/jni/magiskhide/magiskhide.hpp
View file @
b36e6d98
...
@@ -25,13 +25,12 @@ void ls_list(int client);
...
@@ -25,13 +25,12 @@ void ls_list(int client);
// Process monitoring
// Process monitoring
[[
noreturn
]]
void
proc_monitor
();
[[
noreturn
]]
void
proc_monitor
();
void
update_uid_map
();
// Utility functions
// Utility functions
void
crawl_procfs
(
const
std
::
function
<
bool
(
int
)
>
&
fn
);
void
crawl_procfs
(
const
std
::
function
<
bool
(
int
)
>
&
fn
);
void
crawl_procfs
(
DIR
*
dir
,
const
std
::
function
<
bool
(
int
)
>
&
fn
);
void
crawl_procfs
(
DIR
*
dir
,
const
std
::
function
<
bool
(
int
)
>
&
fn
);
bool
hide_enabled
();
bool
hide_enabled
();
void
set_hide_state
(
bool
state
);
void
update_uid_map
(
);
// Hide policies
// Hide policies
void
hide_daemon
(
int
pid
);
void
hide_daemon
(
int
pid
);
...
@@ -39,8 +38,8 @@ void hide_unmount(int pid = getpid());
...
@@ -39,8 +38,8 @@ void hide_unmount(int pid = getpid());
void
hide_sensitive_props
();
void
hide_sensitive_props
();
void
hide_late_sensitive_props
();
void
hide_late_sensitive_props
();
extern
pthread_mutex_t
monitor
_lock
;
extern
pthread_mutex_t
hide_state
_lock
;
extern
std
::
set
<
std
::
pair
<
std
::
string
,
std
::
string
>>
hide_set
;
extern
std
::
map
<
int
,
std
::
vector
<
std
::
string_view
>>
uid_proc_map
;
enum
{
enum
{
LAUNCH_MAGISKHIDE
,
LAUNCH_MAGISKHIDE
,
...
...
native/jni/magiskhide/proc_monitor.cpp
View file @
b36e6d98
...
@@ -18,18 +18,11 @@ using namespace std;
...
@@ -18,18 +18,11 @@ using namespace std;
static
int
inotify_fd
=
-
1
;
static
int
inotify_fd
=
-
1
;
static
void
term_thread
(
int
sig
=
SIGTERMTHRD
);
static
void
new_zygote
(
int
pid
);
static
void
new_zygote
(
int
pid
);
/**********************
/******************
* All data structures
* Data structures
**********************/
******************/
set
<
pair
<
string
,
string
>>
hide_set
;
/* set of <pkg, process> pair */
static
map
<
int
,
struct
stat
>
zygote_map
;
/* zygote pid -> mnt ns */
static
map
<
int
,
vector
<
string_view
>>
uid_proc_map
;
/* uid -> list of process */
pthread_mutex_t
monitor_lock
;
#define PID_MAX 32768
#define PID_MAX 32768
struct
pid_set
{
struct
pid_set
{
...
@@ -41,7 +34,10 @@ private:
...
@@ -41,7 +34,10 @@ private:
};
};
// true if pid is monitored
// true if pid is monitored
pid_set
attaches
;
static
pid_set
attaches
;
// zygote pid -> mnt ns
static
map
<
int
,
struct
stat
>
zygote_map
;
/********
/********
* Utils
* Utils
...
@@ -69,36 +65,6 @@ static int parse_ppid(int pid) {
...
@@ -69,36 +65,6 @@ static int parse_ppid(int pid) {
return
ppid
;
return
ppid
;
}
}
void
update_uid_map
()
{
mutex_guard
lock
(
monitor_lock
);
uid_proc_map
.
clear
();
string
data_path
(
APP_DATA_DIR
);
size_t
len
=
data_path
.
length
();
auto
dir
=
open_dir
(
APP_DATA_DIR
);
bool
first_iter
=
true
;
for
(
dirent
*
entry
;
(
entry
=
xreaddir
(
dir
.
get
()));)
{
data_path
.
resize
(
len
);
data_path
+=
'/'
;
data_path
+=
entry
->
d_name
;
// multiuser user id
data_path
+=
'/'
;
size_t
user_len
=
data_path
.
length
();
struct
stat
st
;
for
(
auto
&
hide
:
hide_set
)
{
if
(
hide
.
first
==
ISOLATED_MAGIC
)
{
if
(
!
first_iter
)
continue
;
// Setup isolated processes
uid_proc_map
[
-
1
].
emplace_back
(
hide
.
second
);
}
data_path
.
resize
(
user_len
);
data_path
+=
hide
.
first
;
if
(
stat
(
data_path
.
data
(),
&
st
))
continue
;
uid_proc_map
[
st
.
st_uid
].
emplace_back
(
hide
.
second
);
}
first_iter
=
false
;
}
}
static
bool
is_zygote_done
()
{
static
bool
is_zygote_done
()
{
#ifdef __LP64__
#ifdef __LP64__
return
zygote_map
.
size
()
>=
2
;
return
zygote_map
.
size
()
>=
2
;
...
@@ -132,7 +98,7 @@ static void check_zygote() {
...
@@ -132,7 +98,7 @@ static void check_zygote() {
static
void
setup_inotify
()
{
static
void
setup_inotify
()
{
inotify_fd
=
xinotify_init1
(
IN_CLOEXEC
);
inotify_fd
=
xinotify_init1
(
IN_CLOEXEC
);
if
(
inotify_fd
<
0
)
if
(
inotify_fd
<
0
)
term_thread
()
;
return
;
// Setup inotify asynchronous I/O
// Setup inotify asynchronous I/O
fcntl
(
inotify_fd
,
F_SETFL
,
O_ASYNC
);
fcntl
(
inotify_fd
,
F_SETFL
,
O_ASYNC
);
...
@@ -160,8 +126,8 @@ static void setup_inotify() {
...
@@ -160,8 +126,8 @@ static void setup_inotify() {
************************/
************************/
static
void
inotify_event
(
int
)
{
static
void
inotify_event
(
int
)
{
/
*
Make sure we can actually read stuffs
/
/
Make sure we can actually read stuffs
* or else the whole thread will be blocked.*/
// or else the whole thread will be blocked.
struct
pollfd
pfd
=
{
struct
pollfd
pfd
=
{
.
fd
=
inotify_fd
,
.
fd
=
inotify_fd
,
.
events
=
POLLIN
,
.
events
=
POLLIN
,
...
@@ -180,13 +146,8 @@ static void inotify_event(int) {
...
@@ -180,13 +146,8 @@ static void inotify_event(int) {
// Workaround for the lack of pthread_cancel
// Workaround for the lack of pthread_cancel
static
void
term_thread
(
int
)
{
static
void
term_thread
(
int
)
{
LOGD
(
"proc_monitor: cleaning up
\n
"
);
LOGD
(
"proc_monitor: cleaning up
\n
"
);
uid_proc_map
.
clear
();
zygote_map
.
clear
();
zygote_map
.
clear
();
hide_set
.
clear
();
attaches
.
reset
();
attaches
.
reset
();
// Misc
set_hide_state
(
false
);
pthread_mutex_destroy
(
&
monitor_lock
);
close
(
inotify_fd
);
close
(
inotify_fd
);
inotify_fd
=
-
1
;
inotify_fd
=
-
1
;
LOGD
(
"proc_monitor: terminate
\n
"
);
LOGD
(
"proc_monitor: terminate
\n
"
);
...
@@ -240,6 +201,9 @@ static bool check_pid(int pid) {
...
@@ -240,6 +201,9 @@ static bool check_pid(int pid) {
return
false
;
return
false
;
int
uid
=
st
.
st_uid
;
int
uid
=
st
.
st_uid
;
// Start accessing uid_proc_map
mutex_guard
lock
(
hide_state_lock
);
auto
it
=
uid_proc_map
.
end
();
auto
it
=
uid_proc_map
.
end
();
if
(
uid
%
100000
>
90000
)
{
if
(
uid
%
100000
>
90000
)
{
...
...
native/jni/utils/misc.cpp
View file @
b36e6d98
...
@@ -121,15 +121,22 @@ int new_daemon_thread(thread_entry entry, void *arg) {
...
@@ -121,15 +121,22 @@ int new_daemon_thread(thread_entry entry, void *arg) {
return
xpthread_create
(
&
thread
,
&
attr
,
entry
,
arg
);
return
xpthread_create
(
&
thread
,
&
attr
,
entry
,
arg
);
}
}
static
void
*
proxy_routine
(
void
*
fp
)
{
int
new_daemon_thread
(
void
(
*
entry
)())
{
auto
fn
=
reinterpret_cast
<
std
::
function
<
void
()
>*>
(
fp
);
thread_entry
proxy
=
[](
void
*
entry
)
->
void
*
{
(
*
fn
)();
reinterpret_cast
<
void
(
*
)()
>
(
entry
)();
delete
fn
;
return
nullptr
;
return
nullptr
;
};
return
new_daemon_thread
(
proxy
,
(
void
*
)
entry
);
}
}
int
new_daemon_thread
(
std
::
function
<
void
()
>
&&
entry
)
{
int
new_daemon_thread
(
std
::
function
<
void
()
>
&&
entry
)
{
return
new_daemon_thread
(
proxy_routine
,
new
std
::
function
<
void
()
>
(
std
::
move
(
entry
)));
thread_entry
proxy
=
[](
void
*
fp
)
->
void
*
{
auto
fn
=
reinterpret_cast
<
std
::
function
<
void
()
>*>
(
fp
);
(
*
fn
)();
delete
fn
;
return
nullptr
;
};
return
new_daemon_thread
(
proxy
,
new
std
::
function
<
void
()
>
(
std
::
move
(
entry
)));
}
}
static
char
*
argv0
;
static
char
*
argv0
;
...
...
native/jni/utils/misc.hpp
View file @
b36e6d98
...
@@ -60,6 +60,7 @@ static inline int parse_int(std::string_view s) { return parse_int(s.data()); }
...
@@ -60,6 +60,7 @@ static inline int parse_int(std::string_view s) { return parse_int(s.data()); }
using
thread_entry
=
void
*
(
*
)(
void
*
);
using
thread_entry
=
void
*
(
*
)(
void
*
);
int
new_daemon_thread
(
thread_entry
entry
,
void
*
arg
=
nullptr
);
int
new_daemon_thread
(
thread_entry
entry
,
void
*
arg
=
nullptr
);
int
new_daemon_thread
(
void
(
*
entry
)());
int
new_daemon_thread
(
std
::
function
<
void
()
>
&&
entry
);
int
new_daemon_thread
(
std
::
function
<
void
()
>
&&
entry
);
static
inline
bool
str_contains
(
std
::
string_view
s
,
std
::
string_view
ss
)
{
static
inline
bool
str_contains
(
std
::
string_view
s
,
std
::
string_view
ss
)
{
...
...
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