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
5c7f69ac
Commit
5c7f69ac
authored
Jun 16, 2019
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Separate SAR and legacy implementation
parent
f1d9015e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
140 additions
and
107 deletions
+140
-107
early_mount.cpp
native/jni/init/early_mount.cpp
+47
-27
init.cpp
native/jni/init/init.cpp
+67
-70
init.h
native/jni/init/init.h
+24
-8
rootfs.cpp
native/jni/init/rootfs.cpp
+2
-2
No files found.
native/jni/init/early_mount.cpp
View file @
5c7f69ac
...
...
@@ -5,6 +5,7 @@
#include <utils.h>
#include <logging.h>
#include <selinux.h>
#include "init.h"
...
...
@@ -73,7 +74,7 @@ static void setup_block(const char *partname, char *block_dev) {
}
}
bool
Magisk
Init
::
read_dt_fstab
(
const
char
*
name
,
char
*
partname
,
char
*
fstype
)
{
bool
Base
Init
::
read_dt_fstab
(
const
char
*
name
,
char
*
partname
,
char
*
fstype
)
{
char
path
[
128
];
int
fd
;
sprintf
(
path
,
"%s/fstab/%s/dev"
,
cmd
->
dt_dir
,
name
);
...
...
@@ -106,38 +107,57 @@ if (!is_lnk("/" #name) && read_dt_fstab(#name, partname, fstype)) { \
mnt_##name = true; \
}
void
Magisk
Init
::
early_mount
()
{
void
Legacy
Init
::
early_mount
()
{
char
partname
[
32
];
char
fstype
[
32
];
char
block_dev
[
64
];
if
(
cmd
->
system_as_root
)
{
LOGD
(
"Early mount system_root
\n
"
);
sprintf
(
partname
,
"system%s"
,
cmd
->
slot
);
setup_block
(
partname
,
block_dev
);
xmkdir
(
"/system_root"
,
0755
);
if
(
xmount
(
block_dev
,
"/system_root"
,
"ext4"
,
MS_RDONLY
,
nullptr
))
xmount
(
block_dev
,
"/system_root"
,
"erofs"
,
MS_RDONLY
,
nullptr
);
xmkdir
(
"/system"
,
0755
);
xmount
(
"/system_root/system"
,
"/system"
,
nullptr
,
MS_BIND
,
nullptr
);
// Android Q
if
(
is_lnk
(
"/system_root/init"
))
load_sepol
=
true
;
// System-as-root with monolithic sepolicy
if
(
access
(
"/system_root/sepolicy"
,
F_OK
)
==
0
)
cp_afc
(
"/system_root/sepolicy"
,
"/sepolicy"
);
// Copy if these partitions are symlinks
link_root
(
"/vendor"
);
link_root
(
"/product"
);
link_root
(
"/odm"
);
}
else
{
mount_root
(
system
);
}
mount_root
(
system
);
mount_root
(
vendor
);
mount_root
(
product
);
mount_root
(
odm
);
}
void
SARInit
::
early_mount
()
{
char
partname
[
32
];
char
fstype
[
32
];
char
block_dev
[
64
];
LOGD
(
"Early mount system_root
\n
"
);
sprintf
(
partname
,
"system%s"
,
cmd
->
slot
);
setup_block
(
partname
,
block_dev
);
xmkdir
(
"/system_root"
,
0755
);
if
(
xmount
(
block_dev
,
"/system_root"
,
"ext4"
,
MS_RDONLY
,
nullptr
))
xmount
(
block_dev
,
"/system_root"
,
"erofs"
,
MS_RDONLY
,
nullptr
);
xmkdir
(
"/system"
,
0755
);
xmount
(
"/system_root/system"
,
"/system"
,
nullptr
,
MS_BIND
,
nullptr
);
// Android Q
if
(
is_lnk
(
"/system_root/init"
))
load_sepol
=
true
;
// System-as-root with monolithic sepolicy
if
(
access
(
"/system_root/sepolicy"
,
F_OK
)
==
0
)
cp_afc
(
"/system_root/sepolicy"
,
"/sepolicy"
);
link_root
(
"/vendor"
);
link_root
(
"/product"
);
link_root
(
"/odm"
);
mount_root
(
vendor
);
mount_root
(
product
);
mount_root
(
odm
);
}
#define umount_root(name) \
if (mnt_##name) \
umount("/" #name);
void
BaseInit
::
cleanup
()
{
umount
(
SELINUX_MNT
);
umount
(
"/sys"
);
umount
(
"/proc"
);
umount_root
(
system
);
umount_root
(
vendor
);
umount_root
(
product
);
umount_root
(
odm
);
}
native/jni/init/init.cpp
View file @
5c7f69ac
...
...
@@ -6,13 +6,11 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <libgen.h>
#include <xz.h>
#include <magisk.h>
#include <selinux.h>
#include <cpio.h>
#include <utils.h>
#include <flags.h>
...
...
@@ -48,12 +46,10 @@ static void setup_klog() {
#define setup_klog(...)
#endif
static
int
test_main
(
int
argc
,
char
*
argv
[]);
constexpr
const
char
*
init_applet
[]
=
{
"magiskpolicy"
,
"supolicy"
,
"init_test"
,
nullptr
};
{
"magiskpolicy"
,
"supolicy"
,
nullptr
};
constexpr
int
(
*
init_applet_main
[])(
int
,
char
*
[])
=
{
magiskpolicy_main
,
magiskpolicy_main
,
test_main
,
nullptr
};
{
magiskpolicy_main
,
magiskpolicy_main
,
nullptr
};
static
bool
unxz
(
int
fd
,
const
uint8_t
*
buf
,
size_t
size
)
{
uint8_t
out
[
8192
];
...
...
@@ -117,50 +113,27 @@ static int dump_manager(const char *path, mode_t mode) {
return
0
;
}
void
MagiskInit
::
preset
()
{
root
=
open
(
"/"
,
O_RDONLY
|
O_CLOEXEC
);
if
(
cmd
->
system_as_root
)
{
// Clear rootfs
LOGD
(
"Cleaning rootfs
\n
"
);
frm_rf
(
root
,
{
"overlay"
,
"proc"
,
"sys"
});
}
else
{
decompress_ramdisk
();
// Revert original init binary
rename
(
"/.backup/init"
,
"/init"
);
rm_rf
(
"/.backup"
);
// Do not go further if device is booting into recovery
if
(
access
(
"/sbin/recovery"
,
F_OK
)
==
0
)
{
LOGD
(
"Ramdisk is recovery, abort
\n
"
);
re_exec_init
();
}
}
}
#define umount_root(name) \
if (mnt_##name) \
umount("/" #name);
void
MagiskInit
::
cleanup
()
{
umount
(
SELINUX_MNT
);
umount
(
"/sys"
);
umount
(
"/proc"
);
umount_root
(
system
);
umount_root
(
vendor
);
umount_root
(
product
);
umount_root
(
odm
);
}
void
MagiskInit
::
re_exec_init
()
{
void
BaseInit
::
re_exec_init
()
{
LOGD
(
"Re-exec /init
\n
"
);
cleanup
();
execv
(
"/init"
,
argv
);
exit
(
1
);
}
void
MagiskInit
::
start
()
{
void
LegacyInit
::
preset
()
{
LOGD
(
"Reverting /init
\n
"
);
root
=
open
(
"/"
,
O_RDONLY
|
O_CLOEXEC
);
rename
(
"/.backup/init"
,
"/init"
);
rm_rf
(
"/.backup"
);
}
void
SARInit
::
preset
()
{
LOGD
(
"Cleaning rootfs
\n
"
);
root
=
open
(
"/"
,
O_RDONLY
|
O_CLOEXEC
);
frm_rf
(
root
,
{
"overlay"
,
"proc"
,
"sys"
});
}
void
BaseInit
::
start
()
{
// Prevent file descriptor confusion
mknod
(
"/null"
,
S_IFCHR
|
0666
,
makedev
(
1
,
3
));
int
null
=
open
(
"/null"
,
O_RDWR
|
O_CLOEXEC
);
...
...
@@ -180,27 +153,27 @@ void MagiskInit::start() {
re_exec_init
();
}
void
MagiskInit
::
test
()
{
cmdline_logging
();
log_cb
.
ex
=
nop_ex
;
chdir
(
dirname
(
argv
[
0
])
);
chroot
(
".
"
);
chdir
(
"/
"
);
preset
();
early_mount
()
;
setup_rootfs
();
cleanup
();
}
static
int
test_main
(
int
,
char
*
argv
[])
{
cmdline
cmd
{}
;
load_kernel_info
(
&
cmd
);
MagiskInit
init
(
argv
,
&
cmd
);
init
.
test
();
return
0
;
}
class
RecoveryInit
:
public
BaseInit
{
public
:
RecoveryInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{}
;
void
start
()
override
{
LOGD
(
"Ramdisk is recovery, abort
\n
"
);
rename
(
"/.backup/init"
,
"/init
"
);
rm_rf
(
"/.backup
"
);
re_exec_init
();
}
}
;
class
TestInit
:
public
SARInit
{
public
:
TestInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
SARInit
(
argv
,
cmd
)
{};
void
start
()
override
{
preset
()
;
early_mount
(
);
setup_rootfs
(
);
cleanup
();
}
}
;
int
main
(
int
argc
,
char
*
argv
[])
{
umask
(
0
);
...
...
@@ -217,16 +190,40 @@ int main(int argc, char *argv[]) {
return
dump_manager
(
argv
[
3
],
0644
);
}
if
(
getpid
()
!=
1
)
return
1
;
#ifdef MAGISK_DEBUG
bool
run_test
=
getenv
(
"INIT_TEST"
)
!=
nullptr
;
#else
constexpr
bool
run_test
=
false
;
#endif
setup_klog
();
if
(
run_test
)
{
chdir
(
dirname
(
argv
[
0
]));
chroot
(
"."
);
chdir
(
"/"
);
cmdline_logging
();
log_cb
.
ex
=
nop_ex
;
}
else
{
if
(
getpid
()
!=
1
)
return
1
;
setup_klog
();
}
cmdline
cmd
{};
load_kernel_info
(
&
cmd
);
MagiskInit
init
(
argv
,
&
cmd
);
unique_ptr
<
BaseInit
>
init
;
if
(
run_test
)
{
init
=
make_unique
<
TestInit
>
(
argv
,
&
cmd
);
}
else
if
(
cmd
.
system_as_root
)
{
init
=
make_unique
<
SARInit
>
(
argv
,
&
cmd
);
}
else
{
decompress_ramdisk
();
if
(
access
(
"/sbin/recovery"
,
F_OK
)
==
0
)
init
=
make_unique
<
RecoveryInit
>
(
argv
,
&
cmd
);
else
init
=
make_unique
<
LegacyInit
>
(
argv
,
&
cmd
);
}
// Run the main routine
init
.
start
();
init
->
start
();
}
native/jni/init/init.h
View file @
5c7f69ac
...
...
@@ -11,21 +11,21 @@ struct raw_data {
size_t
sz
;
};
class
Magisk
Init
{
pr
ivate
:
class
Base
Init
{
pr
otected
:
cmdline
*
cmd
;
raw_data
self
{};
raw_data
config
{};
int
root
=
-
1
;
char
**
argv
;
int
root
=
-
1
;
bool
load_sepol
=
false
;
bool
mnt_system
=
false
;
bool
mnt_vendor
=
false
;
bool
mnt_product
=
false
;
bool
mnt_odm
=
false
;
v
oid
preset
()
;
v
oid
early_mount
();
v
irtual
void
preset
()
{}
;
v
irtual
void
early_mount
()
{}
void
setup_rootfs
();
bool
read_dt_fstab
(
const
char
*
name
,
char
*
partname
,
char
*
fstype
);
bool
patch_sepolicy
();
...
...
@@ -33,9 +33,25 @@ private:
void
re_exec_init
();
public
:
explicit
MagiskInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
cmd
(
cmd
),
argv
(
argv
)
{}
void
start
();
void
test
();
BaseInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
cmd
(
cmd
),
argv
(
argv
)
{}
virtual
~
BaseInit
()
=
default
;
virtual
void
start
();
};
class
LegacyInit
:
public
BaseInit
{
protected
:
void
preset
()
override
;
void
early_mount
()
override
;
public
:
LegacyInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{};
};
class
SARInit
:
public
BaseInit
{
protected
:
void
preset
()
override
;
void
early_mount
()
override
;
public
:
SARInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{};
};
static
inline
bool
is_lnk
(
const
char
*
name
)
{
...
...
native/jni/init/rootfs.cpp
View file @
5c7f69ac
...
...
@@ -37,7 +37,7 @@ constexpr const char wrapper[] =
"exec /sbin/magisk.bin
\"
$0
\"
\"
$@
\"\n
"
;
void
Magisk
Init
::
setup_rootfs
()
{
void
Base
Init
::
setup_rootfs
()
{
bool
patch_init
=
patch_sepolicy
();
if
(
cmd
->
system_as_root
)
{
...
...
@@ -173,7 +173,7 @@ void MagiskInit::setup_rootfs() {
close
(
sbin
);
}
bool
Magisk
Init
::
patch_sepolicy
()
{
bool
Base
Init
::
patch_sepolicy
()
{
bool
patch_init
=
false
;
if
(
access
(
SPLIT_PLAT_CIL
,
R_OK
)
==
0
)
{
...
...
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