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