asan_linux.cpp 7.45 KB
Newer Older
1
//===-- asan_linux.cpp ----------------------------------------------------===//
2
//
3 4 5
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 7 8 9 10 11 12
//
//===----------------------------------------------------------------------===//
//
// This file is a part of AddressSanitizer, an address sanity checker.
//
// Linux-specific details.
//===----------------------------------------------------------------------===//
13 14

#include "sanitizer_common/sanitizer_platform.h"
15 16
#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
    SANITIZER_SOLARIS
17 18 19

#include "asan_interceptors.h"
#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 25 26 27 28 29 30 31
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_procmaps.h"

#include <sys/time.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/types.h>
32
#include <dlfcn.h>
33
#include <fcntl.h>
34
#include <limits.h>
35 36 37 38 39
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <unwind.h>

40 41
#if SANITIZER_FREEBSD
#include <sys/link_elf.h>
42 43
#endif

44 45 46 47 48
#if SANITIZER_SOLARIS
#include <link.h>
#endif

#if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS
49
#include <ucontext.h>
50
extern "C" void* _DYNAMIC;
51 52 53 54
#elif SANITIZER_NETBSD
#include <link_elf.h>
#include <ucontext.h>
extern Elf_Dyn _DYNAMIC;
55 56 57 58 59
#else
#include <sys/ucontext.h>
#include <link.h>
#endif

60 61 62 63 64
// x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
// 32-bit mode.
#if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
  __FreeBSD_version <= 902001  // v9.2
#define ucontext_t xucontext_t
65 66 67 68 69 70 71 72 73 74 75 76 77
#endif

typedef enum {
  ASAN_RT_VERSION_UNDEFINED = 0,
  ASAN_RT_VERSION_DYNAMIC,
  ASAN_RT_VERSION_STATIC,
} asan_rt_version_t;

// FIXME: perhaps also store abi version here?
extern "C" {
SANITIZER_INTERFACE_ATTRIBUTE
asan_rt_version_t  __asan_rt_version;
}
78 79 80

namespace __asan {

81
void InitializePlatformInterceptors() {}
82
void InitializePlatformExceptionHandlers() {}
83
bool IsSystemHeapAddress (uptr addr) { return false; }
84 85 86 87 88 89

void *AsanDoesNotSupportStaticLinkage() {
  // This will fail to link with -static.
  return &_DYNAMIC;  // defined in link.h
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static void UnmapFromTo(uptr from, uptr to) {
  CHECK(to >= from);
  if (to == from) return;
  uptr res = internal_munmap(reinterpret_cast<void *>(from), to - from);
  if (UNLIKELY(internal_iserror(res))) {
    Report(
        "ERROR: AddresSanitizer failed to unmap 0x%zx (%zd) bytes at address "
        "%p\n",
        to - from, to - from, from);
    CHECK("unable to unmap" && 0);
  }
}

#if ASAN_PREMAP_SHADOW
uptr FindPremappedShadowStart() {
  uptr granularity = GetMmapGranularity();
  uptr shadow_start = reinterpret_cast<uptr>(&__asan_shadow);
  uptr premap_shadow_size = PremapShadowSize();
  uptr shadow_size = RoundUpTo(kHighShadowEnd, granularity);
  // We may have mapped too much. Release extra memory.
  UnmapFromTo(shadow_start + shadow_size, shadow_start + premap_shadow_size);
  return shadow_start;
}
#endif

115
uptr FindDynamicShadowStart() {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#if ASAN_PREMAP_SHADOW
  if (!PremapShadowFailed())
    return FindPremappedShadowStart();
#endif

  uptr granularity = GetMmapGranularity();
  uptr alignment = granularity * 8;
  uptr left_padding = granularity;
  uptr shadow_size = RoundUpTo(kHighShadowEnd, granularity);
  uptr map_size = shadow_size + left_padding + alignment;

  uptr map_start = (uptr)MmapNoAccess(map_size);
  CHECK_NE(map_start, ~(uptr)0);

  uptr shadow_start = RoundUpTo(map_start + left_padding, alignment);
  UnmapFromTo(map_start, shadow_start - left_padding);
  UnmapFromTo(shadow_start + shadow_size, map_start + map_size);

  return shadow_start;
135 136
}

137 138 139 140
void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
  UNIMPLEMENTED();
}

141
#if SANITIZER_ANDROID
142 143 144 145 146 147
// FIXME: should we do anything for Android?
void AsanCheckDynamicRTPrereqs() {}
void AsanCheckIncompatibleRT() {}
#else
static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
                                void *data) {
148 149 150
  VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n",
          info->dlpi_name, info->dlpi_addr);

