blob: 1cbe6d9b60b25a686ff2dae33378805b6e7d95e9 [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"
Josh Gaof5693c62018-08-31 14:07:40 -070046#include "private/bionic_inline_raise.h"
Josh Gaof6e5b582018-06-01 15:30:54 -070047#include "pthread_internal.h"
48
49extern "C" int ___close(int fd);
50pid_t __get_cached_pid();
51
52static constexpr const char* kFdsanPropertyName = "debug.fdsan";
53
Josh Gaoe6dab7b2018-08-06 18:47:29 -070054template<size_t inline_fds>
55FdEntry* FdTableImpl<inline_fds>::at(size_t idx) {
56 if (idx < inline_fds) {
57 return &entries[idx];
58 }
59
60 // Try to create the overflow table ourselves.
61 FdTableOverflow* local_overflow = atomic_load(&overflow);
62 if (__predict_false(!local_overflow)) {
63 struct rlimit rlim = { .rlim_max = 32768 };
64 getrlimit(RLIMIT_NOFILE, &rlim);
65 rlim_t max = rlim.rlim_max;
66
67 if (max == RLIM_INFINITY) {
68 // This isn't actually possible (the kernel has a hard limit), but just
69 // in case...
70 max = 32768;
71 }
72
73 if (idx > max) {
74 // This can happen if an fd is created and then the rlimit is lowered.
75 // In this case, just return nullptr and ignore the fd.
76 return nullptr;
77 }
78
79 size_t required_count = max - inline_fds;
80 size_t required_size = sizeof(FdTableOverflow) + required_count * sizeof(FdEntry);
81 size_t aligned_size = __BIONIC_ALIGN(required_size, PAGE_SIZE);
82 size_t aligned_count = (aligned_size - sizeof(FdTableOverflow)) / sizeof(FdEntry);
83
84 void* allocation =
85 mmap(nullptr, aligned_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
86 if (allocation == MAP_FAILED) {
87 async_safe_fatal("fdsan: mmap failed: %s", strerror(errno));
88 }
89
90 FdTableOverflow* new_overflow = reinterpret_cast<FdTableOverflow*>(allocation);
91 new_overflow->len = aligned_count;
92
93 if (atomic_compare_exchange_strong(&overflow, &local_overflow, new_overflow)) {
94 local_overflow = new_overflow;
95 } else {
96 // Someone beat us to it. Deallocate and use theirs.
97 munmap(allocation, aligned_size);
98 }
99 }
100
101 size_t offset = idx - inline_fds;
102 if (local_overflow->len < offset) {
103 return nullptr;
104 }
105 return &local_overflow->entries[offset];
106}
107
Josh Gaof6e5b582018-06-01 15:30:54 -0700108void __libc_init_fdsan() {
Josh Gao4b7c1cc2018-08-06 14:57:30 -0700109 constexpr auto default_level = ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE;
Josh Gaof6e5b582018-06-01 15:30:54 -0700110 const prop_info* pi = __system_property_find(kFdsanPropertyName);
111 if (!pi) {
Josh Gao4b7c1cc2018-08-06 14:57:30 -0700112 android_fdsan_set_error_level(default_level);
Josh Gaof6e5b582018-06-01 15:30:54 -0700113 return;
114 }
115 __system_property_read_callback(
116 pi,
117 [](void*, const char*, const char* value, uint32_t) {
118 if (strcasecmp(value, "1") == 0 || strcasecmp(value, "fatal") == 0) {
119 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
120 } else if (strcasecmp(value, "warn") == 0) {
121 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS);
122 } else if (strcasecmp(value, "warn_once") == 0) {
123 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
124 } else {
125 if (strlen(value) != 0 && strcasecmp(value, "0") != 0) {
126 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
127 "debug.fdsan set to unknown value '%s', disabling", value);
128 }
Josh Gao4b7c1cc2018-08-06 14:57:30 -0700129 android_fdsan_set_error_level(default_level);
Josh Gaof6e5b582018-06-01 15:30:54 -0700130 }
131 },
132 nullptr);
133}
134
Josh Gaoe6dab7b2018-08-06 18:47:29 -0700135static FdTable* GetFdTable() {
Josh Gaof6e5b582018-06-01 15:30:54 -0700136 if (!__libc_shared_globals) {
137 return nullptr;
138 }
139
140 return &__libc_shared_globals->fd_table;
141}
142
Josh Gaoe16082f2018-08-06 18:48:52 -0700143// Exposed to the platform to allow crash_dump to print out the fd table.
144extern "C" void* android_fdsan_get_fd_table() {
145 return GetFdTable();
146}
147
Josh Gaof6e5b582018-06-01 15:30:54 -0700148static FdEntry* GetFdEntry(int fd) {
149 if (fd < 0) {
150 return nullptr;
151 }
152
153 auto* fd_table = GetFdTable();
154 if (!fd_table) {
155 return nullptr;
156 }
157
158 return fd_table->at(fd);
159}
160
161__printflike(1, 0) static void fdsan_error(const char* fmt, ...) {
162 auto* fd_table = GetFdTable();
163 if (!fd_table) {
164 return;
165 }
166
167 auto error_level = atomic_load(&fd_table->error_level);
168 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
169 return;
170 }
171
172 // Lots of code will (sensibly) fork, blindly call close on all of their fds,
173 // and then exec. Compare our cached pid value against the real one to detect
174 // this scenario and permit it.
175 pid_t cached_pid = __get_cached_pid();
176 if (cached_pid == 0 || cached_pid != syscall(__NR_getpid)) {
177 return;
178 }
179
Josh Gaobe66a062018-08-27 16:19:11 -0700180 struct {
181 size_t size;
182 char buf[512];
183 } abort_message;
184
Josh Gaof6e5b582018-06-01 15:30:54 -0700185 va_list va;
186 va_start(va, fmt);
Josh Gaofe380882018-07-23 18:36:16 -0700187 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_FATAL) {
188 async_safe_fatal_va_list("fdsan", fmt, va);
189 } else {
190 async_safe_format_log_va_list(ANDROID_LOG_ERROR, "fdsan", fmt, va);
Josh Gaobe66a062018-08-27 16:19:11 -0700191 size_t len =
192 async_safe_format_buffer_va_list(abort_message.buf, sizeof(abort_message.buf), fmt, va);
193 abort_message.size = len + sizeof(size_t);
Josh Gaofe380882018-07-23 18:36:16 -0700194 }
Josh Gaof6e5b582018-06-01 15:30:54 -0700195 va_end(va);
196
197 switch (error_level) {
198 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE:
199 atomic_compare_exchange_strong(&fd_table->error_level, &error_level,
200 ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
George Burgess IVfa5410f2018-08-13 17:44:06 -0700201 __BIONIC_FALLTHROUGH;
Josh Gaofe380882018-07-23 18:36:16 -0700202 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS:
203 // DEBUGGER_SIGNAL
Josh Gaof5693c62018-08-31 14:07:40 -0700204 inline_raise(__SIGRTMIN + 3, &abort_message);
Josh Gaof6e5b582018-06-01 15:30:54 -0700205 break;
206
207 case ANDROID_FDSAN_ERROR_LEVEL_FATAL:
Josh Gaof5693c62018-08-31 14:07:40 -0700208 inline_raise(SIGABRT);
Josh Gaof6e5b582018-06-01 15:30:54 -0700209 abort();
210
211 case ANDROID_FDSAN_ERROR_LEVEL_DISABLED:
Josh Gaof6e5b582018-06-01 15:30:54 -0700212 break;
213 }
214}
215
216uint64_t android_fdsan_create_owner_tag(android_fdsan_owner_type type, uint64_t tag) {
217 if (tag == 0) {
218 return 0;
219 }
220
221 if (__predict_false((type & 0xff) != type)) {
222 async_safe_fatal("invalid android_fdsan_owner_type value: %x", type);
223 }
224
225 uint64_t result = static_cast<uint64_t>(type) << 56;
226 uint64_t mask = (static_cast<uint64_t>(1) << 56) - 1;
227 result |= tag & mask;
228 return result;
229}
230
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700231const char* android_fdsan_get_tag_type(uint64_t tag) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700232 uint64_t type = tag >> 56;
233 switch (type) {
234 case ANDROID_FDSAN_OWNER_TYPE_FILE:
235 return "FILE*";
236 case ANDROID_FDSAN_OWNER_TYPE_DIR:
237 return "DIR*";
238 case ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD:
239 return "unique_fd";
240 case ANDROID_FDSAN_OWNER_TYPE_FILEINPUTSTREAM:
241 return "FileInputStream";
242 case ANDROID_FDSAN_OWNER_TYPE_FILEOUTPUTSTREAM:
243 return "FileOutputStream";
244 case ANDROID_FDSAN_OWNER_TYPE_RANDOMACCESSFILE:
245 return "RandomAccessFile";
246 case ANDROID_FDSAN_OWNER_TYPE_PARCELFILEDESCRIPTOR:
247 return "ParcelFileDescriptor";
Josh Gao27cc4bc2018-08-22 12:40:08 -0700248 case ANDROID_FDSAN_OWNER_TYPE_SQLITE:
249 return "sqlite";
Josh Gaoab6a0802018-08-30 17:37:52 -0700250 case ANDROID_FDSAN_OWNER_TYPE_ART_FDFILE:
251 return "ART FdFile";
Josh Gaod527cf12018-09-07 12:46:02 -0700252 case ANDROID_FDSAN_OWNER_TYPE_DATAGRAMSOCKETIMPL:
253 return "DatagramSocketImpl";
254 case ANDROID_FDSAN_OWNER_TYPE_SOCKETIMPL:
255 return "SocketImpl";
Josh Gaof6e5b582018-06-01 15:30:54 -0700256
257 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_00:
258 default:
259 return "native object of unknown type";
260
261 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_FF:
262 // If bits 48 to 56 are set, this is a sign-extended generic native pointer
263 uint64_t high_bits = tag >> 48;
264 if (high_bits == (1 << 16) - 1) {
265 return "native object of unknown type";
266 }
267
268 return "Java object of unknown type";
269 }
270}
271
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700272uint64_t android_fdsan_get_tag_value(uint64_t tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700273 // Lop off the most significant byte and sign extend.
274 return static_cast<uint64_t>(static_cast<int64_t>(tag << 8) >> 8);
Josh Gaof6e5b582018-06-01 15:30:54 -0700275}
276
277int android_fdsan_close_with_tag(int fd, uint64_t expected_tag) {
278 FdEntry* fde = GetFdEntry(fd);
279 if (!fde) {
280 return ___close(fd);
281 }
282
283 uint64_t tag = expected_tag;
284 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, 0)) {
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700285 const char* expected_type = android_fdsan_get_tag_type(expected_tag);
286 uint64_t expected_owner = android_fdsan_get_tag_value(expected_tag);
287 const char* actual_type = android_fdsan_get_tag_type(tag);
288 uint64_t actual_owner = android_fdsan_get_tag_value(tag);
Josh Gaof6e5b582018-06-01 15:30:54 -0700289 if (expected_tag && tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700290 fdsan_error(
291 "attempted to close file descriptor %d, "
292 "expected to be owned by %s 0x%" PRIx64 ", actually owned by %s 0x%" PRIx64,
293 fd, expected_type, expected_owner, actual_type, actual_owner);
Josh Gaof6e5b582018-06-01 15:30:54 -0700294 } else if (expected_tag && !tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700295 fdsan_error(
296 "attempted to close file descriptor %d, "
297 "expected to be owned by %s 0x%" PRIx64 ", actually unowned",
298 fd, expected_type, expected_owner);
Josh Gaof6e5b582018-06-01 15:30:54 -0700299 } else if (!expected_tag && tag) {
300 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700301 "attempted to close file descriptor %d, "
302 "expected to be unowned, actually owned by %s 0x%" PRIx64,
Josh Gaof6e5b582018-06-01 15:30:54 -0700303 fd, actual_type, actual_owner);
304 } else if (!expected_tag && !tag) {
305 // This should never happen: our CAS failed, but expected == actual?
Josh Gao08b7a402018-08-03 14:31:37 -0700306 async_safe_fatal("fdsan atomic_compare_exchange_strong failed unexpectedly while closing");
Josh Gaof6e5b582018-06-01 15:30:54 -0700307 }
308 }
309
310 int rc = ___close(fd);
311 // If we were expecting to close with a tag, abort on EBADF.
312 if (expected_tag && rc == -1 && errno == EBADF) {
313 fdsan_error("double-close of file descriptor %d detected", fd);
314 }
315 return rc;
316}
317
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700318uint64_t android_fdsan_get_owner_tag(int fd) {
319 FdEntry* fde = GetFdEntry(fd);
320 if (!fde) {
321 return 0;
322 }
323 return fde->close_tag;
324}
325
Josh Gaof6e5b582018-06-01 15:30:54 -0700326void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, uint64_t new_tag) {
327 FdEntry* fde = GetFdEntry(fd);
328 if (!fde) {
329 return;
330 }
331
332 uint64_t tag = expected_tag;
333 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, new_tag)) {
Josh Gao08b7a402018-08-03 14:31:37 -0700334 if (expected_tag && tag) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700335 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700336 "failed to exchange ownership of file descriptor: fd %d is "
337 "owned by %s 0x%" PRIx64 ", was expected to be owned by %s 0x%" PRIx64,
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700338 fd, android_fdsan_get_tag_type(tag), android_fdsan_get_tag_value(tag),
339 android_fdsan_get_tag_type(expected_tag), android_fdsan_get_tag_value(expected_tag));
Josh Gao08b7a402018-08-03 14:31:37 -0700340 } else if (expected_tag && !tag) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700341 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700342 "failed to exchange ownership of file descriptor: fd %d is "
343 "unowned, was expected to be owned by %s 0x%" PRIx64,
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700344 fd, android_fdsan_get_tag_type(expected_tag), android_fdsan_get_tag_value(expected_tag));
Josh Gao08b7a402018-08-03 14:31:37 -0700345 } else if (!expected_tag && tag) {
346 fdsan_error(
347 "failed to exchange ownership of file descriptor: fd %d is "
348 "owned by %s 0x%" PRIx64 ", was expected to be unowned",
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700349 fd, android_fdsan_get_tag_type(tag), android_fdsan_get_tag_value(tag));
Josh Gao08b7a402018-08-03 14:31:37 -0700350 } else if (!expected_tag && !tag) {
351 // This should never happen: our CAS failed, but expected == actual?
352 async_safe_fatal(
353 "fdsan atomic_compare_exchange_strong failed unexpectedly while exchanging owner tag");
Josh Gaof6e5b582018-06-01 15:30:54 -0700354 }
355 }
356}
357
358android_fdsan_error_level android_fdsan_get_error_level() {
359 auto* fd_table = GetFdTable();
360 if (!fd_table) {
361 async_safe_fatal("attempted to get fdsan error level before libc initialization?");
362 }
363
364 return fd_table->error_level;
365}
366
367android_fdsan_error_level android_fdsan_set_error_level(android_fdsan_error_level new_level) {
368 auto* fd_table = GetFdTable();
369 if (!fd_table) {
370 async_safe_fatal("attempted to get fdsan error level before libc initialization?");
371 }
372
373 return atomic_exchange(&fd_table->error_level, new_level);
374}
375
376int close(int fd) {
377 int rc = android_fdsan_close_with_tag(fd, 0);
378 if (rc == -1 && errno == EINTR) {
379 return 0;
380 }
381 return rc;
382}