This is an archive of the discontinued LLVM Phabricator instance.

[hwasan] Fixup mmap tagging regions
ClosedPublic

Authored by vitalybuka on Jun 14 2023, 1:11 AM.

Diff Detail

Event Timeline

vitalybuka created this revision.Jun 14 2023, 1:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2023, 1:11 AM
Herald added a subscriber: Enna1. · View Herald Transcript
vitalybuka requested review of this revision.Jun 14 2023, 1:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2023, 1:11 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
thurston added inline comments.Jun 14 2023, 9:12 AM
compiler-rt/lib/hwasan/hwasan_interceptors.cpp
220–223
if (IsAligned(beg, GetPageSize()))
  __hwasan::TagMemoryAligned(beg, RoundUpTo(length, GetPageSize()), 0);
int res = real_munmap(addr, length);
CHECK(IsAligned(beg, GetPageSize()) || (res != 0));

Why not CHECK(IsAligned) at the beginning, to avoid duplicating the check?

CHECK(IsAligned(beg, GetPageSize());
__hwasan::TagMemoryAligned(beg, RoundUpTo(length, GetPageSize()), 0);
int res = real_munmap(addr, length);
CHECK(res != 0);
thurston accepted this revision.Jun 14 2023, 9:18 AM
thurston added inline comments.
compiler-rt/lib/hwasan/hwasan_interceptors.cpp
220–223

Ignore my comment, just noticed it was 'CHECK(IsAligned(beg, GetPageSize()) || (res != 0));' not '&&'

This revision is now accepted and ready to land.Jun 14 2023, 9:18 AM
kstoimenov added inline comments.Jun 14 2023, 11:06 AM
compiler-rt/lib/hwasan/hwasan_interceptors.cpp
220

So if the pointer is not aligned we will not untag? Could you pease explain the logic?

vitalybuka added inline comments.Jun 14 2023, 11:26 AM
compiler-rt/lib/hwasan/hwasan_interceptors.cpp
220

So if the pointer is not aligned we will not untag? Could you pease explain the logic?

This part is confusing.

So if pointer misaligned, real_munmap should fail. So if we untag the shadow, memory will still be there.
Ideally for this issue it would be nice to tag after real_munmap, but it will cause a data race with other threads.

Maybe it's fine just round the region and and untag always.
WDYT?

kstoimenov added inline comments.Jun 14 2023, 2:56 PM
compiler-rt/lib/hwasan/hwasan_interceptors.cpp
220

We should verify that real_munmap fails when the pointer is miss

220

I've read the description of munmap and I think you you did makes sense. You should probably also add a check for valid memory similar to mmap. So the logic will be:

bool untagged = false;
if (Aligned(...) && MemIsApp(...) && length > 0) {
  Untag();
  untagged = true;
}

res = munmap(...)

// This might trigger under legitimate situation. For example the 
// address is owned by the app, aligned and length > 0, but it points 
// to memory which is not mapped. Should we fail at this point? 
if (untagged) CHECK(res == 0);

check length

This revision was landed with ongoing or failed builds.Jun 14 2023, 3:32 PM
This revision was automatically updated to reflect the committed changes.