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 extern ElfW(Dyn) _DYNAMIC[];
59 #endif
60 
61 // x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
62 // 32-bit mode.
63 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
64   __FreeBSD_version <= 902001  // v9.2
65 #define ucontext_t xucontext_t
66 #endif
67 
68 typedef enum {
69   ASAN_RT_VERSION_UNDEFINED = 0,
70   ASAN_RT_VERSION_DYNAMIC,
71   ASAN_RT_VERSION_STATIC,
72 } asan_rt_version_t;
73 
74 // FIXME: perhaps also store abi version here?
75 extern "C" {
76 SANITIZER_INTERFACE_ATTRIBUTE
77 asan_rt_version_t  __asan_rt_version;
78 }
79 
80 namespace __asan {
81 
82 void InitializePlatformInterceptors() {}
83 void InitializePlatformExceptionHandlers() {}
84 bool IsSystemHeapAddress (uptr addr) { return false; }
85 
86 void *AsanDoesNotSupportStaticLinkage() {
87   // This will fail to link with -static.
88   return &_DYNAMIC;
89 }
90 
91 #if ASAN_PREMAP_SHADOW
92 uptr FindPremappedShadowStart(uptr shadow_size_bytes) {
93   uptr granularity = GetMmapGranularity();
94   uptr shadow_start = reinterpret_cast<uptr>(&__asan_shadow);
95   uptr premap_shadow_size = PremapShadowSize();
96   uptr shadow_size = RoundUpTo(shadow_size_bytes, granularity);
97   // We may have mapped too much. Release extra memory.
98   UnmapFromTo(shadow_start + shadow_size, shadow_start + premap_shadow_size);
99   return shadow_start;
100 }
101 #endif
102 
103 uptr FindDynamicShadowStart() {
104   uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
105 #if ASAN_PREMAP_SHADOW
106   if (!PremapShadowFailed())
107     return FindPremappedShadowStart(shadow_size_bytes);
108 #endif
109 
110   return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE,
111                           /*min_shadow_base_alignment*/ 0, kHighMemEnd);
112 }
113 
114 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
115   UNIMPLEMENTED();
116 }
117 
118 void FlushUnneededASanShadowMemory(uptr p, uptr size) {
119   // Since asan's mapping is compacting, the shadow chunk may be
120   // not page-aligned, so we only flush the page-aligned portion.
121   ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));
122 }
123 
124 #if SANITIZER_ANDROID
125 // FIXME: should we do anything for Android?
126 void AsanCheckDynamicRTPrereqs() {}
127 void AsanCheckIncompatibleRT() {}
128 #else
129 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
130                                 void *data) {
131   VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n",
132           info->dlpi_name, info->dlpi_addr);
133 
134   // Continue until the first dynamic library is found
135   if (!info->dlpi_name || info->dlpi_name[0] == 0)
136     return 0;
137 
138   // Ignore vDSO
139   if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
140     return 0;
141 
142 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
143   // Ignore first entry (the main program)
144   char **p = (char **)data;
145   if (!(*p)) {
146     *p = (char *)-1;
147     return 0;
148   }
149 #endif
150 
151 #if SANITIZER_SOLARIS
152   // Ignore executable on Solaris
153   if (info->dlpi_addr == 0)
154     return 0;
155 #endif
156 
157   *(const char **)data = info->dlpi_name;
158   return 1;
159 }
160 
161 static bool IsDynamicRTName(const char *libname) {
162   return internal_strstr(libname, "libclang_rt.asan") ||
163     internal_strstr(libname, "libasan.so");
164 }
165 
166 static void ReportIncompatibleRT() {
167   Report("Your application is linked against incompatible ASan runtimes.\n");
168   Die();
169 }
170 
171 void AsanCheckDynamicRTPrereqs() {
172   if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
173     return;
174 
175   // Ensure that dynamic RT is the first DSO in the list
176   const char *first_dso_name = nullptr;
177   dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
178   if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
179     Report("ASan runtime does not come first in initial library list; "
180            "you should either link runtime to your application or "
181            "manually preload it with LD_PRELOAD.\n");
182     Die();
183   }
184 }
185 
186 void AsanCheckIncompatibleRT() {
187   if (ASAN_DYNAMIC) {
188     if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
189       __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
190     } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
191       ReportIncompatibleRT();
192     }
193   } else {
194     if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
195       // Ensure that dynamic runtime is not present. We should detect it
196       // as early as possible, otherwise ASan interceptors could bind to
197       // the functions in dynamic ASan runtime instead of the functions in
198       // system libraries, causing crashes later in ASan initialization.
199       MemoryMappingLayout proc_maps(/*cache_enabled*/true);
200       char filename[PATH_MAX];
201       MemoryMappedSegment segment(filename, sizeof(filename));
202       while (proc_maps.Next(&segment)) {
203         if (IsDynamicRTName(segment.filename)) {
204           Report("Your application is linked against "
205                  "incompatible ASan runtimes.\n");
206           Die();
207         }
208       }
209       __asan_rt_version = ASAN_RT_VERSION_STATIC;
210     } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
211       ReportIncompatibleRT();
212     }
213   }
214 }
215 #endif // SANITIZER_ANDROID
216 
217 #if !SANITIZER_ANDROID
218 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
219   ucontext_t *ucp = (ucontext_t*)context;
220   *stack = (uptr)ucp->uc_stack.ss_sp;
221   *ssize = ucp->uc_stack.ss_size;
222 }
223 #else
224 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
225   UNIMPLEMENTED();
226 }
227 #endif
228 
229 void *AsanDlSymNext(const char *sym) {
230   return dlsym(RTLD_NEXT, sym);
231 }
232 
233 bool HandleDlopenInit() {
234   // Not supported on this platform.
235   static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
236                 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
237   return false;
238 }
239 
240 } // namespace __asan
241 
242 #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||
243         // SANITIZER_SOLARIS
244