Magisk/native/jni/core/bootstages.cpp

398 lines
9.2 KiB
C++
Raw Normal View History

#include <sys/mount.h>
#include <sys/wait.h>
#include <sys/sysmacros.h>
#include <linux/input.h>
2020-04-12 14:34:56 +02:00
#include <libgen.h>
2019-01-20 05:59:37 +01:00
#include <vector>
#include <string>
2017-04-30 19:58:52 +02:00
2020-03-09 09:50:30 +01:00
#include <magisk.hpp>
#include <db.hpp>
#include <utils.hpp>
#include <daemon.hpp>
#include <resetprop.hpp>
#include <selinux.hpp>
2017-04-30 19:58:52 +02:00
2019-01-20 05:59:37 +01:00
using namespace std;
static bool pfs_done = false;
static bool safe_mode = false;
2017-04-30 19:58:52 +02:00
/*********
* Setup *
*********/
2020-04-12 14:34:56 +02:00
#define MNT_DIR_IS(dir) (me->mnt_dir == string_view(dir))
2020-04-12 14:34:56 +02:00
#define SETMIR(b, part) sprintf(b, "%s/" MIRRDIR "/" #part, MAGISKTMP.data())
#define SETBLK(b, part) sprintf(b, "%s/" BLOCKDIR "/" #part, MAGISKTMP.data())
2019-04-08 05:03:49 +02:00
#define do_mount_mirror(part, flag) {\
SETMIR(buf1, part); \
SETBLK(buf2, part); \
unlink(buf2); \
mknod(buf2, S_IFBLK | 0600, st.st_dev); \
xmkdir(buf1, 0755); \
xmount(buf2, buf1, me->mnt_type, flag, nullptr); \
LOGI("mount: %s\n", buf1); \
2019-04-08 05:03:49 +02:00
}
2019-04-07 20:22:45 +02:00
#define mount_mirror(part, flag) \
else if (MNT_DIR_IS("/" #part) && me->mnt_type != "tmpfs"sv && lstat(me->mnt_dir, &st) == 0) \
do_mount_mirror(part, flag)
2020-04-21 08:57:29 +02:00
#define link_mirror(part) \
SETMIR(buf1, part); \
if (access("/system/" #part, F_OK) == 0 && access(buf1, F_OK) != 0) { \
xsymlink("./system/" #part, buf1); \
LOGI("link: %s\n", buf1); \
}
#define link_orig_dir(dir, part) \
else if (MNT_DIR_IS(dir) && me->mnt_type != "tmpfs"sv) { \
SETMIR(buf1, part); \
rmdir(buf1); \
xsymlink(dir, buf1); \
LOGI("link: %s\n", buf1); \
}
#define link_orig(part) link_orig_dir("/" #part, part)
static void mount_mirrors() {
2020-04-12 14:34:56 +02:00
char buf1[4096];
char buf2[4096];
LOGI("* Mounting mirrors");
2020-04-21 08:57:29 +02:00
2019-06-23 12:53:41 +02:00
parse_mnt("/proc/mounts", [&](mntent *me) {
2020-04-21 08:57:29 +02:00
struct stat st;
if (0) {}
mount_mirror(system, MS_RDONLY)
mount_mirror(vendor, MS_RDONLY)
mount_mirror(product, MS_RDONLY)
mount_mirror(system_ext, MS_RDONLY)
mount_mirror(data, 0)
link_orig(cache)
link_orig(metadata)
link_orig(persist)
link_orig_dir("/mnt/vendor/persist", persist)
else if (SDK_INT >= 24 && MNT_DIR_IS("/proc") && !strstr(me->mnt_opts, "hidepid=2")) {
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
}
return true;
});
2020-04-12 14:34:56 +02:00
SETMIR(buf1, system);
if (access(buf1, F_OK) != 0) {
2020-04-12 14:34:56 +02:00
xsymlink("./system_root/system", buf1);
LOGI("link: %s\n", buf1);
parse_mnt("/proc/mounts", [&](mntent *me) {
struct stat st;
if (MNT_DIR_IS("/") && me->mnt_type != "rootfs"sv && stat("/", &st) == 0) {
do_mount_mirror(system_root, MS_RDONLY)
return false;
}
return true;
});
2019-06-26 08:31:59 +02:00
}
link_mirror(vendor)
link_mirror(product)
link_mirror(system_ext)
}
static bool magisk_env() {
char buf[4096];
LOGI("* Initializing Magisk environment\n");
string pkg;
check_manager(&pkg);
sprintf(buf, "%s/0/%s/install", APP_DATA_DIR, pkg.data());
// Alternative binaries paths
const char *alt_bin[] = { "/cache/data_adb/magisk", "/data/magisk", buf };
for (auto alt : alt_bin) {
struct stat st;
if (lstat(alt, &st) == 0) {
if (S_ISLNK(st.st_mode)) {
unlink(alt);
continue;
}
rm_rf(DATABIN);
cp_afc(alt, DATABIN);
rm_rf(alt);
break;
}
}
// Remove stuffs
rm_rf("/cache/data_adb");
rm_rf("/data/adb/modules/.core");
unlink("/data/adb/magisk.img");
unlink("/data/adb/magisk_merge.img");
unlink("/data/magisk.img");
unlink("/data/magisk_merge.img");
unlink("/data/magisk_debug.log");
// Directories in /data/adb
xmkdir(DATABIN, 0755);
xmkdir(MODULEROOT, 0755);
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
xmkdir(SECURE_DIR "/service.d", 0755);
2020-02-01 07:22:31 +01:00
// Disable/remove magiskhide, resetprop
2019-01-20 23:52:19 +01:00
if (SDK_INT < 19) {
unlink("/sbin/resetprop");
unlink("/sbin/magiskhide");
}
2019-03-22 07:01:36 +01:00
if (access(DATABIN "/busybox", X_OK) == -1)
return false;
2020-04-12 14:34:56 +02:00
sprintf(buf, "%s/" BBPATH "/busybox", MAGISKTMP.data());
mkdir(dirname(buf), 0755);
cp_afc(DATABIN "/busybox", buf);
2020-11-07 23:36:13 +01:00
exec_command_async(buf, "--install", "-s", dirname(buf));
2019-03-22 07:01:36 +01:00
return true;
}
2020-04-30 10:26:50 +02:00
void reboot() {
if (RECOVERY_MODE)
exec_command_sync("/system/bin/reboot", "recovery");
else
exec_command_sync("/system/bin/reboot");
}
2018-11-04 09:38:06 +01:00
static bool check_data() {
bool mnt = false;
2020-05-18 13:36:46 +02:00
file_readline("/proc/mounts", [&](string_view s) {
if (str_contains(s, " /data ") && !str_contains(s, "tmpfs")) {
2018-11-04 09:38:06 +01:00
mnt = true;
2020-05-18 13:36:46 +02:00
return false;
}
return true;
});
2020-05-18 13:36:46 +02:00
if (!mnt)
return false;
auto crypto = getprop("ro.crypto.state");
if (!crypto.empty()) {
if (crypto == "unencrypted") {
// Unencrypted, we can directly access data
return true;
2018-10-13 03:46:09 +02:00
} else {
2020-05-18 13:36:46 +02:00
// Encrypted, check whether vold is started
return !getprop("init.svc.vold").empty();
2018-10-13 03:46:09 +02:00
}
}
2020-05-18 13:36:46 +02:00
// ro.crypto.state is not set, assume it's unencrypted
return true;
2018-10-13 03:46:09 +02:00
}
void unlock_blocks() {
int fd, dev, OFF = 0;
2019-12-13 06:37:06 +01:00
auto dir = xopen_dir("/dev/block");
if (!dir)
2018-10-13 03:46:09 +02:00
return;
2019-12-13 06:37:06 +01:00
dev = dirfd(dir.get());
2018-10-13 03:46:09 +02:00
2019-12-13 06:37:06 +01:00
for (dirent *entry; (entry = readdir(dir.get()));) {
2018-10-13 03:46:09 +02:00
if (entry->d_type == DT_BLK) {
2019-04-30 02:04:39 +02:00
if ((fd = openat(dev, entry->d_name, O_RDONLY | O_CLOEXEC)) < 0)
2018-10-13 03:46:09 +02:00
continue;
2019-04-30 02:04:39 +02:00
if (ioctl(fd, BLKROSET, &OFF) < 0)
2018-10-13 03:46:09 +02:00
PLOGE("unlock %s", entry->d_name);
close(fd);
}
}
}
2020-05-10 09:30:11 +02:00
static void collect_logs(bool reset) {
static bool running = false;
static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
{
mutex_guard lock(log_lock);
if (running)
return;
int test = exec_command_sync("/system/bin/logcat", "-d", "-f", "/dev/null");
chmod("/dev/null", 0666);
if (test != 0)
return;
running = true;
}
if (reset)
rename(LOGFILE, LOGFILE ".bak");
// Start a daemon thread and wait indefinitely
2020-05-10 09:30:11 +02:00
new_daemon_thread([]{
int fd = xopen(LOGFILE, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
exec_t exec {
.fd = fd,
.fork = fork_no_zombie
};
int pid = exec_command(exec, "/system/bin/logcat", "-s", "Magisk");
close(fd);
if (pid < 0) {
2020-05-10 09:30:11 +02:00
mutex_guard lock(log_lock);
running = false;
2019-05-26 06:42:51 +02:00
} else {
waitpid(pid, nullptr, 0);
}
});
2019-02-14 10:27:30 +01:00
}
#define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8)))
static bool check_key_combo() {
uint8_t bitmask[(KEY_MAX + 1) / 8];
vector<int> events;
constexpr char name[] = "/dev/.ev";
// First collect candidate events that accepts volume down
for (int minor = 64; minor < 96; ++minor) {
if (xmknod(name, S_IFCHR | 0444, makedev(13, minor)))
continue;
int fd = open(name, O_RDONLY | O_CLOEXEC);
unlink(name);
if (fd < 0)
continue;
memset(bitmask, 0, sizeof(bitmask));
ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitmask)), bitmask);
if (test_bit(KEY_VOLUMEDOWN, bitmask))
events.push_back(fd);
else
close(fd);
}
if (events.empty())
return false;
run_finally fin([&]{ std::for_each(events.begin(), events.end(), close); });
// Check if volume down key is held continuously for more than 3 seconds
for (int i = 0; i < 300; ++i) {
bool pressed = false;
for (const int &fd : events) {
memset(bitmask, 0, sizeof(bitmask));
ioctl(fd, EVIOCGKEY(sizeof(bitmask)), bitmask);
if (test_bit(KEY_VOLUMEDOWN, bitmask)) {
pressed = true;
break;
}
}
if (!pressed)
return false;
// Check every 10ms
usleep(10000);
}
LOGD("KEY_VOLUMEDOWN detected: enter safe mode\n");
return true;
}
2017-04-30 19:58:52 +02:00
/****************
* Entry points *
****************/
void post_fs_data(int client) {
// ack
write_int(client, 0);
close(client);
2019-06-22 12:14:33 +02:00
if (getenv("REMOUNT_ROOT"))
xmount(nullptr, "/", nullptr, MS_REMOUNT | MS_RDONLY, nullptr);
if (!check_data())
2020-05-19 08:29:26 +02:00
goto unblock_init;
2020-05-10 09:30:11 +02:00
collect_logs(true);
2019-02-14 10:27:30 +01:00
LOGI("** post-fs-data mode running\n");
unlock_blocks();
mount_mirrors();
if (access(SECURE_DIR, F_OK) != 0) {
2020-05-19 08:29:26 +02:00
if (SDK_INT < 24) {
// There is no FBE pre 7.0, we can directly create the folder without issues
xmkdir(SECURE_DIR, 0700);
} else {
// If the folder is not automatically created by Android,
// do NOT proceed further. Manual creation of the folder
// will cause bootloops on FBE devices.
LOGE(SECURE_DIR " is not present, abort\n");
goto early_abort;
2020-05-19 08:29:26 +02:00
}
}
2017-04-30 19:58:52 +02:00
if (!magisk_env()) {
LOGE("* Magisk environment incomplete, abort\n");
goto early_abort;
2018-11-04 09:38:06 +01:00
}
2017-04-30 19:58:52 +02:00
if (getprop("persist.sys.safemode", true) == "1" || check_key_combo()) {
safe_mode = true;
// Disable all modules and magiskhide so next boot will be clean
disable_modules();
stop_magiskhide();
} else {
2020-05-18 14:36:02 +02:00
exec_common_scripts("post-fs-data");
auto_start_magiskhide();
2020-05-18 12:51:41 +02:00
handle_modules();
}
2017-05-03 21:05:37 +02:00
pfs_done = true;
early_abort:
2020-05-19 08:29:26 +02:00
// We still do magic mount because root itself might need it
magic_mount();
2020-05-19 08:29:26 +02:00
unblock_init:
close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0));
2017-04-30 19:58:52 +02:00
}
void late_start(int client) {
LOGI("** late_start service mode running\n");
// ack
write_int(client, 0);
close(client);
2020-05-10 09:30:11 +02:00
collect_logs(false);
2019-02-14 10:27:30 +01:00
if (!pfs_done || safe_mode)
return;
2020-05-18 14:36:02 +02:00
exec_common_scripts("service");
exec_module_scripts("service");
}
2017-06-11 10:51:44 +02:00
void boot_complete(int client) {
LOGI("** boot_complete triggered\n");
// ack
write_int(client, 0);
close(client);
2017-04-30 19:58:52 +02:00
2020-05-10 09:30:11 +02:00
collect_logs(false);
2020-05-19 08:29:26 +02:00
if (safe_mode)
return;
// At this point it's safe to create the folder
2020-05-19 08:29:26 +02:00
if (access(SECURE_DIR, F_OK) != 0)
xmkdir(SECURE_DIR, 0700);
if (pfs_done)
auto_start_magiskhide();
if (access(MANAGERAPK, F_OK) == 0) {
2018-06-13 23:09:54 +02:00
// Install Magisk Manager if exists
rename(MANAGERAPK, "/data/magisk.apk");
2018-06-13 23:09:54 +02:00
install_apk("/data/magisk.apk");
} else {
// Check whether we have manager installed
if (!check_manager()) {
// Install stub
2020-05-09 11:43:18 +02:00
auto init = MAGISKTMP + "/magiskinit";
exec_command_sync(init.data(), "-x", "manager", "/data/magisk.apk");
install_apk("/data/magisk.apk");
}
}
2017-04-30 19:58:52 +02:00
}