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