blob: a56d77adfcff7a0f9ab0b3bdd2003674743b2408 [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"
46#include "pthread_internal.h"
47
48extern "C" int ___close(int fd);
49pid_t __get_cached_pid();
50
51static constexpr const char* kFdsanPropertyName = "debug.fdsan";
52
53void __libc_init_fdsan() {
54 const prop_info* pi = __system_property_find(kFdsanPropertyName);
55 if (!pi) {
56 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
57 return;
58 }
59 __system_property_read_callback(
60 pi,
61 [](void*, const char*, const char* value, uint32_t) {
62 if (strcasecmp(value, "1") == 0 || strcasecmp(value, "fatal") == 0) {
63 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
64 } else if (strcasecmp(value, "warn") == 0) {
65 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS);
66 } else if (strcasecmp(value, "warn_once") == 0) {
67 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
68 } else {
69 if (strlen(value) != 0 && strcasecmp(value, "0") != 0) {
70 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
71 "debug.fdsan set to unknown value '%s', disabling", value);
72 }
73 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
74 }
75 },
76 nullptr);
77}
78
79static FdTable<128>* GetFdTable() {
80 if (!__libc_shared_globals) {
81 return nullptr;
82 }
83
84 return &__libc_shared_globals->fd_table;
85}
86
87static FdEntry* GetFdEntry(int fd) {
88 if (fd < 0) {
89 return nullptr;
90 }
91
92 auto* fd_table = GetFdTable();
93 if (!fd_table) {
94 return nullptr;
95 }
96
97 return fd_table->at(fd);
98}
99
100__printflike(1, 0) static void fdsan_error(const char* fmt, ...) {
101 auto* fd_table = GetFdTable();
102 if (!fd_table) {
103 return;
104 }
105
106 auto error_level = atomic_load(&fd_table->error_level);
107 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
108 return;
109 }
110
111 // Lots of code will (sensibly) fork, blindly call close on all of their fds,
112 // and then exec. Compare our cached pid value against the real one to detect
113 // this scenario and permit it.
114 pid_t cached_pid = __get_cached_pid();
115 if (cached_pid == 0 || cached_pid != syscall(__NR_getpid)) {
116 return;
117 }
118
119 va_list va;
120 va_start(va, fmt);
Josh Gaofe380882018-07-23 18:36:16 -0700121 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_FATAL) {
122 async_safe_fatal_va_list("fdsan", fmt, va);
123 } else {
124 async_safe_format_log_va_list(ANDROID_LOG_ERROR, "fdsan", fmt, va);
125 }
Josh Gaof6e5b582018-06-01 15:30:54 -0700126 va_end(va);
127
128 switch (error_level) {
129 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE:
130 atomic_compare_exchange_strong(&fd_table->error_level, &error_level,
131 ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
Josh Gaofe380882018-07-23 18:36:16 -0700132 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS:
133 // DEBUGGER_SIGNAL
134 raise(__SIGRTMIN + 3);
Josh Gaof6e5b582018-06-01 15:30:54 -0700135 break;
136
137 case ANDROID_FDSAN_ERROR_LEVEL_FATAL:
138 abort();
139
140 case ANDROID_FDSAN_ERROR_LEVEL_DISABLED:
Josh Gaof6e5b582018-06-01 15:30:54 -0700141 break;
142 }
143}
144
145uint64_t android_fdsan_create_owner_tag(android_fdsan_owner_type type, uint64_t tag) {
146 if (tag == 0) {
147 return 0;
148 }
149
150 if (__predict_false((type & 0xff) != type)) {
151 async_safe_fatal("invalid android_fdsan_owner_type value: %x", type);
152 }
153
154 uint64_t result = static_cast<uint64_t>(type) << 56;
155 uint64_t mask = (static_cast<uint64_t>(1) << 56) - 1;
156 result |= tag & mask;
157 return result;
158}
159
160static const char* __tag_to_type(uint64_t tag) {
161 uint64_t type = tag >> 56;
162 switch (type) {
163 case ANDROID_FDSAN_OWNER_TYPE_FILE:
164 return "FILE*";
165 case ANDROID_FDSAN_OWNER_TYPE_DIR:
166 return "DIR*";
167 case ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD:
168 return "unique_fd";
169 case ANDROID_FDSAN_OWNER_TYPE_FILEINPUTSTREAM:
170 return "FileInputStream";
171 case ANDROID_FDSAN_OWNER_TYPE_FILEOUTPUTSTREAM:
172 return "FileOutputStream";
173 case ANDROID_FDSAN_OWNER_TYPE_RANDOMACCESSFILE:
174 return "RandomAccessFile";
175 case ANDROID_FDSAN_OWNER_TYPE_PARCELFILEDESCRIPTOR:
176 return "ParcelFileDescriptor";
177
178 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_00:
179 default:
180 return "native object of unknown type";
181
182 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_FF:
183 // If bits 48 to 56 are set, this is a sign-extended generic native pointer
184 uint64_t high_bits = tag >> 48;
185 if (high_bits == (1 << 16) - 1) {
186 return "native object of unknown type";
187 }
188
189 return "Java object of unknown type";
190 }
191}
192
193static uint64_t __tag_to_owner(uint64_t tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700194 // Lop off the most significant byte and sign extend.
195 return static_cast<uint64_t>(static_cast<int64_t>(tag << 8) >> 8);
Josh Gaof6e5b582018-06-01 15:30:54 -0700196}
197
198int android_fdsan_close_with_tag(int fd, uint64_t expected_tag) {
199 FdEntry* fde = GetFdEntry(fd);
200 if (!fde) {
201 return ___close(fd);
202 }
203
204 uint64_t tag = expected_tag;
205 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, 0)) {
206 const char* expected_type = __tag_to_type(expected_tag);
207 uint64_t expected_owner = __tag_to_owner(expected_tag);
208 const char* actual_type = __tag_to_type(tag);
209 uint64_t actual_owner = __tag_to_owner(tag);
210 if (expected_tag && tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700211 fdsan_error(
212 "attempted to close file descriptor %d, "
213 "expected to be owned by %s 0x%" PRIx64 ", actually owned by %s 0x%" PRIx64,
214 fd, expected_type, expected_owner, actual_type, actual_owner);
Josh Gaof6e5b582018-06-01 15:30:54 -0700215 } else if (expected_tag && !tag) {
Josh Gao08b7a402018-08-03 14:31:37 -0700216 fdsan_error(
217 "attempted to close file descriptor %d, "
218 "expected to be owned by %s 0x%" PRIx64 ", actually unowned",
219 fd, expected_type, expected_owner);
Josh Gaof6e5b582018-06-01 15:30:54 -0700220 } else if (!expected_tag && tag) {
221 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700222 "attempted to close file descriptor %d, "
223 "expected to be unowned, actually owned by %s 0x%" PRIx64,
Josh Gaof6e5b582018-06-01 15:30:54 -0700224 fd, actual_type, actual_owner);
225 } else if (!expected_tag && !tag) {
226 // This should never happen: our CAS failed, but expected == actual?
Josh Gao08b7a402018-08-03 14:31:37 -0700227 async_safe_fatal("fdsan atomic_compare_exchange_strong failed unexpectedly while closing");
Josh Gaof6e5b582018-06-01 15:30:54 -0700228 }
229 }
230
231 int rc = ___close(fd);
232 // If we were expecting to close with a tag, abort on EBADF.
233 if (expected_tag && rc == -1 && errno == EBADF) {
234 fdsan_error("double-close of file descriptor %d detected", fd);
235 }
236 return rc;
237}
238
239void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, uint64_t new_tag) {
240 FdEntry* fde = GetFdEntry(fd);
241 if (!fde) {
242 return;
243 }
244
245 uint64_t tag = expected_tag;
246 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, new_tag)) {
Josh Gao08b7a402018-08-03 14:31:37 -0700247 if (expected_tag && tag) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700248 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700249 "failed to exchange ownership of file descriptor: fd %d is "
250 "owned by %s 0x%" PRIx64 ", was expected to be owned by %s 0x%" PRIx64,
251 fd, __tag_to_type(tag), __tag_to_owner(tag), __tag_to_type(expected_tag),
252 __tag_to_owner(expected_tag));
253 } else if (expected_tag && !tag) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700254 fdsan_error(
Josh Gao08b7a402018-08-03 14:31:37 -0700255 "failed to exchange ownership of file descriptor: fd %d is "
256 "unowned, was expected to be owned by %s 0x%" PRIx64,
Josh Gaof6e5b582018-06-01 15:30:54 -0700257 fd, __tag_to_type(expected_tag), __tag_to_owner(expected_tag));
Josh Gao08b7a402018-08-03 14:31:37 -0700258 } else if (!expected_tag && tag) {
259 fdsan_error(
260 "failed to exchange ownership of file descriptor: fd %d is "
261 "owned by %s 0x%" PRIx64 ", was expected to be unowned",
262 fd, __tag_to_type(tag), __tag_to_owner(tag));
263 } else if (!expected_tag && !tag) {
264 // This should never happen: our CAS failed, but expected == actual?
265 async_safe_fatal(
266 "fdsan atomic_compare_exchange_strong failed unexpectedly while exchanging owner tag");
Josh Gaof6e5b582018-06-01 15:30:54 -0700267 }
268 }
269}
270
271android_fdsan_error_level android_fdsan_get_error_level() {
272 auto* fd_table = GetFdTable();
273 if (!fd_table) {
274 async_safe_fatal("attempted to get fdsan error level before libc initialization?");
275 }
276
277 return fd_table->error_level;
278}
279
280android_fdsan_error_level android_fdsan_set_error_level(android_fdsan_error_level new_level) {
281 auto* fd_table = GetFdTable();
282 if (!fd_table) {
283 async_safe_fatal("attempted to get fdsan error level before libc initialization?");
284 }
285
286 return atomic_exchange(&fd_table->error_level, new_level);
287}
288
289int close(int fd) {
290 int rc = android_fdsan_close_with_tag(fd, 0);
291 if (rc == -1 && errno == EINTR) {
292 return 0;
293 }
294 return rc;
295}