2019-04-01 03:01:05 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <linux/input.h>
|
2017-11-06 05:41:03 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2019-02-09 05:23:56 -05:00
|
|
|
#include <functional>
|
|
|
|
#include <string_view>
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2018-08-10 05:03:54 +08:00
|
|
|
#include <xz.h>
|
2019-02-10 03:57:51 -05:00
|
|
|
#include <magisk.h>
|
|
|
|
#include <magiskpolicy.h>
|
|
|
|
#include <selinux.h>
|
2019-02-24 20:39:01 -05:00
|
|
|
#include <cpio.h>
|
2019-02-10 03:57:51 -05:00
|
|
|
#include <utils.h>
|
|
|
|
#include <flags.h>
|
2018-08-10 05:03:54 +08:00
|
|
|
|
2018-08-09 18:13:07 +08:00
|
|
|
#include "binaries.h"
|
2019-02-24 08:13:27 -05:00
|
|
|
#ifdef USE_64BIT
|
|
|
|
#include "binaries_arch64.h"
|
2019-04-03 17:25:47 -04:00
|
|
|
#define LIBNAME "lib64"
|
2019-02-24 08:13:27 -05:00
|
|
|
#else
|
2018-08-09 18:13:07 +08:00
|
|
|
#include "binaries_arch.h"
|
2019-04-03 17:25:47 -04:00
|
|
|
#define LIBNAME "lib"
|
2019-02-24 08:13:27 -05:00
|
|
|
#endif
|
2017-11-28 03:42:48 +08:00
|
|
|
#include "magiskrc.h"
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
using namespace std;
|
|
|
|
|
2018-07-18 00:45:10 +08:00
|
|
|
#define DEFAULT_DT_DIR "/proc/device-tree/firmware/android"
|
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
static FILE *kmsg;
|
|
|
|
static int vprintk(const char *fmt, va_list ap) {
|
2019-04-29 19:53:22 -04:00
|
|
|
fprintf(kmsg, "magiskinit: ");
|
|
|
|
return vfprintf(kmsg, fmt, ap);
|
2019-04-04 00:26:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_klog() {
|
|
|
|
mknod("/kmsg", S_IFCHR | 0666, makedev(1, 11));
|
2019-04-24 00:13:48 -04:00
|
|
|
int fd = xopen("/kmsg", O_WRONLY | O_CLOEXEC);
|
|
|
|
kmsg = fdopen(fd, "w");
|
2019-04-04 00:26:16 -04:00
|
|
|
setbuf(kmsg, nullptr);
|
|
|
|
unlink("/kmsg");
|
|
|
|
log_cb.d = log_cb.i = log_cb.w = log_cb.e = vprintk;
|
|
|
|
log_cb.ex = nop_ex;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define setup_klog(...)
|
|
|
|
#endif
|
|
|
|
|
2019-03-16 04:45:56 -04:00
|
|
|
static int test_main(int argc, char *argv[]);
|
|
|
|
|
|
|
|
constexpr const char *init_applet[] =
|
|
|
|
{ "magiskpolicy", "supolicy", "init_test", nullptr };
|
|
|
|
constexpr int (*init_applet_main[])(int, char *[]) =
|
|
|
|
{ magiskpolicy_main, magiskpolicy_main, test_main, nullptr };
|
2019-02-09 01:51:46 -05:00
|
|
|
|
2017-11-06 05:41:03 +08:00
|
|
|
struct cmdline {
|
2019-03-15 14:46:30 -04:00
|
|
|
bool system_as_root;
|
2017-11-06 05:41:03 +08:00
|
|
|
char slot[3];
|
2018-07-18 00:45:10 +08:00
|
|
|
char dt_dir[128];
|
|
|
|
};
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
struct raw_data {
|
|
|
|
void *buf;
|
|
|
|
size_t sz;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool unxz(int fd, const uint8_t *buf, size_t size) {
|
|
|
|
uint8_t out[8192];
|
|
|
|
xz_crc32_init();
|
|
|
|
struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26);
|
|
|
|
struct xz_buf b = {
|
|
|
|
.in = buf,
|
|
|
|
.in_pos = 0,
|
|
|
|
.in_size = size,
|
|
|
|
.out = out,
|
|
|
|
.out_pos = 0,
|
|
|
|
.out_size = sizeof(out)
|
|
|
|
};
|
|
|
|
enum xz_ret ret;
|
|
|
|
do {
|
|
|
|
ret = xz_dec_run(dec, &b);
|
|
|
|
if (ret != XZ_OK && ret != XZ_STREAM_END)
|
|
|
|
return false;
|
|
|
|
write(fd, out, b.out_pos);
|
|
|
|
b.out_pos = 0;
|
|
|
|
} while (b.in_pos != size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void decompress_ramdisk() {
|
|
|
|
constexpr char tmp[] = "tmp.cpio";
|
|
|
|
constexpr char ramdisk_xz[] = "ramdisk.cpio.xz";
|
|
|
|
if (access(ramdisk_xz, F_OK))
|
|
|
|
return;
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("Decompressing ramdisk from %s\n", ramdisk_xz);
|
2019-03-15 14:46:30 -04:00
|
|
|
uint8_t *buf;
|
|
|
|
size_t sz;
|
|
|
|
mmap_ro(ramdisk_xz, buf, sz);
|
|
|
|
int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC);
|
|
|
|
unxz(fd, buf, sz);
|
|
|
|
munmap(buf, sz);
|
|
|
|
close(fd);
|
|
|
|
cpio_mmap cpio(tmp);
|
|
|
|
cpio.extract();
|
|
|
|
unlink(tmp);
|
|
|
|
unlink(ramdisk_xz);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_magisk(const char *path, mode_t mode) {
|
|
|
|
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
|
|
|
if (fd < 0)
|
|
|
|
return 1;
|
|
|
|
if (!unxz(fd, magisk_xz, sizeof(magisk_xz)))
|
|
|
|
return 1;
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_manager(const char *path, mode_t mode) {
|
|
|
|
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
|
|
|
if (fd < 0)
|
|
|
|
return 1;
|
|
|
|
if (!unxz(fd, manager_xz, sizeof(manager_xz)))
|
|
|
|
return 1;
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MagiskInit {
|
|
|
|
private:
|
|
|
|
cmdline cmd{};
|
2019-03-31 00:48:22 -04:00
|
|
|
raw_data self{};
|
2019-03-15 14:46:30 -04:00
|
|
|
raw_data config{};
|
|
|
|
int root = -1;
|
2019-03-16 17:15:48 -04:00
|
|
|
char **argv;
|
|
|
|
bool load_sepol = false;
|
2019-03-15 14:46:30 -04:00
|
|
|
bool mnt_system = false;
|
|
|
|
bool mnt_vendor = false;
|
|
|
|
bool mnt_product = false;
|
|
|
|
bool mnt_odm = false;
|
|
|
|
|
|
|
|
void load_kernel_info();
|
|
|
|
void preset();
|
|
|
|
void early_mount();
|
|
|
|
void setup_rootfs();
|
2019-04-04 00:26:16 -04:00
|
|
|
bool read_dt_fstab(const char *name, char *partname, char *partfs);
|
2019-03-15 14:46:30 -04:00
|
|
|
bool patch_sepolicy();
|
|
|
|
void cleanup();
|
2019-04-24 00:13:48 -04:00
|
|
|
void re_exec_init();
|
2019-03-15 14:46:30 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MagiskInit(char *argv[]) : argv(argv) {}
|
|
|
|
void start();
|
|
|
|
void test();
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void parse_cmdline(const std::function<void (std::string_view, const char *)> &fn) {
|
2019-02-09 05:23:56 -05:00
|
|
|
char cmdline[4096];
|
|
|
|
int fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
|
|
|
|
cmdline[read(fd, cmdline, sizeof(cmdline))] = '\0';
|
|
|
|
close(fd);
|
2017-11-23 18:38:12 +08:00
|
|
|
|
2019-02-09 05:23:56 -05:00
|
|
|
char *tok, *eql, *tmp, *saveptr;
|
|
|
|
saveptr = cmdline;
|
|
|
|
while ((tok = strtok_r(nullptr, " \n", &saveptr)) != nullptr) {
|
|
|
|
eql = strchr(tok, '=');
|
|
|
|
if (eql) {
|
|
|
|
*eql = '\0';
|
|
|
|
if (eql[1] == '"') {
|
|
|
|
tmp = strchr(saveptr, '"');
|
|
|
|
if (tmp != nullptr) {
|
|
|
|
*tmp = '\0';
|
|
|
|
saveptr[-1] = ' ';
|
|
|
|
saveptr = tmp + 1;
|
|
|
|
eql++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn(tok, eql + 1);
|
|
|
|
} else {
|
|
|
|
fn(tok, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 03:01:05 -04:00
|
|
|
#define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8)))
|
|
|
|
|
|
|
|
static bool check_key_combo() {
|
|
|
|
uint8_t bitmask[(KEY_MAX + 1) / 8];
|
2019-04-21 19:09:08 -04:00
|
|
|
vector<int> events;
|
|
|
|
constexpr const char *name = "/event";
|
2019-04-01 03:01:05 -04:00
|
|
|
|
|
|
|
for (int minor = 64; minor < 96; ++minor) {
|
2019-04-21 19:09:08 -04:00
|
|
|
if (mknod(name, S_IFCHR | 0444, makedev(13, minor))) {
|
2019-04-01 03:01:05 -04:00
|
|
|
PLOGE("mknod");
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-21 19:09:08 -04:00
|
|
|
int fd = open(name, O_RDONLY | O_CLOEXEC);
|
|
|
|
unlink(name);
|
|
|
|
if (fd < 0)
|
2019-04-01 03:01:05 -04:00
|
|
|
continue;
|
|
|
|
memset(bitmask, 0, sizeof(bitmask));
|
2019-04-21 19:09:08 -04:00
|
|
|
ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitmask)), bitmask);
|
|
|
|
if (test_bit(KEY_VOLUMEUP, bitmask))
|
|
|
|
events.push_back(fd);
|
2019-04-01 03:01:05 -04:00
|
|
|
}
|
|
|
|
|
2019-04-21 19:09:08 -04:00
|
|
|
if (events.empty())
|
2019-04-01 03:01:05 -04:00
|
|
|
return false;
|
|
|
|
|
2019-04-21 19:09:08 -04:00
|
|
|
RunFinally fin([&]() -> void {
|
|
|
|
for (const int &fd : events)
|
|
|
|
close(fd);
|
|
|
|
});
|
|
|
|
|
2019-04-01 03:01:05 -04:00
|
|
|
// Return true if volume key up is hold for more than 3 seconds
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < 500; ++i) {
|
2019-04-21 19:09:08 -04:00
|
|
|
for (const int &fd : events) {
|
|
|
|
memset(bitmask, 0, sizeof(bitmask));
|
|
|
|
ioctl(fd, EVIOCGKEY(sizeof(bitmask)), bitmask);
|
|
|
|
if (test_bit(KEY_VOLUMEUP, bitmask)) {
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 03:01:05 -04:00
|
|
|
if (count >= 300) {
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("KEY_VOLUMEUP detected: disable system-as-root\n");
|
2019-04-01 03:01:05 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Check every 10ms
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
void MagiskInit::load_kernel_info() {
|
|
|
|
// Communicate with kernel using procfs and sysfs
|
2019-04-04 00:26:16 -04:00
|
|
|
xmkdir("/proc", 0755);
|
2019-03-15 14:46:30 -04:00
|
|
|
xmount("proc", "/proc", "proc", 0, nullptr);
|
2019-04-04 00:26:16 -04:00
|
|
|
xmkdir("/sys", 0755);
|
2019-03-15 14:46:30 -04:00
|
|
|
xmount("sysfs", "/sys", "sysfs", 0, nullptr);
|
|
|
|
|
2019-03-16 17:15:48 -04:00
|
|
|
bool enter_recovery = false;
|
|
|
|
bool kirin = false;
|
2019-04-01 03:01:05 -04:00
|
|
|
bool recovery_mode = false;
|
2018-12-24 21:36:37 +08:00
|
|
|
|
2019-02-09 05:23:56 -05:00
|
|
|
parse_cmdline([&](auto key, auto value) -> void {
|
2019-03-14 22:27:29 -04:00
|
|
|
LOGD("cmdline: [%s]=[%s]\n", key.data(), value);
|
2019-02-09 05:23:56 -05:00
|
|
|
if (key == "androidboot.slot_suffix") {
|
2019-03-15 14:46:30 -04:00
|
|
|
strcpy(cmd.slot, value);
|
2019-02-09 05:23:56 -05:00
|
|
|
} else if (key == "androidboot.slot") {
|
2019-03-15 14:46:30 -04:00
|
|
|
cmd.slot[0] = '_';
|
|
|
|
strcpy(cmd.slot + 1, value);
|
2019-02-09 05:23:56 -05:00
|
|
|
} else if (key == "skip_initramfs") {
|
2019-03-31 00:48:22 -04:00
|
|
|
cmd.system_as_root = true;
|
2019-02-09 05:23:56 -05:00
|
|
|
} else if (key == "androidboot.android_dt_dir") {
|
2019-03-15 14:46:30 -04:00
|
|
|
strcpy(cmd.dt_dir, value);
|
2019-02-11 02:26:15 -05:00
|
|
|
} else if (key == "enter_recovery") {
|
2019-02-09 05:23:56 -05:00
|
|
|
enter_recovery = value[0] == '1';
|
|
|
|
} else if (key == "androidboot.hardware") {
|
|
|
|
kirin = strstr(value, "kirin") || strstr(value, "hi3660");
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
2019-02-09 05:23:56 -05:00
|
|
|
});
|
2018-12-25 01:08:46 +08:00
|
|
|
|
2019-03-30 00:49:48 -04:00
|
|
|
parse_prop_file("/.backup/.magisk", [&](auto key, auto value) -> bool {
|
|
|
|
if (key == "RECOVERYMODE" && value == "true")
|
2019-04-01 03:01:05 -04:00
|
|
|
recovery_mode = true;
|
2019-03-30 00:49:48 -04:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-12-25 01:08:46 +08:00
|
|
|
if (kirin && enter_recovery) {
|
|
|
|
// Inform that we are actually booting as recovery
|
2019-04-01 03:01:05 -04:00
|
|
|
if (!recovery_mode) {
|
2019-03-30 00:49:48 -04:00
|
|
|
if (FILE *f = fopen("/.backup/.magisk", "ae"); f) {
|
|
|
|
fprintf(f, "RECOVERYMODE=true\n");
|
|
|
|
fclose(f);
|
|
|
|
}
|
2019-04-01 03:01:05 -04:00
|
|
|
recovery_mode = true;
|
2019-03-14 22:27:29 -04:00
|
|
|
}
|
2018-12-25 01:08:46 +08:00
|
|
|
}
|
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
if (recovery_mode) {
|
|
|
|
LOGD("Running in recovery mode, waiting for key...\n");
|
2019-04-01 03:01:05 -04:00
|
|
|
cmd.system_as_root = !check_key_combo();
|
2019-04-04 00:26:16 -04:00
|
|
|
}
|
2019-04-01 03:01:05 -04:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
if (cmd.dt_dir[0] == '\0')
|
|
|
|
strcpy(cmd.dt_dir, DEFAULT_DT_DIR);
|
2019-03-14 22:27:29 -04:00
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("system_as_root=[%d]\n", cmd.system_as_root);
|
|
|
|
LOGD("slot=[%s]\n", cmd.slot);
|
|
|
|
LOGD("dt_dir=[%s]\n", cmd.dt_dir);
|
2019-03-15 14:46:30 -04:00
|
|
|
}
|
2018-02-14 00:57:52 +08:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
void MagiskInit::preset() {
|
|
|
|
root = open("/", O_RDONLY | O_CLOEXEC);
|
2018-07-18 00:45:10 +08:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
if (cmd.system_as_root) {
|
|
|
|
// Clear rootfs
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("Cleaning rootfs\n");
|
2019-03-31 06:32:33 -04:00
|
|
|
frm_rf(root, { "overlay", "proc", "sys" });
|
2019-03-15 14:46:30 -04:00
|
|
|
} else {
|
|
|
|
decompress_ramdisk();
|
|
|
|
|
|
|
|
// Revert original init binary
|
|
|
|
rename("/.backup/init", "/init");
|
|
|
|
rm_rf("/.backup");
|
|
|
|
|
|
|
|
// Do not go further if device is booting into recovery
|
2019-04-04 00:26:16 -04:00
|
|
|
if (access("/sbin/recovery", F_OK) == 0) {
|
|
|
|
LOGD("Ramdisk is recovery, abort\n");
|
2019-03-15 14:46:30 -04:00
|
|
|
re_exec_init();
|
2019-04-04 00:26:16 -04:00
|
|
|
}
|
2019-03-15 14:46:30 -04:00
|
|
|
}
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
struct device {
|
|
|
|
int major;
|
|
|
|
int minor;
|
|
|
|
char devname[32];
|
|
|
|
char partname[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void parse_device(device *dev, const char *uevent) {
|
2017-11-06 22:33:41 +08:00
|
|
|
dev->partname[0] = '\0';
|
2019-04-04 00:26:16 -04:00
|
|
|
FILE *fp = xfopen(uevent, "re");
|
2018-07-18 00:45:10 +08:00
|
|
|
char buf[64];
|
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
|
|
if (strncmp(buf, "MAJOR", 5) == 0) {
|
2019-04-04 00:26:16 -04:00
|
|
|
sscanf(buf, "MAJOR=%d", &dev->major);
|
2018-07-18 00:45:10 +08:00
|
|
|
} else if (strncmp(buf, "MINOR", 5) == 0) {
|
2019-04-04 00:26:16 -04:00
|
|
|
sscanf(buf, "MINOR=%d", &dev->minor);
|
2018-07-18 00:45:10 +08:00
|
|
|
} else if (strncmp(buf, "DEVNAME", 7) == 0) {
|
|
|
|
sscanf(buf, "DEVNAME=%s", dev->devname);
|
|
|
|
} else if (strncmp(buf, "PARTNAME", 8) == 0) {
|
|
|
|
sscanf(buf, "PARTNAME=%s", dev->partname);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 00:45:10 +08:00
|
|
|
fclose(fp);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
static vector<device> dev_list;
|
|
|
|
|
|
|
|
static void collect_devices() {
|
2018-07-18 00:45:10 +08:00
|
|
|
char path[128];
|
2017-11-06 05:41:03 +08:00
|
|
|
struct dirent *entry;
|
2019-04-04 00:26:16 -04:00
|
|
|
device dev;
|
|
|
|
DIR *dir = xopendir("/sys/dev/block");
|
2018-11-08 06:07:52 -05:00
|
|
|
if (dir == nullptr)
|
2019-04-04 00:26:16 -04:00
|
|
|
return;
|
2017-11-06 05:41:03 +08:00
|
|
|
while ((entry = readdir(dir))) {
|
2019-04-04 00:26:16 -04:00
|
|
|
if (entry->d_name == "."sv || entry->d_name == ".."sv)
|
2017-11-06 05:41:03 +08:00
|
|
|
continue;
|
2018-07-18 00:45:10 +08:00
|
|
|
sprintf(path, "/sys/dev/block/%s/uevent", entry->d_name);
|
2019-04-04 00:26:16 -04:00
|
|
|
parse_device(&dev, path);
|
|
|
|
dev_list.push_back(dev);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
closedir(dir);
|
2019-04-04 00:26:16 -04:00
|
|
|
}
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
static bool setup_block(const char *partname, char *block_dev) {
|
|
|
|
if (dev_list.empty())
|
|
|
|
collect_devices();
|
|
|
|
for (auto &dev : dev_list) {
|
|
|
|
if (strcasecmp(dev.partname, partname) == 0) {
|
|
|
|
sprintf(block_dev, "/dev/block/%s", dev.devname);
|
|
|
|
LOGD("Found %s: [%s] (%d, %d)\n", dev.partname, dev.devname, dev.major, dev.minor);
|
|
|
|
xmkdir("/dev", 0755);
|
|
|
|
xmkdir("/dev/block", 0755);
|
|
|
|
mknod(block_dev, S_IFBLK | 0600, makedev(dev.major, dev.minor));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
bool MagiskInit::read_dt_fstab(const char *name, char *partname, char *partfs) {
|
|
|
|
char path[128];
|
|
|
|
int fd;
|
|
|
|
sprintf(path, "%s/fstab/%s/dev", cmd.dt_dir, name);
|
|
|
|
if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) {
|
|
|
|
read(fd, path, sizeof(path));
|
|
|
|
close(fd);
|
|
|
|
// Some custom treble use different names, so use what we read
|
2019-04-06 17:19:47 -04:00
|
|
|
char *part = rtrim(strrchr(path, '/') + 1);
|
|
|
|
sprintf(partname, "%s%s", part, strend(part, cmd.slot) ? cmd.slot : "");
|
2019-04-04 00:26:16 -04:00
|
|
|
sprintf(path, "%s/fstab/%s/type", cmd.dt_dir, name);
|
|
|
|
if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) {
|
|
|
|
read(fd, partfs, 32);
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 06:17:37 -04:00
|
|
|
static inline bool is_lnk(const char *name) {
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(name, &st))
|
|
|
|
return false;
|
|
|
|
return S_ISLNK(st.st_mode);
|
|
|
|
}
|
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
#define link_root(name) \
|
|
|
|
if (is_lnk("/system_root" name)) \
|
|
|
|
cp_afc("/system_root" name, name)
|
|
|
|
|
|
|
|
#define mount_root(name) \
|
|
|
|
if (!is_lnk("/" #name) && read_dt_fstab(#name, partname, fstype)) { \
|
|
|
|
LOGD("Early mount " #name "\n"); \
|
|
|
|
setup_block(partname, block_dev); \
|
|
|
|
xmkdir("/" #name, 0755); \
|
|
|
|
xmount(block_dev, "/" #name, fstype, MS_RDONLY, nullptr); \
|
|
|
|
mnt_##name = true; \
|
2019-03-15 14:46:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MagiskInit::early_mount() {
|
|
|
|
char partname[32];
|
|
|
|
char fstype[32];
|
2019-04-04 00:26:16 -04:00
|
|
|
char block_dev[64];
|
2019-03-15 14:46:30 -04:00
|
|
|
|
|
|
|
if (cmd.system_as_root) {
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("Early mount system_root\n");
|
2019-03-15 14:46:30 -04:00
|
|
|
sprintf(partname, "system%s", cmd.slot);
|
2019-04-04 00:26:16 -04:00
|
|
|
setup_block(partname, block_dev);
|
2019-03-15 14:46:30 -04:00
|
|
|
xmkdir("/system_root", 0755);
|
2019-04-04 00:26:16 -04:00
|
|
|
xmount(block_dev, "/system_root", "ext4", MS_RDONLY, nullptr);
|
2019-03-15 14:46:30 -04:00
|
|
|
xmkdir("/system", 0755);
|
|
|
|
xmount("/system_root/system", "/system", nullptr, MS_BIND, nullptr);
|
|
|
|
|
2019-03-16 17:15:48 -04:00
|
|
|
// Android Q
|
|
|
|
if (is_lnk("/system_root/init"))
|
|
|
|
load_sepol = true;
|
|
|
|
|
2019-03-18 04:54:15 -04:00
|
|
|
// System-as-root with monolithic sepolicy
|
|
|
|
if (access("/system_root/sepolicy", F_OK) == 0)
|
|
|
|
cp_afc("/system_root/sepolicy", "/sepolicy");
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
// Copy if these partitions are symlinks
|
|
|
|
link_root("/vendor");
|
|
|
|
link_root("/product");
|
|
|
|
link_root("/odm");
|
|
|
|
} else {
|
|
|
|
mount_root(system);
|
|
|
|
}
|
|
|
|
|
|
|
|
mount_root(vendor);
|
|
|
|
mount_root(product);
|
|
|
|
mount_root(odm);
|
|
|
|
}
|
|
|
|
|
2019-04-24 00:13:48 -04:00
|
|
|
static void patch_socket_name(const char *path) {
|
|
|
|
uint8_t *buf;
|
|
|
|
char name[sizeof(MAIN_SOCKET)];
|
|
|
|
size_t size;
|
|
|
|
mmap_rw(path, buf, size);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
if (memcmp(buf + i, MAIN_SOCKET, sizeof(MAIN_SOCKET)) == 0) {
|
|
|
|
gen_rand_str(name, sizeof(name));
|
|
|
|
memcpy(buf + i, name, sizeof(name));
|
|
|
|
i += sizeof(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const char wrapper[] =
|
|
|
|
"#!/system/bin/sh\n"
|
|
|
|
"export LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH:/apex/com.android.runtime/" LIBNAME "\"\n"
|
|
|
|
"exec /sbin/magisk.bin \"$0\" \"$@\"\n"
|
|
|
|
;
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
void MagiskInit::setup_rootfs() {
|
|
|
|
bool patch_init = patch_sepolicy();
|
|
|
|
|
|
|
|
if (cmd.system_as_root) {
|
2019-03-31 06:32:33 -04:00
|
|
|
// Clone rootfs
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("Clone root dir from system to rootfs\n");
|
|
|
|
int system_root = xopen("/system_root", O_RDONLY | O_CLOEXEC);
|
2019-03-31 06:32:33 -04:00
|
|
|
clone_dir(system_root, root, false);
|
2019-03-15 14:46:30 -04:00
|
|
|
close(system_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (patch_init) {
|
|
|
|
constexpr char SYSTEM_INIT[] = "/system/bin/init";
|
|
|
|
// If init is symlink, copy it to rootfs so we can patch
|
2019-03-31 06:32:33 -04:00
|
|
|
if (is_lnk("/init"))
|
2019-03-15 14:46:30 -04:00
|
|
|
cp_afc(SYSTEM_INIT, "/init");
|
|
|
|
|
|
|
|
char *addr;
|
|
|
|
size_t size;
|
|
|
|
mmap_rw("/init", addr, size);
|
|
|
|
for (char *p = addr; p < addr + size; ++p) {
|
|
|
|
if (memcmp(p, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL)) == 0) {
|
|
|
|
// Force init to load /sepolicy
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("Remove from init: " SPLIT_PLAT_CIL "\n");
|
2019-03-15 14:46:30 -04:00
|
|
|
memset(p, 'x', sizeof(SPLIT_PLAT_CIL) - 1);
|
|
|
|
p += sizeof(SPLIT_PLAT_CIL) - 1;
|
|
|
|
} else if (memcmp(p, SYSTEM_INIT, sizeof(SYSTEM_INIT)) == 0) {
|
|
|
|
// Force execute /init instead of /system/bin/init
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("Patch init: [/system/bin/init] -> [/init]\n");
|
2019-03-15 14:46:30 -04:00
|
|
|
strcpy(p, "/init");
|
|
|
|
p += sizeof(SYSTEM_INIT) - 1;
|
|
|
|
}
|
2019-02-02 03:28:24 +02:00
|
|
|
}
|
2019-03-15 14:46:30 -04:00
|
|
|
munmap(addr, size);
|
2018-07-18 00:45:10 +08:00
|
|
|
}
|
2019-03-15 14:46:30 -04:00
|
|
|
|
|
|
|
// Handle ramdisk overlays
|
|
|
|
int fd = open("/overlay", O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd >= 0) {
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("Merge overlay folder\n");
|
2019-03-15 14:46:30 -04:00
|
|
|
mv_dir(fd, root);
|
|
|
|
close(fd);
|
|
|
|
rmdir("/overlay");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch init.rc
|
2019-03-30 04:13:45 -04:00
|
|
|
FILE *rc = xfopen("/init.p.rc", "we");
|
|
|
|
file_readline("/init.rc", [&](auto line) -> bool {
|
|
|
|
// Do not start vaultkeeper
|
2019-04-04 00:26:16 -04:00
|
|
|
if (str_contains(line, "start vaultkeeper")) {
|
|
|
|
LOGD("Remove vaultkeeper\n");
|
2019-03-30 04:13:45 -04:00
|
|
|
return true;
|
2019-04-04 00:26:16 -04:00
|
|
|
}
|
2019-03-30 04:13:45 -04:00
|
|
|
// Do not run flash_recovery
|
|
|
|
if (str_starts(line, "service flash_recovery")) {
|
2019-04-04 00:26:16 -04:00
|
|
|
LOGD("Remove flash_recovery\n");
|
2019-03-30 04:13:45 -04:00
|
|
|
fprintf(rc, "service flash_recovery /system/bin/xxxxx\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Else just write the line
|
|
|
|
fprintf(rc, "%s", line.data());
|
|
|
|
return true;
|
|
|
|
});
|
2019-04-05 07:00:30 -04:00
|
|
|
char pfd_svc[8], ls_svc[8], bc_svc[8];
|
|
|
|
// Make sure to be unique
|
|
|
|
pfd_svc[0] = 'a';
|
|
|
|
ls_svc[0] = '0';
|
|
|
|
bc_svc[0] = 'A';
|
|
|
|
gen_rand_str(pfd_svc + 1, sizeof(pfd_svc) - 1);
|
|
|
|
gen_rand_str(ls_svc + 1, sizeof(ls_svc) - 1);
|
|
|
|
gen_rand_str(bc_svc + 1, sizeof(bc_svc) - 1);
|
2019-04-24 00:13:48 -04:00
|
|
|
LOGD("Inject magisk services: [%s] [%s] [%s]\n", pfd_svc, ls_svc, bc_svc);
|
2019-04-05 07:00:30 -04:00
|
|
|
fprintf(rc, magiskrc, pfd_svc, pfd_svc, ls_svc, bc_svc, bc_svc);
|
2019-03-15 14:46:30 -04:00
|
|
|
fclose(rc);
|
2019-03-30 04:13:45 -04:00
|
|
|
clone_attr("/init.rc", "/init.p.rc");
|
|
|
|
rename("/init.p.rc", "/init.rc");
|
2019-03-15 14:46:30 -04:00
|
|
|
|
|
|
|
// Don't let init run in init yet
|
|
|
|
lsetfilecon("/init", "u:object_r:rootfs:s0");
|
|
|
|
|
|
|
|
// Create hardlink mirror of /sbin to /root
|
|
|
|
mkdir("/root", 0750);
|
|
|
|
clone_attr("/sbin", "/root");
|
|
|
|
int rootdir = xopen("/root", O_RDONLY | O_CLOEXEC);
|
|
|
|
int sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
|
|
|
|
link_dir(sbin, rootdir);
|
2019-04-24 00:13:48 -04:00
|
|
|
close(sbin);
|
|
|
|
|
|
|
|
LOGD("Mount /sbin tmpfs overlay\n");
|
|
|
|
xmount("tmpfs", "/sbin", "tmpfs", 0, "mode=755");
|
|
|
|
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
|
|
|
|
|
|
|
|
char path[64];
|
|
|
|
|
|
|
|
// Create symlinks pointing back to /root
|
|
|
|
DIR *dir = xfdopendir(rootdir);
|
|
|
|
struct dirent *entry;
|
|
|
|
while((entry = xreaddir(dir))) {
|
|
|
|
if (entry->d_name == "."sv || entry->d_name == ".."sv)
|
|
|
|
continue;
|
|
|
|
sprintf(path, "/root/%s", entry->d_name);
|
|
|
|
xsymlinkat(path, sbin, entry->d_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump binaries
|
|
|
|
mkdir(MAGISKTMP, 0755);
|
|
|
|
fd = xopen(MAGISKTMP "/config", O_WRONLY | O_CREAT, 0000);
|
|
|
|
write(fd, config.buf, config.sz);
|
|
|
|
close(fd);
|
|
|
|
fd = xopen("/sbin/magiskinit", O_WRONLY | O_CREAT, 0755);
|
|
|
|
write(fd, self.buf, self.sz);
|
|
|
|
close(fd);
|
|
|
|
if (access("/system/apex", F_OK) == 0) {
|
|
|
|
LOGD("APEX detected, use wrapper\n");
|
|
|
|
dump_magisk("/sbin/magisk.bin", 0755);
|
|
|
|
patch_socket_name("/sbin/magisk.bin");
|
|
|
|
fd = xopen("/sbin/magisk", O_WRONLY | O_CREAT, 0755);
|
|
|
|
write(fd, wrapper, sizeof(wrapper) - 1);
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
dump_magisk("/sbin/magisk", 0755);
|
|
|
|
patch_socket_name("/sbin/magisk");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create applet symlinks
|
|
|
|
for (int i = 0; applet_names[i]; ++i) {
|
|
|
|
sprintf(path, "/sbin/%s", applet_names[i]);
|
|
|
|
xsymlink("/sbin/magisk", path);
|
|
|
|
}
|
2019-05-01 13:55:59 -04:00
|
|
|
xsymlink("/sbin/magiskinit", "/sbin/magiskpolicy");
|
|
|
|
xsymlink("/sbin/magiskinit", "/sbin/supolicy");
|
2019-04-24 00:13:48 -04:00
|
|
|
|
|
|
|
close(rootdir);
|
|
|
|
close(sbin);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
bool MagiskInit::patch_sepolicy() {
|
2019-03-15 06:17:37 -04:00
|
|
|
bool patch_init = false;
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
if (access(SPLIT_PLAT_CIL, R_OK) == 0) {
|
|
|
|
LOGD("sepol: split policy\n");
|
|
|
|
patch_init = true;
|
|
|
|
} else if (access("/sepolicy", R_OK) == 0) {
|
|
|
|
LOGD("sepol: monolithic policy\n");
|
|
|
|
load_policydb("/sepolicy");
|
|
|
|
} else {
|
|
|
|
LOGD("sepol: no selinux\n");
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-01 03:33:25 +08:00
|
|
|
|
2019-03-15 06:17:37 -04:00
|
|
|
// Mount selinuxfs to communicate with kernel
|
|
|
|
xmount("selinuxfs", SELINUX_MNT, "selinuxfs", 0, nullptr);
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2019-03-15 06:17:37 -04:00
|
|
|
if (patch_init)
|
|
|
|
load_split_cil();
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2017-11-30 20:57:40 +08:00
|
|
|
sepol_magisk_rules();
|
2018-08-09 03:20:28 +08:00
|
|
|
sepol_allow(SEPOL_PROC_DOMAIN, ALL, ALL, ALL);
|
2019-03-31 06:32:33 -04:00
|
|
|
dump_policydb("/sepolicy");
|
2017-12-31 07:30:56 -06:00
|
|
|
|
2019-03-15 06:17:37 -04:00
|
|
|
// Load policy to kernel so we can label rootfs
|
2019-04-04 00:26:16 -04:00
|
|
|
if (load_sepol) {
|
|
|
|
LOGD("sepol: preload sepolicy\n");
|
2019-03-14 22:48:23 -04:00
|
|
|
dump_policydb(SELINUX_LOAD);
|
2019-04-04 00:26:16 -04:00
|
|
|
}
|
2019-03-14 22:27:29 -04:00
|
|
|
|
|
|
|
// Remove OnePlus stupid debug sepolicy and use our own
|
2018-02-02 04:17:13 +08:00
|
|
|
if (access("/sepolicy_debug", F_OK) == 0) {
|
|
|
|
unlink("/sepolicy_debug");
|
2019-03-31 06:32:33 -04:00
|
|
|
link("/sepolicy", "/sepolicy_debug");
|
2018-02-02 04:17:13 +08:00
|
|
|
}
|
|
|
|
|
2019-03-14 22:27:29 -04:00
|
|
|
// Enable selinux functions
|
|
|
|
selinux_builtin_impl();
|
2018-07-18 00:45:10 +08:00
|
|
|
|
2019-03-14 22:27:29 -04:00
|
|
|
return patch_init;
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
#define umount_root(name) \
|
|
|
|
if (mnt_##name) \
|
|
|
|
umount("/" #name);
|
2017-11-10 00:54:54 +08:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
void MagiskInit::cleanup() {
|
|
|
|
umount(SELINUX_MNT);
|
|
|
|
umount("/sys");
|
|
|
|
umount("/proc");
|
2019-03-16 17:15:48 -04:00
|
|
|
umount_root(system);
|
|
|
|
umount_root(vendor);
|
|
|
|
umount_root(product);
|
|
|
|
umount_root(odm);
|
2018-06-14 00:59:08 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
void MagiskInit::re_exec_init() {
|
2019-04-24 00:13:48 -04:00
|
|
|
LOGD("Re-exec /init\n");
|
2019-03-15 14:46:30 -04:00
|
|
|
cleanup();
|
2018-11-08 13:40:57 -05:00
|
|
|
execv("/init", argv);
|
2019-03-14 22:27:29 -04:00
|
|
|
exit(1);
|
2018-11-08 13:40:57 -05:00
|
|
|
}
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
void MagiskInit::start() {
|
2017-12-05 21:05:20 +08:00
|
|
|
// Prevent file descriptor confusion
|
|
|
|
mknod("/null", S_IFCHR | 0666, makedev(1, 3));
|
|
|
|
int null = open("/null", O_RDWR | O_CLOEXEC);
|
|
|
|
unlink("/null");
|
2018-07-13 05:41:29 +08:00
|
|
|
xdup3(null, STDIN_FILENO, O_CLOEXEC);
|
|
|
|
xdup3(null, STDOUT_FILENO, O_CLOEXEC);
|
|
|
|
xdup3(null, STDERR_FILENO, O_CLOEXEC);
|
2017-12-16 04:40:54 +08:00
|
|
|
if (null > STDERR_FILENO)
|
|
|
|
close(null);
|
2017-12-05 21:05:20 +08:00
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
setup_klog();
|
|
|
|
|
2019-03-30 00:49:48 -04:00
|
|
|
load_kernel_info();
|
|
|
|
|
2019-03-31 00:48:22 -04:00
|
|
|
full_read("/init", &self.buf, &self.sz);
|
2019-03-15 14:46:30 -04:00
|
|
|
full_read("/.backup/.magisk", &config.buf, &config.sz);
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
preset();
|
|
|
|
early_mount();
|
|
|
|
setup_rootfs();
|
2019-04-24 00:13:48 -04:00
|
|
|
re_exec_init();
|
2019-03-15 14:46:30 -04:00
|
|
|
}
|
2019-03-15 06:17:37 -04:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
void MagiskInit::test() {
|
|
|
|
cmdline_logging();
|
|
|
|
log_cb.ex = nop_ex;
|
2019-03-14 22:27:29 -04:00
|
|
|
|
2019-03-16 04:45:56 -04:00
|
|
|
chdir(dirname(argv[0]));
|
2019-03-15 14:46:30 -04:00
|
|
|
chroot(".");
|
|
|
|
chdir("/");
|
2019-03-14 22:27:29 -04:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
load_kernel_info();
|
|
|
|
preset();
|
|
|
|
early_mount();
|
|
|
|
setup_rootfs();
|
|
|
|
cleanup();
|
|
|
|
}
|
2019-03-14 22:27:29 -04:00
|
|
|
|
2019-04-04 00:26:16 -04:00
|
|
|
static int test_main(int, char *argv[]) {
|
2019-03-16 04:45:56 -04:00
|
|
|
MagiskInit init(argv);
|
|
|
|
init.test();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
umask(0);
|
2019-03-14 22:27:29 -04:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
for (int i = 0; init_applet[i]; ++i) {
|
|
|
|
if (strcmp(basename(argv[0]), init_applet[i]) == 0)
|
|
|
|
return (*init_applet_main[i])(argc, argv);
|
2019-03-14 22:27:29 -04:00
|
|
|
}
|
2018-07-18 00:45:10 +08:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
if (argc > 1 && strcmp(argv[1], "-x") == 0) {
|
|
|
|
if (strcmp(argv[2], "magisk") == 0)
|
|
|
|
return dump_magisk(argv[3], 0755);
|
|
|
|
else if (strcmp(argv[2], "manager") == 0)
|
|
|
|
return dump_manager(argv[3], 0644);
|
2018-08-07 02:20:40 +08:00
|
|
|
}
|
2019-03-14 22:27:29 -04:00
|
|
|
|
2019-04-01 17:14:18 -04:00
|
|
|
if (getpid() != 1)
|
|
|
|
return 1;
|
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
MagiskInit init(argv);
|
2019-03-14 22:27:29 -04:00
|
|
|
|
2019-03-15 14:46:30 -04:00
|
|
|
// Run the main routine
|
|
|
|
init.start();
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|