Magisk/native/jni/core/bootstages.cpp

787 lines
20 KiB
C++
Raw Normal View History

#include <sys/mount.h>
#include <sys/wait.h>
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>
2020-04-12 14:34:56 +02:00
#include <libgen.h>
2019-01-20 05:59:37 +01:00
#include <vector>
#include <string>
2017-04-30 19:58:52 +02:00
2020-03-09 09:50:30 +01:00
#include <magisk.hpp>
#include <db.hpp>
#include <utils.hpp>
#include <daemon.hpp>
#include <resetprop.hpp>
#include <selinux.hpp>
#include <flags.h>
2017-04-30 19:58:52 +02:00
2019-01-20 05:59:37 +01:00
using namespace std;
static vector<string> module_list;
2019-03-22 06:50:33 +01:00
static bool no_secure_dir = false;
static bool pfs_done = false;
2017-04-30 19:58:52 +02:00
static int bind_mount(const char *from, const char *to, bool log = true);
2018-11-16 07:15:34 +01:00
extern void auto_start_magiskhide();
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
2019-01-26 08:41:25 +01:00
#ifdef MAGISK_DEBUG
#define VLOGI(tag, from, to) LOGI("%s: %s <- %s\n", tag, to, from)
#else
#define VLOGI(tag, from, to) LOGI("%s: %s\n", tag, to)
#endif
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 /* replace with tmpfs */
2017-06-11 21:29:01 +02:00
#define IS_MODULE 0x08 /* mount from module */
2019-01-20 05:59:37 +01: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)
2017-08-11 05:04:02 +02:00
2018-11-04 09:38:06 +01:00
class node_entry {
public:
explicit node_entry(const char *name, uint8_t type = DT_DIR, uint8_t status = IS_INTER)
: module(nullptr), name(name), type(type), status(status), parent(nullptr) {}
2018-11-07 08:10:38 +01:00
~node_entry();
void create_module_tree(const char *module);
void magic_mount();
node_entry *extract(const char *name);
static bool vendor_root;
static bool product_root;
2018-11-07 08:10:38 +01:00
private:
const char *module; /* Only used when IS_MODULE */
2019-01-20 05:59:37 +01:00
const string 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;
2019-01-20 05:59:37 +01:00
vector<node_entry *> children;
2018-11-04 09:38:06 +01:00
node_entry(node_entry *parent, const char *module, const char *name, uint8_t type)
: module(module), name(name), type(type), status(0), parent(parent) {}
bool is_special();
bool is_root();
2019-01-20 05:59:37 +01:00
string get_path();
void insert(node_entry *&);
2018-11-04 09:38:06 +01:00
void clone_skeleton();
2017-04-30 19:58:52 +02:00
};
bool node_entry::vendor_root = false;
bool node_entry::product_root = false;
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
}
#define SPECIAL_NODE (parent->parent ? false : \
((vendor_root && name == "vendor") || (product_root && name == "product")))
2019-06-29 23:19:10 +02:00
bool node_entry::is_special() {
2019-06-29 23:19:10 +02:00
return parent ? SPECIAL_NODE : false;
2018-11-07 08:10:38 +01:00
}
bool node_entry::is_root() {
2019-06-29 23:19:10 +02:00
return parent ? SPECIAL_NODE : true;
}
2019-01-20 05:59:37 +01:00
string node_entry::get_path() {
2020-04-12 14:34:56 +02:00
string path;
2018-11-04 09:38:06 +01:00
if (parent)
2020-04-12 14:34:56 +02:00
path = parent->get_path();
path += "/";
path += name;
return path;
2018-08-03 11:09:24 +02:00
}
void node_entry::insert(node_entry *&node) {
2018-11-04 09:38:06 +01:00
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;
} else {
delete node;
node = child;
2018-11-04 09:38:06 +01:00
}
return;
2017-04-30 19:58:52 +02:00
}
}
2018-11-04 09:38:06 +01:00
children.push_back(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) {
2019-01-20 05:59:37 +01:00
auto full_path = get_path();
2020-04-12 14:34:56 +02:00
auto cwd = MODULEROOT + "/"s + module + full_path;
2019-08-31 15:53:47 +02:00
2020-04-12 14:34:56 +02:00
auto dir = xopen_dir(cwd.data());
2019-08-31 15:53:47 +02:00
if (!dir)
2018-11-07 08:10:38 +01:00
return;
2017-04-30 19:58:52 +02:00
2019-08-31 15:53:47 +02:00
// Check directory replace
if (faccessat(dirfd(dir.get()), ".replace", F_OK, 0) == 0) {
if (is_root()) {
// Root nodes should not be replaced
2020-04-12 14:34:56 +02:00
rm_rf(cwd.data());
2019-09-12 22:08:30 +02:00
} else if (status < IS_MODULE) {
// Upgrade current node to current module
this->module = module;
status = IS_MODULE;
2019-08-31 15:53:47 +02:00
}
return;
}
2019-12-13 06:37:06 +01:00
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
2017-04-30 19:58:52 +02:00
continue;
// Create new node
auto node = new node_entry(this, module, entry->d_name, entry->d_type);
2020-04-12 14:34:56 +02:00
auto dest = full_path + "/" + entry->d_name;
auto src = cwd + "/" + entry->d_name;
2017-06-11 21:29:01 +02:00
/*
* Clone current directory in one of the following conditions:
* - Target does not exist
* - Module file is a symlink
* - Target file is a symlink (exclude special nodes)
2017-07-30 14:14:12 +02:00
*/
2018-11-04 09:38:06 +01:00
bool clone = false;
2020-04-12 14:34:56 +02:00
if (IS_LNK(node) || access(dest.data(), F_OK) != 0) {
2018-11-04 09:38:06 +01:00
clone = true;
} else if (!node->is_special()) {
2017-06-11 21:29:01 +02:00
struct stat s;
2020-04-12 14:34:56 +02:00
xlstat(dest.data(), &s);
2017-06-11 21:29:01 +02:00
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 && is_root()) {
2019-08-31 15:53:47 +02:00
// Root nodes should not be cloned
2020-04-12 14:34:56 +02:00
rm_rf(src.data());
delete node;
continue;
}
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 (node->is_special()) {
// Special nodes will be pulled out as root nodes later
node->status = IS_INTER;
} else {
// Clone attributes from real path
2020-04-12 14:34:56 +02:00
clone_attr(dest.data(), src.data());
if (IS_DIR(node)) {
2019-08-31 15:53:47 +02:00
// First mark as an intermediate node
node->status = IS_INTER;
} else if (IS_REG(node)) {
// This is a file, mark as leaf
2017-06-11 21:29:01 +02:00
node->status = IS_MODULE;
2017-04-30 19:58:52 +02:00
}
}
insert(node);
if (IS_DIR(node)) {
// Recursive traverse through everything
2018-11-04 09:38:06 +01:00
node->create_module_tree(module);
2017-04-30 19:58:52 +02:00
}
}
}
2018-11-04 09:38:06 +01:00
void node_entry::clone_skeleton() {
2017-04-30 19:58:52 +02:00
// Clone the structure
2019-01-20 05:59:37 +01:00
auto full_path = get_path();
2020-04-12 14:34:56 +02:00
auto mirror_path = MAGISKTMP + "/" MIRRDIR + full_path;
if (auto dir = xopen_dir(mirror_path.data()); dir) {
2019-12-13 06:37:06 +01:00
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
// Create dummy node
auto dummy = new node_entry(entry->d_name, entry->d_type, IS_DUMMY);
insert(dummy);
}
} else { return; }
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;
2020-04-12 14:34:56 +02:00
getattr(full_path.data(), &attr);
LOGI("mnt_tmpfs : %s\n", full_path.data());
xmount("tmpfs", full_path.data(), "tmpfs", 0, nullptr);
setattr(full_path.data(), &attr);
}
2017-04-30 19:58:52 +02:00
2018-11-04 09:38:06 +01:00
for (auto &child : children) {
2020-04-12 14:34:56 +02:00
auto dest = full_path + "/" + child->name;
string src;
2017-06-11 21:29:01 +02:00
// Create the dummy file/directory
if (IS_DIR(child))
2020-04-12 14:34:56 +02:00
xmkdir(dest.data(), 0755);
else if (IS_REG(child))
2020-04-12 14:34:56 +02:00
close(creat(dest.data(), 0644));
2017-06-11 21:29:01 +02:00
// Links will be handled later
if (child->status & IS_MODULE) {
2017-04-30 19:58:52 +02:00
// Mount from module file to dummy file
2020-04-12 14:34:56 +02:00
src = MAGISKTMP + "/" MODULEMNT "/" + child->module + full_path + "/" + child->name;
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
2020-04-12 14:34:56 +02:00
src = MAGISKTMP + "/" MIRRDIR + full_path + "/" + child->name;
2017-04-30 19:58:52 +02:00
}
if (IS_LNK(child)) {
2017-06-11 21:29:01 +02:00
// Copy symlinks directly
2020-04-12 14:34:56 +02:00
cp_afc(src.data(), dest.data());
VLOGI("copy_link ", src.data(), dest.data());
2017-04-30 19:58:52 +02:00
} else {
2020-04-12 14:34:56 +02:00
bind_mount(src.data(), dest.data());
2017-04-30 19:58:52 +02:00
}
}
}
2018-11-04 09:38:06 +01:00
void node_entry::magic_mount() {
if (status & IS_MODULE) {
// Mount module item
2020-04-12 14:34:56 +02:00
auto dest = get_path();
auto src = MAGISKTMP + "/" MODULEMNT "/" + module + dest;
bind_mount(src.data(), dest.data());
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
}
}
2018-11-07 08:10:38 +01:00
node_entry *node_entry::extract(const char *name) {
node_entry *node = nullptr;
// Extract the node out of the tree
for (auto it = children.begin(); it != children.end(); ++it) {
if ((*it)->name == name) {
node = *it;
2018-11-07 08:10:38 +01:00
node->parent = nullptr;
children.erase(it);
2018-11-07 08:10:38 +01:00
break;
}
}
return node;
}
2017-08-11 05:43:45 +02:00
/*****************
* Miscellaneous *
*****************/
static int bind_mount(const char *from, const char *to, bool log) {
2018-11-04 09:38:06 +01:00
int ret = xmount(from, to, nullptr, MS_BIND, nullptr);
if (log) VLOGI("bind_mount", from, to);
2018-11-04 09:38:06 +01:00
return ret;
}
2020-04-12 14:34:56 +02:00
#define DIR_IS(part) (me->mnt_dir == "/" #part ""sv)
#define SETMIR(b, part) sprintf(b, "%s/" MIRRDIR "/" #part, MAGISKTMP.data())
#define SETBLK(b, part) sprintf(b, "%s/" BLOCKDIR "/" #part, MAGISKTMP.data())
2019-04-08 05:03:49 +02:00
#define mount_mirror(part, flag) { \
2019-06-23 12:53:41 +02:00
xstat(me->mnt_fsname, &st); \
2020-04-12 14:34:56 +02:00
SETMIR(buf1, part); \
SETBLK(buf2, part); \
mknod(buf2, (st.st_mode & S_IFMT) | 0600, st.st_rdev); \
xmkdir(buf1, 0755); \
xmount(buf2, buf1, me->mnt_type, flag, nullptr); \
VLOGI("mount", buf2, buf1); \
2019-04-08 05:03:49 +02:00
}
2019-04-07 20:22:45 +02:00
static bool magisk_env() {
LOGI("* Initializing Magisk environment\n");
2017-12-25 20:23:58 +01:00
string pkg;
check_manager(&pkg);
2020-04-12 14:34:56 +02:00
char buf1[4096];
char buf2[4096];
sprintf(buf1, "%s/0/%s/install", APP_DATA_DIR, pkg.data());
// Alternative binaries paths
2020-04-12 14:34:56 +02:00
const char *alt_bin[] = { "/cache/data_adb/magisk", "/data/magisk", buf1 };
for (auto alt : alt_bin) {
struct stat st;
if (lstat(alt, &st) != -1) {
if (S_ISLNK(st.st_mode)) {
unlink(alt);
continue;
}
rm_rf(DATABIN);;
cp_afc(alt, DATABIN);
rm_rf(alt);
break;
}
}
// Remove stuffs
rm_rf("/cache/data_adb");
rm_rf("/data/adb/modules/.core");
unlink("/data/adb/magisk.img");
unlink("/data/adb/magisk_merge.img");
unlink("/data/magisk.img");
unlink("/data/magisk_merge.img");
unlink("/data/magisk_debug.log");
2020-04-12 14:34:56 +02:00
sprintf(buf1, "%s/" MODULEMNT, MAGISKTMP.data());
xmkdir(buf1, 0755);
// TODO: Remove. Backwards compatibility for old manager
sprintf(buf1, "%s/" INTLROOT "/img", MAGISKTMP.data());
xsymlink("./modules", buf1);
2019-03-22 06:50:33 +01:00
// Directories in /data/adb
xmkdir(DATABIN, 0755);
xmkdir(MODULEROOT, 0755);
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
xmkdir(SECURE_DIR "/service.d", 0755);
LOGI("* Mounting mirrors");
bool system_as_root = false;
2019-04-08 05:03:49 +02:00
struct stat st;
2019-06-23 12:53:41 +02:00
parse_mnt("/proc/mounts", [&](mntent *me) {
if (DIR_IS(system_root)) {
2019-04-07 20:22:45 +02:00
mount_mirror(system_root, MS_RDONLY);
2020-04-12 14:34:56 +02:00
SETMIR(buf1, system);
xsymlink("./system_root/system", buf1);
VLOGI("link", "./system_root/system", buf1);
system_as_root = true;
2019-06-23 12:53:41 +02:00
} else if (!system_as_root && DIR_IS(system)) {
2019-04-07 20:22:45 +02:00
mount_mirror(system, MS_RDONLY);
2019-06-23 12:53:41 +02:00
} else if (DIR_IS(vendor)) {
2019-04-07 20:22:45 +02:00
mount_mirror(vendor, MS_RDONLY);
2019-06-29 23:19:10 +02:00
} else if (DIR_IS(product)) {
mount_mirror(product, MS_RDONLY);
2019-06-23 12:53:41 +02:00
} else if (DIR_IS(data) && me->mnt_type != "tmpfs"sv) {
2019-04-07 20:22:45 +02:00
mount_mirror(data, 0);
2019-06-23 12:53:41 +02:00
} else if (SDK_INT >= 24 && DIR_IS(proc) && !strstr(me->mnt_opts, "hidepid=2")) {
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
}
return true;
});
2020-04-12 14:34:56 +02:00
SETMIR(buf1, system);
SETMIR(buf2, system_root);
if (access(buf1, F_OK) != 0 && access(buf2, F_OK) == 0) {
2019-06-26 08:31:59 +02:00
// Pre-init mirrors
2020-04-12 14:34:56 +02:00
xsymlink("./system_root/system", buf1);
VLOGI("link", "./system_root/system", buf1);
2019-06-26 08:31:59 +02:00
}
2020-04-12 14:34:56 +02:00
SETMIR(buf1, vendor);
if (access(buf1, F_OK) != 0) {
xsymlink("./system/vendor", buf1);
VLOGI("link", "./system/vendor", buf1);
}
2020-04-12 14:34:56 +02:00
SETMIR(buf1, product);
if (access("/system/product", F_OK) == 0 && access(buf1, F_OK) != 0) {
xsymlink("./system/product", buf1);
VLOGI("link", "./system/product", buf1);
}
2020-02-01 07:22:31 +01:00
// Disable/remove magiskhide, resetprop
2019-01-20 23:52:19 +01:00
if (SDK_INT < 19) {
unlink("/sbin/resetprop");
unlink("/sbin/magiskhide");
}
2019-03-22 07:01:36 +01:00
if (access(DATABIN "/busybox", X_OK) == -1)
return false;
2020-04-12 14:34:56 +02:00
// TODO: Remove. Backwards compatibility for old manager
LOGI("* Setting up internal busybox\n");
sprintf(buf1, "%s/" BBPATH "/busybox", MAGISKTMP.data());
mkdir(dirname(buf1), 0755);
cp_afc(DATABIN "/busybox", buf1);
exec_command_sync(buf1, "--install", "-s", dirname(buf1));
2019-03-22 07:01:36 +01:00
return true;
}
static void prepare_modules() {
// Upgrade modules
2019-12-30 06:21:39 +01:00
if (auto dir = open_dir(MODULEUPGRADE); dir) {
2020-04-12 14:34:56 +02:00
int ufd = dirfd(dir.get());
int mfd = xopen(MODULEROOT, O_RDONLY | O_CLOEXEC);
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
// Cleanup old module if exists
2020-04-12 14:34:56 +02:00
if (faccessat(mfd, entry->d_name, F_OK, 0) == 0) {
frm_rf(xopenat(mfd, entry->d_name, O_RDONLY | O_CLOEXEC));
unlinkat(mfd, entry->d_name, AT_REMOVEDIR);
}
LOGI("Upgrade / New module: %s\n", entry->d_name);
2020-04-12 14:34:56 +02:00
renameat(ufd, entry->d_name, mfd, entry->d_name);
}
}
2020-04-12 14:34:56 +02:00
close(mfd);
rm_rf(MODULEUPGRADE);
}
2020-04-12 14:34:56 +02:00
auto src = MAGISKTMP + "/" MIRRDIR "/" MODULEROOT;
auto dest = MAGISKTMP + "/" MODULEMNT;
bind_mount(src.data(), dest.data(), false);
restorecon();
chmod(SECURE_DIR, 0700);
}
static void reboot() {
if (RECOVERY_MODE)
exec_command_sync("/system/bin/reboot", "recovery");
else
exec_command_sync("/system/bin/reboot");
}
void remove_modules() {
LOGI("* Remove all modules and reboot");
2019-12-21 11:29:38 +01:00
auto dir = xopen_dir(MODULEROOT);
int dfd = dirfd(dir.get());
2019-12-13 06:37:06 +01:00
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv || entry->d_name == ".core"sv)
continue;
2019-12-21 11:29:38 +01:00
int modfd = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC);
close(xopenat(modfd, "remove", O_RDONLY | O_CREAT | O_CLOEXEC));
close(modfd);
}
}
reboot();
}
static void collect_modules() {
2019-12-21 11:29:38 +01:00
auto dir = xopen_dir(MODULEROOT);
int dfd = dirfd(dir.get());
2019-12-13 06:37:06 +01:00
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv || entry->d_name == ".core"sv)
continue;
2019-12-21 11:29:38 +01:00
int modfd = xopenat(dfd, entry->d_name, O_RDONLY);
run_finally f([=]{ close(modfd); });
if (faccessat(modfd, "remove", F_OK, 0) == 0) {
LOGI("%s: remove\n", entry->d_name);
2020-04-12 14:34:56 +02:00
auto uninstaller = MODULEROOT + "/"s + entry->d_name + "/uninstall.sh";
if (access(uninstaller.data(), F_OK) == 0)
exec_script(uninstaller.data());
2020-04-02 08:37:11 +02:00
frm_rf(xdup(modfd));
2019-12-30 06:21:39 +01:00
unlinkat(dfd, entry->d_name, AT_REMOVEDIR);
continue;
}
2019-12-21 11:29:38 +01:00
unlinkat(modfd, "update", 0);
if (faccessat(modfd, "disable", F_OK, 0) != 0)
2019-01-20 05:59:37 +01:00
module_list.emplace_back(entry->d_name);
}
}
}
2019-08-31 15:53:47 +02:00
static bool load_modules(node_entry *root) {
2020-04-12 14:34:56 +02:00
char buf1[4096];
char buf2[4096];
2019-08-31 15:53:47 +02:00
2020-04-12 14:34:56 +02:00
LOGI("* Loading modules\n");
2019-08-31 15:53:47 +02:00
bool has_modules = false;
for (const auto &m : module_list) {
2020-04-12 14:34:56 +02:00
auto module = m.data();
char *b1 = buf1 + sprintf(buf1, MODULEROOT "/%s/", module);
2019-08-31 15:53:47 +02:00
// Read props
2020-04-12 14:34:56 +02:00
strcpy(b1, "system.prop");
if (access(buf1, F_OK) == 0) {
2019-08-31 15:53:47 +02:00
LOGI("%s: loading [system.prop]\n", module);
2020-04-12 14:34:56 +02:00
load_prop_file(buf1, false);
2019-08-31 15:53:47 +02:00
}
2020-04-12 14:34:56 +02:00
// Copy sepolicy rules
2020-04-12 14:34:56 +02:00
strcpy(b1, "sepolicy.rule");
char *b2 = buf2 + sprintf(buf2, "%s/" MIRRDIR "/persist", MAGISKTMP.data());
if (access(buf2, F_OK) == 0 && access(buf1, F_OK) == 0) {
b2 += sprintf(b2, "/magisk/%s", module);
xmkdirs(buf2, 0755);
2020-04-12 14:34:56 +02:00
strcpy(b2, "/sepolicy.rule");
cp_afc(buf1, buf2);
}
2019-08-31 15:53:47 +02:00
// Check whether skip mounting
2020-04-12 14:34:56 +02:00
strcpy(b1, "skip_mount");
if (access(buf1, F_OK) == 0)
2019-08-31 15:53:47 +02:00
continue;
2020-04-12 14:34:56 +02:00
2019-08-31 15:53:47 +02:00
// Double check whether the system folder exists
2020-04-12 14:34:56 +02:00
strcpy(b1, "system");
if (access(buf1, F_OK) != 0)
2019-08-31 15:53:47 +02:00
continue;
// Construct structure
has_modules = true;
LOGI("%s: constructing magic mount structure\n", module);
// If /system/vendor exists in module, create a link outside
2020-04-12 14:34:56 +02:00
strcpy(b1, "system/vendor");
if (node_entry::vendor_root && access(buf1, F_OK) == 0) {
sprintf(buf2, MODULEROOT "/%s/vendor", module);
2019-08-31 15:53:47 +02:00
unlink(buf2);
xsymlink("./system/vendor", buf2);
}
// If /system/product exists in module, create a link outside
2020-04-12 14:34:56 +02:00
strcpy(b1, "system/product");
if (node_entry::product_root && access(buf1, F_OK) == 0) {
sprintf(buf2, MODULEROOT "/%s/product", module);
2019-08-31 15:53:47 +02:00
unlink(buf2);
xsymlink("./system/product", buf2);
}
root->create_module_tree(module);
}
return has_modules;
}
2018-11-04 09:38:06 +01:00
static bool check_data() {
bool mnt = false;
bool data = false;
2019-03-06 02:27:09 +01:00
file_readline("/proc/mounts", [&](string_view s) -> bool {
if (str_contains(s, " /data ") && !str_contains(s, "tmpfs"))
2018-11-04 09:38:06 +01:00
mnt = true;
return true;
});
2018-10-13 03:46:09 +02:00
if (mnt) {
2019-01-20 05:59:37 +01:00
auto crypto = getprop("ro.crypto.state");
2018-11-07 08:10:38 +01:00
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;
}
void unlock_blocks() {
int fd, dev, OFF = 0;
2019-12-13 06:37:06 +01:00
auto dir = xopen_dir("/dev/block");
if (!dir)
2018-10-13 03:46:09 +02:00
return;
2019-12-13 06:37:06 +01:00
dev = dirfd(dir.get());
2018-10-13 03:46:09 +02:00
2019-12-13 06:37:06 +01:00
for (dirent *entry; (entry = readdir(dir.get()));) {
2018-10-13 03:46:09 +02:00
if (entry->d_type == DT_BLK) {
2019-04-30 02:04:39 +02:00
if ((fd = openat(dev, entry->d_name, O_RDONLY | O_CLOEXEC)) < 0)
2018-10-13 03:46:09 +02:00
continue;
2019-04-30 02:04:39 +02:00
if (ioctl(fd, BLKROSET, &OFF) < 0)
2018-10-13 03:46:09 +02:00
PLOGE("unlock %s", entry->d_name);
close(fd);
}
}
}
2019-02-14 10:27:30 +01:00
static bool log_dump = false;
static void dump_logs() {
if (log_dump)
return;
int test = exec_command_sync("/system/bin/logcat", "-d", "-f", "/dev/null");
chmod("/dev/null", 0666);
if (test != 0)
return;
rename(LOGFILE, LOGFILE ".bak");
log_dump = true;
// Start a daemon thread and wait indefinitely
2019-05-26 06:42:51 +02:00
new_daemon_thread([]() -> void {
int fd = xopen(LOGFILE, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
exec_t exec {
.fd = fd,
.fork = fork_no_zombie
};
int pid = exec_command(exec, "/system/bin/logcat", "-s", "Magisk");
close(fd);
if (pid < 0) {
log_dump = false;
2019-05-26 06:42:51 +02:00
} else {
waitpid(pid, nullptr, 0);
}
});
2019-02-14 10:27:30 +01:00
}
2017-04-30 19:58:52 +02:00
/****************
* Entry points *
****************/
[[noreturn]] 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
}
2019-03-04 22:45:18 +01:00
[[noreturn]] static void core_only() {
pfs_done = true;
auto_start_magiskhide();
unblock_boot_process();
}
void post_fs_data(int client) {
// ack
write_int(client, 0);
close(client);
2019-06-22 12:14:33 +02:00
if (getenv("REMOUNT_ROOT"))
xmount(nullptr, "/", nullptr, MS_REMOUNT | MS_RDONLY, nullptr);
if (!check_data())
unblock_boot_process();
2019-02-14 10:27:30 +01:00
dump_logs();
LOGI("** post-fs-data mode running\n");
// Unlock all blocks for rw
unlock_blocks();
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...");
2019-03-22 06:50:33 +01:00
no_secure_dir = true;
unblock_boot_process();
}
2017-04-30 19:58:52 +02:00
if (!magisk_env()) {
LOGE("* Magisk environment setup incomplete, abort\n");
unblock_boot_process();
2018-11-04 09:38:06 +01:00
}
2017-04-30 19:58:52 +02:00
LOGI("* Running post-fs-data.d scripts\n");
exec_common_script("post-fs-data");
prepare_modules();
// Core only mode
if (access(DISABLEFILE, F_OK) == 0)
core_only();
collect_modules();
2017-08-12 19:15:18 +02:00
// Execute module scripts
LOGI("* Running module post-fs-data scripts\n");
2019-02-16 02:45:05 +01:00
exec_module_script("post-fs-data", module_list);
2017-08-12 19:15:18 +02:00
// Recollect modules
2019-01-20 05:59:37 +01:00
module_list.clear();
collect_modules();
node_entry::vendor_root = access("/vendor", F_OK) == 0;
node_entry::product_root = access("/product", F_OK) == 0;
2017-04-30 19:58:52 +02:00
// Create the system root entry
auto sys_root = new node_entry("system");
2017-04-30 19:58:52 +02:00
2019-08-31 15:53:47 +02:00
if (load_modules(sys_root)) {
// Pull out special nodes if exist
node_entry *special;
if (node_entry::vendor_root && (special = sys_root->extract("vendor"))) {
special->magic_mount();
delete special;
}
if (node_entry::product_root && (special = sys_root->extract("product"))) {
2019-06-29 23:19:10 +02:00
special->magic_mount();
delete special;
}
2017-06-11 21:29:01 +02:00
2018-11-04 09:38:06 +01:00
sys_root->magic_mount();
2017-04-30 19:58:52 +02:00
}
// Cleanup memory
2018-11-04 09:38:06 +01:00
delete sys_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);
2019-02-14 10:27:30 +01:00
dump_logs();
2019-03-22 06:50:33 +01:00
if (no_secure_dir) {
// It's safe to create the folder at this point if the system didn't create it
2019-03-22 06:50:33 +01:00
if (access(SECURE_DIR, F_OK) != 0)
xmkdir(SECURE_DIR, 0700);
// And reboot to make proper setup possible
reboot();
}
if (!pfs_done)
return;
2018-08-02 19:58:56 +02:00
auto_start_magiskhide();
2017-08-12 19:15:18 +02:00
LOGI("* Running service.d scripts\n");
exec_common_script("service");
2017-06-11 10:51:44 +02:00
// Core only mode
if (access(DISABLEFILE, F_OK) != 0) {
LOGI("* Running module service scripts\n");
exec_module_script("service", module_list);
}
// All boot stage done, cleanup
module_list.clear();
module_list.shrink_to_fit();
}
2017-06-11 10:51:44 +02:00
void boot_complete(int client) {
LOGI("** boot_complete triggered\n");
// ack
write_int(client, 0);
close(client);
2017-04-30 19:58:52 +02:00
if (!pfs_done)
return;
auto_start_magiskhide();
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 manager installed
if (!check_manager()) {
// Install stub
2019-01-26 19:39:24 +01:00
exec_command_sync("/sbin/magiskinit", "-x", "manager", "/data/magisk.apk");
install_apk("/data/magisk.apk");
}
}
2017-04-30 19:58:52 +02:00
}