From a41a32488dfd10b45f74dd3f105be47037506502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Fri, 18 Oct 2024 16:33:23 +0100 Subject: [PATCH] Makes program and section header scan more similar. --- src/elf_parser/mod.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/elf_parser/mod.rs b/src/elf_parser/mod.rs index 21499701..cb5934eb 100644 --- a/src/elf_parser/mod.rs +++ b/src/elf_parser/mod.rs @@ -194,28 +194,20 @@ impl<'a> Elf64<'a> { .filter(|section_header| section_header.sh_type == SHT_NULL) .ok_or(ElfParserError::InvalidSectionHeader)?; - let mut prev_program_header: Option<&Elf64Phdr> = None; + let mut vaddr = 0usize; for program_header in program_header_table { if program_header.p_type != PT_LOAD { continue; } - - if let Some(prev_program_header) = prev_program_header { - // program headers must be ascending - if program_header.p_vaddr < prev_program_header.p_vaddr { - return Err(ElfParserError::InvalidProgramHeader); - } + if (program_header.p_vaddr as usize) < vaddr { + return Err(ElfParserError::InvalidProgramHeader); } - - if program_header + vaddr = program_header .p_offset - .err_checked_add(program_header.p_filesz)? as usize - > elf_bytes.len() - { + .err_checked_add(program_header.p_filesz)? as usize; + if vaddr > elf_bytes.len() { return Err(ElfParserError::OutOfBounds); } - - prev_program_header = Some(program_header) } let mut offset = 0usize; @@ -232,10 +224,10 @@ impl<'a> Elf64<'a> { if section_range.start < offset { return Err(ElfParserError::SectionNotInOrder); } - if section_range.end > elf_bytes.len() { + offset = section_range.end; + if offset > elf_bytes.len() { return Err(ElfParserError::OutOfBounds); } - offset = section_range.end; } let section_names_section_header = (file_header.e_shstrndx != SHN_UNDEF)