This is an archive of the discontinued LLVM Phabricator instance.

ASan: fix potential use-after-free in backtrace interceptor
ClosedPublic

Authored by thurston on May 12 2023, 4:27 PM.

Details

Summary

Various ASan interceptors may corrupt memory if passed a
pointer to freed memory (https://github.com/google/sanitizers/issues/321).
This patch fixes the issue for the backtrace interceptor,
by calling REAL(backtrace) with a known-good scratch buffer,
and performing an addressability check on the user-provided
buffer prior to writing to it.

Diff Detail

Event Timeline

thurston created this revision.May 12 2023, 4:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2023, 4:27 PM
Herald added a subscriber: Enna1. · View Herald Transcript
thurston requested review of this revision.May 12 2023, 4:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2023, 4:27 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
vitalybuka added inline comments.May 12 2023, 10:28 PM
compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
4408

why we can't check with COMMON_INTERCEPTOR_WRITE_RANGE before real and after?
before for asan, after for msan

4414

internal_memcpy?

thurston added inline comments.May 12 2023, 10:33 PM
compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
4408

That would be a stricter check than currently done, because it would have to performed with 'size' rather than 'res'.

For example, if the user calls backtrace with a buffer of 100 elements but a size parameter of 1000000, COMMON_INTERCEPTOR_WRITE_RANGE before REAL would complain because the buffer is not large enough.

However, if the REAL(backtrace) function only ends up returning 50 items, then the buffer is large enough in practice, and it doesn't really matter that the size parameter was too large. The current implementation only checks that the buffer was large enough for the actual number of items written by REAL(backtrace).

4414

Will do.

thurston updated this revision to Diff 521871.May 12 2023, 11:04 PM

Use internal_memcpy as suggested by Vitaly

thurston marked an inline comment as done.May 12 2023, 11:04 PM
vitalybuka accepted this revision.May 12 2023, 11:06 PM
This revision is now accepted and ready to land.May 12 2023, 11:06 PM
This revision was landed with ongoing or failed builds.May 13 2023, 4:22 PM
This revision was automatically updated to reflect the committed changes.