From 80e9dd08784fc4f28bb04a8e9c0639d433a94d08 Mon Sep 17 00:00:00 2001 From: Georgii Rymar Date: Thu, 20 Aug 2020 18:40:52 +0300 Subject: [PATCH] [llvm-readobj] - Change how we create DynRegionInfo objects. NFCI. Currently we have `checkDRI` and two `createDRIFrom` methods which are used to create `DynRegionInfo` objects. And we have an issue: constructions like: `ObjF->getELFFile()->base() + P->p_offset` that are used in `createDRIFrom` functions might overflow. I had to revert `D85519` which triggered such UBSan failure. This NFC, simplifies and generalizes how we create `DynRegionInfo` objects. It will allow us to introduce more/better validation checks in a single place. It also will allow to change `createDRI` to return `Expected<>` so that we will be able to stop using the `reportError`, which is used inside currently, and have a warning instead. Differential revision: https://reviews.llvm.org/D86297 --- llvm/tools/llvm-readobj/ELFDumper.cpp | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index e171cdc..50612e1 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -243,24 +243,14 @@ private: TYPEDEF_ELF_TYPES(ELFT) - DynRegionInfo checkDRI(DynRegionInfo DRI) { + DynRegionInfo createDRI(uint64_t Offset, uint64_t Size, uint64_t EntSize) { const ELFFile *Obj = ObjF->getELFFile(); - if (DRI.Addr < Obj->base() || - reinterpret_cast(DRI.Addr) + DRI.Size > - Obj->base() + Obj->getBufSize()) + const uint8_t *Addr = Obj->base() + Offset; + if (Addr < Obj->base() || Addr + Size > Obj->base() + Obj->getBufSize()) reportError(errorCodeToError(llvm::object::object_error::parse_failed), ObjF->getFileName()); - return DRI; - } - - DynRegionInfo createDRIFrom(const Elf_Phdr *P, uintX_t EntSize) { - return checkDRI({ObjF->getELFFile()->base() + P->p_offset, P->p_filesz, - EntSize, ObjF->getFileName()}); - } - DynRegionInfo createDRIFrom(const Elf_Shdr *S) { - return checkDRI({ObjF->getELFFile()->base() + S->sh_offset, S->sh_size, - S->sh_entsize, ObjF->getFileName()}); + return {Addr, Size, EntSize, ObjF->getFileName()}; } void printAttributes(); @@ -1936,7 +1926,8 @@ void ELFDumper::loadDynamicTable(const ELFFile *Obj) { DynRegionInfo FromPhdr(ObjF->getFileName()); bool IsPhdrTableValid = false; if (DynamicPhdr) { - FromPhdr = createDRIFrom(DynamicPhdr, sizeof(Elf_Dyn)); + FromPhdr = createDRI(DynamicPhdr->p_offset, DynamicPhdr->p_filesz, + sizeof(Elf_Dyn)); FromPhdr.SizePrintName = "PT_DYNAMIC size"; FromPhdr.EntSizePrintName = ""; @@ -1951,8 +1942,7 @@ void ELFDumper::loadDynamicTable(const ELFFile *Obj) { bool IsSecTableValid = false; if (DynamicSec) { FromSec = - checkDRI({ObjF->getELFFile()->base() + DynamicSec->sh_offset, - DynamicSec->sh_size, sizeof(Elf_Dyn), ObjF->getFileName()}); + createDRI(DynamicSec->sh_offset, DynamicSec->sh_size, sizeof(Elf_Dyn)); FromSec.Context = describe(*DynamicSec); FromSec.EntSizePrintName = ""; @@ -2040,7 +2030,7 @@ ELFDumper::ELFDumper(const object::ELFObjectFile *ObjF, DotDynsymSec = &Sec; if (!DynSymRegion) { - DynSymRegion = createDRIFrom(&Sec); + DynSymRegion = createDRI(Sec.sh_offset, Sec.sh_size, Sec.sh_entsize); DynSymRegion->Context = describe(Sec); if (Expected E = Obj->getStringTableForSymtab(Sec)) -- 2.7.4