Skip to content

Commit

Permalink
Added mountinfo_root_resolver and removed...
Browse files Browse the repository at this point in the history
mount_parser.
  • Loading branch information
snake-4 committed Apr 28, 2024
1 parent 2a31d65 commit 7cbb41b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 158 deletions.
2 changes: 1 addition & 1 deletion module/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/elfio
LOCAL_MODULE := zygisk
LOCAL_SRC_FILES := utils.cpp map_parser.cpp mount_parser.cpp mountinfo_parser.cpp modules.cpp main.cpp
LOCAL_SRC_FILES := utils.cpp map_parser.cpp mountinfo_parser.cpp modules.cpp main.cpp
LOCAL_STATIC_LIBRARIES := libcxx
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Expand Down
28 changes: 0 additions & 28 deletions module/jni/include/mount_parser.hpp

This file was deleted.

19 changes: 15 additions & 4 deletions module/jni/include/mountinfo_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
#include <string>
#include <vector>
#include <unordered_map>
#include <sys/types.h>

namespace Parsers
{
class mountinfo_entry_t
{
public:
mountinfo_entry_t(int mount_id, int parent_id, int major, int minor,
mountinfo_entry_t(int mount_id, int parent_id, dev_t device,
const std::string &root, const std::string &mount_point,
const std::string &mount_options, const std::string &optional_fields,
const std::string &filesystem_type, const std::string &mount_source,
const std::string &super_options);

int getMountId() const;
int getParentId() const;
int getMajor() const;
int getMinor() const;
dev_t getDevice() const;
const std::string &getRoot() const;
const std::string &getMountPoint() const;
const std::unordered_map<std::string, std::string> &getMountOptions() const;
Expand All @@ -27,10 +27,21 @@ namespace Parsers
const std::unordered_map<std::string, std::string> &getSuperOptions() const;

private:
int mount_id, parent_id, major, minor;
dev_t device;
int mount_id, parent_id;
std::string root, mount_point, optional_fields, filesystem_type, mount_source;
std::unordered_map<std::string, std::string> mount_options, super_options;
};

const std::vector<mountinfo_entry_t> &parseSelfMountinfo(bool cached = true);

class mountinfo_root_resolver
{
public:
mountinfo_root_resolver(const std::vector<mountinfo_entry_t> &mount_infos);
std::string resolveRootOf(const mountinfo_entry_t &mount_info) const;

private:
std::unordered_map<dev_t, std::string> device_mount_map;
};
}
80 changes: 29 additions & 51 deletions module/jni/modules.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <string>
#include <vector>
#include <set>
#include <unordered_map>
#include <cstdint>
Expand All @@ -9,10 +8,11 @@
#include "zygisk.hpp"
#include "logging.hpp"
#include "map_parser.hpp"
#include "mount_parser.hpp"
#include "mountinfo_parser.hpp"
#include "utils.hpp"

using namespace Parsers;

static const std::set<std::string> fsname_list = {"KSU", "APatch", "magisk", "worker"};
static const std::unordered_map<std::string, int> mount_flags_procfs = {
{"nosuid", MS_NOSUID},
Expand All @@ -23,31 +23,29 @@ static const std::unordered_map<std::string, int> mount_flags_procfs = {
{"relatime", MS_RELATIME},
{"nosymfollow", MS_NOSYMFOLLOW}};

static bool shouldUnmount(const Parsers::mountinfo_entry_t &mount_info)
static bool shouldUnmount(const mountinfo_entry_t &mount, const mountinfo_root_resolver &root_resolver)
{
const auto &root = mount_info.getRoot();

// Unmount all module bind mounts
return root.starts_with("/adb/");
}
const auto true_root = root_resolver.resolveRootOf(mount);
const auto &mount_point = mount.getMountPoint();
const auto &type = mount.getFilesystemType();

static bool shouldUnmount(const Parsers::mount_entry_t &mount)
{
const auto &mountPoint = mount.getMountPoint();
const auto &type = mount.getType();
const auto &options = mount.getOptions();
// Mount is from /data/adb
if (true_root.starts_with("/data/adb"))
return true;

// Unmount everything mounted to /data/adb
if (mountPoint.starts_with("/data/adb"))
// Mount is to /data/adb
if (mount_point.starts_with("/data/adb"))
return true;

// Unmount all module overlayfs and tmpfs
if ((type == "overlay" || type == "tmpfs") && fsname_list.contains(mount.getFsName()))
if ((type == "overlay" || type == "tmpfs") && fsname_list.contains(mount.getMountSource()))
return true;

// Unmount all overlayfs with lowerdir/upperdir/workdir starting with /data/adb
if (type == "overlay")
{
const auto &options = mount.getSuperOptions();

if (options.contains("lowerdir") && options.at("lowerdir").starts_with("/data/adb"))
return true;

Expand All @@ -63,58 +61,38 @@ static bool shouldUnmount(const Parsers::mount_entry_t &mount)

void doUnmount()
{
std::vector<std::string> mountPoints;

// Check mounts first
for (const auto &mount : Parsers::parseSelfMounts(false))
{
if (shouldUnmount(mount))
{
mountPoints.push_back(mount.getMountPoint());
}
}
const auto &mount_infos = parseSelfMountinfo(false);
auto root_resolver = mountinfo_root_resolver(mount_infos);

// Check mountinfos so that we can find bind mounts as well
for (const auto &mount_info : Parsers::parseSelfMountinfo(false))
for (auto it = mount_infos.rbegin(); it != mount_infos.rend(); it++)
{
if (shouldUnmount(mount_info))
{
mountPoints.push_back(mount_info.getMountPoint());
}
}

// Sort by string lengths, descending
std::sort(mountPoints.begin(), mountPoints.end(), [](const auto &lhs, const auto &rhs)
{ return lhs.size() > rhs.size(); });

for (const auto &mountPoint : mountPoints)
{
if (umount2(mountPoint.c_str(), MNT_DETACH) == 0)
{
LOGD("umount2(\"%s\", MNT_DETACH) returned 0", mountPoint.c_str());
}
else
if (shouldUnmount(*it, root_resolver))
{
LOGW("umount2(\"%s\", MNT_DETACH) returned -1: %d (%s)", mountPoint.c_str(), errno, strerror(errno));
const auto &mount_point_cstr = it->getMountPoint().c_str();
if (umount2(mount_point_cstr, MNT_DETACH) == 0)
LOGD("umount2(\"%s\", MNT_DETACH) returned 0", mount_point_cstr);
else
LOGW("umount2(\"%s\", MNT_DETACH) returned -1: %d (%s)", mount_point_cstr, errno, strerror(errno));
}
}
}

void doRemount()
{
for (const auto &mount : Parsers::parseSelfMounts(false))
for (const auto &mount : parseSelfMountinfo(false))
{
if (mount.getMountPoint() == "/data")
{
const auto &options = mount.getOptions();
const auto &superOptions = mount.getSuperOptions();
const auto &mountOptions = mount.getMountOptions();

// If errors=remount-ro, remount it with errors=continue
if (options.contains("errors") && options.at("errors") == "remount-ro")
if (superOptions.contains("errors") && superOptions.at("errors") == "remount-ro")
{
unsigned long flags = MS_REMOUNT;
for (const auto &flagName : mount_flags_procfs)
{
if (options.contains(flagName.first))
if (mountOptions.contains(flagName.first))
flags |= flagName.second;
}

Expand Down Expand Up @@ -146,7 +124,7 @@ void doHideZygisk()
std::string filePath;
uintptr_t startAddress = 0, bssAddress = 0;

for (const auto &map : Parsers::parseSelfMaps())
for (const auto &map : parseSelfMaps())
{
if (map.getPathname().ends_with("/libnativebridge.so") && map.getPerms() == "r--p")
{
Expand Down
66 changes: 0 additions & 66 deletions module/jni/mount_parser.cpp

This file was deleted.

Loading

0 comments on commit 7cbb41b

Please sign in to comment.