This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy][ELF] --only-keep-debug: set offset/size of segments with no sections to zero
ClosedPublic

Authored by MaskRay on Apr 29 2021, 11:58 AM.

Details

Summary

PR50160: we currently ignore non-PT_PHDR segments with no sections, not
accounting for its p_offset and p_filesz: this can cause an out-of-bounds write
in writeSegmentData if the p_offset+p_filesz is larger than the total file
size.

This can be fixed by setting p_offset=p_filesz=0. The logic nicely unifies with
the logic added in D90897.

Diff Detail

Event Timeline

MaskRay created this revision.Apr 29 2021, 11:58 AM
MaskRay requested review of this revision.Apr 29 2021, 11:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 29 2021, 11:58 AM
jhenderson added a comment.EditedApr 30 2021, 1:04 AM

This change makes some sense to me in itself (IIRC GNU objcopy sets segment offsets to 0 for empty segments even without --only-keep-debug), but I don't think the test quite covers the same case as @rupprecht's bug report? In that report, the sections have been stripped. Maybe it doesn't matter, but maybe we need a separate test case for --strip-sections (or equivalent) followed separately by --only-keep-debug?

llvm/test/tools/llvm-objcopy/ELF/only-keep-debug.test
284

Any particular reason for the double blank line?

llvm/tools/llvm-objcopy/ELF/Object.cpp
2320
MaskRay updated this revision to Diff 341939.Apr 30 2021, 9:14 AM
MaskRay marked 2 inline comments as done.

Add a test

pirama added a subscriber: pirama.Apr 30 2021, 10:17 AM
jhenderson accepted this revision.May 4 2021, 1:06 AM

LGTM, but probably would be useful for someone else (especially @rupprecht) to take a quick look.

This revision is now accepted and ready to land.May 4 2021, 1:06 AM
rupprecht accepted this revision.May 5 2021, 10:22 AM
This revision was landed with ongoing or failed builds.May 5 2021, 10:27 AM
This revision was automatically updated to reflect the committed changes.