llvm-project
243 строки · 7.4 Кб
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 || \
16SANITIZER_SOLARIS
17
18# include <dlfcn.h>
19# include <fcntl.h>
20# include <limits.h>
21# include <pthread.h>
22# include <stdio.h>
23# include <sys/mman.h>
24# include <sys/resource.h>
25# include <sys/syscall.h>
26# include <sys/time.h>
27# include <sys/types.h>
28# include <unistd.h>
29# include <unwind.h>
30
31# include "asan_interceptors.h"
32# include "asan_internal.h"
33# include "asan_premap_shadow.h"
34# include "asan_thread.h"
35# include "sanitizer_common/sanitizer_flags.h"
36# include "sanitizer_common/sanitizer_hash.h"
37# include "sanitizer_common/sanitizer_libc.h"
38# include "sanitizer_common/sanitizer_procmaps.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# elif SANITIZER_NETBSD
51# include <link_elf.h>
52# include <ucontext.h>
53# else
54# include <link.h>
55# include <sys/ucontext.h>
56# endif
57
58typedef enum {
59ASAN_RT_VERSION_UNDEFINED = 0,
60ASAN_RT_VERSION_DYNAMIC,
61ASAN_RT_VERSION_STATIC,
62} asan_rt_version_t;
63
64// FIXME: perhaps also store abi version here?
65extern "C" {
66SANITIZER_INTERFACE_ATTRIBUTE
67asan_rt_version_t __asan_rt_version;
68}
69
70namespace __asan {
71
72void InitializePlatformInterceptors() {}
73void InitializePlatformExceptionHandlers() {}
74bool IsSystemHeapAddress(uptr addr) { return false; }
75
76# if ASAN_PREMAP_SHADOW
77uptr FindPremappedShadowStart(uptr shadow_size_bytes) {
78uptr granularity = GetMmapGranularity();
79uptr shadow_start = reinterpret_cast<uptr>(&__asan_shadow);
80uptr premap_shadow_size = PremapShadowSize();
81uptr shadow_size = RoundUpTo(shadow_size_bytes, granularity);
82// We may have mapped too much. Release extra memory.
83UnmapFromTo(shadow_start + shadow_size, shadow_start + premap_shadow_size);
84return shadow_start;
85}
86# endif
87
88uptr FindDynamicShadowStart() {
89uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
90# if ASAN_PREMAP_SHADOW
91if (!PremapShadowFailed())
92return FindPremappedShadowStart(shadow_size_bytes);
93# endif
94
95return MapDynamicShadow(shadow_size_bytes, ASAN_SHADOW_SCALE,
96/*min_shadow_base_alignment*/ 0, kHighMemEnd,
97GetMmapGranularity());
98}
99
100void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
101UNIMPLEMENTED();
102}
103
104void FlushUnneededASanShadowMemory(uptr p, uptr size) {
105// Since asan's mapping is compacting, the shadow chunk may be
106// not page-aligned, so we only flush the page-aligned portion.
107ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));
108}
109
110# if SANITIZER_ANDROID
111// FIXME: should we do anything for Android?
112void AsanCheckDynamicRTPrereqs() {}
113void AsanCheckIncompatibleRT() {}
114# else
115static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
116void *data) {
117VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n", info->dlpi_name,
118(void *)info->dlpi_addr);
119
120const char **name = (const char **)data;
121
122// Ignore first entry (the main program)
123if (!*name) {
124*name = "";
125return 0;
126}
127
128# if SANITIZER_LINUX
129// Ignore vDSO. glibc versions earlier than 2.15 (and some patched
130// by distributors) return an empty name for the vDSO entry, so
131// detect this as well.
132if (!info->dlpi_name[0] ||
133internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
134return 0;
135# endif
136# if SANITIZER_FREEBSD
137// Ignore vDSO.
138if (internal_strcmp(info->dlpi_name, "[vdso]") == 0)
139return 0;
140# endif
141
142*name = info->dlpi_name;
143return 1;
144}
145
146static bool IsDynamicRTName(const char *libname) {
147return internal_strstr(libname, "libclang_rt.asan") ||
148internal_strstr(libname, "libasan.so");
149}
150
151static void ReportIncompatibleRT() {
152Report("Your application is linked against incompatible ASan runtimes.\n");
153Die();
154}
155
156void AsanCheckDynamicRTPrereqs() {
157if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
158return;
159
160// Ensure that dynamic RT is the first DSO in the list
161const char *first_dso_name = nullptr;
162dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
163if (first_dso_name && first_dso_name[0] && !IsDynamicRTName(first_dso_name)) {
164Report(
165"ASan runtime does not come first in initial library list; "
166"you should either link runtime to your application or "
167"manually preload it with LD_PRELOAD.\n");
168Die();
169}
170}
171
172void AsanCheckIncompatibleRT() {
173if (ASAN_DYNAMIC) {
174if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
175__asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
176} else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
177ReportIncompatibleRT();
178}
179} else {
180if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
181// Ensure that dynamic runtime is not present. We should detect it
182// as early as possible, otherwise ASan interceptors could bind to
183// the functions in dynamic ASan runtime instead of the functions in
184// system libraries, causing crashes later in ASan initialization.
185MemoryMappingLayout proc_maps(/*cache_enabled*/ true);
186char filename[PATH_MAX];
187MemoryMappedSegment segment(filename, sizeof(filename));
188while (proc_maps.Next(&segment)) {
189if (IsDynamicRTName(segment.filename)) {
190Report(
191"Your application is linked against "
192"incompatible ASan runtimes.\n");
193Die();
194}
195}
196__asan_rt_version = ASAN_RT_VERSION_STATIC;
197} else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
198ReportIncompatibleRT();
199}
200}
201}
202# endif // SANITIZER_ANDROID
203
204# if ASAN_INTERCEPT_SWAPCONTEXT
205constexpr u32 kAsanContextStackFlagsMagic = 0x51260eea;
206
207static int HashContextStack(const ucontext_t &ucp) {
208MurMur2Hash64Builder hash(kAsanContextStackFlagsMagic);
209hash.add(reinterpret_cast<uptr>(ucp.uc_stack.ss_sp));
210hash.add(ucp.uc_stack.ss_size);
211return static_cast<int>(hash.get());
212}
213
214void SignContextStack(void *context) {
215ucontext_t *ucp = reinterpret_cast<ucontext_t *>(context);
216ucp->uc_stack.ss_flags = HashContextStack(*ucp);
217}
218
219void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
220const ucontext_t *ucp = reinterpret_cast<const ucontext_t *>(context);
221if (HashContextStack(*ucp) == ucp->uc_stack.ss_flags) {
222*stack = reinterpret_cast<uptr>(ucp->uc_stack.ss_sp);
223*ssize = ucp->uc_stack.ss_size;
224return;
225}
226*stack = 0;
227*ssize = 0;
228}
229# endif // ASAN_INTERCEPT_SWAPCONTEXT
230
231void *AsanDlSymNext(const char *sym) { return dlsym(RTLD_NEXT, sym); }
232
233bool HandleDlopenInit() {
234// Not supported on this platform.
235static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
236"Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
237return false;
238}
239
240} // namespace __asan
241
242#endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||
243// SANITIZER_SOLARIS
244