[llvm] [llvm-objdump][ELF] Add Section size check. (#86612) (PR #124458)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 26 01:44:03 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-binary-utilities

Author: Cabbaken (cabbaken)

<details>
<summary>Changes</summary>

This change make the check of the section size to
avoid crashing  of llvm-objdump and making warning when processing misformatted elf file.

---
Full diff: https://github.com/llvm/llvm-project/pull/124458.diff


1 Files Affected:

- (modified) llvm/tools/llvm-objdump/ELFDump.cpp (+9) 


``````````diff
diff --git a/llvm/tools/llvm-objdump/ELFDump.cpp b/llvm/tools/llvm-objdump/ELFDump.cpp
index e9e5b059f1786e..34dcf60084e8e3 100644
--- a/llvm/tools/llvm-objdump/ELFDump.cpp
+++ b/llvm/tools/llvm-objdump/ELFDump.cpp
@@ -221,6 +221,10 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
   std::string TagFmt = "  %-" + std::to_string(MaxLen) + "s ";
 
   outs() << "\nDynamic Section:\n";
+  const auto StringTableSize =
+      unwrapOrError(Elf.getSection(ELF::SHT_DYNAMIC), Obj.getFileName())
+          ->sh_size;
+
   for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
     if (Dyn.d_tag == ELF::DT_NULL)
       continue;
@@ -235,6 +239,11 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
       Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
       if (StrTabOrErr) {
         const char *Data = StrTabOrErr->data();
+        if (Dyn.getVal() > StringTableSize) {
+          reportWarning("Invalid string table offset for section .dynstr",
+                        Obj.getFileName());
+          continue;
+        }
         outs() << format(TagFmt.c_str(), Str.c_str()) << Data + Dyn.getVal()
                << "\n";
         continue;

``````````

</details>


https://github.com/llvm/llvm-project/pull/124458


More information about the llvm-commits mailing list