blob: 1ace9b69ca801f1aee8db9c906f83fa544f196ac [file] [log] [blame]
Josh Gaof6e5b582018-06-01 15:30:54 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <android/fdsan.h>
30
31#include <errno.h>
32#include <inttypes.h>
Josh Gaofe380882018-07-23 18:36:16 -070033#include <signal.h>
Josh Gaof6e5b582018-06-01 15:30:54 -070034#include <stdarg.h>
35#include <stdatomic.h>
36#include <string.h>
37#include <sys/cdefs.h>
38#include <sys/mman.h>
39#include <sys/syscall.h>
40#include <unistd.h>
41
42#include <async_safe/log.h>
43#include <sys/system_properties.h>
44
45#include "private/bionic_globals.h"
46#include "pthread_internal.h"
47
48extern "C" int ___close(int fd);
49pid_t __get_cached_pid();
50
51static constexpr const char* kFdsanPropertyName = "debug.fdsan";
52
53void __libc_init_fdsan() {
54 const prop_info* pi = __system_property_find(kFdsanPropertyName);
55 if (!pi) {
56 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
57 return;
58 }
59 __system_property_read_callback(
60 pi,
61 [](void*, const char*, const char* value, uint32_t) {
62 if (strcasecmp(value, "1") == 0 || strcasecmp(value, "fatal") == 0) {
63 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
64 } else if (strcasecmp(value, "warn") == 0) {
65 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS);
66 } else if (strcasecmp(value, "warn_once") == 0) {
67 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
68 } else {
69 if (strlen(value) != 0 && strcasecmp(value, "0") != 0) {
70 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
71 "debug.fdsan set to unknown value '%s', disabling", value);
72 }
73 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
74 }
75 },
76 nullptr);
77}
78
79static FdTable<128>* GetFdTable() {
80 if (!__libc_shared_globals) {
81 return nullptr;
82 }
83
84 return &__libc_shared_globals->fd_table;
85}
86
87static FdEntry* GetFdEntry(int fd) {
88 if (fd < 0) {
89 return nullptr;
90 }
91
92 auto* fd_table = GetFdTable();
93 if (!fd_table) {
94 return nullptr;
95 }
96
97 return fd_table->at(fd);
98}
99
100__printflike(1, 0) static void fdsan_error(const char* fmt, ...) {
101 auto* fd_table = GetFdTable();
102 if (!fd_table) {
103 return;
104 }
105
106 auto error_level = atomic_load(&fd_table->error_level);
107 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
108 return;
109 }
110
111 // Lots of code will (sensibly) fork, blindly call close on all of their fds,
112 // and then exec. Compare our cached pid value against the real one to detect
113 // this scenario and permit it.
114 pid_t cached_pid = __get_cached_pid();
115 if (cached_pid == 0 || cached_pid != syscall(__NR_getpid)) {
116 return;
117 }
118
119 va_list va;
120 va_start(va, fmt);
Josh Gaofe380882018-07-23 18:36:16 -0700121 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_FATAL) {
122 async_safe_fatal_va_list("fdsan", fmt, va);
123 } else {
124 async_safe_format_log_va_list(ANDROID_LOG_ERROR, "fdsan", fmt, va);
125 }
Josh Gaof6e5b582018-06-01 15:30:54 -0700126 va_end(va);
127
128 switch (error_level) {
129 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE:
130 atomic_compare_exchange_strong(&fd_table->error_level, &error_level,
131 ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
Josh Gaofe380882018-07-23 18:36:16 -0700132 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS:
133 // DEBUGGER_SIGNAL
134 raise(__SIGRTMIN + 3);
Josh Gaof6e5b582018-06-01 15:30:54 -0700135 break;
136
137 case ANDROID_FDSAN_ERROR_LEVEL_FATAL:
138 abort();
139
140 case ANDROID_FDSAN_ERROR_LEVEL_DISABLED:
Josh Gaof6e5b582018-06-01 15:30:54 -0700141 break;
142 }
143}
144
145uint64_t android_fdsan_create_owner_tag(android_fdsan_owner_type type, uint64_t tag) {
146 if (tag == 0) {
147 return 0;
148 }
149
150 if (__predict_false((type & 0xff) != type)) {
151 async_safe_fatal("invalid android_fdsan_owner_type value: %x", type);
152 }
153
154 uint64_t result = static_cast<uint64_t>(type) << 56;
155 uint64_t mask = (static_cast<uint64_t>(1) << 56) - 1;
156 result |= tag & mask;
157 return result;
158}
159
160static const char* __tag_to_type(uint64_t tag) {
161 uint64_t type = tag >> 56;
162 switch (type) {
163 case ANDROID_FDSAN_OWNER_TYPE_FILE:
164 return "FILE*";
165 case ANDROID_FDSAN_OWNER_TYPE_DIR:
166 return "DIR*";
167 case ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD:
168 return "unique_fd";
169 case ANDROID_FDSAN_OWNER_TYPE_FILEINPUTSTREAM:
170 return "FileInputStream";
171 case ANDROID_FDSAN_OWNER_TYPE_FILEOUTPUTSTREAM:
172 return "FileOutputStream";
173 case ANDROID_FDSAN_OWNER_TYPE_RANDOMACCESSFILE:
174 return "RandomAccessFile";
175 case ANDROID_FDSAN_OWNER_TYPE_PARCELFILEDESCRIPTOR:
176 return "ParcelFileDescriptor";
177
178 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_00:
179 default:
180 return "native object of unknown type";
181
182 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_FF:
183 // If bits 48 to 56 are set, this is a sign-extended generic native pointer
184 uint64_t high_bits = tag >> 48;
185 if (high_bits == (1 << 16) - 1) {
186 return "native object of unknown type";
187 }
188
189 return "Java object of unknown type";
190 }
191}
192
193static uint64_t __tag_to_owner(uint64_t tag) {
194 return tag;
195}
196
197int android_fdsan_close_with_tag(int fd, uint64_t expected_tag) {
198 FdEntry* fde = GetFdEntry(fd);
199 if (!fde) {
200 return ___close(fd);
201 }
202
203 uint64_t tag = expected_tag;
204 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, 0)) {
205 const char* expected_type = __tag_to_type(expected_tag);
206 uint64_t expected_owner = __tag_to_owner(expected_tag);
207 const char* actual_type = __tag_to_type(tag);
208 uint64_t actual_owner = __tag_to_owner(tag);
209 if (expected_tag && tag) {
210 fdsan_error("attempted to close file descriptor %d, expected to be owned by %s 0x%" PRIx64
211 ", actually owned by %s 0x%" PRIx64,
212 fd, expected_type, expected_owner, actual_type, actual_owner);
213 } else if (expected_tag && !tag) {
214 fdsan_error("attempted to close file descriptor %d, expected to be owned by %s 0x%" PRIx64
215 ", actually unowned",
216 fd, expected_type, expected_owner);
217 } else if (!expected_tag && tag) {
218 fdsan_error(
219 "attempted to close file descriptor %d, expected to be unowned, actually owned by %s "
220 "0x%" PRIx64,
221 fd, actual_type, actual_owner);
222 } else if (!expected_tag && !tag) {
223 // This should never happen: our CAS failed, but expected == actual?
224 async_safe_fatal("fdsan atomic_compare_exchange_strong failed unexpectedly");
225 }
226 }
227
228 int rc = ___close(fd);
229 // If we were expecting to close with a tag, abort on EBADF.
230 if (expected_tag && rc == -1 && errno == EBADF) {
231 fdsan_error("double-close of file descriptor %d detected", fd);
232 }
233 return rc;
234}
235
236void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, uint64_t new_tag) {
237 FdEntry* fde = GetFdEntry(fd);
238 if (!fde) {
239 return;
240 }
241
242 uint64_t tag = expected_tag;
243 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, new_tag)) {
244 if (expected_tag == 0) {
245 fdsan_error(
246 "failed to take ownership of already-owned file descriptor: fd %d is owned by %s "
247 "%" PRIx64,
248 fd, __tag_to_type(tag), __tag_to_owner(tag));
249 } else {
250 fdsan_error(
251 "failed to exchange ownership of unowned file descriptor: expected fd %d to be owned "
252 "by %s %" PRIx64,
253 fd, __tag_to_type(expected_tag), __tag_to_owner(expected_tag));
254 }
255 }
256}
257
258android_fdsan_error_level android_fdsan_get_error_level() {
259 auto* fd_table = GetFdTable();
260 if (!fd_table) {
261 async_safe_fatal("attempted to get fdsan error level before libc initialization?");
262 }
263
264 return fd_table->error_level;
265}
266
267android_fdsan_error_level android_fdsan_set_error_level(android_fdsan_error_level new_level) {
268 auto* fd_table = GetFdTable();
269 if (!fd_table) {
270 async_safe_fatal("attempted to get fdsan error level before libc initialization?");
271 }
272
273 return atomic_exchange(&fd_table->error_level, new_level);
274}
275
276int close(int fd) {
277 int rc = android_fdsan_close_with_tag(fd, 0);
278 if (rc == -1 && errno == EINTR) {
279 return 0;
280 }
281 return rc;
282}