diff --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp --- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp +++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp @@ -229,7 +229,20 @@ flags()->tag_in_free && malloc_bisect(stack, 0) && atomic_load_relaxed(&hwasan_allocator_tagging_enabled)) { // Always store full 8-bit tags on free to maximize UAF detection. - tag_t tag = t ? t->GenerateRandomTag(/*num_bits=*/8) : kFallbackFreeTag; + tag_t tag; + if (t) { + // Make sure we are not using a short granule tag as a poison tag. This + // would make us attempt to read the memory on a UaF. + // + // Technically, the low kShadowScale bits could be 0 without causing + // problems, but using GenerateRandomTag (which excludes 0) for + // simplicity here. + tag = + (t->GenerateRandomTag(/*num_bits=*/8 - kShadowScale) << kShadowScale) | + t->GenerateRandomTag(/*num_bits=*/kShadowScale); + } else { + tag = kFallbackFreeTag; + } TagMemoryAligned(reinterpret_cast(aligned_ptr), TaggedSize(orig_size), tag); }