lib/elf, lib/pe: Reject non-power-of-2 alignment values
diff --git a/common/lib/elf.c b/common/lib/elf.c
index 407c443e..b7282fe2 100644
--- a/common/lib/elf.c
+++ b/common/lib/elf.c
@@ -725,6 +725,10 @@ static uint64_t elf64_max_align(uint8_t *elf) {
continue;
}
+ if (phdr->p_align > 1 && (phdr->p_align & (phdr->p_align - 1)) != 0) {
+ panic(true, "elf: p_align is not a power of 2");
+ }
+
if (phdr->p_align > ret) {
ret = phdr->p_align;
}
diff --git a/common/lib/pe.c b/common/lib/pe.c
index 967c62ae..5078d42f 100644
--- a/common/lib/pe.c
+++ b/common/lib/pe.c
@@ -271,6 +271,10 @@ bool pe64_load(uint8_t *image, size_t file_size, uint64_t *entry_point, uint64_t
uint64_t image_size = nt_hdrs->OptionalHeader.SizeOfImage;
uint64_t alignment = nt_hdrs->OptionalHeader.SectionAlignment;
+ if (alignment > 1 && (alignment & (alignment - 1)) != 0) {
+ panic(true, "pe: SectionAlignment is not a power of 2");
+ }
+
bool lower_to_higher = false;
if (image_base < FIXED_HIGHER_HALF_OFFSET_64) {
