Skip to content

Commit

Permalink
read/elf: check for relocations that apply to relocations (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc authored May 6, 2024
1 parent 284975d commit f0f2d55
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/read/elf/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,25 @@ impl RelocationSections {
continue;
}

let sh_info = section.sh_info(endian) as usize;
if sh_info == 0 {
let sh_info = SectionIndex(section.sh_info(endian) as usize);
if sh_info.0 == 0 {
// Skip dynamic relocations.
continue;
}
if sh_info >= relocations.len() {
if sh_info.0 >= relocations.len() {
return Err(Error("Invalid ELF sh_info for relocation section"));
}

// We don't support relocations that apply to other relocation sections
// because it interferes with the chaining of relocation sections below.
let sh_info_type = sections.section(sh_info)?.sh_type(endian);
if sh_info_type == elf::SHT_REL || sh_info_type == elf::SHT_RELA {
return Err(Error("Unsupported ELF sh_info for relocation section"));
}

// Handle multiple relocation sections by chaining them.
let next = relocations[sh_info];
relocations[sh_info] = index;
let next = relocations[sh_info.0];
relocations[sh_info.0] = index;
relocations[index] = next;
}
}
Expand Down

0 comments on commit f0f2d55

Please sign in to comment.