2019-05-27 00:29:43 -07:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2019-07-01 22:58:19 -07:00
|
|
|
#include <vector>
|
2019-05-27 00:29:43 -07:00
|
|
|
|
|
|
|
#include <xz.h>
|
2020-03-09 01:50:30 -07:00
|
|
|
#include <magisk.hpp>
|
|
|
|
#include <cpio.hpp>
|
|
|
|
#include <utils.hpp>
|
2019-05-27 00:29:43 -07:00
|
|
|
#include <flags.h>
|
|
|
|
|
|
|
|
#include "binaries.h"
|
|
|
|
#ifdef USE_64BIT
|
|
|
|
#include "binaries_arch64.h"
|
|
|
|
#else
|
|
|
|
#include "binaries_arch.h"
|
|
|
|
#endif
|
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "init.hpp"
|
2019-05-27 00:29:43 -07:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-06-22 03:14:33 -07:00
|
|
|
constexpr const char *init_applet[] =
|
|
|
|
{ "magiskpolicy", "supolicy", "magisk", nullptr };
|
|
|
|
constexpr int (*init_applet_main[])(int, char *[]) =
|
|
|
|
{ magiskpolicy_main, magiskpolicy_main, magisk_proxy_main, nullptr };
|
|
|
|
|
2019-05-27 00:29:43 -07:00
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
static FILE *kmsg;
|
2019-11-18 17:18:56 -05:00
|
|
|
static char kmsg_buf[4096];
|
2019-05-27 00:29:43 -07:00
|
|
|
static int vprintk(const char *fmt, va_list ap) {
|
2019-11-18 17:18:56 -05:00
|
|
|
vsnprintf(kmsg_buf + 12, sizeof(kmsg_buf) - 12, fmt, ap);
|
|
|
|
return fprintf(kmsg, "%s", kmsg_buf);
|
2019-05-27 00:29:43 -07:00
|
|
|
}
|
2019-11-18 17:18:56 -05:00
|
|
|
void setup_klog() {
|
|
|
|
// Shut down first 3 fds
|
2019-11-25 19:09:02 -05:00
|
|
|
int fd;
|
2019-11-18 17:18:56 -05:00
|
|
|
if (access("/dev/null", W_OK) == 0) {
|
2019-11-25 19:09:02 -05:00
|
|
|
fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
|
2019-11-18 17:18:56 -05:00
|
|
|
} else {
|
|
|
|
mknod("/null", S_IFCHR | 0666, makedev(1, 3));
|
2019-11-25 19:09:02 -05:00
|
|
|
fd = xopen("/null", O_RDWR | O_CLOEXEC);
|
2019-11-18 17:18:56 -05:00
|
|
|
unlink("/null");
|
|
|
|
}
|
2019-11-25 19:09:02 -05:00
|
|
|
xdup3(fd, STDIN_FILENO, O_CLOEXEC);
|
|
|
|
xdup3(fd, STDOUT_FILENO, O_CLOEXEC);
|
|
|
|
xdup3(fd, STDERR_FILENO, O_CLOEXEC);
|
|
|
|
if (fd > STDERR_FILENO)
|
|
|
|
close(fd);
|
2019-11-18 17:18:56 -05:00
|
|
|
|
2019-11-25 19:09:02 -05:00
|
|
|
if (access("/dev/kmsg", W_OK) == 0) {
|
|
|
|
fd = xopen("/dev/kmsg", O_WRONLY | O_CLOEXEC);
|
2019-11-18 17:18:56 -05:00
|
|
|
} else {
|
|
|
|
mknod("/kmsg", S_IFCHR | 0666, makedev(1, 11));
|
|
|
|
fd = xopen("/kmsg", O_WRONLY | O_CLOEXEC);
|
|
|
|
unlink("/kmsg");
|
|
|
|
}
|
|
|
|
|
|
|
|
kmsg = fdopen(fd, "w");
|
|
|
|
setbuf(kmsg, nullptr);
|
|
|
|
log_cb.d = log_cb.i = log_cb.w = log_cb.e = vprintk;
|
|
|
|
log_cb.ex = nop_ex;
|
|
|
|
strcpy(kmsg_buf, "magiskinit: ");
|
2019-05-27 00:29:43 -07:00
|
|
|
}
|
|
|
|
#else
|
2019-11-18 17:18:56 -05:00
|
|
|
void setup_klog() {}
|
2019-05-27 00:29:43 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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 = {
|
2019-12-06 12:02:34 -05:00
|
|
|
.in = buf,
|
|
|
|
.in_pos = 0,
|
|
|
|
.in_size = size,
|
|
|
|
.out = out,
|
|
|
|
.out_pos = 0,
|
|
|
|
.out_size = sizeof(out)
|
2019-05-27 00:29:43 -07:00
|
|
|
};
|
|
|
|
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;
|
|
|
|
LOGD("Decompressing ramdisk from %s\n", ramdisk_xz);
|
|
|
|
uint8_t *buf;
|
|
|
|
size_t sz;
|
|
|
|
mmap_ro(ramdisk_xz, buf, sz);
|
2020-04-03 02:58:39 -07:00
|
|
|
int fd = xopen(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
2019-05-27 00:29:43 -07:00
|
|
|
unxz(fd, buf, sz);
|
|
|
|
munmap(buf, sz);
|
|
|
|
close(fd);
|
|
|
|
cpio_mmap cpio(tmp);
|
|
|
|
cpio.extract();
|
|
|
|
unlink(tmp);
|
|
|
|
unlink(ramdisk_xz);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dump_magisk(const char *path, mode_t mode) {
|
2020-04-03 02:58:39 -07:00
|
|
|
int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
2019-05-27 00:29:43 -07:00
|
|
|
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) {
|
2020-04-03 02:58:39 -07:00
|
|
|
int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
2019-05-27 00:29:43 -07:00
|
|
|
if (fd < 0)
|
|
|
|
return 1;
|
|
|
|
if (!unxz(fd, manager_xz, sizeof(manager_xz)))
|
|
|
|
return 1;
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-16 12:45:32 -07:00
|
|
|
class RecoveryInit : public BaseInit {
|
|
|
|
public:
|
|
|
|
RecoveryInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
|
|
|
|
void start() override {
|
|
|
|
LOGD("Ramdisk is recovery, abort\n");
|
|
|
|
rename("/.backup/init", "/init");
|
|
|
|
rm_rf("/.backup");
|
2019-06-30 11:39:13 -07:00
|
|
|
exec_init();
|
2019-06-16 12:45:32 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-18 17:18:56 -05:00
|
|
|
class TestInit : public BaseInit {
|
2019-06-16 12:45:32 -07:00
|
|
|
public:
|
2019-11-18 17:18:56 -05:00
|
|
|
TestInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
|
2019-06-16 12:45:32 -07:00
|
|
|
void start() override {
|
2019-12-06 12:02:34 -05:00
|
|
|
// Place init tests here
|
2019-06-16 12:45:32 -07:00
|
|
|
}
|
|
|
|
};
|
2019-05-27 00:29:43 -07:00
|
|
|
|
2019-12-06 12:02:34 -05:00
|
|
|
[[maybe_unused]] static int test_main(int argc, char *argv[]) {
|
2019-06-25 03:34:54 -07:00
|
|
|
// Log to console
|
|
|
|
cmdline_logging();
|
|
|
|
log_cb.ex = nop_ex;
|
|
|
|
|
|
|
|
// Switch to isolate namespace
|
|
|
|
xunshare(CLONE_NEWNS);
|
|
|
|
xmount(nullptr, "/", nullptr, MS_PRIVATE | MS_REC, nullptr);
|
|
|
|
|
|
|
|
// Unmount everything in reverse
|
|
|
|
vector<string> mounts;
|
|
|
|
parse_mnt("/proc/mounts", [&](mntent *me) {
|
|
|
|
if (me->mnt_dir != "/"sv)
|
|
|
|
mounts.emplace_back(me->mnt_dir);
|
|
|
|
return true;
|
|
|
|
});
|
2019-12-06 12:02:34 -05:00
|
|
|
for (auto &m : reversed(mounts))
|
|
|
|
xumount(m.data());
|
2019-06-25 03:34:54 -07:00
|
|
|
|
|
|
|
// chroot jail
|
2019-12-06 12:02:34 -05:00
|
|
|
chdir(dirname(argv[0]));
|
2019-06-25 03:34:54 -07:00
|
|
|
chroot(".");
|
|
|
|
chdir("/");
|
2019-12-06 12:02:34 -05:00
|
|
|
|
|
|
|
cmdline cmd{};
|
|
|
|
load_kernel_info(&cmd);
|
|
|
|
|
|
|
|
auto init = make_unique<TestInit>(argv, &cmd);
|
|
|
|
init->start();
|
|
|
|
|
|
|
|
return 1;
|
2019-06-25 03:34:54 -07:00
|
|
|
}
|
|
|
|
|
2019-05-27 00:29:43 -07:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
umask(0);
|
|
|
|
|
|
|
|
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-12-06 12:02:34 -05:00
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
if (getenv("INIT_TEST") != nullptr)
|
|
|
|
return test_main(argc, argv);
|
|
|
|
#endif
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
if (argc > 1 && argv[1] == "-x"sv) {
|
|
|
|
if (argv[2] == "magisk"sv)
|
2019-05-27 00:29:43 -07:00
|
|
|
return dump_magisk(argv[3], 0755);
|
2019-12-13 00:37:06 -05:00
|
|
|
else if (argv[2] == "manager"sv)
|
2019-05-27 00:29:43 -07:00
|
|
|
return dump_manager(argv[3], 0644);
|
|
|
|
}
|
|
|
|
|
2019-12-06 12:02:34 -05:00
|
|
|
if (getpid() != 1)
|
|
|
|
return 1;
|
|
|
|
setup_klog();
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
unique_ptr<BaseInit> init;
|
2019-06-15 22:25:09 -07:00
|
|
|
cmdline cmd{};
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
if (argc > 1 && argv[1] == "selinux_setup"sv) {
|
|
|
|
init = make_unique<SecondStageInit>(argv);
|
2019-06-16 12:45:32 -07:00
|
|
|
} else {
|
2019-12-13 00:37:06 -05:00
|
|
|
// This will also mount /sys and /proc
|
|
|
|
load_kernel_info(&cmd);
|
|
|
|
|
2020-04-01 04:39:28 -07:00
|
|
|
if (access("/apex", F_OK) == 0) {
|
|
|
|
if (cmd.force_normal_boot)
|
|
|
|
init = make_unique<ForcedFirstStageInit>(argv, &cmd);
|
|
|
|
else if (cmd.skip_initramfs)
|
|
|
|
init = make_unique<SARFirstStageInit>(argv, &cmd);
|
|
|
|
else
|
|
|
|
init = make_unique<FirstStageInit>(argv, &cmd);
|
2019-12-13 00:37:06 -05:00
|
|
|
} else if (cmd.skip_initramfs) {
|
|
|
|
init = make_unique<SARInit>(argv, &cmd);
|
|
|
|
} else {
|
|
|
|
decompress_ramdisk();
|
|
|
|
if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0)
|
|
|
|
init = make_unique<RecoveryInit>(argv, &cmd);
|
|
|
|
else
|
|
|
|
init = make_unique<RootFSInit>(argv, &cmd);
|
|
|
|
}
|
2019-06-16 12:45:32 -07:00
|
|
|
}
|
2019-05-27 00:29:43 -07:00
|
|
|
|
|
|
|
// Run the main routine
|
2019-06-16 12:45:32 -07:00
|
|
|
init->start();
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 00:47:29 -07:00
|
|
|
exit(1);
|
2019-05-27 00:29:43 -07:00
|
|
|
}
|