Skip to content

Commit 125ed24

Browse files
committed
modpost: add array range check to sec_name()
The section index is always positive, so the argument, secindex, should be unsigned. Also, inserted the array range check. If sym->st_shndx is a special section index (between SHN_LORESERVE and SHN_HIRESERVE), there is no corresponding section header. For example, if a symbol specifies an absolute value, sym->st_shndx is SHN_ABS (=0xfff1). The current users do not cause the out-of-range access of info->sechddrs[], but it is better to avoid such a pitfall. Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 36b0f0d commit 125ed24

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

scripts/mod/modpost.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,16 @@ static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
336336
sechdr->sh_name);
337337
}
338338

339-
static const char *sec_name(const struct elf_info *info, int secindex)
339+
static const char *sec_name(const struct elf_info *info, unsigned int secindex)
340340
{
341+
/*
342+
* If sym->st_shndx is a special section index, there is no
343+
* corresponding section header.
344+
* Return "" if the index is out of range of info->sechdrs[] array.
345+
*/
346+
if (secindex >= info->num_sections)
347+
return "";
348+
341349
return sech_name(info, &info->sechdrs[secindex]);
342350
}
343351

0 commit comments

Comments
 (0)