asan_poisoning.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //===-- asan_poisoning.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 a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // Shadow memory poisoning by ASan RTL and by user application.
  12. //===----------------------------------------------------------------------===//
  13. #include "asan_poisoning.h"
  14. #include "asan_report.h"
  15. #include "asan_stack.h"
  16. #include "sanitizer_common/sanitizer_atomic.h"
  17. #include "sanitizer_common/sanitizer_libc.h"
  18. #include "sanitizer_common/sanitizer_flags.h"
  19. namespace __asan {
  20. static atomic_uint8_t can_poison_memory;
  21. void SetCanPoisonMemory(bool value) {
  22. atomic_store(&can_poison_memory, value, memory_order_release);
  23. }
  24. bool CanPoisonMemory() {
  25. return atomic_load(&can_poison_memory, memory_order_acquire);
  26. }
  27. void PoisonShadow(uptr addr, uptr size, u8 value) {
  28. if (value && !CanPoisonMemory()) return;
  29. CHECK(AddrIsAlignedByGranularity(addr));
  30. CHECK(AddrIsInMem(addr));
  31. CHECK(AddrIsAlignedByGranularity(addr + size));
  32. CHECK(AddrIsInMem(addr + size - ASAN_SHADOW_GRANULARITY));
  33. CHECK(REAL(memset));
  34. FastPoisonShadow(addr, size, value);
  35. }
  36. void PoisonShadowPartialRightRedzone(uptr addr,
  37. uptr size,
  38. uptr redzone_size,
  39. u8 value) {
  40. if (!CanPoisonMemory()) return;
  41. CHECK(AddrIsAlignedByGranularity(addr));
  42. CHECK(AddrIsInMem(addr));
  43. FastPoisonShadowPartialRightRedzone(addr, size, redzone_size, value);
  44. }
  45. struct ShadowSegmentEndpoint {
  46. u8 *chunk;
  47. s8 offset; // in [0, ASAN_SHADOW_GRANULARITY)
  48. s8 value; // = *chunk;
  49. explicit ShadowSegmentEndpoint(uptr address) {
  50. chunk = (u8*)MemToShadow(address);
  51. offset = address & (ASAN_SHADOW_GRANULARITY - 1);
  52. value = *chunk;
  53. }
  54. };
  55. void AsanPoisonOrUnpoisonIntraObjectRedzone(uptr ptr, uptr size, bool poison) {
  56. uptr end = ptr + size;
  57. if (Verbosity()) {
  58. Printf("__asan_%spoison_intra_object_redzone [%p,%p) %zd\n",
  59. poison ? "" : "un", (void *)ptr, (void *)end, size);
  60. if (Verbosity() >= 2)
  61. PRINT_CURRENT_STACK();
  62. }
  63. CHECK(size);
  64. CHECK_LE(size, 4096);
  65. CHECK(IsAligned(end, ASAN_SHADOW_GRANULARITY));
  66. if (!IsAligned(ptr, ASAN_SHADOW_GRANULARITY)) {
  67. *(u8 *)MemToShadow(ptr) =
  68. poison ? static_cast<u8>(ptr % ASAN_SHADOW_GRANULARITY) : 0;
  69. ptr |= ASAN_SHADOW_GRANULARITY - 1;
  70. ptr++;
  71. }
  72. for (; ptr < end; ptr += ASAN_SHADOW_GRANULARITY)
  73. *(u8*)MemToShadow(ptr) = poison ? kAsanIntraObjectRedzone : 0;
  74. }
  75. } // namespace __asan
  76. // ---------------------- Interface ---------------- {{{1
  77. using namespace __asan;
  78. // Current implementation of __asan_(un)poison_memory_region doesn't check
  79. // that user program (un)poisons the memory it owns. It poisons memory
  80. // conservatively, and unpoisons progressively to make sure asan shadow
  81. // mapping invariant is preserved (see detailed mapping description here:
  82. // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm).
  83. //
  84. // * if user asks to poison region [left, right), the program poisons
  85. // at least [left, AlignDown(right)).
  86. // * if user asks to unpoison region [left, right), the program unpoisons
  87. // at most [AlignDown(left), right).
  88. void __asan_poison_memory_region(void const volatile *addr, uptr size) {
  89. if (!flags()->allow_user_poisoning || size == 0) return;
  90. uptr beg_addr = (uptr)addr;
  91. uptr end_addr = beg_addr + size;
  92. VPrintf(3, "Trying to poison memory region [%p, %p)\n", (void *)beg_addr,
  93. (void *)end_addr);
  94. ShadowSegmentEndpoint beg(beg_addr);
  95. ShadowSegmentEndpoint end(end_addr);
  96. if (beg.chunk == end.chunk) {
  97. CHECK_LT(beg.offset, end.offset);
  98. s8 value = beg.value;
  99. CHECK_EQ(value, end.value);
  100. // We can only poison memory if the byte in end.offset is unaddressable.
  101. // No need to re-poison memory if it is poisoned already.
  102. if (value > 0 && value <= end.offset) {
  103. if (beg.offset > 0) {
  104. *beg.chunk = Min(value, beg.offset);
  105. } else {
  106. *beg.chunk = kAsanUserPoisonedMemoryMagic;
  107. }
  108. }
  109. return;
  110. }
  111. CHECK_LT(beg.chunk, end.chunk);
  112. if (beg.offset > 0) {
  113. // Mark bytes from beg.offset as unaddressable.
  114. if (beg.value == 0) {
  115. *beg.chunk = beg.offset;
  116. } else {
  117. *beg.chunk = Min(beg.value, beg.offset);
  118. }
  119. beg.chunk++;
  120. }
  121. REAL(memset)(beg.chunk, kAsanUserPoisonedMemoryMagic, end.chunk - beg.chunk);
  122. // Poison if byte in end.offset is unaddressable.
  123. if (end.value > 0 && end.value <= end.offset) {
  124. *end.chunk = kAsanUserPoisonedMemoryMagic;
  125. }
  126. }
  127. void __asan_unpoison_memory_region(void const volatile *addr, uptr size) {
  128. if (!flags()->allow_user_poisoning || size == 0) return;
  129. uptr beg_addr = (uptr)addr;
  130. uptr end_addr = beg_addr + size;
  131. VPrintf(3, "Trying to unpoison memory region [%p, %p)\n", (void *)beg_addr,
  132. (void *)end_addr);
  133. ShadowSegmentEndpoint beg(beg_addr);
  134. ShadowSegmentEndpoint end(end_addr);
  135. if (beg.chunk == end.chunk) {
  136. CHECK_LT(beg.offset, end.offset);
  137. s8 value = beg.value;
  138. CHECK_EQ(value, end.value);
  139. // We unpoison memory bytes up to enbytes up to end.offset if it is not
  140. // unpoisoned already.
  141. if (value != 0) {
  142. *beg.chunk = Max(value, end.offset);
  143. }
  144. return;
  145. }
  146. CHECK_LT(beg.chunk, end.chunk);
  147. if (beg.offset > 0) {
  148. *beg.chunk = 0;
  149. beg.chunk++;
  150. }
  151. REAL(memset)(beg.chunk, 0, end.chunk - beg.chunk);
  152. if (end.offset > 0 && end.value != 0) {
  153. *end.chunk = Max(end.value, end.offset);
  154. }
  155. }
  156. int __asan_address_is_poisoned(void const volatile *addr) {
  157. return __asan::AddressIsPoisoned((uptr)addr);
  158. }
  159. uptr __asan_region_is_poisoned(uptr beg, uptr size) {
  160. if (!size)
  161. return 0;
  162. uptr end = beg + size;
  163. if (!AddrIsInMem(beg))
  164. return beg;
  165. if (!AddrIsInMem(end))
  166. return end;
  167. CHECK_LT(beg, end);
  168. uptr aligned_b = RoundUpTo(beg, ASAN_SHADOW_GRANULARITY);
  169. uptr aligned_e = RoundDownTo(end, ASAN_SHADOW_GRANULARITY);
  170. uptr shadow_beg = MemToShadow(aligned_b);
  171. uptr shadow_end = MemToShadow(aligned_e);
  172. // First check the first and the last application bytes,
  173. // then check the ASAN_SHADOW_GRANULARITY-aligned region by calling
  174. // mem_is_zero on the corresponding shadow.
  175. if (!__asan::AddressIsPoisoned(beg) && !__asan::AddressIsPoisoned(end - 1) &&
  176. (shadow_end <= shadow_beg ||
  177. __sanitizer::mem_is_zero((const char *)shadow_beg,
  178. shadow_end - shadow_beg)))
  179. return 0;
  180. // The fast check failed, so we have a poisoned byte somewhere.
  181. // Find it slowly.
  182. for (; beg < end; beg++)
  183. if (__asan::AddressIsPoisoned(beg))
  184. return beg;
  185. UNREACHABLE("mem_is_zero returned false, but poisoned byte was not found");
  186. return 0;
  187. }
  188. #define CHECK_SMALL_REGION(p, size, isWrite) \
  189. do { \
  190. uptr __p = reinterpret_cast<uptr>(p); \
  191. uptr __size = size; \
  192. if (UNLIKELY(__asan::AddressIsPoisoned(__p) || \
  193. __asan::AddressIsPoisoned(__p + __size - 1))) { \
  194. GET_CURRENT_PC_BP_SP; \
  195. uptr __bad = __asan_region_is_poisoned(__p, __size); \
  196. __asan_report_error(pc, bp, sp, __bad, isWrite, __size, 0);\
  197. } \
  198. } while (false)
  199. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  200. u16 __sanitizer_unaligned_load16(const uu16 *p) {
  201. CHECK_SMALL_REGION(p, sizeof(*p), false);
  202. return *p;
  203. }
  204. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  205. u32 __sanitizer_unaligned_load32(const uu32 *p) {
  206. CHECK_SMALL_REGION(p, sizeof(*p), false);
  207. return *p;
  208. }
  209. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  210. u64 __sanitizer_unaligned_load64(const uu64 *p) {
  211. CHECK_SMALL_REGION(p, sizeof(*p), false);
  212. return *p;
  213. }
  214. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  215. void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
  216. CHECK_SMALL_REGION(p, sizeof(*p), true);
  217. *p = x;
  218. }
  219. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  220. void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
  221. CHECK_SMALL_REGION(p, sizeof(*p), true);
  222. *p = x;
  223. }
  224. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  225. void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
  226. CHECK_SMALL_REGION(p, sizeof(*p), true);
  227. *p = x;
  228. }
  229. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  230. void __asan_poison_cxx_array_cookie(uptr p) {
  231. if (SANITIZER_WORDSIZE != 64) return;
  232. if (!flags()->poison_array_cookie) return;
  233. uptr s = MEM_TO_SHADOW(p);
  234. *reinterpret_cast<u8*>(s) = kAsanArrayCookieMagic;
  235. }
  236. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  237. uptr __asan_load_cxx_array_cookie(uptr *p) {
  238. if (SANITIZER_WORDSIZE != 64) return *p;
  239. if (!flags()->poison_array_cookie) return *p;
  240. uptr s = MEM_TO_SHADOW(reinterpret_cast<uptr>(p));
  241. u8 sval = *reinterpret_cast<u8*>(s);
  242. if (sval == kAsanArrayCookieMagic) return *p;
  243. // If sval is not kAsanArrayCookieMagic it can only be freed memory,
  244. // which means that we are going to get double-free. So, return 0 to avoid
  245. // infinite loop of destructors. We don't want to report a double-free here
  246. // though, so print a warning just in case.
  247. // CHECK_EQ(sval, kAsanHeapFreeMagic);
  248. if (sval == kAsanHeapFreeMagic) {
  249. Report("AddressSanitizer: loaded array cookie from free-d memory; "
  250. "expect a double-free report\n");
  251. return 0;
  252. }
  253. // The cookie may remain unpoisoned if e.g. it comes from a custom
  254. // operator new defined inside a class.
  255. return *p;
  256. }
  257. // This is a simplified version of __asan_(un)poison_memory_region, which
  258. // assumes that left border of region to be poisoned is properly aligned.
  259. static void PoisonAlignedStackMemory(uptr addr, uptr size, bool do_poison) {
  260. if (size == 0) return;
  261. uptr aligned_size = size & ~(ASAN_SHADOW_GRANULARITY - 1);
  262. PoisonShadow(addr, aligned_size,
  263. do_poison ? kAsanStackUseAfterScopeMagic : 0);
  264. if (size == aligned_size)
  265. return;
  266. s8 end_offset = (s8)(size - aligned_size);
  267. s8* shadow_end = (s8*)MemToShadow(addr + aligned_size);
  268. s8 end_value = *shadow_end;
  269. if (do_poison) {
  270. // If possible, mark all the bytes mapping to last shadow byte as
  271. // unaddressable.
  272. if (end_value > 0 && end_value <= end_offset)
  273. *shadow_end = (s8)kAsanStackUseAfterScopeMagic;
  274. } else {
  275. // If necessary, mark few first bytes mapping to last shadow byte
  276. // as addressable
  277. if (end_value != 0)
  278. *shadow_end = Max(end_value, end_offset);
  279. }
  280. }
  281. void __asan_set_shadow_00(uptr addr, uptr size) {
  282. REAL(memset)((void *)addr, 0, size);
  283. }
  284. void __asan_set_shadow_f1(uptr addr, uptr size) {
  285. REAL(memset)((void *)addr, 0xf1, size);
  286. }
  287. void __asan_set_shadow_f2(uptr addr, uptr size) {
  288. REAL(memset)((void *)addr, 0xf2, size);
  289. }
  290. void __asan_set_shadow_f3(uptr addr, uptr size) {
  291. REAL(memset)((void *)addr, 0xf3, size);
  292. }
  293. void __asan_set_shadow_f5(uptr addr, uptr size) {
  294. REAL(memset)((void *)addr, 0xf5, size);
  295. }
  296. void __asan_set_shadow_f8(uptr addr, uptr size) {
  297. REAL(memset)((void *)addr, 0xf8, size);
  298. }
  299. void __asan_poison_stack_memory(uptr addr, uptr size) {
  300. VReport(1, "poisoning: %p %zx\n", (void *)addr, size);
  301. PoisonAlignedStackMemory(addr, size, true);
  302. }
  303. void __asan_unpoison_stack_memory(uptr addr, uptr size) {
  304. VReport(1, "unpoisoning: %p %zx\n", (void *)addr, size);
  305. PoisonAlignedStackMemory(addr, size, false);
  306. }
  307. void __sanitizer_annotate_contiguous_container(const void *beg_p,
  308. const void *end_p,
  309. const void *old_mid_p,
  310. const void *new_mid_p) {
  311. if (!flags()->detect_container_overflow) return;
  312. VPrintf(2, "contiguous_container: %p %p %p %p\n", beg_p, end_p, old_mid_p,
  313. new_mid_p);
  314. uptr beg = reinterpret_cast<uptr>(beg_p);
  315. uptr end = reinterpret_cast<uptr>(end_p);
  316. uptr old_mid = reinterpret_cast<uptr>(old_mid_p);
  317. uptr new_mid = reinterpret_cast<uptr>(new_mid_p);
  318. uptr granularity = ASAN_SHADOW_GRANULARITY;
  319. if (!(beg <= old_mid && beg <= new_mid && old_mid <= end && new_mid <= end &&
  320. IsAligned(beg, granularity))) {
  321. GET_STACK_TRACE_FATAL_HERE;
  322. ReportBadParamsToAnnotateContiguousContainer(beg, end, old_mid, new_mid,
  323. &stack);
  324. }
  325. CHECK_LE(end - beg,
  326. FIRST_32_SECOND_64(1UL << 30, 1ULL << 40)); // Sanity check.
  327. uptr a = RoundDownTo(Min(old_mid, new_mid), granularity);
  328. uptr c = RoundUpTo(Max(old_mid, new_mid), granularity);
  329. uptr d1 = RoundDownTo(old_mid, granularity);
  330. // uptr d2 = RoundUpTo(old_mid, granularity);
  331. // Currently we should be in this state:
  332. // [a, d1) is good, [d2, c) is bad, [d1, d2) is partially good.
  333. // Make a quick sanity check that we are indeed in this state.
  334. //
  335. // FIXME: Two of these three checks are disabled until we fix
  336. // https://github.com/google/sanitizers/issues/258.
  337. // if (d1 != d2)
  338. // CHECK_EQ(*(u8*)MemToShadow(d1), old_mid - d1);
  339. if (a + granularity <= d1)
  340. CHECK_EQ(*(u8*)MemToShadow(a), 0);
  341. // if (d2 + granularity <= c && c <= end)
  342. // CHECK_EQ(*(u8 *)MemToShadow(c - granularity),
  343. // kAsanContiguousContainerOOBMagic);
  344. uptr b1 = RoundDownTo(new_mid, granularity);
  345. uptr b2 = RoundUpTo(new_mid, granularity);
  346. // New state:
  347. // [a, b1) is good, [b2, c) is bad, [b1, b2) is partially good.
  348. PoisonShadow(a, b1 - a, 0);
  349. PoisonShadow(b2, c - b2, kAsanContiguousContainerOOBMagic);
  350. if (b1 != b2) {
  351. CHECK_EQ(b2 - b1, granularity);
  352. *(u8*)MemToShadow(b1) = static_cast<u8>(new_mid - b1);
  353. }
  354. }
  355. const void *__sanitizer_contiguous_container_find_bad_address(
  356. const void *beg_p, const void *mid_p, const void *end_p) {
  357. if (!flags()->detect_container_overflow)
  358. return nullptr;
  359. uptr beg = reinterpret_cast<uptr>(beg_p);
  360. uptr end = reinterpret_cast<uptr>(end_p);
  361. uptr mid = reinterpret_cast<uptr>(mid_p);
  362. CHECK_LE(beg, mid);
  363. CHECK_LE(mid, end);
  364. // Check some bytes starting from beg, some bytes around mid, and some bytes
  365. // ending with end.
  366. uptr kMaxRangeToCheck = 32;
  367. uptr r1_beg = beg;
  368. uptr r1_end = Min(beg + kMaxRangeToCheck, mid);
  369. uptr r2_beg = Max(beg, mid - kMaxRangeToCheck);
  370. uptr r2_end = Min(end, mid + kMaxRangeToCheck);
  371. uptr r3_beg = Max(end - kMaxRangeToCheck, mid);
  372. uptr r3_end = end;
  373. for (uptr i = r1_beg; i < r1_end; i++)
  374. if (AddressIsPoisoned(i))
  375. return reinterpret_cast<const void *>(i);
  376. for (uptr i = r2_beg; i < mid; i++)
  377. if (AddressIsPoisoned(i))
  378. return reinterpret_cast<const void *>(i);
  379. for (uptr i = mid; i < r2_end; i++)
  380. if (!AddressIsPoisoned(i))
  381. return reinterpret_cast<const void *>(i);
  382. for (uptr i = r3_beg; i < r3_end; i++)
  383. if (!AddressIsPoisoned(i))
  384. return reinterpret_cast<const void *>(i);
  385. return nullptr;
  386. }
  387. int __sanitizer_verify_contiguous_container(const void *beg_p,
  388. const void *mid_p,
  389. const void *end_p) {
  390. return __sanitizer_contiguous_container_find_bad_address(beg_p, mid_p,
  391. end_p) == nullptr;
  392. }
  393. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  394. void __asan_poison_intra_object_redzone(uptr ptr, uptr size) {
  395. AsanPoisonOrUnpoisonIntraObjectRedzone(ptr, size, true);
  396. }
  397. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  398. void __asan_unpoison_intra_object_redzone(uptr ptr, uptr size) {
  399. AsanPoisonOrUnpoisonIntraObjectRedzone(ptr, size, false);
  400. }
  401. // --- Implementation of LSan-specific functions --- {{{1
  402. namespace __lsan {
  403. bool WordIsPoisoned(uptr addr) {
  404. return (__asan_region_is_poisoned(addr, sizeof(uptr)) != 0);
  405. }
  406. }