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
e16d604d
Commit
e16d604d
authored
Apr 30, 2017
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement Magic Mount
parent
d3d5703f
Show whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
580 additions
and
202 deletions
+580
-202
Android.mk
jni/Android.mk
+1
-3
bootstages.c
jni/daemon/bootstages.c
+494
-0
daemon.c
jni/daemon/daemon.c
+7
-10
late_start.c
jni/daemon/late_start.c
+0
-23
post_fs.c
jni/daemon/post_fs.c
+0
-21
post_fs_data.c
jni/daemon/post_fs_data.c
+0
-84
magisk.h
jni/magisk.h
+5
-6
cpio.c
jni/magiskboot/cpio.c
+1
-1
magiskhide.c
jni/magiskhide/magiskhide.c
+4
-2
magiskhide.h
jni/magiskhide/magiskhide.h
+0
-2
pre_process.c
jni/magiskhide/pre_process.c
+3
-3
magiskpolicy
jni/magiskpolicy
+1
-1
main.c
jni/main.c
+12
-8
resetprop.cpp
jni/resetprop/resetprop.cpp
+5
-5
resetprop.h
jni/resetprop/resetprop.h
+2
-1
misc.c
jni/utils/misc.c
+30
-26
utils.h
jni/utils/utils.h
+4
-1
vector.h
jni/utils/vector.h
+4
-4
xwrap.c
jni/utils/xwrap.c
+7
-1
No files found.
jni/Android.mk
View file @
e16d604d
...
@@ -22,9 +22,7 @@ LOCAL_SRC_FILES := \
...
@@ -22,9 +22,7 @@ LOCAL_SRC_FILES := \
daemon/daemon.c \
daemon/daemon.c \
daemon/socket_trans.c \
daemon/socket_trans.c \
daemon/log_monitor.c \
daemon/log_monitor.c \
daemon/post_fs.c \
daemon/bootstages.c \
daemon/post_fs_data.c \
daemon/late_start.c \
magiskhide/magiskhide.c \
magiskhide/magiskhide.c \
magiskhide/hide_daemon.c \
magiskhide/hide_daemon.c \
magiskhide/proc_monitor.c \
magiskhide/proc_monitor.c \
...
...
jni/daemon/bootstages.c
0 → 100644
View file @
e16d604d
/* post_fs_data.c - post-fs-data actions
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <linux/loop.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
#include "resetprop.h"
static
char
*
buf
,
*
buf2
;
static
struct
vector
module_list
;
/******************
* Node structure *
******************/
#define DO_NOTHING 0x0
/* intermediate node */
#define IS_MODULE 0x1
/* mount from module */
#define IS_DUMMY 0x2
/* mount from mirror */
#define IS_SKEL 0x4
/* mount from skeleton */
struct
node_entry
{
const
char
*
module
;
char
*
name
;
uint8_t
type
;
uint8_t
status
;
struct
node_entry
*
parent
;
struct
vector
*
children
;
};
/******************
* Image handling *
******************/
static
char
*
loopsetup
(
const
char
*
img
)
{
char
device
[
20
];
struct
loop_info64
info
;
int
i
,
lfd
,
ffd
;
memset
(
&
info
,
0
,
sizeof
(
info
));
// First get an empty loop device
for
(
i
=
0
;
i
<=
7
;
++
i
)
{
sprintf
(
device
,
"/dev/block/loop%d"
,
i
);
lfd
=
xopen
(
device
,
O_RDWR
);
if
(
ioctl
(
lfd
,
LOOP_GET_STATUS64
,
&
info
)
==
-
1
)
break
;
close
(
lfd
);
}
if
(
i
==
8
)
return
NULL
;
ffd
=
xopen
(
img
,
O_RDWR
);
if
(
ioctl
(
lfd
,
LOOP_SET_FD
,
ffd
)
==
-
1
)
return
NULL
;
strcpy
((
char
*
)
info
.
lo_file_name
,
img
);
ioctl
(
lfd
,
LOOP_SET_STATUS64
,
&
info
);
return
strdup
(
device
);
}
static
char
*
mount_image
(
const
char
*
img
,
const
char
*
target
)
{
char
*
device
=
loopsetup
(
img
);
if
(
device
)
xmount
(
device
,
target
,
"ext4"
,
0
,
NULL
);
return
device
;
}
/***********
* Scripts *
***********/
void
exec_common_script
(
const
char
*
stage
)
{
DIR
*
dir
;
struct
dirent
*
entry
;
snprintf
(
buf
,
PATH_MAX
,
"/magisk/.core/%s.d"
,
stage
);
if
(
!
(
dir
=
opendir
(
buf
)))
return
;
while
((
entry
=
xreaddir
(
dir
)))
{
if
(
entry
->
d_type
==
DT_REG
)
{
snprintf
(
buf
,
PATH_MAX
,
"%s/%s"
,
buf
,
entry
->
d_name
);
if
(
access
(
buf
,
X_OK
)
==
-
1
)
continue
;
LOGI
(
"%s.d: exec [%s]
\n
"
,
stage
,
entry
->
d_name
);
char
*
const
command
[]
=
{
"sh"
,
buf
,
NULL
};
int
pid
=
run_command
(
NULL
,
"/system/bin/sh"
,
command
);
if
(
pid
!=
-
1
)
waitpid
(
pid
,
NULL
,
0
);
}
}
closedir
(
dir
);
}
void
exec_module_script
(
const
char
*
stage
)
{
char
*
module
;
vec_for_each
(
&
module_list
,
module
)
{
snprintf
(
buf
,
PATH_MAX
,
"/magisk/%s/%s.sh"
,
module
,
stage
);
if
(
access
(
buf
,
F_OK
)
==
-
1
)
continue
;
LOGI
(
"%s: exec [%s.sh]
\n
"
,
module
,
stage
);
char
*
const
command
[]
=
{
"sh"
,
buf
,
NULL
};
int
pid
=
run_command
(
NULL
,
"/system/bin/sh"
,
command
);
if
(
pid
!=
-
1
)
waitpid
(
pid
,
NULL
,
0
);
}
}
/***************
* Magic Mount *
***************/
static
int
hasChild
(
struct
node_entry
*
node
,
const
char
*
name
)
{
struct
node_entry
*
child
;
vec_for_each
(
node
->
children
,
child
)
{
if
(
strcmp
(
child
->
name
,
name
)
==
0
)
return
1
;
}
return
0
;
}
static
char
*
get_full_path
(
struct
node_entry
*
node
)
{
char
buffer
[
PATH_MAX
],
temp
[
PATH_MAX
];
// Concat the paths
struct
node_entry
*
cur
=
node
;
strcpy
(
buffer
,
node
->
name
);
while
(
cur
->
parent
)
{
strcpy
(
temp
,
buffer
);
snprintf
(
buffer
,
sizeof
(
buffer
),
"%s/%s"
,
cur
->
parent
->
name
,
temp
);
cur
=
cur
->
parent
;
}
return
strdup
(
buffer
);
}
static
void
destroy_subtree
(
struct
node_entry
*
node
)
{
// Never free parent, since it shall be freed by themselves
free
(
node
->
name
);
struct
node_entry
*
e
;
vec_for_each
(
node
->
children
,
e
)
{
destroy_subtree
(
e
);
}
vec_destroy
(
node
->
children
);
free
(
node
->
children
);
free
(
node
);
}
static
void
insert_child
(
struct
node_entry
*
p
,
struct
node_entry
*
c
)
{
c
->
parent
=
p
;
if
(
p
->
children
==
NULL
)
{
p
->
children
=
xmalloc
(
sizeof
(
struct
vector
));
vec_init
(
p
->
children
);
}
struct
node_entry
*
e
;
vec_for_each
(
p
->
children
,
e
)
{
if
(
strcmp
(
e
->
name
,
c
->
name
)
==
0
)
{
// Exist duplicate, replace
destroy_subtree
(
e
);
vec_entry
(
p
->
children
)[
_
]
=
c
;
return
;
}
}
// New entry, push back
vec_push_back
(
p
->
children
,
c
);
}
static
void
construct_tree
(
const
char
*
module
,
const
char
*
path
,
struct
node_entry
*
parent
)
{
DIR
*
dir
;
struct
dirent
*
entry
;
struct
node_entry
*
node
;
snprintf
(
buf
,
PATH_MAX
,
"/magisk/%s/%s"
,
module
,
path
);
if
(
!
(
dir
=
xopendir
(
buf
)))
return
;
while
((
entry
=
xreaddir
(
dir
)))
{
if
(
strcmp
(
entry
->
d_name
,
"."
)
==
0
||
strcmp
(
entry
->
d_name
,
".."
)
==
0
)
continue
;
// Create new node
node
=
xcalloc
(
sizeof
(
*
node
),
1
);
node
->
module
=
module
;
node
->
name
=
strdup
(
entry
->
d_name
);
node
->
type
=
entry
->
d_type
;
insert_child
(
parent
,
node
);
char
*
real_path
=
get_full_path
(
node
);
// Check if the entry has a correspond target
if
(
entry
->
d_type
==
DT_LNK
||
access
(
real_path
,
F_OK
)
==
-
1
)
{
// Mark the parent folder as a skeleton
parent
->
status
=
IS_SKEL
;
node
->
status
=
IS_MODULE
;
}
else
{
if
(
entry
->
d_type
==
DT_DIR
)
{
// Check if marked as replace
snprintf
(
buf
,
PATH_MAX
,
"/magisk/%s/%s/%s/.replace"
,
module
,
path
,
entry
->
d_name
);
if
(
access
(
buf
,
F_OK
)
==
0
)
{
// Replace everything, mark as leaf
node
->
status
=
IS_MODULE
;
}
else
{
// Travel deeper
snprintf
(
buf
,
PATH_MAX
,
"%s/%s"
,
path
,
entry
->
d_name
);
char
*
new_path
=
strdup
(
buf
);
construct_tree
(
module
,
new_path
,
node
);
free
(
new_path
);
}
}
else
if
(
entry
->
d_type
==
DT_REG
)
{
// This is a leaf, mark as target
node
->
status
=
IS_MODULE
;
}
}
free
(
real_path
);
}
closedir
(
dir
);
}
static
void
clone_skeleton
(
struct
node_entry
*
node
,
const
char
*
real_path
)
{
DIR
*
dir
;
struct
dirent
*
entry
;
struct
node_entry
*
dummy
,
*
child
;
// Clone the structure
dir
=
xopendir
(
real_path
);
while
((
entry
=
xreaddir
(
dir
)))
{
if
(
strcmp
(
entry
->
d_name
,
"."
)
==
0
||
strcmp
(
entry
->
d_name
,
".."
)
==
0
)
continue
;
if
(
!
hasChild
(
node
,
entry
->
d_name
))
{
// Create dummy node
dummy
=
xcalloc
(
sizeof
(
*
dummy
),
1
);
dummy
->
name
=
strdup
(
entry
->
d_name
);
dummy
->
type
=
entry
->
d_type
;
dummy
->
status
=
IS_DUMMY
;
insert_child
(
node
,
dummy
);
}
}
snprintf
(
buf
,
PATH_MAX
,
"/dev/magisk/dummy%s"
,
real_path
);
xmkdir_p
(
buf
,
0755
);
bind_mount
(
buf
,
real_path
);
vec_for_each
(
node
->
children
,
child
)
{
snprintf
(
buf
,
PATH_MAX
,
"/dev/magisk/dummy%s/%s"
,
real_path
,
child
->
name
);
if
(
child
->
type
==
DT_DIR
)
{
xmkdir
(
buf
,
0755
);
}
else
if
(
child
->
type
==
DT_REG
)
{
close
(
open_new
(
buf
));
}
if
(
child
->
status
==
IS_MODULE
)
{
// Mount from module file to dummy file
snprintf
(
buf2
,
PATH_MAX
,
"/magisk/%s%s/%s"
,
child
->
module
,
real_path
,
child
->
name
);
}
else
if
(
child
->
status
==
IS_DUMMY
)
{
// Mount from mirror to dummy file
snprintf
(
buf2
,
PATH_MAX
,
"/dev/magisk/mirror%s/%s"
,
real_path
,
child
->
name
);
}
else
if
(
child
->
status
==
IS_SKEL
)
{
// It's another skeleton, recursive call and end
char
*
s
=
get_full_path
(
child
);
clone_skeleton
(
child
,
s
);
free
(
s
);
continue
;
}
if
(
child
->
type
==
DT_LNK
)
{
// Symlink special treatments
char
*
temp
=
xmalloc
(
PATH_MAX
);
xreadlink
(
buf2
,
temp
,
PATH_MAX
);
symlink
(
temp
,
buf
);
free
(
temp
);
LOGD
(
"cplink: %s -> %s
\n
"
,
buf2
,
buf
);
}
else
{
snprintf
(
buf
,
PATH_MAX
,
"%s/%s"
,
real_path
,
child
->
name
);
bind_mount
(
buf2
,
buf
);
}
}
}
static
void
magic_mount
(
struct
node_entry
*
node
)
{
char
*
real_path
;
struct
node_entry
*
child
;
if
(
strcmp
(
node
->
name
,
"vendor"
)
==
0
&&
strcmp
(
node
->
parent
->
name
,
"/system"
)
==
0
)
return
;
if
(
node
->
status
==
DO_NOTHING
)
{
vec_for_each
(
node
->
children
,
child
)
magic_mount
(
child
);
}
else
{
real_path
=
get_full_path
(
node
);
if
(
node
->
status
==
IS_MODULE
)
{
snprintf
(
buf
,
PATH_MAX
,
"/magisk/%s%s"
,
node
->
module
,
real_path
);
bind_mount
(
buf
,
real_path
);
}
else
if
(
node
->
status
==
IS_SKEL
)
{
clone_skeleton
(
node
,
real_path
);
}
free
(
real_path
);
}
}
/****************
* Entry points *
****************/
static
void
*
start_magisk_hide
(
void
*
args
)
{
launch_magiskhide
(
-
1
);
return
NULL
;
}
void
post_fs
(
int
client
)
{
LOGI
(
"** post-fs mode running
\n
"
);
// ack
write_int
(
client
,
0
);
close
(
client
);
// TODO: Simple bind mounts
// Allocate buffer
buf
=
xmalloc
(
PATH_MAX
);
buf2
=
xmalloc
(
PATH_MAX
);
unblock:
unblock_boot_process
();
}
void
post_fs_data
(
int
client
)
{
// ack
write_int
(
client
,
0
);
close
(
client
);
if
(
!
check_data
())
goto
unblock
;
// Allocate buffer
if
(
buf
==
NULL
)
buf
=
xmalloc
(
PATH_MAX
);
if
(
buf2
==
NULL
)
buf2
=
xmalloc
(
PATH_MAX
);
LOGI
(
"** post-fs-data mode running
\n
"
);
LOGI
(
"* Mounting magisk.img
\n
"
);
// Mounting magisk image
char
*
magiskimg
=
mount_image
(
"/data/magisk.img"
,
"/magisk"
);
free
(
magiskimg
);
// Run common scripts
LOGI
(
"* Running post-fs-data.d scripts
\n
"
);
exec_common_script
(
"post-fs-data"
);
DIR
*
dir
;
struct
dirent
*
entry
;
char
*
module
;
struct
node_entry
*
sys_root
,
*
ven_root
=
NULL
,
*
child
;
if
(
!
(
dir
=
xopendir
(
"/magisk"
)))
goto
unblock
;
// Create the system root entry
sys_root
=
xcalloc
(
sizeof
(
*
sys_root
),
1
);
sys_root
->
name
=
strdup
(
"/system"
);
int
has_modules
=
0
;
// Travel through each modules
vec_init
(
&
module_list
);
LOGI
(
"* Loading modules
\n
"
);
while
((
entry
=
xreaddir
(
dir
)))
{
if
(
entry
->
d_type
==
DT_DIR
)
{
if
(
strcmp
(
entry
->
d_name
,
"."
)
==
0
||
strcmp
(
entry
->
d_name
,
".."
)
==
0
||
strcmp
(
entry
->
d_name
,
".core"
)
==
0
||
strcmp
(
entry
->
d_name
,
"lost+found"
)
==
0
)
continue
;
// Check whether disable
snprintf
(
buf
,
PATH_MAX
,
"/magisk/%s/disable"
,
entry
->
d_name
);
if
(
access
(
buf
,
F_OK
)
==
0
)
continue
;
// Add the module to list
module
=
strdup
(
entry
->
d_name
);
vec_push_back
(
&
module_list
,
module
);
// Read props
snprintf
(
buf
,
PATH_MAX
,
"/magisk/%s/system.prop"
,
module
);
if
(
access
(
buf
,
F_OK
)
==
0
)
{
LOGI
(
"%s: loading [system.prop]
\n
"
,
module
);
read_prop_file
(
buf
,
0
);
}
// Check whether enable auto_mount
snprintf
(
buf
,
PATH_MAX
,
"/magisk/%s/auto_mount"
,
module
);
if
(
access
(
buf
,
F_OK
)
==
-
1
)
continue
;
// Double check whether the system folder exists
snprintf
(
buf
,
PATH_MAX
,
"/magisk/%s/system"
,
module
);
if
(
access
(
buf
,
F_OK
)
==
-
1
)
continue
;
// Construct structure
has_modules
=
1
;
LOGI
(
"%s: constructing magic mount structure
\n
"
,
module
);
construct_tree
(
module
,
"system"
,
sys_root
);
}
}
closedir
(
dir
);
if
(
has_modules
)
{
// Mount mirrors
LOGI
(
"* Mounting system/vendor mirrors"
);
char
block
[
256
];
int
seperate_vendor
=
0
;
struct
vector
mounts
;
vec_init
(
&
mounts
);
file_to_vector
(
"/proc/mounts"
,
&
mounts
);
char
*
line
;
vec_for_each
(
&
mounts
,
line
)
{
if
(
strstr
(
line
,
" /system "
))
{
sscanf
(
line
,
"%s"
,
block
);
xmkdir_p
(
"/dev/magisk/mirror/system"
,
0755
);
xmount
(
block
,
"/dev/magisk/mirror/system"
,
"ext4"
,
MS_RDONLY
,
NULL
);
LOGD
(
"mount: %s -> /dev/magisk/mirror/system
\n
"
,
block
);
continue
;
}
if
(
strstr
(
line
,
" /vendor "
))
{
seperate_vendor
=
1
;
sscanf
(
line
,
"%s"
,
block
);
xmkdir_p
(
"/dev/magisk/mirror/vendor"
,
0755
);
xmount
(
block
,
"/dev/magisk/mirror/vendor"
,
"ext4"
,
MS_RDONLY
,
NULL
);
LOGD
(
"mount: %s -> /dev/magisk/mirror/vendor
\n
"
,
block
);
continue
;
}
}
vec_deep_destroy
(
&
mounts
);
if
(
!
seperate_vendor
)
{
symlink
(
"/dev/magisk/mirror/system/vendor"
,
"/dev/magisk/mirror/vendor"
);
LOGD
(
"link: /dev/magisk/mirror/system/vendor -> /dev/magisk/mirror/vendor
\n
"
);
}
// Magic!!
magic_mount
(
sys_root
);
// Get the vendor node if exists
vec_for_each
(
sys_root
->
children
,
child
)
{
if
(
strcmp
(
child
->
name
,
"vendor"
)
==
0
)
{
ven_root
=
child
;
free
(
ven_root
->
name
);
ven_root
->
name
=
strdup
(
"/vendor"
);
ven_root
->
parent
=
NULL
;
break
;
}
}
if
(
ven_root
)
magic_mount
(
ven_root
);
}
// Cleanup memory
destroy_subtree
(
sys_root
);
// Execute module scripts
LOGI
(
"* Running module post-fs-data scripts
\n
"
);
exec_module_script
(
"post-fs-data"
);
// Start magiskhide if enabled
char
*
hide_prop
=
getprop
(
"persist.magisk.hide"
);
if
(
hide_prop
)
{
if
(
strcmp
(
hide_prop
,
"1"
)
==
0
)
{
pthread_t
thread
;
xpthread_create
(
&
thread
,
NULL
,
start_magisk_hide
,
NULL
);
}
free
(
hide_prop
);
}
unblock:
unblock_boot_process
();
}
void
late_start
(
int
client
)
{
LOGI
(
"** late_start service mode running
\n
"
);
// ack
write_int
(
client
,
0
);
close
(
client
);
// Wait till the full patch is done
pthread_join
(
sepol_patch
,
NULL
);
// Run scripts after full patch, most reliable way to run scripts
LOGI
(
"* Running service.d scripts
\n
"
);
exec_common_script
(
"service"
);
LOGI
(
"* Running module service scripts
\n
"
);
exec_module_script
(
"service"
);
// All boot stage done, cleanup everything
free
(
buf
);
free
(
buf2
);
vec_deep_destroy
(
&
module_list
);
}
jni/daemon/daemon.c
View file @
e16d604d
...
@@ -31,8 +31,6 @@ static void *request_handler(void *args) {
...
@@ -31,8 +31,6 @@ static void *request_handler(void *args) {
int
client
=
*
((
int
*
)
args
);
int
client
=
*
((
int
*
)
args
);
free
(
args
);
free
(
args
);
client_request
req
=
read_int
(
client
);
client_request
req
=
read_int
(
client
);
char
*
s
;
int
pid
,
status
,
code
;
switch
(
req
)
{
switch
(
req
)
{
case
LAUNCH_MAGISKHIDE
:
case
LAUNCH_MAGISKHIDE
:
launch_magiskhide
(
client
);
launch_magiskhide
(
client
);
...
@@ -67,11 +65,7 @@ static void *request_handler(void *args) {
...
@@ -67,11 +65,7 @@ static void *request_handler(void *args) {
late_start
(
client
);
late_start
(
client
);
break
;
break
;
case
TEST
:
case
TEST
:
s
=
read_string
(
client
);
// test(client);
LOGI
(
"%s
\n
"
,
s
);
free
(
s
);
write_int
(
client
,
0
);
close
(
client
);
break
;
break
;
}
}
return
NULL
;
return
NULL
;
...
@@ -94,7 +88,7 @@ static void *large_sepol_patch(void *args) {
...
@@ -94,7 +88,7 @@ static void *large_sepol_patch(void *args) {
LOGD
(
"sepol: Starting large patch thread
\n
"
);
LOGD
(
"sepol: Starting large patch thread
\n
"
);
// Patch su to everything
// Patch su to everything
sepol_allow
(
"su"
,
ALL
,
ALL
,
ALL
);
sepol_allow
(
"su"
,
ALL
,
ALL
,
ALL
);
dump_policydb
(
"/sys/fs/selinux/load"
);
dump_policydb
(
SELINUX_LOAD
);
LOGD
(
"sepol: Large patch done
\n
"
);
LOGD
(
"sepol: Large patch done
\n
"
);
destroy_policydb
();
destroy_policydb
();
return
NULL
;
return
NULL
;
...
@@ -117,11 +111,14 @@ void start_daemon() {
...
@@ -117,11 +111,14 @@ void start_daemon() {
xsetsid
();
xsetsid
();
setcon
(
"u:r:su:s0"
);
setcon
(
"u:r:su:s0"
);
umask
(
022
);
umask
(
022
);
close
(
STDIN_FILENO
);
close
(
STDOUT_FILENO
);
close
(
STDERR_FILENO
);
// Patch selinux with medium patch before we do anything
// Patch selinux with medium patch before we do anything
load_policydb
(
"/sys/fs/selinux/policy"
);
load_policydb
(
SELINUX_POLICY
);
sepol_med_rules
();
sepol_med_rules
();
dump_policydb
(
"/sys/fs/selinux/load"
);
dump_policydb
(
SELINUX_LOAD
);
// Continue the larger patch in another thread, we will need to join later
// Continue the larger patch in another thread, we will need to join later
pthread_create
(
&
sepol_patch
,
NULL
,
large_sepol_patch
,
NULL
);
pthread_create
(
&
sepol_patch
,
NULL
,
large_sepol_patch
,
NULL
);
...
...
jni/daemon/late_start.c
deleted
100644 → 0
View file @
d3d5703f
/* late_start.c - late_start service actions
*/
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "magisk.h"
#include "daemon.h"
#include "utils.h"
void
late_start
(
int
client
)
{
LOGI
(
"** late_start service mode running
\n
"
);
// ack
write_int
(
client
,
0
);
close
(
client
);
// Wait till the full patch is done
pthread_join
(
sepol_patch
,
NULL
);
// Run scripts after full patch, most reliable way to run scripts
exec_common_script
(
"service"
);
}
jni/daemon/post_fs.c
deleted
100644 → 0
View file @
d3d5703f
/* post_fs.c - post-fs actions
*/
#include <unistd.h>
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
void
post_fs
(
int
client
)
{
LOGI
(
"** post-fs mode running
\n
"
);
// ack
write_int
(
client
,
0
);
// TODO: Simple bind mounts
close
(
client
);
unblock:
unblock_boot_process
();
}
jni/daemon/post_fs_data.c
deleted
100644 → 0
View file @
d3d5703f
/* post_fs_data.c - post-fs-data actions
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <linux/loop.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
#include "resetprop.h"
static
char
*
loopsetup
(
const
char
*
img
)
{
char
device
[
20
];
struct
loop_info64
info
;
int
i
,
lfd
,
ffd
;
memset
(
&
info
,
0
,
sizeof
(
info
));
// First get an empty loop device
for
(
i
=
0
;
i
<=
7
;
++
i
)
{
sprintf
(
device
,
"/dev/block/loop%d"
,
i
);
lfd
=
xopen
(
device
,
O_RDWR
);
if
(
ioctl
(
lfd
,
LOOP_GET_STATUS64
,
&
info
)
==
-
1
)
break
;
close
(
lfd
);
}
if
(
i
==
8
)
return
NULL
;
ffd
=
xopen
(
img
,
O_RDWR
);
if
(
ioctl
(
lfd
,
LOOP_SET_FD
,
ffd
)
==
-
1
)
return
NULL
;
strcpy
((
char
*
)
info
.
lo_file_name
,
img
);
ioctl
(
lfd
,
LOOP_SET_STATUS64
,
&
info
);
return
strdup
(
device
);
}
static
void
*
start_magisk_hide
(
void
*
args
)
{
// Setup default error handler for thread
err_handler
=
exit_thread
;
launch_magiskhide
(
-
1
);
return
NULL
;
}
char
*
mount_image
(
const
char
*
img
,
const
char
*
target
)
{
char
*
device
=
loopsetup
(
img
);
if
(
device
)
xmount
(
device
,
target
,
"ext4"
,
0
,
NULL
);
return
device
;
}
void
post_fs_data
(
int
client
)
{
// ack
write_int
(
client
,
0
);
close
(
client
);
if
(
!
check_data
())
goto
unblock
;
LOGI
(
"** post-fs-data mode running
\n
"
);
LOGI
(
"* Mounting magisk.img
\n
"
);
// Mounting magisk image
char
*
magiskimg
=
mount_image
(
"/data/magisk.img"
,
"/magisk"
);
free
(
magiskimg
);
// TODO: Magic Mounts, modules etc.
// Run common scripts
exec_common_script
(
"post-fs-data"
);
// Start magiskhide if enabled
char
*
hide_prop
=
getprop
(
"persist.magisk.hide"
);
if
(
hide_prop
)
{
if
(
strcmp
(
hide_prop
,
"1"
)
==
0
)
{
pthread_t
thread
;
xpthread_create
(
&
thread
,
NULL
,
start_magisk_hide
,
NULL
);
}
free
(
hide_prop
);
}
unblock:
unblock_boot_process
();
}
jni/magisk.h
View file @
e16d604d
...
@@ -10,12 +10,6 @@
...
@@ -10,12 +10,6 @@
#include <pthread.h>
#include <pthread.h>
#include <android/log.h>
#include <android/log.h>
#ifndef MAGISK_VER_CODE
#define MAGISK_VER_CODE 99999
#endif
#ifndef MAGISK_VERSION
#define MAGISK_VERSION VER_DEBUG
#endif
#define MAGISK_VER_STR xstr(MAGISK_VERSION) ":MAGISK"
#define MAGISK_VER_STR xstr(MAGISK_VERSION) ":MAGISK"
#define str(a) #a
#define str(a) #a
...
@@ -30,6 +24,11 @@
...
@@ -30,6 +24,11 @@
#define ARG_MAX 4096
#define ARG_MAX 4096
#endif
#endif
#define SELINUX_PATH "/sys/fs/selinux/"
#define SELINUX_ENFORCE SELINUX_PATH "enforce"
#define SELINUX_POLICY SELINUX_PATH "policy"
#define SELINUX_LOAD SELINUX_PATH "load"
// Global handler for PLOGE
// Global handler for PLOGE
extern
__thread
void
(
*
err_handler
)(
void
);
extern
__thread
void
(
*
err_handler
)(
void
);
...
...
jni/magiskboot/cpio.c
View file @
e16d604d
...
@@ -283,7 +283,7 @@ static void cpio_extract(const char *entry, const char *filename, struct vector
...
@@ -283,7 +283,7 @@ static void cpio_extract(const char *entry, const char *filename, struct vector
static
void
cpio_backup
(
const
char
*
orig
,
struct
vector
*
v
)
{
static
void
cpio_backup
(
const
char
*
orig
,
struct
vector
*
v
)
{
struct
vector
o_body
,
*
o
=
&
o_body
,
bak
;
struct
vector
o_body
,
*
o
=
&
o_body
,
bak
;
cpio_file
*
m
,
*
n
,
*
dir
,
*
rem
;
cpio_file
*
m
,
*
n
,
*
dir
,
*
rem
;
char
chk1
[
21
],
chk2
[
21
],
buf
[
PATH_MAX
];
char
buf
[
PATH_MAX
];
int
res
,
doBak
;
int
res
,
doBak
;
dir
=
xcalloc
(
sizeof
(
*
dir
),
1
);
dir
=
xcalloc
(
sizeof
(
*
dir
),
1
);
...
...
jni/magiskhide/magiskhide.c
View file @
e16d604d
...
@@ -60,8 +60,10 @@ void launch_magiskhide(int client) {
...
@@ -60,8 +60,10 @@ void launch_magiskhide(int client) {
if
(
init_resetprop
())
if
(
init_resetprop
())
goto
error
;
goto
error
;
if
(
setprop2
(
"persist.magisk.hide"
,
"1"
,
0
))
if
(
client
!=
-
1
)
{
if
(
setprop
(
"persist.magisk.hide"
,
"1"
))
goto
error
;
goto
error
;
}
hide_sensitive_props
();
hide_sensitive_props
();
...
...
jni/magiskhide/magiskhide.h
View file @
e16d604d
...
@@ -5,8 +5,6 @@
...
@@ -5,8 +5,6 @@
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
#define DUMMYPATH "/dev/magisk/dummy"
#define DUMMYPATH "/dev/magisk/dummy"
#define ENFORCE_FILE "/sys/fs/selinux/enforce"
#define POLICY_FILE "/sys/fs/selinux/policy"
typedef
enum
{
typedef
enum
{
HIDE_ERROR
=
-
1
,
HIDE_ERROR
=
-
1
,
...
...
jni/magiskhide/pre_process.c
View file @
e16d604d
...
@@ -29,15 +29,15 @@ static int isMocked = 0;
...
@@ -29,15 +29,15 @@ static int isMocked = 0;
void
manage_selinux
()
{
void
manage_selinux
()
{
if
(
isMocked
)
return
;
if
(
isMocked
)
return
;
char
val
[
1
];
char
val
[
1
];
int
fd
=
xopen
(
ENFORCE_FIL
E
,
O_RDONLY
);
int
fd
=
xopen
(
SELINUX_ENFORC
E
,
O_RDONLY
);
xxread
(
fd
,
val
,
1
);
xxread
(
fd
,
val
,
1
);
close
(
fd
);
close
(
fd
);
// Permissive
// Permissive
if
(
val
[
0
]
==
'0'
)
{
if
(
val
[
0
]
==
'0'
)
{
LOGI
(
"hide_daemon: Permissive detected, hide the state
\n
"
);
LOGI
(
"hide_daemon: Permissive detected, hide the state
\n
"
);
chmod
(
ENFORCE_FIL
E
,
0640
);
chmod
(
SELINUX_ENFORC
E
,
0640
);
chmod
(
POLICY_FILE
,
0440
);
chmod
(
SELINUX_POLICY
,
0440
);
isMocked
=
1
;
isMocked
=
1
;
}
}
}
}
...
...
magiskpolicy
@
feec3e82
Subproject commit
3e106a9dc53400b2dde2e80cff277a5552a447ac
Subproject commit
feec3e82558b1640e2deb88fde19e50fe589bbcb
jni/main.c
View file @
e16d604d
...
@@ -35,7 +35,8 @@ static void usage() {
...
@@ -35,7 +35,8 @@ static void usage() {
" post-fs, post-fs-data, service
\n
"
" post-fs, post-fs-data, service
\n
"
"
\n
"
"
\n
"
"Options:
\n
"
"Options:
\n
"
" -v print client and daemon version
\n
"
" -c print client version
\n
"
" -v print daemon version
\n
"
" -V print daemon version code
\n
"
" -V print daemon version code
\n
"
"
\n
"
"
\n
"
"Supported applets:
\n
"
"Supported applets:
\n
"
...
@@ -57,12 +58,14 @@ int main(int argc, char *argv[]) {
...
@@ -57,12 +58,14 @@ int main(int argc, char *argv[]) {
else
arg
=
argv
[
0
];
else
arg
=
argv
[
0
];
if
(
strcmp
(
arg
,
"magisk"
)
==
0
)
{
if
(
strcmp
(
arg
,
"magisk"
)
==
0
)
{
if
(
argc
<
2
)
usage
();
if
(
argc
<
2
)
usage
();
if
(
strcmp
(
argv
[
1
],
"-v"
)
==
0
)
{
if
(
strcmp
(
argv
[
1
],
"-c"
)
==
0
)
{
printf
(
"Client: %s
\n
"
,
MAGISK_VER_STR
);
printf
(
"%s
\n
"
,
MAGISK_VER_STR
);
return
0
;
}
else
if
(
strcmp
(
argv
[
1
],
"-v"
)
==
0
)
{
int
fd
=
connect_daemon
();
int
fd
=
connect_daemon
();
write_int
(
fd
,
CHECK_VERSION
);
write_int
(
fd
,
CHECK_VERSION
);
char
*
v
=
read_string
(
fd
);
char
*
v
=
read_string
(
fd
);
printf
(
"
Daemon:
%s
\n
"
,
v
);
printf
(
"%s
\n
"
,
v
);
free
(
v
);
free
(
v
);
return
0
;
return
0
;
}
else
if
(
strcmp
(
argv
[
1
],
"-V"
)
==
0
)
{
}
else
if
(
strcmp
(
argv
[
1
],
"-V"
)
==
0
)
{
...
@@ -92,10 +95,11 @@ int main(int argc, char *argv[]) {
...
@@ -92,10 +95,11 @@ int main(int argc, char *argv[]) {
return
read_int
(
fd
);
return
read_int
(
fd
);
}
else
if
(
strcmp
(
argv
[
1
],
"--test"
)
==
0
)
{
}
else
if
(
strcmp
(
argv
[
1
],
"--test"
)
==
0
)
{
// Temporary testing entry
// Temporary testing entry
int
fd
=
connect_daemon
();
// int fd = connect_daemon();
write_int
(
fd
,
TEST
);
// write_int(fd, TEST);
write_string
(
fd
,
argv
[
2
]);
// return read_int(fd);
return
read_int
(
fd
);
// test();
return
0
;
}
else
{
}
else
{
// It's calling applets
// It's calling applets
--
argc
;
--
argc
;
...
...
jni/resetprop/resetprop.cpp
View file @
e16d604d
...
@@ -151,7 +151,7 @@ int setprop(const char *name, const char *value) {
...
@@ -151,7 +151,7 @@ int setprop(const char *name, const char *value) {
return
setprop2
(
name
,
value
,
1
);
return
setprop2
(
name
,
value
,
1
);
}
}
int
setprop2
(
const
char
*
name
,
const
char
*
value
,
int
trigger
)
{
int
setprop2
(
const
char
*
name
,
const
char
*
value
,
const
int
trigger
)
{
int
ret
;
int
ret
;
char
*
check
=
getprop
(
name
);
char
*
check
=
getprop
(
name
);
...
@@ -190,7 +190,7 @@ int deleteprop(const char *name) {
...
@@ -190,7 +190,7 @@ int deleteprop(const char *name) {
return
0
;
return
0
;
}
}
int
read_prop_file
(
const
char
*
filename
)
{
int
read_prop_file
(
const
char
*
filename
,
const
int
trigger
)
{
PRINT_D
(
"resetprop: Load prop file [%s]
\n
"
,
filename
);
PRINT_D
(
"resetprop: Load prop file [%s]
\n
"
,
filename
);
FILE
*
fp
=
fopen
(
filename
,
"r"
);
FILE
*
fp
=
fopen
(
filename
,
"r"
);
if
(
fp
==
NULL
)
{
if
(
fp
==
NULL
)
{
...
@@ -223,7 +223,7 @@ int read_prop_file(const char* filename) {
...
@@ -223,7 +223,7 @@ int read_prop_file(const char* filename) {
if
(
((
pch
==
NULL
)
||
(
i
>=
(
pch
-
line
)))
||
(
pch
>=
line
+
read
-
1
)
)
continue
;
if
(
((
pch
==
NULL
)
||
(
i
>=
(
pch
-
line
)))
||
(
pch
>=
line
+
read
-
1
)
)
continue
;
// Separate the string
// Separate the string
*
pch
=
'\0'
;
*
pch
=
'\0'
;
setprop
(
line
+
i
,
pch
+
1
);
setprop
2
(
line
+
i
,
pch
+
1
,
trigger
);
}
}
free
(
line
);
free
(
line
);
fclose
(
fp
);
fclose
(
fp
);
...
@@ -234,7 +234,7 @@ int resetprop_main(int argc, char *argv[]) {
...
@@ -234,7 +234,7 @@ int resetprop_main(int argc, char *argv[]) {
int
del
=
0
,
file
=
0
,
trigger
=
1
;
int
del
=
0
,
file
=
0
,
trigger
=
1
;
int
exp_arg
=
2
,
stdout_bak
,
null
;
int
exp_arg
=
2
;
char
*
name
,
*
value
,
*
filename
;
char
*
name
,
*
value
,
*
filename
;
if
(
argc
<
3
)
{
if
(
argc
<
3
)
{
...
@@ -283,7 +283,7 @@ int resetprop_main(int argc, char *argv[]) {
...
@@ -283,7 +283,7 @@ int resetprop_main(int argc, char *argv[]) {
return
-
1
;
return
-
1
;
if
(
file
)
{
if
(
file
)
{
return
read_prop_file
(
filename
);
return
read_prop_file
(
filename
,
trigger
);
}
else
if
(
del
)
{
}
else
if
(
del
)
{
return
deleteprop
(
name
);
return
deleteprop
(
name
);
}
else
{
}
else
{
...
...
jni/resetprop/resetprop.h
View file @
e16d604d
...
@@ -10,9 +10,10 @@ extern "C" {
...
@@ -10,9 +10,10 @@ extern "C" {
int
init_resetprop
();
int
init_resetprop
();
int
setprop
(
const
char
*
name
,
const
char
*
value
);
int
setprop
(
const
char
*
name
,
const
char
*
value
);
int
setprop2
(
const
char
*
name
,
const
char
*
value
,
int
trigger
);
int
setprop2
(
const
char
*
name
,
const
char
*
value
,
const
int
trigger
);
char
*
getprop
(
const
char
*
name
);
char
*
getprop
(
const
char
*
name
);
int
deleteprop
(
const
char
*
name
);
int
deleteprop
(
const
char
*
name
);
int
read_prop_file
(
const
char
*
filename
,
const
int
trigger
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
jni/utils/misc.c
View file @
e16d604d
...
@@ -9,6 +9,7 @@
...
@@ -9,6 +9,7 @@
#include <fcntl.h>
#include <fcntl.h>
#include <pwd.h>
#include <pwd.h>
#include <signal.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/mount.h>
...
@@ -238,10 +239,9 @@ int run_command(int *fd, const char *path, char *const argv[]) {
...
@@ -238,10 +239,9 @@ int run_command(int *fd, const char *path, char *const argv[]) {
xdup2
(
sv
[
0
],
STDOUT_FILENO
);
xdup2
(
sv
[
0
],
STDOUT_FILENO
);
xdup2
(
sv
[
0
],
STDERR_FILENO
);
xdup2
(
sv
[
0
],
STDERR_FILENO
);
}
else
{
}
else
{
int
null
=
open
(
"/dev/null"
,
O_RDWR
);
close
(
STDIN_FILENO
);
xdup2
(
null
,
STDIN_FILENO
);
close
(
STDOUT_FILENO
);
xdup2
(
null
,
STDOUT_FILENO
);
close
(
STDERR_FILENO
);
xdup2
(
null
,
STDERR_FILENO
);
}
}
execv
(
path
,
argv
);
execv
(
path
,
argv
);
...
@@ -249,29 +249,33 @@ int run_command(int *fd, const char *path, char *const argv[]) {
...
@@ -249,29 +249,33 @@ int run_command(int *fd, const char *path, char *const argv[]) {
return
-
1
;
return
-
1
;
}
}
#define MAGISK_CORE "/magisk/.core/"
int
mkdir_p
(
const
char
*
pathname
,
mode_t
mode
)
{
char
*
path
=
strdup
(
pathname
),
*
p
;
void
exec_common_script
(
const
char
*
stage
)
{
errno
=
0
;
DIR
*
dir
;
for
(
p
=
path
+
1
;
*
p
;
++
p
)
{
struct
dirent
*
entry
;
if
(
*
p
==
'/'
)
{
char
buf
[
PATH_MAX
];
*
p
=
'\0'
;
snprintf
(
buf
,
sizeof
(
buf
),
MAGISK_CORE
"%s.d"
,
stage
);
if
(
mkdir
(
path
,
mode
)
==
-
1
)
{
if
(
errno
!=
EEXIST
)
if
(
!
(
dir
=
opendir
(
buf
)))
return
-
1
;
return
;
while
((
entry
=
xreaddir
(
dir
)))
{
if
(
entry
->
d_type
==
DT_REG
)
{
snprintf
(
buf
,
sizeof
(
buf
),
MAGISK_CORE
"%s.d/%s"
,
stage
,
entry
->
d_name
);
if
(
access
(
buf
,
X_OK
)
==
-
1
)
continue
;
LOGI
(
"%s.d: exec [%s]
\n
"
,
stage
,
entry
->
d_name
);
char
*
const
command
[]
=
{
"sh"
,
buf
,
NULL
};
int
pid
=
run_command
(
NULL
,
"/system/bin/sh"
,
command
);
if
(
pid
!=
-
1
)
waitpid
(
pid
,
NULL
,
0
);
}
}
*
p
=
'/'
;
}
}
}
if
(
mkdir
(
path
,
mode
)
==
-
1
)
{
if
(
errno
!=
EEXIST
)
return
-
1
;
}
free
(
path
);
return
0
;
}
closedir
(
dir
);
int
bind_mount
(
const
char
*
from
,
const
char
*
to
)
{
int
ret
=
xmount
(
from
,
to
,
NULL
,
MS_BIND
,
NULL
);
LOGD
(
"bind_mount: %s -> %s
\n
"
,
from
,
to
);
return
ret
;
}
int
open_new
(
const
char
*
filename
)
{
return
xopen
(
filename
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
0644
);
}
}
jni/utils/utils.h
View file @
e16d604d
...
@@ -61,6 +61,7 @@ int xmkdir(const char *pathname, mode_t mode);
...
@@ -61,6 +61,7 @@ int xmkdir(const char *pathname, mode_t mode);
void
*
xmmap
(
void
*
addr
,
size_t
length
,
int
prot
,
int
flags
,
void
*
xmmap
(
void
*
addr
,
size_t
length
,
int
prot
,
int
flags
,
int
fd
,
off_t
offset
);
int
fd
,
off_t
offset
);
ssize_t
xsendfile
(
int
out_fd
,
int
in_fd
,
off_t
*
offset
,
size_t
count
);
ssize_t
xsendfile
(
int
out_fd
,
int
in_fd
,
off_t
*
offset
,
size_t
count
);
int
xmkdir_p
(
const
char
*
pathname
,
mode_t
mode
);
// misc.c
// misc.c
...
@@ -79,6 +80,8 @@ void unlock_blocks();
...
@@ -79,6 +80,8 @@ void unlock_blocks();
void
unblock_boot_process
();
void
unblock_boot_process
();
void
setup_sighandlers
(
void
(
*
handler
)(
int
));
void
setup_sighandlers
(
void
(
*
handler
)(
int
));
int
run_command
(
int
*
fd
,
const
char
*
path
,
char
*
const
argv
[]);
int
run_command
(
int
*
fd
,
const
char
*
path
,
char
*
const
argv
[]);
void
exec_common_script
(
const
char
*
stage
);
int
mkdir_p
(
const
char
*
pathname
,
mode_t
mode
);
int
bind_mount
(
const
char
*
from
,
const
char
*
to
);
int
open_new
(
const
char
*
filename
);
#endif
#endif
jni/utils/vector.h
View file @
e16d604d
...
@@ -21,11 +21,11 @@ void vec_deep_destroy(struct vector *v);
...
@@ -21,11 +21,11 @@ void vec_deep_destroy(struct vector *v);
#define vec_entry(v) (v)->data
#define vec_entry(v) (v)->data
/* Usage: vec_for_each(vector *v, void *e) */
/* Usage: vec_for_each(vector *v, void *e) */
#define vec_for_each(v, e) \
#define vec_for_each(v, e) \
e =
(v)->data[0]
; \
e =
v ? (v)->data[0] : NULL
; \
for (size_t _ = 0; _ < (v)->size; ++_, e = (v)->data[_])
for (size_t _ = 0;
v &&
_ < (v)->size; ++_, e = (v)->data[_])
#define vec_for_each_r(v, e) \
#define vec_for_each_r(v, e) \
e =
(v)->data[(v)->size - 1]
; \
e =
v ? (v)->data[(v)->size - 1] : NULL
; \
for (size_t _ = (v)->size; _ > 0; --_, e = (v)->data[_ - 1])
for (size_t _ = (v)->size;
v &&
_ > 0; --_, e = (v)->data[_ - 1])
#endif
#endif
\ No newline at end of file
jni/utils/xwrap.c
View file @
e16d604d
...
@@ -298,4 +298,10 @@ ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count) {
...
@@ -298,4 +298,10 @@ ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count) {
return
ret
;
return
ret
;
}
}
int
xmkdir_p
(
const
char
*
pathname
,
mode_t
mode
)
{
int
ret
=
mkdir_p
(
pathname
,
mode
);
if
(
ret
==
-
1
)
{
PLOGE
(
"mkdir_p %s"
,
pathname
);
}
return
ret
;
}
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