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