| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Gao | fe38088 | 2018-07-23 18:36:16 -0700 | [diff] [blame] | 33 | #include <signal.h> | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 34 | #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> | 
| Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame] | 43 | #include <platform/bionic/page.h> | 
| Josh Gao | 5074e7d | 2019-12-13 13:55:53 -0800 | [diff] [blame] | 44 | #include <platform/bionic/reserved_signals.h> | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 45 | #include <sys/system_properties.h> | 
|  | 46 |  | 
| Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 47 | #include "private/bionic_fdtrack.h" | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 48 | #include "private/bionic_globals.h" | 
| Josh Gao | f5693c6 | 2018-08-31 14:07:40 -0700 | [diff] [blame] | 49 | #include "private/bionic_inline_raise.h" | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 50 | #include "pthread_internal.h" | 
|  | 51 |  | 
| Elliott Hughes | 50080a2 | 2019-06-19 12:47:53 -0700 | [diff] [blame] | 52 | extern "C" int __close(int fd); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 53 | pid_t __get_cached_pid(); | 
|  | 54 |  | 
|  | 55 | static constexpr const char* kFdsanPropertyName = "debug.fdsan"; | 
|  | 56 |  | 
| Josh Gao | e6dab7b | 2018-08-06 18:47:29 -0700 | [diff] [blame] | 57 | template<size_t inline_fds> | 
|  | 58 | FdEntry* FdTableImpl<inline_fds>::at(size_t idx) { | 
|  | 59 | if (idx < inline_fds) { | 
|  | 60 | return &entries[idx]; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | // Try to create the overflow table ourselves. | 
|  | 64 | FdTableOverflow* local_overflow = atomic_load(&overflow); | 
|  | 65 | if (__predict_false(!local_overflow)) { | 
|  | 66 | struct rlimit rlim = { .rlim_max = 32768 }; | 
|  | 67 | getrlimit(RLIMIT_NOFILE, &rlim); | 
|  | 68 | rlim_t max = rlim.rlim_max; | 
|  | 69 |  | 
|  | 70 | if (max == RLIM_INFINITY) { | 
|  | 71 | // This isn't actually possible (the kernel has a hard limit), but just | 
|  | 72 | // in case... | 
|  | 73 | max = 32768; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | if (idx > max) { | 
|  | 77 | // This can happen if an fd is created and then the rlimit is lowered. | 
|  | 78 | // In this case, just return nullptr and ignore the fd. | 
|  | 79 | return nullptr; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | size_t required_count = max - inline_fds; | 
|  | 83 | size_t required_size = sizeof(FdTableOverflow) + required_count * sizeof(FdEntry); | 
| Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame] | 84 | size_t aligned_size = __BIONIC_ALIGN(required_size, page_size()); | 
| Josh Gao | e6dab7b | 2018-08-06 18:47:29 -0700 | [diff] [blame] | 85 | size_t aligned_count = (aligned_size - sizeof(FdTableOverflow)) / sizeof(FdEntry); | 
|  | 86 |  | 
|  | 87 | void* allocation = | 
|  | 88 | mmap(nullptr, aligned_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | 
|  | 89 | if (allocation == MAP_FAILED) { | 
| Elliott Hughes | 2557f73 | 2023-07-12 21:15:23 +0000 | [diff] [blame] | 90 | async_safe_fatal("fdsan: mmap failed: %m"); | 
| Josh Gao | e6dab7b | 2018-08-06 18:47:29 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | FdTableOverflow* new_overflow = reinterpret_cast<FdTableOverflow*>(allocation); | 
|  | 94 | new_overflow->len = aligned_count; | 
|  | 95 |  | 
|  | 96 | if (atomic_compare_exchange_strong(&overflow, &local_overflow, new_overflow)) { | 
|  | 97 | local_overflow = new_overflow; | 
|  | 98 | } else { | 
|  | 99 | // Someone beat us to it. Deallocate and use theirs. | 
|  | 100 | munmap(allocation, aligned_size); | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | size_t offset = idx - inline_fds; | 
| Benjamin Lerman | 27a37e7 | 2022-09-27 11:35:52 +0200 | [diff] [blame] | 105 | if (local_overflow->len <= offset) { | 
| Josh Gao | e6dab7b | 2018-08-06 18:47:29 -0700 | [diff] [blame] | 106 | return nullptr; | 
|  | 107 | } | 
|  | 108 | return &local_overflow->entries[offset]; | 
|  | 109 | } | 
|  | 110 |  | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 111 | void __libc_init_fdsan() { | 
| Josh Gao | 6a51013 | 2019-08-20 17:59:14 -0700 | [diff] [blame] | 112 | constexpr auto default_level = ANDROID_FDSAN_ERROR_LEVEL_FATAL; | 
|  | 113 | android_fdsan_set_error_level_from_property(default_level); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 114 | } | 
|  | 115 |  | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 116 | static FdTable& GetFdTable() { | 
|  | 117 | return __libc_shared_globals()->fd_table; | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
| Josh Gao | e16082f | 2018-08-06 18:48:52 -0700 | [diff] [blame] | 120 | // Exposed to the platform to allow crash_dump to print out the fd table. | 
|  | 121 | extern "C" void* android_fdsan_get_fd_table() { | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 122 | return &GetFdTable(); | 
| Josh Gao | e16082f | 2018-08-06 18:48:52 -0700 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 125 | static FdEntry* GetFdEntry(int fd) { | 
|  | 126 | if (fd < 0) { | 
|  | 127 | return nullptr; | 
|  | 128 | } | 
|  | 129 |  | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 130 | return GetFdTable().at(fd); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
|  | 133 | __printflike(1, 0) static void fdsan_error(const char* fmt, ...) { | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 134 | auto& fd_table = GetFdTable(); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 135 |  | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 136 | auto error_level = atomic_load(&fd_table.error_level); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 137 | if (error_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) { | 
|  | 138 | return; | 
|  | 139 | } | 
|  | 140 |  | 
| Josh Gao | be66a06 | 2018-08-27 16:19:11 -0700 | [diff] [blame] | 141 | struct { | 
|  | 142 | size_t size; | 
|  | 143 | char buf[512]; | 
|  | 144 | } abort_message; | 
|  | 145 |  | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 146 | va_list va; | 
|  | 147 | va_start(va, fmt); | 
| Josh Gao | fe38088 | 2018-07-23 18:36:16 -0700 | [diff] [blame] | 148 | if (error_level == ANDROID_FDSAN_ERROR_LEVEL_FATAL) { | 
|  | 149 | async_safe_fatal_va_list("fdsan", fmt, va); | 
|  | 150 | } else { | 
|  | 151 | async_safe_format_log_va_list(ANDROID_LOG_ERROR, "fdsan", fmt, va); | 
| Greg Hackmann | cc953e0 | 2018-10-12 15:04:26 -0700 | [diff] [blame] | 152 | va_end(va); | 
|  | 153 | va_start(va, fmt); | 
| Josh Gao | be66a06 | 2018-08-27 16:19:11 -0700 | [diff] [blame] | 154 | size_t len = | 
|  | 155 | async_safe_format_buffer_va_list(abort_message.buf, sizeof(abort_message.buf), fmt, va); | 
|  | 156 | abort_message.size = len + sizeof(size_t); | 
| Josh Gao | fe38088 | 2018-07-23 18:36:16 -0700 | [diff] [blame] | 157 | } | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 158 | va_end(va); | 
|  | 159 |  | 
|  | 160 | switch (error_level) { | 
|  | 161 | case ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE: | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 162 | atomic_compare_exchange_strong(&fd_table.error_level, &error_level, | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 163 | ANDROID_FDSAN_ERROR_LEVEL_DISABLED); | 
| George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 164 | __BIONIC_FALLTHROUGH; | 
| Josh Gao | fe38088 | 2018-07-23 18:36:16 -0700 | [diff] [blame] | 165 | case ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS: | 
| Josh Gao | 5074e7d | 2019-12-13 13:55:53 -0800 | [diff] [blame] | 166 | inline_raise(BIONIC_SIGNAL_DEBUGGER, &abort_message); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 167 | break; | 
|  | 168 |  | 
|  | 169 | case ANDROID_FDSAN_ERROR_LEVEL_FATAL: | 
| Josh Gao | f5693c6 | 2018-08-31 14:07:40 -0700 | [diff] [blame] | 170 | inline_raise(SIGABRT); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 171 | abort(); | 
|  | 172 |  | 
|  | 173 | case ANDROID_FDSAN_ERROR_LEVEL_DISABLED: | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 174 | break; | 
|  | 175 | } | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | uint64_t android_fdsan_create_owner_tag(android_fdsan_owner_type type, uint64_t tag) { | 
|  | 179 | if (tag == 0) { | 
|  | 180 | return 0; | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | if (__predict_false((type & 0xff) != type)) { | 
|  | 184 | async_safe_fatal("invalid android_fdsan_owner_type value: %x", type); | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | uint64_t result = static_cast<uint64_t>(type) << 56; | 
|  | 188 | uint64_t mask = (static_cast<uint64_t>(1) << 56) - 1; | 
|  | 189 | result |= tag & mask; | 
|  | 190 | return result; | 
|  | 191 | } | 
|  | 192 |  | 
| Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 193 | const char* android_fdsan_get_tag_type(uint64_t tag) { | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 194 | uint64_t type = tag >> 56; | 
|  | 195 | switch (type) { | 
|  | 196 | case ANDROID_FDSAN_OWNER_TYPE_FILE: | 
|  | 197 | return "FILE*"; | 
|  | 198 | case ANDROID_FDSAN_OWNER_TYPE_DIR: | 
|  | 199 | return "DIR*"; | 
|  | 200 | case ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD: | 
|  | 201 | return "unique_fd"; | 
|  | 202 | case ANDROID_FDSAN_OWNER_TYPE_FILEINPUTSTREAM: | 
|  | 203 | return "FileInputStream"; | 
|  | 204 | case ANDROID_FDSAN_OWNER_TYPE_FILEOUTPUTSTREAM: | 
|  | 205 | return "FileOutputStream"; | 
|  | 206 | case ANDROID_FDSAN_OWNER_TYPE_RANDOMACCESSFILE: | 
|  | 207 | return "RandomAccessFile"; | 
|  | 208 | case ANDROID_FDSAN_OWNER_TYPE_PARCELFILEDESCRIPTOR: | 
|  | 209 | return "ParcelFileDescriptor"; | 
| Josh Gao | 27cc4bc | 2018-08-22 12:40:08 -0700 | [diff] [blame] | 210 | case ANDROID_FDSAN_OWNER_TYPE_SQLITE: | 
|  | 211 | return "sqlite"; | 
| Josh Gao | ab6a080 | 2018-08-30 17:37:52 -0700 | [diff] [blame] | 212 | case ANDROID_FDSAN_OWNER_TYPE_ART_FDFILE: | 
|  | 213 | return "ART FdFile"; | 
| Josh Gao | d527cf1 | 2018-09-07 12:46:02 -0700 | [diff] [blame] | 214 | case ANDROID_FDSAN_OWNER_TYPE_DATAGRAMSOCKETIMPL: | 
|  | 215 | return "DatagramSocketImpl"; | 
|  | 216 | case ANDROID_FDSAN_OWNER_TYPE_SOCKETIMPL: | 
|  | 217 | return "SocketImpl"; | 
| Josh Gao | 5fa9df8 | 2018-09-07 12:47:00 -0700 | [diff] [blame] | 218 | case ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE: | 
|  | 219 | return "ZipArchive"; | 
| Brian Duddie | 5cfca6b | 2022-08-24 19:51:20 +0000 | [diff] [blame] | 220 | case ANDROID_FDSAN_OWNER_TYPE_NATIVE_HANDLE: | 
|  | 221 | return "native_handle_t"; | 
| Steven Moreland | 1d413fc | 2023-06-22 23:03:57 +0000 | [diff] [blame] | 222 | case ANDROID_FDSAN_OWNER_TYPE_PARCEL: | 
|  | 223 | return "Parcel"; | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 224 |  | 
|  | 225 | case ANDROID_FDSAN_OWNER_TYPE_GENERIC_00: | 
|  | 226 | default: | 
|  | 227 | return "native object of unknown type"; | 
|  | 228 |  | 
|  | 229 | case ANDROID_FDSAN_OWNER_TYPE_GENERIC_FF: | 
|  | 230 | // If bits 48 to 56 are set, this is a sign-extended generic native pointer | 
|  | 231 | uint64_t high_bits = tag >> 48; | 
|  | 232 | if (high_bits == (1 << 16) - 1) { | 
|  | 233 | return "native object of unknown type"; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | return "Java object of unknown type"; | 
|  | 237 | } | 
|  | 238 | } | 
|  | 239 |  | 
| Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 240 | uint64_t android_fdsan_get_tag_value(uint64_t tag) { | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 241 | // Lop off the most significant byte and sign extend. | 
|  | 242 | return static_cast<uint64_t>(static_cast<int64_t>(tag << 8) >> 8); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
|  | 245 | int android_fdsan_close_with_tag(int fd, uint64_t expected_tag) { | 
| Josh Gao | 65fb2a7 | 2020-05-07 19:40:14 -0700 | [diff] [blame] | 246 | if (__get_thread()->is_vforked()) { | 
|  | 247 | return __close(fd); | 
|  | 248 | } | 
|  | 249 |  | 
| Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 250 | FDTRACK_CLOSE(fd); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 251 | FdEntry* fde = GetFdEntry(fd); | 
|  | 252 | if (!fde) { | 
| Elliott Hughes | 50080a2 | 2019-06-19 12:47:53 -0700 | [diff] [blame] | 253 | return __close(fd); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 254 | } | 
|  | 255 |  | 
|  | 256 | uint64_t tag = expected_tag; | 
|  | 257 | if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, 0)) { | 
| Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 258 | const char* expected_type = android_fdsan_get_tag_type(expected_tag); | 
|  | 259 | uint64_t expected_owner = android_fdsan_get_tag_value(expected_tag); | 
|  | 260 | const char* actual_type = android_fdsan_get_tag_type(tag); | 
|  | 261 | uint64_t actual_owner = android_fdsan_get_tag_value(tag); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 262 | if (expected_tag && tag) { | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 263 | fdsan_error( | 
|  | 264 | "attempted to close file descriptor %d, " | 
|  | 265 | "expected to be owned by %s 0x%" PRIx64 ", actually owned by %s 0x%" PRIx64, | 
|  | 266 | fd, expected_type, expected_owner, actual_type, actual_owner); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 267 | } else if (expected_tag && !tag) { | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 268 | fdsan_error( | 
|  | 269 | "attempted to close file descriptor %d, " | 
|  | 270 | "expected to be owned by %s 0x%" PRIx64 ", actually unowned", | 
|  | 271 | fd, expected_type, expected_owner); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 272 | } else if (!expected_tag && tag) { | 
|  | 273 | fdsan_error( | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 274 | "attempted to close file descriptor %d, " | 
|  | 275 | "expected to be unowned, actually owned by %s 0x%" PRIx64, | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 276 | fd, actual_type, actual_owner); | 
|  | 277 | } else if (!expected_tag && !tag) { | 
|  | 278 | // This should never happen: our CAS failed, but expected == actual? | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 279 | async_safe_fatal("fdsan atomic_compare_exchange_strong failed unexpectedly while closing"); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 280 | } | 
|  | 281 | } | 
|  | 282 |  | 
| Elliott Hughes | 50080a2 | 2019-06-19 12:47:53 -0700 | [diff] [blame] | 283 | int rc = __close(fd); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 284 | // If we were expecting to close with a tag, abort on EBADF. | 
|  | 285 | if (expected_tag && rc == -1 && errno == EBADF) { | 
|  | 286 | fdsan_error("double-close of file descriptor %d detected", fd); | 
|  | 287 | } | 
|  | 288 | return rc; | 
|  | 289 | } | 
|  | 290 |  | 
| Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 291 | uint64_t android_fdsan_get_owner_tag(int fd) { | 
|  | 292 | FdEntry* fde = GetFdEntry(fd); | 
|  | 293 | if (!fde) { | 
|  | 294 | return 0; | 
|  | 295 | } | 
|  | 296 | return fde->close_tag; | 
|  | 297 | } | 
|  | 298 |  | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 299 | void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, uint64_t new_tag) { | 
| Josh Gao | 65fb2a7 | 2020-05-07 19:40:14 -0700 | [diff] [blame] | 300 | if (__get_thread()->is_vforked()) { | 
|  | 301 | return; | 
|  | 302 | } | 
|  | 303 |  | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 304 | FdEntry* fde = GetFdEntry(fd); | 
|  | 305 | if (!fde) { | 
|  | 306 | return; | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | uint64_t tag = expected_tag; | 
|  | 310 | if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, new_tag)) { | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 311 | if (expected_tag && tag) { | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 312 | fdsan_error( | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 313 | "failed to exchange ownership of file descriptor: fd %d is " | 
|  | 314 | "owned by %s 0x%" PRIx64 ", was expected to be owned by %s 0x%" PRIx64, | 
| Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 315 | fd, android_fdsan_get_tag_type(tag), android_fdsan_get_tag_value(tag), | 
|  | 316 | android_fdsan_get_tag_type(expected_tag), android_fdsan_get_tag_value(expected_tag)); | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 317 | } else if (expected_tag && !tag) { | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 318 | fdsan_error( | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 319 | "failed to exchange ownership of file descriptor: fd %d is " | 
|  | 320 | "unowned, was expected to be owned by %s 0x%" PRIx64, | 
| Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 321 | fd, android_fdsan_get_tag_type(expected_tag), android_fdsan_get_tag_value(expected_tag)); | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 322 | } else if (!expected_tag && tag) { | 
|  | 323 | fdsan_error( | 
|  | 324 | "failed to exchange ownership of file descriptor: fd %d is " | 
|  | 325 | "owned by %s 0x%" PRIx64 ", was expected to be unowned", | 
| Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 326 | fd, android_fdsan_get_tag_type(tag), android_fdsan_get_tag_value(tag)); | 
| Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 327 | } else if (!expected_tag && !tag) { | 
|  | 328 | // This should never happen: our CAS failed, but expected == actual? | 
|  | 329 | async_safe_fatal( | 
|  | 330 | "fdsan atomic_compare_exchange_strong failed unexpectedly while exchanging owner tag"); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 331 | } | 
|  | 332 | } | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | android_fdsan_error_level android_fdsan_get_error_level() { | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 336 | return GetFdTable().error_level; | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
|  | 339 | android_fdsan_error_level android_fdsan_set_error_level(android_fdsan_error_level new_level) { | 
| Josh Gao | 65fb2a7 | 2020-05-07 19:40:14 -0700 | [diff] [blame] | 340 | if (__get_thread()->is_vforked()) { | 
|  | 341 | return android_fdsan_get_error_level(); | 
|  | 342 | } | 
|  | 343 |  | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 344 | return atomic_exchange(&GetFdTable().error_level, new_level); | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 345 | } | 
|  | 346 |  | 
| Josh Gao | 6a51013 | 2019-08-20 17:59:14 -0700 | [diff] [blame] | 347 | android_fdsan_error_level android_fdsan_set_error_level_from_property( | 
|  | 348 | android_fdsan_error_level default_level) { | 
|  | 349 | const prop_info* pi = __system_property_find(kFdsanPropertyName); | 
|  | 350 | if (!pi) { | 
|  | 351 | return android_fdsan_set_error_level(default_level); | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | struct callback_data { | 
|  | 355 | android_fdsan_error_level default_value; | 
|  | 356 | android_fdsan_error_level result; | 
|  | 357 | }; | 
|  | 358 |  | 
|  | 359 | callback_data data; | 
|  | 360 | data.default_value = default_level; | 
|  | 361 |  | 
|  | 362 | __system_property_read_callback( | 
|  | 363 | pi, | 
|  | 364 | [](void* arg, const char*, const char* value, uint32_t) { | 
|  | 365 | callback_data* data = static_cast<callback_data*>(arg); | 
|  | 366 |  | 
|  | 367 | if (strcasecmp(value, "1") == 0 || strcasecmp(value, "fatal") == 0) { | 
|  | 368 | data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL); | 
|  | 369 | } else if (strcasecmp(value, "warn") == 0) { | 
|  | 370 | data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS); | 
|  | 371 | } else if (strcasecmp(value, "warn_once") == 0) { | 
|  | 372 | data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE); | 
|  | 373 | } else { | 
|  | 374 | if (strlen(value) != 0 && strcasecmp(value, "0") != 0) { | 
|  | 375 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", | 
|  | 376 | "debug.fdsan set to unknown value '%s', disabling", value); | 
|  | 377 | } | 
|  | 378 | data->result = android_fdsan_set_error_level(data->default_value); | 
|  | 379 | } | 
|  | 380 | }, | 
|  | 381 | &data); | 
|  | 382 |  | 
|  | 383 | return data.result; | 
|  | 384 | } | 
|  | 385 |  | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 386 | int close(int fd) { | 
|  | 387 | int rc = android_fdsan_close_with_tag(fd, 0); | 
|  | 388 | if (rc == -1 && errno == EINTR) { | 
|  | 389 | return 0; | 
|  | 390 | } | 
|  | 391 | return rc; | 
|  | 392 | } |