Add new util function

This commit is contained in:
topjohnwu 2019-06-23 03:53:41 -07:00
parent 1058aeb04f
commit 4cc7aced15
4 changed files with 38 additions and 33 deletions

View File

@ -307,13 +307,13 @@ static int bind_mount(const char *from, const char *to, bool log) {
#define MIRRMNT(part) MIRRDIR "/" #part #define MIRRMNT(part) MIRRDIR "/" #part
#define PARTBLK(part) BLOCKDIR "/" #part #define PARTBLK(part) BLOCKDIR "/" #part
#define DIR_IS(part) (me->mnt_dir == "/" #part ""sv)
#define mount_mirror(part, flag) { \ #define mount_mirror(part, flag) { \
sscanf(line.data(), "%s %*s %s", buf, buf2); \ xstat(me->mnt_fsname, &st); \
xstat(buf, &st); \
mknod(PARTBLK(part), (st.st_mode & S_IFMT) | 0600, st.st_rdev); \ mknod(PARTBLK(part), (st.st_mode & S_IFMT) | 0600, st.st_rdev); \
xmkdir(MIRRMNT(part), 0755); \ xmkdir(MIRRMNT(part), 0755); \
xmount(PARTBLK(part), MIRRMNT(part), buf2, flag, nullptr); \ xmount(PARTBLK(part), MIRRMNT(part), me->mnt_type, flag, nullptr); \
VLOGI("mount", PARTBLK(part), MIRRMNT(part)); \ VLOGI("mount", PARTBLK(part), MIRRMNT(part)); \
} }
@ -364,21 +364,19 @@ static bool magisk_env() {
LOGI("* Mounting mirrors"); LOGI("* Mounting mirrors");
bool system_as_root = false; bool system_as_root = false;
struct stat st; struct stat st;
file_readline("/proc/mounts", [&](string_view line) -> bool { parse_mnt("/proc/mounts", [&](mntent *me) {
if (str_contains(line, " /system_root ")) { if (DIR_IS(system_root)) {
mount_mirror(system_root, MS_RDONLY); mount_mirror(system_root, MS_RDONLY);
xsymlink(MIRRMNT(system_root) "/system", MIRRMNT(system)); xsymlink(MIRRMNT(system_root) "/system", MIRRMNT(system));
VLOGI("link", MIRRMNT(system_root) "/system", MIRRMNT(system)); VLOGI("link", MIRRMNT(system_root) "/system", MIRRMNT(system));
system_as_root = true; system_as_root = true;
} else if (!system_as_root && str_contains(line, " /system ")) { } else if (!system_as_root && DIR_IS(system)) {
mount_mirror(system, MS_RDONLY); mount_mirror(system, MS_RDONLY);
} else if (str_contains(line, " /vendor ")) { } else if (DIR_IS(vendor)) {
mount_mirror(vendor, MS_RDONLY); mount_mirror(vendor, MS_RDONLY);
} else if (str_contains(line, " /data ") && !str_contains(line, "tmpfs")) { } else if (DIR_IS(data) && me->mnt_type != "tmpfs"sv) {
mount_mirror(data, 0); mount_mirror(data, 0);
} else if (SDK_INT >= 24 && } else if (SDK_INT >= 24 && DIR_IS(proc) && !strstr(me->mnt_opts, "hidepid=2")) {
str_contains(line, " /proc ") && !str_contains(line, "hidepid=2")) {
// Enforce hidepid
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009"); xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
} }
return true; return true;

View File

@ -44,6 +44,9 @@ void hide_daemon(int pid) {
hide_unmount(pid); hide_unmount(pid);
} }
#define TMPFS_MNT(dir) (mentry->mnt_type == "tmpfs"sv && \
strncmp(mentry->mnt_dir, "/" #dir, sizeof("/" #dir) - 1) == 0)
void hide_unmount(int pid) { void hide_unmount(int pid) {
if (switch_mnt_ns(pid)) if (switch_mnt_ns(pid))
return; return;
@ -60,7 +63,7 @@ void hide_unmount(int pid) {
chmod(SELINUX_POLICY, 0440); chmod(SELINUX_POLICY, 0440);
} }
getprop([](const char *name, auto, auto) -> void { getprop([](const char *name, auto, auto) {
if (strstr(name, "magisk")) if (strstr(name, "magisk"))
deleteprop(name); deleteprop(name);
}, nullptr, false); }, nullptr, false);
@ -68,14 +71,9 @@ void hide_unmount(int pid) {
vector<string> targets; vector<string> targets;
// Unmount dummy skeletons and /sbin links // Unmount dummy skeletons and /sbin links
file_readline("/proc/self/mounts", [&](string_view s) -> bool { parse_mnt("/proc/self/mounts", [&](mntent *mentry) {
if (str_contains(s, "tmpfs /system/") || str_contains(s, "tmpfs /vendor/") || if (TMPFS_MNT(system) || TMPFS_MNT(vendor) || TMPFS_MNT(sbin))
str_contains(s, "tmpfs /sbin")) { targets.emplace_back(mentry->mnt_dir);
char *path = (char *) s.data();
// Skip first token
strtok_r(nullptr, " ", &path);
targets.emplace_back(strtok_r(nullptr, " ", &path));
}
return true; return true;
}); });
@ -84,13 +82,9 @@ void hide_unmount(int pid) {
targets.clear(); targets.clear();
// Unmount all Magisk created mounts // Unmount all Magisk created mounts
file_readline("/proc/self/mounts", [&](string_view s) -> bool { parse_mnt("/proc/self/mounts", [&](mntent *mentry) {
if (str_contains(s, BLOCKDIR)) { if (strstr(mentry->mnt_fsname, BLOCKDIR))
char *path = (char *) s.data(); targets.emplace_back(mentry->mnt_dir);
// Skip first token
strtok_r(nullptr, " ", &path);
targets.emplace_back(strtok_r(nullptr, " ", &path));
}
return true; return true;
}); });

View File

@ -1,15 +1,15 @@
/* file.cpp - Contains all files related utilities /* file.cpp - Contains all files related utilities
*/ */
#include <sys/sendfile.h>
#include <sys/mman.h>
#include <linux/fs.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <libgen.h> #include <libgen.h>
#include <sys/sendfile.h>
#include <sys/mman.h>
#include <linux/fs.h>
#include <magisk.h> #include <magisk.h>
#include <utils.h> #include <utils.h>
@ -396,3 +396,14 @@ void parse_prop_file(const char *file, const function<bool (string_view, string_
return fn(line, eql + 1); return fn(line, eql + 1);
}, true); }, true);
} }
void parse_mnt(const char *file, const function<bool (mntent*)> &fn) {
unique_ptr<FILE, decltype(&fclose)> fp(xfopen(file, "rce"), &fclose);
if (fp) {
mntent *mentry;
while ((mentry = getmntent(fp.get())) != nullptr) {
if (!fn(mentry))
break;
}
}
}

View File

@ -3,13 +3,14 @@
#pragma once #pragma once
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdio.h> #include <stdio.h>
#include <dirent.h> #include <dirent.h>
#include <pthread.h> #include <pthread.h>
#include <poll.h> #include <poll.h>
#include <sys/socket.h> #include <mntent.h>
#include <sys/stat.h>
#include <sys/mman.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -173,6 +174,7 @@ void parse_prop_file(const char *file, const std::function
void *__mmap(const char *filename, size_t *size, bool rw); void *__mmap(const char *filename, size_t *size, bool rw);
void frm_rf(int dirfd, std::initializer_list<const char *> excl = std::initializer_list<const char *>()); void frm_rf(int dirfd, std::initializer_list<const char *> excl = std::initializer_list<const char *>());
void clone_dir(int src, int dest, bool overwrite = true); void clone_dir(int src, int dest, bool overwrite = true);
void parse_mnt(const char *file, const std::function<bool (mntent*)> &fn);
template <typename B> template <typename B>
void mmap_ro(const char *filename, B &buf, size_t &sz) { void mmap_ro(const char *filename, B &buf, size_t &sz) {