llext: validate st_shndx before indexing sect_hdrs in copy_symbols

llext_copy_symbols() used each ELF symbol's st_shndx directly to
index ext->sect_hdrs[] and, through llext_loaded_sect_ptr(), the
ldr->sect_map[] array. Both arrays are sized e_shnum. The only
existing gate was != SHN_UNDEF, so a st_shndx = 0xFFFE
(or any value >= sect_cnt) would cause an out of bounds read of
both arrays.

The relocation path (llext_lookup_symbol) already guards this with:

  (sym->st_shndx < ldr->hdr.e_shnum) &&

Apply the equivalent discipline to the load path: reject any symbol
whose shndx >= ext->sect_cnt with -ENOEXEC before the first array
access. Because sect_cnt is always much smaller than SHN_LORESERVE
(0xff00), this single test also covers all reserved indices
(SHN_ABS, SHN_COMMON, etc.) that are not valid section array
indices.

Assisted-by: GitHub Copilot:claude-sonnet-4.6
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
Signed-off-by: Lauren Murphy <lauren.murphy@intel.com>
1 file changed