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