You've already forked ReZygisk
mirror of
https://github.com/PerformanC/ReZygisk.git
synced 2025-09-06 06:37:01 +00:00
remove: open file helpers
This commit remove the `open_...` and `xopen_...` helpers, making the code simpler and more direct.
This commit is contained in:
@@ -25,12 +25,17 @@ void file_readline(bool trim, FILE *fp, const std::function<bool(std::string_vie
|
|||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_readline(bool trim, const char *file, const std::function<bool(std::string_view)> &fn) {
|
|
||||||
if (auto fp = open_file(file, "re"))
|
|
||||||
file_readline(trim, fp.get(), fn);
|
|
||||||
}
|
|
||||||
void file_readline(const char *file, const std::function<bool(std::string_view)> &fn) {
|
void file_readline(const char *file, const std::function<bool(std::string_view)> &fn) {
|
||||||
file_readline(false, file, fn);
|
FILE *fp = fopen(file, "re");
|
||||||
|
if (!fp) {
|
||||||
|
PLOGE("Failed to open file %s", file);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
file_readline(false, fp, fn);
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<mount_info> parse_mount_info(const char *pid) {
|
std::vector<mount_info> parse_mount_info(const char *pid) {
|
||||||
@@ -112,26 +117,3 @@ std::vector<mount_info> parse_mount_info(const char *pid) {
|
|||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
sDIR make_dir(DIR *dp) {
|
|
||||||
return sDIR(dp, [](DIR *dp){ return dp ? closedir(dp) : 1; });
|
|
||||||
}
|
|
||||||
|
|
||||||
sFILE make_file(FILE *fp) {
|
|
||||||
return sFILE(fp, [](FILE *fp){ return fp ? fclose(fp) : 1; });
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_path_from_fd(int fd, char *buf, size_t size) {
|
|
||||||
if (fd < 0 || !buf || size == 0) return -1;
|
|
||||||
|
|
||||||
/* NOTE: We assume that the path is always at /data/adb/modules/xxx
|
|
||||||
which should never be longer than 128 chars. */
|
|
||||||
char proc_path[128];
|
|
||||||
snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", fd);
|
|
||||||
|
|
||||||
ssize_t len = readlink(proc_path, buf, size - 1);
|
|
||||||
if (len == -1) return -1;
|
|
||||||
|
|
||||||
buf[len] = '\0';
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -21,39 +21,4 @@ struct mount_info {
|
|||||||
std::string fs_option;
|
std::string fs_option;
|
||||||
};
|
};
|
||||||
|
|
||||||
void file_readline(bool trim, FILE *fp, const std::function<bool(std::string_view)> &fn);
|
|
||||||
void file_readline(bool trim, const char *file, const std::function<bool(std::string_view)> &fn);
|
|
||||||
void file_readline(const char *file, const std::function<bool(std::string_view)> &fn);
|
|
||||||
|
|
||||||
std::vector<mount_info> parse_mount_info(const char *pid);
|
std::vector<mount_info> parse_mount_info(const char *pid);
|
||||||
|
|
||||||
int get_path_from_fd(int fd, char *buf, size_t size);
|
|
||||||
|
|
||||||
using sFILE = std::unique_ptr<FILE, decltype(&fclose)>;
|
|
||||||
using sDIR = std::unique_ptr<DIR, decltype(&closedir)>;
|
|
||||||
sDIR make_dir(DIR *dp);
|
|
||||||
sFILE make_file(FILE *fp);
|
|
||||||
|
|
||||||
static inline sDIR open_dir(const char *path) {
|
|
||||||
return make_dir(opendir(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline sDIR xopen_dir(const char *path) {
|
|
||||||
return make_dir(opendir(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline sDIR xopen_dir(int dirfd) {
|
|
||||||
return make_dir(fdopendir(dirfd));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline sFILE open_file(const char *path, const char *mode) {
|
|
||||||
return make_file(fopen(path, mode));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline sFILE xopen_file(const char *path, const char *mode) {
|
|
||||||
return make_file(fopen(path, mode));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline sFILE xopen_file(int fd, const char *mode) {
|
|
||||||
return make_file(fdopen(fd, mode));
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
#include <android/dlext.h>
|
|
||||||
#include <sys/mount.h>
|
#include <sys/mount.h>
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
@@ -10,9 +9,12 @@
|
|||||||
#include <lsplt.hpp>
|
#include <lsplt.hpp>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <sys/prctl.h>
|
#include <sys/prctl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "daemon.h"
|
#include "daemon.h"
|
||||||
@@ -487,25 +489,38 @@ int sigmask(int how, int signum) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ZygiskContext::fork_pre() {
|
void ZygiskContext::fork_pre() {
|
||||||
// Do our own fork before loading any 3rd party code
|
/* INFO: Do our own fork before loading any 3rd party code.
|
||||||
// First block SIGCHLD, unblock after original fork is done
|
First block SIGCHLD, unblock after original fork is done.
|
||||||
|
*/
|
||||||
sigmask(SIG_BLOCK, SIGCHLD);
|
sigmask(SIG_BLOCK, SIGCHLD);
|
||||||
pid = old_fork();
|
pid = old_fork();
|
||||||
if (pid != 0 || flags[SKIP_FD_SANITIZATION])
|
if (pid != 0 || flags[SKIP_FD_SANITIZATION])
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Record all open fds
|
/* INFO: Record all open fds */
|
||||||
auto dir = xopen_dir("/proc/self/fd");
|
DIR *dir = opendir("/proc/self/fd");
|
||||||
for (dirent *entry; (entry = readdir(dir.get()));) {
|
if (dir == nullptr) {
|
||||||
|
PLOGE("Failed to open /proc/self/fd");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dirent *entry;
|
||||||
|
while ((entry = readdir(dir))) {
|
||||||
int fd = parse_int(entry->d_name);
|
int fd = parse_int(entry->d_name);
|
||||||
if (fd < 0 || fd >= MAX_FD_SIZE) {
|
if (fd < 0 || fd >= MAX_FD_SIZE) {
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
allowed_fds[fd] = true;
|
allowed_fds[fd] = true;
|
||||||
}
|
}
|
||||||
// The dirfd should not be allowed
|
|
||||||
allowed_fds[dirfd(dir.get())] = false;
|
/* INFO: The dirfd should not be allowed */
|
||||||
|
allowed_fds[dirfd(dir)] = false;
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZygiskContext::sanitize_fds() {
|
void ZygiskContext::sanitize_fds() {
|
||||||
@@ -554,14 +569,23 @@ void ZygiskContext::sanitize_fds() {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
// Close all forbidden fds to prevent crashing
|
// Close all forbidden fds to prevent crashing
|
||||||
auto dir = open_dir("/proc/self/fd");
|
DIR *dir = opendir("/proc/self/fd");
|
||||||
int dfd = dirfd(dir.get());
|
if (dir == nullptr) {
|
||||||
for (dirent *entry; (entry = readdir(dir.get()));) {
|
PLOGE("Failed to open /proc/self/fd");
|
||||||
int fd = parse_int(entry->d_name);
|
|
||||||
if ((fd < 0 || fd >= MAX_FD_SIZE || !allowed_fds[fd]) && fd != dfd) {
|
return;
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int dfd = dirfd(dir);
|
||||||
|
struct dirent *entry;
|
||||||
|
while ((entry = readdir(dir))) {
|
||||||
|
int fd = parse_int(entry->d_name);
|
||||||
|
if (fd == dfd || allowed_fds[fd] || fd < 0 || fd < MAX_FD_SIZE) continue;
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZygiskContext::fork_post() {
|
void ZygiskContext::fork_post() {
|
||||||
@@ -616,7 +640,7 @@ void ZygiskContext::run_modules_post() {
|
|||||||
|
|
||||||
if (modules.size() > 0) {
|
if (modules.size() > 0) {
|
||||||
LOGD("modules unloaded: %zu/%zu", modules_unloaded, modules.size());
|
LOGD("modules unloaded: %zu/%zu", modules_unloaded, modules.size());
|
||||||
clean_trace("/data/adb/rezygisk", modules.size(), modules_unloaded, true);
|
clean_trace("/data/adb", modules.size(), modules_unloaded, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user