sanitizer_linux_s390.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //===-- sanitizer_linux_s390.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 s390-linux-specific functions from
  11. // sanitizer_libc.h.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_platform.h"
  14. #if SANITIZER_LINUX && SANITIZER_S390
  15. # include <dlfcn.h>
  16. # include <errno.h>
  17. # include <sys/syscall.h>
  18. # include <sys/utsname.h>
  19. # include <unistd.h>
  20. # include "sanitizer_libc.h"
  21. # include "sanitizer_linux.h"
  22. namespace __sanitizer {
  23. // --------------- sanitizer_libc.h
  24. uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
  25. u64 offset) {
  26. struct s390_mmap_params {
  27. unsigned long addr;
  28. unsigned long length;
  29. unsigned long prot;
  30. unsigned long flags;
  31. unsigned long fd;
  32. unsigned long offset;
  33. } params = {
  34. (unsigned long)addr, (unsigned long)length, (unsigned long)prot,
  35. (unsigned long)flags, (unsigned long)fd,
  36. # ifdef __s390x__
  37. (unsigned long)offset,
  38. # else
  39. (unsigned long)(offset / 4096),
  40. # endif
  41. };
  42. # ifdef __s390x__
  43. return syscall(__NR_mmap, &params);
  44. # else
  45. return syscall(__NR_mmap2, &params);
  46. # endif
  47. }
  48. uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
  49. int *parent_tidptr, void *newtls, int *child_tidptr) {
  50. if (!fn || !child_stack) {
  51. errno = EINVAL;
  52. return -1;
  53. }
  54. CHECK_EQ(0, (uptr)child_stack % 16);
  55. // Minimum frame size.
  56. # ifdef __s390x__
  57. child_stack = (char *)child_stack - 160;
  58. # else
  59. child_stack = (char *)child_stack - 96;
  60. # endif
  61. // Terminate unwind chain.
  62. ((unsigned long *)child_stack)[0] = 0;
  63. // And pass parameters.
  64. ((unsigned long *)child_stack)[1] = (uptr)fn;
  65. ((unsigned long *)child_stack)[2] = (uptr)arg;
  66. register uptr res __asm__("r2");
  67. register void *__cstack __asm__("r2") = child_stack;
  68. register long __flags __asm__("r3") = flags;
  69. register int *__ptidptr __asm__("r4") = parent_tidptr;
  70. register int *__ctidptr __asm__("r5") = child_tidptr;
  71. register void *__newtls __asm__("r6") = newtls;
  72. __asm__ __volatile__(
  73. /* Clone. */
  74. "svc %1\n"
  75. /* if (%r2 != 0)
  76. * return;
  77. */
  78. # ifdef __s390x__
  79. "cghi %%r2, 0\n"
  80. # else
  81. "chi %%r2, 0\n"
  82. # endif
  83. "jne 1f\n"
  84. /* Call "fn(arg)". */
  85. # ifdef __s390x__
  86. "lmg %%r1, %%r2, 8(%%r15)\n"
  87. # else
  88. "lm %%r1, %%r2, 4(%%r15)\n"
  89. # endif
  90. "basr %%r14, %%r1\n"
  91. /* Call _exit(%r2). */
  92. "svc %2\n"
  93. /* Return to parent. */
  94. "1:\n"
  95. : "=r"(res)
  96. : "i"(__NR_clone), "i"(__NR_exit), "r"(__cstack), "r"(__flags),
  97. "r"(__ptidptr), "r"(__ctidptr), "r"(__newtls)
  98. : "memory", "cc");
  99. if (res >= (uptr)-4095) {
  100. errno = -res;
  101. return -1;
  102. }
  103. return res;
  104. }
  105. # if SANITIZER_S390_64
  106. static bool FixedCVE_2016_2143() {
  107. // Try to determine if the running kernel has a fix for CVE-2016-2143,
  108. // return false if in doubt (better safe than sorry). Distros may want to
  109. // adjust this for their own kernels.
  110. struct utsname buf;
  111. unsigned int major, minor, patch = 0;
  112. // This should never fail, but just in case...
  113. if (internal_uname(&buf))
  114. return false;
  115. const char *ptr = buf.release;
  116. major = internal_simple_strtoll(ptr, &ptr, 10);
  117. // At least first 2 should be matched.
  118. if (ptr[0] != '.')
  119. return false;
  120. minor = internal_simple_strtoll(ptr + 1, &ptr, 10);
  121. // Third is optional.
  122. if (ptr[0] == '.')
  123. patch = internal_simple_strtoll(ptr + 1, &ptr, 10);
  124. if (major < 3) {
  125. if (major == 2 && minor == 6 && patch == 32 && ptr[0] == '-' &&
  126. internal_strstr(ptr, ".el6")) {
  127. // Check RHEL6
  128. int r1 = internal_simple_strtoll(ptr + 1, &ptr, 10);
  129. if (r1 >= 657) // 2.6.32-657.el6 or later
  130. return true;
  131. if (r1 == 642 && ptr[0] == '.') {
  132. int r2 = internal_simple_strtoll(ptr + 1, &ptr, 10);
  133. if (r2 >= 9) // 2.6.32-642.9.1.el6 or later
  134. return true;
  135. }
  136. }
  137. // <3.0 is bad.
  138. return false;
  139. } else if (major == 3) {
  140. // 3.2.79+ is OK.
  141. if (minor == 2 && patch >= 79)
  142. return true;
  143. // 3.12.58+ is OK.
  144. if (minor == 12 && patch >= 58)
  145. return true;
  146. if (minor == 10 && patch == 0 && ptr[0] == '-' &&
  147. internal_strstr(ptr, ".el7")) {
  148. // Check RHEL7
  149. int r1 = internal_simple_strtoll(ptr + 1, &ptr, 10);
  150. if (r1 >= 426) // 3.10.0-426.el7 or later
  151. return true;
  152. if (r1 == 327 && ptr[0] == '.') {
  153. int r2 = internal_simple_strtoll(ptr + 1, &ptr, 10);
  154. if (r2 >= 27) // 3.10.0-327.27.1.el7 or later
  155. return true;
  156. }
  157. }
  158. // Otherwise, bad.
  159. return false;
  160. } else if (major == 4) {
  161. // 4.1.21+ is OK.
  162. if (minor == 1 && patch >= 21)
  163. return true;
  164. // 4.4.6+ is OK.
  165. if (minor == 4 && patch >= 6)
  166. return true;
  167. if (minor == 4 && patch == 0 && ptr[0] == '-' &&
  168. internal_strstr(buf.version, "Ubuntu")) {
  169. // Check Ubuntu 16.04
  170. int r1 = internal_simple_strtoll(ptr + 1, &ptr, 10);
  171. if (r1 >= 13) // 4.4.0-13 or later
  172. return true;
  173. }
  174. // Otherwise, OK if 4.5+.
  175. return minor >= 5;
  176. } else {
  177. // Linux 5 and up are fine.
  178. return true;
  179. }
  180. }
  181. void AvoidCVE_2016_2143() {
  182. // Older kernels are affected by CVE-2016-2143 - they will crash hard
  183. // if someone uses 4-level page tables (ie. virtual addresses >= 4TB)
  184. // and fork() in the same process. Unfortunately, sanitizers tend to
  185. // require such addresses. Since this is very likely to crash the whole
  186. // machine (sanitizers themselves use fork() for llvm-symbolizer, for one),
  187. // abort the process at initialization instead.
  188. if (FixedCVE_2016_2143())
  189. return;
  190. if (GetEnv("SANITIZER_IGNORE_CVE_2016_2143"))
  191. return;
  192. Report(
  193. "ERROR: Your kernel seems to be vulnerable to CVE-2016-2143. Using "
  194. "ASan,\n"
  195. "MSan, TSan, DFSan or LSan with such kernel can and will crash your\n"
  196. "machine, or worse.\n"
  197. "\n"
  198. "If you are certain your kernel is not vulnerable (you have compiled it\n"
  199. "yourself, or are using an unrecognized distribution kernel), you can\n"
  200. "override this safety check by exporting SANITIZER_IGNORE_CVE_2016_2143\n"
  201. "with any value.\n");
  202. Die();
  203. }
  204. # endif
  205. } // namespace __sanitizer
  206. #endif // SANITIZER_LINUX && SANITIZER_S390