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