From 182248f364e6375eaad30cefdd6b67660abaa3b3 Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Fri, 19 Jan 2024 09:23:48 -0800 Subject: [PATCH] Validate size of entries before accessing members Signed-off-by: Alan Jowett --- elfio/elfio_relocation.hpp | 25 +++++++++++++++++-------- elfio/elfio_symbols.hpp | 3 +++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/elfio/elfio_relocation.hpp b/elfio/elfio_relocation.hpp index 2f39a36..1da8c92 100644 --- a/elfio/elfio_relocation.hpp +++ b/elfio/elfio_relocation.hpp @@ -96,26 +96,26 @@ template class relocation_section_accessor_template if ( elf_file.get_class() == ELFCLASS32 ) { if ( SHT_REL == relocation_section->get_type() ) { - generic_get_entry_rel( index, offset, symbol, type, + return generic_get_entry_rel( index, offset, symbol, type, addend ); } else if ( SHT_RELA == relocation_section->get_type() ) { - generic_get_entry_rela( index, offset, symbol, type, + return generic_get_entry_rela( index, offset, symbol, type, addend ); } } else { if ( SHT_REL == relocation_section->get_type() ) { - generic_get_entry_rel( index, offset, symbol, type, + return generic_get_entry_rel( index, offset, symbol, type, addend ); } else if ( SHT_RELA == relocation_section->get_type() ) { - generic_get_entry_rela( index, offset, symbol, type, + return generic_get_entry_rela( index, offset, symbol, type, addend ); } } - - return true; + // Unknown relocation section type. + return false; } //------------------------------------------------------------------------------ @@ -319,7 +319,7 @@ template class relocation_section_accessor_template //------------------------------------------------------------------------------ template - void generic_get_entry_rel( Elf_Xword index, + bool generic_get_entry_rel( Elf_Xword index, Elf64_Addr& offset, Elf_Word& symbol, unsigned& type, @@ -327,6 +327,9 @@ template class relocation_section_accessor_template { const endianess_convertor& convertor = elf_file.get_convertor(); + if (relocation_section->get_entry_size() < sizeof( T ) ) { + return false; + } const T* pEntry = reinterpret_cast( relocation_section->get_data() + index * relocation_section->get_entry_size() ); @@ -335,11 +338,12 @@ template class relocation_section_accessor_template symbol = get_sym_and_type::get_r_sym( tmp ); type = get_sym_and_type::get_r_type( tmp ); addend = 0; + return true; } //------------------------------------------------------------------------------ template - void generic_get_entry_rela( Elf_Xword index, + bool generic_get_entry_rela( Elf_Xword index, Elf64_Addr& offset, Elf_Word& symbol, unsigned& type, @@ -347,6 +351,10 @@ template class relocation_section_accessor_template { const endianess_convertor& convertor = elf_file.get_convertor(); + if (relocation_section->get_entry_size() < sizeof( T ) ) { + return false; + } + const T* pEntry = reinterpret_cast( relocation_section->get_data() + index * relocation_section->get_entry_size() ); @@ -355,6 +363,7 @@ template class relocation_section_accessor_template symbol = get_sym_and_type::get_r_sym( tmp ); type = get_sym_and_type::get_r_type( tmp ); addend = convertor( pEntry->r_addend ); + return true; } //------------------------------------------------------------------------------ diff --git a/elfio/elfio_symbols.hpp b/elfio/elfio_symbols.hpp index d868500..71b240a 100644 --- a/elfio/elfio_symbols.hpp +++ b/elfio/elfio_symbols.hpp @@ -395,6 +395,9 @@ template class symbol_section_accessor_template template const T* generic_get_symbol_ptr( Elf_Xword index ) const { if ( 0 != symbol_section->get_data() && index < get_symbols_num() ) { + if ( symbol_section->get_entry_size() < sizeof( T ) ) { + return nullptr; + } const T* pSym = reinterpret_cast( symbol_section->get_data() + index * symbol_section->get_entry_size() );