blob: df369cc818b71dbfc160efe8a4c925ce05d84ad3 [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>
33#include <stdarg.h>
34#include <stdatomic.h>
35#include <string.h>
36#include <sys/cdefs.h>
37#include <sys/mman.h>
38#include <sys/syscall.h>
39#include <unistd.h>
40
41#include <async_safe/log.h>
42#include <sys/system_properties.h>
43
44#include "private/bionic_globals.h"
45#include "pthread_internal.h"
46
47extern "C" int ___close(int fd);
48pid_t __get_cached_pid();
49
50static constexpr const char* kFdsanPropertyName = "debug.fdsan";
51
52void __libc_init_fdsan() {
53 const prop_info* pi = __system_property_find(kFdsanPropertyName);
54 if (!pi) {
55 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
56 return;
57 }
58 __system_property_read_callback(
59 pi,
60 [](void*, const char*, const char* value, uint32_t) {
61 if (strcasecmp(value, "1") == 0 || strcasecmp(value, "fatal") == 0) {
62 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
63 } else if (strcasecmp(value, "warn") == 0) {
64 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS);
65 } else if (strcasecmp(value, "warn_once") == 0) {
66 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
67 } else {
68 if (strlen(value) != 0 && strcasecmp(value, "0") != 0) {
69 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
70 "debug.fdsan set to unknown value '%s', disabling", value);
71 }
72 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
73 }
74 },
75 nullptr);
76}
77
78static FdTable<128>* GetFdTable() {
79 if (!__libc_shared_globals) {
80 return nullptr;
81 }
82
83 return &__libc_shared_globals->fd_table;
84}
85
86static FdEntry* GetFdEntry(int fd) {
87 if (fd < 0) {
88 return nullptr;
89 }
90
91 auto* fd_table = GetFdTable();
92 if (!fd_table) {
93 return nullptr;
94 }
95
96 return fd_table->at(fd);
97}
98
99__printflike(1, 0) static void fdsan_error(const char* fmt, ...) {
100 auto* fd_table = GetFdTable();
101 if (!fd_table) {
102 return;
103 }
104
105 auto error_level = atomic_load(&fd_table->error_level);
106 if (error_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
107 return;
108 }
109
110 // Lots of code will (sensibly) fork, blindly call close on all of their fds,
111 // and then exec. Compare our cached pid value against the real one to detect
112 // this scenario and permit it.
113 pid_t cached_pid = __get_cached_pid();
114 if (cached_pid == 0 || cached_pid != syscall(__NR_getpid)) {
115 return;
116 }
117
118 va_list va;
119 va_start(va, fmt);
120 async_safe_fatal_va_list("fdsan", fmt, va);
121 va_end(va);
122
123 switch (error_level) {
124 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE:
125 atomic_compare_exchange_strong(&fd_table->error_level, &error_level,
126 ANDROID_FDSAN_ERROR_LEVEL_DISABLED);
127 break;
128
129 case ANDROID_FDSAN_ERROR_LEVEL_FATAL:
130 abort();
131
132 case ANDROID_FDSAN_ERROR_LEVEL_DISABLED:
133 case ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS:
134 break;
135 }
136}
137
138uint64_t android_fdsan_create_owner_tag(android_fdsan_owner_type type, uint64_t tag) {
139 if (tag == 0) {
140 return 0;
141 }
142
143 if (__predict_false((type & 0xff) != type)) {
144 async_safe_fatal("invalid android_fdsan_owner_type value: %x", type);
145 }
146
147 uint64_t result = static_cast<uint64_t>(type) << 56;
148 uint64_t mask = (static_cast<uint64_t>(1) << 56) - 1;
149 result |= tag & mask;
150 return result;
151}
152
153static const char* __tag_to_type(uint64_t tag) {
154 uint64_t type = tag >> 56;
155 switch (type) {
156 case ANDROID_FDSAN_OWNER_TYPE_FILE:
157 return "FILE*";
158 case ANDROID_FDSAN_OWNER_TYPE_DIR:
159 return "DIR*";
160 case ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD:
161 return "unique_fd";
162 case ANDROID_FDSAN_OWNER_TYPE_FILEINPUTSTREAM:
163 return "FileInputStream";
164 case ANDROID_FDSAN_OWNER_TYPE_FILEOUTPUTSTREAM:
165 return "FileOutputStream";
166 case ANDROID_FDSAN_OWNER_TYPE_RANDOMACCESSFILE:
167 return "RandomAccessFile";
168 case ANDROID_FDSAN_OWNER_TYPE_PARCELFILEDESCRIPTOR:
169 return "ParcelFileDescriptor";
170
171 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_00:
172 default:
173 return "native object of unknown type";
174
175 case ANDROID_FDSAN_OWNER_TYPE_GENERIC_FF:
176 // If bits 48 to 56 are set, this is a sign-extended generic native pointer
177 uint64_t high_bits = tag >> 48;
178 if (high_bits == (1 << 16) - 1) {
179 return "native object of unknown type";
180 }
181
182 return "Java object of unknown type";
183 }
184}
185
186static uint64_t __tag_to_owner(uint64_t tag) {
187 return tag;
188}
189
190int android_fdsan_close_with_tag(int fd, uint64_t expected_tag) {
191 FdEntry* fde = GetFdEntry(fd);
192 if (!fde) {
193 return ___close(fd);
194 }
195
196 uint64_t tag = expected_tag;
197 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, 0)) {
198 const char* expected_type = __tag_to_type(expected_tag);
199 uint64_t expected_owner = __tag_to_owner(expected_tag);
200 const char* actual_type = __tag_to_type(tag);
201 uint64_t actual_owner = __tag_to_owner(tag);
202 if (expected_tag && tag) {
203 fdsan_error("attempted to close file descriptor %d, expected to be owned by %s 0x%" PRIx64
204 ", actually owned by %s 0x%" PRIx64,
205 fd, expected_type, expected_owner, actual_type, actual_owner);
206 } else if (expected_tag && !tag) {
207 fdsan_error("attempted to close file descriptor %d, expected to be owned by %s 0x%" PRIx64
208 ", actually unowned",
209 fd, expected_type, expected_owner);
210 } else if (!expected_tag && tag) {
211 fdsan_error(
212 "attempted to close file descriptor %d, expected to be unowned, actually owned by %s "
213 "0x%" PRIx64,
214 fd, actual_type, actual_owner);
215 } else if (!expected_tag && !tag) {
216 // This should never happen: our CAS failed, but expected == actual?
217 async_safe_fatal("fdsan atomic_compare_exchange_strong failed unexpectedly");
218 }
219 }
220
221 int rc = ___close(fd);
222 // If we were expecting to close with a tag, abort on EBADF.
223 if (expected_tag && rc == -1 && errno == EBADF) {
224 fdsan_error("double-close of file descriptor %d detected", fd);
225 }
226 return rc;
227}
228
229void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, uint64_t new_tag) {
230 FdEntry* fde = GetFdEntry(fd);
231 if (!fde) {
232 return;
233 }
234
235 uint64_t tag = expected_tag;
236 if (!atomic_compare_exchange_strong(&fde->close_tag, &tag, new_tag)) {
237 if (expected_tag == 0) {
238 fdsan_error(
239 "failed to take ownership of already-owned file descriptor: fd %d is owned by %s "
240 "%" PRIx64,
241 fd, __tag_to_type(tag), __tag_to_owner(tag));
242 } else {
243 fdsan_error(
244 "failed to exchange ownership of unowned file descriptor: expected fd %d to be owned "
245 "by %s %" PRIx64,
246 fd, __tag_to_type(expected_tag), __tag_to_owner(expected_tag));
247 }
248 }
249}
250
251android_fdsan_error_level android_fdsan_get_error_level() {
252 auto* fd_table = GetFdTable();
253 if (!fd_table) {
254 async_safe_fatal("attempted to get fdsan error level before libc initialization?");
255 }
256
257 return fd_table->error_level;
258}
259
260android_fdsan_error_level android_fdsan_set_error_level(android_fdsan_error_level new_level) {
261 auto* fd_table = GetFdTable();
262 if (!fd_table) {
263 async_safe_fatal("attempted to get fdsan error level before libc initialization?");
264 }
265
266 return atomic_exchange(&fd_table->error_level, new_level);
267}
268
269int close(int fd) {
270 int rc = android_fdsan_close_with_tag(fd, 0);
271 if (rc == -1 && errno == EINTR) {
272 return 0;
273 }
274 return rc;
275}