This is an archive of the discontinued LLVM Phabricator instance.

[llvm] Check for overflows when computing load-command's addresses.
Needs ReviewPublic

Authored by oontvoo on Nov 28 2022, 9:15 AM.

Details

Summary

PR/56746

Diff Detail

Event Timeline

oontvoo created this revision.Nov 28 2022, 9:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 9:15 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
oontvoo requested review of this revision.Nov 28 2022, 9:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 9:15 AM
This revision is now accepted and ready to land.Nov 28 2022, 9:58 AM
llvm/lib/Object/MachOObjectFile.cpp
202

nit: I'd probably report a separate message for the overflow case (cause it'd make debugging a tiny bit easier)

tschuett added inline comments.
llvm/lib/Object/MachOObjectFile.cpp
202

nit: could you instead use an integer with a defined bit width: uint64_t?

oontvoo marked 2 inline comments as done.Nov 28 2022, 10:27 AM
oontvoo added inline comments.
llvm/lib/Object/MachOObjectFile.cpp
202

nit: could you instead use an integer with a defined bit width: uint64_t?

How about uintptr_t ?

oontvoo updated this revision to Diff 478279.Nov 28 2022, 10:38 AM
oontvoo marked an inline comment as done.
  • updated type
  • separate overflow case to different err msg to make debugging easier
jyknight added inline comments.Nov 28 2022, 11:29 AM
llvm/lib/Object/MachOObjectFile.cpp
202

long unsigned int should be uintptr_t here.
And I think the comparison should be >=?

230

Probably all of the checks here can go away, except to check that L.Ptr + L.C.cmdsize doesn't overflow.

All the range checks are done by getLoadCommandInfo anyways?

tschuett added inline comments.Nov 28 2022, 11:39 AM
llvm/lib/Object/MachOObjectFile.cpp
232

I am known to be stupid. You do saturating add with uintptr_t and claim errors on uint32_t?

Would it help to make the asserts into real errors? I like to be informed about malformed files in release builds.

oontvoo updated this revision to Diff 478332.Nov 28 2022, 12:35 PM

addressed review comment:

  • refactor code so that the overflow checks can be done in place
  • remove redundant check
  • fix off-by-one
oontvoo requested review of this revision.Nov 28 2022, 12:38 PM
oontvoo marked an inline comment as done.
oontvoo added inline comments.
llvm/lib/Object/MachOObjectFile.cpp
232

oops, sorry - forgot to update the error messages when I updated the types.
yes - can make these errors instead of asserts.

oontvoo updated this revision to Diff 478684.Nov 29 2022, 12:40 PM
oontvoo marked an inline comment as done.

updated err msgs and removed a few asserts

llvm/lib/Object/MachOObjectFile.cpp
134

A slightly more general version:

template <class T, class... Ts>
std::enable_if_t<std::is_unsigned<T>::value, T>
SaturatingAdd(T X, T Y, T Z, Ts... args) {
  bool Overflowed = false;
  T XY = SaturatingAdd(X, Y, &Overflowed);
  if (Overflowed)
    return SaturatingAdd(std::numeric_limits<T>::max(), 1, args...);
  return SaturatingAdd(XY, Z, args...);
}

(p.s. if you like - i can send a small patch with this addition to MathExtras)

oontvoo added inline comments.Nov 30 2022, 5:51 AM
llvm/lib/Object/MachOObjectFile.cpp
134

sure! Thanks!