151 152 153 154
  // Continue until the first dynamic library is found
  if (!info->dlpi_name || info->dlpi_name[0] == 0)
    return 0;

155 156 157 158
  // Ignore vDSO
  if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
    return 0;

159
#if SANITIZER_FREEBSD || SANITIZER_NETBSD
160 161 162 163 164 165 166 167
  // Ignore first entry (the main program)
  char **p = (char **)data;
  if (!(*p)) {
    *p = (char *)-1;
    return 0;
  }
#endif

168 169 170 171 172 173
#if SANITIZER_SOLARIS
  // Ignore executable on Solaris
  if (info->dlpi_addr == 0)
    return 0;
#endif

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  *(const char **)data = info->dlpi_name;
  return 1;
}

static bool IsDynamicRTName(const char *libname) {
  return internal_strstr(libname, "libclang_rt.asan") ||
    internal_strstr(libname, "libasan.so");
}

static void ReportIncompatibleRT() {
  Report("Your application is linked against incompatible ASan runtimes.\n");
  Die();
}

void AsanCheckDynamicRTPrereqs() {
189
  if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
190 191
    return;

192
  // Ensure that dynamic RT is the first DSO in the list
193
  const char *first_dso_name = nullptr;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
  if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
    Report("ASan runtime does not come first in initial library list; "
           "you should either link runtime to your application or "
           "manually preload it with LD_PRELOAD.\n");
    Die();
  }
}

void AsanCheckIncompatibleRT() {
  if (ASAN_DYNAMIC) {
    if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
      __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
    } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
      ReportIncompatibleRT();
    }
  } else {
    if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
      // Ensure that dynamic runtime is not present. We should detect it
      // as early as possible, otherwise ASan interceptors could bind to
      // the functions in dynamic ASan runtime instead of the functions in
      // system libraries, causing crashes later in ASan initialization.
      MemoryMappingLayout proc_maps(/*cache_enabled*/true);
217
      char filename[PATH_MAX];
218 219 220
      MemoryMappedSegment segment(filename, sizeof(filename));
      while (proc_maps.Next(&segment)) {
        if (IsDynamicRTName(segment.filename)) {
221 222 223 224 225 226 227 228 229 230 231
          Report("Your application is linked against "
                 "incompatible ASan runtimes.\n");
          Die();
        }
      }
      __asan_rt_version = ASAN_RT_VERSION_STATIC;
    } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
      ReportIncompatibleRT();
    }
  }
}
232
#endif // SANITIZER_ANDROID
233

234
#if !SANITIZER_ANDROID
235
void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
236
  ucontext_t *ucp = (ucontext_t*)context;
237 238
  *stack = (uptr)ucp->uc_stack.ss_sp;
  *ssize = ucp->uc_stack.ss_size;
239 240
}
#else
241
void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
242 243 244 245
  UNIMPLEMENTED();
}
#endif

246 247 248 249
void *AsanDlSymNext(const char *sym) {
  return dlsym(RTLD_NEXT, sym);
}

250 251 252 253 254 255 256
bool HandleDlopenInit() {
  // Not supported on this platform.
  static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
                "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
  return false;
}

257
} // namespace __asan
258

259 260
#endif  // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||
        // SANITIZER_SOLARIS