2017-11-06 05:41:03 +08:00
|
|
|
/* magiskinit.c - Pre-init Magisk support
|
|
|
|
*
|
|
|
|
* This code has to be compiled statically to work properly.
|
|
|
|
*
|
2017-11-10 00:54:54 +08: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-06 05:41:03 +08: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-10 00:54:54 +08:00
|
|
|
* rootfs files from system.
|
|
|
|
*
|
2017-11-06 05:41:03 +08: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)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-12-05 21:05:20 +08:00
|
|
|
#define _GNU_SOURCE
|
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>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
|
2017-11-10 00:54:54 +08:00
|
|
|
#include <lzma.h>
|
2017-11-06 05:41:03 +08:00
|
|
|
#include <cil/cil.h>
|
|
|
|
|
2017-11-28 03:42:48 +08:00
|
|
|
#include "dump.h"
|
|
|
|
#include "magiskrc.h"
|
2017-11-06 05:41:03 +08:00
|
|
|
#include "utils.h"
|
|
|
|
#include "magiskpolicy.h"
|
2017-11-28 03:42:48 +08:00
|
|
|
#include "daemon.h"
|
2017-12-06 12:51:16 +08:00
|
|
|
#include "cpio.h"
|
2017-11-28 03:42:48 +08:00
|
|
|
#include "magisk.h"
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2017-12-07 03:21:13 +08:00
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
#define VLOG(fmt, ...) printf(fmt, __VA_ARGS__)
|
|
|
|
#else
|
2017-11-06 22:33:41 +08:00
|
|
|
#define VLOG(fmt, ...)
|
2017-12-07 03:21:13 +08:00
|
|
|
#endif
|
2017-11-06 22:33:41 +08:00
|
|
|
|
2017-12-07 03:21:13 +08:00
|
|
|
extern policydb_t *policydb;
|
2017-11-28 04:43:46 +08:00
|
|
|
int (*init_applet_main[]) (int, char *[]) = { magiskpolicy_main, magiskpolicy_main, NULL };
|
2018-02-11 03:40:09 +08:00
|
|
|
static char RAND_SOCKET_NAME[sizeof(SOCKET_NAME)];
|
|
|
|
static int SOCKET_OFF = -1;
|
2017-11-28 04:43:46 +08:00
|
|
|
|
2017-11-06 05:41:03 +08:00
|
|
|
struct cmdline {
|
2018-02-14 00:57:52 +08:00
|
|
|
char skip_initramfs;
|
2017-11-06 05:41:03 +08:00
|
|
|
char slot[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct device {
|
|
|
|
dev_t major;
|
|
|
|
dev_t minor;
|
|
|
|
char devname[32];
|
|
|
|
char partname[32];
|
|
|
|
char path[64];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void parse_cmdline(struct cmdline *cmd) {
|
2017-11-23 18:38:12 +08:00
|
|
|
// cleanup
|
2018-02-14 00:57:52 +08:00
|
|
|
memset(cmd, 0, sizeof(&cmd));
|
2017-11-23 18:38:12 +08:00
|
|
|
|
|
|
|
char cmdline[4096];
|
2017-11-06 05:41:03 +08:00
|
|
|
mkdir("/proc", 0555);
|
2018-01-11 03:06:20 +08:00
|
|
|
xmount("proc", "/proc", "proc", 0, NULL);
|
2017-11-06 05:41:03 +08:00
|
|
|
int fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
|
2017-11-23 18:38:12 +08:00
|
|
|
cmdline[read(fd, cmdline, sizeof(cmdline))] = '\0';
|
2017-11-06 05:41:03 +08:00
|
|
|
close(fd);
|
|
|
|
umount("/proc");
|
2017-12-07 01:30:48 +08:00
|
|
|
for (char *tok = strtok(cmdline, " "); tok; tok = strtok(NULL, " ")) {
|
2017-11-06 05:41:03 +08:00
|
|
|
if (strncmp(tok, "androidboot.slot_suffix", 23) == 0) {
|
|
|
|
sscanf(tok, "androidboot.slot_suffix=%s", cmd->slot);
|
2017-11-06 22:33:41 +08:00
|
|
|
} else if (strncmp(tok, "androidboot.slot", 16) == 0) {
|
|
|
|
cmd->slot[0] = '_';
|
2018-02-14 00:57:52 +08:00
|
|
|
sscanf(tok, "androidboot.slot=%c", cmd->slot + 1);
|
2017-11-06 05:41:03 +08:00
|
|
|
} else if (strcmp(tok, "skip_initramfs") == 0) {
|
|
|
|
cmd->skip_initramfs = 1;
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 00:57:52 +08:00
|
|
|
|
|
|
|
VLOG("cmdline: skip_initramfs[%d] slot[%s]\n", cmd->skip_initramfs, cmd->slot);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_device(struct device *dev, char *uevent) {
|
2017-11-06 22:33:41 +08:00
|
|
|
dev->partname[0] = '\0';
|
2017-11-06 05:41:03 +08:00
|
|
|
char *tok;
|
|
|
|
tok = strtok(uevent, "\n");
|
|
|
|
while (tok != NULL) {
|
|
|
|
if (strncmp(tok, "MAJOR", 5) == 0) {
|
|
|
|
sscanf(tok, "MAJOR=%ld", (long*) &dev->major);
|
|
|
|
} else if (strncmp(tok, "MINOR", 5) == 0) {
|
|
|
|
sscanf(tok, "MINOR=%ld", (long*) &dev->minor);
|
|
|
|
} else if (strncmp(tok, "DEVNAME", 7) == 0) {
|
|
|
|
sscanf(tok, "DEVNAME=%s", dev->devname);
|
|
|
|
} else if (strncmp(tok, "PARTNAME", 8) == 0) {
|
|
|
|
sscanf(tok, "PARTNAME=%s", dev->partname);
|
|
|
|
}
|
|
|
|
tok = strtok(NULL, "\n");
|
|
|
|
}
|
2017-11-06 22:33:41 +08:00
|
|
|
VLOG("%s [%s] (%u, %u)\n", dev->devname, dev->partname, (unsigned) dev->major, (unsigned) dev->minor);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int setup_block(struct device *dev, const char *partname) {
|
|
|
|
char buffer[1024], path[128];
|
|
|
|
struct dirent *entry;
|
|
|
|
DIR *dir = opendir("/sys/dev/block");
|
|
|
|
if (dir == NULL)
|
|
|
|
return 1;
|
|
|
|
int found = 0;
|
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
snprintf(path, sizeof(path), "/sys/dev/block/%s/uevent", entry->d_name);
|
|
|
|
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
|
|
ssize_t size = read(fd, buffer, sizeof(buffer));
|
|
|
|
buffer[size] = '\0';
|
|
|
|
close(fd);
|
|
|
|
parse_device(dev, buffer);
|
|
|
|
if (strcmp(dev->partname, partname) == 0) {
|
|
|
|
snprintf(dev->path, sizeof(dev->path), "/dev/block/%s", dev->devname);
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
mkdir("/dev", 0755);
|
|
|
|
mkdir("/dev/block", 0755);
|
|
|
|
mknod(dev->path, S_IFBLK | 0600, makedev(dev->major, dev->minor));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
static void patch_ramdisk() {
|
2017-11-06 05:41:03 +08:00
|
|
|
void *addr;
|
|
|
|
size_t size;
|
|
|
|
mmap_rw("/init", &addr, &size);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2017-12-01 03:33:25 +08:00
|
|
|
if (memcmp(addr + i, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL) - 1) == 0) {
|
|
|
|
memcpy(addr + i + sizeof(SPLIT_PLAT_CIL) - 4, "xxx", 3);
|
2017-11-06 05:41:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(addr, size);
|
|
|
|
|
2017-12-07 01:30:48 +08:00
|
|
|
full_read("/init.rc", &addr, &size);
|
|
|
|
patch_init_rc(&addr, &size);
|
|
|
|
int fd = creat("/init.rc", 0750);
|
|
|
|
write(fd, addr, size);
|
2017-11-06 05:41:03 +08:00
|
|
|
close(fd);
|
2017-12-07 01:30:48 +08:00
|
|
|
free(addr);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int strend(const char *s1, const char *s2) {
|
2018-02-02 03:22:38 +08:00
|
|
|
size_t l1 = strlen(s1);
|
|
|
|
size_t l2 = strlen(s2);
|
2017-11-06 05:41:03 +08:00
|
|
|
return strcmp(s1 + l1 - l2, s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compile_cil() {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
char path[128];
|
|
|
|
|
|
|
|
struct cil_db *db = NULL;
|
|
|
|
sepol_policydb_t *pdb = NULL;
|
|
|
|
void *addr;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
cil_db_init(&db);
|
|
|
|
cil_set_mls(db, 1);
|
2017-12-22 18:26:51 +08:00
|
|
|
cil_set_multiple_decls(db, 1);
|
|
|
|
cil_set_disable_neverallow(db, 1);
|
2017-11-06 05:41:03 +08:00
|
|
|
cil_set_target_platform(db, SEPOL_TARGET_SELINUX);
|
|
|
|
cil_set_policy_version(db, POLICYDB_VERSION_XPERMS_IOCTL);
|
|
|
|
cil_set_attrs_expand_generated(db, 0);
|
|
|
|
|
|
|
|
// plat
|
2017-12-01 03:33:25 +08:00
|
|
|
mmap_ro(SPLIT_PLAT_CIL, &addr, &size);
|
|
|
|
VLOG("cil_add[%s]\n", SPLIT_PLAT_CIL);
|
|
|
|
cil_add_file(db, SPLIT_PLAT_CIL, addr, size);
|
2017-11-06 05:41:03 +08:00
|
|
|
munmap(addr, size);
|
|
|
|
|
|
|
|
// mapping
|
|
|
|
char plat[10];
|
2017-12-01 03:33:25 +08:00
|
|
|
int fd = open(SPLIT_NONPLAT_VER, O_RDONLY | O_CLOEXEC);
|
|
|
|
plat[read(fd, plat, sizeof(plat)) - 1] = '\0';
|
|
|
|
snprintf(path, sizeof(path), SPLIT_PLAT_MAPPING, plat);
|
|
|
|
mmap_ro(path, &addr, &size);
|
|
|
|
VLOG("cil_add[%s]\n", path);
|
|
|
|
cil_add_file(db, path, addr, size);
|
|
|
|
munmap(addr, size);
|
|
|
|
close(fd);
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2017-12-01 03:33:25 +08:00
|
|
|
// nonplat
|
|
|
|
dir = opendir(NONPLAT_POLICY_DIR);
|
2017-11-06 05:41:03 +08:00
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (strend(entry->d_name, ".cil") == 0) {
|
2017-12-01 03:33:25 +08:00
|
|
|
snprintf(path, sizeof(path), NONPLAT_POLICY_DIR "%s", entry->d_name);
|
2017-11-06 05:41:03 +08:00
|
|
|
mmap_ro(path, &addr, &size);
|
2017-12-01 03:33:25 +08:00
|
|
|
VLOG("cil_add[%s]\n", path);
|
2017-11-06 05:41:03 +08:00
|
|
|
cil_add_file(db, path, addr, size);
|
|
|
|
munmap(addr, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
cil_compile(db);
|
|
|
|
cil_build_policydb(db, &pdb);
|
|
|
|
cil_db_destroy(&db);
|
|
|
|
|
|
|
|
policydb = &pdb->p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int verify_precompiled() {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
int fd;
|
|
|
|
char sys_sha[70], ven_sha[70];
|
|
|
|
|
2017-12-01 03:33:25 +08:00
|
|
|
// init the strings with different value
|
|
|
|
sys_sha[0] = 0;
|
|
|
|
ven_sha[0] = 1;
|
|
|
|
|
|
|
|
dir = opendir(NONPLAT_POLICY_DIR);
|
2017-11-06 05:41:03 +08: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);
|
2017-12-01 03:33:25 +08:00
|
|
|
ven_sha[read(fd, ven_sha, sizeof(ven_sha)) - 1] = '\0';
|
2017-11-06 05:41:03 +08:00
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2017-12-01 03:33:25 +08:00
|
|
|
dir = opendir(PLAT_POLICY_DIR);
|
2017-11-06 05:41:03 +08: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);
|
2017-12-01 03:33:25 +08:00
|
|
|
sys_sha[read(fd, sys_sha, sizeof(sys_sha)) - 1] = '\0';
|
2017-11-06 05:41:03 +08:00
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2017-12-01 03:33:25 +08:00
|
|
|
VLOG("sys_sha[%s]\nven_sha[%s]\n", sys_sha, ven_sha);
|
|
|
|
return strcmp(sys_sha, ven_sha) == 0;
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2017-12-31 07:30:56 -06:00
|
|
|
static int patch_sepolicy() {
|
2017-12-01 03:33:25 +08:00
|
|
|
if (access("/sepolicy", R_OK) == 0)
|
2017-11-06 05:41:03 +08:00
|
|
|
load_policydb("/sepolicy");
|
2017-12-01 03:33:25 +08:00
|
|
|
else if (access(SPLIT_PRECOMPILE, R_OK) == 0 && verify_precompiled())
|
|
|
|
load_policydb(SPLIT_PRECOMPILE);
|
|
|
|
else if (access(SPLIT_PLAT_CIL, R_OK) == 0)
|
2017-11-06 05:41:03 +08:00
|
|
|
compile_cil();
|
2017-12-31 07:30:56 -06:00
|
|
|
else
|
|
|
|
return 1;
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2017-11-30 20:57:40 +08:00
|
|
|
sepol_magisk_rules();
|
2017-11-06 05:41:03 +08:00
|
|
|
dump_policydb("/sepolicy");
|
2017-12-31 07:30:56 -06:00
|
|
|
|
2018-02-02 04:17:13 +08:00
|
|
|
// Remove the stupid debug sepolicy and use our own
|
|
|
|
if (access("/sepolicy_debug", F_OK) == 0) {
|
|
|
|
unlink("/sepolicy_debug");
|
|
|
|
link("/sepolicy", "/sepolicy_debug");
|
|
|
|
}
|
|
|
|
|
2017-12-31 07:30:56 -06:00
|
|
|
return 0;
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2017-11-10 00:54:54 +08:00
|
|
|
#define BUFSIZE (1 << 20)
|
|
|
|
|
|
|
|
static int unxz(const void *buf, size_t size, int fd) {
|
|
|
|
lzma_stream strm = LZMA_STREAM_INIT;
|
|
|
|
if (lzma_auto_decoder(&strm, UINT64_MAX, 0) != LZMA_OK)
|
|
|
|
return 1;
|
2018-02-02 03:22:38 +08:00
|
|
|
lzma_ret ret;
|
2017-11-10 00:54:54 +08:00
|
|
|
void *out = malloc(BUFSIZE);
|
|
|
|
strm.next_in = buf;
|
|
|
|
strm.avail_in = size;
|
|
|
|
do {
|
|
|
|
strm.next_out = out;
|
|
|
|
strm.avail_out = BUFSIZE;
|
|
|
|
ret = lzma_code(&strm, LZMA_RUN);
|
|
|
|
write(fd, out, BUFSIZE - strm.avail_out);
|
|
|
|
} while (strm.avail_out == 0 && ret == LZMA_OK);
|
|
|
|
|
|
|
|
free(out);
|
|
|
|
lzma_end(&strm);
|
|
|
|
|
|
|
|
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_magisk(const char *path, mode_t mode) {
|
|
|
|
unlink(path);
|
|
|
|
int fd = creat(path, mode);
|
|
|
|
int ret = unxz(magisk_dump, sizeof(magisk_dump), fd);
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_magiskrc(const char *path, mode_t mode) {
|
|
|
|
int fd = creat(path, mode);
|
|
|
|
write(fd, magiskrc, sizeof(magiskrc));
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-11 03:40:09 +08:00
|
|
|
static void patch_socket_name(const char *path) {
|
|
|
|
void *buf;
|
|
|
|
size_t size;
|
|
|
|
mmap_rw(path, &buf, &size);
|
|
|
|
if (SOCKET_OFF < 0) {
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2018-02-21 14:44:24 +08:00
|
|
|
if (memcmp(buf + i, socket_name, sizeof(SOCKET_NAME)) == 0) {
|
2018-02-11 03:40:09 +08:00
|
|
|
SOCKET_OFF = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gen_rand_str(RAND_SOCKET_NAME, sizeof(SOCKET_NAME));
|
|
|
|
memcpy(buf + SOCKET_OFF, RAND_SOCKET_NAME, sizeof(SOCKET_NAME));
|
|
|
|
munmap(buf, size);
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:37:28 +08:00
|
|
|
static void magisk_init_daemon() {
|
|
|
|
setsid();
|
2017-11-30 20:57:40 +08:00
|
|
|
|
|
|
|
// Full patch
|
2017-11-27 15:37:28 +08:00
|
|
|
sepol_allow("su", ALL, ALL, ALL);
|
2017-11-28 03:42:48 +08:00
|
|
|
|
|
|
|
// Wait till init cold boot done
|
2017-12-31 19:32:04 +08:00
|
|
|
while (access("/dev/.coldboot_done", F_OK))
|
|
|
|
usleep(1);
|
2017-11-28 03:42:48 +08:00
|
|
|
|
2017-12-16 04:40:54 +08:00
|
|
|
int null = open("/dev/null", O_RDWR | O_CLOEXEC);
|
|
|
|
dup3(null, STDIN_FILENO, O_CLOEXEC);
|
|
|
|
dup3(null, STDOUT_FILENO, O_CLOEXEC);
|
|
|
|
dup3(null, STDERR_FILENO, O_CLOEXEC);
|
2018-02-11 03:40:09 +08:00
|
|
|
if (null > STDERR_FILENO)
|
|
|
|
close(null);
|
2017-12-16 04:40:54 +08:00
|
|
|
|
2017-11-28 03:42:48 +08:00
|
|
|
// Transit our context to su (mimic setcon)
|
|
|
|
int fd, crap;
|
|
|
|
fd = open("/proc/self/attr/current", O_WRONLY);
|
|
|
|
write(fd, "u:r:su:s0", 9);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
// Dump full patch to kernel
|
2017-11-27 15:37:28 +08:00
|
|
|
dump_policydb(SELINUX_LOAD);
|
2017-11-30 20:57:40 +08:00
|
|
|
close(creat(PATCHDONE, 0));
|
2017-11-27 15:37:28 +08:00
|
|
|
destroy_policydb();
|
2017-11-28 03:42:48 +08:00
|
|
|
|
|
|
|
// Keep Magisk daemon always alive
|
|
|
|
while (1) {
|
2017-12-18 02:51:27 +08:00
|
|
|
struct sockaddr_un sun;
|
|
|
|
fd = setup_socket(&sun);
|
2018-02-11 03:40:09 +08:00
|
|
|
memcpy(sun.sun_path + 1, RAND_SOCKET_NAME, sizeof(SOCKET_NAME));
|
2017-11-28 03:42:48 +08:00
|
|
|
while (connect(fd, (struct sockaddr*) &sun, sizeof(sun)))
|
|
|
|
usleep(10000); /* Wait 10 ms after each try */
|
|
|
|
|
|
|
|
/* Should hold forever */
|
|
|
|
read(fd, &crap, sizeof(crap));
|
|
|
|
|
|
|
|
/* If things went here, it means the other side of the socket is closed
|
|
|
|
* We restart the daemon again */
|
2017-12-18 02:51:27 +08:00
|
|
|
close(fd);
|
2017-11-28 03:42:48 +08:00
|
|
|
if (fork_dont_care() == 0) {
|
2017-12-19 14:27:54 +08:00
|
|
|
execv("/sbin/magisk", (char *[]) { "magisk", "--daemon", NULL } );
|
2017-11-28 03:42:48 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2017-11-27 15:37:28 +08:00
|
|
|
}
|
|
|
|
|
2017-11-06 05:41:03 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2017-11-23 18:38:12 +08:00
|
|
|
umask(0);
|
|
|
|
|
2017-11-28 04:43:46 +08: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-06 05:41:03 +08:00
|
|
|
|
2017-11-23 18:38:12 +08:00
|
|
|
if (argc > 1 && strcmp(argv[1], "-x") == 0) {
|
2017-11-10 00:54:54 +08:00
|
|
|
if (strcmp(argv[2], "magisk") == 0)
|
|
|
|
return dump_magisk(argv[3], 0755);
|
|
|
|
else if (strcmp(argv[2], "magiskrc") == 0)
|
|
|
|
return dump_magiskrc(argv[3], 0755);
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
dup3(null, STDIN_FILENO, O_CLOEXEC);
|
|
|
|
dup3(null, STDOUT_FILENO, O_CLOEXEC);
|
|
|
|
dup3(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
|
|
|
|
2017-11-10 00:54:54 +08:00
|
|
|
// Extract and link files
|
|
|
|
mkdir("/overlay", 0000);
|
|
|
|
dump_magiskrc("/overlay/init.magisk.rc", 0750);
|
|
|
|
mkdir("/overlay/sbin", 0755);
|
|
|
|
dump_magisk("/overlay/sbin/magisk", 0755);
|
2018-02-14 00:57:52 +08:00
|
|
|
patch_socket_name("/overlay/sbin/magisk");
|
2017-11-10 00:54:54 +08:00
|
|
|
mkdir("/overlay/root", 0755);
|
|
|
|
link("/init", "/overlay/root/magiskinit");
|
|
|
|
|
2017-11-06 05:41:03 +08:00
|
|
|
struct cmdline cmd;
|
|
|
|
parse_cmdline(&cmd);
|
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
/* ***********
|
|
|
|
* Initialize
|
|
|
|
* ***********/
|
2017-11-06 22:33:41 +08:00
|
|
|
|
2017-11-06 05:41:03 +08:00
|
|
|
int root = open("/", O_RDONLY | O_CLOEXEC);
|
|
|
|
|
|
|
|
if (cmd.skip_initramfs) {
|
|
|
|
// Clear rootfs
|
2018-02-14 00:57:52 +08:00
|
|
|
excl_list = (char *[]) { "overlay", ".backup", NULL };
|
|
|
|
frm_rf(root);
|
|
|
|
} else if (access("/ramdisk.cpio.xz", R_OK) == 0) {
|
|
|
|
// High compression mode
|
|
|
|
void *addr;
|
|
|
|
size_t size;
|
|
|
|
mmap_ro("/ramdisk.cpio.xz", &addr, &size);
|
|
|
|
int fd = creat("/ramdisk.cpio", 0);
|
|
|
|
unxz(addr, size, fd);
|
|
|
|
munmap(addr, size);
|
|
|
|
close(fd);
|
|
|
|
struct vector v;
|
|
|
|
vec_init(&v);
|
|
|
|
parse_cpio(&v, "/ramdisk.cpio");
|
|
|
|
excl_list = (char *[]) { "overlay", ".backup", NULL };
|
2017-11-06 05:41:03 +08:00
|
|
|
frm_rf(root);
|
2018-02-14 00:57:52 +08:00
|
|
|
chdir("/");
|
|
|
|
cpio_extract_all(&v);
|
|
|
|
cpio_vec_destroy(&v);
|
|
|
|
} else {
|
|
|
|
// Revert original init binary
|
|
|
|
unlink("/init");
|
|
|
|
link("/.backup/init", "/init");
|
|
|
|
}
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
/* ************
|
|
|
|
* Early Mount
|
|
|
|
* ************/
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
// If skip_initramfs or using split policies, we need early mount
|
|
|
|
if (cmd.skip_initramfs || access("/sepolicy", R_OK) != 0) {
|
2017-11-06 05:41:03 +08:00
|
|
|
char partname[32];
|
|
|
|
struct device dev;
|
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
// Mount sysfs
|
|
|
|
mkdir("/sys", 0755);
|
|
|
|
xmount("sysfs", "/sys", "sysfs", 0, NULL);
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
// Mount system
|
|
|
|
snprintf(partname, sizeof(partname), "system%s", cmd.slot);
|
|
|
|
setup_block(&dev, partname);
|
|
|
|
if (cmd.skip_initramfs) {
|
|
|
|
mkdir("/system_root", 0755);
|
|
|
|
xmount(dev.path, "/system_root", "ext4", MS_RDONLY, NULL);
|
|
|
|
int system_root = open("/system_root", O_RDONLY | O_CLOEXEC);
|
|
|
|
|
|
|
|
// Clone rootfs except /system
|
|
|
|
excl_list = (char *[]) { "system", NULL };
|
|
|
|
clone_dir(system_root, root);
|
|
|
|
close(system_root);
|
|
|
|
|
|
|
|
mkdir("/system", 0755);
|
|
|
|
xmount("/system_root/system", "/system", NULL, MS_BIND, NULL);
|
|
|
|
} else {
|
|
|
|
xmount(dev.path, "/system", "ext4", MS_RDONLY, NULL);
|
|
|
|
}
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
// Mount vendor
|
2017-11-06 05:41:03 +08:00
|
|
|
snprintf(partname, sizeof(partname), "vendor%s", cmd.slot);
|
|
|
|
if (setup_block(&dev, partname) == 0)
|
2018-01-11 03:06:20 +08:00
|
|
|
xmount(dev.path, "/vendor", "ext4", MS_RDONLY, NULL);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
/* *************
|
|
|
|
* Patch rootfs
|
|
|
|
* *************/
|
2017-12-07 03:21:13 +08:00
|
|
|
|
|
|
|
// Only patch rootfs if not intended to run in recovery
|
2017-12-05 21:05:20 +08:00
|
|
|
if (access("/etc/recovery.fstab", F_OK) != 0) {
|
2018-02-02 03:22:38 +08:00
|
|
|
int overlay = open("/overlay", O_RDONLY | O_CLOEXEC);
|
2017-12-05 21:05:20 +08:00
|
|
|
mv_dir(overlay, root);
|
2018-02-14 00:57:52 +08:00
|
|
|
close(overlay);
|
|
|
|
rmdir("/overlay");
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2018-02-14 00:57:52 +08:00
|
|
|
patch_ramdisk();
|
|
|
|
patch_sepolicy();
|
2017-11-23 18:38:12 +08:00
|
|
|
|
2018-02-11 03:40:09 +08:00
|
|
|
close(STDIN_FILENO);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
2018-02-02 03:22:38 +08:00
|
|
|
|
2017-12-05 21:05:20 +08:00
|
|
|
if (fork_dont_care() == 0) {
|
|
|
|
strcpy(argv[0], "magiskinit");
|
2017-12-16 04:40:54 +08:00
|
|
|
close(root);
|
2017-12-05 21:05:20 +08:00
|
|
|
magisk_init_daemon();
|
|
|
|
}
|
2017-11-28 03:42:48 +08:00
|
|
|
}
|
2017-11-06 05:41:03 +08:00
|
|
|
|
2017-12-07 03:21:13 +08:00
|
|
|
// Clean up
|
|
|
|
close(root);
|
2018-02-14 00:57:52 +08:00
|
|
|
if (!cmd.skip_initramfs)
|
|
|
|
umount("/system");
|
2017-12-05 21:05:20 +08:00
|
|
|
umount("/vendor");
|
2017-12-07 03:21:13 +08:00
|
|
|
|
2017-11-23 18:38:12 +08:00
|
|
|
// Finally, give control back!
|
|
|
|
execv("/init", argv);
|
2017-11-06 05:41:03 +08:00
|
|
|
}
|