blob: b8563a0a24cc290f6c2219c58fac29157b4c76d9 [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>
Josh Gao5074e7d2019-12-13 13:55:53 -080043#include <platform/bionic/reserved_signals.h>
Josh Gaof6e5b582018-06-01 15:30:54 -070044#include <sys/system_properties.h>
45
Josh Gao97271922019-11-06 13:15:00 -080046#include "private/bionic_fdtrack.h"
Josh Gaof6e5b582018-06-01 15:30:54 -070047#include "private/bionic_globals.h"
Josh Gaof5693c62018-08-31 14:07:40 -070048#include "private/bionic_inline_raise.h"
Josh Gaof6e5b582018-06-01 15:30:54 -070049#include "pthread_internal.h"
50
Elliott Hughes50080a22019-06-19 12:47:53 -070051extern "C" int __close(int fd);
Josh Gaof6e5b582018-06-01 15:30:54 -070052pid_t __get_cached_pid();
53
54static constexpr const char* kFdsanPropertyName = "debug.fdsan";
55
Josh Gaoe6dab7b2018-08-06 18:47:29 -070056template<size_t inline_fds>
57FdEntry* FdTableImpl<inline_fds>::at(size_t idx) {
58 if (idx < inline_fds) {
59 return &entries[idx];
60 }
61
62 // Try to create the overflow table ourselves.
63 FdTableOverflow* local_overflow = atomic_load(&overflow);
64 if (__predict_false(!local_overflow)) {
65 struct rlimit rlim = { .rlim_max = 32768 };
66 getrlimit(RLIMIT_NOFILE, &rlim);
67 rlim_t max = rlim.rlim_max;
68
69 if (max == RLIM_INFINITY) {
70 // This isn't actually possible (the kernel has a hard limit), but just
71 // in case...
72 max = 32768;
73 }
74
75 if (idx > max) {
76 // This can happen if an fd is created and then the rlimit is lowered.
77 // In this case, just return nullptr and ignore the fd.
78 return nullptr;
79 }
80
81 size_t required_count = max - inline_fds;
82 size_t required_size = sizeof(FdTableOverflow) + required_count * sizeof(FdEntry);
83 size_t aligned_size = __BIONIC_ALIGN(required_size, PAGE_SIZE);
84 size_t aligned_count = (aligned_size - sizeof(FdTableOverflow)) / sizeof(FdEntry);
85
86 void* allocation =
87 mmap(nullptr, aligned_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
88 if (allocation == MAP_FAILED) {
89 async_safe_fatal("fdsan: mmap failed: %s", strerror(errno));
90 }
91
92 FdTableOverflow* new_overflow = reinterpret_cast<FdTableOverflow*>(allocation);
93 new_overflow->len = aligned_count;
94
95 if (atomic_compare_exchange_strong(&overflow, &local_overflow, new_overflow)) {
96 local_overflow = new_overflow;
97 } else {
98 // Someone beat us to it. Deallocate and use theirs.
99 munmap(allocation, aligned_size);
100 }
101 }
102
103 size_t offset = idx - inline_fds;
Benjamin Lerman27a37e72022-09-27 11:35:52 +0200104 if (local_overflow->len <= offset) {
Josh Gaoe6dab7b2018-08-06 18:47:29 -0700105 return nullptr;
106 }
107 return &local_overflow->entries[offset];
108}
109
Josh Gaof6e5b582018-06-01 15:30:54 -0700110void __libc_init_fdsan() {
Josh Gao6a510132019-08-20 17:59:14 -0700111 constexpr auto default_level = ANDROID_FDSAN_ERROR_LEVEL_FATAL;
112 android_fdsan_set_error_level_from_property(default_level);
Josh Gaof6e5b582018-06-01 15:30:54 -0700113}
114
Ryan Prichardabf736a2018-11-22 02:40:17 -0800115static FdTable& GetFdTable() {
116 return __libc_shared_globals()->fd_table;
Josh Gaof6e5b582018-06-01 15:30:54 -0700117}
118
Josh Gaoe16082f2018-08-06 18:48:52 -0700119// Exposed to the platform to allow crash_dump to print out the fd table.
120extern "C" void* android_fdsan_get_fd_table() {
Ryan Prichardabf736a2018-11-22 02:40:17 -0800121 return &GetFdTable();
Josh Gaoe16082f2018-08-06 18:48:52 -0700122}
123
Josh Gaof6e5b582018-06-01 15:30:54 -0700124static FdEntry* GetFdEntry(int fd) {
125 if (fd < 0) {
126 return nullptr;
127 }
128
Ryan Prichardabf736a2018-11-22 02:40:17 -0800129 return GetFdTable().at(fd);
Josh Gaof6e5b582018-06-01 15:30:54 -0700130}
131
132__printflike(1, 0) static void fdsan_error(const char* fmt, ...) {
Ryan Prichardabf736a2018-11-22 02:40:17 -0800133 auto& fd_table = GetFdTable();
Josh Gaof6e5b582018-06-01 15:30:54 -0700134
Ryan Prichardabf736a2018-11-22 02:40:17 -0800135 auto error_level = atomic_load(&fd_table.error_level);
Josh Gaof6e5b582018-06-01 15:30:54 -0700136 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
137 return;
138 }
139
Josh Gaobe66a062018-08-27 16:19:11 -0700140 struct {
141 size_t size;
142 char buf[512];
143 } abort_message;
144
Josh Gaof6e5b582018-06-01 15:30:54 -0700145 va_list va;
146 va_start(va, fmt);
Josh Gaofe380882018-07-23 18:36:16 -0700147 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_FATAL) {
148 async_safe_fatal_va_list("fdsan", fmt, va);
149 } else {
150 async_safe_format_log_va_list(ANDROID_LOG_ERROR, "fdsan", fmt, va);
Greg Hackmanncc953e02018-10-12 15:04:26 -0700151 va_end(va);
152 va_start(va, fmt);
Josh Gaobe66a062018-08-27 16:19:11 -0700153 size_t len =
154 async_safe_format_buffer_va_list(abort_message.buf, sizeof(abort_message.buf), fmt, va);
155 abort_message.size = len + sizeof(size_t);
Josh Gaofe380882018-07-23 18:36:16 -0700156 }
Josh Gaof6e5b582018-06-01 15:30:54 -0700157 va_end(va);
158
159 switch (error_level) {
160 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE:
Ryan Prichardabf736a2018-11-22 02:40:17 -0800161 atomic_compare_exchange_strong(&fd_table.error_level, &error_level,
Josh Gaof6e5b582018-06-01 15:30:54 -0700162 ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
George Burgess IVfa5410f2018-08-13 17:44:06 -0700163 __BIONIC_FALLTHROUGH;
Josh Gaofe380882018-07-23 18:36:16 -0700164 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS:
Josh Gao5074e7d2019-12-13 13:55:53 -0800165 inline_raise(BIONIC_SIGNAL_DEBUGGER, &abort_message);
Josh Gaof6e5b582018-06-01 15:30:54 -0700166 break;
167
168 case ANDROID_FDSAN_ERROR_LEVEL_FATAL:
Josh Gaof5693c62018-08-31 14:07:40 -0700169 inline_raise(SIGABRT);
Josh Gaof6e5b582018-06-01 15:30:54 -0700170 abort();
171
172 case ANDROID_FDSAN_ERROR_LEVEL_DISABLED:
Josh Gaof6e5b582018-06-01 15:30:54 -0700173 break;
174 }
175}
176
177uint64_t android_fdsan_create_owner_tag(android_fdsan_owner_type type, uint64_t tag) {
178 if (tag == 0) {
179 return 0;
180 }
181
182 if (__predict_false((type & 0xff) != type)) {
183 async_safe_fatal("invalid android_fdsan_owner_type value: %x", type);
184 }
185
186 uint64_t result = static_cast<uint64_t>(type) << 56;
187 uint64_t mask = (static_cast<uint64_t>(1) << 56) - 1;
188 result |= tag & mask;
189 return result;
190}
191
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700192const char* android_fdsan_get_tag_type(uint64_t tag) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700193 uint64_t type = tag >> 56;
194 switch (type) {
195 case ANDROID_FDSAN_OWNER_TYPE_FILE:
196 return "FILE*";
197 case ANDROID_FDSAN_OWNER_TYPE_DIR:
198 return "DIR*";
199 case ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD:
200 return "unique_fd";
201 case ANDROID_FDSAN_OWNER_TYPE_FILEINPUTSTREAM:
202 return "FileInputStream";
203 case ANDROID_FDSAN_OWNER_TYPE_FILEOUTPUTSTREAM:
204 return "FileOutputStream";
205 case ANDROID_FDSAN_OWNER_TYPE_RANDOMACCESSFILE:
206 return "RandomAccessFile";
207 case ANDROID_FDSAN_OWNER_TYPE_PARCELFILEDESCRIPTOR:
208 return "ParcelFileDescriptor";
Josh Gao27cc4bc2018-08-22 12:40:08 -0700209 case ANDROID_FDSAN_OWNER_TYPE_SQLITE:
210 return "sqlite";
Josh Gaoab6a0802018-08-30 17:37:52 -0700211 case ANDROID_FDSAN_OWNER_TYPE_ART_FDFILE:
212 return "ART FdFile";
Josh Gaod527cf12018-09-07 12:46:02 -0700213 case ANDROID_FDSAN_OWNER_TYPE_DATAGRAMSOCKETIMPL:
214 return "DatagramSocketImpl";
215 case ANDROID_FDSAN_OWNER_TYPE_SOCKETIMPL:
216 return "SocketImpl";
Josh Gao5fa9df82018-09-07 12:47:00 -0700217 case ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE:
218 return "ZipArchive";
Brian Duddie5cfca6b2022-08-24 19:51:20 +0000219 case ANDROID_FDSAN_OWNER_TYPE_NATIVE_HANDLE:
220 return "native_handle_t";
Steven Moreland1d413fc2023-06-22 23:03:57 +0000221 case ANDROID_FDSAN_OWNER_TYPE_PARCEL:
222 return "Parcel";
Josh Gaof6e5b582018-06-01 15:30:54 -0700223
224 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_00:
225 default:
226 return "native object of unknown type";
227
228 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_FF:
229 // If bits 48 to 56 are set, this is a sign-extended generic native pointer
230 uint64_t high_bits = tag >> 48;
231 if (high_bits == (1 << 16) - 1) {
232 return "native object of unknown type";
233 }
234
235 return "Java object of unknown type";
236 }
237}
238
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700239uint64_t android_fdsan_get_tag_value(uint64_t tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700240 // Lop off the most significant byte and sign extend.
241 return static_cast<uint64_t>(static_cast<int64_t>(tag << 8) >> 8);
Josh Gaof6e5b582018-06-01 15:30:54 -0700242}
243
244int android_fdsan_close_with_tag(int fd, uint64_t expected_tag) {
Josh Gao65fb2a72020-05-07 19:40:14 -0700245 if (__get_thread()->is_vforked()) {
246 return __close(fd);
247 }
248
Josh Gao97271922019-11-06 13:15:00 -0800249 FDTRACK_CLOSE(fd);
Josh Gaof6e5b582018-06-01 15:30:54 -0700250 FdEntry* fde = GetFdEntry(fd);
251 if (!fde) {
Elliott Hughes50080a22019-06-19 12:47:53 -0700252 return __close(fd);
Josh Gaof6e5b582018-06-01 15:30:54 -0700253 }
254
255 uint64_t tag = expected_tag;
256 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, 0)) {
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700257 const char* expected_type = android_fdsan_get_tag_type(expected_tag);
258 uint64_t expected_owner = android_fdsan_get_tag_value(expected_tag);
259 const char* actual_type = android_fdsan_get_tag_type(tag);
260 uint64_t actual_owner = android_fdsan_get_tag_value(tag);
Josh Gaof6e5b582018-06-01 15:30:54 -0700261 if (expected_tag && tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700262 fdsan_error(
263 "attempted to close file descriptor %d, "
264 "expected to be owned by %s 0x%" PRIx64 ", actually owned by %s 0x%" PRIx64,
265 fd, expected_type, expected_owner, actual_type, actual_owner);
Josh Gaof6e5b582018-06-01 15:30:54 -0700266 } else if (expected_tag && !tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700267 fdsan_error(
268 "attempted to close file descriptor %d, "
269 "expected to be owned by %s 0x%" PRIx64 ", actually unowned",
270 fd, expected_type, expected_owner);
Josh Gaof6e5b582018-06-01 15:30:54 -0700271 } else if (!expected_tag && tag) {
272 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700273 "attempted to close file descriptor %d, "
274 "expected to be unowned, actually owned by %s 0x%" PRIx64,
Josh Gaof6e5b582018-06-01 15:30:54 -0700275 fd, actual_type, actual_owner);
276 } else if (!expected_tag && !tag) {
277 // This should never happen: our CAS failed, but expected == actual?
Josh Gao08b7a402018-08-03 14:31:37 -0700278 async_safe_fatal("fdsan atomic_compare_exchange_strong failed unexpectedly while closing");
Josh Gaof6e5b582018-06-01 15:30:54 -0700279 }
280 }
281
Elliott Hughes50080a22019-06-19 12:47:53 -0700282 int rc = __close(fd);
Josh Gaof6e5b582018-06-01 15:30:54 -0700283 // If we were expecting to close with a tag, abort on EBADF.
284 if (expected_tag && rc == -1 && errno == EBADF) {
285 fdsan_error("double-close of file descriptor %d detected", fd);
286 }
287 return rc;
288}
289
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700290uint64_t android_fdsan_get_owner_tag(int fd) {
291 FdEntry* fde = GetFdEntry(fd);
292 if (!fde) {
293 return 0;
294 }
295 return fde->close_tag;
296}
297
Josh Gaof6e5b582018-06-01 15:30:54 -0700298void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, uint64_t new_tag) {
Josh Gao65fb2a72020-05-07 19:40:14 -0700299 if (__get_thread()->is_vforked()) {
300 return;
301 }
302
Josh Gaof6e5b582018-06-01 15:30:54 -0700303 FdEntry* fde = GetFdEntry(fd);
304 if (!fde) {
305 return;
306 }
307
308 uint64_t tag = expected_tag;
309 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, new_tag)) {
Josh Gao08b7a402018-08-03 14:31:37 -0700310 if (expected_tag && tag) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700311 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700312 "failed to exchange ownership of file descriptor: fd %d is "
313 "owned by %s 0x%" PRIx64 ", was expected to be owned by %s 0x%" PRIx64,
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700314 fd, android_fdsan_get_tag_type(tag), android_fdsan_get_tag_value(tag),
315 android_fdsan_get_tag_type(expected_tag), android_fdsan_get_tag_value(expected_tag));
Josh Gao08b7a402018-08-03 14:31:37 -0700316 } else if (expected_tag && !tag) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700317 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700318 "failed to exchange ownership of file descriptor: fd %d is "
319 "unowned, was expected to be owned by %s 0x%" PRIx64,
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700320 fd, android_fdsan_get_tag_type(expected_tag), android_fdsan_get_tag_value(expected_tag));
Josh Gao08b7a402018-08-03 14:31:37 -0700321 } else if (!expected_tag && tag) {
322 fdsan_error(
323 "failed to exchange ownership of file descriptor: fd %d is "
324 "owned by %s 0x%" PRIx64 ", was expected to be unowned",
Josh Gaob6b3a1d2018-08-23 14:04:11 -0700325 fd, android_fdsan_get_tag_type(tag), android_fdsan_get_tag_value(tag));
Josh Gao08b7a402018-08-03 14:31:37 -0700326 } else if (!expected_tag && !tag) {
327 // This should never happen: our CAS failed, but expected == actual?
328 async_safe_fatal(
329 "fdsan atomic_compare_exchange_strong failed unexpectedly while exchanging owner tag");
Josh Gaof6e5b582018-06-01 15:30:54 -0700330 }
331 }
332}
333
334android_fdsan_error_level android_fdsan_get_error_level() {
Ryan Prichardabf736a2018-11-22 02:40:17 -0800335 return GetFdTable().error_level;
Josh Gaof6e5b582018-06-01 15:30:54 -0700336}
337
338android_fdsan_error_level android_fdsan_set_error_level(android_fdsan_error_level new_level) {
Josh Gao65fb2a72020-05-07 19:40:14 -0700339 if (__get_thread()->is_vforked()) {
340 return android_fdsan_get_error_level();
341 }
342
Ryan Prichardabf736a2018-11-22 02:40:17 -0800343 return atomic_exchange(&GetFdTable().error_level, new_level);
Josh Gaof6e5b582018-06-01 15:30:54 -0700344}
345
Josh Gao6a510132019-08-20 17:59:14 -0700346android_fdsan_error_level android_fdsan_set_error_level_from_property(
347 android_fdsan_error_level default_level) {
348 const prop_info* pi = __system_property_find(kFdsanPropertyName);
349 if (!pi) {
350 return android_fdsan_set_error_level(default_level);
351 }
352
353 struct callback_data {
354 android_fdsan_error_level default_value;
355 android_fdsan_error_level result;
356 };
357
358 callback_data data;
359 data.default_value = default_level;
360
361 __system_property_read_callback(
362 pi,
363 [](void* arg, const char*, const char* value, uint32_t) {
364 callback_data* data = static_cast<callback_data*>(arg);
365
366 if (strcasecmp(value, "1") == 0 || strcasecmp(value, "fatal") == 0) {
367 data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
368 } else if (strcasecmp(value, "warn") == 0) {
369 data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS);
370 } else if (strcasecmp(value, "warn_once") == 0) {
371 data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
372 } else {
373 if (strlen(value) != 0 && strcasecmp(value, "0") != 0) {
374 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
375 "debug.fdsan set to unknown value '%s', disabling", value);
376 }
377 data->result = android_fdsan_set_error_level(data->default_value);
378 }
379 },
380 &data);
381
382 return data.result;
383}
384
Josh Gaof6e5b582018-06-01 15:30:54 -0700385int close(int fd) {
386 int rc = android_fdsan_close_with_tag(fd, 0);
387 if (rc == -1 && errno == EINTR) {
388 return 0;
389 }
390 return rc;
391}