2018-11-08 09:20:16 +00:00
|
|
|
/* init.cpp - Pre-init Magisk support
|
2017-11-05 21:41:03 +00:00
|
|
|
*
|
|
|
|
* This code has to be compiled statically to work properly.
|
|
|
|
*
|
2017-11-09 16:54:54 +00:00
|
|
|
* To unify Magisk support for both legacy "normal" devices and new skip_initramfs devices,
|
|
|
|
* magisk binary compilation is split into two parts - first part only compiles "magisk".
|
|
|
|
* The python build script will load the magisk main binary and compress with lzma2, dumping
|
|
|
|
* the results into "dump.h". The "magisk" binary is embedded into this binary, and will
|
|
|
|
* get extracted to the overlay folder along with init.magisk.rc.
|
|
|
|
*
|
|
|
|
* This tool does all pre-init operations to setup a Magisk environment, which pathces rootfs
|
|
|
|
* on the fly, providing fundamental support such as init, init.rc, and sepolicy patching.
|
|
|
|
*
|
|
|
|
* Magiskinit is also responsible for constructing a proper rootfs on skip_initramfs devices.
|
2017-11-05 21:41:03 +00:00
|
|
|
* On skip_initramfs devices, it will parse kernel cmdline, mount sysfs, parse through
|
|
|
|
* uevent files to make the system (or vendor if available) block device node, then copy
|
2017-11-09 16:54:54 +00:00
|
|
|
* rootfs files from system.
|
|
|
|
*
|
2017-11-05 21:41:03 +00:00
|
|
|
* This tool will be replaced with the real init to continue the boot process, but hardlinks are
|
|
|
|
* preserved as it also provides CLI for sepolicy patching (magiskpolicy)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <sys/sysmacros.h>
|
2019-02-09 10:23:56 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <string_view>
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2018-08-09 21:03:54 +00:00
|
|
|
#include <xz.h>
|
2019-02-10 08:57:51 +00:00
|
|
|
#include <magisk.h>
|
|
|
|
#include <magiskpolicy.h>
|
|
|
|
#include <selinux.h>
|
2019-02-25 01:39:01 +00:00
|
|
|
#include <cpio.h>
|
2019-02-10 08:57:51 +00:00
|
|
|
#include <utils.h>
|
|
|
|
#include <flags.h>
|
2018-08-09 21:03:54 +00:00
|
|
|
|
2018-08-09 10:13:07 +00:00
|
|
|
#include "binaries.h"
|
2019-02-24 13:13:27 +00:00
|
|
|
#ifdef USE_64BIT
|
|
|
|
#include "binaries_arch64.h"
|
|
|
|
#else
|
2018-08-09 10:13:07 +00:00
|
|
|
#include "binaries_arch.h"
|
2019-02-24 13:13:27 +00:00
|
|
|
#endif
|
2017-11-27 19:42:48 +00:00
|
|
|
#include "magiskrc.h"
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2018-07-17 16:45:10 +00:00
|
|
|
#define DEFAULT_DT_DIR "/proc/device-tree/firmware/android"
|
|
|
|
|
2018-11-08 11:07:52 +00:00
|
|
|
int (*init_applet_main[]) (int, char *[]) = { magiskpolicy_main, magiskpolicy_main, nullptr };
|
2017-11-27 20:43:46 +00:00
|
|
|
|
2018-11-08 18:40:57 +00:00
|
|
|
static bool mnt_system = false;
|
|
|
|
static bool mnt_vendor = false;
|
|
|
|
|
2019-02-09 06:51:46 +00:00
|
|
|
static void *self, *config;
|
|
|
|
static size_t self_sz, config_sz;
|
|
|
|
|
2017-11-05 21:41:03 +00:00
|
|
|
struct cmdline {
|
2018-12-24 13:36:37 +00:00
|
|
|
bool early_boot;
|
2017-11-05 21:41:03 +00:00
|
|
|
char slot[3];
|
2018-07-17 16:45:10 +00:00
|
|
|
char dt_dir[128];
|
|
|
|
};
|
|
|
|
|
2017-11-05 21:41:03 +00:00
|
|
|
struct device {
|
|
|
|
dev_t major;
|
|
|
|
dev_t minor;
|
|
|
|
char devname[32];
|
|
|
|
char partname[32];
|
|
|
|
char path[64];
|
|
|
|
};
|
|
|
|
|
2019-02-09 10:23:56 +00:00
|
|
|
static void parse_cmdline(const std::function<void (std::string_view, const char *)> &fn) {
|
|
|
|
char cmdline[4096];
|
|
|
|
int fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
|
|
|
|
cmdline[read(fd, cmdline, sizeof(cmdline))] = '\0';
|
|
|
|
close(fd);
|
2017-11-23 10:38:12 +00:00
|
|
|
|
2019-02-09 10:23:56 +00: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, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_cmdline(struct cmdline *cmd) {
|
2017-11-23 10:38:12 +00:00
|
|
|
char cmdline[4096];
|
2017-11-05 21:41:03 +00:00
|
|
|
int fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
|
2017-11-23 10:38:12 +00:00
|
|
|
cmdline[read(fd, cmdline, sizeof(cmdline))] = '\0';
|
2017-11-05 21:41:03 +00:00
|
|
|
close(fd);
|
2018-12-24 13:36:37 +00:00
|
|
|
|
2019-02-09 10:23:56 +00:00
|
|
|
bool skip_initramfs = false, kirin = false, enter_recovery = false;
|
2018-12-24 13:36:37 +00:00
|
|
|
|
2019-02-09 10:23:56 +00:00
|
|
|
parse_cmdline([&](auto key, auto value) -> void {
|
|
|
|
if (key == "androidboot.slot_suffix") {
|
|
|
|
strcpy(cmd->slot, value);
|
|
|
|
} else if (key == "androidboot.slot") {
|
2017-11-06 14:33:41 +00:00
|
|
|
cmd->slot[0] = '_';
|
2019-02-09 10:23:56 +00:00
|
|
|
strcpy(cmd->slot + 1, value);
|
|
|
|
} else if (key == "skip_initramfs") {
|
2018-12-24 13:36:37 +00:00
|
|
|
skip_initramfs = true;
|
2019-02-09 10:23:56 +00:00
|
|
|
} else if (key == "androidboot.android_dt_dir") {
|
|
|
|
strcpy(cmd->dt_dir, value);
|
2019-02-11 07:26:15 +00:00
|
|
|
} else if (key == "enter_recovery") {
|
2019-02-09 10:23:56 +00:00
|
|
|
enter_recovery = value[0] == '1';
|
|
|
|
} else if (key == "androidboot.hardware") {
|
|
|
|
kirin = strstr(value, "kirin") || strstr(value, "hi3660");
|
2017-11-05 21:41:03 +00:00
|
|
|
}
|
2019-02-09 10:23:56 +00:00
|
|
|
});
|
2018-12-24 17:08:46 +00:00
|
|
|
|
|
|
|
if (kirin && enter_recovery) {
|
|
|
|
// Inform that we are actually booting as recovery
|
2019-02-09 10:23:56 +00:00
|
|
|
FILE *f = fopen("/.backup/.magisk", "ae");
|
2018-12-24 17:08:46 +00:00
|
|
|
fprintf(f, "RECOVERYMODE=true\n");
|
|
|
|
fclose(f);
|
|
|
|
cmd->early_boot = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd->early_boot |= skip_initramfs;
|
2018-02-13 16:57:52 +00:00
|
|
|
|
2018-07-17 16:45:10 +00:00
|
|
|
if (cmd->dt_dir[0] == '\0')
|
|
|
|
strcpy(cmd->dt_dir, DEFAULT_DT_DIR);
|
|
|
|
|
2018-12-24 13:36:37 +00:00
|
|
|
LOGD("cmdline: early_boot[%d] slot[%s] dt_dir[%s]\n", cmd->early_boot, cmd->slot, cmd->dt_dir);
|
2017-11-05 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 16:45:10 +00:00
|
|
|
static void parse_device(struct device *dev, const char *uevent) {
|
2017-11-06 14:33:41 +00:00
|
|
|
dev->partname[0] = '\0';
|
2018-07-17 16:45:10 +00:00
|
|
|
FILE *fp = xfopen(uevent, "r");
|
|
|
|
char buf[64];
|
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
|
|
if (strncmp(buf, "MAJOR", 5) == 0) {
|
|
|
|
sscanf(buf, "MAJOR=%ld", (long*) &dev->major);
|
|
|
|
} else if (strncmp(buf, "MINOR", 5) == 0) {
|
|
|
|
sscanf(buf, "MINOR=%ld", (long*) &dev->minor);
|
|
|
|
} 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-05 21:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-17 16:45:10 +00:00
|
|
|
fclose(fp);
|
2018-09-27 07:11:10 +00:00
|
|
|
LOGD("%s [%s] (%u, %u)\n", dev->devname, dev->partname, (unsigned) dev->major, (unsigned) dev->minor);
|
2017-11-05 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 18:40:57 +00:00
|
|
|
static bool setup_block(struct device *dev, const char *partname) {
|
2018-07-17 16:45:10 +00:00
|
|
|
char path[128];
|
2017-11-05 21:41:03 +00:00
|
|
|
struct dirent *entry;
|
|
|
|
DIR *dir = opendir("/sys/dev/block");
|
2018-11-08 11:07:52 +00:00
|
|
|
if (dir == nullptr)
|
2019-02-09 10:23:56 +00:00
|
|
|
return false;
|
2018-11-08 18:40:57 +00:00
|
|
|
bool found = false;
|
2017-11-05 21:41:03 +00:00
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
2018-07-17 16:45:10 +00:00
|
|
|
sprintf(path, "/sys/dev/block/%s/uevent", entry->d_name);
|
|
|
|
parse_device(dev, path);
|
2018-05-05 07:50:06 +00:00
|
|
|
if (strcasecmp(dev->partname, partname) == 0) {
|
2018-07-17 16:45:10 +00:00
|
|
|
sprintf(dev->path, "/dev/block/%s", dev->devname);
|
2018-11-08 18:40:57 +00:00
|
|
|
found = true;
|
2017-11-05 21:41:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
if (!found)
|
2018-11-08 18:40:57 +00:00
|
|
|
return false;
|
2017-11-05 21:41:03 +00:00
|
|
|
|
|
|
|
mkdir("/dev", 0755);
|
|
|
|
mkdir("/dev/block", 0755);
|
|
|
|
mknod(dev->path, S_IFBLK | 0600, makedev(dev->major, dev->minor));
|
2018-11-08 18:40:57 +00:00
|
|
|
return true;
|
2017-11-05 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:28:24 +00:00
|
|
|
static bool read_fstab_dt(const struct cmdline *cmd, const char *mnt_point, char *partname, char *partfs) {
|
2018-07-18 19:12:28 +00:00
|
|
|
char buf[128];
|
2018-07-19 09:43:37 +00:00
|
|
|
struct stat st;
|
|
|
|
sprintf(buf, "/%s", mnt_point);
|
|
|
|
lstat(buf, &st);
|
|
|
|
// Don't early mount if the mount point is symlink
|
|
|
|
if (S_ISLNK(st.st_mode))
|
2018-11-08 18:40:57 +00:00
|
|
|
return false;
|
2019-02-09 10:23:56 +00:00
|
|
|
int fd;
|
2018-07-18 19:12:28 +00:00
|
|
|
sprintf(buf, "%s/fstab/%s/dev", cmd->dt_dir, mnt_point);
|
2019-02-09 10:23:56 +00:00
|
|
|
if ((fd = xopen(buf, O_RDONLY | O_CLOEXEC)) >= 0) {
|
2018-07-17 16:45:10 +00:00
|
|
|
read(fd, buf, sizeof(buf));
|
|
|
|
close(fd);
|
2018-07-18 19:12:28 +00:00
|
|
|
char *name = strrchr(buf, '/') + 1;
|
|
|
|
sprintf(partname, "%s%s", name, strend(name, cmd->slot) ? cmd->slot : "");
|
2019-02-02 01:28:24 +00:00
|
|
|
sprintf(buf, "%s/fstab/%s/type", cmd->dt_dir, mnt_point);
|
2019-02-09 10:23:56 +00:00
|
|
|
if ((fd = xopen(buf, O_RDONLY | O_CLOEXEC)) >= 0) {
|
2019-02-02 01:28:24 +00:00
|
|
|
lstat(buf, &st);
|
|
|
|
read(fd, partfs, st.st_size);
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-17 16:45:10 +00:00
|
|
|
}
|
2018-11-08 18:40:57 +00:00
|
|
|
return false;
|
2017-11-05 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 18:40:57 +00:00
|
|
|
static bool verify_precompiled() {
|
2017-11-05 21:41:03 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
int fd;
|
2018-07-17 16:45:10 +00:00
|
|
|
char sys_sha[64], ven_sha[64];
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2017-11-30 19:33:25 +00:00
|
|
|
// init the strings with different value
|
|
|
|
sys_sha[0] = 0;
|
|
|
|
ven_sha[0] = 1;
|
|
|
|
|
|
|
|
dir = opendir(NONPLAT_POLICY_DIR);
|
2017-11-05 21:41:03 +00:00
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (strend(entry->d_name, ".sha256") == 0) {
|
|
|
|
fd = openat(dirfd(dir), entry->d_name, O_RDONLY | O_CLOEXEC);
|
2018-07-17 16:45:10 +00:00
|
|
|
read(fd, ven_sha, sizeof(ven_sha));
|
2017-11-05 21:41:03 +00:00
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2017-11-30 19:33:25 +00:00
|
|
|
dir = opendir(PLAT_POLICY_DIR);
|
2017-11-05 21:41:03 +00:00
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (strend(entry->d_name, ".sha256") == 0) {
|
|
|
|
fd = openat(dirfd(dir), entry->d_name, O_RDONLY | O_CLOEXEC);
|
2018-07-17 16:45:10 +00:00
|
|
|
read(fd, sys_sha, sizeof(sys_sha));
|
2017-11-05 21:41:03 +00:00
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2018-09-27 07:11:10 +00:00
|
|
|
LOGD("sys_sha[%.*s]\nven_sha[%.*s]\n", sizeof(sys_sha), sys_sha, sizeof(ven_sha), ven_sha);
|
2018-07-17 16:45:10 +00:00
|
|
|
return memcmp(sys_sha, ven_sha, sizeof(sys_sha)) == 0;
|
2017-11-05 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 18:40:57 +00:00
|
|
|
static bool patch_sepolicy() {
|
|
|
|
bool init_patch = false;
|
2018-07-17 16:45:10 +00:00
|
|
|
if (access(SPLIT_PRECOMPILE, R_OK) == 0 && verify_precompiled()) {
|
2018-11-08 18:40:57 +00:00
|
|
|
init_patch = true;
|
2017-11-30 19:33:25 +00:00
|
|
|
load_policydb(SPLIT_PRECOMPILE);
|
2018-07-17 16:45:10 +00:00
|
|
|
} else if (access(SPLIT_PLAT_CIL, R_OK) == 0) {
|
2018-11-08 18:40:57 +00:00
|
|
|
init_patch = true;
|
2018-07-20 21:12:22 +00:00
|
|
|
compile_split_cil();
|
2018-07-17 16:45:10 +00:00
|
|
|
} else if (access("/sepolicy", R_OK) == 0) {
|
|
|
|
load_policydb("/sepolicy");
|
|
|
|
} else {
|
2018-11-08 18:40:57 +00:00
|
|
|
return false;
|
2018-07-17 16:45:10 +00:00
|
|
|
}
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2017-11-30 12:57:40 +00:00
|
|
|
sepol_magisk_rules();
|
2018-08-08 19:20:28 +00:00
|
|
|
sepol_allow(SEPOL_PROC_DOMAIN, ALL, ALL, ALL);
|
2017-11-05 21:41:03 +00:00
|
|
|
dump_policydb("/sepolicy");
|
2017-12-31 13:30:56 +00:00
|
|
|
|
2018-02-01 20:17:13 +00:00
|
|
|
// Remove the stupid debug sepolicy and use our own
|
|
|
|
if (access("/sepolicy_debug", F_OK) == 0) {
|
|
|
|
unlink("/sepolicy_debug");
|
|
|
|
link("/sepolicy", "/sepolicy_debug");
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:45:10 +00:00
|
|
|
if (init_patch) {
|
|
|
|
// Force init to load /sepolicy
|
2018-11-08 09:20:16 +00:00
|
|
|
uint8_t *addr;
|
2018-07-17 16:45:10 +00:00
|
|
|
size_t size;
|
2019-02-25 04:09:34 +00:00
|
|
|
mmap_rw("/init", addr, size);
|
2018-07-17 16:45:10 +00:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
if (memcmp(addr + i, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL) - 1) == 0) {
|
|
|
|
memcpy(addr + i + sizeof(SPLIT_PLAT_CIL) - 4, "xxx", 3);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(addr, size);
|
|
|
|
}
|
|
|
|
|
2018-11-08 18:40:57 +00:00
|
|
|
return true;
|
2017-11-05 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 18:40:57 +00:00
|
|
|
static bool unxz(int fd, const uint8_t *buf, size_t size) {
|
2018-08-09 21:03:54 +00:00
|
|
|
uint8_t out[8192];
|
2018-08-10 07:04:32 +00:00
|
|
|
xz_crc32_init();
|
2018-08-09 21:03:54 +00:00
|
|
|
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)
|
2018-11-08 18:40:57 +00:00
|
|
|
return false;
|
2018-08-09 21:03:54 +00:00
|
|
|
write(fd, out, b.out_pos);
|
|
|
|
b.out_pos = 0;
|
|
|
|
} while (b.in_pos != size);
|
2018-11-08 18:40:57 +00:00
|
|
|
return true;
|
2018-08-09 21:03:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 01:39:01 +00:00
|
|
|
static void decompress_ramdisk() {
|
|
|
|
constexpr char tmp[] = "tmp.cpio";
|
|
|
|
constexpr char ramdisk_xz[] = "ramdisk.cpio.xz";
|
|
|
|
if (access(ramdisk_xz, F_OK))
|
|
|
|
return;
|
|
|
|
uint8_t *buf;
|
|
|
|
size_t sz;
|
2019-02-25 04:09:34 +00:00
|
|
|
mmap_ro(ramdisk_xz, buf, sz);
|
2019-02-25 01:39:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:54:54 +00:00
|
|
|
static int dump_magisk(const char *path, mode_t mode) {
|
2019-02-09 10:23:56 +00:00
|
|
|
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
2018-08-10 07:04:32 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return 1;
|
2018-11-08 18:40:57 +00:00
|
|
|
if (!unxz(fd, magisk_xz, sizeof(magisk_xz)))
|
2018-08-10 07:04:32 +00:00
|
|
|
return 1;
|
2017-11-09 16:54:54 +00:00
|
|
|
close(fd);
|
2018-08-09 10:13:07 +00:00
|
|
|
return 0;
|
2017-11-09 16:54:54 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 16:59:08 +00:00
|
|
|
static int dump_manager(const char *path, mode_t mode) {
|
2019-02-09 10:23:56 +00:00
|
|
|
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
2018-08-10 07:04:32 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return 1;
|
2018-11-08 18:40:57 +00:00
|
|
|
if (!unxz(fd, manager_xz, sizeof(manager_xz)))
|
2018-08-10 07:04:32 +00:00
|
|
|
return 1;
|
2018-06-13 16:59:08 +00:00
|
|
|
close(fd);
|
2018-08-09 10:13:07 +00:00
|
|
|
return 0;
|
2018-06-13 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 19:40:09 +00:00
|
|
|
static void patch_socket_name(const char *path) {
|
2018-11-08 09:20:16 +00:00
|
|
|
uint8_t *buf;
|
2018-07-02 14:11:28 +00:00
|
|
|
char name[sizeof(MAIN_SOCKET)];
|
2018-02-10 19:40:09 +00:00
|
|
|
size_t size;
|
2019-02-25 04:09:34 +00:00
|
|
|
mmap_rw(path, buf, size);
|
2018-07-02 14:11:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (memcmp(buf + i, LOG_SOCKET, sizeof(LOG_SOCKET)) == 0) {
|
|
|
|
gen_rand_str(name, sizeof(name));
|
|
|
|
memcpy(buf + i, name, sizeof(name));
|
|
|
|
i += sizeof(name);
|
2018-02-10 19:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(buf, size);
|
|
|
|
}
|
|
|
|
|
2019-02-09 07:48:05 +00:00
|
|
|
static void setup_init_rc() {
|
|
|
|
FILE *rc = xfopen("/init.rc", "ae");
|
|
|
|
char pfd_svc[8], ls_svc[8];
|
|
|
|
gen_rand_str(pfd_svc, sizeof(pfd_svc));
|
|
|
|
do {
|
|
|
|
gen_rand_str(ls_svc, sizeof(ls_svc));
|
|
|
|
} while (strcmp(pfd_svc, ls_svc) == 0);
|
|
|
|
fprintf(rc, magiskrc, pfd_svc, pfd_svc, ls_svc);
|
|
|
|
fclose(rc);
|
2019-02-09 06:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_overlay() {
|
|
|
|
char buf[128];
|
|
|
|
int fd;
|
|
|
|
|
2019-02-09 10:23:56 +00:00
|
|
|
// Wait for early-init start
|
2019-02-09 06:51:46 +00:00
|
|
|
while (access(EARLYINIT, F_OK) != 0)
|
|
|
|
usleep(10);
|
|
|
|
selinux_builtin_impl();
|
|
|
|
setcon("u:r:" SEPOL_PROC_DOMAIN ":s0");
|
|
|
|
unlink(EARLYINIT);
|
|
|
|
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
xdup2(fd, STDIN_FILENO);
|
|
|
|
xdup2(fd, STDOUT_FILENO);
|
|
|
|
xdup2(fd, STDERR_FILENO);
|
|
|
|
|
|
|
|
// Mount the /sbin tmpfs overlay
|
|
|
|
xmount("tmpfs", "/sbin", "tmpfs", 0, nullptr);
|
|
|
|
chmod("/sbin", 0755);
|
|
|
|
setfilecon("/sbin", "u:object_r:rootfs:s0");
|
|
|
|
|
|
|
|
// Dump binaries
|
|
|
|
mkdir(MAGISKTMP, 0755);
|
|
|
|
fd = open(MAGISKTMP "/config", O_WRONLY | O_CREAT, 0000);
|
|
|
|
write(fd, config, config_sz);
|
|
|
|
close(fd);
|
|
|
|
fd = open("/sbin/magiskinit", O_WRONLY | O_CREAT, 0755);
|
|
|
|
write(fd, self, self_sz);
|
|
|
|
close(fd);
|
2019-02-24 13:13:27 +00:00
|
|
|
dump_magisk("/sbin/magisk", 0755);
|
|
|
|
patch_socket_name("/sbin/magisk");
|
2019-02-09 06:51:46 +00:00
|
|
|
setfilecon("/sbin/magisk", "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
|
|
|
|
setfilecon("/sbin/magiskinit", "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
|
|
|
|
|
|
|
|
// Create applet symlinks
|
|
|
|
for (int i = 0; applet_names[i]; ++i) {
|
|
|
|
sprintf(buf, "/sbin/%s", applet_names[i]);
|
|
|
|
xsymlink("/sbin/magisk", buf);
|
|
|
|
}
|
|
|
|
for (int i = 0; init_applet[i]; ++i) {
|
|
|
|
sprintf(buf, "/sbin/%s", init_applet[i]);
|
|
|
|
xsymlink("/sbin/magiskinit", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create symlinks pointing back to /root
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
dir = xopendir("/root");
|
|
|
|
fd = xopen("/sbin", O_RDONLY);
|
|
|
|
while((entry = xreaddir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
|
|
|
|
snprintf(buf, PATH_MAX, "/root/%s", entry->d_name);
|
|
|
|
xsymlinkat(buf, fd, entry->d_name);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
close(xopen(EARLYINITDONE, O_RDONLY | O_CREAT, 0));
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2018-11-08 18:40:57 +00:00
|
|
|
static void exec_init(char *argv[]) {
|
|
|
|
// Clean up
|
|
|
|
umount("/proc");
|
|
|
|
umount("/sys");
|
|
|
|
if (mnt_system)
|
|
|
|
umount("/system");
|
|
|
|
if (mnt_vendor)
|
|
|
|
umount("/vendor");
|
|
|
|
|
|
|
|
execv("/init", argv);
|
|
|
|
}
|
|
|
|
|
2017-11-05 21:41:03 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2017-11-23 10:38:12 +00:00
|
|
|
umask(0);
|
|
|
|
|
2017-11-27 20:43:46 +00: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);
|
|
|
|
}
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2017-11-23 10:38:12 +00:00
|
|
|
if (argc > 1 && strcmp(argv[1], "-x") == 0) {
|
2017-11-09 16:54:54 +00:00
|
|
|
if (strcmp(argv[2], "magisk") == 0)
|
|
|
|
return dump_magisk(argv[3], 0755);
|
2018-06-13 16:59:08 +00:00
|
|
|
else if (strcmp(argv[2], "manager") == 0)
|
|
|
|
return dump_manager(argv[3], 0644);
|
2017-11-09 16:54:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 13:05:20 +00: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-12 21:41:29 +00:00
|
|
|
xdup3(null, STDIN_FILENO, O_CLOEXEC);
|
|
|
|
xdup3(null, STDOUT_FILENO, O_CLOEXEC);
|
|
|
|
xdup3(null, STDERR_FILENO, O_CLOEXEC);
|
2017-12-15 20:40:54 +00:00
|
|
|
if (null > STDERR_FILENO)
|
|
|
|
close(null);
|
2017-12-05 13:05:20 +00:00
|
|
|
|
2018-07-17 16:45:10 +00:00
|
|
|
// Communicate with kernel using procfs and sysfs
|
|
|
|
mkdir("/proc", 0755);
|
2018-11-08 11:07:52 +00:00
|
|
|
xmount("proc", "/proc", "proc", 0, nullptr);
|
2018-07-17 16:45:10 +00:00
|
|
|
mkdir("/sys", 0755);
|
2018-11-08 11:07:52 +00:00
|
|
|
xmount("sysfs", "/sys", "sysfs", 0, nullptr);
|
2018-07-17 16:45:10 +00:00
|
|
|
|
2019-02-09 10:23:56 +00:00
|
|
|
struct cmdline cmd{};
|
2017-11-05 21:41:03 +00:00
|
|
|
parse_cmdline(&cmd);
|
|
|
|
|
2019-02-09 10:23:56 +00:00
|
|
|
// Backup stuffs
|
|
|
|
full_read("/init", &self, &self_sz);
|
|
|
|
full_read("/.backup/.magisk", &config, &config_sz);
|
|
|
|
|
2018-07-17 16:45:10 +00:00
|
|
|
/* ***********
|
|
|
|
* Initialize
|
|
|
|
* ***********/
|
|
|
|
|
2019-02-09 06:51:46 +00:00
|
|
|
int root, sbin;
|
2018-11-08 18:40:57 +00:00
|
|
|
root = open("/", O_RDONLY | O_CLOEXEC);
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2018-12-24 13:36:37 +00:00
|
|
|
if (cmd.early_boot) {
|
2017-11-05 21:41:03 +00:00
|
|
|
// Clear rootfs
|
2019-02-09 06:51:46 +00:00
|
|
|
const char *excl[] = { "overlay", "proc", "sys", nullptr };
|
2018-11-08 11:07:52 +00:00
|
|
|
excl_list = excl;
|
2018-02-13 16:57:52 +00:00
|
|
|
frm_rf(root);
|
2018-11-08 11:07:52 +00:00
|
|
|
excl_list = nullptr;
|
2018-02-13 16:57:52 +00:00
|
|
|
} else {
|
2019-02-25 01:39:01 +00:00
|
|
|
decompress_ramdisk();
|
|
|
|
|
2018-02-13 16:57:52 +00:00
|
|
|
// Revert original init binary
|
2019-02-10 18:51:41 +00:00
|
|
|
rename("/.backup/init", "/init");
|
2019-02-09 06:51:46 +00:00
|
|
|
rm_rf("/.backup");
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2019-02-09 10:23:56 +00:00
|
|
|
// Do not go further if device is booting into recovery
|
|
|
|
if (access("/sbin/recovery", F_OK) == 0)
|
|
|
|
exec_init(argv);
|
|
|
|
}
|
2018-08-06 18:20:40 +00:00
|
|
|
|
2018-02-13 16:57:52 +00:00
|
|
|
/* ************
|
|
|
|
* Early Mount
|
|
|
|
* ************/
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2018-07-17 16:45:10 +00:00
|
|
|
struct device dev;
|
2018-07-18 19:12:28 +00:00
|
|
|
char partname[32];
|
2019-02-02 01:28:24 +00:00
|
|
|
char partfs[32];
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2018-12-24 13:36:37 +00:00
|
|
|
if (cmd.early_boot) {
|
2018-07-18 19:12:28 +00:00
|
|
|
sprintf(partname, "system%s", cmd.slot);
|
|
|
|
setup_block(&dev, partname);
|
2018-07-17 16:45:10 +00:00
|
|
|
xmkdir("/system_root", 0755);
|
2018-11-08 11:07:52 +00:00
|
|
|
xmount(dev.path, "/system_root", "ext4", MS_RDONLY, nullptr);
|
2018-07-17 16:45:10 +00:00
|
|
|
int system_root = open("/system_root", O_RDONLY | O_CLOEXEC);
|
|
|
|
|
|
|
|
// Clone rootfs except /system
|
2018-11-08 18:40:57 +00:00
|
|
|
const char *excl[] = { "system", nullptr };
|
2018-11-08 11:07:52 +00:00
|
|
|
excl_list = excl;
|
2018-07-17 16:45:10 +00:00
|
|
|
clone_dir(system_root, root);
|
|
|
|
close(system_root);
|
2018-11-08 18:40:57 +00:00
|
|
|
excl_list = nullptr;
|
2018-07-17 16:45:10 +00:00
|
|
|
|
|
|
|
xmkdir("/system", 0755);
|
2018-11-08 11:07:52 +00:00
|
|
|
xmount("/system_root/system", "/system", nullptr, MS_BIND, nullptr);
|
2019-02-02 01:28:24 +00:00
|
|
|
} else if (read_fstab_dt(&cmd, "system", partname, partfs)) {
|
2018-07-18 19:12:28 +00:00
|
|
|
setup_block(&dev, partname);
|
2019-02-02 01:28:24 +00:00
|
|
|
xmount(dev.path, "/system", partfs, MS_RDONLY, nullptr);
|
2018-11-08 09:20:16 +00:00
|
|
|
mnt_system = true;
|
2018-07-17 16:45:10 +00:00
|
|
|
}
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2019-02-02 01:28:24 +00:00
|
|
|
if (read_fstab_dt(&cmd, "vendor", partname, partfs)) {
|
2018-07-18 19:12:28 +00:00
|
|
|
setup_block(&dev, partname);
|
2019-02-02 01:28:24 +00:00
|
|
|
xmount(dev.path, "/vendor", partfs, MS_RDONLY, nullptr);
|
2018-11-08 09:20:16 +00:00
|
|
|
mnt_vendor = true;
|
2018-07-16 22:18:36 +00:00
|
|
|
}
|
2017-12-06 19:21:13 +00:00
|
|
|
|
2018-07-17 16:45:10 +00:00
|
|
|
/* ****************
|
|
|
|
* Ramdisk Patches
|
|
|
|
* ****************/
|
|
|
|
|
2018-08-06 18:20:40 +00:00
|
|
|
// Handle ramdisk overlays
|
2018-11-08 18:40:57 +00:00
|
|
|
int fd = open("/overlay", O_RDONLY | O_CLOEXEC);
|
2018-08-06 18:20:40 +00:00
|
|
|
if (fd >= 0) {
|
|
|
|
mv_dir(fd, root);
|
|
|
|
close(fd);
|
|
|
|
rmdir("/overlay");
|
|
|
|
}
|
2019-02-09 06:51:46 +00:00
|
|
|
close(root);
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2019-02-09 06:51:46 +00:00
|
|
|
// Create hardlink mirror of /sbin to /root
|
|
|
|
mkdir("/root", 0750);
|
|
|
|
clone_attr("/sbin", "/root");
|
|
|
|
root = xopen("/root", O_RDONLY | O_CLOEXEC);
|
|
|
|
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
|
|
|
|
link_dir(sbin, root);
|
2018-07-16 22:18:36 +00:00
|
|
|
|
2019-02-09 07:48:05 +00:00
|
|
|
setup_init_rc();
|
2018-08-06 18:20:40 +00:00
|
|
|
patch_sepolicy();
|
2018-04-21 12:16:59 +00:00
|
|
|
|
2019-02-09 06:51:46 +00:00
|
|
|
// Close all file descriptors
|
|
|
|
for (int i = 0; i < 30; ++i)
|
|
|
|
close(i);
|
|
|
|
|
|
|
|
// Launch daemon to setup overlay
|
|
|
|
if (fork_dont_care() == 0)
|
|
|
|
setup_overlay();
|
2017-11-05 21:41:03 +00:00
|
|
|
|
2018-11-08 18:40:57 +00:00
|
|
|
exec_init(argv);
|
2017-11-05 21:41:03 +00:00
|
|
|
}
|