Skip to content

ELF loader should validate before load#687

Open
bentheredonethat wants to merge 7 commits into
OpenAMP:mainfrom
bentheredonethat:amd-elf-load-fixup-june-1-2026
Open

ELF loader should validate before load#687
bentheredonethat wants to merge 7 commits into
OpenAMP:mainfrom
bentheredonethat:amd-elf-load-fixup-june-1-2026

Conversation

@bentheredonethat
Copy link
Copy Markdown
Contributor

OpenAMP’s ELF loader should reject malformed firmware images before using ELF header metadata to size allocations, copy header tables, or look up section names.

OpenAMP’s remoteproc ELF loader trusts several ELF header and section-header fields while parsing firmware images. These fields control program-header and section-header table sizes, table
offsets, segment copy ranges, and section string-table lookups.

The main issue is in the header-table loading path. The loader computes program and section table sizes from firmware-controlled values such as e_phnum * e_phentsize and e_shnum * e_shentsize.
Those computed sizes are then used for allocation and memcpy(), while later code walks the copied buffers as native Elf32_Phdr, Elf64_Phdr, Elf32_Shdr, or Elf64_Shdr arrays. If the count, entry
size, or offset metadata is malformed, the allocation/copy size can diverge from the way the loader later indexes the table.

The range checks around these copies also rely on offset-plus-length arithmetic. With malformed offsets or sizes, unchecked addition can wrap and make an invalid range appear valid. Similar range
assumptions apply when loading the section string table and when the loader asks the backing image store for the next chunk of firmware data.

Section-name lookup has a separate parser-side read issue. The loader searches for sections such as .resource_table by using each section header’s sh_name as an offset into the loaded section
string table. Because sh_name is firmware-controlled, the loader must not form or compare name_table + sh_name unless the offset is within the loaded string table and the referenced string is NUL-
terminated before the end of that table.

The fix makes the ELF loader fail closed on malformed metadata:

  • add portable checked arithmetic helpers for size_t multiplication, addition, and image-chunk range validation;
  • validate ELF header shape before loading tables, including ELF class, ELF header size, program-header entry size, section-header entry size, and section string-table index;
  • use checked multiplication for program-header and section-header table sizes;
  • replace wrapping offset-plus-length checks with overflow-safe range validation;
  • validate the section string-table range before copying it;
  • store the loaded section string-table size in the ELF image state;
  • bound section-name lookup by checking sh_name and comparing only within the loaded string table;
  • avoid wrapping arithmetic in the remoteproc caller’s non-seekable offset comparison.

With these changes, valid firmware images continue to load normally, while malformed images with inconsistent table metadata, overflowing ranges, invalid entry sizes, or out-of-bounds section
names are rejected before allocation, copy, or table lookup proceeds.

Add local helpers in the ELF loader for checked size_t multiplication,
addition, and image chunk range validation.

The ELF loader derives allocation sizes and copy ranges from firmware
header fields. Centralizing the overflow checks keeps later validation
portable across embedded toolchains and avoids relying on compiler
builtins.

Signed-off-by: Ben Levinsky <ben.levinsky@amd.com>
Reject malformed ELF headers before using program or section table
metadata from the firmware image.

Validate the ELF class, ELF header size, program header entry size,
section header entry size, and section string-table index. This
prevents the loader from allocating or indexing native header arrays
using entry sizes that do not match the structures used by OpenAMP.

Signed-off-by: Ben Levinsky <ben.levinsky@amd.com>
Use checked multiplication when computing ELF program and section header
table sizes from e_phnum/e_phentsize and e_shnum/e_shentsize.

Also replace wrapping offset-plus-length range checks with overflow-safe
image chunk validation before allocation and memcpy. Malformed firmware
images with impossible table sizes now fail with -RPROC_EINVAL.

Signed-off-by: Ben Levinsky <ben.levinsky@amd.com>
Use overflow-safe range validation before copying the ELF section string
table from the firmware image.

The section string-table offset and size are read from untrusted section
headers. Validate that the full table is present in the current image
chunk without relying on wrapping offset arithmetic.

Signed-off-by: Ben Levinsky <ben.levinsky@amd.com>
Store the loaded section string-table size in the ELF image information
for both ELF32 and ELF64 images.

Keeping the size alongside the string-table pointer allows later section
name lookups to validate sh_name offsets against the actual loaded table
bounds.

Signed-off-by: Ben Levinsky <ben.levinsky@amd.com>
Make ELF section-name lookup validate sh_name before reading from the
loaded section string table.

Skip malformed section names whose sh_name offset is outside the table
or whose string is not NUL-terminated within the remaining table bytes.
Use bounded comparison for valid candidates so .resource_table lookup
cannot read past the loaded string table.

Signed-off-by: Ben Levinsky <ben.levinsky@amd.com>
Replace the non-seekable loader offset comparison that used offset + len
with an overflow-safe equivalent.

This preserves existing behavior for normal ranges while avoiding a
wraparound case when deciding whether required image data is contiguous
with the current chunk.

Signed-off-by: Ben Levinsky <ben.levinsky@amd.com>
@arnopo arnopo requested review from arnopo, edmooring and tnmysh June 2, 2026 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant