1 //===-- asan_linux.cpp ----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of AddressSanitizer, an address sanity checker. 10 // 11 // Linux-specific details. 12 //===----------------------------------------------------------------------===// 13 14 #include "sanitizer_common/sanitizer_platform.h" 15 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \ 16 SANITIZER_SOLARIS 17 18 #include "asan_interceptors.h" 19 #include "asan_internal.h" 20 #include "asan_premap_shadow.h" 21 #include "asan_thread.h" 22 #include "sanitizer_common/sanitizer_flags.h" 23 #include "sanitizer_common/sanitizer_freebsd.h" 24 #include "sanitizer_common/sanitizer_libc.h" 25 #include "sanitizer_common/sanitizer_procmaps.h" 26 27 #include <sys/time.h> 28 #include <sys/resource.h> 29 #include <sys/mman.h> 30 #include <sys/syscall.h> 31 #include <sys/types.h> 32 #include <dlfcn.h> 33 #include <fcntl.h> 34 #include <limits.h> 35 #include <pthread.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 #include <unwind.h> 39 40 #if SANITIZER_FREEBSD 41 #include <sys/link_elf.h> 42 #endif 43 44 #if SANITIZER_SOLARIS 45 #include <link.h> 46 #endif 47 48 #if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS 49 #include <ucontext.h> 50 extern "C" void* _DYNAMIC; 51 #elif SANITIZER_NETBSD 52 #include <link_elf.h> 53 #include <ucontext.h> 54 extern Elf_Dyn _DYNAMIC; 55 #else 56 #include <sys/ucontext.h> 57 #include <link.h> 58 #endif 59 60 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in 61 // 32-bit mode. 62 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \ 63 __FreeBSD_version <= 902001 // v9.2 64 #define ucontext_t xucontext_t 65 #endif 66 67 typedef enum { 68 ASAN_RT_VERSION_UNDEFINED = 0, 69 ASAN_RT_VERSION_DYNAMIC, 70 ASAN_RT_VERSION_STATIC, 71 } asan_rt_version_t; 72 73 // FIXME: perhaps also store abi version here? 74 extern "C" { 75 SANITIZER_INTERFACE_ATTRIBUTE 76 asan_rt_version_t __asan_rt_version; 77 } 78 79 namespace __asan { 80 81 void InitializePlatformInterceptors() {} 82 void InitializePlatformExceptionHandlers() {} 83 bool IsSystemHeapAddress (uptr addr) { return false; } 84 85 void *AsanDoesNotSupportStaticLinkage() { 86 // This will fail to link with -static. 87 return &_DYNAMIC; // defined in link.h 88 } 89 90 #if ASAN_PREMAP_SHADOW 91 uptr FindPremappedShadowStart(uptr shadow_size_bytes) { 92 uptr granularity = GetMmapGranularity(); 93 uptr shadow_start = reinterpret_cast<uptr>(&__asan_shadow); 94 uptr premap_shadow_size = PremapShadowSize(); 95 uptr shadow_size = RoundUpTo(shadow_size_bytes, granularity); 96 // We may have mapped too much. Release extra memory. 97 UnmapFromTo(shadow_start + shadow_size, shadow_start + premap_shadow_size); 98 return shadow_start; 99 } 100 #endif 101 102 uptr FindDynamicShadowStart() { 103 uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd); 104 #if ASAN_PREMAP_SHADOW 105 if (!PremapShadowFailed()) 106 return FindPremappedShadowStart(shadow_size_bytes); 107 #endif 108 109 return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE, 110 /*min_shadow_base_alignment*/ 0, kHighMemEnd); 111 } 112 113 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) { 114 UNIMPLEMENTED(); 115 } 116 117 void FlushUnneededASanShadowMemory(uptr p, uptr size) { 118 // Since asan's mapping is compacting, the shadow chunk may be 119 // not page-aligned, so we only flush the page-aligned portion. 120 ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size)); 121 } 122 123 #if SANITIZER_ANDROID 124 // FIXME: should we do anything for Android? 125 void AsanCheckDynamicRTPrereqs() {} 126 void AsanCheckIncompatibleRT() {} 127 #else 128 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size, 129 void *data) { 130 VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n", 131 info->dlpi_name, info->dlpi_addr); 132 133 // Continue until the first dynamic library is found 134 if (!info->dlpi_name || info->dlpi_name[0] == 0) 135 return 0; 136 137 // Ignore vDSO 138 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0) 139 return 0; 140 141 #if SANITIZER_FREEBSD || SANITIZER_NETBSD 142 // Ignore first entry (the main program) 143 char **p = (char **)data; 144 if (!(*p)) { 145 *p = (char *)-1; 146 return 0; 147 } 148 #endif 149 150 #if SANITIZER_SOLARIS 151 // Ignore executable on Solaris 152 if (info->dlpi_addr == 0) 153 return 0; 154 #endif 155 156 *(const char **)data = info->dlpi_name; 157 return 1; 158 } 159 160 static bool IsDynamicRTName(const char *libname) { 161 return internal_strstr(libname, "libclang_rt.asan") || 162 internal_strstr(libname, "libasan.so"); 163 } 164 165 static void ReportIncompatibleRT() { 166 Report("Your application is linked against incompatible ASan runtimes.\n"); 167 Die(); 168 } 169 170 void AsanCheckDynamicRTPrereqs() { 171 if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order) 172 return; 173 174 // Ensure that dynamic RT is the first DSO in the list 175 const char *first_dso_name = nullptr; 176 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name); 177 if (first_dso_name && !IsDynamicRTName(first_dso_name)) { 178 Report("ASan runtime does not come first in initial library list; " 179 "you should either link runtime to your application or " 180 "manually preload it with LD_PRELOAD.\n"); 181 Die(); 182 } 183 } 184 185 void AsanCheckIncompatibleRT() { 186 if (ASAN_DYNAMIC) { 187 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) { 188 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC; 189 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) { 190 ReportIncompatibleRT(); 191 } 192 } else { 193 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) { 194 // Ensure that dynamic runtime is not present. We should detect it 195 // as early as possible, otherwise ASan interceptors could bind to 196 // the functions in dynamic ASan runtime instead of the functions in 197 // system libraries, causing crashes later in ASan initialization. 198 MemoryMappingLayout proc_maps(/*cache_enabled*/true); 199 char filename[PATH_MAX]; 200 MemoryMappedSegment segment(filename, sizeof(filename)); 201 while (proc_maps.Next(&segment)) { 202 if (IsDynamicRTName(segment.filename)) { 203 Report("Your application is linked against " 204 "incompatible ASan runtimes.\n"); 205 Die(); 206 } 207 } 208 __asan_rt_version = ASAN_RT_VERSION_STATIC; 209 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) { 210 ReportIncompatibleRT(); 211 } 212 } 213 } 214 #endif // SANITIZER_ANDROID 215 216 #if !SANITIZER_ANDROID 217 void ReadContextStack(void *context, uptr *stack, uptr *ssize) { 218 ucontext_t *ucp = (ucontext_t*)context; 219 *stack = (uptr)ucp->uc_stack.ss_sp; 220 *ssize = ucp->uc_stack.ss_size; 221 } 222 #else 223 void ReadContextStack(void *context, uptr *stack, uptr *ssize) { 224 UNIMPLEMENTED(); 225 } 226 #endif 227 228 void *AsanDlSymNext(const char *sym) { 229 return dlsym(RTLD_NEXT, sym); 230 } 231 232 bool HandleDlopenInit() { 233 // Not supported on this platform. 234 static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN, 235 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false"); 236 return false; 237 } 238 239 } // namespace __asan 240 241 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || 242 // SANITIZER_SOLARIS 243