History

modpost: fix potential segmentation fault for addend_i386_rel()

This may not be a practical problem, but the second pass of ARCH=i386 modpost causes segmentation fault if the -s option is not passed. MODPOST 12 modules Segmentation fault (core dumped) make[2]: *** [scripts/Makefile.modpost:94: __modpost] Error 139 make[1]: *** [Makefile:1339: modules] Error 2 make[1]: *** Waiting for unfinished jobs.... The segmentation fault occurs when section_rel() is called for vmlinux, which is untested in regular builds. The cause of the problem is reloc_location() returning a wrong pointer for ET_EXEC object type. In this case, you need to subtract sechdr->sh_addr, otherwise it would get access beyond the mmap'ed memory. Add sym_get_data_by_offset() helper to avoid code duplication.

Masahiro Yamada committed 6 years ago

Commit d2e4d051 parent e9e81b61 file · +9 −9Browse files

Showing the changes to scripts/mod (1 of 1 file). Show all

Unified · Split

scripts/mod/modpost.c+9−9
@@ -300,19 +300,23 @@ static const char *sec_name(struct elf_info *elf, int secindex) · 299 unmodified lines
300 return sech_name(elf, &elf->sechdrs[secindex]);300 return sech_name(elf, &elf->sechdrs[secindex]);
301}301}
302 302
303static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)303static void *sym_get_data_by_offset(const struct elf_info *info,
304 unsigned int secindex, unsigned long offset)
304{305{
305 unsigned int secindex = get_secindex(info, sym);
306 Elf_Shdr *sechdr = &info->sechdrs[secindex];306 Elf_Shdr *sechdr = &info->sechdrs[secindex];
307 unsigned long offset;
308 307
309 offset = sym->st_value;
310 if (info->hdr->e_type != ET_REL)308 if (info->hdr->e_type != ET_REL)
311 offset -= sechdr->sh_addr;309 offset -= sechdr->sh_addr;
312 310
313 return (void *)info->hdr + sechdr->sh_offset + offset;311 return (void *)info->hdr + sechdr->sh_offset + offset;
314}312}
315 313
314static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
315{
316 return sym_get_data_by_offset(info, get_secindex(info, sym),
317 sym->st_value);
318}
319
316#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)320#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
317 321
318static enum export export_from_secname(struct elf_info *elf, unsigned int sec)322static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
@@ -1752,11 +1756,7 @@ static void check_section_mismatch(const char *modname, struct elf_info *elf, · 1433 unmodified lines
1752static unsigned int *reloc_location(struct elf_info *elf,1756static unsigned int *reloc_location(struct elf_info *elf,
1753 Elf_Shdr *sechdr, Elf_Rela *r)1757 Elf_Shdr *sechdr, Elf_Rela *r)
1754{1758{
1755 Elf_Shdr *sechdrs = elf->sechdrs;1759 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
1756 int section = sechdr->sh_info;
1757
1758 return (void *)elf->hdr + sechdrs[section].sh_offset +
1759 r->r_offset;
1760}1760}
1761 1761
1762static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)1762static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)