2019-03-05 09:22:20 +01:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/wait.h>
|
2020-05-19 13:53:16 +02:00
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <linux/input.h>
|
2020-04-12 14:34:56 +02:00
|
|
|
#include <libgen.h>
|
2023-03-03 15:53:53 +01:00
|
|
|
#include <set>
|
2019-01-20 05:59:37 +01:00
|
|
|
#include <string>
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2020-03-09 09:50:30 +01:00
|
|
|
#include <magisk.hpp>
|
|
|
|
#include <db.hpp>
|
2022-05-12 11:03:42 +02:00
|
|
|
#include <base.hpp>
|
2020-03-09 09:50:30 +01:00
|
|
|
#include <daemon.hpp>
|
|
|
|
#include <resetprop.hpp>
|
|
|
|
#include <selinux.hpp>
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2021-01-11 11:19:10 +01:00
|
|
|
#include "core.hpp"
|
|
|
|
|
2019-01-20 05:59:37 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2022-12-26 09:04:58 +01:00
|
|
|
// Boot stage state
|
|
|
|
enum : int {
|
|
|
|
FLAG_NONE = 0,
|
|
|
|
FLAG_POST_FS_DATA_DONE = (1 << 0),
|
|
|
|
FLAG_LATE_START_DONE = (1 << 1),
|
|
|
|
FLAG_BOOT_COMPLETE = (1 << 2),
|
|
|
|
FLAG_SAFE_MODE = (1 << 3),
|
|
|
|
};
|
|
|
|
|
|
|
|
static int boot_state = FLAG_NONE;
|
|
|
|
|
2021-09-15 11:49:54 +02:00
|
|
|
bool zygisk_enabled = false;
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2020-04-18 11:00:48 +02:00
|
|
|
/*********
|
|
|
|
* Setup *
|
|
|
|
*********/
|
2020-04-12 14:34:56 +02:00
|
|
|
|
Refactor magic mount to support overlayfs
Previously, magic mount creates its own mirror devices and mount
mirror mount points. With these mirror mount points, magic mount
can get the original files and directory trees. However, some
devices use overlayfs to modify some mount points, and thus after
magic mount, the overlayed files are missing because the mirror
mount points do not contain the overlayed files. To address this
issue and make magic mount more compatible, this patch refactors
how magic mount works.
The new workflows are as follows:
1. make MAGISKTMP a private mount point so that we can create the
private mount points there
2. for mirror mount points, we instead of creating our own mirror
devices and mount the mirror mount points, we "copy" the
original mount points by recursively mounting /
3. to prevent magic mount affecting the mirror mount points, we
recursively set the mirror mount points private
4. to trace the mount points we created for reverting mounts, we
again make the mirror mount points shared, and by this way we
create a new peer group for each mirror mount points
5. as for tracing the newly created tmpfs mount point by magic
mount, we create a dedicated tmpfs mount point for them, namely
worker mount point, and obviously, it is shared as in a newly
created peer group for tracing
6. when reverting mount points by magic mount, we can then trace
the peer group id and unmount the mount points whose peer group
ids are created by us
The advantages are as follows:
1. it is more compatible, (e.g., with overlayfs, fix #2359)
2. it can mount more partitions for which previous implementation
cannot create mirror mount points (fix #3338)
2022-12-26 21:30:12 +01:00
|
|
|
static bool mount_mirror(const std::string_view from, const std::string_view to) {
|
|
|
|
return !xmkdirs(to.data(), 0755) &&
|
|
|
|
// recursively bind mount to mirror dir, rootfs will fail before 3.12 kernel
|
|
|
|
// because of MS_NOUSER
|
|
|
|
!mount(from.data(), to.data(), nullptr, MS_BIND | MS_REC, nullptr) &&
|
|
|
|
// make mirror dir as a private mount so that it won't be affected by magic mount
|
2023-03-05 21:35:10 +01:00
|
|
|
!xmount(nullptr, to.data(), nullptr, MS_PRIVATE | MS_REC, nullptr);
|
2019-04-08 05:03:49 +02:00
|
|
|
}
|
2019-04-07 20:22:45 +02:00
|
|
|
|
2020-11-04 13:49:43 +01:00
|
|
|
static void mount_mirrors() {
|
2023-03-05 21:35:10 +01:00
|
|
|
LOGI("* Mounting mirrors\n");
|
2023-03-08 07:42:54 +01:00
|
|
|
auto self_mount_info = parse_mount_info("self");
|
2023-03-05 21:35:10 +01:00
|
|
|
|
|
|
|
// Bind remount module root to clear nosuid
|
2023-03-07 10:47:26 +01:00
|
|
|
if (access(SECURE_DIR, F_OK) == 0 || SDK_INT < 24) {
|
2023-03-05 21:35:10 +01:00
|
|
|
auto dest = MAGISKTMP + "/" MODULEMNT;
|
2023-03-07 10:47:26 +01:00
|
|
|
xmkdir(SECURE_DIR, 0700);
|
|
|
|
xmkdir(MODULEROOT, 0755);
|
|
|
|
xmkdir(dest.data(), 0755);
|
2023-03-05 21:35:10 +01:00
|
|
|
xmount(MODULEROOT, dest.data(), nullptr, MS_BIND, nullptr);
|
2023-03-07 10:47:26 +01:00
|
|
|
xmount(nullptr, dest.data(), nullptr, MS_REMOUNT | MS_BIND | MS_NOATIME, nullptr);
|
2023-03-05 21:35:10 +01:00
|
|
|
xmount(nullptr, dest.data(), nullptr, MS_PRIVATE, nullptr);
|
|
|
|
chmod(SECURE_DIR, 0700);
|
|
|
|
restorecon();
|
|
|
|
}
|
|
|
|
|
2023-03-16 12:07:00 +01:00
|
|
|
// Check and mount preinit mirror
|
|
|
|
if (struct stat st{}; stat((MAGISKTMP + "/" PREINITDEV).data(), &st) == 0 && (st.st_mode & S_IFBLK)) {
|
2023-03-16 12:13:45 +01:00
|
|
|
// DO NOT mount the block device directly, as we do not know the flags and configs
|
|
|
|
// to properly mount the partition; mounting block devices directly as rw could cause
|
|
|
|
// crashes if the filesystem driver is crap (e.g. some broken F2FS drivers).
|
|
|
|
// What we do instead is to scan through the current mountinfo and find a pre-existing
|
|
|
|
// mount point mounting our desired partition, and then bind mount the target folder.
|
2023-03-16 12:07:00 +01:00
|
|
|
dev_t preinit_dev = st.st_rdev;
|
2023-03-08 07:42:54 +01:00
|
|
|
for (const auto &info: self_mount_info) {
|
2023-03-16 12:07:00 +01:00
|
|
|
if (info.root == "/" && info.device == preinit_dev) {
|
2023-03-08 07:42:54 +01:00
|
|
|
auto flags = split_ro(info.fs_option, ",");
|
|
|
|
auto rw = std::any_of(flags.begin(), flags.end(), [](const auto &flag) {
|
|
|
|
return flag == "rw"sv;
|
|
|
|
});
|
|
|
|
if (!rw) continue;
|
2023-03-16 12:07:00 +01:00
|
|
|
string preinit_dir = resolve_preinit_dir(info.target.data());
|
|
|
|
xmkdir(preinit_dir.data(), 0700);
|
|
|
|
auto mirror_dir = MAGISKTMP + "/" PREINITMIRR;
|
|
|
|
mount_mirror(preinit_dir, mirror_dir);
|
2023-03-16 14:37:29 +01:00
|
|
|
xmount(nullptr, mirror_dir.data(), nullptr, MS_UNBINDABLE, nullptr);
|
2023-03-08 07:42:54 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 21:35:10 +01:00
|
|
|
// Prepare worker
|
2023-02-12 19:11:25 +01:00
|
|
|
auto worker_dir = MAGISKTMP + "/" WORKERDIR;
|
|
|
|
xmount("worker", worker_dir.data(), "tmpfs", 0, "mode=755");
|
2023-03-02 09:12:13 +01:00
|
|
|
xmount(nullptr, worker_dir.data(), nullptr, MS_PRIVATE, nullptr);
|
2023-01-19 20:46:35 +01:00
|
|
|
|
2023-03-05 21:35:10 +01:00
|
|
|
// Recursively bind mount / to mirror dir
|
Refactor magic mount to support overlayfs
Previously, magic mount creates its own mirror devices and mount
mirror mount points. With these mirror mount points, magic mount
can get the original files and directory trees. However, some
devices use overlayfs to modify some mount points, and thus after
magic mount, the overlayed files are missing because the mirror
mount points do not contain the overlayed files. To address this
issue and make magic mount more compatible, this patch refactors
how magic mount works.
The new workflows are as follows:
1. make MAGISKTMP a private mount point so that we can create the
private mount points there
2. for mirror mount points, we instead of creating our own mirror
devices and mount the mirror mount points, we "copy" the
original mount points by recursively mounting /
3. to prevent magic mount affecting the mirror mount points, we
recursively set the mirror mount points private
4. to trace the mount points we created for reverting mounts, we
again make the mirror mount points shared, and by this way we
create a new peer group for each mirror mount points
5. as for tracing the newly created tmpfs mount point by magic
mount, we create a dedicated tmpfs mount point for them, namely
worker mount point, and obviously, it is shared as in a newly
created peer group for tracing
6. when reverting mount points by magic mount, we can then trace
the peer group id and unmount the mount points whose peer group
ids are created by us
The advantages are as follows:
1. it is more compatible, (e.g., with overlayfs, fix #2359)
2. it can mount more partitions for which previous implementation
cannot create mirror mount points (fix #3338)
2022-12-26 21:30:12 +01:00
|
|
|
if (auto mirror_dir = MAGISKTMP + "/" MIRRDIR; !mount_mirror("/", mirror_dir)) {
|
|
|
|
LOGI("fallback to mount subtree\n");
|
|
|
|
// rootfs may fail, fallback to bind mount each mount point
|
2023-03-03 15:53:53 +01:00
|
|
|
set<string, greater<>> mounted_dirs {{ MAGISKTMP }};
|
2023-03-08 07:42:54 +01:00
|
|
|
for (const auto &info: self_mount_info) {
|
Refactor magic mount to support overlayfs
Previously, magic mount creates its own mirror devices and mount
mirror mount points. With these mirror mount points, magic mount
can get the original files and directory trees. However, some
devices use overlayfs to modify some mount points, and thus after
magic mount, the overlayed files are missing because the mirror
mount points do not contain the overlayed files. To address this
issue and make magic mount more compatible, this patch refactors
how magic mount works.
The new workflows are as follows:
1. make MAGISKTMP a private mount point so that we can create the
private mount points there
2. for mirror mount points, we instead of creating our own mirror
devices and mount the mirror mount points, we "copy" the
original mount points by recursively mounting /
3. to prevent magic mount affecting the mirror mount points, we
recursively set the mirror mount points private
4. to trace the mount points we created for reverting mounts, we
again make the mirror mount points shared, and by this way we
create a new peer group for each mirror mount points
5. as for tracing the newly created tmpfs mount point by magic
mount, we create a dedicated tmpfs mount point for them, namely
worker mount point, and obviously, it is shared as in a newly
created peer group for tracing
6. when reverting mount points by magic mount, we can then trace
the peer group id and unmount the mount points whose peer group
ids are created by us
The advantages are as follows:
1. it is more compatible, (e.g., with overlayfs, fix #2359)
2. it can mount more partitions for which previous implementation
cannot create mirror mount points (fix #3338)
2022-12-26 21:30:12 +01:00
|
|
|
if (info.type == "rootfs"sv) continue;
|
2023-03-03 15:53:53 +01:00
|
|
|
// the greatest mount point that less than info.target, which is possibly a parent
|
|
|
|
if (auto last_mount = mounted_dirs.upper_bound(info.target);
|
|
|
|
last_mount != mounted_dirs.end() && info.target.starts_with(*last_mount + '/')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mount_mirror(info.target, mirror_dir + info.target)) {
|
|
|
|
LOGD("%-8s: %s <- %s\n", "rbind", (mirror_dir + info.target).data(), info.target.data());
|
|
|
|
mounted_dirs.insert(info.target);
|
Refactor magic mount to support overlayfs
Previously, magic mount creates its own mirror devices and mount
mirror mount points. With these mirror mount points, magic mount
can get the original files and directory trees. However, some
devices use overlayfs to modify some mount points, and thus after
magic mount, the overlayed files are missing because the mirror
mount points do not contain the overlayed files. To address this
issue and make magic mount more compatible, this patch refactors
how magic mount works.
The new workflows are as follows:
1. make MAGISKTMP a private mount point so that we can create the
private mount points there
2. for mirror mount points, we instead of creating our own mirror
devices and mount the mirror mount points, we "copy" the
original mount points by recursively mounting /
3. to prevent magic mount affecting the mirror mount points, we
recursively set the mirror mount points private
4. to trace the mount points we created for reverting mounts, we
again make the mirror mount points shared, and by this way we
create a new peer group for each mirror mount points
5. as for tracing the newly created tmpfs mount point by magic
mount, we create a dedicated tmpfs mount point for them, namely
worker mount point, and obviously, it is shared as in a newly
created peer group for tracing
6. when reverting mount points by magic mount, we can then trace
the peer group id and unmount the mount points whose peer group
ids are created by us
The advantages are as follows:
1. it is more compatible, (e.g., with overlayfs, fix #2359)
2. it can mount more partitions for which previous implementation
cannot create mirror mount points (fix #3338)
2022-12-26 21:30:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 13:49:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-16 14:37:29 +01:00
|
|
|
string find_preinit_device() {
|
|
|
|
enum {
|
|
|
|
UNKNOWN,
|
|
|
|
PERSIST,
|
|
|
|
METADATA,
|
|
|
|
CACHE,
|
|
|
|
DATA,
|
|
|
|
} matched = UNKNOWN;
|
2023-03-08 07:42:54 +01:00
|
|
|
bool encrypted = getprop("ro.crypto.state") == "encrypted";
|
2023-03-11 20:44:59 +01:00
|
|
|
bool mount = getuid() == 0 && getenv("MAGISKTMP");
|
|
|
|
|
2023-03-16 14:37:29 +01:00
|
|
|
string preinit_source;
|
|
|
|
string preinit_dir;
|
|
|
|
|
2023-03-11 20:44:59 +01:00
|
|
|
for (const auto &info: parse_mount_info("self")) {
|
2023-03-16 12:07:00 +01:00
|
|
|
if (info.target.ends_with(PREINITMIRR))
|
2023-03-16 14:37:29 +01:00
|
|
|
return basename(info.source.data());
|
|
|
|
if (info.root != "/" || info.source[0] != '/' || info.source.find("/dm-") != string::npos)
|
2023-03-08 07:42:54 +01:00
|
|
|
continue;
|
|
|
|
if (info.type != "ext4" && info.type != "f2fs")
|
|
|
|
continue;
|
|
|
|
auto flags = split_ro(info.fs_option, ",");
|
|
|
|
auto rw = std::any_of(flags.begin(), flags.end(), [](const auto &flag) {
|
|
|
|
return flag == "rw"sv;
|
|
|
|
});
|
|
|
|
if (!rw) continue;
|
2023-03-16 14:37:29 +01:00
|
|
|
if (auto base = std::string_view(info.source).substr(0, info.source.find_last_of('/'));
|
|
|
|
!base.ends_with("/by-name") && !base.ends_with("/block")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (matched) {
|
|
|
|
case UNKNOWN:
|
|
|
|
if (info.target == "/persist" || info.target == "/mnt/vendor/persist") {
|
|
|
|
matched = PERSIST;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case PERSIST:
|
|
|
|
if (info.target == "/metadata") {
|
|
|
|
matched = METADATA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case METADATA:
|
|
|
|
if (info.target == "/cache") {
|
|
|
|
matched = CACHE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case CACHE:
|
|
|
|
if (info.target == "/data") {
|
|
|
|
if (!encrypted || access("/data/unencrypted", F_OK) == 0) {
|
|
|
|
matched = DATA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case DATA:
|
2023-03-08 07:42:54 +01:00
|
|
|
continue;
|
2023-03-16 14:37:29 +01:00
|
|
|
}
|
2023-03-08 07:42:54 +01:00
|
|
|
|
2023-03-11 20:44:59 +01:00
|
|
|
if (mount) {
|
2023-03-16 12:07:00 +01:00
|
|
|
preinit_dir = resolve_preinit_dir(info.target.data());
|
2023-03-11 20:44:59 +01:00
|
|
|
}
|
2023-03-16 14:37:29 +01:00
|
|
|
preinit_source = info.source;
|
2023-03-08 07:42:54 +01:00
|
|
|
}
|
2023-03-11 20:44:59 +01:00
|
|
|
|
2023-03-16 12:07:00 +01:00
|
|
|
if (!preinit_dir.empty()) {
|
|
|
|
auto mirror_dir = string(getenv("MAGISKTMP")) + "/" PREINITMIRR;
|
|
|
|
mkdirs(preinit_dir.data(), 0700);
|
|
|
|
mkdirs(mirror_dir.data(), 0700);
|
|
|
|
xmount(preinit_dir.data(), mirror_dir.data(), nullptr, MS_BIND, nullptr);
|
2023-03-11 20:44:59 +01:00
|
|
|
}
|
2023-03-16 14:37:29 +01:00
|
|
|
return preinit_source.empty() ? "" : basename(preinit_source.data());
|
2023-03-08 07:42:54 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 13:49:43 +01:00
|
|
|
static bool magisk_env() {
|
2020-12-31 07:11:24 +01:00
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
LOGI("* Initializing Magisk environment\n");
|
|
|
|
|
2022-05-20 07:54:49 +02:00
|
|
|
preserve_stub_apk();
|
2020-12-31 07:11:24 +01:00
|
|
|
string pkg;
|
2022-05-18 10:55:58 +02:00
|
|
|
get_manager(0, &pkg);
|
2020-12-31 07:11:24 +01:00
|
|
|
|
2022-11-01 10:04:50 +01:00
|
|
|
ssprintf(buf, sizeof(buf), "%s/0/%s/install", APP_DATA_DIR,
|
2021-09-17 11:07:32 +02:00
|
|
|
pkg.empty() ? "xxx" /* Ensure non-exist path */ : pkg.data());
|
2020-12-31 07:11:24 +01:00
|
|
|
|
|
|
|
// Alternative binaries paths
|
|
|
|
const char *alt_bin[] = { "/cache/data_adb/magisk", "/data/magisk", buf };
|
|
|
|
for (auto alt : alt_bin) {
|
2022-03-17 20:56:34 +01:00
|
|
|
struct stat st{};
|
2020-12-31 07:11:24 +01:00
|
|
|
if (lstat(alt, &st) == 0) {
|
|
|
|
if (S_ISLNK(st.st_mode)) {
|
|
|
|
unlink(alt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rm_rf(DATABIN);
|
|
|
|
cp_afc(alt, DATABIN);
|
|
|
|
rm_rf(alt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rm_rf("/cache/data_adb");
|
|
|
|
|
|
|
|
// Directories in /data/adb
|
|
|
|
xmkdir(DATABIN, 0755);
|
|
|
|
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
|
|
|
|
xmkdir(SECURE_DIR "/service.d", 0755);
|
|
|
|
|
2022-07-21 21:08:37 +02:00
|
|
|
restore_databincon();
|
|
|
|
|
2021-01-25 09:23:42 +01:00
|
|
|
if (access(DATABIN "/busybox", X_OK))
|
|
|
|
return false;
|
2020-12-31 07:11:24 +01:00
|
|
|
|
|
|
|
sprintf(buf, "%s/" BBPATH "/busybox", MAGISKTMP.data());
|
|
|
|
mkdir(dirname(buf), 0755);
|
2021-01-25 09:23:42 +01:00
|
|
|
cp_afc(DATABIN "/busybox", buf);
|
2020-12-31 07:11:24 +01:00
|
|
|
exec_command_async(buf, "--install", "-s", dirname(buf));
|
|
|
|
|
2022-03-17 11:15:39 +01:00
|
|
|
if (access(DATABIN "/magiskpolicy", X_OK) == 0) {
|
|
|
|
sprintf(buf, "%s/magiskpolicy", MAGISKTMP.data());
|
|
|
|
cp_afc(DATABIN "/magiskpolicy", buf);
|
|
|
|
}
|
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
return true;
|
2018-11-16 04:55:28 +01:00
|
|
|
}
|
2018-11-13 07:53:48 +01:00
|
|
|
|
2020-04-30 10:26:50 +02:00
|
|
|
void reboot() {
|
2020-12-31 07:11:24 +01:00
|
|
|
if (RECOVERY_MODE)
|
|
|
|
exec_command_sync("/system/bin/reboot", "recovery");
|
|
|
|
else
|
|
|
|
exec_command_sync("/system/bin/reboot");
|
2019-11-01 08:07:12 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
static bool check_data() {
|
2020-12-31 07:11:24 +01:00
|
|
|
bool mnt = false;
|
|
|
|
file_readline("/proc/mounts", [&](string_view s) {
|
|
|
|
if (str_contains(s, " /data ") && !str_contains(s, "tmpfs")) {
|
|
|
|
mnt = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (!mnt)
|
|
|
|
return false;
|
|
|
|
auto crypto = getprop("ro.crypto.state");
|
|
|
|
if (!crypto.empty()) {
|
2022-06-28 07:21:07 +02:00
|
|
|
if (crypto != "encrypted") {
|
2020-12-31 07:11:24 +01:00
|
|
|
// Unencrypted, we can directly access data
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Encrypted, check whether vold is started
|
|
|
|
return !getprop("init.svc.vold").empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ro.crypto.state is not set, assume it's unencrypted
|
|
|
|
return true;
|
2018-10-13 03:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_blocks() {
|
2020-12-31 07:11:24 +01:00
|
|
|
int fd, dev, OFF = 0;
|
|
|
|
|
|
|
|
auto dir = xopen_dir("/dev/block");
|
|
|
|
if (!dir)
|
|
|
|
return;
|
|
|
|
dev = dirfd(dir.get());
|
|
|
|
|
|
|
|
for (dirent *entry; (entry = readdir(dir.get()));) {
|
|
|
|
if (entry->d_type == DT_BLK) {
|
|
|
|
if ((fd = openat(dev, entry->d_name, O_RDONLY | O_CLOEXEC)) < 0)
|
|
|
|
continue;
|
|
|
|
if (ioctl(fd, BLKROSET, &OFF) < 0)
|
|
|
|
PLOGE("unlock %s", entry->d_name);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 03:46:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:53:16 +02:00
|
|
|
#define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8)))
|
|
|
|
|
|
|
|
static bool check_key_combo() {
|
2020-12-31 07:11:24 +01:00
|
|
|
uint8_t bitmask[(KEY_MAX + 1) / 8];
|
|
|
|
vector<int> events;
|
|
|
|
constexpr char name[] = "/dev/.ev";
|
|
|
|
|
|
|
|
// First collect candidate events that accepts volume down
|
|
|
|
for (int minor = 64; minor < 96; ++minor) {
|
|
|
|
if (xmknod(name, S_IFCHR | 0444, makedev(13, minor)))
|
|
|
|
continue;
|
|
|
|
int fd = open(name, O_RDONLY | O_CLOEXEC);
|
|
|
|
unlink(name);
|
|
|
|
if (fd < 0)
|
|
|
|
continue;
|
|
|
|
memset(bitmask, 0, sizeof(bitmask));
|
|
|
|
ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitmask)), bitmask);
|
|
|
|
if (test_bit(KEY_VOLUMEDOWN, bitmask))
|
|
|
|
events.push_back(fd);
|
|
|
|
else
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
if (events.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
run_finally fin([&]{ std::for_each(events.begin(), events.end(), close); });
|
|
|
|
|
|
|
|
// Check if volume down key is held continuously for more than 3 seconds
|
|
|
|
for (int i = 0; i < 300; ++i) {
|
|
|
|
bool pressed = false;
|
|
|
|
for (const int &fd : events) {
|
|
|
|
memset(bitmask, 0, sizeof(bitmask));
|
|
|
|
ioctl(fd, EVIOCGKEY(sizeof(bitmask)), bitmask);
|
|
|
|
if (test_bit(KEY_VOLUMEDOWN, bitmask)) {
|
|
|
|
pressed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!pressed)
|
|
|
|
return false;
|
|
|
|
// Check every 10ms
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
|
|
LOGD("KEY_VOLUMEDOWN detected: enter safe mode\n");
|
|
|
|
return true;
|
2020-05-19 13:53:16 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 12:40:37 +01:00
|
|
|
/***********************
|
|
|
|
* Boot Stage Handlers *
|
|
|
|
***********************/
|
|
|
|
|
2022-03-01 11:13:18 +01:00
|
|
|
extern int disable_deny();
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2022-12-26 09:04:58 +01:00
|
|
|
static void post_fs_data() {
|
2020-12-31 07:11:24 +01:00
|
|
|
if (!check_data())
|
2022-12-26 09:04:58 +01:00
|
|
|
return;
|
2020-12-31 07:11:24 +01:00
|
|
|
|
|
|
|
setup_logfile(true);
|
|
|
|
|
|
|
|
LOGI("** post-fs-data mode running\n");
|
|
|
|
|
|
|
|
unlock_blocks();
|
|
|
|
mount_mirrors();
|
2022-05-30 08:43:22 +02:00
|
|
|
prune_su_access();
|
2020-12-31 07:11:24 +01:00
|
|
|
|
|
|
|
if (access(SECURE_DIR, F_OK) != 0) {
|
Refactor magic mount to support overlayfs
Previously, magic mount creates its own mirror devices and mount
mirror mount points. With these mirror mount points, magic mount
can get the original files and directory trees. However, some
devices use overlayfs to modify some mount points, and thus after
magic mount, the overlayed files are missing because the mirror
mount points do not contain the overlayed files. To address this
issue and make magic mount more compatible, this patch refactors
how magic mount works.
The new workflows are as follows:
1. make MAGISKTMP a private mount point so that we can create the
private mount points there
2. for mirror mount points, we instead of creating our own mirror
devices and mount the mirror mount points, we "copy" the
original mount points by recursively mounting /
3. to prevent magic mount affecting the mirror mount points, we
recursively set the mirror mount points private
4. to trace the mount points we created for reverting mounts, we
again make the mirror mount points shared, and by this way we
create a new peer group for each mirror mount points
5. as for tracing the newly created tmpfs mount point by magic
mount, we create a dedicated tmpfs mount point for them, namely
worker mount point, and obviously, it is shared as in a newly
created peer group for tracing
6. when reverting mount points by magic mount, we can then trace
the peer group id and unmount the mount points whose peer group
ids are created by us
The advantages are as follows:
1. it is more compatible, (e.g., with overlayfs, fix #2359)
2. it can mount more partitions for which previous implementation
cannot create mirror mount points (fix #3338)
2022-12-26 21:30:12 +01:00
|
|
|
LOGE(SECURE_DIR " is not present, abort\n");
|
|
|
|
goto early_abort;
|
2020-12-31 07:11:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!magisk_env()) {
|
|
|
|
LOGE("* Magisk environment incomplete, abort\n");
|
|
|
|
goto early_abort;
|
|
|
|
}
|
|
|
|
|
2022-12-24 14:44:56 +01:00
|
|
|
if (getprop("persist.sys.safemode", true) == "1" ||
|
|
|
|
getprop("ro.sys.safemode") == "1" || check_key_combo()) {
|
2022-12-26 09:04:58 +01:00
|
|
|
boot_state |= FLAG_SAFE_MODE;
|
2021-09-12 21:40:34 +02:00
|
|
|
// Disable all modules and denylist so next boot will be clean
|
2020-12-31 07:11:24 +01:00
|
|
|
disable_modules();
|
2021-09-12 21:40:34 +02:00
|
|
|
disable_deny();
|
2020-12-31 07:11:24 +01:00
|
|
|
} else {
|
|
|
|
exec_common_scripts("post-fs-data");
|
2021-09-15 11:49:54 +02:00
|
|
|
db_settings dbs;
|
|
|
|
get_db_settings(dbs, ZYGISK_CONFIG);
|
2021-09-16 14:27:34 +02:00
|
|
|
zygisk_enabled = dbs[ZYGISK_CONFIG];
|
|
|
|
initialize_denylist();
|
2020-12-31 07:11:24 +01:00
|
|
|
handle_modules();
|
|
|
|
}
|
2017-05-03 21:05:37 +02:00
|
|
|
|
2020-11-04 13:49:43 +01:00
|
|
|
early_abort:
|
2020-12-31 07:11:24 +01:00
|
|
|
// We still do magic mount because root itself might need it
|
2023-02-26 03:11:25 +01:00
|
|
|
load_modules();
|
2022-12-26 09:04:58 +01:00
|
|
|
boot_state |= FLAG_POST_FS_DATA_DONE;
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
2022-12-26 09:04:58 +01:00
|
|
|
static void late_start() {
|
2020-12-31 07:11:24 +01:00
|
|
|
setup_logfile(false);
|
2019-02-14 10:27:30 +01:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
LOGI("** late_start service mode running\n");
|
2020-12-15 12:40:37 +01:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
exec_common_scripts("service");
|
|
|
|
exec_module_scripts("service");
|
2017-06-11 10:51:44 +02:00
|
|
|
|
2022-12-26 09:04:58 +01:00
|
|
|
boot_state |= FLAG_LATE_START_DONE;
|
|
|
|
}
|
2020-12-31 07:11:24 +01:00
|
|
|
|
2022-12-26 09:04:58 +01:00
|
|
|
static void boot_complete() {
|
|
|
|
boot_state |= FLAG_BOOT_COMPLETE;
|
2020-12-31 07:11:24 +01:00
|
|
|
setup_logfile(false);
|
|
|
|
|
2022-05-29 07:39:44 +02:00
|
|
|
LOGI("** boot-complete triggered\n");
|
2020-12-31 07:11:24 +01:00
|
|
|
|
|
|
|
// At this point it's safe to create the folder
|
|
|
|
if (access(SECURE_DIR, F_OK) != 0)
|
|
|
|
xmkdir(SECURE_DIR, 0700);
|
|
|
|
|
2022-05-20 07:54:49 +02:00
|
|
|
// Ensure manager exists
|
2022-05-30 08:31:57 +02:00
|
|
|
check_pkg_refresh();
|
2022-05-20 07:54:49 +02:00
|
|
|
get_manager(0, nullptr, true);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
2022-05-29 07:39:44 +02:00
|
|
|
|
2023-03-16 03:26:27 +01:00
|
|
|
void boot_stage_handler(int client, int code) {
|
2022-12-26 09:04:58 +01:00
|
|
|
// Make sure boot stage execution is always serialized
|
|
|
|
static pthread_mutex_t stage_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
mutex_guard lock(stage_lock);
|
2022-05-29 07:39:44 +02:00
|
|
|
|
2022-12-26 09:04:58 +01:00
|
|
|
switch (code) {
|
|
|
|
case MainRequest::POST_FS_DATA:
|
|
|
|
if ((boot_state & FLAG_POST_FS_DATA_DONE) == 0)
|
|
|
|
post_fs_data();
|
2023-03-16 03:26:27 +01:00
|
|
|
close(client);
|
2022-12-26 09:04:58 +01:00
|
|
|
break;
|
|
|
|
case MainRequest::LATE_START:
|
2023-03-16 03:26:27 +01:00
|
|
|
close(client);
|
2022-12-26 09:04:58 +01:00
|
|
|
if ((boot_state & FLAG_POST_FS_DATA_DONE) && (boot_state & FLAG_SAFE_MODE) == 0)
|
|
|
|
late_start();
|
|
|
|
break;
|
|
|
|
case MainRequest::BOOT_COMPLETE:
|
2023-03-16 03:26:27 +01:00
|
|
|
close(client);
|
2022-12-26 09:04:58 +01:00
|
|
|
if ((boot_state & FLAG_SAFE_MODE) == 0)
|
|
|
|
boot_complete();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2022-05-29 07:39:44 +02:00
|
|
|
}
|