1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "DEBUG"
18
19 #include "libdebuggerd/open_files_list.h"
20
21 #include <android/fdsan.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <string>
31 #include <utility>
32 #include <vector>
33
34 #include <android-base/file.h>
35 #include <log/log.h>
36 #include <unwindstack/Memory.h>
37
38 #include "libdebuggerd/utility.h"
39 #include "private/bionic_fdsan.h"
40
populate_open_files_list(OpenFilesList * list,pid_t pid)41 void populate_open_files_list(OpenFilesList* list, pid_t pid) {
42 std::string fd_dir_name = "/proc/" + std::to_string(pid) + "/fd";
43 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(fd_dir_name.c_str()), closedir);
44 if (dir == nullptr) {
45 ALOGE("failed to open directory %s: %s", fd_dir_name.c_str(), strerror(errno));
46 return;
47 }
48
49 struct dirent* de;
50 while ((de = readdir(dir.get())) != nullptr) {
51 if (*de->d_name == '.') {
52 continue;
53 }
54
55 int fd = atoi(de->d_name);
56 std::string path = fd_dir_name + "/" + std::string(de->d_name);
57 std::string target;
58 if (android::base::Readlink(path, &target)) {
59 (*list)[fd].path = target;
60 } else {
61 (*list)[fd].path = "???";
62 ALOGE("failed to readlink %s: %s", path.c_str(), strerror(errno));
63 }
64 }
65 }
66
populate_fdsan_table(OpenFilesList * list,std::shared_ptr<unwindstack::Memory> memory,uint64_t fdsan_table_address)67 void populate_fdsan_table(OpenFilesList* list, std::shared_ptr<unwindstack::Memory> memory,
68 uint64_t fdsan_table_address) {
69 constexpr size_t inline_fds = sizeof(FdTable::entries) / sizeof(*FdTable::entries);
70 static_assert(inline_fds == 128);
71 size_t entry_offset = offsetof(FdTable, entries);
72 for (size_t i = 0; i < inline_fds; ++i) {
73 uint64_t address = fdsan_table_address + entry_offset + sizeof(FdEntry) * i;
74 FdEntry entry;
75 if (!memory->Read(address, &entry, sizeof(entry))) {
76 ALOGE("failed to read fdsan table entry %zu: %s", i, strerror(errno));
77 return;
78 }
79 if (entry.close_tag) {
80 (*list)[i].fdsan_owner = entry.close_tag.load();
81 }
82 }
83
84 size_t overflow_offset = offsetof(FdTable, overflow);
85 uintptr_t overflow = 0;
86 if (!memory->Read(fdsan_table_address + overflow_offset, &overflow, sizeof(overflow))) {
87 ALOGE("failed to read fdsan table overflow pointer: %s", strerror(errno));
88 return;
89 }
90
91 if (!overflow) {
92 return;
93 }
94
95 size_t overflow_length;
96 if (!memory->Read(overflow, &overflow_length, sizeof(overflow_length))) {
97 ALOGE("failed to read fdsan overflow table length: %s", strerror(errno));
98 return;
99 }
100
101 if (overflow_length > 131072) {
102 ALOGE("unreasonable large fdsan overflow table size %zu, bailing out", overflow_length);
103 return;
104 }
105
106 for (size_t i = 0; i < overflow_length; ++i) {
107 int fd = i + inline_fds;
108 uint64_t address = overflow + offsetof(FdTableOverflow, entries) + i * sizeof(FdEntry);
109 FdEntry entry;
110 if (!memory->Read(address, &entry, sizeof(entry))) {
111 ALOGE("failed to read fdsan overflow entry for fd %d: %s", fd, strerror(errno));
112 return;
113 }
114 if (entry.close_tag) {
115 (*list)[fd].fdsan_owner = entry.close_tag;
116 }
117 }
118 return;
119 }
120
dump_open_files_list(log_t * log,const OpenFilesList & files,const char * prefix)121 void dump_open_files_list(log_t* log, const OpenFilesList& files, const char* prefix) {
122 for (auto& [fd, entry] : files) {
123 const std::optional<std::string>& path = entry.path;
124 const std::optional<uint64_t>& fdsan_owner = entry.fdsan_owner;
125 if (path && fdsan_owner) {
126 const char* type = android_fdsan_get_tag_type(*fdsan_owner);
127 uint64_t value = android_fdsan_get_tag_value(*fdsan_owner);
128 _LOG(log, logtype::OPEN_FILES, "%sfd %i: %s (owned by %s %#" PRIx64 ")\n", prefix, fd,
129 path->c_str(), type, value);
130 } else if (path && !fdsan_owner) {
131 _LOG(log, logtype::OPEN_FILES, "%sfd %i: %s (unowned)\n", prefix, fd, path->c_str());
132 } else if (!path && fdsan_owner) {
133 _LOG(log, logtype::OPEN_FILES, "%sfd %i: <MISSING> (owned by %#" PRIx64 ")\n", prefix, fd,
134 *fdsan_owner);
135 } else {
136 ALOGE("OpenFilesList contains an entry (fd %d) with no path or owner", fd);
137 }
138 }
139 }
140
141