1 //===-- asan_linux.cc -----------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Linux-specific details.
11 //===----------------------------------------------------------------------===//
12
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
15
16 #include "asan_interceptors.h"
17 #include "asan_internal.h"
18 #include "asan_thread.h"
19 #include "sanitizer_common/sanitizer_flags.h"
20 #include "sanitizer_common/sanitizer_freebsd.h"
21 #include "sanitizer_common/sanitizer_libc.h"
22 #include "sanitizer_common/sanitizer_procmaps.h"
23
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #include <sys/mman.h>
27 #include <sys/syscall.h>
28 #include <sys/types.h>
29 #include <dlfcn.h>
30 #include <fcntl.h>
31 #include <pthread.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <unwind.h>
35
36 #if SANITIZER_FREEBSD
37 #include <sys/link_elf.h>
38 #endif
39
40 #if SANITIZER_ANDROID || SANITIZER_FREEBSD
41 #include <ucontext.h>
42 extern "C" void* _DYNAMIC;
43 #elif SANITIZER_NETBSD
44 #include <link_elf.h>
45 #include <ucontext.h>
46 extern Elf_Dyn _DYNAMIC;
47 #else
48 #include <sys/ucontext.h>
49 #include <link.h>
50 #endif
51
52 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
53 // 32-bit mode.
54 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
55 __FreeBSD_version <= 902001 // v9.2
56 #define ucontext_t xucontext_t
57 #endif
58
59 typedef enum {
60 ASAN_RT_VERSION_UNDEFINED = 0,
61 ASAN_RT_VERSION_DYNAMIC,
62 ASAN_RT_VERSION_STATIC,
63 } asan_rt_version_t;
64
65 // FIXME: perhaps also store abi version here?
66 extern "C" {
67 SANITIZER_INTERFACE_ATTRIBUTE
68 asan_rt_version_t __asan_rt_version;
69 }
70
71 namespace __asan {
72
InitializePlatformInterceptors()73 void InitializePlatformInterceptors() {}
InitializePlatformExceptionHandlers()74 void InitializePlatformExceptionHandlers() {}
IsSystemHeapAddress(uptr addr)75 bool IsSystemHeapAddress (uptr addr) { return false; }
76
AsanDoesNotSupportStaticLinkage()77 void *AsanDoesNotSupportStaticLinkage() {
78 // This will fail to link with -static.
79 return &_DYNAMIC; // defined in link.h
80 }
81
FindDynamicShadowStart()82 uptr FindDynamicShadowStart() {
83 UNREACHABLE("FindDynamicShadowStart is not available");
84 return 0;
85 }
86
AsanApplyToGlobals(globals_op_fptr op,const void * needle)87 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
88 UNIMPLEMENTED();
89 }
90
91 #if SANITIZER_ANDROID
92 // FIXME: should we do anything for Android?
AsanCheckDynamicRTPrereqs()93 void AsanCheckDynamicRTPrereqs() {}
AsanCheckIncompatibleRT()94 void AsanCheckIncompatibleRT() {}
95 #else
FindFirstDSOCallback(struct dl_phdr_info * info,size_t size,void * data)96 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
97 void *data) {
98 // Continue until the first dynamic library is found
99 if (!info->dlpi_name || info->dlpi_name[0] == 0)
100 return 0;
101
102 // Ignore vDSO
103 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
104 return 0;
105
106 #if SANITIZER_NETBSD
107 // Ignore first entry (the main program)
108 char **p = (char **)data;
109 if (!(*p)) {
110 *p = (char *)-1;
111 return 0;
112 }
113 #endif
114
115 *(const char **)data = info->dlpi_name;
116 return 1;
117 }
118
IsDynamicRTName(const char * libname)119 static bool IsDynamicRTName(const char *libname) {
120 return internal_strstr(libname, "libclang_rt.asan") ||
121 internal_strstr(libname, "libasan.so");
122 }
123
ReportIncompatibleRT()124 static void ReportIncompatibleRT() {
125 Report("Your application is linked against incompatible ASan runtimes.\n");
126 Die();
127 }
128
AsanCheckDynamicRTPrereqs()129 void AsanCheckDynamicRTPrereqs() {
130 if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
131 return;
132
133 // Ensure that dynamic RT is the first DSO in the list
134 const char *first_dso_name = nullptr;
135 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
136 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
137 Report("ASan runtime does not come first in initial library list; "
138 "you should either link runtime to your application or "
139 "manually preload it with LD_PRELOAD.\n");
140 Die();
141 }
142 }
143
AsanCheckIncompatibleRT()144 void AsanCheckIncompatibleRT() {
145 if (ASAN_DYNAMIC) {
146 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
147 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
148 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
149 ReportIncompatibleRT();
150 }
151 } else {
152 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
153 // Ensure that dynamic runtime is not present. We should detect it
154 // as early as possible, otherwise ASan interceptors could bind to
155 // the functions in dynamic ASan runtime instead of the functions in
156 // system libraries, causing crashes later in ASan initialization.
157 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
158 char filename[128];
159 MemoryMappedSegment segment(filename, sizeof(filename));
160 while (proc_maps.Next(&segment)) {
161 if (IsDynamicRTName(segment.filename)) {
162 Report("Your application is linked against "
163 "incompatible ASan runtimes.\n");
164 Die();
165 }
166 }
167 __asan_rt_version = ASAN_RT_VERSION_STATIC;
168 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
169 ReportIncompatibleRT();
170 }
171 }
172 }
173 #endif // SANITIZER_ANDROID
174
175 #if !SANITIZER_ANDROID
ReadContextStack(void * context,uptr * stack,uptr * ssize)176 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
177 ucontext_t *ucp = (ucontext_t*)context;
178 *stack = (uptr)ucp->uc_stack.ss_sp;
179 *ssize = ucp->uc_stack.ss_size;
180 }
181 #else
ReadContextStack(void * context,uptr * stack,uptr * ssize)182 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
183 UNIMPLEMENTED();
184 }
185 #endif
186
AsanDlSymNext(const char * sym)187 void *AsanDlSymNext(const char *sym) {
188 return dlsym(RTLD_NEXT, sym);
189 }
190
191 } // namespace __asan
192
193 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
194