Skip to content

Commit 36aa1dc

Browse files
lrgirdwolgirdwood
authored andcommitted
rimage: bound string table lookups to the section size
A symbol name was duplicated from the string table without verifying a terminator within the section, so an unterminated table could be read past its end. Validate the index and bound the length to the section. Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
1 parent f58d583 commit 36aa1dc

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

tools/rimage/src/elf_file.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,23 @@ int elf_strings_read_by_index(const struct elf_file *elf, int index, struct elf_
531531

532532
int elf_strings_get(const struct elf_strings *strings, int index, char **str)
533533
{
534-
if (index >= strings->section.header.data.size)
534+
size_t size = strings->section.header.data.size;
535+
const char *base = (const char *)strings->section.data;
536+
537+
if (index < 0 || (size_t)index >= size)
538+
return -EINVAL;
539+
540+
/*
541+
* A crafted ELF may provide a string table that is not NUL-terminated;
542+
* make sure a terminator exists within the section before strdup() so
543+
* it cannot read past the end of the mapped section.
544+
*/
545+
if (strnlen(base + index, size - index) == size - index) {
546+
fprintf(stderr, "error: unterminated string in string table\n");
535547
return -EINVAL;
548+
}
536549

537-
*str = strdup((const char *)strings->section.data + index);
550+
*str = strdup(base + index);
538551
if (!*str)
539552
return -ENOMEM;
540553

0 commit comments

Comments
 (0)