Skip to content

Commit c9d4137

Browse files
committed
[llvm-objdump][ELF] Add Section size check. (#86612)
This change make the check of the section size to avoid crashing of llvm-objdump when processing misformated elf file. Signed-off-by: cabbaken <[email protected]>
1 parent 850852e commit c9d4137

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

llvm/tools/llvm-objdump/ELFDump.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
221221
std::string TagFmt = " %-" + std::to_string(MaxLen) + "s ";
222222

223223
outs() << "\nDynamic Section:\n";
224+
auto DynamicSectionOrErr = Elf.getSection(ELF::SHT_DYNAMIC);
225+
if (!DynamicSectionOrErr) {
226+
reportWarning(toString(DynamicSectionOrErr.takeError()), Obj.getFileName());
227+
return;
228+
}
229+
const auto StringTableSize = (*DynamicSectionOrErr)->sh_size;
230+
224231
for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
225232
if (Dyn.d_tag == ELF::DT_NULL)
226233
continue;
@@ -235,6 +242,11 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
235242
Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
236243
if (StrTabOrErr) {
237244
const char *Data = StrTabOrErr->data();
245+
if (Dyn.getVal() > StringTableSize) {
246+
reportWarning("Invalid string table offset for section .dynstr",
247+
Obj.getFileName());
248+
continue;
249+
}
238250
outs() << format(TagFmt.c_str(), Str.c_str()) << Data + Dyn.getVal()
239251
<< "\n";
240252
continue;

0 commit comments

Comments
 (0)