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> |
Josh Gao | 5074e7d | 2019-12-13 13:55:53 -0800 | [diff] [blame] | 43 | #include <platform/bionic/reserved_signals.h> |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 44 | #include <sys/system_properties.h> |
| 45 | |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame^] | 46 | #include "private/bionic_fdtrack.h" |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 47 | #include "private/bionic_globals.h" |
Josh Gao | f5693c6 | 2018-08-31 14:07:40 -0700 | [diff] [blame] | 48 | #include "private/bionic_inline_raise.h" |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 49 | #include "pthread_internal.h" |
| 50 | |
Elliott Hughes | 50080a2 | 2019-06-19 12:47:53 -0700 | [diff] [blame] | 51 | extern "C" int __close(int fd); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 52 | pid_t __get_cached_pid(); |
| 53 | |
| 54 | static constexpr const char* kFdsanPropertyName = "debug.fdsan"; |
| 55 | |
Josh Gao | e6dab7b | 2018-08-06 18:47:29 -0700 | [diff] [blame] | 56 | template<size_t inline_fds> |
| 57 | FdEntry* 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; |
| 104 | if (local_overflow->len < offset) { |
| 105 | return nullptr; |
| 106 | } |
| 107 | return &local_overflow->entries[offset]; |
| 108 | } |
| 109 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 110 | void __libc_init_fdsan() { |
Josh Gao | 6a51013 | 2019-08-20 17:59:14 -0700 | [diff] [blame] | 111 | constexpr auto default_level = ANDROID_FDSAN_ERROR_LEVEL_FATAL; |
| 112 | android_fdsan_set_error_level_from_property(default_level); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 115 | static FdTable& GetFdTable() { |
| 116 | return __libc_shared_globals()->fd_table; |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Josh Gao | e16082f | 2018-08-06 18:48:52 -0700 | [diff] [blame] | 119 | // Exposed to the platform to allow crash_dump to print out the fd table. |
| 120 | extern "C" void* android_fdsan_get_fd_table() { |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 121 | return &GetFdTable(); |
Josh Gao | e16082f | 2018-08-06 18:48:52 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 124 | static FdEntry* GetFdEntry(int fd) { |
| 125 | if (fd < 0) { |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 129 | return GetFdTable().at(fd); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | __printflike(1, 0) static void fdsan_error(const char* fmt, ...) { |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 133 | auto& fd_table = GetFdTable(); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 134 | |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 135 | auto error_level = atomic_load(&fd_table.error_level); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 136 | if (error_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | // Lots of code will (sensibly) fork, blindly call close on all of their fds, |
| 141 | // and then exec. Compare our cached pid value against the real one to detect |
| 142 | // this scenario and permit it. |
| 143 | pid_t cached_pid = __get_cached_pid(); |
| 144 | if (cached_pid == 0 || cached_pid != syscall(__NR_getpid)) { |
| 145 | return; |
| 146 | } |
| 147 | |
Josh Gao | be66a06 | 2018-08-27 16:19:11 -0700 | [diff] [blame] | 148 | struct { |
| 149 | size_t size; |
| 150 | char buf[512]; |
| 151 | } abort_message; |
| 152 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 153 | va_list va; |
| 154 | va_start(va, fmt); |
Josh Gao | fe38088 | 2018-07-23 18:36:16 -0700 | [diff] [blame] | 155 | if (error_level == ANDROID_FDSAN_ERROR_LEVEL_FATAL) { |
| 156 | async_safe_fatal_va_list("fdsan", fmt, va); |
| 157 | } else { |
| 158 | async_safe_format_log_va_list(ANDROID_LOG_ERROR, "fdsan", fmt, va); |
Greg Hackmann | cc953e0 | 2018-10-12 15:04:26 -0700 | [diff] [blame] | 159 | va_end(va); |
| 160 | va_start(va, fmt); |
Josh Gao | be66a06 | 2018-08-27 16:19:11 -0700 | [diff] [blame] | 161 | size_t len = |
| 162 | async_safe_format_buffer_va_list(abort_message.buf, sizeof(abort_message.buf), fmt, va); |
| 163 | abort_message.size = len + sizeof(size_t); |
Josh Gao | fe38088 | 2018-07-23 18:36:16 -0700 | [diff] [blame] | 164 | } |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 165 | va_end(va); |
| 166 | |
| 167 | switch (error_level) { |
| 168 | case ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE: |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 169 | atomic_compare_exchange_strong(&fd_table.error_level, &error_level, |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 170 | ANDROID_FDSAN_ERROR_LEVEL_DISABLED); |
George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 171 | __BIONIC_FALLTHROUGH; |
Josh Gao | fe38088 | 2018-07-23 18:36:16 -0700 | [diff] [blame] | 172 | case ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS: |
Josh Gao | 5074e7d | 2019-12-13 13:55:53 -0800 | [diff] [blame] | 173 | inline_raise(BIONIC_SIGNAL_DEBUGGER, &abort_message); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 174 | break; |
| 175 | |
| 176 | case ANDROID_FDSAN_ERROR_LEVEL_FATAL: |
Josh Gao | f5693c6 | 2018-08-31 14:07:40 -0700 | [diff] [blame] | 177 | inline_raise(SIGABRT); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 178 | abort(); |
| 179 | |
| 180 | case ANDROID_FDSAN_ERROR_LEVEL_DISABLED: |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | uint64_t android_fdsan_create_owner_tag(android_fdsan_owner_type type, uint64_t tag) { |
| 186 | if (tag == 0) { |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | if (__predict_false((type & 0xff) != type)) { |
| 191 | async_safe_fatal("invalid android_fdsan_owner_type value: %x", type); |
| 192 | } |
| 193 | |
| 194 | uint64_t result = static_cast<uint64_t>(type) << 56; |
| 195 | uint64_t mask = (static_cast<uint64_t>(1) << 56) - 1; |
| 196 | result |= tag & mask; |
| 197 | return result; |
| 198 | } |
| 199 | |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 200 | const char* android_fdsan_get_tag_type(uint64_t tag) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 201 | uint64_t type = tag >> 56; |
| 202 | switch (type) { |
| 203 | case ANDROID_FDSAN_OWNER_TYPE_FILE: |
| 204 | return "FILE*"; |
| 205 | case ANDROID_FDSAN_OWNER_TYPE_DIR: |
| 206 | return "DIR*"; |
| 207 | case ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD: |
| 208 | return "unique_fd"; |
| 209 | case ANDROID_FDSAN_OWNER_TYPE_FILEINPUTSTREAM: |
| 210 | return "FileInputStream"; |
| 211 | case ANDROID_FDSAN_OWNER_TYPE_FILEOUTPUTSTREAM: |
| 212 | return "FileOutputStream"; |
| 213 | case ANDROID_FDSAN_OWNER_TYPE_RANDOMACCESSFILE: |
| 214 | return "RandomAccessFile"; |
| 215 | case ANDROID_FDSAN_OWNER_TYPE_PARCELFILEDESCRIPTOR: |
| 216 | return "ParcelFileDescriptor"; |
Josh Gao | 27cc4bc | 2018-08-22 12:40:08 -0700 | [diff] [blame] | 217 | case ANDROID_FDSAN_OWNER_TYPE_SQLITE: |
| 218 | return "sqlite"; |
Josh Gao | ab6a080 | 2018-08-30 17:37:52 -0700 | [diff] [blame] | 219 | case ANDROID_FDSAN_OWNER_TYPE_ART_FDFILE: |
| 220 | return "ART FdFile"; |
Josh Gao | d527cf1 | 2018-09-07 12:46:02 -0700 | [diff] [blame] | 221 | case ANDROID_FDSAN_OWNER_TYPE_DATAGRAMSOCKETIMPL: |
| 222 | return "DatagramSocketImpl"; |
| 223 | case ANDROID_FDSAN_OWNER_TYPE_SOCKETIMPL: |
| 224 | return "SocketImpl"; |
Josh Gao | 5fa9df8 | 2018-09-07 12:47:00 -0700 | [diff] [blame] | 225 | case ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE: |
| 226 | return "ZipArchive"; |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 227 | |
| 228 | case ANDROID_FDSAN_OWNER_TYPE_GENERIC_00: |
| 229 | default: |
| 230 | return "native object of unknown type"; |
| 231 | |
| 232 | case ANDROID_FDSAN_OWNER_TYPE_GENERIC_FF: |
| 233 | // If bits 48 to 56 are set, this is a sign-extended generic native pointer |
| 234 | uint64_t high_bits = tag >> 48; |
| 235 | if (high_bits == (1 << 16) - 1) { |
| 236 | return "native object of unknown type"; |
| 237 | } |
| 238 | |
| 239 | return "Java object of unknown type"; |
| 240 | } |
| 241 | } |
| 242 | |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 243 | uint64_t android_fdsan_get_tag_value(uint64_t tag) { |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 244 | // Lop off the most significant byte and sign extend. |
| 245 | return static_cast<uint64_t>(static_cast<int64_t>(tag << 8) >> 8); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | int android_fdsan_close_with_tag(int fd, uint64_t expected_tag) { |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame^] | 249 | FDTRACK_CLOSE(fd); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 250 | FdEntry* fde = GetFdEntry(fd); |
| 251 | if (!fde) { |
Elliott Hughes | 50080a2 | 2019-06-19 12:47:53 -0700 | [diff] [blame] | 252 | return __close(fd); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | uint64_t tag = expected_tag; |
| 256 | if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, 0)) { |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 257 | 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 Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 261 | if (expected_tag && tag) { |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 262 | 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 Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 266 | } else if (expected_tag && !tag) { |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 267 | 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 Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 271 | } else if (!expected_tag && tag) { |
| 272 | fdsan_error( |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 273 | "attempted to close file descriptor %d, " |
| 274 | "expected to be unowned, actually owned by %s 0x%" PRIx64, |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 275 | fd, actual_type, actual_owner); |
| 276 | } else if (!expected_tag && !tag) { |
| 277 | // This should never happen: our CAS failed, but expected == actual? |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 278 | async_safe_fatal("fdsan atomic_compare_exchange_strong failed unexpectedly while closing"); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Elliott Hughes | 50080a2 | 2019-06-19 12:47:53 -0700 | [diff] [blame] | 282 | int rc = __close(fd); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 283 | // 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 Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 290 | uint64_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 Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 298 | void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, uint64_t new_tag) { |
| 299 | FdEntry* fde = GetFdEntry(fd); |
| 300 | if (!fde) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | uint64_t tag = expected_tag; |
| 305 | if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, new_tag)) { |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 306 | if (expected_tag && tag) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 307 | fdsan_error( |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 308 | "failed to exchange ownership of file descriptor: fd %d is " |
| 309 | "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] | 310 | fd, android_fdsan_get_tag_type(tag), android_fdsan_get_tag_value(tag), |
| 311 | 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] | 312 | } else if (expected_tag && !tag) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 313 | fdsan_error( |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 314 | "failed to exchange ownership of file descriptor: fd %d is " |
| 315 | "unowned, was expected to be owned by %s 0x%" PRIx64, |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 316 | 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] | 317 | } else if (!expected_tag && tag) { |
| 318 | fdsan_error( |
| 319 | "failed to exchange ownership of file descriptor: fd %d is " |
| 320 | "owned by %s 0x%" PRIx64 ", was expected to be unowned", |
Josh Gao | b6b3a1d | 2018-08-23 14:04:11 -0700 | [diff] [blame] | 321 | 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] | 322 | } else if (!expected_tag && !tag) { |
| 323 | // This should never happen: our CAS failed, but expected == actual? |
| 324 | async_safe_fatal( |
| 325 | "fdsan atomic_compare_exchange_strong failed unexpectedly while exchanging owner tag"); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | android_fdsan_error_level android_fdsan_get_error_level() { |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 331 | return GetFdTable().error_level; |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | android_fdsan_error_level android_fdsan_set_error_level(android_fdsan_error_level new_level) { |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 335 | return atomic_exchange(&GetFdTable().error_level, new_level); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Josh Gao | 6a51013 | 2019-08-20 17:59:14 -0700 | [diff] [blame] | 338 | android_fdsan_error_level android_fdsan_set_error_level_from_property( |
| 339 | android_fdsan_error_level default_level) { |
| 340 | const prop_info* pi = __system_property_find(kFdsanPropertyName); |
| 341 | if (!pi) { |
| 342 | return android_fdsan_set_error_level(default_level); |
| 343 | } |
| 344 | |
| 345 | struct callback_data { |
| 346 | android_fdsan_error_level default_value; |
| 347 | android_fdsan_error_level result; |
| 348 | }; |
| 349 | |
| 350 | callback_data data; |
| 351 | data.default_value = default_level; |
| 352 | |
| 353 | __system_property_read_callback( |
| 354 | pi, |
| 355 | [](void* arg, const char*, const char* value, uint32_t) { |
| 356 | callback_data* data = static_cast<callback_data*>(arg); |
| 357 | |
| 358 | if (strcasecmp(value, "1") == 0 || strcasecmp(value, "fatal") == 0) { |
| 359 | data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL); |
| 360 | } else if (strcasecmp(value, "warn") == 0) { |
| 361 | data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS); |
| 362 | } else if (strcasecmp(value, "warn_once") == 0) { |
| 363 | data->result = android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE); |
| 364 | } else { |
| 365 | if (strlen(value) != 0 && strcasecmp(value, "0") != 0) { |
| 366 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", |
| 367 | "debug.fdsan set to unknown value '%s', disabling", value); |
| 368 | } |
| 369 | data->result = android_fdsan_set_error_level(data->default_value); |
| 370 | } |
| 371 | }, |
| 372 | &data); |
| 373 | |
| 374 | return data.result; |
| 375 | } |
| 376 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 377 | int close(int fd) { |
| 378 | int rc = android_fdsan_close_with_tag(fd, 0); |
| 379 | if (rc == -1 && errno == EINTR) { |
| 380 | return 0; |
| 381 | } |
| 382 | return rc; |
| 383 | } |