Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Mitch Phillips | 4c9293f | 2020-02-14 14:50:35 -0800 | [diff] [blame] | 29 | #include <platform/bionic/android_unsafe_frame_pointer_chase.h> |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 30 | #include <platform/bionic/malloc.h> |
Mitch Phillips | 0083b0f | 2020-02-13 17:37:11 -0800 | [diff] [blame] | 31 | #include <private/bionic_arc4random.h> |
Mitch Phillips | 4c9293f | 2020-02-14 14:50:35 -0800 | [diff] [blame] | 32 | #include <private/bionic_globals.h> |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 33 | #include <private/bionic_malloc_dispatch.h> |
| 34 | #include <stddef.h> |
| 35 | #include <stdint.h> |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
| 38 | #include <sys/types.h> |
| 39 | |
| 40 | #include "bionic/gwp_asan_wrappers.h" |
| 41 | #include "gwp_asan/guarded_pool_allocator.h" |
| 42 | #include "gwp_asan/options.h" |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 43 | #include "malloc_common.h" |
| 44 | |
| 45 | #ifndef LIBC_STATIC |
| 46 | #include "bionic/malloc_common_dynamic.h" |
| 47 | #endif // LIBC_STATIC |
| 48 | |
| 49 | static gwp_asan::GuardedPoolAllocator GuardedAlloc; |
| 50 | static const MallocDispatch* prev_dispatch; |
| 51 | |
| 52 | using Options = gwp_asan::options::Options; |
| 53 | |
| 54 | // ============================================================================ |
| 55 | // Implementation of gFunctions. |
| 56 | // ============================================================================ |
| 57 | |
| 58 | // This function handles initialisation as asked for by MallocInitImpl. This |
| 59 | // should always be called in a single-threaded context. |
| 60 | bool gwp_asan_initialize(const MallocDispatch* dispatch, bool*, const char*) { |
| 61 | prev_dispatch = dispatch; |
| 62 | |
| 63 | Options Opts; |
| 64 | Opts.Enabled = true; |
| 65 | Opts.MaxSimultaneousAllocations = 32; |
| 66 | Opts.SampleRate = 2500; |
| 67 | Opts.InstallSignalHandlers = false; |
| 68 | Opts.InstallForkHandlers = true; |
Mitch Phillips | 4c9293f | 2020-02-14 14:50:35 -0800 | [diff] [blame] | 69 | Opts.Backtrace = android_unsafe_frame_pointer_chase; |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 70 | |
| 71 | GuardedAlloc.init(Opts); |
Mitch Phillips | 4d62ffc | 2020-02-18 17:13:52 -0800 | [diff] [blame] | 72 | // TODO(b/149790891): The log line below causes ART tests to fail as they're |
| 73 | // not expecting any output. Disable the output for now. |
| 74 | // info_log("GWP-ASan has been enabled."); |
Mitch Phillips | 4c9293f | 2020-02-14 14:50:35 -0800 | [diff] [blame] | 75 | |
| 76 | __libc_shared_globals()->gwp_asan_state = GuardedAlloc.getAllocatorState(); |
| 77 | __libc_shared_globals()->gwp_asan_metadata = GuardedAlloc.getMetadataRegion(); |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | |
| 81 | void gwp_asan_finalize() { |
| 82 | } |
| 83 | |
| 84 | void gwp_asan_get_malloc_leak_info(uint8_t**, size_t*, size_t*, size_t*, size_t*) { |
| 85 | } |
| 86 | |
| 87 | void gwp_asan_free_malloc_leak_info(uint8_t*) { |
| 88 | } |
| 89 | |
| 90 | ssize_t gwp_asan_malloc_backtrace(void*, uintptr_t*, size_t) { |
| 91 | // TODO(mitchp): GWP-ASan might be able to return the backtrace for the |
| 92 | // provided address. |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | bool gwp_asan_write_malloc_leak_info(FILE*) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | void* gwp_asan_gfunctions[] = { |
| 101 | (void*)gwp_asan_initialize, (void*)gwp_asan_finalize, |
| 102 | (void*)gwp_asan_get_malloc_leak_info, (void*)gwp_asan_free_malloc_leak_info, |
| 103 | (void*)gwp_asan_malloc_backtrace, (void*)gwp_asan_write_malloc_leak_info, |
| 104 | }; |
| 105 | |
| 106 | // ============================================================================ |
| 107 | // Implementation of GWP-ASan malloc wrappers. |
| 108 | // ============================================================================ |
| 109 | |
| 110 | void* gwp_asan_calloc(size_t n_elements, size_t elem_size) { |
| 111 | if (__predict_false(GuardedAlloc.shouldSample())) { |
| 112 | size_t bytes; |
| 113 | if (!__builtin_mul_overflow(n_elements, elem_size, &bytes)) { |
| 114 | if (void* result = GuardedAlloc.allocate(bytes)) { |
| 115 | return result; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | return prev_dispatch->calloc(n_elements, elem_size); |
| 120 | } |
| 121 | |
| 122 | void gwp_asan_free(void* mem) { |
| 123 | if (__predict_false(GuardedAlloc.pointerIsMine(mem))) { |
| 124 | GuardedAlloc.deallocate(mem); |
| 125 | return; |
| 126 | } |
| 127 | prev_dispatch->free(mem); |
| 128 | } |
| 129 | |
| 130 | void* gwp_asan_malloc(size_t bytes) { |
| 131 | if (__predict_false(GuardedAlloc.shouldSample())) { |
| 132 | if (void* result = GuardedAlloc.allocate(bytes)) { |
| 133 | return result; |
| 134 | } |
| 135 | } |
| 136 | return prev_dispatch->malloc(bytes); |
| 137 | } |
| 138 | |
| 139 | size_t gwp_asan_malloc_usable_size(const void* mem) { |
| 140 | if (__predict_false(GuardedAlloc.pointerIsMine(mem))) { |
| 141 | return GuardedAlloc.getSize(mem); |
| 142 | } |
| 143 | return prev_dispatch->malloc_usable_size(mem); |
| 144 | } |
| 145 | |
| 146 | void* gwp_asan_realloc(void* old_mem, size_t bytes) { |
| 147 | if (__predict_false(GuardedAlloc.pointerIsMine(old_mem))) { |
| 148 | size_t old_size = GuardedAlloc.getSize(old_mem); |
| 149 | void* new_ptr = gwp_asan_malloc(bytes); |
| 150 | if (new_ptr) memcpy(new_ptr, old_mem, (bytes < old_size) ? bytes : old_size); |
| 151 | GuardedAlloc.deallocate(old_mem); |
| 152 | return new_ptr; |
| 153 | } |
| 154 | return prev_dispatch->realloc(old_mem, bytes); |
| 155 | } |
| 156 | |
| 157 | int gwp_asan_malloc_iterate(uintptr_t base, size_t size, |
| 158 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { |
| 159 | if (__predict_false(GuardedAlloc.pointerIsMine(reinterpret_cast<void*>(base)))) { |
| 160 | // TODO(mitchp): GPA::iterate() returns void, but should return int. |
| 161 | // TODO(mitchp): GPA::iterate() should take uintptr_t, not void*. |
| 162 | GuardedAlloc.iterate(reinterpret_cast<void*>(base), size, callback, arg); |
| 163 | return 0; |
| 164 | } |
| 165 | return prev_dispatch->malloc_iterate(base, size, callback, arg); |
| 166 | } |
| 167 | |
| 168 | void gwp_asan_malloc_disable() { |
| 169 | GuardedAlloc.disable(); |
| 170 | prev_dispatch->malloc_disable(); |
| 171 | } |
| 172 | |
| 173 | void gwp_asan_malloc_enable() { |
| 174 | GuardedAlloc.enable(); |
| 175 | prev_dispatch->malloc_enable(); |
| 176 | } |
| 177 | |
| 178 | static const MallocDispatch gwp_asan_dispatch __attribute__((unused)) = { |
Mitch Phillips | 32abb0b | 2020-03-16 09:52:45 -0700 | [diff] [blame] | 179 | gwp_asan_calloc, |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 180 | gwp_asan_free, |
| 181 | Malloc(mallinfo), |
| 182 | gwp_asan_malloc, |
| 183 | gwp_asan_malloc_usable_size, |
| 184 | Malloc(memalign), |
| 185 | Malloc(posix_memalign), |
| 186 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 187 | Malloc(pvalloc), |
| 188 | #endif |
| 189 | gwp_asan_realloc, |
| 190 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 191 | Malloc(valloc), |
| 192 | #endif |
| 193 | gwp_asan_malloc_iterate, |
| 194 | gwp_asan_malloc_disable, |
| 195 | gwp_asan_malloc_enable, |
| 196 | Malloc(mallopt), |
| 197 | Malloc(aligned_alloc), |
| 198 | Malloc(malloc_info), |
| 199 | }; |
| 200 | |
Mitch Phillips | 0083b0f | 2020-02-13 17:37:11 -0800 | [diff] [blame] | 201 | // The probability (1 / kProcessSampleRate) that a process will be ranodmly |
| 202 | // selected for sampling. kProcessSampleRate should always be a power of two to |
| 203 | // avoid modulo bias. |
| 204 | static constexpr uint8_t kProcessSampleRate = 128; |
| 205 | |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 206 | bool ShouldGwpAsanSampleProcess() { |
Mitch Phillips | 0083b0f | 2020-02-13 17:37:11 -0800 | [diff] [blame] | 207 | uint8_t random_number; |
| 208 | __libc_safe_arc4random_buf(&random_number, sizeof(random_number)); |
| 209 | return random_number % kProcessSampleRate == 0; |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Mitch Phillips | bba80dc | 2020-02-11 14:42:14 -0800 | [diff] [blame] | 212 | bool MaybeInitGwpAsanFromLibc(libc_globals* globals) { |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 213 | // Never initialize the Zygote here. A Zygote chosen for sampling would also |
| 214 | // have all of its children sampled. Instead, the Zygote child will choose |
| 215 | // whether it samples or not just after the Zygote forks. For |
| 216 | // libc_scudo-preloaded executables (like mediaswcodec), the program name |
| 217 | // might not be available yet. The zygote never uses dynamic libc_scudo. |
| 218 | const char* progname = getprogname(); |
| 219 | if (progname && strncmp(progname, "app_process", 11) == 0) { |
| 220 | return false; |
| 221 | } |
Mitch Phillips | bba80dc | 2020-02-11 14:42:14 -0800 | [diff] [blame] | 222 | return MaybeInitGwpAsan(globals); |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static bool GwpAsanInitialized = false; |
| 226 | |
| 227 | // Maybe initializes GWP-ASan. Called by android_mallopt() and libc's |
| 228 | // initialisation. This should always be called in a single-threaded context. |
Mitch Phillips | bba80dc | 2020-02-11 14:42:14 -0800 | [diff] [blame] | 229 | bool MaybeInitGwpAsan(libc_globals* globals, bool force_init) { |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 230 | if (GwpAsanInitialized) { |
| 231 | error_log("GWP-ASan was already initialized for this process."); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | // If the caller hasn't forced GWP-ASan on, check whether we should sample |
| 236 | // this process. |
| 237 | if (!force_init && !ShouldGwpAsanSampleProcess()) { |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | // GWP-ASan is compatible with heapprofd/malloc_debug/malloc_hooks iff |
| 242 | // GWP-ASan was installed first. If one of these other libraries was already |
| 243 | // installed, we don't enable GWP-ASan. These libraries are normally enabled |
| 244 | // in libc_init after GWP-ASan, but if the new process is a zygote child and |
| 245 | // trying to initialize GWP-ASan through mallopt(), one of these libraries may |
| 246 | // be installed. It may be possible to change this in future by modifying the |
| 247 | // internal dispatch pointers of these libraries at this point in time, but |
| 248 | // given that they're all debug-only, we don't really mind for now. |
| 249 | if (GetDefaultDispatchTable() != nullptr) { |
| 250 | // Something else is installed. |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | // GWP-ASan's initialization is always called in a single-threaded context, so |
| 255 | // we can initialize lock-free. |
Mitch Phillips | bba80dc | 2020-02-11 14:42:14 -0800 | [diff] [blame] | 256 | // Set GWP-ASan as the malloc dispatch table. |
| 257 | globals->malloc_dispatch_table = gwp_asan_dispatch; |
| 258 | atomic_store(&globals->default_dispatch_table, &gwp_asan_dispatch); |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 259 | |
Mitch Phillips | bba80dc | 2020-02-11 14:42:14 -0800 | [diff] [blame] | 260 | // If malloc_limit isn't installed, we can skip the default_dispatch_table |
| 261 | // lookup. |
| 262 | if (GetDispatchTable() == nullptr) { |
| 263 | atomic_store(&globals->current_dispatch_table, &gwp_asan_dispatch); |
| 264 | } |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 265 | |
| 266 | #ifndef LIBC_STATIC |
| 267 | SetGlobalFunctions(gwp_asan_gfunctions); |
| 268 | #endif // LIBC_STATIC |
| 269 | |
| 270 | GwpAsanInitialized = true; |
| 271 | |
| 272 | gwp_asan_initialize(NativeAllocatorDispatch(), nullptr, nullptr); |
| 273 | |
| 274 | return true; |
| 275 | } |
Mitch Phillips | c03856c | 2020-02-13 16:41:14 -0800 | [diff] [blame] | 276 | |
| 277 | bool DispatchIsGwpAsan(const MallocDispatch* dispatch) { |
| 278 | return dispatch == &gwp_asan_dispatch; |
| 279 | } |