|
| 1 | +#include "fstatfs.hpp" |
| 2 | + |
| 3 | +#include "logging.h" |
| 4 | +#include <dirent.h> |
| 5 | +#include <string> |
| 6 | +#include <sys/vfs.h> |
| 7 | +#include <sys/stat.h> |
| 8 | +#include <unistd.h> |
| 9 | + |
| 10 | +// The magic number for OverlayFS, defined in kernel headers (e.g., |
| 11 | +// linux/magic.h) We define it here to avoid dependency on kernel headers in the |
| 12 | +// build environment. |
| 13 | +#define OVERLAYFS_SUPER_MAGIC 0x794c7630 |
| 14 | + |
| 15 | +// For comparison, the magic number for EXT4 |
| 16 | +#define EXT4_SUPER_MAGIC 0xEF53 |
| 17 | + |
| 18 | +void check_system_fds() { |
| 19 | + const std::string fd_dir_path = "/proc/self/fd"; |
| 20 | + DIR *dir = opendir(fd_dir_path.c_str()); |
| 21 | + |
| 22 | + if (!dir) { |
| 23 | + LOGD("Error: Could not open %s: %s", fd_dir_path.c_str(), strerror(errno)); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + LOGD("Starting scan of inherited file descriptors..."); |
| 28 | + |
| 29 | + struct dirent *entry; |
| 30 | + while ((entry = readdir(dir)) != nullptr) { |
| 31 | + // Each entry name is a file descriptor number |
| 32 | + const std::string fd_str = entry->d_name; |
| 33 | + |
| 34 | + // Skip '.' and '..' directories |
| 35 | + if (fd_str == "." || fd_str == "..") { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + int fd = -1; |
| 40 | + try { |
| 41 | + fd = std::stoi(fd_str); |
| 42 | + } catch (const std::invalid_argument &e) { |
| 43 | + LOGD("Warning: Could not parse FD: %s", fd_str.c_str()); |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + // Construct the full path to the symbolic link |
| 48 | + char symlink_path[PATH_MAX]; |
| 49 | + snprintf(symlink_path, sizeof(symlink_path), "%s/%s", fd_dir_path.c_str(), |
| 50 | + fd_str.c_str()); |
| 51 | + |
| 52 | + // Use readlink to find out what file this FD points to |
| 53 | + char real_path[PATH_MAX]; |
| 54 | + ssize_t len = readlink(symlink_path, real_path, sizeof(real_path) - 1); |
| 55 | + |
| 56 | + if (len == -1) { |
| 57 | + // This can happen for sockets, pipes, etc. It's normal. |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + // readlink does not null-terminate, so we must do it ourselves. |
| 62 | + real_path[len] = '\0'; |
| 63 | + std::string real_path_str(real_path); |
| 64 | + |
| 65 | + // This is the filter you requested: only check files from /system |
| 66 | + if (real_path_str.rfind("/system/", 0) == 0) { |
| 67 | + |
| 68 | + LOGD("Checking FD %d -> %s", fd, real_path_str.c_str()); |
| 69 | + |
| 70 | + struct stat stat_from_fstat; |
| 71 | + if (fstat(fd, &stat_from_fstat) == -1) { |
| 72 | + LOGD(" -> fstat() failed. Skipping."); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + struct stat stat_from_stat; |
| 77 | + if (stat(real_path, &stat_from_stat) == -1) { |
| 78 | + LOGD(" -> stat() failed. Skipping."); |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + LOGD(" -> fstat() dev:inode = %llu:%llu", |
| 83 | + (unsigned long long)stat_from_fstat.st_dev, |
| 84 | + (unsigned long long)stat_from_fstat.st_ino); |
| 85 | + LOGD(" -> stat() dev:inode = %llu:%llu", |
| 86 | + (unsigned long long)stat_from_stat.st_dev, |
| 87 | + (unsigned long long)stat_from_stat.st_ino); |
| 88 | + |
| 89 | + struct statfs fs_info; |
| 90 | + // The CRITICAL part: call fstatfs on the integer FD, not statfs on the |
| 91 | + // path. |
| 92 | + if (fstatfs(fd, &fs_info) == -1) { |
| 93 | + LOGD(" -> fstatfs failed: %s", strerror(errno)); |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + // Analyze the result |
| 98 | + if (fs_info.f_type == OVERLAYFS_SUPER_MAGIC) { |
| 99 | + LOGD(" -> Filesystem type: 0x%lX. *** OVERLAYFS DETECTED! ***", |
| 100 | + fs_info.f_type); |
| 101 | + } else if (fs_info.f_type == EXT4_SUPER_MAGIC) { |
| 102 | + LOGD(" -> Filesystem type: 0x%lX. (This is ext4)", fs_info.f_type); |
| 103 | + } else { |
| 104 | + LOGD(" -> Filesystem type: 0x%lX. (Unknown)", fs_info.f_type); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + closedir(dir); |
| 110 | + LOGD("File descriptor scan complete."); |
| 111 | +} |
0 commit comments