#ifndef DROIDDECK_PROOT_STATX_MNT_ID_H #define DROIDDECK_PROOT_STATX_MNT_ID_H #include #include #include #include #include #include #include #include #include #include #include static inline bool droiddeck_proot_read_fdinfo(int fd, uint64_t *mnt_id, uint64_t *ino) { if (fd < 0 || !mnt_id || !ino) return false; char fdinfo_path[64]; int n = snprintf(fdinfo_path, sizeof fdinfo_path, "/proc/self/fdinfo/%d", fd); if (n < 0 || (size_t)n >= sizeof fdinfo_path) return false; FILE *fdinfo = fopen(fdinfo_path, "r"); if (!fdinfo) return false; bool have_mnt_id = false, have_ino = false; uint64_t parsed_mnt_id = 0, parsed_ino = 0; char line[256]; while (fgets(line, sizeof line, fdinfo)) { const char *value = NULL; uint64_t *field = NULL; if (strncmp(line, "mnt_id:", 7) == 0) { value = line + 7; field = &parsed_mnt_id; have_mnt_id = true; } else if (strncmp(line, "ino:", 4) == 0) { value = line + 4; field = &parsed_ino; have_ino = true; } if (!value) continue; errno = 0; char *end = NULL; unsigned long long parsed = strtoull(value, &end, 10); if (errno || end == value || (*end && *end != '\n' && *end != '\r')) { if (field == &parsed_mnt_id) have_mnt_id = false; else have_ino = false; continue; } *field = (uint64_t)parsed; } bool read_error = ferror(fdinfo); fclose(fdinfo); if (read_error || !have_mnt_id || !have_ino || parsed_mnt_id == 0) return false; *mnt_id = parsed_mnt_id; *ino = parsed_ino; return true; } static inline bool droiddeck_proot_statx_fill_mnt_id(pid_t pid, int fd, int flags, const char *path, unsigned int mask, struct statx *buf) { if (fd < 0 || !(flags & AT_EMPTY_PATH) || !path || path[0] != '\0' || !(mask & STATX_MNT_ID) || (buf->stx_mask & STATX_MNT_ID) || !(buf->stx_mask & STATX_INO)) return false; char tracee_fd_path[64]; int n = snprintf(tracee_fd_path, sizeof tracee_fd_path, "/proc/%ld/fd/%d", (long)pid, fd); if (n < 0 || (size_t)n >= sizeof tracee_fd_path) return false; int pinned = open(tracee_fd_path, O_PATH | O_CLOEXEC); if (pinned < 0) return false; struct stat st; bool same = fstat(pinned, &st) == 0 && st.st_ino == buf->stx_ino && major(st.st_dev) == buf->stx_dev_major && minor(st.st_dev) == buf->stx_dev_minor; uint64_t mnt_id = 0, ino = 0; bool have_fdinfo = same && droiddeck_proot_read_fdinfo(pinned, &mnt_id, &ino); close(pinned); if (!have_fdinfo || ino != st.st_ino) return false; buf->stx_mnt_id = mnt_id; buf->stx_mask |= STATX_MNT_ID; return true; } #endif