2019-03-05 09:22:20 +01:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/wait.h>
|
2017-04-30 19:58:52 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.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;
|
|
|
|
|
2019-03-22 06:50:33 +01:00
|
|
|
static bool no_secure_dir = false;
|
2019-09-17 06:21:07 +02:00
|
|
|
static bool pfs_done = false;
|
2020-05-08 09:45:11 +02:00
|
|
|
static bool safe_mode = false;
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2020-04-18 11:00:48 +02:00
|
|
|
/*********
|
|
|
|
* Setup *
|
|
|
|
*********/
|
2020-04-12 14:34:56 +02:00
|
|
|
|
|
|
|
#define DIR_IS(part) (me->mnt_dir == "/" #part ""sv)
|
|
|
|
#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
|
|
|
|
2020-04-21 08:57:29 +02:00
|
|
|
#define mount_mirror(part, flag) \
|
|
|
|
else if (DIR_IS(part) && me->mnt_type != "tmpfs"sv && lstat(me->mnt_dir, &st) == 0) { \
|
2020-04-12 14:34:56 +02:00
|
|
|
SETMIR(buf1, part); \
|
|
|
|
SETBLK(buf2, part); \
|
2020-04-21 08:57:29 +02:00
|
|
|
mknod(buf2, S_IFBLK | 0600, st.st_dev); \
|
2020-04-12 14:34:56 +02:00
|
|
|
xmkdir(buf1, 0755); \
|
|
|
|
xmount(buf2, buf1, me->mnt_type, flag, nullptr); \
|
2020-04-18 11:00:48 +02:00
|
|
|
LOGI("mount: %s\n", buf1); \
|
2019-04-08 05:03:49 +02:00
|
|
|
}
|
2019-04-07 20:22:45 +02:00
|
|
|
|
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); \
|
|
|
|
}
|
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
static bool magisk_env() {
|
|
|
|
LOGI("* Initializing Magisk environment\n");
|
2017-12-25 20:23:58 +01:00
|
|
|
|
2019-12-17 22:38:12 +01:00
|
|
|
string pkg;
|
|
|
|
check_manager(&pkg);
|
|
|
|
|
2020-04-12 14:34:56 +02:00
|
|
|
char buf1[4096];
|
|
|
|
char buf2[4096];
|
|
|
|
|
|
|
|
sprintf(buf1, "%s/0/%s/install", APP_DATA_DIR, pkg.data());
|
2019-12-17 22:38:12 +01:00
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
// Alternative binaries paths
|
2020-04-12 14:34:56 +02:00
|
|
|
const char *alt_bin[] = { "/cache/data_adb/magisk", "/data/magisk", buf1 };
|
2019-12-17 22:38:12 +01:00
|
|
|
for (auto alt : alt_bin) {
|
2018-11-13 07:53:48 +01:00
|
|
|
struct stat st;
|
2020-04-21 08:57:29 +02:00
|
|
|
if (lstat(alt, &st) == 0) {
|
2018-11-13 07:53:48 +01:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
|
|
|
unlink(alt);
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-21 08:57:29 +02:00
|
|
|
rm_rf(DATABIN);
|
2018-11-13 07:53:48 +01:00
|
|
|
cp_afc(alt, DATABIN);
|
|
|
|
rm_rf(alt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 10:45:47 +01:00
|
|
|
// Remove stuffs
|
|
|
|
rm_rf("/cache/data_adb");
|
2019-12-29 20:52:58 +01:00
|
|
|
rm_rf("/data/adb/modules/.core");
|
2020-03-24 00:24:18 +01:00
|
|
|
unlink("/data/adb/magisk.img");
|
|
|
|
unlink("/data/adb/magisk_merge.img");
|
2018-11-13 07:53:48 +01:00
|
|
|
unlink("/data/magisk.img");
|
2020-03-24 00:24:18 +01:00
|
|
|
unlink("/data/magisk_merge.img");
|
2018-11-13 07:53:48 +01:00
|
|
|
unlink("/data/magisk_debug.log");
|
|
|
|
|
2020-04-12 14:34:56 +02:00
|
|
|
sprintf(buf1, "%s/" MODULEMNT, MAGISKTMP.data());
|
|
|
|
xmkdir(buf1, 0755);
|
2020-03-23 04:59:28 +01:00
|
|
|
|
2019-03-22 06:50:33 +01:00
|
|
|
// Directories in /data/adb
|
|
|
|
xmkdir(DATABIN, 0755);
|
2019-02-12 08:14:57 +01:00
|
|
|
xmkdir(MODULEROOT, 0755);
|
2018-11-16 04:55:28 +01:00
|
|
|
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
|
|
|
|
xmkdir(SECURE_DIR "/service.d", 0755);
|
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
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)
|
|
|
|
else if (SDK_INT >= 24 && DIR_IS(proc) && !strstr(me->mnt_opts, "hidepid=2")) {
|
2018-11-28 07:27:32 +01:00
|
|
|
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
|
2018-11-13 07:53:48 +01:00
|
|
|
}
|
2019-02-18 04:30:23 +01:00
|
|
|
return true;
|
|
|
|
});
|
2020-04-12 14:34:56 +02:00
|
|
|
SETMIR(buf1, system);
|
|
|
|
SETMIR(buf2, system_root);
|
|
|
|
if (access(buf1, F_OK) != 0 && access(buf2, F_OK) == 0) {
|
|
|
|
xsymlink("./system_root/system", buf1);
|
2020-04-18 11:00:48 +02:00
|
|
|
LOGI("link: %s\n", buf1);
|
2019-06-26 08:31:59 +02:00
|
|
|
}
|
2020-04-21 08:57:29 +02:00
|
|
|
link_mirror(vendor);
|
|
|
|
link_mirror(product);
|
|
|
|
link_mirror(system_ext);
|
2018-11-13 07:53:48 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
// TODO: Remove. Backwards compatibility for old manager
|
|
|
|
LOGI("* Setting up internal busybox\n");
|
|
|
|
sprintf(buf1, "%s/" BBPATH "/busybox", MAGISKTMP.data());
|
|
|
|
mkdir(dirname(buf1), 0755);
|
|
|
|
cp_afc(DATABIN "/busybox", buf1);
|
|
|
|
exec_command_sync(buf1, "--install", "-s", dirname(buf1));
|
2019-03-22 07:01:36 +01:00
|
|
|
|
2018-11-16 04:55:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-13 07:53:48 +01:00
|
|
|
|
2020-04-30 10:26:50 +02:00
|
|
|
void reboot() {
|
2019-11-01 08:07:12 +01:00
|
|
|
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;
|
|
|
|
bool data = false;
|
2019-03-06 02:27:09 +01:00
|
|
|
file_readline("/proc/mounts", [&](string_view s) -> bool {
|
2019-02-18 04:30:23 +01:00
|
|
|
if (str_contains(s, " /data ") && !str_contains(s, "tmpfs"))
|
2018-11-04 09:38:06 +01:00
|
|
|
mnt = true;
|
2019-02-18 04:30:23 +01:00
|
|
|
return true;
|
|
|
|
});
|
2018-10-13 03:46:09 +02:00
|
|
|
if (mnt) {
|
2019-01-20 05:59:37 +01:00
|
|
|
auto crypto = getprop("ro.crypto.state");
|
2018-11-07 08:10:38 +01:00
|
|
|
if (!crypto.empty()) {
|
|
|
|
if (crypto == "unencrypted") {
|
2018-10-13 03:46:09 +02:00
|
|
|
// Unencrypted, we can directly access data
|
2018-11-04 09:38:06 +01:00
|
|
|
data = true;
|
2018-10-13 03:46:09 +02:00
|
|
|
} else {
|
|
|
|
// Encrypted, check whether vold is started
|
2018-11-07 08:10:38 +01:00
|
|
|
data = !getprop("init.svc.vold").empty();
|
2018-10-13 03:46:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ro.crypto.state is not set, assume it's unencrypted
|
2018-11-04 09:38:06 +01:00
|
|
|
data = true;
|
2018-10-13 03:46:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 10:27:30 +01:00
|
|
|
static bool log_dump = false;
|
|
|
|
static void dump_logs() {
|
|
|
|
if (log_dump)
|
|
|
|
return;
|
|
|
|
int test = exec_command_sync("/system/bin/logcat", "-d", "-f", "/dev/null");
|
|
|
|
chmod("/dev/null", 0666);
|
|
|
|
if (test != 0)
|
|
|
|
return;
|
|
|
|
rename(LOGFILE, LOGFILE ".bak");
|
|
|
|
log_dump = true;
|
2019-02-14 23:36:18 +01:00
|
|
|
// Start a daemon thread and wait indefinitely
|
2019-05-26 06:42:51 +02:00
|
|
|
new_daemon_thread([]() -> void {
|
2019-02-14 23:36:18 +01:00
|
|
|
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) {
|
|
|
|
log_dump = false;
|
2019-05-26 06:42:51 +02:00
|
|
|
} else {
|
|
|
|
waitpid(pid, nullptr, 0);
|
2019-02-14 23:36:18 +01:00
|
|
|
}
|
|
|
|
});
|
2019-02-14 10:27:30 +01:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
/****************
|
|
|
|
* Entry points *
|
|
|
|
****************/
|
|
|
|
|
2018-11-16 04:55:28 +01:00
|
|
|
[[noreturn]] static void unblock_boot_process() {
|
2017-11-05 22:41:03 +01:00
|
|
|
close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0));
|
2018-11-04 09:38:06 +01:00
|
|
|
pthread_exit(nullptr);
|
2017-05-03 19:13:04 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 07:51:46 +01:00
|
|
|
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);
|
|
|
|
|
2018-04-21 20:16:56 +02:00
|
|
|
if (!check_data())
|
2018-06-10 20:26:18 +02:00
|
|
|
unblock_boot_process();
|
|
|
|
|
2019-02-14 10:27:30 +01:00
|
|
|
dump_logs();
|
|
|
|
|
2019-02-09 07:51:46 +01:00
|
|
|
LOGI("** post-fs-data mode running\n");
|
|
|
|
|
|
|
|
// Unlock all blocks for rw
|
|
|
|
unlock_blocks();
|
|
|
|
|
2018-06-10 20:26:18 +02:00
|
|
|
if (access(SECURE_DIR, F_OK) != 0) {
|
|
|
|
/* If the folder is not automatically created by the system,
|
|
|
|
* do NOT proceed further. Manual creation of the folder
|
|
|
|
* will cause bootloops on FBE devices. */
|
2018-11-16 04:55:28 +01:00
|
|
|
LOGE(SECURE_DIR " is not present, abort...");
|
2019-03-22 06:50:33 +01:00
|
|
|
no_secure_dir = true;
|
2018-06-10 20:26:18 +02:00
|
|
|
unblock_boot_process();
|
|
|
|
}
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
if (!magisk_env()) {
|
2018-11-16 04:55:28 +01:00
|
|
|
LOGE("* Magisk environment setup incomplete, abort\n");
|
|
|
|
unblock_boot_process();
|
2018-11-04 09:38:06 +01:00
|
|
|
}
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2020-05-08 09:45:11 +02:00
|
|
|
if (getprop("persist.sys.safemode", true) == "1") {
|
|
|
|
safe_mode = true;
|
|
|
|
// Disable all modules and magiskhide so next boot will be clean
|
|
|
|
foreach_modules("disable");
|
|
|
|
stop_magiskhide();
|
|
|
|
} else {
|
|
|
|
LOGI("* Running post-fs-data.d scripts\n");
|
|
|
|
exec_common_script("post-fs-data");
|
|
|
|
handle_modules();
|
|
|
|
auto_start_magiskhide();
|
|
|
|
}
|
2017-05-03 21:05:37 +02:00
|
|
|
|
2020-05-08 09:45:11 +02:00
|
|
|
// We still want to do magic mount because root itself might need it
|
|
|
|
magic_mount();
|
2020-05-09 11:43:18 +02:00
|
|
|
pfs_done = true;
|
2020-05-08 09:45:11 +02:00
|
|
|
unblock_boot_process();
|
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);
|
|
|
|
|
2019-02-14 10:27:30 +01:00
|
|
|
dump_logs();
|
|
|
|
|
2019-03-22 06:50:33 +01:00
|
|
|
if (no_secure_dir) {
|
2018-06-10 20:26:18 +02:00
|
|
|
// It's safe to create the folder at this point if the system didn't create it
|
2019-03-22 06:50:33 +01:00
|
|
|
if (access(SECURE_DIR, F_OK) != 0)
|
|
|
|
xmkdir(SECURE_DIR, 0700);
|
2018-11-16 04:55:28 +01:00
|
|
|
// And reboot to make proper setup possible
|
2019-09-13 09:14:21 +02:00
|
|
|
reboot();
|
2018-06-10 20:26:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 09:45:11 +02:00
|
|
|
if (!pfs_done || safe_mode)
|
2019-09-17 06:21:07 +02:00
|
|
|
return;
|
|
|
|
|
2018-08-02 19:58:56 +02:00
|
|
|
auto_start_magiskhide();
|
|
|
|
|
2017-08-12 19:15:18 +02:00
|
|
|
LOGI("* Running service.d scripts\n");
|
|
|
|
exec_common_script("service");
|
|
|
|
|
2020-05-08 09:45:11 +02:00
|
|
|
LOGI("* Running module service scripts\n");
|
|
|
|
exec_module_script("service", module_list);
|
2019-04-05 13:00:30 +02:00
|
|
|
|
|
|
|
// All boot stage done, cleanup
|
|
|
|
module_list.clear();
|
|
|
|
module_list.shrink_to_fit();
|
|
|
|
}
|
2017-06-11 10:51:44 +02:00
|
|
|
|
2019-04-05 13:00:30 +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-08 09:45:11 +02:00
|
|
|
if (!pfs_done || safe_mode)
|
2019-09-17 06:21:07 +02:00
|
|
|
return;
|
|
|
|
|
2019-11-01 07:12:28 +01:00
|
|
|
auto_start_magiskhide();
|
|
|
|
|
2017-05-08 19:16:58 +02:00
|
|
|
if (access(MANAGERAPK, F_OK) == 0) {
|
2018-06-13 23:09:54 +02:00
|
|
|
// Install Magisk Manager if exists
|
2018-01-11 13:57:05 +01:00
|
|
|
rename(MANAGERAPK, "/data/magisk.apk");
|
2018-06-13 23:09:54 +02:00
|
|
|
install_apk("/data/magisk.apk");
|
|
|
|
} else {
|
2019-12-17 22:38:12 +01:00
|
|
|
// 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");
|
2018-11-16 09:20:30 +01:00
|
|
|
install_apk("/data/magisk.apk");
|
2017-05-08 19:16:58 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|