Magisk/native/jni/daemon/bootstages.cpp

920 lines
23 KiB
C++
Raw Normal View History

2017-07-10 16:48:14 +02:00
/* bootstages.c - Core bootstage operations
*
* All bootstage operations, including simple mount in post-fs,
* magisk mount in post-fs-data, various image handling, script
* execution, load modules, install Magisk Manager etc.
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>
#include <sys/mount.h>
#include <sys/wait.h>
#include "magisk.h"
2018-06-13 23:09:54 +02:00
#include "db.h"
2017-04-30 19:58:52 +02:00
#include "utils.h"
2018-08-09 18:40:23 +02:00
#include "img.h"
2017-04-30 19:58:52 +02:00
#include "daemon.h"
#include "resetprop.h"
2018-09-27 06:09:59 +02:00
#include "selinux.h"
2018-09-27 09:56:56 +02:00
#include "flags.h"
2017-04-30 19:58:52 +02:00
2018-08-03 15:30:44 +02:00
static char buf[PATH_MAX], buf2[PATH_MAX];
static Vector<CharArray> module_list;
2017-04-30 19:58:52 +02:00
2018-11-04 09:38:06 +01:00
static int bind_mount(const char *from, const char *to);
2017-08-30 20:20:06 +02:00
2018-11-04 09:38:06 +01:00
/***************
* Magic Mount *
***************/
2017-04-30 19:58:52 +02:00
2017-06-11 21:29:01 +02:00
// Precedence: MODULE > SKEL > INTER > DUMMY
#define IS_DUMMY 0x01 /* mount from mirror */
#define IS_INTER 0x02 /* intermediate node */
#define IS_SKEL 0x04 /* mount from skeleton */
#define IS_MODULE 0x08 /* mount from module */
2017-08-11 05:04:02 +02:00
#define IS_DIR(n) (n->type == DT_DIR)
#define IS_LNK(n) (n->type == DT_LNK)
#define IS_REG(n) (n->type == DT_REG)
2018-11-04 09:38:06 +01:00
class node_entry {
public:
2018-11-07 08:10:38 +01:00
node_entry(const char *, uint8_t status = 0, uint8_t type = 0);
~node_entry();
void create_module_tree(const char *module);
void magic_mount();
node_entry *extract(const char *name);
private:
const char *module; /* Only used when status & IS_MODULE */
2018-11-07 08:10:38 +01:00
const CharArray name;
2017-04-30 19:58:52 +02:00
uint8_t type;
uint8_t status;
2018-11-04 09:38:06 +01:00
node_entry *parent;
Vector<node_entry *> children;
2018-11-04 09:38:06 +01:00
2018-11-07 08:10:38 +01:00
node_entry(const char *, const char *, uint8_t type);
bool is_root();
CharArray get_path();
2018-11-04 09:38:06 +01:00
node_entry *insert(node_entry *);
void clone_skeleton();
int get_path(char *path);
2017-04-30 19:58:52 +02:00
};
2018-11-07 08:10:38 +01:00
node_entry::node_entry(const char *name, uint8_t status, uint8_t type)
: name(name), type(type), status(status), parent(nullptr) {}
2017-08-11 05:04:02 +02:00
2018-11-07 08:10:38 +01:00
node_entry::node_entry(const char *module, const char *name, uint8_t type)
: node_entry(name, (uint8_t) 0, type) {
this->module = module;
2017-08-11 05:04:02 +02:00
}
2018-11-04 09:38:06 +01:00
node_entry::~node_entry() {
for (auto &node : children)
delete node;
2017-08-11 05:04:02 +02:00
}
2018-11-07 08:10:38 +01:00
bool node_entry::is_root() {
return parent == nullptr;
}
CharArray node_entry::get_path() {
2018-11-04 09:38:06 +01:00
get_path(buf);
2018-11-07 08:10:38 +01:00
return buf;
2017-08-30 20:20:06 +02:00
}
2018-11-04 09:38:06 +01:00
int node_entry::get_path(char *path) {
int len = 0;
if (parent)
len = parent->get_path(path);
2018-11-07 08:10:38 +01:00
len += sprintf(path + len, "/%s", name.c_str());
2018-11-04 09:38:06 +01:00
return len;
2018-08-03 11:09:24 +02:00
}
2018-11-04 09:38:06 +01:00
node_entry *node_entry::insert(node_entry *node) {
node->parent = this;
for (auto &child : children) {
2018-11-07 08:10:38 +01:00
if (child->name == node->name) {
2018-11-04 09:38:06 +01:00
if (node->status > child->status) {
// The new node has higher precedence
delete child;
child = node;
return node;
} else {
delete node;
return child;
}
2017-04-30 19:58:52 +02:00
}
}
2018-11-04 09:38:06 +01:00
children.push_back(node);
return node;
2017-04-30 19:58:52 +02:00
}
2018-11-04 09:38:06 +01:00
void node_entry::create_module_tree(const char *module) {
2017-04-30 19:58:52 +02:00
DIR *dir;
struct dirent *entry;
2018-11-07 08:10:38 +01:00
CharArray full_path = get_path();
snprintf(buf, PATH_MAX, "%s/%s%s", MOUNTPOINT, module, full_path.c_str());
2017-04-30 19:58:52 +02:00
2017-10-13 18:08:12 +02:00
if (!(dir = xopendir(buf)))
2018-11-07 08:10:38 +01:00
return;
2017-04-30 19:58:52 +02:00
while ((entry = xreaddir(dir))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// Create new node
2018-11-04 09:38:06 +01:00
node_entry *node = new node_entry(module, entry->d_name, entry->d_type);
2018-11-07 08:10:38 +01:00
snprintf(buf, PATH_MAX, "%s/%s", full_path.c_str(), entry->d_name);
2017-06-11 21:29:01 +02:00
/*
* Clone the parent in the following condition:
* 1. File in module is a symlink
* 2. Target file do not exist
2018-11-04 09:38:06 +01:00
* 3. Target file is a symlink (exclude /system/vendor)
2017-07-30 14:14:12 +02:00
*/
2018-11-04 09:38:06 +01:00
bool clone = false;
if (IS_LNK(node) || access(buf, F_OK) == -1) {
2018-11-04 09:38:06 +01:00
clone = true;
2018-11-07 08:10:38 +01:00
} else if (!is_root() || node->name != "vendor") {
2017-06-11 21:29:01 +02:00
struct stat s;
xstat(buf, &s);
if (S_ISLNK(s.st_mode))
2018-11-04 09:38:06 +01:00
clone = true;
2017-06-11 21:29:01 +02:00
}
if (clone) {
2018-11-04 09:38:06 +01:00
// Mark self as a skeleton
status |= IS_SKEL; /* This will not overwrite if parent is module */
2017-06-11 21:29:01 +02:00
node->status = IS_MODULE;
} else if (IS_DIR(node)) {
// Check if marked as replace
snprintf(buf2, PATH_MAX, "%s/%s%s/.replace", MOUNTPOINT, module, buf);
if (access(buf2, F_OK) == 0) {
// Replace everything, mark as leaf
2017-06-11 21:29:01 +02:00
node->status = IS_MODULE;
} else {
// This will be an intermediate node
node->status = IS_INTER;
2017-04-30 19:58:52 +02:00
}
} else if (IS_REG(node)) {
2018-11-04 09:38:06 +01:00
// This is a file, mark as leaf
2017-06-11 21:29:01 +02:00
node->status = IS_MODULE;
}
2018-11-04 09:38:06 +01:00
node = insert(node);
2017-06-13 18:55:41 +02:00
if (node->status & (IS_SKEL | IS_INTER)) {
// Intermediate folder, travel deeper
2018-11-04 09:38:06 +01:00
node->create_module_tree(module);
2017-04-30 19:58:52 +02:00
}
}
closedir(dir);
}
2018-11-04 09:38:06 +01:00
void node_entry::clone_skeleton() {
2017-04-30 19:58:52 +02:00
DIR *dir;
struct dirent *entry;
2018-11-04 09:38:06 +01:00
struct node_entry *dummy;
2017-04-30 19:58:52 +02:00
// Clone the structure
2018-11-07 08:10:38 +01:00
CharArray full_path = get_path();
snprintf(buf, PATH_MAX, "%s%s", MIRRDIR, full_path.c_str());
2017-10-13 18:08:12 +02:00
if (!(dir = xopendir(buf)))
2018-11-07 08:10:38 +01:00
return;
2017-04-30 19:58:52 +02:00
while ((entry = xreaddir(dir))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// Create dummy node
2018-11-07 08:10:38 +01:00
dummy = new node_entry(entry->d_name, IS_DUMMY, entry->d_type);
2018-11-04 09:38:06 +01:00
insert(dummy);
2017-04-30 19:58:52 +02:00
}
2017-05-01 22:55:55 +02:00
closedir(dir);
2017-04-30 19:58:52 +02:00
2018-11-04 09:38:06 +01:00
if (status & IS_SKEL) {
2018-11-07 08:10:38 +01:00
file_attr attr;
getattr(full_path, &attr);
LOGI("mnt_tmpfs : %s\n", full_path.c_str());
2018-11-04 09:38:06 +01:00
xmount("tmpfs", full_path, "tmpfs", 0, nullptr);
2018-11-07 08:10:38 +01:00
setattr(full_path, &attr);
}
2017-04-30 19:58:52 +02:00
2018-11-04 09:38:06 +01:00
for (auto &child : children) {
2018-11-07 08:10:38 +01:00
snprintf(buf, PATH_MAX, "%s/%s", full_path.c_str(), child->name.c_str());
2017-06-11 21:29:01 +02:00
// Create the dummy file/directory
if (IS_DIR(child))
2017-06-30 17:22:51 +02:00
xmkdir(buf, 0755);
else if (IS_REG(child))
2017-11-09 18:51:41 +01:00
close(creat(buf, 0644));
2017-06-11 21:29:01 +02:00
// Links will be handled later
2018-11-07 08:10:38 +01:00
if (is_root() && child->name == "vendor") {
if (seperate_vendor) {
2017-06-11 21:29:01 +02:00
cp_afc(MIRRDIR "/system/vendor", "/system/vendor");
2018-11-07 08:10:38 +01:00
LOGI("copy_link : %s <- %s\n", "/system/vendor", MIRRDIR "/system/vendor");
2017-06-11 21:29:01 +02:00
}
// Skip
continue;
} else if (child->status & IS_MODULE) {
2017-04-30 19:58:52 +02:00
// Mount from module file to dummy file
2018-11-07 08:10:38 +01:00
snprintf(buf2, PATH_MAX, "%s/%s%s/%s", MOUNTPOINT,
child->module, full_path.c_str(), child->name.c_str());
2017-06-11 21:29:01 +02:00
} else if (child->status & (IS_SKEL | IS_INTER)) {
// It's an intermediate folder, recursive clone
2018-11-04 09:38:06 +01:00
child->clone_skeleton();
2017-04-30 19:58:52 +02:00
continue;
} else if (child->status & IS_DUMMY) {
// Mount from mirror to dummy file
2018-11-07 08:10:38 +01:00
snprintf(buf2, PATH_MAX, "%s%s/%s", MIRRDIR, full_path.c_str(), child->name.c_str());
2017-04-30 19:58:52 +02:00
}
if (IS_LNK(child)) {
2017-06-11 21:29:01 +02:00
// Copy symlinks directly
cp_afc(buf2, buf);
2018-06-03 08:43:03 +02:00
#ifdef MAGISK_DEBUG
2018-11-07 08:10:38 +01:00
LOGI("copy_link : %s <- %s\n",buf, buf2);
2018-06-03 08:43:03 +02:00
#else
2018-11-07 08:10:38 +01:00
LOGI("copy_link : %s\n", buf);
2018-06-03 08:43:03 +02:00
#endif
2017-04-30 19:58:52 +02:00
} else {
2018-11-07 08:10:38 +01:00
snprintf(buf, PATH_MAX, "%s/%s", full_path.c_str(), child->name.c_str());
2017-04-30 19:58:52 +02:00
bind_mount(buf2, buf);
}
}
}
2018-11-04 09:38:06 +01:00
void node_entry::magic_mount() {
if (status & IS_MODULE) {
// Mount module item
2018-11-07 08:10:38 +01:00
CharArray real_path = get_path();
snprintf(buf, PATH_MAX, "%s/%s%s", MOUNTPOINT, module, real_path.c_str());
2017-06-11 21:29:01 +02:00
bind_mount(buf, real_path);
2018-11-04 09:38:06 +01:00
} else if (status & IS_SKEL) {
2017-06-11 21:29:01 +02:00
// The node is labeled to be cloned with skeleton, lets do it
2018-11-04 09:38:06 +01:00
clone_skeleton();
} else if (status & IS_INTER) {
// It's an intermediate node, travel deeper
2018-11-04 09:38:06 +01:00
for (auto &child : children)
child->magic_mount();
2017-04-30 19:58:52 +02:00
}
2017-06-11 21:29:01 +02:00
// The only thing goes here should be vendor placeholder
// There should be no dummies, so don't need to handle it here
2017-04-30 19:58:52 +02:00
}
2018-11-07 08:10:38 +01:00
node_entry *node_entry::extract(const char *name) {
node_entry *node = nullptr;
// Extract the vendor node out of system tree and swap with placeholder
for (auto &child : children) {
if (child->name == name) {
node = child;
child = new node_entry(name);
child->parent = node->parent;
node->parent = nullptr;
break;
}
}
return node;
}
2018-11-04 09:38:06 +01:00
/***********
* setenvs *
***********/
static void set_path() {
char buffer[512];
sprintf(buffer, BBPATH ":%s", getenv("PATH"));
setenv("PATH", buffer, 1);
}
static void set_mirror_path() {
setenv("PATH", BBPATH ":/sbin:" MIRRDIR "/system/bin:"
MIRRDIR "/system/xbin:" MIRRDIR "/vendor/bin", 1);
}
/***********
* Scripts *
***********/
static void exec_common_script(const char* stage) {
DIR *dir;
struct dirent *entry;
snprintf(buf2, PATH_MAX, "%s/%s.d", COREDIR, stage);
if (!(dir = xopendir(buf2)))
return;
while ((entry = xreaddir(dir))) {
if (entry->d_type == DT_REG) {
snprintf(buf2, PATH_MAX, "%s/%s.d/%s", COREDIR, stage, entry->d_name);
if (access(buf2, X_OK) == -1)
continue;
LOGI("%s.d: exec [%s]\n", stage, entry->d_name);
int pid = exec_command(
0, nullptr,
strcmp(stage, "post-fs-data") ? set_path : set_mirror_path,
"sh", buf2, nullptr);
if (pid != -1)
waitpid(pid, nullptr, 0);
}
}
closedir(dir);
}
static void exec_module_script(const char* stage) {
2018-11-07 08:10:38 +01:00
for (const char *module : module_list) {
2018-11-04 09:38:06 +01:00
snprintf(buf2, PATH_MAX, "%s/%s/%s.sh", MOUNTPOINT, module, stage);
snprintf(buf, PATH_MAX, "%s/%s/disable", MOUNTPOINT, module);
if (access(buf2, F_OK) == -1 || access(buf, F_OK) == 0)
continue;
LOGI("%s: exec [%s.sh]\n", module, stage);
int pid = exec_command(
0, nullptr,
strcmp(stage, "post-fs-data") ? set_path : set_mirror_path,
"sh", buf2, nullptr);
if (pid != -1)
waitpid(pid, nullptr, 0);
}
}
2017-05-03 20:39:53 +02:00
/****************
* Simple Mount *
****************/
static void simple_mount(const char *path) {
DIR *dir;
struct dirent *entry;
snprintf(buf, PATH_MAX, "%s%s", SIMPLEMOUNT, path);
2017-05-03 20:39:53 +02:00
if (!(dir = opendir(buf)))
return;
while ((entry = xreaddir(dir))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// Target file path
snprintf(buf2, PATH_MAX, "%s/%s", path, entry->d_name);
// Only mount existing file
if (access(buf2, F_OK) == -1)
continue;
if (entry->d_type == DT_DIR) {
2018-11-08 21:23:36 +01:00
simple_mount(CharArray(buf2));
2017-05-03 20:39:53 +02:00
} else if (entry->d_type == DT_REG) {
// Actual file path
snprintf(buf, PATH_MAX, "%s%s", SIMPLEMOUNT, buf2);
2017-05-03 20:39:53 +02:00
// Clone all attributes
2017-05-04 22:16:00 +02:00
clone_attr(buf2, buf);
2017-05-03 20:39:53 +02:00
// Finally, mount the file
bind_mount(buf, buf2);
}
}
closedir(dir);
}
2017-08-11 05:43:45 +02:00
/*****************
* Miscellaneous *
*****************/
2018-11-04 09:38:06 +01:00
static int bind_mount(const char *from, const char *to) {
int ret = xmount(from, to, nullptr, MS_BIND, nullptr);
#ifdef MAGISK_DEBUG
LOGI("bind_mount: %s <- %s\n", to, from);
#else
LOGI("bind_mount: %s\n", to);
#endif
return ret;
}
#define alt_img ((const char *[]) \
{ "/cache/magisk.img", "/data/magisk_merge.img", "/data/adb/magisk_merge.img", nullptr })
2017-12-25 20:23:58 +01:00
2017-08-12 19:15:18 +02:00
static int prepare_img() {
2017-12-25 20:23:58 +01:00
// Merge images
for (int i = 0; alt_img[i]; ++i) {
if (merge_img(alt_img[i], MAINIMG)) {
LOGE("Image merge %s -> " MAINIMG " failed!\n", alt_img[i]);
return 1;
}
2017-10-11 21:39:39 +02:00
}
2017-08-12 19:15:18 +02:00
if (access(MAINIMG, F_OK) == -1) {
if (create_img(MAINIMG, 64))
return 1;
}
LOGI("* Mounting " MAINIMG "\n");
// Mounting magisk image
char *magiskloop = mount_image(MAINIMG, MOUNTPOINT);
2018-11-04 09:38:06 +01:00
if (magiskloop == nullptr)
2017-08-12 19:15:18 +02:00
return 1;
2017-09-27 23:50:06 +02:00
xmkdir(COREDIR, 0755);
xmkdir(COREDIR "/post-fs-data.d", 0755);
xmkdir(COREDIR "/service.d", 0755);
xmkdir(COREDIR "/props", 0755);
DIR *dir = xopendir(MOUNTPOINT);
struct dirent *entry;
while ((entry = xreaddir(dir))) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0 ||
strcmp(entry->d_name, ".core") == 0 ||
strcmp(entry->d_name, "lost+found") == 0)
continue;
snprintf(buf, PATH_MAX, "%s/%s/remove", MOUNTPOINT, entry->d_name);
if (access(buf, F_OK) == 0) {
snprintf(buf, PATH_MAX, "%s/%s", MOUNTPOINT, entry->d_name);
2017-10-11 20:57:18 +02:00
rm_rf(buf);
2017-09-27 23:50:06 +02:00
continue;
2017-08-12 19:15:18 +02:00
}
snprintf(buf, PATH_MAX, "%s/%s/update", MOUNTPOINT, entry->d_name);
unlink(buf);
2017-09-27 23:50:06 +02:00
snprintf(buf, PATH_MAX, "%s/%s/disable", MOUNTPOINT, entry->d_name);
if (access(buf, F_OK) == 0)
continue;
2018-11-07 08:10:38 +01:00
module_list.push_back(entry->d_name);
2017-08-12 19:15:18 +02:00
}
2017-09-27 23:50:06 +02:00
}
closedir(dir);
2017-08-12 19:15:18 +02:00
if (trim_img(MAINIMG, MOUNTPOINT, magiskloop))
return 1;
2018-07-06 16:04:06 +02:00
free(magiskloop);
2017-08-12 19:15:18 +02:00
return 0;
}
2018-10-13 03:46:09 +02:00
static void install_apk(const char *apk) {
2018-11-04 09:38:06 +01:00
setfilecon(apk, "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
2018-06-13 23:09:54 +02:00
while (1) {
sleep(5);
LOGD("apk_install: attempting to install APK");
int apk_res = -1, pid;
2018-11-04 09:38:06 +01:00
pid = exec_command(1, &apk_res, nullptr, "/system/bin/pm", "install", "-r", apk, nullptr);
2018-06-13 23:09:54 +02:00
if (pid != -1) {
int err = 0;
while (fdgets(buf, PATH_MAX, apk_res) > 0) {
LOGD("apk_install: %s", buf);
2018-11-04 09:38:06 +01:00
err |= strstr(buf, "Error:") != nullptr;
2018-06-13 23:09:54 +02:00
}
2018-11-04 09:38:06 +01:00
waitpid(pid, nullptr, 0);
2018-06-13 23:09:54 +02:00
close(apk_res);
// Keep trying until pm is started
if (err)
continue;
break;
}
}
unlink(apk);
}
2018-11-04 09:38:06 +01:00
static bool check_data() {
bool mnt = false;
bool data = false;
Vector<CharArray> mounts;
file_to_vector("/proc/mounts", mounts);
2018-11-04 09:38:06 +01:00
for (auto &line : mounts) {
2018-11-07 08:10:38 +01:00
if (line.contains(" /data ") && !line.contains("tmpfs"))
2018-11-04 09:38:06 +01:00
mnt = true;
2018-10-13 03:46:09 +02:00
}
if (mnt) {
2018-11-07 08:10:38 +01:00
CharArray crypto = getprop("ro.crypto.state");
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;
}
extern int launch_magiskhide();
2018-11-04 09:38:06 +01:00
static void *start_magisk_hide(void *) {
launch_magiskhide();
2018-11-04 09:38:06 +01:00
return nullptr;
2018-08-02 19:58:56 +02:00
}
static void auto_start_magiskhide() {
2018-10-12 06:50:47 +02:00
if (!start_log_daemon())
2018-08-02 19:58:56 +02:00
return;
2018-11-07 08:10:38 +01:00
CharArray hide_prop = getprop(MAGISKHIDE_PROP, true);
if (hide_prop != "0") {
2018-08-02 19:58:56 +02:00
pthread_t thread;
2018-11-04 09:38:06 +01:00
xpthread_create(&thread, nullptr, start_magisk_hide, nullptr);
2018-08-02 19:58:56 +02:00
pthread_detach(thread);
}
}
2018-10-13 03:46:09 +02:00
void unlock_blocks() {
DIR *dir;
struct dirent *entry;
int fd, dev, OFF = 0;
if ((dev = xopen("/dev/block", O_RDONLY | O_CLOEXEC)) < 0)
return;
dir = xfdopendir(dev);
while((entry = readdir(dir))) {
if (entry->d_type == DT_BLK) {
if ((fd = openat(dev, entry->d_name, O_RDONLY)) < 0)
continue;
if (ioctl(fd, BLKROSET, &OFF) == -1)
PLOGE("unlock %s", entry->d_name);
close(fd);
}
}
close(dev);
}
2017-04-30 19:58:52 +02:00
/****************
* Entry points *
****************/
2017-05-03 19:13:04 +02:00
static void unblock_boot_process() {
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
}
static const char wrapper[] =
"#!/system/bin/sh\n"
"unset LD_LIBRARY_PATH\n"
"unset LD_PRELOAD\n"
"exec /sbin/magisk.bin \"${0##*/}\" \"$@\"\n";
void startup() {
android_logging();
if (!check_data())
unblock_boot_process();
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. */
LOGE(SECURE_DIR" is not present, abort...");
unblock_boot_process();
}
2017-04-30 19:58:52 +02:00
#if 0
// Increment boot count
int boot_count = 0;
2018-08-09 09:10:00 +02:00
FILE *cf = fopen(BOOTCOUNT, "r");
if (cf) {
fscanf(cf, "%d", &boot_count);
fclose(cf);
}
boot_count++;
if (boot_count > 2)
creat(DISABLEFILE, 0644);
cf = xfopen(BOOTCOUNT, "w");
fprintf(cf, "%d", boot_count);
fclose(cf);
#endif
// No uninstaller or core-only mode
2018-06-27 00:04:16 +02:00
if (access(DISABLEFILE, F_OK) != 0) {
simple_mount("/system");
simple_mount("/vendor");
}
2017-05-03 20:39:53 +02:00
LOGI("** Initializing Magisk\n");
// Unlock all blocks for rw
unlock_blocks();
LOGI("* Creating /sbin overlay");
DIR *dir;
struct dirent *entry;
int root, sbin, fd;
void *magisk, *init;
size_t magisk_size, init_size;
2018-11-04 09:38:06 +01:00
xmount(nullptr, "/", nullptr, MS_REMOUNT, nullptr);
2018-04-29 06:34:36 +02:00
// Remove some traits of Magisk
2018-08-31 09:51:30 +02:00
unlink(MAGISKRC);
2018-04-29 06:34:36 +02:00
2018-06-16 19:25:27 +02:00
// GSIs will have to override /sbin/adbd with /system/bin/adbd
if (access("/sbin/adbd", F_OK) == 0 && access("/system/bin/adbd", F_OK) == 0) {
umount2("/sbin/adbd", MNT_DETACH);
cp_afc("/system/bin/adbd", "/sbin/adbd");
}
2018-04-29 06:34:36 +02:00
// Create hardlink mirror of /sbin to /root
mkdir("/root", 0750);
2018-06-16 19:25:27 +02:00
clone_attr("/sbin", "/root");
full_read("/sbin/magisk", &magisk, &magisk_size);
unlink("/sbin/magisk");
full_read("/sbin/magiskinit", &init, &init_size);
unlink("/sbin/magiskinit");
root = xopen("/root", O_RDONLY | O_CLOEXEC);
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
link_dir(sbin, root);
close(sbin);
// Mount the /sbin tmpfs overlay
2018-11-04 09:38:06 +01:00
xmount("tmpfs", "/sbin", "tmpfs", 0, nullptr);
chmod("/sbin", 0755);
setfilecon("/sbin", "u:object_r:rootfs:s0");
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
2018-09-28 00:26:41 +02:00
// Create applet symlinks
for (int i = 0; applet_names[i]; ++i) {
snprintf(buf, PATH_MAX, "/sbin/%s", applet_names[i]);
xsymlink("/sbin/magisk", buf);
}
// Setup binary and wrapper
fd = creat("/sbin/magisk.bin", 0755);
xwrite(fd, magisk, magisk_size);
close(fd);
free(magisk);
2018-09-28 00:26:41 +02:00
unlink("/sbin/magisk");
fd = creat("/sbin/magisk", 0755);
xwrite(fd, wrapper, sizeof(wrapper) - 1);
close(fd);
2018-11-04 09:38:06 +01:00
setfilecon("/sbin/magisk.bin", "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
setfilecon("/sbin/magisk", "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
// Setup magiskinit symlinks
fd = creat("/sbin/magiskinit", 0755);
xwrite(fd, init, init_size);
close(fd);
free(init);
2018-11-04 09:38:06 +01:00
setfilecon("/sbin/magiskinit", "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
for (int i = 0; init_applet[i]; ++i) {
snprintf(buf, PATH_MAX, "/sbin/%s", init_applet[i]);
xsymlink("/sbin/magiskinit", buf);
}
// Create symlinks pointing back to /root
dir = xfdopendir(root);
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, sbin, entry->d_name);
}
close(sbin);
close(root);
// Alternative binaries paths
2018-11-04 09:38:06 +01:00
const char *alt_bin[] = { "/cache/data_bin", "/data/magisk",
"/data/data/com.topjohnwu.magisk/install",
2018-11-04 09:38:06 +01:00
"/data/user_de/0/com.topjohnwu.magisk/install", nullptr };
const char *bin_path = nullptr;
for (int i = 0; alt_bin[i]; ++i) {
2018-09-01 17:15:05 +02:00
struct stat st;
if (lstat(alt_bin[i], &st) != -1 && !S_ISLNK(st.st_mode)) {
rm_rf(DATABIN);
cp_afc(bin_path, DATABIN);
bin_path = alt_bin[i];
break;
}
}
if (bin_path) {
rm_rf(DATABIN);
cp_afc(bin_path, DATABIN);
rm_rf(bin_path);
}
// Remove legacy stuffs
rm_rf("/data/magisk");
unlink("/data/magisk.img");
unlink("/data/magisk_debug.log");
2018-05-26 15:25:59 +02:00
// Create directories in tmpfs overlay
xmkdirs(MIRRDIR "/system", 0755);
xmkdir(MIRRDIR "/bin", 0755);
xmkdir(BBPATH, 0755);
xmkdir(MOUNTPOINT, 0755);
2018-07-06 19:32:58 +02:00
xmkdir(BLOCKDIR, 0755);
2018-05-26 15:25:59 +02:00
LOGI("* Mounting mirrors");
Vector<CharArray> mounts;
file_to_vector("/proc/mounts", mounts);
2018-11-07 08:10:38 +01:00
bool system_as_root = false;
2018-11-04 09:38:06 +01:00
for (auto &line : mounts) {
2018-11-07 08:10:38 +01:00
if (line.contains(" /system_root ")) {
bind_mount("/system_root/system", MIRRDIR "/system");
2018-11-07 08:10:38 +01:00
system_as_root = true;
} else if (!system_as_root && line.contains(" /system ")) {
sscanf(line, "%s %*s %s", buf, buf2);
2018-11-04 09:38:06 +01:00
xmount(buf, MIRRDIR "/system", buf2, MS_RDONLY, nullptr);
#ifdef MAGISK_DEBUG
LOGI("mount: %s <- %s\n", MIRRDIR "/system", buf);
#else
LOGI("mount: %s\n", MIRRDIR "/system");
#endif
2018-11-07 08:10:38 +01:00
} else if (line.contains(" /vendor ")) {
seperate_vendor = 1;
sscanf(line, "%s %*s %s", buf, buf2);
2018-05-26 15:25:59 +02:00
xmkdir(MIRRDIR "/vendor", 0755);
2018-11-04 09:38:06 +01:00
xmount(buf, MIRRDIR "/vendor", buf2, MS_RDONLY, nullptr);
#ifdef MAGISK_DEBUG
LOGI("mount: %s <- %s\n", MIRRDIR "/vendor", buf);
#else
LOGI("mount: %s\n", MIRRDIR "/vendor");
#endif
}
}
if (!seperate_vendor) {
xsymlink(MIRRDIR "/system/vendor", MIRRDIR "/vendor");
#ifdef MAGISK_DEBUG
LOGI("link: %s <- %s\n", MIRRDIR "/vendor", MIRRDIR "/system/vendor");
#else
LOGI("link: %s\n", MIRRDIR "/vendor");
#endif
}
2018-05-13 12:14:44 +02:00
xmkdirs(DATABIN, 0755);
bind_mount(DATABIN, MIRRDIR "/bin");
2018-05-13 12:14:44 +02:00
if (access(MIRRDIR "/bin/busybox", X_OK) == 0) {
LOGI("* Setting up internal busybox");
2018-11-04 09:38:06 +01:00
exec_command_sync(MIRRDIR "/bin/busybox", "--install", "-s", BBPATH, nullptr);
2018-05-13 12:14:44 +02:00
xsymlink(MIRRDIR "/bin/busybox", BBPATH "/busybox");
}
// Start post-fs-data mode
2018-11-04 09:38:06 +01:00
execl("/sbin/magisk.bin", "magisk", "--post-fs-data", nullptr);
}
static void core_only() {
// Systemless hosts
if (access(HOSTSFILE, F_OK) == 0) {
LOGI("* Enabling systemless hosts file support");
bind_mount(HOSTSFILE, "/system/etc/hosts");
}
auto_start_magiskhide();
unblock_boot_process();
2017-04-30 19:58:52 +02:00
}
void post_fs_data(int client) {
// ack
write_int(client, 0);
close(client);
// If post-fs-data mode is started, it means startup succeeded
setup_done = 1;
2018-11-04 09:38:06 +01:00
xmount(nullptr, "/", nullptr, MS_REMOUNT | MS_RDONLY, nullptr);
2018-10-12 06:50:47 +02:00
// Start log_daemon
start_log_daemon();
2017-05-03 19:13:04 +02:00
LOGI("** post-fs-data mode running\n");
2017-10-11 21:39:39 +02:00
// Merge, trim, mount magisk.img, which will also travel through the modules
2017-08-12 19:15:18 +02:00
// After this, it will create the module list
2018-11-04 09:38:06 +01:00
if (prepare_img()) {
// Mounting fails, we can only do core only stuffs
core_only();
return;
}
2017-04-30 19:58:52 +02:00
2018-06-03 08:43:03 +02:00
restorecon();
chmod(SECURE_DIR, 0700);
2018-06-03 08:43:03 +02:00
2017-08-12 19:15:18 +02:00
// Run common scripts
LOGI("* Running post-fs-data.d scripts\n");
exec_common_script("post-fs-data");
2017-06-02 23:52:49 +02:00
2017-05-03 19:13:04 +02:00
// Core only mode
2018-11-04 09:38:06 +01:00
if (access(DISABLEFILE, F_OK) == 0) {
core_only();
return;
}
2017-05-03 19:13:04 +02:00
2017-08-12 19:15:18 +02:00
// Execute module scripts
LOGI("* Running module post-fs-data scripts\n");
exec_module_script("post-fs-data");
2017-04-30 19:58:52 +02:00
// Create the system root entry
2018-11-07 08:10:38 +01:00
node_entry *sys_root = new node_entry("system", IS_INTER);
2017-04-30 19:58:52 +02:00
2018-11-04 09:38:06 +01:00
// Vendor root entry
node_entry *ven_root = nullptr;
bool has_modules = false;
2017-04-30 19:58:52 +02:00
LOGI("* Loading modules\n");
2018-11-07 08:10:38 +01:00
for (const char *module : module_list) {
2017-08-12 19:15:18 +02:00
// Read props
snprintf(buf, PATH_MAX, "%s/%s/system.prop", MOUNTPOINT, module);
if (access(buf, F_OK) == 0) {
LOGI("%s: loading [system.prop]\n", module);
2018-11-04 09:38:06 +01:00
load_prop_file(buf, 0);
2017-08-12 19:15:18 +02:00
}
// Check whether enable auto_mount
snprintf(buf, PATH_MAX, "%s/%s/auto_mount", MOUNTPOINT, module);
if (access(buf, F_OK) == -1)
continue;
// Double check whether the system folder exists
snprintf(buf, PATH_MAX, "%s/%s/system", MOUNTPOINT, module);
if (access(buf, F_OK) == -1)
continue;
2017-05-03 19:13:04 +02:00
2017-08-12 19:15:18 +02:00
// Construct structure
2018-11-04 09:38:06 +01:00
has_modules = true;
2017-08-12 19:15:18 +02:00
LOGI("%s: constructing magic mount structure\n", module);
// If /system/vendor exists in module, create a link outside
snprintf(buf, PATH_MAX, "%s/%s/system/vendor", MOUNTPOINT, module);
if (access(buf, F_OK) == 0) {
snprintf(buf2, PATH_MAX, "%s/%s/vendor", MOUNTPOINT, module);
unlink(buf2);
2017-10-13 18:08:12 +02:00
xsymlink(buf, buf2);
2017-04-30 19:58:52 +02:00
}
2018-11-04 09:38:06 +01:00
sys_root->create_module_tree(module);
2017-04-30 19:58:52 +02:00
}
if (has_modules) {
2018-11-07 08:10:38 +01:00
// Pull out /system/vendor node if exist
ven_root = sys_root->extract("vendor");
2017-06-11 21:29:01 +02:00
// Magic!!
2018-11-04 09:38:06 +01:00
sys_root->magic_mount();
if (ven_root) ven_root->magic_mount();
2017-04-30 19:58:52 +02:00
}
// Cleanup memory
2018-11-04 09:38:06 +01:00
delete sys_root;
2018-11-07 08:10:38 +01:00
delete ven_root;
2017-05-03 21:05:37 +02:00
2018-11-04 09:38:06 +01:00
core_only();
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);
if (access(SECURE_DIR, F_OK) != 0) {
// It's safe to create the folder at this point if the system didn't create it
xmkdir(SECURE_DIR, 0700);
}
if (!setup_done) {
// The setup failed for some reason, reboot and try again
2018-11-04 09:38:06 +01:00
exec_command_sync("/system/bin/reboot", nullptr);
return;
}
2018-08-02 19:58:56 +02:00
auto_start_magiskhide();
2017-08-12 19:15:18 +02:00
// Run scripts after full patch, most reliable way to run scripts
LOGI("* Running service.d scripts\n");
exec_common_script("service");
2017-06-11 10:51:44 +02:00
// Core only mode
2017-07-31 17:31:40 +02:00
if (access(DISABLEFILE, F_OK) == 0)
goto core_only;
2017-06-11 10:51:44 +02:00
2017-04-30 19:58:52 +02:00
LOGI("* Running module service scripts\n");
exec_module_script("service");
2017-07-31 17:31:40 +02:00
core_only:
if (access(MANAGERAPK, F_OK) == 0) {
2018-06-13 23:09:54 +02:00
// Install Magisk Manager if exists
rename(MANAGERAPK, "/data/magisk.apk");
2018-06-13 23:09:54 +02:00
install_apk("/data/magisk.apk");
} else {
// Check whether we have a valid manager installed
sqlite3 *db = get_magiskdb();
if (db) {
2018-11-05 00:24:08 +01:00
db_strings str;
get_db_strings(db, &str, SU_MANAGER);
if (validate_manager(str[SU_MANAGER], 0, nullptr)) {
// There is no manager installed, install the stub
2018-11-04 09:38:06 +01:00
exec_command_sync("/sbin/magiskinit", "-x", "manager", "/data/magisk.apk", nullptr);
install_apk("/data/magisk.apk");
}
sqlite3_close_v2(db);
}
}
2018-08-03 15:30:44 +02:00
// All boot stage done, cleanup
2018-11-07 08:10:38 +01:00
module_list.clear(true);
2018-08-09 08:52:44 +02:00
}
2018-08-09 08:52:44 +02:00
void boot_complete(int client) {
LOGI("** boot_complete triggered\n");
// ack
write_int(client, 0);
close(client);
unlink(BOOTCOUNT);
2017-04-30 19:58:52 +02:00
}