From d3fa7460fdc516747426e283b04b4f87a32962ed Mon Sep 17 00:00:00 2001 From: Vijay Dhanraj Date: Wed, 23 Oct 2024 08:32:04 -0700 Subject: [PATCH] elf: Fix elf program header verification logic 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 Signed-off-by: Joerg Roedel --- elf/src/program_header.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/elf/src/program_header.rs b/elf/src/program_header.rs index ec91561f9..562bda66e 100644 --- a/elf/src/program_header.rs +++ b/elf/src/program_header.rs @@ -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); } }