Skip to content

Commit 5b3d9c7

Browse files
committed
remove: open file helpers
This commit remove the `open_...` and `xopen_...` helpers, making the code simpler and more direct.
1 parent 4625587 commit 5b3d9c7

3 files changed

Lines changed: 49 additions & 78 deletions

File tree

loader/src/common/files.cpp

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ void file_readline(bool trim, FILE *fp, const std::function<bool(std::string_vie
2525
free(buf);
2626
}
2727

28-
void file_readline(bool trim, const char *file, const std::function<bool(std::string_view)> &fn) {
29-
if (auto fp = open_file(file, "re"))
30-
file_readline(trim, fp.get(), fn);
31-
}
3228
void file_readline(const char *file, const std::function<bool(std::string_view)> &fn) {
33-
file_readline(false, file, fn);
29+
FILE *fp = fopen(file, "re");
30+
if (!fp) {
31+
PLOGE("Failed to open file %s", file);
32+
33+
return;
34+
}
35+
36+
file_readline(false, fp, fn);
37+
38+
fclose(fp);
3439
}
3540

3641
std::vector<mount_info> parse_mount_info(const char *pid) {
@@ -112,26 +117,3 @@ std::vector<mount_info> parse_mount_info(const char *pid) {
112117
});
113118
return result;
114119
}
115-
116-
sDIR make_dir(DIR *dp) {
117-
return sDIR(dp, [](DIR *dp){ return dp ? closedir(dp) : 1; });
118-
}
119-
120-
sFILE make_file(FILE *fp) {
121-
return sFILE(fp, [](FILE *fp){ return fp ? fclose(fp) : 1; });
122-
}
123-
124-
int get_path_from_fd(int fd, char *buf, size_t size) {
125-
if (fd < 0 || !buf || size == 0) return -1;
126-
127-
/* NOTE: We assume that the path is always at /data/adb/modules/xxx
128-
which should never be longer than 128 chars. */
129-
char proc_path[128];
130-
snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", fd);
131-
132-
ssize_t len = readlink(proc_path, buf, size - 1);
133-
if (len == -1) return -1;
134-
135-
buf[len] = '\0';
136-
return 0;
137-
}

loader/src/include/files.hpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,4 @@ struct mount_info {
2121
std::string fs_option;
2222
};
2323

24-
void file_readline(bool trim, FILE *fp, const std::function<bool(std::string_view)> &fn);
25-
void file_readline(bool trim, const char *file, const std::function<bool(std::string_view)> &fn);
26-
void file_readline(const char *file, const std::function<bool(std::string_view)> &fn);
27-
2824
std::vector<mount_info> parse_mount_info(const char *pid);
29-
30-
int get_path_from_fd(int fd, char *buf, size_t size);
31-
32-
using sFILE = std::unique_ptr<FILE, decltype(&fclose)>;
33-
using sDIR = std::unique_ptr<DIR, decltype(&closedir)>;
34-
sDIR make_dir(DIR *dp);
35-
sFILE make_file(FILE *fp);
36-
37-
static inline sDIR open_dir(const char *path) {
38-
return make_dir(opendir(path));
39-
}
40-
41-
static inline sDIR xopen_dir(const char *path) {
42-
return make_dir(opendir(path));
43-
}
44-
45-
static inline sDIR xopen_dir(int dirfd) {
46-
return make_dir(fdopendir(dirfd));
47-
}
48-
49-
static inline sFILE open_file(const char *path, const char *mode) {
50-
return make_file(fopen(path, mode));
51-
}
52-
53-
static inline sFILE xopen_file(const char *path, const char *mode) {
54-
return make_file(fopen(path, mode));
55-
}
56-
57-
static inline sFILE xopen_file(int fd, const char *mode) {
58-
return make_file(fdopen(fd, mode));
59-
}

loader/src/injector/hook.cpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <android/dlext.h>
21
#include <sys/mount.h>
32
#include <dlfcn.h>
43
#include <regex.h>
@@ -10,9 +9,12 @@
109
#include <lsplt.hpp>
1110

1211
#include <fcntl.h>
12+
#include <dirent.h>
13+
#include <sys/types.h>
1314
#include <sys/prctl.h>
1415
#include <sys/stat.h>
1516
#include <sys/mman.h>
17+
1618
#include <unistd.h>
1719

1820
#include "daemon.h"
@@ -487,25 +489,38 @@ int sigmask(int how, int signum) {
487489
}
488490

489491
void ZygiskContext::fork_pre() {
490-
// Do our own fork before loading any 3rd party code
491-
// First block SIGCHLD, unblock after original fork is done
492+
/* INFO: Do our own fork before loading any 3rd party code.
493+
First block SIGCHLD, unblock after original fork is done.
494+
*/
492495
sigmask(SIG_BLOCK, SIGCHLD);
493496
pid = old_fork();
494497
if (pid != 0 || flags[SKIP_FD_SANITIZATION])
495498
return;
496499

497-
// Record all open fds
498-
auto dir = xopen_dir("/proc/self/fd");
499-
for (dirent *entry; (entry = readdir(dir.get()));) {
500+
/* INFO: Record all open fds */
501+
DIR *dir = opendir("/proc/self/fd");
502+
if (dir == nullptr) {
503+
PLOGE("Failed to open /proc/self/fd");
504+
505+
return;
506+
}
507+
508+
struct dirent *entry;
509+
while ((entry = readdir(dir))) {
500510
int fd = parse_int(entry->d_name);
501511
if (fd < 0 || fd >= MAX_FD_SIZE) {
502512
close(fd);
513+
503514
continue;
504515
}
516+
505517
allowed_fds[fd] = true;
506518
}
507-
// The dirfd should not be allowed
508-
allowed_fds[dirfd(dir.get())] = false;
519+
520+
/* INFO: The dirfd should not be allowed */
521+
allowed_fds[dirfd(dir)] = false;
522+
523+
closedir(dir);
509524
}
510525

511526
void ZygiskContext::sanitize_fds() {
@@ -554,14 +569,23 @@ void ZygiskContext::sanitize_fds() {
554569
return;
555570

556571
// Close all forbidden fds to prevent crashing
557-
auto dir = open_dir("/proc/self/fd");
558-
int dfd = dirfd(dir.get());
559-
for (dirent *entry; (entry = readdir(dir.get()));) {
572+
DIR *dir = opendir("/proc/self/fd");
573+
if (dir == nullptr) {
574+
PLOGE("Failed to open /proc/self/fd");
575+
576+
return;
577+
}
578+
579+
int dfd = dirfd(dir);
580+
struct dirent *entry;
581+
while ((entry = readdir(dir))) {
560582
int fd = parse_int(entry->d_name);
561-
if ((fd < 0 || fd >= MAX_FD_SIZE || !allowed_fds[fd]) && fd != dfd) {
562-
close(fd);
563-
}
583+
if (fd == dfd || allowed_fds[fd] || fd < 0 || fd < MAX_FD_SIZE) continue;
584+
585+
close(fd);
564586
}
587+
588+
closedir(dir);
565589
}
566590

567591
void ZygiskContext::fork_post() {
@@ -616,7 +640,7 @@ void ZygiskContext::run_modules_post() {
616640

617641
if (modules.size() > 0) {
618642
LOGD("modules unloaded: %zu/%zu", modules_unloaded, modules.size());
619-
clean_trace("/data/adb/rezygisk", modules.size(), modules_unloaded, true);
643+
clean_trace("/data/adb", modules.size(), modules_unloaded, true);
620644
}
621645
}
622646

0 commit comments

Comments
 (0)