|
1 | | -#include <android/dlext.h> |
2 | 1 | #include <sys/mount.h> |
3 | 2 | #include <dlfcn.h> |
4 | 3 | #include <regex.h> |
|
10 | 9 | #include <lsplt.hpp> |
11 | 10 |
|
12 | 11 | #include <fcntl.h> |
| 12 | +#include <dirent.h> |
| 13 | +#include <sys/types.h> |
13 | 14 | #include <sys/prctl.h> |
14 | 15 | #include <sys/stat.h> |
15 | 16 | #include <sys/mman.h> |
| 17 | + |
16 | 18 | #include <unistd.h> |
17 | 19 |
|
18 | 20 | #include "daemon.h" |
@@ -487,25 +489,38 @@ int sigmask(int how, int signum) { |
487 | 489 | } |
488 | 490 |
|
489 | 491 | 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 | + */ |
492 | 495 | sigmask(SIG_BLOCK, SIGCHLD); |
493 | 496 | pid = old_fork(); |
494 | 497 | if (pid != 0 || flags[SKIP_FD_SANITIZATION]) |
495 | 498 | return; |
496 | 499 |
|
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))) { |
500 | 510 | int fd = parse_int(entry->d_name); |
501 | 511 | if (fd < 0 || fd >= MAX_FD_SIZE) { |
502 | 512 | close(fd); |
| 513 | + |
503 | 514 | continue; |
504 | 515 | } |
| 516 | + |
505 | 517 | allowed_fds[fd] = true; |
506 | 518 | } |
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); |
509 | 524 | } |
510 | 525 |
|
511 | 526 | void ZygiskContext::sanitize_fds() { |
@@ -554,14 +569,23 @@ void ZygiskContext::sanitize_fds() { |
554 | 569 | return; |
555 | 570 |
|
556 | 571 | // 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))) { |
560 | 582 | 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); |
564 | 586 | } |
| 587 | + |
| 588 | + closedir(dir); |
565 | 589 | } |
566 | 590 |
|
567 | 591 | void ZygiskContext::fork_post() { |
@@ -616,7 +640,7 @@ void ZygiskContext::run_modules_post() { |
616 | 640 |
|
617 | 641 | if (modules.size() > 0) { |
618 | 642 | 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); |
620 | 644 | } |
621 | 645 | } |
622 | 646 |
|
|
0 commit comments