1 //===-- hwasan_report.cc --------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of HWAddressSanitizer.
11 //
12 // Error reporting.
13 //===----------------------------------------------------------------------===//
14
15 #include "hwasan.h"
16 #include "hwasan_allocator.h"
17 #include "hwasan_mapping.h"
18 #include "hwasan_thread.h"
19 #include "hwasan_thread_list.h"
20 #include "sanitizer_common/sanitizer_allocator_internal.h"
21 #include "sanitizer_common/sanitizer_common.h"
22 #include "sanitizer_common/sanitizer_flags.h"
23 #include "sanitizer_common/sanitizer_mutex.h"
24 #include "sanitizer_common/sanitizer_report_decorator.h"
25 #include "sanitizer_common/sanitizer_stackdepot.h"
26 #include "sanitizer_common/sanitizer_stacktrace_printer.h"
27 #include "sanitizer_common/sanitizer_symbolizer.h"
28
29 using namespace __sanitizer;
30
31 namespace __hwasan {
32
33 class ScopedReport {
34 public:
ScopedReport(bool fatal=false)35 ScopedReport(bool fatal = false) : error_message_(1), fatal(fatal) {
36 BlockingMutexLock lock(&error_message_lock_);
37 error_message_ptr_ = fatal ? &error_message_ : nullptr;
38 }
39
~ScopedReport()40 ~ScopedReport() {
41 BlockingMutexLock lock(&error_message_lock_);
42 if (fatal) {
43 SetAbortMessage(error_message_.data());
44 Die();
45 }
46 error_message_ptr_ = nullptr;
47 }
48
MaybeAppendToErrorMessage(const char * msg)49 static void MaybeAppendToErrorMessage(const char *msg) {
50 BlockingMutexLock lock(&error_message_lock_);
51 if (!error_message_ptr_)
52 return;
53 uptr len = internal_strlen(msg);
54 uptr old_size = error_message_ptr_->size();
55 error_message_ptr_->resize(old_size + len);
56 // overwrite old trailing '\0', keep new trailing '\0' untouched.
57 internal_memcpy(&(*error_message_ptr_)[old_size - 1], msg, len);
58 }
59 private:
60 ScopedErrorReportLock error_report_lock_;
61 InternalMmapVector<char> error_message_;
62 bool fatal;
63
64 static InternalMmapVector<char> *error_message_ptr_;
65 static BlockingMutex error_message_lock_;
66 };
67
68 InternalMmapVector<char> *ScopedReport::error_message_ptr_;
69 BlockingMutex ScopedReport::error_message_lock_;
70
71 // If there is an active ScopedReport, append to its error message.
AppendToErrorMessageBuffer(const char * buffer)72 void AppendToErrorMessageBuffer(const char *buffer) {
73 ScopedReport::MaybeAppendToErrorMessage(buffer);
74 }
75
GetStackTraceFromId(u32 id)76 static StackTrace GetStackTraceFromId(u32 id) {
77 CHECK(id);
78 StackTrace res = StackDepotGet(id);
79 CHECK(res.trace);
80 return res;
81 }
82
83 // A RAII object that holds a copy of the current thread stack ring buffer.
84 // The actual stack buffer may change while we are iterating over it (for
85 // example, Printf may call syslog() which can itself be built with hwasan).
86 class SavedStackAllocations {
87 public:
SavedStackAllocations(StackAllocationsRingBuffer * rb)88 SavedStackAllocations(StackAllocationsRingBuffer *rb) {
89 uptr size = rb->size() * sizeof(uptr);
90 void *storage =
91 MmapAlignedOrDieOnFatalError(size, size * 2, "saved stack allocations");
92 new (&rb_) StackAllocationsRingBuffer(*rb, storage);
93 }
94
~SavedStackAllocations()95 ~SavedStackAllocations() {
96 StackAllocationsRingBuffer *rb = get();
97 UnmapOrDie(rb->StartOfStorage(), rb->size() * sizeof(uptr));
98 }
99
get()100 StackAllocationsRingBuffer *get() {
101 return (StackAllocationsRingBuffer *)&rb_;
102 }
103
104 private:
105 uptr rb_;
106 };
107
108 class Decorator: public __sanitizer::SanitizerCommonDecorator {
109 public:
Decorator()110 Decorator() : SanitizerCommonDecorator() { }
Access()111 const char *Access() { return Blue(); }
Allocation() const112 const char *Allocation() const { return Magenta(); }
Origin() const113 const char *Origin() const { return Magenta(); }
Name() const114 const char *Name() const { return Green(); }
Location()115 const char *Location() { return Green(); }
Thread()116 const char *Thread() { return Green(); }
117 };
118
119 // Returns the index of the rb element that matches tagged_addr (plus one),
120 // or zero if found nothing.
FindHeapAllocation(HeapAllocationsRingBuffer * rb,uptr tagged_addr,HeapAllocationRecord * har)121 uptr FindHeapAllocation(HeapAllocationsRingBuffer *rb,
122 uptr tagged_addr,
123 HeapAllocationRecord *har) {
124 if (!rb) return 0;
125 for (uptr i = 0, size = rb->size(); i < size; i++) {
126 auto h = (*rb)[i];
127 if (h.tagged_addr <= tagged_addr &&
128 h.tagged_addr + h.requested_size > tagged_addr) {
129 *har = h;
130 return i + 1;
131 }
132 }
133 return 0;
134 }
135
PrintAddressDescription(uptr tagged_addr,uptr access_size,StackAllocationsRingBuffer * current_stack_allocations)136 void PrintAddressDescription(
137 uptr tagged_addr, uptr access_size,
138 StackAllocationsRingBuffer *current_stack_allocations) {
139 Decorator d;
140 int num_descriptions_printed = 0;
141 uptr untagged_addr = UntagAddr(tagged_addr);
142
143 // Print some very basic information about the address, if it's a heap.
144 HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr);
145 if (uptr beg = chunk.Beg()) {
146 uptr size = chunk.ActualSize();
147 Printf("%s[%p,%p) is a %s %s heap chunk; "
148 "size: %zd offset: %zd\n%s",
149 d.Location(),
150 beg, beg + size,
151 chunk.FromSmallHeap() ? "small" : "large",
152 chunk.IsAllocated() ? "allocated" : "unallocated",
153 size, untagged_addr - beg,
154 d.Default());
155 }
156
157 // Check if this looks like a heap buffer overflow by scanning
158 // the shadow left and right and looking for the first adjacent
159 // object with a different memory tag. If that tag matches addr_tag,
160 // check the allocator if it has a live chunk there.
161 tag_t addr_tag = GetTagFromPointer(tagged_addr);
162 tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr));
163 if (*tag_ptr != addr_tag) { // should be true usually.
164 tag_t *left = tag_ptr, *right = tag_ptr;
165 // scan left.
166 for (int i = 0; i < 1000 && *left == *tag_ptr; i++, left--){}
167 // scan right.
168 for (int i = 0; i < 1000 && *right == *tag_ptr; i++, right++){}
169 // Chose the object that has addr_tag and that is closer to addr.
170 tag_t *candidate = nullptr;
171 if (*right == addr_tag && *left == addr_tag)
172 candidate = right - tag_ptr < tag_ptr - left ? right : left;
173 else if (*right == addr_tag)
174 candidate = right;
175 else if (*left == addr_tag)
176 candidate = left;
177
178 if (candidate) {
179 uptr mem = ShadowToMem(reinterpret_cast<uptr>(candidate));
180 HwasanChunkView chunk = FindHeapChunkByAddress(mem);
181 if (chunk.IsAllocated()) {
182 Printf("%s", d.Location());
183 Printf(
184 "%p is located %zd bytes to the %s of %zd-byte region [%p,%p)\n",
185 untagged_addr,
186 candidate == left ? untagged_addr - chunk.End()
187 : chunk.Beg() - untagged_addr,
188 candidate == right ? "left" : "right", chunk.UsedSize(),
189 chunk.Beg(), chunk.End());
190 Printf("%s", d.Allocation());
191 Printf("allocated here:\n");
192 Printf("%s", d.Default());
193 GetStackTraceFromId(chunk.GetAllocStackId()).Print();
194 num_descriptions_printed++;
195 }
196 }
197 }
198
199 hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {
200 // Scan all threads' ring buffers to find if it's a heap-use-after-free.
201 HeapAllocationRecord har;
202 if (uptr D = FindHeapAllocation(t->heap_allocations(), tagged_addr, &har)) {
203 Printf("%s", d.Location());
204 Printf("%p is located %zd bytes inside of %zd-byte region [%p,%p)\n",
205 untagged_addr, untagged_addr - UntagAddr(har.tagged_addr),
206 har.requested_size, UntagAddr(har.tagged_addr),
207 UntagAddr(har.tagged_addr) + har.requested_size);
208 Printf("%s", d.Allocation());
209 Printf("freed by thread T%zd here:\n", t->unique_id());
210 Printf("%s", d.Default());
211 GetStackTraceFromId(har.free_context_id).Print();
212
213 Printf("%s", d.Allocation());
214 Printf("previously allocated here:\n", t);
215 Printf("%s", d.Default());
216 GetStackTraceFromId(har.alloc_context_id).Print();
217
218 // Print a developer note: the index of this heap object
219 // in the thread's deallocation ring buffer.
220 Printf("hwasan_dev_note_heap_rb_distance: %zd %zd\n", D,
221 flags()->heap_history_size);
222
223 t->Announce();
224 num_descriptions_printed++;
225 }
226
227 // Very basic check for stack memory.
228 if (t->AddrIsInStack(untagged_addr)) {
229 Printf("%s", d.Location());
230 Printf("Address %p is located in stack of thread T%zd\n", untagged_addr,
231 t->unique_id());
232 Printf("%s", d.Default());
233 t->Announce();
234
235 // Temporary report section, needs to be improved.
236 Printf("Previously allocated frames:\n");
237 auto *sa = (t == GetCurrentThread() && current_stack_allocations)
238 ? current_stack_allocations
239 : t->stack_allocations();
240 uptr frames = Min((uptr)flags()->stack_history_size, sa->size());
241 InternalScopedString frame_desc(GetPageSizeCached() * 2);
242 for (uptr i = 0; i < frames; i++) {
243 uptr record = (*sa)[i];
244 if (!record)
245 break;
246 uptr sp = (record >> 48) << 4;
247 uptr pc_mask = (1ULL << 48) - 1;
248 uptr pc = record & pc_mask;
249 if (SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc)) {
250 frame_desc.append(" sp: 0x%zx pc: %p ", sp, pc);
251 RenderFrame(&frame_desc, "in %f %s:%l\n", 0, frame->info,
252 common_flags()->symbolize_vs_style,
253 common_flags()->strip_path_prefix);
254 frame->ClearAll();
255 if (auto Descr = GetStackFrameDescr(pc))
256 frame_desc.append(" %s\n", Descr);
257 }
258 Printf("%s", frame_desc.data());
259 frame_desc.clear();
260 }
261
262 num_descriptions_printed++;
263 }
264 });
265
266 // Print the remaining threads, as an extra information, 1 line per thread.
267 hwasanThreadList().VisitAllLiveThreads([&](Thread *t) { t->Announce(); });
268
269 if (!num_descriptions_printed)
270 // We exhausted our possibilities. Bail out.
271 Printf("HWAddressSanitizer can not describe address in more detail.\n");
272 }
273
ReportStats()274 void ReportStats() {}
275
PrintTagsAroundAddr(tag_t * tag_ptr)276 static void PrintTagsAroundAddr(tag_t *tag_ptr) {
277 Printf(
278 "Memory tags around the buggy address (one tag corresponds to %zd "
279 "bytes):\n", kShadowAlignment);
280
281 const uptr row_len = 16; // better be power of two.
282 const uptr num_rows = 17;
283 tag_t *center_row_beg = reinterpret_cast<tag_t *>(
284 RoundDownTo(reinterpret_cast<uptr>(tag_ptr), row_len));
285 tag_t *beg_row = center_row_beg - row_len * (num_rows / 2);
286 tag_t *end_row = center_row_beg + row_len * (num_rows / 2);
287 InternalScopedString s(GetPageSizeCached() * 8);
288 for (tag_t *row = beg_row; row < end_row; row += row_len) {
289 s.append("%s", row == center_row_beg ? "=>" : " ");
290 for (uptr i = 0; i < row_len; i++) {
291 s.append("%s", row + i == tag_ptr ? "[" : " ");
292 s.append("%02x", row[i]);
293 s.append("%s", row + i == tag_ptr ? "]" : " ");
294 }
295 s.append("%s\n", row == center_row_beg ? "<=" : " ");
296 }
297 Printf("%s", s.data());
298 }
299
ReportInvalidFree(StackTrace * stack,uptr tagged_addr)300 void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) {
301 ScopedReport R(flags()->halt_on_error);
302
303 uptr untagged_addr = UntagAddr(tagged_addr);
304 tag_t ptr_tag = GetTagFromPointer(tagged_addr);
305 tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr));
306 tag_t mem_tag = *tag_ptr;
307 Decorator d;
308 Printf("%s", d.Error());
309 uptr pc = stack->size ? stack->trace[0] : 0;
310 const char *bug_type = "invalid-free";
311 Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,
312 untagged_addr, pc);
313 Printf("%s", d.Access());
314 Printf("tags: %02x/%02x (ptr/mem)\n", ptr_tag, mem_tag);
315 Printf("%s", d.Default());
316
317 stack->Print();
318
319 PrintAddressDescription(tagged_addr, 0, nullptr);
320
321 PrintTagsAroundAddr(tag_ptr);
322
323 ReportErrorSummary(bug_type, stack);
324 }
325
ReportTailOverwritten(StackTrace * stack,uptr tagged_addr,uptr orig_size,uptr tail_size,const u8 * expected)326 void ReportTailOverwritten(StackTrace *stack, uptr tagged_addr, uptr orig_size,
327 uptr tail_size, const u8 *expected) {
328 ScopedReport R(flags()->halt_on_error);
329 Decorator d;
330 uptr untagged_addr = UntagAddr(tagged_addr);
331 Printf("%s", d.Error());
332 const char *bug_type = "alocation-tail-overwritten";
333 Report("ERROR: %s: %s; heap object [%p,%p) of size %zd\n", SanitizerToolName,
334 bug_type, untagged_addr, untagged_addr + orig_size, orig_size);
335 Printf("\n%s", d.Default());
336 stack->Print();
337 HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr);
338 if (chunk.Beg()) {
339 Printf("%s", d.Allocation());
340 Printf("allocated here:\n");
341 Printf("%s", d.Default());
342 GetStackTraceFromId(chunk.GetAllocStackId()).Print();
343 }
344
345 InternalScopedString s(GetPageSizeCached() * 8);
346 CHECK_GT(tail_size, 0U);
347 CHECK_LT(tail_size, kShadowAlignment);
348 u8 *tail = reinterpret_cast<u8*>(untagged_addr + orig_size);
349 s.append("Tail contains: ");
350 for (uptr i = 0; i < kShadowAlignment - tail_size; i++)
351 s.append(".. ");
352 for (uptr i = 0; i < tail_size; i++)
353 s.append("%02x ", tail[i]);
354 s.append("\n");
355 s.append("Expected: ");
356 for (uptr i = 0; i < kShadowAlignment - tail_size; i++)
357 s.append(".. ");
358 for (uptr i = 0; i < tail_size; i++)
359 s.append("%02x ", expected[i]);
360 s.append("\n");
361 s.append(" ");
362 for (uptr i = 0; i < kShadowAlignment - tail_size; i++)
363 s.append(" ");
364 for (uptr i = 0; i < tail_size; i++)
365 s.append("%s ", expected[i] != tail[i] ? "^^" : " ");
366
367 s.append("\nThis error occurs when a buffer overflow overwrites memory\n"
368 "to the right of a heap object, but within the %zd-byte granule, e.g.\n"
369 " char *x = new char[20];\n"
370 " x[25] = 42;\n"
371 "By default %s does not detect such bugs at the time of write,\n"
372 "but can detect them at the time of free/delete.\n"
373 "To disable this feature set HWASAN_OPTIONS=free_checks_tail_magic=0;\n"
374 "To enable checking at the time of access, set "
375 "HWASAN_OPTIONS=malloc_align_right to non-zero\n\n",
376 kShadowAlignment, SanitizerToolName);
377 Printf("%s", s.data());
378 GetCurrentThread()->Announce();
379
380 tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr));
381 PrintTagsAroundAddr(tag_ptr);
382
383 ReportErrorSummary(bug_type, stack);
384 }
385
ReportTagMismatch(StackTrace * stack,uptr tagged_addr,uptr access_size,bool is_store,bool fatal)386 void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size,
387 bool is_store, bool fatal) {
388 ScopedReport R(fatal);
389 SavedStackAllocations current_stack_allocations(
390 GetCurrentThread()->stack_allocations());
391
392 Decorator d;
393 Printf("%s", d.Error());
394 uptr untagged_addr = UntagAddr(tagged_addr);
395 // TODO: when possible, try to print heap-use-after-free, etc.
396 const char *bug_type = "tag-mismatch";
397 uptr pc = stack->size ? stack->trace[0] : 0;
398 Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,
399 untagged_addr, pc);
400
401 Thread *t = GetCurrentThread();
402
403 tag_t ptr_tag = GetTagFromPointer(tagged_addr);
404 tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr));
405 tag_t mem_tag = *tag_ptr;
406 Printf("%s", d.Access());
407 Printf("%s of size %zu at %p tags: %02x/%02x (ptr/mem) in thread T%zd\n",
408 is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag,
409 mem_tag, t->unique_id());
410 Printf("%s", d.Default());
411
412 stack->Print();
413
414 PrintAddressDescription(tagged_addr, access_size,
415 current_stack_allocations.get());
416 t->Announce();
417
418 PrintTagsAroundAddr(tag_ptr);
419
420 ReportErrorSummary(bug_type, stack);
421 }
422
423 } // namespace __hwasan
424