This is an archive of the discontinued LLVM Phabricator instance.

[IR] Fix replaceUsesWithIf ponetial issue with constants
ClosedPublic

Authored by rampitec on Jun 28 2021, 1:50 PM.

Details

Summary

There can be a use after free in the Value::replaceUsesWithIf()
if two uses point to the same constant. Patch defers handling
of the constants past the iterator scan.

Another potential issue is that handleOperandChange updates all
the uses in a given Constant, not just the one passed to
ShouldReplace. Added a FIXME comment.

Both issues are not currently exploitable as the only use of
this call with constants avoids it.

Diff Detail

Event Timeline

rampitec created this revision.Jun 28 2021, 1:50 PM
rampitec requested review of this revision.Jun 28 2021, 1:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2021, 1:50 PM
efriedma added inline comments.Jun 28 2021, 2:02 PM
llvm/lib/IR/Value.cpp
534

I think you need a TrackingVH here, instead of a raw pointer. Suppose you have something like the following:

ConstantStruct(ConstantStruct(C, C), C)

Say we replaceUsesWithIf on C. Calling handleOperandChange on the inner ConstantStruct will invalidate pointers to the outer CostantStruct. So depending on the use-list order, you might have a dangling pointer.

rampitec updated this revision to Diff 355046.Jun 28 2021, 2:43 PM
rampitec marked an inline comment as done.

Use TrackingVH.

llvm/lib/IR/Value.cpp
534

Like in the updated patch?

This revision is now accepted and ready to land.Jun 28 2021, 3:25 PM
This revision was landed with ongoing or failed builds.Jun 28 2021, 3:57 PM
This revision was automatically updated to reflect the committed changes.