mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-02 18:12:07 +00:00
Use mmap_data more widely
This commit is contained in:
@@ -252,7 +252,7 @@ bool check_two_stage() {
|
||||
if (access("/system/bin/init", F_OK) == 0)
|
||||
return true;
|
||||
// If we still have no indication, parse the original init and see what's up
|
||||
auto init = mmap_data::ro(backup_init());
|
||||
auto init = mmap_data(backup_init());
|
||||
return init.contains("selinux_setup");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#include <utils.hpp>
|
||||
|
||||
#include "raw_data.hpp"
|
||||
|
||||
using kv_pairs = std::vector<std::pair<std::string, std::string>>;
|
||||
|
||||
struct BootConfig {
|
||||
|
||||
@@ -286,7 +286,7 @@ success:
|
||||
}
|
||||
|
||||
void RootFSInit::early_mount() {
|
||||
self = mmap_data::ro("/init");
|
||||
self = mmap_data("/init");
|
||||
|
||||
LOGD("Restoring /init\n");
|
||||
rename(backup_init(), "/init");
|
||||
@@ -298,9 +298,9 @@ void SARBase::backup_files() {
|
||||
if (access("/overlay.d", F_OK) == 0)
|
||||
backup_folder("/overlay.d", overlays);
|
||||
|
||||
self = mmap_data::ro("/proc/self/exe");
|
||||
self = mmap_data("/proc/self/exe");
|
||||
if (access("/.backup/.magisk", R_OK) == 0)
|
||||
magisk_config = mmap_data::ro("/.backup/.magisk");
|
||||
magisk_config = mmap_data("/.backup/.magisk");
|
||||
}
|
||||
|
||||
void SARBase::mount_system_root() {
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
#include "raw_data.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int mmap_data::patch(str_pairs list) {
|
||||
if (buf == nullptr)
|
||||
return 0;
|
||||
int count = 0;
|
||||
for (uint8_t *p = buf, *eof = buf + sz; p < eof; ++p) {
|
||||
for (auto [from, to] : list) {
|
||||
if (memcmp(p, from.data(), from.length() + 1) == 0) {
|
||||
LOGD("Replace [%s] -> [%s]\n", from.data(), to.data());
|
||||
memset(p, 0, from.length());
|
||||
memcpy(p, to.data(), to.length());
|
||||
++count;
|
||||
p += from.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
bool mmap_data::contains(string_view pattern) {
|
||||
if (buf == nullptr)
|
||||
return false;
|
||||
for (uint8_t *p = buf, *eof = buf + sz; p < eof; ++p) {
|
||||
if (memcmp(p, pattern.data(), pattern.length() + 1) == 0) {
|
||||
LOGD("Found pattern [%s]\n", pattern.data());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void mmap_data::consume(mmap_data &other) {
|
||||
buf = other.buf;
|
||||
sz = other.sz;
|
||||
other.buf = nullptr;
|
||||
other.sz = 0;
|
||||
}
|
||||
|
||||
mmap_data mmap_data::rw(const char *name) {
|
||||
mmap_data data;
|
||||
mmap_rw(name, data.buf, data.sz);
|
||||
return data;
|
||||
}
|
||||
|
||||
mmap_data mmap_data::ro(const char *name) {
|
||||
mmap_data data;
|
||||
mmap_ro(name, data.buf, data.sz);
|
||||
return data;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <utils.hpp>
|
||||
|
||||
struct mmap_data {
|
||||
uint8_t *buf = nullptr;
|
||||
size_t sz = 0;
|
||||
|
||||
mmap_data() = default;
|
||||
mmap_data(const mmap_data&) = delete;
|
||||
mmap_data(mmap_data &&other) { consume(other); }
|
||||
~mmap_data() { if (buf) munmap(buf, sz); }
|
||||
mmap_data& operator=(mmap_data &&other) { consume(other); return *this; }
|
||||
|
||||
using str_pairs = std::initializer_list<std::pair<std::string_view, std::string_view>>;
|
||||
int patch(str_pairs list);
|
||||
bool contains(std::string_view pattern);
|
||||
|
||||
static mmap_data rw(const char *name);
|
||||
static mmap_data ro(const char *name);
|
||||
|
||||
private:
|
||||
void consume(mmap_data &other);
|
||||
};
|
||||
@@ -183,7 +183,7 @@ static void patch_socket_name(const char *path) {
|
||||
static char rstr[16] = { 0 };
|
||||
if (rstr[0] == '\0')
|
||||
gen_rand_str(rstr, sizeof(rstr));
|
||||
auto bin = mmap_data::rw(path);
|
||||
auto bin = mmap_data(path, true);
|
||||
bin.patch({ make_pair(MAIN_SOCKET, rstr) });
|
||||
}
|
||||
|
||||
@@ -224,7 +224,7 @@ void SARBase::patch_rootdir() {
|
||||
int patch_count;
|
||||
{
|
||||
int src = xopen("/init", O_RDONLY | O_CLOEXEC);
|
||||
auto init = mmap_data::ro("/init");
|
||||
auto init = mmap_data("/init");
|
||||
patch_count = init.patch({
|
||||
make_pair(SPLIT_PLAT_CIL, "xxx"), /* Force loading monolithic sepolicy */
|
||||
make_pair(MONOPOLICY, sepol) /* Redirect /sepolicy to custom path */
|
||||
@@ -248,8 +248,8 @@ void SARBase::patch_rootdir() {
|
||||
if (path) {
|
||||
char ovl[128];
|
||||
sprintf(ovl, ROOTOVL "%s", path);
|
||||
auto lib = mmap_data::ro(path);
|
||||
lib.patch({make_pair(MONOPOLICY, sepol)});
|
||||
auto lib = mmap_data(path);
|
||||
lib.patch({ make_pair(MONOPOLICY, sepol) });
|
||||
xmkdirs(dirname(ovl), 0755);
|
||||
int dest = xopen(ovl, O_CREAT | O_WRONLY | O_CLOEXEC, 0);
|
||||
xwrite(dest, lib.buf, lib.sz);
|
||||
@@ -290,19 +290,19 @@ void SARBase::patch_rootdir() {
|
||||
xmkdirs(dirname(ROOTOVL NEW_INITRC), 0755);
|
||||
patch_init_rc(NEW_INITRC, ROOTOVL NEW_INITRC, tmp_dir.data());
|
||||
} else {
|
||||
patch_init_rc("/init.rc", ROOTOVL "/init.rc", tmp_dir.data());
|
||||
patch_init_rc("/init.rc", ROOTOVL "/init.rc", tmp_dir.data());
|
||||
}
|
||||
|
||||
// Extract magisk
|
||||
{
|
||||
auto magisk = mmap_data::ro("magisk32.xz");
|
||||
auto magisk = mmap_data("magisk32.xz");
|
||||
unlink("magisk32.xz");
|
||||
int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755);
|
||||
unxz(fd, magisk.buf, magisk.sz);
|
||||
close(fd);
|
||||
patch_socket_name("magisk32");
|
||||
if (access("magisk64.xz", F_OK) == 0) {
|
||||
magisk = mmap_data::ro("magisk64.xz");
|
||||
magisk = mmap_data("magisk64.xz");
|
||||
unlink("magisk64.xz");
|
||||
fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755);
|
||||
unxz(fd, magisk.buf, magisk.sz);
|
||||
@@ -344,13 +344,13 @@ void RootFSBase::patch_rootfs() {
|
||||
|
||||
if (patch_sepolicy("/sepolicy")) {
|
||||
if (access("/system/bin/init", F_OK) == 0) {
|
||||
auto init = mmap_data::ro("/system/bin/init");
|
||||
auto init = mmap_data("/system/bin/init");
|
||||
init.patch({ make_pair(SPLIT_PLAT_CIL, "xxx") });
|
||||
int dest = xopen("/init", O_TRUNC | O_WRONLY | O_CLOEXEC, 0);
|
||||
xwrite(dest, init.buf, init.sz);
|
||||
close(dest);
|
||||
} else {
|
||||
auto init = mmap_data::rw("/init");
|
||||
auto init = mmap_data("/init", true);
|
||||
init.patch({ make_pair(SPLIT_PLAT_CIL, "xxx") });
|
||||
}
|
||||
}
|
||||
@@ -376,10 +376,10 @@ void MagiskProxy::start() {
|
||||
xmount(nullptr, "/", nullptr, MS_REMOUNT, nullptr);
|
||||
|
||||
// Backup stuffs before removing them
|
||||
self = mmap_data::ro("/sbin/magisk");
|
||||
magisk_config = mmap_data::ro("/.backup/.magisk");
|
||||
auto magisk = mmap_data::ro("/sbin/magisk32.xz");
|
||||
auto magisk64 = mmap_data::ro("/sbin/magisk64.xz");
|
||||
self = mmap_data("/sbin/magisk");
|
||||
magisk_config = mmap_data("/.backup/.magisk");
|
||||
auto magisk = mmap_data("/sbin/magisk32.xz");
|
||||
auto magisk64 = mmap_data("/sbin/magisk64.xz");
|
||||
char custom_rules_dir[64];
|
||||
custom_rules_dir[0] = '\0';
|
||||
xreadlink(TMP_RULESDIR, custom_rules_dir, sizeof(custom_rules_dir));
|
||||
|
||||
@@ -122,7 +122,7 @@ exit_loop:
|
||||
}
|
||||
|
||||
// Patch init to force IsDtFstabCompatible() return false
|
||||
auto init = mmap_data::rw("/init");
|
||||
auto init = mmap_data("/init", true);
|
||||
init.patch({ make_pair("android,fstab", "xxx") });
|
||||
} else {
|
||||
// Parse and load the fstab file
|
||||
@@ -192,7 +192,7 @@ void SARInit::first_stage_prep() {
|
||||
int src = xopen("/init", O_RDONLY);
|
||||
int dest = xopen("/dev/init", O_CREAT | O_WRONLY, 0);
|
||||
{
|
||||
auto init = mmap_data::ro("/init");
|
||||
auto init = mmap_data("/init");
|
||||
init.patch({ make_pair(INIT_PATH, REDIR_PATH) });
|
||||
write(dest, init.buf, init.sz);
|
||||
fclone_attr(src, dest);
|
||||
|
||||
Reference in New Issue
Block a user