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
14aa6041
Commit
14aa6041
authored
Feb 18, 2019
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a better function to read through files
parent
fb55fe18
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
51 deletions
+49
-51
bootstages.cpp
native/jni/core/bootstages.cpp
+11
-12
hide_utils.cpp
native/jni/magiskhide/hide_utils.cpp
+4
-3
proc_monitor.cpp
native/jni/magiskhide/proc_monitor.cpp
+13
-18
file.cpp
native/jni/utils/file.cpp
+18
-17
utils.h
native/jni/utils/include/utils.h
+3
-1
No files found.
native/jni/core/bootstages.cpp
View file @
14aa6041
...
@@ -378,35 +378,35 @@ static bool magisk_env() {
...
@@ -378,35 +378,35 @@ static bool magisk_env() {
xmkdir
(
SECURE_DIR
"/service.d"
,
0755
);
xmkdir
(
SECURE_DIR
"/service.d"
,
0755
);
LOGI
(
"* Mounting mirrors"
);
LOGI
(
"* Mounting mirrors"
);
auto
mounts
=
file_to_vector
(
"/proc/mounts"
);
bool
system_as_root
=
false
;
bool
system_as_root
=
false
;
f
or
(
auto
&
line
:
mounts
)
{
f
ile_readline
(
"/proc/mounts"
,
[
&
](
string_view
&
line
)
->
bool
{
if
(
str_contains
(
line
,
" /system_root "
))
{
if
(
str_contains
(
line
,
" /system_root "
))
{
bind_mount
(
"/system_root/system"
,
MIRRDIR
"/system"
);
bind_mount
(
"/system_root/system"
,
MIRRDIR
"/system"
);
sscanf
(
line
.
c_str
(),
"%s"
,
buf
);
sscanf
(
line
.
data
(),
"%s"
,
buf
);
system_block
=
strdup
(
buf
);
system_block
=
strdup
(
buf
);
system_as_root
=
true
;
system_as_root
=
true
;
}
else
if
(
!
system_as_root
&&
str_contains
(
line
,
" /system "
))
{
}
else
if
(
!
system_as_root
&&
str_contains
(
line
,
" /system "
))
{
sscanf
(
line
.
c_str
(),
"%s %*s %s"
,
buf
,
buf2
);
sscanf
(
line
.
data
(),
"%s %*s %s"
,
buf
,
buf2
);
system_block
=
strdup
(
buf
);
system_block
=
strdup
(
buf
);
xmount
(
system_block
,
MIRRDIR
"/system"
,
buf2
,
MS_RDONLY
,
nullptr
);
xmount
(
system_block
,
MIRRDIR
"/system"
,
buf2
,
MS_RDONLY
,
nullptr
);
VLOGI
(
"mount"
,
system_block
,
MIRRDIR
"/system"
);
VLOGI
(
"mount"
,
system_block
,
MIRRDIR
"/system"
);
}
else
if
(
str_contains
(
line
,
" /vendor "
))
{
}
else
if
(
str_contains
(
line
,
" /vendor "
))
{
seperate_vendor
=
true
;
seperate_vendor
=
true
;
sscanf
(
line
.
c_str
(),
"%s %*s %s"
,
buf
,
buf2
);
sscanf
(
line
.
data
(),
"%s %*s %s"
,
buf
,
buf2
);
vendor_block
=
strdup
(
buf
);
vendor_block
=
strdup
(
buf
);
xmkdir
(
MIRRDIR
"/vendor"
,
0755
);
xmkdir
(
MIRRDIR
"/vendor"
,
0755
);
xmount
(
vendor_block
,
MIRRDIR
"/vendor"
,
buf2
,
MS_RDONLY
,
nullptr
);
xmount
(
vendor_block
,
MIRRDIR
"/vendor"
,
buf2
,
MS_RDONLY
,
nullptr
);
VLOGI
(
"mount"
,
vendor_block
,
MIRRDIR
"/vendor"
);
VLOGI
(
"mount"
,
vendor_block
,
MIRRDIR
"/vendor"
);
}
else
if
(
str_contains
(
line
,
" /data "
))
{
}
else
if
(
str_contains
(
line
,
" /data "
))
{
sscanf
(
line
.
c_str
(),
"%s"
,
buf
);
sscanf
(
line
.
data
(),
"%s"
,
buf
);
data_block
=
strdup
(
buf
);
data_block
=
strdup
(
buf
);
}
else
if
(
SDK_INT
>=
24
&&
}
else
if
(
SDK_INT
>=
24
&&
str_contains
(
line
,
" /proc "
)
&&
!
str_contains
(
line
,
"hidepid=2"
))
{
str_contains
(
line
,
" /proc "
)
&&
!
str_contains
(
line
,
"hidepid=2"
))
{
// Enforce hidepid
// Enforce hidepid
xmount
(
nullptr
,
"/proc"
,
nullptr
,
MS_REMOUNT
,
"hidepid=2,gid=3009"
);
xmount
(
nullptr
,
"/proc"
,
nullptr
,
MS_REMOUNT
,
"hidepid=2,gid=3009"
);
}
}
}
return
true
;
});
if
(
!
seperate_vendor
)
{
if
(
!
seperate_vendor
)
{
xsymlink
(
MIRRDIR
"/system/vendor"
,
MIRRDIR
"/vendor"
);
xsymlink
(
MIRRDIR
"/system/vendor"
,
MIRRDIR
"/vendor"
);
VLOGI
(
"link"
,
MIRRDIR
"/system/vendor"
,
MIRRDIR
"/vendor"
);
VLOGI
(
"link"
,
MIRRDIR
"/system/vendor"
,
MIRRDIR
"/vendor"
);
...
@@ -492,12 +492,11 @@ static void collect_modules() {
...
@@ -492,12 +492,11 @@ static void collect_modules() {
static
bool
check_data
()
{
static
bool
check_data
()
{
bool
mnt
=
false
;
bool
mnt
=
false
;
bool
data
=
false
;
bool
data
=
false
;
auto
mounts
=
file_to_vector
(
"/proc/mounts"
);
file_readline
(
"/proc/mounts"
,
[
&
](
string_view
&
s
)
->
bool
{
for
(
auto
&
line
:
mounts
)
{
if
(
str_contains
(
s
,
" /data "
)
&&
!
str_contains
(
s
,
"tmpfs"
))
if
(
line
.
find
(
" /data "
)
!=
string
::
npos
&&
line
.
find
(
"tmpfs"
)
==
string
::
npos
)
mnt
=
true
;
mnt
=
true
;
}
return
true
;
});
if
(
mnt
)
{
if
(
mnt
)
{
auto
crypto
=
getprop
(
"ro.crypto.state"
);
auto
crypto
=
getprop
(
"ro.crypto.state"
);
if
(
!
crypto
.
empty
())
{
if
(
!
crypto
.
empty
())
{
...
...
native/jni/magiskhide/hide_utils.cpp
View file @
14aa6041
...
@@ -263,9 +263,10 @@ bool init_list() {
...
@@ -263,9 +263,10 @@ bool init_list() {
// Migrate old hide list into database
// Migrate old hide list into database
if
(
access
(
LEGACY_LIST
,
R_OK
)
==
0
)
{
if
(
access
(
LEGACY_LIST
,
R_OK
)
==
0
)
{
auto
tmp
=
file_to_vector
(
LEGACY_LIST
);
file_readline
(
LEGACY_LIST
,
[](
string_view
&
s
)
->
bool
{
for
(
auto
&
s
:
tmp
)
add_list
(
s
.
data
());
add_list
(
s
.
c_str
());
return
true
;
});
unlink
(
LEGACY_LIST
);
unlink
(
LEGACY_LIST
);
}
}
...
...
native/jni/magiskhide/proc_monitor.cpp
View file @
14aa6041
...
@@ -97,41 +97,36 @@ static bool is_pid_safetynet_process(const int pid) {
...
@@ -97,41 +97,36 @@ static bool is_pid_safetynet_process(const int pid) {
}
}
static
void
hide_daemon
(
int
pid
)
{
static
void
hide_daemon
(
int
pid
)
{
LOGD
(
"hide_daemon: handling PID=[%d]
\n
"
,
pid
);
char
buffer
[
4096
];
char
buffer
[
4096
];
vector
<
string
>
mounts
;
manage_selinux
();
clean_magisk_props
();
if
(
switch_mnt_ns
(
pid
))
if
(
switch_mnt_ns
(
pid
))
goto
exit
;
goto
exit
;
LOGD
(
"hide_daemon: handling PID=[%d]
\n
"
,
pid
);
manage_selinux
();
clean_magisk_props
();
snprintf
(
buffer
,
sizeof
(
buffer
),
"/proc/%d"
,
pid
);
snprintf
(
buffer
,
sizeof
(
buffer
),
"/proc/%d"
,
pid
);
chdir
(
buffer
);
chdir
(
buffer
);
mounts
=
file_to_vector
(
"mounts"
);
// Unmount dummy skeletons and /sbin links
// Unmount dummy skeletons and /sbin links
f
or
(
auto
&
s
:
mounts
)
{
f
ile_readline
(
"mounts"
,
[
&
](
string_view
&
s
)
->
bool
{
if
(
str_contains
(
s
,
"tmpfs /system/"
)
||
str_contains
(
s
,
"tmpfs /vendor/"
)
||
if
(
str_contains
(
s
,
"tmpfs /system/"
)
||
str_contains
(
s
,
"tmpfs /vendor/"
)
||
str_contains
(
s
,
"tmpfs /sbin"
))
{
str_contains
(
s
,
"tmpfs /sbin"
))
{
sscanf
(
s
.
c_str
(),
"%*s %4096s"
,
buffer
);
sscanf
(
s
.
data
(),
"%*s %4096s"
,
buffer
);
lazy_unmount
(
buffer
);
lazy_unmount
(
buffer
);
}
}
}
return
true
;
});
// Re-read mount infos
mounts
=
file_to_vector
(
"mounts"
);
// Unmount everything under /system, /vendor, and data mounts
// Unmount everything under /system, /vendor, and data mounts
f
or
(
auto
&
s
:
mounts
)
{
f
ile_readline
(
"mounts"
,
[
&
](
string_view
&
s
)
->
bool
{
if
((
str_contains
(
s
,
" /system/"
)
||
str_contains
(
s
,
" /vendor/"
))
&&
if
((
str_contains
(
s
,
" /system/"
)
||
str_contains
(
s
,
" /vendor/"
))
&&
(
str_contains
(
s
,
system_block
)
||
str_contains
(
s
,
vendor_block
)
||
str_contains
(
s
,
data_block
)))
{
(
str_contains
(
s
,
system_block
)
||
str_contains
(
s
,
vendor_block
)
||
sscanf
(
s
.
c_str
(),
"%*s %4096s"
,
buffer
);
str_contains
(
s
,
data_block
)))
{
sscanf
(
s
.
data
(),
"%*s %4096s"
,
buffer
);
lazy_unmount
(
buffer
);
lazy_unmount
(
buffer
);
}
}
}
return
true
;
});
exit:
exit:
// Send resume signal
// Send resume signal
...
...
native/jni/utils/file.cpp
View file @
14aa6041
...
@@ -384,25 +384,26 @@ void write_zero(int fd, size_t size) {
...
@@ -384,25 +384,26 @@ void write_zero(int fd, size_t size) {
lseek
(
fd
,
pos
+
size
,
SEEK_SET
);
lseek
(
fd
,
pos
+
size
,
SEEK_SET
);
}
}
vector
<
string
>
file_to_vector
(
const
char
*
filename
)
{
void
file_readline
(
const
char
*
filename
,
const
function
<
bool
(
string_view
&
)
>
&
fn
,
bool
trim
)
{
vector
<
string
>
arr
;
if
(
access
(
filename
,
R_OK
)
!=
0
)
return
arr
;
char
*
line
=
nullptr
;
size_t
len
=
0
;
ssize_t
read
;
FILE
*
fp
=
xfopen
(
filename
,
"re"
);
FILE
*
fp
=
xfopen
(
filename
,
"re"
);
if
(
fp
==
nullptr
)
if
(
fp
==
nullptr
)
return
arr
;
return
;
size_t
len
=
1024
;
while
((
read
=
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
buf
=
(
char
*
)
malloc
(
len
);
// Remove end newline
char
*
start
;
if
(
line
[
read
-
1
]
==
'\n'
)
ssize_t
read
;
line
[
read
-
1
]
=
'\0'
;
while
((
read
=
getline
(
&
buf
,
&
len
,
fp
))
>=
0
)
{
arr
.
emplace_back
(
line
);
start
=
buf
;
if
(
trim
)
{
while
(
buf
[
read
-
1
]
==
'\n'
||
buf
[
read
-
1
]
==
' '
)
buf
[
read
--
-
1
]
=
'\0'
;
while
(
*
start
==
' '
)
++
start
;
}
string_view
s
(
start
);
if
(
!
fn
(
s
))
break
;
}
}
fclose
(
fp
);
fclose
(
fp
);
free
(
line
);
free
(
buf
);
return
arr
;
}
}
native/jni/utils/include/utils.h
View file @
14aa6041
...
@@ -140,13 +140,15 @@ void write_zero(int fd, size_t size);
...
@@ -140,13 +140,15 @@ void write_zero(int fd, size_t size);
#include <string>
#include <string>
#include <vector>
#include <vector>
#include <functional>
#include <string_view>
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
// file.cpp
// file.cpp
std
::
vector
<
std
::
string
>
file_to_vector
(
const
char
*
filenam
e
);
void
file_readline
(
const
char
*
filename
,
const
std
::
function
<
bool
(
std
::
string_view
&
)
>
&
fn
,
bool
trim
=
fals
e
);
// misc.cpp
// misc.cpp
...
...
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