mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-11-27 20:15:29 +00:00
Separate SAR and legacy implementation
This commit is contained in:
parent
f1d9015e5f
commit
5c7f69acaa
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <utils.h>
|
#include <utils.h>
|
||||||
#include <logging.h>
|
#include <logging.h>
|
||||||
|
#include <selinux.h>
|
||||||
|
|
||||||
#include "init.h"
|
#include "init.h"
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ static void setup_block(const char *partname, char *block_dev) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MagiskInit::read_dt_fstab(const char *name, char *partname, char *fstype) {
|
bool BaseInit::read_dt_fstab(const char *name, char *partname, char *fstype) {
|
||||||
char path[128];
|
char path[128];
|
||||||
int fd;
|
int fd;
|
||||||
sprintf(path, "%s/fstab/%s/dev", cmd->dt_dir, name);
|
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; \
|
mnt_##name = true; \
|
||||||
}
|
}
|
||||||
|
|
||||||
void MagiskInit::early_mount() {
|
void LegacyInit::early_mount() {
|
||||||
char partname[32];
|
char partname[32];
|
||||||
char fstype[32];
|
char fstype[32];
|
||||||
char block_dev[64];
|
char block_dev[64];
|
||||||
|
|
||||||
if (cmd->system_as_root) {
|
mount_root(system);
|
||||||
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(vendor);
|
mount_root(vendor);
|
||||||
mount_root(product);
|
mount_root(product);
|
||||||
mount_root(odm);
|
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);
|
||||||
|
}
|
||||||
|
@ -6,13 +6,11 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <dirent.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
|
|
||||||
#include <xz.h>
|
#include <xz.h>
|
||||||
#include <magisk.h>
|
#include <magisk.h>
|
||||||
#include <selinux.h>
|
|
||||||
#include <cpio.h>
|
#include <cpio.h>
|
||||||
#include <utils.h>
|
#include <utils.h>
|
||||||
#include <flags.h>
|
#include <flags.h>
|
||||||
@ -48,12 +46,10 @@ static void setup_klog() {
|
|||||||
#define setup_klog(...)
|
#define setup_klog(...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int test_main(int argc, char *argv[]);
|
|
||||||
|
|
||||||
constexpr const char *init_applet[] =
|
constexpr const char *init_applet[] =
|
||||||
{ "magiskpolicy", "supolicy", "init_test", nullptr };
|
{ "magiskpolicy", "supolicy", nullptr };
|
||||||
constexpr int (*init_applet_main[])(int, char *[]) =
|
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) {
|
static bool unxz(int fd, const uint8_t *buf, size_t size) {
|
||||||
uint8_t out[8192];
|
uint8_t out[8192];
|
||||||
@ -117,50 +113,27 @@ static int dump_manager(const char *path, mode_t mode) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MagiskInit::preset() {
|
void BaseInit::re_exec_init() {
|
||||||
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() {
|
|
||||||
LOGD("Re-exec /init\n");
|
LOGD("Re-exec /init\n");
|
||||||
cleanup();
|
cleanup();
|
||||||
execv("/init", argv);
|
execv("/init", argv);
|
||||||
exit(1);
|
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
|
// Prevent file descriptor confusion
|
||||||
mknod("/null", S_IFCHR | 0666, makedev(1, 3));
|
mknod("/null", S_IFCHR | 0666, makedev(1, 3));
|
||||||
int null = open("/null", O_RDWR | O_CLOEXEC);
|
int null = open("/null", O_RDWR | O_CLOEXEC);
|
||||||
@ -180,27 +153,27 @@ void MagiskInit::start() {
|
|||||||
re_exec_init();
|
re_exec_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MagiskInit::test() {
|
class RecoveryInit : public BaseInit {
|
||||||
cmdline_logging();
|
public:
|
||||||
log_cb.ex = nop_ex;
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
chdir(dirname(argv[0]));
|
class TestInit : public SARInit {
|
||||||
chroot(".");
|
public:
|
||||||
chdir("/");
|
TestInit(char *argv[], cmdline *cmd) : SARInit(argv, cmd) {};
|
||||||
|
void start() override {
|
||||||
preset();
|
preset();
|
||||||
early_mount();
|
early_mount();
|
||||||
setup_rootfs();
|
setup_rootfs();
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
static int test_main(int, char *argv[]) {
|
|
||||||
cmdline cmd{};
|
|
||||||
load_kernel_info(&cmd);
|
|
||||||
MagiskInit init(argv, &cmd);
|
|
||||||
init.test();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
umask(0);
|
umask(0);
|
||||||
@ -217,16 +190,40 @@ int main(int argc, char *argv[]) {
|
|||||||
return dump_manager(argv[3], 0644);
|
return dump_manager(argv[3], 0644);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getpid() != 1)
|
#ifdef MAGISK_DEBUG
|
||||||
return 1;
|
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{};
|
cmdline cmd{};
|
||||||
load_kernel_info(&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
|
// Run the main routine
|
||||||
init.start();
|
init->start();
|
||||||
}
|
}
|
||||||
|
@ -11,21 +11,21 @@ struct raw_data {
|
|||||||
size_t sz;
|
size_t sz;
|
||||||
};
|
};
|
||||||
|
|
||||||
class MagiskInit {
|
class BaseInit {
|
||||||
private:
|
protected:
|
||||||
cmdline *cmd;
|
cmdline *cmd;
|
||||||
raw_data self{};
|
raw_data self{};
|
||||||
raw_data config{};
|
raw_data config{};
|
||||||
int root = -1;
|
|
||||||
char **argv;
|
char **argv;
|
||||||
|
int root = -1;
|
||||||
bool load_sepol = false;
|
bool load_sepol = false;
|
||||||
bool mnt_system = false;
|
bool mnt_system = false;
|
||||||
bool mnt_vendor = false;
|
bool mnt_vendor = false;
|
||||||
bool mnt_product = false;
|
bool mnt_product = false;
|
||||||
bool mnt_odm = false;
|
bool mnt_odm = false;
|
||||||
|
|
||||||
void preset();
|
virtual void preset() {};
|
||||||
void early_mount();
|
virtual void early_mount() {}
|
||||||
void setup_rootfs();
|
void setup_rootfs();
|
||||||
bool read_dt_fstab(const char *name, char *partname, char *fstype);
|
bool read_dt_fstab(const char *name, char *partname, char *fstype);
|
||||||
bool patch_sepolicy();
|
bool patch_sepolicy();
|
||||||
@ -33,9 +33,25 @@ private:
|
|||||||
void re_exec_init();
|
void re_exec_init();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit MagiskInit(char *argv[], cmdline *cmd) : cmd(cmd), argv(argv) {}
|
BaseInit(char *argv[], cmdline *cmd) : cmd(cmd), argv(argv) {}
|
||||||
void start();
|
virtual ~BaseInit() = default;
|
||||||
void test();
|
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) {
|
static inline bool is_lnk(const char *name) {
|
||||||
|
@ -37,7 +37,7 @@ constexpr const char wrapper[] =
|
|||||||
"exec /sbin/magisk.bin \"$0\" \"$@\"\n"
|
"exec /sbin/magisk.bin \"$0\" \"$@\"\n"
|
||||||
;
|
;
|
||||||
|
|
||||||
void MagiskInit::setup_rootfs() {
|
void BaseInit::setup_rootfs() {
|
||||||
bool patch_init = patch_sepolicy();
|
bool patch_init = patch_sepolicy();
|
||||||
|
|
||||||
if (cmd->system_as_root) {
|
if (cmd->system_as_root) {
|
||||||
@ -173,7 +173,7 @@ void MagiskInit::setup_rootfs() {
|
|||||||
close(sbin);
|
close(sbin);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MagiskInit::patch_sepolicy() {
|
bool BaseInit::patch_sepolicy() {
|
||||||
bool patch_init = false;
|
bool patch_init = false;
|
||||||
|
|
||||||
if (access(SPLIT_PLAT_CIL, R_OK) == 0) {
|
if (access(SPLIT_PLAT_CIL, R_OK) == 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user