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, ASAN_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", info->dlpi_name,
132           (void *)info->dlpi_addr);
133 
134   const char **name = (const char **)data;
135 
136   // Ignore first entry (the main program)
137   if (!*name) {
138     *name = "";
139     return 0;
140   }
141 
142 #    if SANITIZER_LINUX
143   // Ignore vDSO. glibc versions earlier than 2.15 (and some patched
144   // by distributors) return an empty name for the vDSO entry, so
145   // detect this as well.
146   if (!info->dlpi_name[0] ||
147       internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
148     return 0;
149 #    endif
150 
151   *name = info->dlpi_name;
152   return 1;
153 }
154 
155 static bool IsDynamicRTName(const char *libname) {
156   return internal_strstr(libname, "libclang_rt.asan") ||
157     internal_strstr(libname, "libasan.so");
158 }
159 
160 static void ReportIncompatibleRT() {
161   Report("Your application is linked against incompatible ASan runtimes.\n");
162   Die();
163 }
164 
165 void AsanCheckDynamicRTPrereqs() {
166   if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
167     return;
168 
169   // Ensure that dynamic RT is the first DSO in the list
170   const char *first_dso_name = nullptr;
171   dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
172   if (first_dso_name && first_dso_name[0] && !IsDynamicRTName(first_dso_name)) {
173     Report("ASan runtime does not come first in initial library list; "
174            "you should either link runtime to your application or "
175            "manually preload it with LD_PRELOAD.\n");
176     Die();
177   }
178 }
179 
180 void AsanCheckIncompatibleRT() {
181   if (ASAN_DYNAMIC) {
182     if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
183       __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
184     } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
185       ReportIncompatibleRT();
186     }
187   } else {
188     if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
189       // Ensure that dynamic runtime is not present. We should detect it
190       // as early as possible, otherwise ASan interceptors could bind to
191       // the functions in dynamic ASan runtime instead of the functions in
192       // system libraries, causing crashes later in ASan initialization.
193       MemoryMappingLayout proc_maps(/*cache_enabled*/true);
194       char filename[PATH_MAX];
195       MemoryMappedSegment segment(filename, sizeof(filename));
196       while (proc_maps.Next(&segment)) {
197         if (IsDynamicRTName(segment.filename)) {
198           Report("Your application is linked against "
199                  "incompatible ASan runtimes.\n");
200           Die();
201         }
202       }
203       __asan_rt_version = ASAN_RT_VERSION_STATIC;
204     } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
205       ReportIncompatibleRT();
206     }
207   }
208 }
209 #endif // SANITIZER_ANDROID
210 
211 #if !SANITIZER_ANDROID
212 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
213   ucontext_t *ucp = (ucontext_t*)context;
214   *stack = (uptr)ucp->uc_stack.ss_sp;
215   *ssize = ucp->uc_stack.ss_size;
216 }
217 
218 void ResetContextStack(void *context) {
219   ucontext_t *ucp = (ucontext_t *)context;
220   ucp->uc_stack.ss_sp = nullptr;
221   ucp->uc_stack.ss_size = 0;
222 }
223 #  else
224 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
225   UNIMPLEMENTED();
226 }
227 
228 void ResetContextStack(void *context) { UNIMPLEMENTED(); }
229 #  endif
230 
231 void *AsanDlSymNext(const char *sym) {
232   return dlsym(RTLD_NEXT, sym);
233 }
234 
235 bool HandleDlopenInit() {
236   // Not supported on this platform.
237   static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
238                 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
239   return false;
240 }
241 
242 } // namespace __asan
243 
244 #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||
245         // SANITIZER_SOLARIS
246