From 93de4008ffff30621caec1313bc6edab79411935 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Fri, 5 Jan 2024 00:42:33 +0800 Subject: [PATCH 1/5] Remove unnecessary mirror for magic mount Mirror was previously used for accessing the original file during magic mount when we are using a tmpfs to cover the target. However, since we introduce atomic mount, we switch all tmpfs mount in worker and then move to the target at once. It means that we can still access the original file when we are constructing the tmpfs mount point. Thus we no longer need mirror. --- native/src/core/bootstages.cpp | 50 +++++----------------------------- native/src/core/module.cpp | 48 ++++++++++++-------------------- native/src/core/node.hpp | 35 ++++++++++-------------- 3 files changed, 40 insertions(+), 93 deletions(-) diff --git a/native/src/core/bootstages.cpp b/native/src/core/bootstages.cpp index 20b99b6abfde..4ba39438925c 100644 --- a/native/src/core/bootstages.cpp +++ b/native/src/core/bootstages.cpp @@ -19,18 +19,10 @@ bool zygisk_enabled = false; /********* * Setup * *********/ - -static bool rec_mount(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); -} - -static void mount_mirrors() { - LOGI("* Mounting mirrors\n"); +static void setup_mounts() { + LOGI("* Magic mount setup\n"); auto self_mount_info = parse_mount_info("self"); - char path[64]; + char path[PATH_MAX]; // Bind remount module root to clear nosuid if (access(SECURE_DIR, F_OK) == 0 || SDK_INT < 24) { @@ -65,8 +57,9 @@ static void mount_mirrors() { if (!rw) continue; string preinit_dir = resolve_preinit_dir(info.target.data()); xmkdir(preinit_dir.data(), 0700); - if ((mounted = rec_mount(preinit_dir, path))) { - xmount(nullptr, path, nullptr, MS_UNBINDABLE, nullptr); + xmkdirs(path, 0755); + mounted = xmount(preinit_dir.data(), path, nullptr, MS_BIND, nullptr) == 0; + if (mounted) { break; } } @@ -81,27 +74,6 @@ static void mount_mirrors() { ssprintf(path, sizeof(path), "%s/" WORKERDIR, get_magisk_tmp()); xmount("worker", path, "tmpfs", 0, "mode=755"); xmount(nullptr, path, nullptr, MS_PRIVATE, nullptr); - // Recursively bind mount / to mirror dir - // Keep mirror shared so that mounting during post-fs-data will be propagated - if (auto mirror_dir = get_magisk_tmp() + "/"s MIRRDIR; !rec_mount("/", mirror_dir)) { - LOGI("fallback to mount subtree\n"); - // create new a bind mount for easy make private - xmount(mirror_dir.data(), mirror_dir.data(), nullptr, MS_BIND, nullptr); - // rootfs may fail, fallback to bind mount each mount point - set> mounted_dirs {{ get_magisk_tmp() }}; - for (const auto &info: self_mount_info) { - if (info.type == "rootfs"sv) continue; - // 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 (rec_mount(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); - } - } - } } string find_preinit_device() { @@ -310,7 +282,7 @@ bool MagiskD::post_fs_data() const { LOGI("** post-fs-data mode running\n"); unlock_blocks(); - mount_mirrors(); + setup_mounts(); prune_su_access(); bool safe_mode = false; @@ -341,15 +313,7 @@ bool MagiskD::post_fs_data() const { } early_abort: - auto mirror_dir = get_magisk_tmp() + "/"s MIRRDIR; - // make mirror dir as a private mount so that it won't be affected by magic mount - LOGD("make %s private\n", mirror_dir.data()); - xmount(nullptr, mirror_dir.data(), nullptr, MS_PRIVATE | MS_REC, nullptr); - // We still do magic mount because root itself might need it load_modules(); - // make mirror dir as a shared mount to make magisk --stop work for other ns - xmount(nullptr, mirror_dir.data(), nullptr, MS_SHARED | MS_REC, nullptr); - LOGD("make %s shared\n", mirror_dir.data()); return safe_mode; } diff --git a/native/src/core/module.cpp b/native/src/core/module.cpp index cd77c65579ad..64c9328c1b11 100644 --- a/native/src/core/module.cpp +++ b/native/src/core/module.cpp @@ -27,24 +27,18 @@ static int bind_mount(const char *reason, const char *from, const char *to) { *************************/ tmpfs_node::tmpfs_node(node_entry *node) : dir_node(node, this) { - if (!skip_mirror()) { - string mirror = mirror_path(); - if (auto dir = open_dir(mirror.data())) { + if (!replace()) { + if (auto dir = open_dir(node_path().data())) { set_exist(true); for (dirent *entry; (entry = xreaddir(dir.get()));) { - if (entry->d_type == DT_DIR) { - // create a dummy inter_node to upgrade later - emplace(entry->d_name, entry->d_name); - } else { - // Insert mirror nodes - emplace(entry->d_name, entry); - } + // create a dummy inter_node to upgrade later + emplace(entry->d_name, entry); } } } for (auto it = children.begin(); it != children.end(); ++it) { - // Need to upgrade all inter_node children to tmpfs_node + // Upgrade resting inter_node children to tmpfs_node if (isa(it->second)) it = upgrade(it); } @@ -52,7 +46,7 @@ tmpfs_node::tmpfs_node(node_entry *node) : dir_node(node, this) { bool dir_node::prepare() { // If direct replace or not exist, mount ourselves as tmpfs - bool upgrade_to_tmpfs = skip_mirror() || !exist(); + bool upgrade_to_tmpfs = replace() || !exist(); for (auto it = children.begin(); it != children.end();) { // We also need to upgrade to tmpfs node if any child: @@ -77,9 +71,9 @@ bool dir_node::prepare() { upgrade_to_tmpfs = true; } if (auto dn = dyn_cast(it->second)) { - if (skip_mirror()) { + if (replace()) { // Propagate skip mirror state to all children - dn->set_skip_mirror(true); + dn->set_replace(true); } if (dn->prepare()) { // Upgrade child to tmpfs @@ -98,7 +92,7 @@ void dir_node::collect_module_files(const char *module, int dfd) { for (dirent *entry; (entry = xreaddir(dir.get()));) { if (entry->d_name == ".replace"sv) { - set_skip_mirror(true); + set_replace(true); continue; } @@ -141,18 +135,12 @@ void node_entry::create_and_mount(const char *reason, const string &src, bool ro } } -void mirror_node::mount() { - create_and_mount("mirror", mirror_path()); -} - void module_node::mount() { std::string path = module + (parent()->root()->prefix + node_path()); string mnt_src = module_mnt + path; { string src = MODULEROOT "/" + path; - if (exist()) clone_attr(mirror_path().data(), src.data()); - // special case for /system/etc/hosts to ensure it is writable - if (node_path() == "/system/etc/hosts") mnt_src = std::move(src); + if (exist()) clone_attr(node_path().data(), src.data()); } if (isa(parent())) { create_and_mount("module", mnt_src); @@ -162,22 +150,23 @@ void module_node::mount() { } void tmpfs_node::mount() { - string src = mirror_path(); - const char *src_path = access(src.data(), F_OK) == 0 ? src.data() : nullptr; + if (!is_dir()) { + create_and_mount("mirror", node_path()); + return; + } if (!isa(parent())) { - const string &dest = node_path(); auto worker_dir = worker_path(); mkdirs(worker_dir.data(), 0); bind_mount("tmpfs", worker_dir.data(), worker_dir.data()); - clone_attr(src_path ?: parent()->node_path().data(), worker_dir.data()); + clone_attr(exist() ? node_path().data() : parent()->node_path().data(), worker_dir.data()); dir_node::mount(); - VLOGD(skip_mirror() ? "replace" : "move", worker_dir.data(), dest.data()); - xmount(worker_dir.data(), dest.data(), nullptr, MS_MOVE, nullptr); + VLOGD(replace() ? "replace" : "move", worker_dir.data(), node_path().data()); + xmount(worker_dir.data(), node_path().data(), nullptr, MS_MOVE, nullptr); } else { const string dest = worker_path(); // We don't need another layer of tmpfs if parent is tmpfs mkdir(dest.data(), 0); - clone_attr(src_path ?: parent()->worker_path().data(), dest.data()); + clone_attr(exist() ? node_path().data() : parent()->worker_path().data(), dest.data()); dir_node::mount(); } } @@ -265,7 +254,6 @@ static void inject_zygisk_libs(root_node *system) { vector *module_list; void load_modules() { - node_entry::mirror_dir = get_magisk_tmp() + "/"s MIRRDIR; node_entry::module_mnt = get_magisk_tmp() + "/"s MODULEMNT "/"; auto root = make_unique(""); diff --git a/native/src/core/node.hpp b/native/src/core/node.hpp index f0969cc4986a..b34d40b91f0a 100644 --- a/native/src/core/node.hpp +++ b/native/src/core/node.hpp @@ -5,18 +5,16 @@ using namespace std; -#define TYPE_MIRROR (1 << 0) /* mount from mirror */ -#define TYPE_INTER (1 << 1) /* intermediate node */ -#define TYPE_TMPFS (1 << 2) /* replace with tmpfs */ -#define TYPE_MODULE (1 << 3) /* mount from module */ -#define TYPE_ROOT (1 << 4) /* partition root */ -#define TYPE_CUSTOM (1 << 5) /* custom node type overrides all */ +#define TYPE_INTER (1 << 0) /* intermediate node */ +#define TYPE_TMPFS (1 << 1) /* replace with tmpfs */ +#define TYPE_MODULE (1 << 2) /* mount from module */ +#define TYPE_ROOT (1 << 3) /* partition root */ +#define TYPE_CUSTOM (1 << 4) /* custom node type overrides all */ #define TYPE_DIR (TYPE_INTER|TYPE_TMPFS|TYPE_ROOT) class node_entry; class dir_node; class inter_node; -class mirror_node; class tmpfs_node; class module_node; class root_node; @@ -27,7 +25,6 @@ template static T *dyn_cast(node_entry *node); template uint8_t type_id() { return TYPE_CUSTOM; } template<> uint8_t type_id() { return TYPE_DIR; } template<> uint8_t type_id() { return TYPE_INTER; } -template<> uint8_t type_id() { return TYPE_MIRROR; } template<> uint8_t type_id() { return TYPE_TMPFS; } template<> uint8_t type_id() { return TYPE_MODULE; } template<> uint8_t type_id() { return TYPE_ROOT; } @@ -47,12 +44,9 @@ class node_entry { const string &node_path(); const string worker_path(); - string mirror_path() { return mirror_dir + node_path(); } - virtual void mount() = 0; inline static string module_mnt; - inline static string mirror_dir; protected: template @@ -174,6 +168,12 @@ class dir_node : public node_entry { _root = self; } + template + dir_node(dirent *entry, T *self) : node_entry(entry->d_name, entry->d_type, self) { + if constexpr (std::is_same_v) + _root = self; + } + template dir_node(node_entry *node, T *self) : node_entry(self) { if constexpr (std::is_same_v) @@ -192,8 +192,8 @@ class dir_node : public node_entry { // Use bit 6 of _file_type // Skip binding mirror for this directory - bool skip_mirror() const { return static_cast(_file_type & (1 << 6)); } - void set_skip_mirror(bool b) { if (b) _file_type |= (1 << 6); else _file_type &= ~(1 << 6); } + bool replace() const { return static_cast(_file_type & (1 << 6)); } + void set_replace(bool b) { if (b) _file_type |= (1 << 6); else _file_type &= ~(1 << 6); } template T *iterator_to_node(iterator it) { @@ -273,6 +273,7 @@ class root_node : public dir_node { class inter_node : public dir_node { public: inter_node(const char *name) : dir_node(name, this) {} + inter_node(dirent *entry) : dir_node(entry, this) {} }; class module_node : public node_entry { @@ -289,13 +290,7 @@ class module_node : public node_entry { const char *module; }; -// Don't create the following two nodes before prepare -class mirror_node : public node_entry { -public: - explicit mirror_node(dirent *entry) : node_entry(entry->d_name, entry->d_type, this) {} - void mount() override; -}; - +// Don't create tmpfs_node before prepare class tmpfs_node : public dir_node { public: explicit tmpfs_node(node_entry *node); From f6f33474954580fddbcc34390ce2c2125a65ae6b Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Fri, 5 Jan 2024 01:47:15 +0800 Subject: [PATCH 2/5] Rename dir name --- app/src/main/res/raw/manager.sh | 2 +- native/src/core/bootstages.cpp | 1 + native/src/include/consts.hpp | 8 ++++---- native/src/include/consts.rs | 2 +- native/src/init/mount.cpp | 18 +++++++----------- native/src/init/rootdir.cpp | 9 ++++----- scripts/avd_magisk.sh | 4 +--- 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/app/src/main/res/raw/manager.sh b/app/src/main/res/raw/manager.sh index 37bb5b35cbb3..1d2342909b82 100644 --- a/app/src/main/res/raw/manager.sh +++ b/app/src/main/res/raw/manager.sh @@ -14,7 +14,7 @@ env_check() { [ -f "$MAGISKBIN/magiskpolicy" ] || return 1 fi if [ "$2" -ge 25210 ]; then - [ -b "$MAGISKTMP/.magisk/block/preinit" ] || return 2 + [ -b "$MAGISKTMP/.magisk/device/preinit" ] || [ -b "$MAGISKTMP/.magisk/block/preinit" ] || return 2 fi grep -xqF "MAGISK_VER='$1'" "$MAGISKBIN/util_functions.sh" || return 3 grep -xqF "MAGISK_VER_CODE=$2" "$MAGISKBIN/util_functions.sh" || return 3 diff --git a/native/src/core/bootstages.cpp b/native/src/core/bootstages.cpp index 4ba39438925c..19586bf34e1f 100644 --- a/native/src/core/bootstages.cpp +++ b/native/src/core/bootstages.cpp @@ -72,6 +72,7 @@ static void setup_mounts() { // Prepare worker ssprintf(path, sizeof(path), "%s/" WORKERDIR, get_magisk_tmp()); + xmkdir(path, 0); xmount("worker", path, "tmpfs", 0, "mode=755"); xmount(nullptr, path, nullptr, MS_PRIVATE, nullptr); } diff --git a/native/src/include/consts.hpp b/native/src/include/consts.hpp index 7df3f96a65ee..4dd7952c2d65 100644 --- a/native/src/include/consts.hpp +++ b/native/src/include/consts.hpp @@ -13,8 +13,8 @@ #define INTLROOT ".magisk" #define MIRRDIR INTLROOT "/mirror" #define PREINITMIRR INTLROOT "/preinit" -#define BLOCKDIR INTLROOT "/block" -#define PREINITDEV BLOCKDIR "/preinit" +#define DEVICEDIR INTLROOT "/device" +#define PREINITDEV DEVICEDIR "/preinit" #define WORKERDIR INTLROOT "/worker" #define MODULEMNT INTLROOT "/modules" #define BBPATH INTLROOT "/busybox" @@ -23,8 +23,8 @@ #define ROOTMNT ROOTOVL "/.mount_list" #define SELINUXMOCK INTLROOT "/selinux" #define MAIN_CONFIG INTLROOT "/config" -#define MAIN_SOCKET INTLROOT "/socket" -#define LOG_PIPE INTLROOT "/log" +#define MAIN_SOCKET DEVICEDIR "/socket" +#define LOG_PIPE DEVICEDIR "/log" constexpr const char *applet_names[] = { "su", "resetprop", nullptr }; diff --git a/native/src/include/consts.rs b/native/src/include/consts.rs index 19fc45606708..ac5ec6b8e5cc 100644 --- a/native/src/include/consts.rs +++ b/native/src/include/consts.rs @@ -17,7 +17,7 @@ macro_rules! INTLROOT { #[macro_export] macro_rules! LOG_PIPE { () => { - concat!($crate::INTLROOT!(), "/log") + concat!($crate::INTLROOT!(), "/device/log") }; } diff --git a/native/src/init/mount.cpp b/native/src/init/mount.cpp index 63276901ceba..b00a836a3153 100644 --- a/native/src/init/mount.cpp +++ b/native/src/init/mount.cpp @@ -100,8 +100,6 @@ static dev_t setup_block() { return 0; } -#define PREINITMNT MIRRDIR "/preinit" - static void mount_preinit_dir(string preinit_dev) { if (preinit_dev.empty()) return; strcpy(blk_info.partname, preinit_dev.data()); @@ -111,13 +109,13 @@ static void mount_preinit_dir(string preinit_dev) { LOGE("Cannot find preinit %s, abort!\n", preinit_dev.data()); return; } - xmkdir(PREINITMNT, 0); + xmkdir(MIRRDIR, 0); bool mounted = false; // First, find if it is already mounted std::string mnt_point; if (rust::is_device_mounted(dev, mnt_point)) { // Already mounted, just bind mount - xmount(mnt_point.data(), PREINITMNT, nullptr, MS_BIND, nullptr); + xmount(mnt_point.data(), MIRRDIR, nullptr, MS_BIND, nullptr); mounted = true; } @@ -125,9 +123,9 @@ static void mount_preinit_dir(string preinit_dev) { // as read-only, or else the kernel might crash due to crappy drivers. // After the device boots up, magiskd will properly bind mount the correct partition // on to PREINITMIRR as writable. For more details, check bootstages.cpp - if (mounted || mount(PREINITDEV, PREINITMNT, "ext4", MS_RDONLY, nullptr) == 0 || - mount(PREINITDEV, PREINITMNT, "f2fs", MS_RDONLY, nullptr) == 0) { - string preinit_dir = resolve_preinit_dir(PREINITMNT); + if (mounted || mount(PREINITDEV, MIRRDIR, "ext4", MS_RDONLY, nullptr) == 0 || + mount(PREINITDEV, MIRRDIR, "f2fs", MS_RDONLY, nullptr) == 0) { + string preinit_dir = resolve_preinit_dir(MIRRDIR); // Create bind mount xmkdirs(PREINITMIRR, 0); if (access(preinit_dir.data(), F_OK)) { @@ -136,7 +134,7 @@ static void mount_preinit_dir(string preinit_dev) { LOGD("preinit: %s\n", preinit_dir.data()); xmount(preinit_dir.data(), PREINITMIRR, nullptr, MS_BIND, nullptr); } - xumount2(PREINITMNT, MNT_DETACH); + xumount2(MIRRDIR, MNT_DETACH); } else { PLOGE("Failed to mount preinit %s\n", preinit_dev.data()); unlink(PREINITDEV); @@ -237,9 +235,7 @@ void MagiskInit::setup_tmp(const char *path) { chdir("/data"); xmkdir(INTLROOT, 0711); - xmkdir(MIRRDIR, 0); - xmkdir(BLOCKDIR, 0); - xmkdir(WORKERDIR, 0); + xmkdir(DEVICEDIR, 0711); mount_preinit_dir(preinit_dev); diff --git a/native/src/init/rootdir.cpp b/native/src/init/rootdir.cpp index 6182da6a62e0..44e410a6afe9 100644 --- a/native/src/init/rootdir.cpp +++ b/native/src/init/rootdir.cpp @@ -13,7 +13,6 @@ using namespace std; static vector rc_list; -#define ROOTMIR MIRRDIR "/system_root" #define NEW_INITRC_DIR "/system/etc/init/hw" #define INIT_RC "init.rc" @@ -250,10 +249,10 @@ void MagiskInit::patch_ro_root() { if (tmp_dir == "/sbin") { // Recreate original sbin structure - xmkdir(ROOTMIR, 0755); - xmount("/", ROOTMIR, nullptr, MS_BIND, nullptr); - recreate_sbin(ROOTMIR "/sbin", true); - xumount2(ROOTMIR, MNT_DETACH); + xmkdir(MIRRDIR, 0755); + xmount("/", MIRRDIR, nullptr, MS_BIND, nullptr); + recreate_sbin(MIRRDIR "/sbin", true); + xumount2(MIRRDIR, MNT_DETACH); } else { // Restore debug_ramdisk xmount("/data/debug_ramdisk", "/debug_ramdisk", nullptr, MS_MOVE, nullptr); diff --git a/scripts/avd_magisk.sh b/scripts/avd_magisk.sh index 2b6ba4702bc2..1746a3493ef2 100755 --- a/scripts/avd_magisk.sh +++ b/scripts/avd_magisk.sh @@ -138,9 +138,7 @@ ln -s ./magisk $MAGISKTMP/su ln -s ./magisk $MAGISKTMP/resetprop ln -s ./magiskpolicy $MAGISKTMP/supolicy -mkdir -p $MAGISKTMP/.magisk/mirror -mkdir $MAGISKTMP/.magisk/block -mkdir $MAGISKTMP/.magisk/worker +mkdir -p $MAGISKTMP/.magisk/device touch $MAGISKTMP/.magisk/config export MAGISKTMP From be75408d6acc79102fe9e97d867f2e2865fd1f44 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Thu, 22 Feb 2024 22:19:31 +0800 Subject: [PATCH 3/5] No need extra tmpfs for worker --- native/src/core/bootstages.cpp | 2 +- native/src/core/deny/revert.cpp | 2 +- native/src/core/module.cpp | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/native/src/core/bootstages.cpp b/native/src/core/bootstages.cpp index 19586bf34e1f..6a2ea6126dbc 100644 --- a/native/src/core/bootstages.cpp +++ b/native/src/core/bootstages.cpp @@ -73,7 +73,7 @@ static void setup_mounts() { // Prepare worker ssprintf(path, sizeof(path), "%s/" WORKERDIR, get_magisk_tmp()); xmkdir(path, 0); - xmount("worker", path, "tmpfs", 0, "mode=755"); + xmount(path, path, nullptr, MS_BIND, nullptr); xmount(nullptr, path, nullptr, MS_PRIVATE, nullptr); } diff --git a/native/src/core/deny/revert.cpp b/native/src/core/deny/revert.cpp index 90dd206cabf2..e6c56ebd2c17 100644 --- a/native/src/core/deny/revert.cpp +++ b/native/src/core/deny/revert.cpp @@ -20,7 +20,7 @@ void revert_unmount() { // Unmount dummy skeletons and MAGISKTMP // since mirror nodes are always mounted under skeleton, we don't have to specifically unmount for (auto &info: parse_mount_info("self")) { - if (info.source == "magisk" || info.source == "worker" || // magisktmp tmpfs + if (info.source == "magisk" || // magisktmp tmpfs info.root.starts_with("/adb/modules")) { // bind mount from data partition targets.insert(info.target); } diff --git a/native/src/core/module.cpp b/native/src/core/module.cpp index 64c9328c1b11..6b3b750dce9c 100644 --- a/native/src/core/module.cpp +++ b/native/src/core/module.cpp @@ -162,6 +162,7 @@ void tmpfs_node::mount() { dir_node::mount(); VLOGD(replace() ? "replace" : "move", worker_dir.data(), node_path().data()); xmount(worker_dir.data(), node_path().data(), nullptr, MS_MOVE, nullptr); + xmount(nullptr, node_path().data(), nullptr, MS_REMOUNT | MS_BIND | MS_RDONLY, nullptr); } else { const string dest = worker_path(); // We don't need another layer of tmpfs if parent is tmpfs @@ -326,8 +327,11 @@ void load_modules() { root->mount(); } + // cleanup mounts ssprintf(buf, sizeof(buf), "%s/" WORKERDIR, get_magisk_tmp()); - xmount(nullptr, buf, nullptr, MS_REMOUNT | MS_RDONLY, nullptr); + xumount2(buf, MNT_DETACH); + ssprintf(buf, sizeof(buf), "%s/" MODULEMNT, get_magisk_tmp()); + xumount2(buf, MNT_DETACH); } /************************ From 9f2a6205470b70adefc8069f5bb7890f6bd354fe Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Fri, 23 Feb 2024 04:08:36 +0800 Subject: [PATCH 4/5] No need to unshare --- native/src/core/daemon.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/native/src/core/daemon.cpp b/native/src/core/daemon.cpp index a944cebd3bf6..3fbf32eb9747 100644 --- a/native/src/core/daemon.cpp +++ b/native/src/core/daemon.cpp @@ -391,13 +391,6 @@ static void daemon_entry() { ssprintf(path, sizeof(path), "%s/" ROOTOVL, tmp); rm_rf(path); - // Unshare magiskd - xunshare(CLONE_NEWNS); - // Hide magisk internal mount point - xmount(nullptr, tmp, nullptr, MS_PRIVATE | MS_REC, nullptr); - // Fix sdcardfs bug on old kernel - xmount(nullptr, "/mnt", nullptr, MS_SLAVE | MS_REC, nullptr); - // Use isolated devpts if kernel support if (access("/dev/pts/ptmx", F_OK) == 0) { ssprintf(path, sizeof(path), "%s/" SHELLPTS, tmp); From 2c15d769bf780856f4ebc37464b9788531669b09 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Fri, 5 Jan 2024 18:39:59 +0800 Subject: [PATCH 5/5] Let magic mount optional --- .../java/com/topjohnwu/magisk/core/App.kt | 3 + native/src/core/bootstages.cpp | 60 ++++++++++--------- scripts/avd_test.sh | 4 +- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/App.kt b/app/src/main/java/com/topjohnwu/magisk/core/App.kt index 949b513534dc..6519533cf85a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/App.kt @@ -5,6 +5,7 @@ import android.app.Application import android.content.Context import android.content.res.Configuration import android.os.Bundle +import android.system.Os import androidx.profileinstaller.ProfileInstaller import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.StubApk @@ -46,6 +47,8 @@ open class App() : Application() { Timber.e(e) exitProcess(1) } + + Os.setenv("PATH", "${Os.getenv("PATH")}:/debug_ramdisk:/sbin", true) } override fun attachBaseContext(context: Context) { diff --git a/native/src/core/bootstages.cpp b/native/src/core/bootstages.cpp index 6a2ea6126dbc..36d8b136c8b2 100644 --- a/native/src/core/bootstages.cpp +++ b/native/src/core/bootstages.cpp @@ -25,16 +25,11 @@ static void setup_mounts() { char path[PATH_MAX]; // Bind remount module root to clear nosuid - if (access(SECURE_DIR, F_OK) == 0 || SDK_INT < 24) { - ssprintf(path, sizeof(path), "%s/" MODULEMNT, get_magisk_tmp()); - xmkdir(SECURE_DIR, 0700); - xmkdir(MODULEROOT, 0755); - xmkdir(path, 0755); - xmount(MODULEROOT, path, nullptr, MS_BIND, nullptr); - xmount(nullptr, path, nullptr, MS_REMOUNT | MS_BIND | MS_RDONLY, nullptr); - xmount(nullptr, path, nullptr, MS_PRIVATE, nullptr); - chmod(SECURE_DIR, 0700); - } + ssprintf(path, sizeof(path), "%s/" MODULEMNT, get_magisk_tmp()); + xmkdir(path, 0755); + xmount(MODULEROOT, path, nullptr, MS_BIND, nullptr); + xmount(nullptr, path, nullptr, MS_REMOUNT | MS_BIND | MS_RDONLY, nullptr); + xmount(nullptr, path, nullptr, MS_PRIVATE, nullptr); // Check and mount preinit mirror char dev_path[64]; @@ -181,10 +176,10 @@ static bool magisk_env() { LOGI("* Initializing Magisk environment\n"); - preserve_stub_apk(); - // Directories in /data/adb + chmod(SECURE_DIR, 0700); xmkdir(DATABIN, 0755); + xmkdir(MODULEROOT, 0755); xmkdir(SECURE_DIR "/post-fs-data.d", 0755); xmkdir(SECURE_DIR "/service.d", 0755); restorecon(); @@ -275,45 +270,54 @@ static bool check_key_combo() { * Boot Stage Handlers * ***********************/ -extern int disable_deny(); +static void disable_zygisk() { + char sql[64]; + sprintf(sql, "REPLACE INTO settings (key,value) VALUES('%s',%d)", + DB_SETTING_KEYS[ZYGISK_CONFIG], false); + char *err = db_exec(sql); + db_err(err); +} bool MagiskD::post_fs_data() const { as_rust().setup_logfile(); LOGI("** post-fs-data mode running\n"); - unlock_blocks(); - setup_mounts(); + preserve_stub_apk(); prune_su_access(); bool safe_mode = false; if (access(SECURE_DIR, F_OK) != 0) { - LOGE(SECURE_DIR " is not present, abort\n"); - goto early_abort; + if (SDK_INT < 24) { + xmkdir(SECURE_DIR, 0700); + } else { + LOGE(SECURE_DIR " is not present, abort\n"); + return safe_mode; + } } if (!magisk_env()) { LOGE("* Magisk environment incomplete, abort\n"); - goto early_abort; + return safe_mode; } if (get_prop("persist.sys.safemode", true) == "1" || get_prop("ro.sys.safemode") == "1" || check_key_combo()) { safe_mode = true; - // Disable all modules and denylist so next boot will be clean + // Disable all modules and zygisk so next boot will be clean disable_modules(); - disable_deny(); - } else { - exec_common_scripts("post-fs-data"); - db_settings dbs; - get_db_settings(dbs, ZYGISK_CONFIG); - zygisk_enabled = dbs[ZYGISK_CONFIG]; - initialize_denylist(); - handle_modules(); + disable_zygisk(); + return safe_mode; } -early_abort: + exec_common_scripts("post-fs-data"); + db_settings dbs; + get_db_settings(dbs, ZYGISK_CONFIG); + zygisk_enabled = dbs[ZYGISK_CONFIG]; + initialize_denylist(); + setup_mounts(); + handle_modules(); load_modules(); return safe_mode; } diff --git a/scripts/avd_test.sh b/scripts/avd_test.sh index 4f18693446a4..94f7b5c8ddd4 100755 --- a/scripts/avd_test.sh +++ b/scripts/avd_test.sh @@ -138,7 +138,7 @@ test_emu() { emu_pid=$! wait_emu wait_for_boot - adb shell magisk -v + adb shell 'PATH=$PATH:/debug_ramdisk magisk -v' # Install the Magisk app adb install -r -g out/app-${variant}.apk @@ -149,7 +149,7 @@ test_emu() { # Install LSPosed if [ $api -ge $lsposed_min_api -a $api -le $atd_max_api ]; then adb push out/lsposed.zip /data/local/tmp/lsposed.zip - adb shell echo 'magisk --install-module /data/local/tmp/lsposed.zip' \| /system/xbin/su + adb shell echo 'PATH=$PATH:/debug_ramdisk magisk --install-module /data/local/tmp/lsposed.zip' \| /system/xbin/su fi adb reboot