xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp (revision e53a21abdf2953714e44e3c54b4bb78557cb096c)
1 //===-- sanitizer_posix_libcdep.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 shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements libc-dependent POSIX-specific functions
11 // from sanitizer_libc.h.
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_platform.h"
15 
16 #if SANITIZER_POSIX
17 
18 #include "sanitizer_common.h"
19 #include "sanitizer_flags.h"
20 #include "sanitizer_platform_limits_netbsd.h"
21 #include "sanitizer_platform_limits_posix.h"
22 #include "sanitizer_platform_limits_solaris.h"
23 #include "sanitizer_posix.h"
24 #include "sanitizer_procmaps.h"
25 
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <sys/mman.h>
32 #include <sys/resource.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <unistd.h>
38 
39 #if SANITIZER_FREEBSD
40 // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
41 // that, it was never implemented.  So just define it to zero.
42 #undef MAP_NORESERVE
43 #define MAP_NORESERVE 0
44 #endif
45 
46 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
47 
48 namespace __sanitizer {
49 
50 u32 GetUid() {
51   return getuid();
52 }
53 
54 uptr GetThreadSelf() {
55   return (uptr)pthread_self();
56 }
57 
58 void ReleaseMemoryPagesToOS(uptr beg, uptr end) {
59   uptr page_size = GetPageSizeCached();
60   uptr beg_aligned = RoundUpTo(beg, page_size);
61   uptr end_aligned = RoundDownTo(end, page_size);
62   if (beg_aligned < end_aligned)
63     internal_madvise(beg_aligned, end_aligned - beg_aligned,
64                      SANITIZER_MADVISE_DONTNEED);
65 }
66 
67 void SetShadowRegionHugePageMode(uptr addr, uptr size) {
68 #ifdef MADV_NOHUGEPAGE  // May not be defined on old systems.
69   if (common_flags()->no_huge_pages_for_shadow)
70     internal_madvise(addr, size, MADV_NOHUGEPAGE);
71   else
72     internal_madvise(addr, size, MADV_HUGEPAGE);
73 #endif  // MADV_NOHUGEPAGE
74 }
75 
76 bool DontDumpShadowMemory(uptr addr, uptr length) {
77 #if defined(MADV_DONTDUMP)
78   return internal_madvise(addr, length, MADV_DONTDUMP) == 0;
79 #elif defined(MADV_NOCORE)
80   return internal_madvise(addr, length, MADV_NOCORE) == 0;
81 #else
82   return true;
83 #endif  // MADV_DONTDUMP
84 }
85 
86 static rlim_t getlim(int res) {
87   rlimit rlim;
88   CHECK_EQ(0, getrlimit(res, &rlim));
89   return rlim.rlim_cur;
90 }
91 
92 static void setlim(int res, rlim_t lim) {
93   struct rlimit rlim;
94   if (getrlimit(res, &rlim)) {
95     Report("ERROR: %s getrlimit() failed %d\n", SanitizerToolName, errno);
96     Die();
97   }
98   rlim.rlim_cur = lim;
99   if (setrlimit(res, &rlim)) {
100     Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
101     Die();
102   }
103 }
104 
105 void DisableCoreDumperIfNecessary() {
106   if (common_flags()->disable_coredump) {
107     rlimit rlim;
108     CHECK_EQ(0, getrlimit(RLIMIT_CORE, &rlim));
109     // On Linux, if the kernel.core_pattern sysctl starts with a '|' (i.e. it
110     // is being piped to a coredump handler such as systemd-coredumpd), the
111     // kernel ignores RLIMIT_CORE (since we aren't creating a file in the file
112     // system) except for the magic value of 1, which disables coredumps when
113     // piping. 1 byte is too small for any kind of valid core dump, so it
114     // also disables coredumps if kernel.core_pattern creates files directly.
115     // While most piped coredump handlers do respect the crashing processes'
116     // RLIMIT_CORE, this is notable not the case for Debian's systemd-coredump
117     // due to a local patch that changes sysctl.d/50-coredump.conf to ignore
118     // the specified limit and instead use RLIM_INFINITY.
119     //
120     // The alternative to using RLIMIT_CORE=1 would be to use prctl() with the
121     // PR_SET_DUMPABLE flag, however that also prevents ptrace(), so makes it
122     // impossible to attach a debugger.
123     //
124     // Note: we use rlim_max in the Min() call here since that is the upper
125     // limit for what can be set without getting an EINVAL error.
126     rlim.rlim_cur = Min<rlim_t>(SANITIZER_LINUX ? 1 : 0, rlim.rlim_max);
127     CHECK_EQ(0, setrlimit(RLIMIT_CORE, &rlim));
128   }
129 }
130 
131 bool StackSizeIsUnlimited() {
132   rlim_t stack_size = getlim(RLIMIT_STACK);
133   return (stack_size == RLIM_INFINITY);
134 }
135 
136 void SetStackSizeLimitInBytes(uptr limit) {
137   setlim(RLIMIT_STACK, (rlim_t)limit);
138   CHECK(!StackSizeIsUnlimited());
139 }
140 
141 bool AddressSpaceIsUnlimited() {
142   rlim_t as_size = getlim(RLIMIT_AS);
143   return (as_size == RLIM_INFINITY);
144 }
145 
146 void SetAddressSpaceUnlimited() {
147   setlim(RLIMIT_AS, RLIM_INFINITY);
148   CHECK(AddressSpaceIsUnlimited());
149 }
150 
151 void Abort() {
152 #if !SANITIZER_GO
153   // If we are handling SIGABRT, unhandle it first.
154   // TODO(vitalybuka): Check if handler belongs to sanitizer.
155   if (GetHandleSignalMode(SIGABRT) != kHandleSignalNo) {
156     struct sigaction sigact;
157     internal_memset(&sigact, 0, sizeof(sigact));
158     sigact.sa_handler = SIG_DFL;
159     internal_sigaction(SIGABRT, &sigact, nullptr);
160   }
161 #endif
162 
163   abort();
164 }
165 
166 int Atexit(void (*function)(void)) {
167 #if !SANITIZER_GO
168   return atexit(function);
169 #else
170   return 0;
171 #endif
172 }
173 
174 bool CreateDir(const char *pathname) { return mkdir(pathname, 0755) == 0; }
175 
176 bool SupportsColoredOutput(fd_t fd) {
177   return isatty(fd) != 0;
178 }
179 
180 #if !SANITIZER_GO
181 // TODO(glider): different tools may require different altstack size.
182 static uptr GetAltStackSize() {
183   // Note: since GLIBC_2.31, SIGSTKSZ may be a function call, so this may be
184   // more costly that you think. However GetAltStackSize is only call 2-3 times
185   // per thread so don't cache the evaluation.
186   return SIGSTKSZ * 4;
187 }
188 
189 void SetAlternateSignalStack() {
190   stack_t altstack, oldstack;
191   CHECK_EQ(0, sigaltstack(nullptr, &oldstack));
192   // If the alternate stack is already in place, do nothing.
193   // Android always sets an alternate stack, but it's too small for us.
194   if (!SANITIZER_ANDROID && !(oldstack.ss_flags & SS_DISABLE)) return;
195   // TODO(glider): the mapped stack should have the MAP_STACK flag in the
196   // future. It is not required by man 2 sigaltstack now (they're using
197   // malloc()).
198   altstack.ss_size = GetAltStackSize();
199   altstack.ss_sp = (char *)MmapOrDie(altstack.ss_size, __func__);
200   altstack.ss_flags = 0;
201   CHECK_EQ(0, sigaltstack(&altstack, nullptr));
202 }
203 
204 void UnsetAlternateSignalStack() {
205   stack_t altstack, oldstack;
206   altstack.ss_sp = nullptr;
207   altstack.ss_flags = SS_DISABLE;
208   altstack.ss_size = GetAltStackSize();  // Some sane value required on Darwin.
209   CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
210   UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
211 }
212 
213 static void MaybeInstallSigaction(int signum,
214                                   SignalHandlerType handler) {
215   if (GetHandleSignalMode(signum) == kHandleSignalNo) return;
216 
217   struct sigaction sigact;
218   internal_memset(&sigact, 0, sizeof(sigact));
219   sigact.sa_sigaction = (sa_sigaction_t)handler;
220   // Do not block the signal from being received in that signal's handler.
221   // Clients are responsible for handling this correctly.
222   sigact.sa_flags = SA_SIGINFO | SA_NODEFER;
223   if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
224   CHECK_EQ(0, internal_sigaction(signum, &sigact, nullptr));
225   VReport(1, "Installed the sigaction for signal %d\n", signum);
226 }
227 
228 void InstallDeadlySignalHandlers(SignalHandlerType handler) {
229   // Set the alternate signal stack for the main thread.
230   // This will cause SetAlternateSignalStack to be called twice, but the stack
231   // will be actually set only once.
232   if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
233   MaybeInstallSigaction(SIGSEGV, handler);
234   MaybeInstallSigaction(SIGBUS, handler);
235   MaybeInstallSigaction(SIGABRT, handler);
236   MaybeInstallSigaction(SIGFPE, handler);
237   MaybeInstallSigaction(SIGILL, handler);
238   MaybeInstallSigaction(SIGTRAP, handler);
239 }
240 
241 bool SignalContext::IsStackOverflow() const {
242   // Access at a reasonable offset above SP, or slightly below it (to account
243   // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
244   // probably a stack overflow.
245 #ifdef __s390__
246   // On s390, the fault address in siginfo points to start of the page, not
247   // to the precise word that was accessed.  Mask off the low bits of sp to
248   // take it into account.
249   bool IsStackAccess = addr >= (sp & ~0xFFF) && addr < sp + 0xFFFF;
250 #else
251   // Let's accept up to a page size away from top of stack. Things like stack
252   // probing can trigger accesses with such large offsets.
253   bool IsStackAccess = addr + GetPageSizeCached() > sp && addr < sp + 0xFFFF;
254 #endif
255 
256 #if __powerpc__
257   // Large stack frames can be allocated with e.g.
258   //   lis r0,-10000
259   //   stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
260   // If the store faults then sp will not have been updated, so test above
261   // will not work, because the fault address will be more than just "slightly"
262   // below sp.
263   if (!IsStackAccess && IsAccessibleMemoryRange(pc, 4)) {
264     u32 inst = *(unsigned *)pc;
265     u32 ra = (inst >> 16) & 0x1F;
266     u32 opcd = inst >> 26;
267     u32 xo = (inst >> 1) & 0x3FF;
268     // Check for store-with-update to sp. The instructions we accept are:
269     //   stbu rs,d(ra)          stbux rs,ra,rb
270     //   sthu rs,d(ra)          sthux rs,ra,rb
271     //   stwu rs,d(ra)          stwux rs,ra,rb
272     //   stdu rs,ds(ra)         stdux rs,ra,rb
273     // where ra is r1 (the stack pointer).
274     if (ra == 1 &&
275         (opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 ||
276          (opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181))))
277       IsStackAccess = true;
278   }
279 #endif  // __powerpc__
280 
281   // We also check si_code to filter out SEGV caused by something else other
282   // then hitting the guard page or unmapped memory, like, for example,
283   // unaligned memory access.
284   auto si = static_cast<const siginfo_t *>(siginfo);
285   return IsStackAccess &&
286          (si->si_code == si_SEGV_MAPERR || si->si_code == si_SEGV_ACCERR);
287 }
288 
289 #endif  // SANITIZER_GO
290 
291 bool IsAccessibleMemoryRange(uptr beg, uptr size) {
292   uptr page_size = GetPageSizeCached();
293   // Checking too large memory ranges is slow.
294   CHECK_LT(size, page_size * 10);
295   int sock_pair[2];
296   if (pipe(sock_pair))
297     return false;
298   uptr bytes_written =
299       internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size);
300   int write_errno;
301   bool result;
302   if (internal_iserror(bytes_written, &write_errno)) {
303     CHECK_EQ(EFAULT, write_errno);
304     result = false;
305   } else {
306     result = (bytes_written == size);
307   }
308   internal_close(sock_pair[0]);
309   internal_close(sock_pair[1]);
310   return result;
311 }
312 
313 void PlatformPrepareForSandboxing(void *args) {
314   // Some kinds of sandboxes may forbid filesystem access, so we won't be able
315   // to read the file mappings from /proc/self/maps. Luckily, neither the
316   // process will be able to load additional libraries, so it's fine to use the
317   // cached mappings.
318   MemoryMappingLayout::CacheMemoryMappings();
319 }
320 
321 static bool MmapFixed(uptr fixed_addr, uptr size, int additional_flags,
322                       const char *name) {
323   size = RoundUpTo(size, GetPageSizeCached());
324   fixed_addr = RoundDownTo(fixed_addr, GetPageSizeCached());
325   uptr p =
326       MmapNamed((void *)fixed_addr, size, PROT_READ | PROT_WRITE,
327                 MAP_PRIVATE | MAP_FIXED | additional_flags | MAP_ANON, name);
328   int reserrno;
329   if (internal_iserror(p, &reserrno)) {
330     Report(
331         "ERROR: %s failed to "
332         "allocate 0x%zx (%zd) bytes at address %p (errno: %d)\n",
333         SanitizerToolName, size, size, (void *)fixed_addr, reserrno);
334     return false;
335   }
336   IncreaseTotalMmap(size);
337   return true;
338 }
339 
340 bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) {
341   return MmapFixed(fixed_addr, size, MAP_NORESERVE, name);
342 }
343 
344 bool MmapFixedSuperNoReserve(uptr fixed_addr, uptr size, const char *name) {
345 #if SANITIZER_FREEBSD
346   if (common_flags()->no_huge_pages_for_shadow)
347     return MmapFixedNoReserve(fixed_addr, size, name);
348   // MAP_NORESERVE is implicit with FreeBSD
349   return MmapFixed(fixed_addr, size, MAP_ALIGNED_SUPER, name);
350 #else
351   bool r = MmapFixedNoReserve(fixed_addr, size, name);
352   if (r)
353     SetShadowRegionHugePageMode(fixed_addr, size);
354   return r;
355 #endif
356 }
357 
358 uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) {
359   base_ = fixed_addr ? MmapFixedNoAccess(fixed_addr, size, name)
360                      : MmapNoAccess(size);
361   size_ = size;
362   name_ = name;
363   (void)os_handle_;  // unsupported
364   return reinterpret_cast<uptr>(base_);
365 }
366 
367 // Uses fixed_addr for now.
368 // Will use offset instead once we've implemented this function for real.
369 uptr ReservedAddressRange::Map(uptr fixed_addr, uptr size, const char *name) {
370   return reinterpret_cast<uptr>(
371       MmapFixedOrDieOnFatalError(fixed_addr, size, name));
372 }
373 
374 uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr size,
375                                     const char *name) {
376   return reinterpret_cast<uptr>(MmapFixedOrDie(fixed_addr, size, name));
377 }
378 
379 void ReservedAddressRange::Unmap(uptr addr, uptr size) {
380   CHECK_LE(size, size_);
381   if (addr == reinterpret_cast<uptr>(base_))
382     // If we unmap the whole range, just null out the base.
383     base_ = (size == size_) ? nullptr : reinterpret_cast<void*>(addr + size);
384   else
385     CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_);
386   size_ -= size;
387   UnmapOrDie(reinterpret_cast<void*>(addr), size);
388 }
389 
390 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
391   return (void *)MmapNamed((void *)fixed_addr, size, PROT_NONE,
392                            MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE | MAP_ANON,
393                            name);
394 }
395 
396 void *MmapNoAccess(uptr size) {
397   unsigned flags = MAP_PRIVATE | MAP_ANON | MAP_NORESERVE;
398   return (void *)internal_mmap(nullptr, size, PROT_NONE, flags, -1, 0);
399 }
400 
401 // This function is defined elsewhere if we intercepted pthread_attr_getstack.
402 extern "C" {
403 SANITIZER_WEAK_ATTRIBUTE int
404 real_pthread_attr_getstack(void *attr, void **addr, size_t *size);
405 } // extern "C"
406 
407 int internal_pthread_attr_getstack(void *attr, void **addr, uptr *size) {
408 #if !SANITIZER_GO && !SANITIZER_APPLE
409   if (&real_pthread_attr_getstack)
410     return real_pthread_attr_getstack((pthread_attr_t *)attr, addr,
411                                       (size_t *)size);
412 #endif
413   return pthread_attr_getstack((pthread_attr_t *)attr, addr, (size_t *)size);
414 }
415 
416 #if !SANITIZER_GO
417 void AdjustStackSize(void *attr_) {
418   pthread_attr_t *attr = (pthread_attr_t *)attr_;
419   uptr stackaddr = 0;
420   uptr stacksize = 0;
421   internal_pthread_attr_getstack(attr, (void **)&stackaddr, &stacksize);
422   // GLibC will return (0 - stacksize) as the stack address in the case when
423   // stacksize is set, but stackaddr is not.
424   bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0);
425   // We place a lot of tool data into TLS, account for that.
426   const uptr minstacksize = GetTlsSize() + 128*1024;
427   if (stacksize < minstacksize) {
428     if (!stack_set) {
429       if (stacksize != 0) {
430         VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize,
431                 minstacksize);
432         pthread_attr_setstacksize(attr, minstacksize);
433       }
434     } else {
435       Printf("Sanitizer: pre-allocated stack size is insufficient: "
436              "%zu < %zu\n", stacksize, minstacksize);
437       Printf("Sanitizer: pthread_create is likely to fail.\n");
438     }
439   }
440 }
441 #endif // !SANITIZER_GO
442 
443 pid_t StartSubprocess(const char *program, const char *const argv[],
444                       const char *const envp[], fd_t stdin_fd, fd_t stdout_fd,
445                       fd_t stderr_fd) {
446   auto file_closer = at_scope_exit([&] {
447     if (stdin_fd != kInvalidFd) {
448       internal_close(stdin_fd);
449     }
450     if (stdout_fd != kInvalidFd) {
451       internal_close(stdout_fd);
452     }
453     if (stderr_fd != kInvalidFd) {
454       internal_close(stderr_fd);
455     }
456   });
457 
458   int pid = internal_fork();
459 
460   if (pid < 0) {
461     int rverrno;
462     if (internal_iserror(pid, &rverrno)) {
463       Report("WARNING: failed to fork (errno %d)\n", rverrno);
464     }
465     return pid;
466   }
467 
468   if (pid == 0) {
469     // Child subprocess
470     if (stdin_fd != kInvalidFd) {
471       internal_close(STDIN_FILENO);
472       internal_dup2(stdin_fd, STDIN_FILENO);
473       internal_close(stdin_fd);
474     }
475     if (stdout_fd != kInvalidFd) {
476       internal_close(STDOUT_FILENO);
477       internal_dup2(stdout_fd, STDOUT_FILENO);
478       internal_close(stdout_fd);
479     }
480     if (stderr_fd != kInvalidFd) {
481       internal_close(STDERR_FILENO);
482       internal_dup2(stderr_fd, STDERR_FILENO);
483       internal_close(stderr_fd);
484     }
485 
486     for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd);
487 
488     internal_execve(program, const_cast<char **>(&argv[0]),
489                     const_cast<char *const *>(envp));
490     internal__exit(1);
491   }
492 
493   return pid;
494 }
495 
496 bool IsProcessRunning(pid_t pid) {
497   int process_status;
498   uptr waitpid_status = internal_waitpid(pid, &process_status, WNOHANG);
499   int local_errno;
500   if (internal_iserror(waitpid_status, &local_errno)) {
501     VReport(1, "Waiting on the process failed (errno %d).\n", local_errno);
502     return false;
503   }
504   return waitpid_status == 0;
505 }
506 
507 int WaitForProcess(pid_t pid) {
508   int process_status;
509   uptr waitpid_status = internal_waitpid(pid, &process_status, 0);
510   int local_errno;
511   if (internal_iserror(waitpid_status, &local_errno)) {
512     VReport(1, "Waiting on the process failed (errno %d).\n", local_errno);
513     return -1;
514   }
515   return process_status;
516 }
517 
518 bool IsStateDetached(int state) {
519   return state == PTHREAD_CREATE_DETACHED;
520 }
521 
522 } // namespace __sanitizer
523 
524 #endif // SANITIZER_POSIX
525