Add sanity checks for e_shentsize and e_shstrndx
This helps us avoid situations when malformed elf-files
are mistakenly loaded, - which might result in unexpected
behavior.
Bug: http://b/30166532
Test: bionic-unit-tests --gtest_filter=dl*:Dl*
Change-Id: Idd6b4fa20e1d69a9f8d8391ba69c724d930fee51
diff --git a/linker/linker_phdr.cpp b/linker/linker_phdr.cpp
index a7af82f..ebc3166 100644
--- a/linker/linker_phdr.cpp
+++ b/linker/linker_phdr.cpp
@@ -245,6 +245,17 @@
return false;
}
+ if (header_.e_shentsize != sizeof(ElfW(Shdr))) {
+ DL_ERR("\"%s\" has unsupported e_shentsize: 0x%x (expected 0x%zx)",
+ name_.c_str(), header_.e_shentsize, sizeof(ElfW(Shdr)));
+ return false;
+ }
+
+ if (header_.e_shstrndx == 0) {
+ DL_ERR("\"%s\" has invalid e_shstrndx", name_.c_str());
+ return false;
+ }
+
return true;
}