This is an archive of the discontinued LLVM Phabricator instance.

scudo: Make prepareTaggedChunk() and resizeTaggedChunk() generic.
ClosedPublic

Authored by pcc on Apr 20 2021, 4:27 PM.

Details

Summary

Now that we have a more efficient implementation of storeTags(),
we should start using it from resizeTaggedChunk(). With that, plus
a new storeTag() function, resizeTaggedChunk() can be made generic,
and so can prepareTaggedChunk(). Make it so.

Now that the functions are generic, move them to combined.h so that
memtag.h no longer needs to know about chunks.

Depends on D100910

Diff Detail

Event Timeline

pcc requested review of this revision.Apr 20 2021, 4:27 PM
pcc created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptApr 20 2021, 4:27 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
vitalybuka added inline comments.
compiler-rt/lib/scudo/standalone/combined.h
1146

Why we don't tag entire unused tail? It may detect UseAfterFree.

pcc added inline comments.Apr 20 2021, 4:47 PM
compiler-rt/lib/scudo/standalone/combined.h
1146

We detect UAF by retagging on free. So from that perspective there's no advantage to retagging the tail here.

vitalybuka added inline comments.Apr 21 2021, 9:52 AM
compiler-rt/lib/scudo/standalone/combined.h
1146

then why do we need to remove a tag on a single granule here?
To distinguish UAF and out of bounds?

eugenis added inline comments.Apr 21 2021, 10:56 AM
compiler-rt/lib/scudo/standalone/combined.h
1146

For guaranteed linear overflow detection.

In fact, by not retagging the tail, we have guaranteed false negative for overflow by 16 bytes, right? Should we change this? It should be pretty cheap in the primary allocator.

pcc added inline comments.Apr 21 2021, 11:12 AM
compiler-rt/lib/scudo/standalone/combined.h
1146

Hmm, I would expect the cost increase to correspond to the average ratio between size classes (i.e. around sqrt(2)).

Let's investigate doing that as part of a separate change. It should be easier to implement once we have the generic resizeTaggedChunk() implementation in.

vitalybuka added inline comments.Apr 21 2021, 11:21 AM
compiler-rt/lib/scudo/standalone/combined.h
1146

So UAF is not guaranteed, but we want linear overflow guaranteed and without spending cycles to retag entire [RoundNewPtr, BlockEnd)?

vitalybuka added inline comments.Apr 21 2021, 11:26 AM
compiler-rt/lib/scudo/standalone/combined.h
1146

Hmm, I would expect the cost increase to correspond to the average ratio between size classes (i.e. around sqrt(2)).

Let's investigate doing that as part of a separate change. It should be easier to implement once we have the generic resizeTaggedChunk() implementation in.

Agree, this is unchanged in the patch and better addressed (if needed) separately.

pcc added inline comments.Apr 21 2021, 11:35 AM
compiler-rt/lib/scudo/standalone/combined.h
1146

Immediate UAF is guaranteed due to retag on free -- that retags the entire previous allocation (of course, we don't know how large the next allocation to use that chunk will be).

Yes, the reason why we didn't retag that entire region with 0 was to save cycles.

vitalybuka accepted this revision.Apr 21 2021, 1:21 PM
This revision is now accepted and ready to land.Apr 21 2021, 1:21 PM
eugenis accepted this revision.Apr 21 2021, 1:37 PM
eugenis added inline comments.
compiler-rt/lib/scudo/standalone/combined.h
1146

well probably half of that, on average.
SGTM, let's not worry about that for now.