Skip to content

Commit

Permalink
elf: Fix elf program header verification logic
Browse files Browse the repository at this point in the history
In the current ELF parser implementation, `verify_phdr` function
returns an error when `p_vaddr` is not aligned. But since spec
doesn't guarantee that `p_vaddr` must be aligned, fixed the check
in our ELF parser. Also, a value of 0 or 1 for `p_align` means
no alignment is required, so fix this check as well.

Signed-off-by: Vijay Dhanraj <[email protected]>
Signed-off-by: Joerg Roedel <[email protected]>
  • Loading branch information
vijaydhanraj authored and joergroedel committed Nov 27, 2024
1 parent 2c1be77 commit d3fa746
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions elf/src/program_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ impl Elf64Phdr {
return Err(ElfError::InvalidSegmentSize);
}

if self.p_align != 0 {
if self.p_align > 1 {
if !self.p_align.is_power_of_two() {
return Err(ElfError::InvalidAddressAlignment);
}
if self.p_vaddr & (self.p_align - 1) != 0 {

if self.p_vaddr % self.p_align != self.p_offset % self.p_align {
return Err(ElfError::UnalignedSegmentAddress);
}
}
Expand Down

0 comments on commit d3fa746

Please sign in to comment.