2 2005-06-09 Roland McGrath <roland@redhat.com>
4 * readelf.c (handle_dynamic, handle_symtab): Check for bogus sh_link.
5 (handle_verneed, handle_verdef, handle_versym, handle_hash): Likewise.
6 (handle_scngrp): Check for bogus sh_info.
8 * strip.c (handle_elf): Check for bogus values in sh_link, sh_info,
9 st_shndx, e_shstrndx, and SHT_GROUP or SHT_SYMTAB_SHNDX data.
10 Don't use assert on input values, instead bail with "illformed" error.
12 2005-05-17 Jakub Jelinek <jakub@redhat.com>
15 * elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
16 table fits into object's bounds.
17 * elf_getshstrndx.c (elf_getshstrndx): Add elf->start_offset to
18 elf->map_address. Check if first section header fits into object's
20 * elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)):
21 Check if section header table fits into object's bounds.
22 * elf_begin.c (get_shnum): Ensure section headers fits into
24 (file_read_elf): Make sure scncnt is small enough to allocate both
25 ElfXX_Shdr and Elf_Scn array. Make sure section and program header
26 tables fit into object's bounds. Avoid memory leak on failure.
29 * elflint.c (check_hash): Don't check entries beyond end of section.
30 (check_note): Don't crash if gelf_rawchunk fails.
31 (section_name): Return <invalid> if gelf_getshdr returns NULL.
33 2005-05-14 Jakub Jelinek <jakub@redhat.com>
36 * libelfP.h (INVALID_NDX): Define.
37 * gelf_getdyn.c (gelf_getdyn): Use it. Remove ndx < 0 test if any.
38 * gelf_getlib.c (gelf_getlib): Likewise.
39 * gelf_getmove.c (gelf_getmove): Likewise.
40 * gelf_getrel.c (gelf_getrel): Likewise.
41 * gelf_getrela.c (gelf_getrela): Likewise.
42 * gelf_getsym.c (gelf_getsym): Likewise.
43 * gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
44 * gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
45 * gelf_getversym.c (gelf_getversym): Likewise.
46 * gelf_update_dyn.c (gelf_update_dyn): Likewise.
47 * gelf_update_lib.c (gelf_update_lib): Likewise.
48 * gelf_update_move.c (gelf_update_move): Likewise.
49 * gelf_update_rel.c (gelf_update_rel): Likewise.
50 * gelf_update_rela.c (gelf_update_rela): Likewise.
51 * gelf_update_sym.c (gelf_update_sym): Likewise.
52 * gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
53 * gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
54 * gelf_update_versym.c (gelf_update_versym): Likewise.
55 * elf_newscn.c (elf_newscn): Check for overflow.
56 * elf32_updatefile.c (__elfw2(LIBELFBITS,updatemmap)): Likewise.
57 (__elfw2(LIBELFBITS,updatefile)): Likewise.
58 * elf_begin.c (file_read_elf): Likewise.
59 * elf32_newphdr.c (elfw2(LIBELFBITS,newphdr)): Likewise.
60 * elf_getarsym.c (elf_getarsym): Likewise.
61 * elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Likewise.
63 * elflint.c (section_name): Return "<invalid>" instead of
64 crashing on invalid section name.
65 (check_symtab, is_rel_dyn, check_rela, check_rel, check_dynamic,
66 check_symtab_shndx, check_hash, check_versym): Robustify.
68 --- elfutils-0.135/src/elflint.c.robustify
69 +++ elfutils-0.135/src/elflint.c
70 @@ -131,6 +131,9 @@ static uint32_t shstrndx;
71 /* Array to count references in section groups. */
74 +/* Number of sections. */
75 +static unsigned int shnum;
79 main (int argc, char *argv[])
80 @@ -320,10 +323,19 @@ section_name (Ebl *ebl, int idx)
86 + if ((unsigned int) idx > shnum)
89 shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
93 - return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
94 + ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
101 @@ -345,10 +357,6 @@ static const int valid_e_machine[] =
102 (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
105 -/* Number of sections. */
106 -static unsigned int shnum;
110 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
112 @@ -613,7 +621,8 @@ section [%2d] '%s': symbol table cannot
116 - if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
117 + size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
118 + if (shdr->sh_entsize != sh_entsize)
120 section [%2u] '%s': entry size is does not match ElfXX_Sym\n"),
121 idx, section_name (ebl, idx));
122 @@ -651,7 +660,7 @@ section [%2d] '%s': XINDEX for zeroth en
123 xndxscnidx, section_name (ebl, xndxscnidx));
126 - for (size_t cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
127 + for (size_t cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
129 sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
131 @@ -671,7 +680,8 @@ section [%2d] '%s': symbol %zu: invalid
134 name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
135 - assert (name != NULL);
136 + assert (name != NULL
137 + || strshdr->sh_type != SHT_STRTAB);
140 if (sym->st_shndx == SHN_XINDEX)
141 @@ -1001,9 +1011,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
143 GElf_Shdr rcshdr_mem;
144 const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
145 - assert (rcshdr != NULL);
147 - if (rcshdr->sh_type == SHT_DYNAMIC)
148 + if (rcshdr == NULL)
151 + if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
153 /* Found the dynamic section. Look through it. */
154 Elf_Data *d = elf_getdata (scn, NULL);
155 @@ -1013,7 +1025,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
158 GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
159 - assert (dyn != NULL);
164 if (dyn->d_tag == DT_RELCOUNT)
166 @@ -1027,7 +1041,9 @@ section [%2d] '%s': DT_RELCOUNT used for
167 /* Does the number specified number of relative
168 relocations exceed the total number of
170 - if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
171 + if (shdr->sh_entsize != 0
172 + && dyn->d_un.d_val > (shdr->sh_size
173 + / shdr->sh_entsize))
175 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
176 idx, section_name (ebl, idx),
177 @@ -1187,7 +1203,8 @@ section [%2d] '%s': no relocations for m
181 - if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
182 + size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
183 + if (shdr->sh_entsize != sh_entsize)
184 ERROR (gettext (reltype == ELF_T_RELA ? "\
185 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
186 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
187 @@ -1410,7 +1427,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
188 Elf_Data *symdata = elf_getdata (symscn, NULL);
189 enum load_state state = state_undecided;
191 - for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
192 + size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
193 + for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
196 GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
197 @@ -1460,7 +1478,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
198 Elf_Data *symdata = elf_getdata (symscn, NULL);
199 enum load_state state = state_undecided;
201 - for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
202 + size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
203 + for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
206 GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
207 @@ -1563,7 +1582,8 @@ section [%2d] '%s': referenced as string
208 shdr->sh_link, section_name (ebl, shdr->sh_link),
209 idx, section_name (ebl, idx));
211 - if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
212 + size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
213 + if (shdr->sh_entsize != sh_entsize)
215 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
216 idx, section_name (ebl, idx));
217 @@ -1573,7 +1593,7 @@ section [%2d] '%s': section entry size d
218 idx, section_name (ebl, idx));
220 bool non_null_warned = false;
221 - for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
222 + for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
225 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
226 @@ -1854,6 +1874,8 @@ section [%2d] '%s': entry size does not
227 idx, section_name (ebl, idx));
230 + && shdr->sh_entsize
231 + && symshdr->sh_entsize
232 && (shdr->sh_size / shdr->sh_entsize
233 < symshdr->sh_size / symshdr->sh_entsize))
235 @@ -1880,6 +1902,12 @@ section [%2d] '%s': extended section ind
238 Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
241 + ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
242 + idx, section_name (ebl, idx));
246 if (*((Elf32_Word *) data->d_buf) != 0)
247 ERROR (gettext ("symbol 0 should have zero extended section index\n"));
248 @@ -1922,7 +1950,7 @@ section [%2d] '%s': hash table section i
250 size_t maxidx = nchain;
252 - if (symshdr != NULL)
253 + if (symshdr != NULL && symshdr->sh_entsize != 0)
255 size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
257 @@ -1933,18 +1961,28 @@ section [%2d] '%s': hash table section i
261 + Elf32_Word *buf = (Elf32_Word *) data->d_buf;
262 + Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
264 for (cnt = 2; cnt < 2 + nbucket; ++cnt)
265 - if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
267 + if (buf + cnt >= end)
269 + else if (buf[cnt] >= maxidx)
271 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
272 idx, section_name (ebl, idx), cnt - 2);
275 for (; cnt < 2 + nbucket + nchain; ++cnt)
276 - if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
278 + if (buf + cnt >= end)
280 + else if (buf[cnt] >= maxidx)
282 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
283 idx, section_name (ebl, idx), cnt - 2 - nbucket);
288 @@ -1974,18 +2012,28 @@ section [%2d] '%s': hash table section i
292 + Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
293 + Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
295 for (cnt = 2; cnt < 2 + nbucket; ++cnt)
296 - if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
298 + if (buf + cnt >= end)
300 + else if (buf[cnt] >= maxidx)
302 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
303 idx, section_name (ebl, idx), cnt - 2);
306 for (; cnt < 2 + nbucket + nchain; ++cnt)
307 - if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
309 + if (buf + cnt >= end)
311 + else if (buf[cnt] >= maxidx)
313 section [%2d] '%s': hash chain reference %" PRIu64 " out of bounds\n"),
314 - idx, section_name (ebl, idx), (uint64_t) (cnt - 2 - nbucket));
315 + idx, section_name (ebl, idx), (uint64_t) cnt - 2 - nbucket);
320 @@ -2010,7 +2058,7 @@ section [%2d] '%s': bitmask size not pow
321 if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
324 -section [%2d] '%s': hash table section is too small (is %ld, expected at least%ld)\n"),
325 +section [%2d] '%s': hash table section is too small (is %ld, expected at least %ld)\n"),
326 idx, section_name (ebl, idx), (long int) shdr->sh_size,
327 (long int) ((4 + bitmask_words + nbuckets) * sizeof (Elf32_Word)));
329 @@ -2682,8 +2730,9 @@ section [%2d] '%s' refers in sh_link to
331 /* The number of elements in the version symbol table must be the
332 same as the number of symbols. */
333 - if (shdr->sh_size / shdr->sh_entsize
334 - != symshdr->sh_size / symshdr->sh_entsize)
335 + if (shdr->sh_entsize && symshdr->sh_entsize
336 + && (shdr->sh_size / shdr->sh_entsize
337 + != symshdr->sh_size / symshdr->sh_entsize))
339 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
340 idx, section_name (ebl, idx),
341 diff -up elfutils-0.135/src/readelf.c.robustify elfutils-0.135/src/readelf.c
342 --- elfutils-0.135/src/readelf.c.robustify
343 +++ elfutils-0.135/src/readelf.c
344 @@ -1111,6 +1111,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
345 Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
348 + GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
350 printf ((grpref[0] & GRP_COMDAT)
352 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
353 @@ -1123,8 +1125,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
354 data->d_size / sizeof (Elf32_Word) - 1),
356 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
357 - elf_strptr (ebl->elf, symshdr->sh_link,
358 - gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
359 + (sym == NULL ? NULL
360 + : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
361 ?: gettext ("<INVALID SYMBOL>"),
362 data->d_size / sizeof (Elf32_Word) - 1);
364 @@ -1275,7 +1277,8 @@ static void
365 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
367 int class = gelf_getclass (ebl->elf);
369 + GElf_Shdr glink_mem;
374 @@ -1290,6 +1293,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
375 error (EXIT_FAILURE, 0,
376 gettext ("cannot get section header string table index"));
378 + glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
380 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
384 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
386 @@ -1299,9 +1307,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
387 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
390 - elf_strptr (ebl->elf, shstrndx,
391 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
392 - &glink)->sh_name));
393 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
394 fputs_unlocked (gettext (" Type Value\n"), stdout);
396 for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
397 @@ -1801,6 +1807,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
398 error (EXIT_FAILURE, 0,
399 gettext ("cannot get section header string table index"));
401 + GElf_Shdr glink_mem;
402 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
405 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
408 /* Now we can compute the number of entries in the section. */
409 unsigned int nsyms = data->d_size / (class == ELFCLASS32
411 @@ -1811,15 +1824,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
413 (unsigned int) elf_ndxscn (scn),
414 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
416 printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
417 " %lu local symbols String table: [%2u] '%s'\n",
419 (unsigned long int) shdr->sh_info,
420 (unsigned int) shdr->sh_link,
421 - elf_strptr (ebl->elf, shstrndx,
422 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
423 - &glink)->sh_name));
424 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
426 fputs_unlocked (class == ELFCLASS32
428 @@ -2055,7 +2065,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
429 error (EXIT_FAILURE, 0,
430 gettext ("cannot get section header string table index"));
433 + GElf_Shdr glink_mem;
434 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
437 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
441 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
443 @@ -2066,9 +2082,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
444 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
446 (unsigned int) shdr->sh_link,
447 - elf_strptr (ebl->elf, shstrndx,
448 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
449 - &glink)->sh_name));
450 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
452 unsigned int offset = 0;
453 for (int cnt = shdr->sh_info; --cnt >= 0; )
454 @@ -2121,8 +2135,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
455 error (EXIT_FAILURE, 0,
456 gettext ("cannot get section header string table index"));
458 + GElf_Shdr glink_mem;
459 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
462 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
465 int class = gelf_getclass (ebl->elf);
468 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
470 @@ -2134,9 +2154,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
471 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
473 (unsigned int) shdr->sh_link,
474 - elf_strptr (ebl->elf, shstrndx,
475 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
476 - &glink)->sh_name));
477 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
479 unsigned int offset = 0;
480 for (int cnt = shdr->sh_info; --cnt >= 0; )
481 @@ -2398,8 +2416,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
485 + GElf_Shdr glink_mem;
486 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
489 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
492 /* Print the header. */
495 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
497 @@ -2411,9 +2435,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
498 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
500 (unsigned int) shdr->sh_link,
501 - elf_strptr (ebl->elf, shstrndx,
502 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
503 - &glink)->sh_name));
504 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
506 /* Now we can finally look at the actual contents of this section. */
507 for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
508 @@ -2465,7 +2487,17 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
509 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
510 ++counts[lengths[cnt]];
513 + GElf_Shdr glink_mem;
514 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
519 + error (0, 0, gettext ("invalid sh_link value in section %Zu"),
525 \nHistogram for bucket list length in section [%2u] '%s' (total of %d bucket):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
527 @@ -2478,9 +2510,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
530 (unsigned int) shdr->sh_link,
531 - elf_strptr (ebl->elf, shstrndx,
532 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
533 - &glink)->sh_name));
534 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
536 if (extrastr != NULL)
537 fputs (extrastr, stdout);
538 @@ -4039,6 +4069,16 @@ print_debug_aranges_section (Dwfl_Module
542 + GElf_Shdr glink_mem;
544 + glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
547 + error (0, 0, gettext ("invalid sh_link value in section %Zu"),
553 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
555 diff -up elfutils-0.135/src/strip.c.robustify elfutils-0.135/src/strip.c
556 --- elfutils-0.135/src/strip.c.robustify
557 +++ elfutils-0.135/src/strip.c
558 @@ -544,6 +544,11 @@ handle_elf (int fd, Elf *elf, const char
562 + if (shstrndx >= shnum)
565 +#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
567 /* Storage for section information. We leave room for two more
568 entries since we unconditionally create a section header string
569 table. Maybe some weird tool created an ELF file without one.
570 @@ -565,7 +570,7 @@ handle_elf (int fd, Elf *elf, const char
572 /* This should always be true (i.e., there should not be any
573 holes in the numbering). */
574 - assert (elf_ndxscn (scn) == cnt);
575 + elf_assert (elf_ndxscn (scn) == cnt);
577 shdr_info[cnt].scn = scn;
579 @@ -578,6 +583,7 @@ handle_elf (int fd, Elf *elf, const char
580 shdr_info[cnt].shdr.sh_name);
581 if (shdr_info[cnt].name == NULL)
584 error (0, 0, gettext ("illformed file '%s'"), fname);
587 @@ -587,6 +593,8 @@ handle_elf (int fd, Elf *elf, const char
589 /* Remember the shdr.sh_link value. */
590 shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
591 + if (shdr_info[cnt].old_sh_link >= shnum)
594 /* Sections in files other than relocatable object files which
595 are not loaded can be freely moved by us. In relocatable
596 @@ -599,7 +607,7 @@ handle_elf (int fd, Elf *elf, const char
597 appropriate reference. */
598 if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
600 - assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
601 + elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
602 shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
604 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
605 @@ -616,7 +624,12 @@ handle_elf (int fd, Elf *elf, const char
607 inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
610 + if (grpref[inner] < shnum)
611 shdr_info[grpref[inner]].group_idx = cnt;
616 if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
617 /* If the section group contains only one element and this
618 @@ -627,7 +640,7 @@ handle_elf (int fd, Elf *elf, const char
620 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
622 - assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
623 + elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
624 shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
627 @@ -635,7 +648,7 @@ handle_elf (int fd, Elf *elf, const char
628 discarded right away. */
629 if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
631 - assert (shdr_info[cnt].group_idx != 0);
632 + elf_assert (shdr_info[cnt].group_idx != 0);
634 if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
636 @@ -710,11 +723,15 @@ handle_elf (int fd, Elf *elf, const char
638 /* If a relocation section is marked as being removed make
639 sure the section it is relocating is removed, too. */
640 - if ((shdr_info[cnt].shdr.sh_type == SHT_REL
641 + if (shdr_info[cnt].shdr.sh_type == SHT_REL
642 || shdr_info[cnt].shdr.sh_type == SHT_RELA)
643 - && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
645 + if (shdr_info[cnt].shdr.sh_info >= shnum)
647 + else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
648 shdr_info[cnt].idx = 1;
652 if (shdr_info[cnt].idx == 1)
654 @@ -741,7 +758,7 @@ handle_elf (int fd, Elf *elf, const char
655 if (shdr_info[cnt].symtab_idx != 0
656 && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
658 - assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
659 + elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
661 shdr_info[shdr_info[cnt].symtab_idx].data
662 = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
663 @@ -781,6 +798,9 @@ handle_elf (int fd, Elf *elf, const char
664 else if (scnidx == SHN_XINDEX)
667 + if (scnidx >= shnum)
670 if (shdr_info[scnidx].idx == 0)
671 /* This symbol table has a real symbol in
672 a discarded section. So preserve the
673 @@ -811,12 +831,16 @@ handle_elf (int fd, Elf *elf, const char
676 /* Handle references through sh_info. */
677 - if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
678 - && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
679 + if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
681 + if (shdr_info[cnt].shdr.sh_info >= shnum)
683 + else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
685 shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
686 changes |= shdr_info[cnt].shdr.sh_info < cnt;
690 /* Mark the section as investigated. */
691 shdr_info[cnt].idx = 2;
692 @@ -954,7 +978,7 @@ handle_elf (int fd, Elf *elf, const char
693 error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
696 - assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
697 + elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
699 /* Add this name to the section header string table. */
700 shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
701 @@ -991,7 +1015,7 @@ handle_elf (int fd, Elf *elf, const char
702 error (EXIT_FAILURE, 0,
703 gettext ("while create section header section: %s"),
705 - assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
706 + elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
708 shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
709 if (shdr_info[cnt].data == NULL)
710 @@ -1047,7 +1071,7 @@ handle_elf (int fd, Elf *elf, const char
711 error (EXIT_FAILURE, 0,
712 gettext ("while create section header section: %s"),
714 - assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
715 + elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
717 /* Finalize the string table and fill in the correct indices in the
719 @@ -1137,20 +1161,20 @@ handle_elf (int fd, Elf *elf, const char
720 shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
723 - assert ((versiondata->d_size / sizeof (Elf32_Word))
724 + elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
725 >= shdr_info[cnt].data->d_size / elsize);
728 if (shdr_info[cnt].version_idx != 0)
730 - assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
731 + elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
732 /* This section has associated version
733 information. We have to modify that
735 versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
738 - assert ((versiondata->d_size / sizeof (GElf_Versym))
739 + elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
740 >= shdr_info[cnt].data->d_size / elsize);
743 @@ -1205,7 +1229,7 @@ handle_elf (int fd, Elf *elf, const char
744 sec = shdr_info[sym->st_shndx].idx;
747 - assert (shndxdata != NULL);
748 + elf_assert (shndxdata != NULL);
750 sec = shdr_info[xshndx].idx;
752 @@ -1226,7 +1250,7 @@ handle_elf (int fd, Elf *elf, const char
756 - assert (sec < SHN_LORESERVE || shndxdata != NULL);
757 + elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
759 if ((inner != destidx || nshndx != sym->st_shndx
760 || (shndxdata != NULL && nxshndx != xshndx))
761 @@ -1250,7 +1274,7 @@ handle_elf (int fd, Elf *elf, const char
762 || shdr_info[cnt].debug_data == NULL)
763 /* This is a section symbol for a section which has
765 - assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
766 + elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
769 if (destidx != inner)
770 @@ -1437,11 +1461,11 @@ handle_elf (int fd, Elf *elf, const char
773 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
774 - assert (sym != NULL);
775 + elf_assert (sym != NULL);
777 const char *name = elf_strptr (elf, strshndx,
779 - assert (name != NULL);
780 + elf_assert (name != NULL);
781 size_t hidx = elf_hash (name) % nbucket;
783 if (bucket[hidx] == 0)
784 @@ -1460,7 +1484,7 @@ handle_elf (int fd, Elf *elf, const char
787 /* Alpha and S390 64-bit use 64-bit SHT_HASH entries. */
788 - assert (shdr_info[cnt].shdr.sh_entsize
789 + elf_assert (shdr_info[cnt].shdr.sh_entsize
790 == sizeof (Elf64_Xword));
792 Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
793 @@ -1491,11 +1515,11 @@ handle_elf (int fd, Elf *elf, const char
796 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
797 - assert (sym != NULL);
798 + elf_assert (sym != NULL);
800 const char *name = elf_strptr (elf, strshndx,
802 - assert (name != NULL);
803 + elf_assert (name != NULL);
804 size_t hidx = elf_hash (name) % nbucket;
806 if (bucket[hidx] == 0)
807 diff -up elfutils-0.135/libelf/gelf_getdyn.c.robustify elfutils-0.135/libelf/gelf_getdyn.c
808 --- elfutils-0.135/libelf/gelf_getdyn.c.robustify
809 +++ elfutils-0.135/libelf/gelf_getdyn.c
810 @@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
811 table entries has to be adopted. The user better has provided
812 a buffer where we can store the information. While copying the
813 data we are converting the format. */
814 - if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
815 + if (INVALID_NDX (ndx, Elf32_Dyn)
816 + || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
818 __libelf_seterrno (ELF_E_INVALID_INDEX);
820 @@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
822 /* The data is already in the correct form. Just make sure the
824 - if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
825 + if (INVALID_NDX (ndx, GElf_Dyn)
826 + || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
828 __libelf_seterrno (ELF_E_INVALID_INDEX);
830 diff -up elfutils-0.135/libelf/gelf_getrel.c.robustify elfutils-0.135/libelf/gelf_getrel.c
831 --- elfutils-0.135/libelf/gelf_getrel.c.robustify
832 +++ elfutils-0.135/libelf/gelf_getrel.c
833 @@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
834 if (data_scn == NULL)
837 - if (unlikely (ndx < 0))
839 - __libelf_seterrno (ELF_E_INVALID_INDEX);
843 if (unlikely (data_scn->d.d_type != ELF_T_REL))
845 __libelf_seterrno (ELF_E_INVALID_HANDLE);
846 @@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
847 if (scn->elf->class == ELFCLASS32)
849 /* We have to convert the data. */
850 - if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
851 + if (INVALID_NDX (ndx, Elf32_Rel)
852 + || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
854 __libelf_seterrno (ELF_E_INVALID_INDEX);
856 @@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
858 /* Simply copy the data after we made sure we are actually getting
860 - if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
861 + if (INVALID_NDX (ndx, Elf64_Rel)
862 + || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
864 __libelf_seterrno (ELF_E_INVALID_INDEX);
866 diff -up elfutils-0.135/libelf/gelf_getversym.c.robustify elfutils-0.135/libelf/gelf_getversym.c
867 --- elfutils-0.135/libelf/gelf_getversym.c.robustify
868 +++ elfutils-0.135/libelf/gelf_getversym.c
869 @@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
871 /* The data is already in the correct form. Just make sure the
873 - if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
874 + if (INVALID_NDX (ndx, GElf_Versym)
875 + || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
877 __libelf_seterrno (ELF_E_INVALID_INDEX);
879 diff -up elfutils-0.135/libelf/elf_newscn.c.robustify elfutils-0.135/libelf/elf_newscn.c
880 --- elfutils-0.135/libelf/elf_newscn.c.robustify
881 +++ elfutils-0.135/libelf/elf_newscn.c
882 @@ -104,10 +104,18 @@ elf_newscn (elf)
885 /* We must allocate a new element. */
887 + Elf_ScnList *newp = NULL;
889 assert (elf->state.elf.scnincr > 0);
892 +#if SIZE_MAX <= 4294967295U
893 + likely (elf->state.elf.scnincr
894 + < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
899 newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
900 + ((elf->state.elf.scnincr *= 2)
901 * sizeof (Elf_Scn)), 1);
902 diff -up elfutils-0.135/libelf/elf32_getshdr.c.robustify elfutils-0.135/libelf/elf32_getshdr.c
903 --- elfutils-0.135/libelf/elf32_getshdr.c.robustify
904 +++ elfutils-0.135/libelf/elf32_getshdr.c
905 @@ -101,7 +101,8 @@ elfw2(LIBELFBITS,getshdr) (scn)
909 - if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
910 + if (INTUSE (elf_getshnum) (elf, &shnum) != 0
911 + || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
913 size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
915 @@ -118,6 +119,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
917 if (elf->map_address != NULL)
919 + /* First see whether the information in the ELF header is
920 + valid and it does not ask for too much. */
921 + if (unlikely (ehdr->e_shoff >= elf->maximum_size)
922 + || unlikely (ehdr->e_shoff + size > elf->maximum_size))
924 + /* Something is wrong. */
925 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
929 ElfW2(LIBELFBITS,Shdr) *notcvt;
931 /* All the data is already mapped. If we could use it
932 diff -up elfutils-0.135/libelf/gelf_getsymshndx.c.robustify elfutils-0.135/libelf/gelf_getsymshndx.c
933 --- elfutils-0.135/libelf/gelf_getsymshndx.c.robustify
934 +++ elfutils-0.135/libelf/gelf_getsymshndx.c
935 @@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, nd
936 section index table. */
937 if (likely (shndxdata_scn != NULL))
939 - if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
940 + if (INVALID_NDX (ndx, Elf32_Word)
941 + || unlikely ((ndx + 1) * sizeof (Elf32_Word)
942 + > shndxdata_scn->d.d_size))
944 __libelf_seterrno (ELF_E_INVALID_INDEX);
946 @@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
947 table entries has to be adopted. The user better has provided
948 a buffer where we can store the information. While copying the
949 data we are converting the format. */
950 - if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
951 + if (INVALID_NDX (ndx, Elf32_Sym)
952 + || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
954 __libelf_seterrno (ELF_E_INVALID_INDEX);
956 @@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
958 /* The data is already in the correct form. Just make sure the
960 - if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
961 + if (INVALID_NDX (ndx, GElf_Sym)
962 + || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
964 __libelf_seterrno (ELF_E_INVALID_INDEX);
966 diff -up elfutils-0.135/libelf/gelf_update_versym.c.robustify elfutils-0.135/libelf/gelf_update_versym.c
967 --- elfutils-0.135/libelf/gelf_update_versym.c.robustify
968 +++ elfutils-0.135/libelf/gelf_update_versym.c
969 @@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
970 assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
972 /* Check whether we have to resize the data buffer. */
973 - if (unlikely (ndx < 0)
974 + if (INVALID_NDX (ndx, GElf_Versym)
975 || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
977 __libelf_seterrno (ELF_E_INVALID_INDEX);
978 diff -up elfutils-0.135/libelf/elf_getshstrndx.c.robustify elfutils-0.135/libelf/elf_getshstrndx.c
979 --- elfutils-0.135/libelf/elf_getshstrndx.c.robustify
980 +++ elfutils-0.135/libelf/elf_getshstrndx.c
981 @@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
982 if (elf->map_address != NULL
983 && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
985 - || (((size_t) ((char *) elf->map_address + offset))
986 + || (((size_t) ((char *) elf->map_address
987 + + elf->start_offset + offset))
988 & (__alignof__ (Elf32_Shdr) - 1)) == 0))
990 + /* First see whether the information in the ELF header is
991 + valid and it does not ask for too much. */
992 + if (unlikely (offset + sizeof (Elf32_Shdr)
993 + > elf->maximum_size))
995 + /* Something is wrong. */
996 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
1001 /* We can directly access the memory. */
1002 - num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
1003 + num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
1004 + + offset))->sh_link;
1008 /* We avoid reading in all the section headers. Just read
1009 @@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
1010 if (elf->map_address != NULL
1011 && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
1013 - || (((size_t) ((char *) elf->map_address + offset))
1014 + || (((size_t) ((char *) elf->map_address
1015 + + elf->start_offset + offset))
1016 & (__alignof__ (Elf64_Shdr) - 1)) == 0))
1018 + /* First see whether the information in the ELF header is
1019 + valid and it does not ask for too much. */
1020 + if (unlikely (offset + sizeof (Elf64_Shdr)
1021 + > elf->maximum_size))
1023 + /* Something is wrong. */
1024 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
1029 /* We can directly access the memory. */
1030 - num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
1031 + num = ((Elf64_Shdr *) (elf->map_address
1032 + + elf->start_offset + offset))->sh_link;
1036 /* We avoid reading in all the section headers. Just read
1037 diff -up elfutils-0.135/libelf/libelfP.h.robustify elfutils-0.135/libelf/libelfP.h
1038 --- elfutils-0.135/libelf/libelfP.h.robustify
1039 +++ elfutils-0.135/libelf/libelfP.h
1040 @@ -596,4 +596,13 @@ extern uint32_t __libelf_crc32 (uint32_t
1041 /* Align offset to 4 bytes as needed for note name and descriptor data. */
1042 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1044 +/* Convenience macro. Assumes int NDX and TYPE with size at least
1046 +#if SIZE_MAX > 4294967295U
1047 +# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
1049 +# define INVALID_NDX(ndx, type) \
1050 + unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
1053 #endif /* libelfP.h */
1054 diff -up elfutils-0.135/libelf/gelf_getmove.c.robustify elfutils-0.135/libelf/gelf_getmove.c
1055 --- elfutils-0.135/libelf/gelf_getmove.c.robustify
1056 +++ elfutils-0.135/libelf/gelf_getmove.c
1057 @@ -83,7 +83,8 @@ gelf_getmove (data, ndx, dst)
1059 /* The data is already in the correct form. Just make sure the
1061 - if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
1062 + if (INVALID_NDX (ndx, GElf_Move)
1063 + || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
1065 __libelf_seterrno (ELF_E_INVALID_INDEX);
1067 diff -up elfutils-0.135/libelf/elf32_updatefile.c.robustify elfutils-0.135/libelf/elf32_updatefile.c
1068 --- elfutils-0.135/libelf/elf32_updatefile.c.robustify
1069 +++ elfutils-0.135/libelf/elf32_updatefile.c
1070 @@ -212,6 +212,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
1071 /* Write all the sections. Well, only those which are modified. */
1074 + if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
1077 Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
1078 Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
1079 char *const shdr_start = ((char *) elf->map_address + elf->start_offset
1080 @@ -582,6 +585,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
1081 /* Write all the sections. Well, only those which are modified. */
1084 + if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
1085 + + sizeof (ElfW2(LIBELFBITS,Shdr)))))
1088 off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
1090 xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
1091 diff -up elfutils-0.135/libelf/gelf_update_lib.c.robustify elfutils-0.135/libelf/gelf_update_lib.c
1092 --- elfutils-0.135/libelf/gelf_update_lib.c.robustify
1093 +++ elfutils-0.135/libelf/gelf_update_lib.c
1094 @@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
1098 - if (unlikely (ndx < 0))
1100 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1104 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
1105 if (unlikely (data_scn->d.d_type != ELF_T_LIB))
1107 @@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
1109 /* Check whether we have to resize the data buffer. */
1111 - if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
1112 + if (INVALID_NDX (ndx, Elf64_Lib)
1113 + || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
1114 __libelf_seterrno (ELF_E_INVALID_INDEX);
1117 diff -up elfutils-0.135/libelf/elf32_getphdr.c.robustify elfutils-0.135/libelf/elf32_getphdr.c
1118 --- elfutils-0.135/libelf/elf32_getphdr.c.robustify
1119 +++ elfutils-0.135/libelf/elf32_getphdr.c
1120 @@ -116,6 +116,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
1122 if (elf->map_address != NULL)
1124 + /* First see whether the information in the ELF header is
1125 + valid and it does not ask for too much. */
1126 + if (unlikely (ehdr->e_phoff >= elf->maximum_size)
1127 + || unlikely (ehdr->e_phoff + size > elf->maximum_size))
1129 + /* Something is wrong. */
1130 + __libelf_seterrno (ELF_E_INVALID_PHDR);
1134 /* All the data is already mapped. Use it. */
1135 void *file_phdr = ((char *) elf->map_address
1136 + elf->start_offset + ehdr->e_phoff);
1137 diff -up elfutils-0.135/libelf/gelf_getlib.c.robustify elfutils-0.135/libelf/gelf_getlib.c
1138 --- elfutils-0.135/libelf/gelf_getlib.c.robustify
1139 +++ elfutils-0.135/libelf/gelf_getlib.c
1140 @@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
1141 /* The data is already in the correct form. Just make sure the
1143 GElf_Lib *result = NULL;
1144 - if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
1145 + if (INVALID_NDX (ndx, GElf_Lib)
1146 + || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
1147 __libelf_seterrno (ELF_E_INVALID_INDEX);
1150 diff -up elfutils-0.135/libelf/elf32_newphdr.c.robustify elfutils-0.135/libelf/elf32_newphdr.c
1151 --- elfutils-0.135/libelf/elf32_newphdr.c.robustify
1152 +++ elfutils-0.135/libelf/elf32_newphdr.c
1153 @@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
1154 else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
1155 || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
1157 + if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
1163 /* Allocate a new program header with the appropriate number of
1165 result = (ElfW2(LIBELFBITS,Phdr) *)
1166 diff -up elfutils-0.135/libelf/gelf_update_rela.c.robustify elfutils-0.135/libelf/gelf_update_rela.c
1167 --- elfutils-0.135/libelf/gelf_update_rela.c.robustify
1168 +++ elfutils-0.135/libelf/gelf_update_rela.c
1169 @@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx
1173 - if (unlikely (ndx < 0))
1175 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1179 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
1181 /* The type of the data better should match. */
1182 @@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
1185 /* Check whether we have to resize the data buffer. */
1186 - if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
1187 + if (INVALID_NDX (ndx, Elf32_Rela)
1188 + || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
1190 __libelf_seterrno (ELF_E_INVALID_INDEX);
1192 @@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
1195 /* Check whether we have to resize the data buffer. */
1196 - if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
1197 + if (INVALID_NDX (ndx, Elf64_Rela)
1198 + || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
1200 __libelf_seterrno (ELF_E_INVALID_INDEX);
1202 diff -up elfutils-0.135/libelf/gelf_update_syminfo.c.robustify elfutils-0.135/libelf/gelf_update_syminfo.c
1203 --- elfutils-0.135/libelf/gelf_update_syminfo.c.robustify
1204 +++ elfutils-0.135/libelf/gelf_update_syminfo.c
1205 @@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
1209 - if (unlikely (ndx < 0))
1211 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1215 if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
1217 /* The type of the data better should match. */
1218 @@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
1219 rwlock_wrlock (scn->elf->lock);
1221 /* Check whether we have to resize the data buffer. */
1222 - if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
1223 + if (INVALID_NDX (ndx, GElf_Syminfo)
1224 + || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
1226 __libelf_seterrno (ELF_E_INVALID_INDEX);
1228 diff -up elfutils-0.135/libelf/gelf_update_sym.c.robustify elfutils-0.135/libelf/gelf_update_sym.c
1229 --- elfutils-0.135/libelf/gelf_update_sym.c.robustify
1230 +++ elfutils-0.135/libelf/gelf_update_sym.c
1231 @@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
1235 - if (unlikely (ndx < 0))
1237 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1241 if (unlikely (data_scn->d.d_type != ELF_T_SYM))
1243 /* The type of the data better should match. */
1244 @@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
1247 /* Check whether we have to resize the data buffer. */
1248 - if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
1249 + if (INVALID_NDX (ndx, Elf32_Sym)
1250 + || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
1252 __libelf_seterrno (ELF_E_INVALID_INDEX);
1254 @@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
1257 /* Check whether we have to resize the data buffer. */
1258 - if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
1259 + if (INVALID_NDX (ndx, Elf64_Sym)
1260 + || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
1262 __libelf_seterrno (ELF_E_INVALID_INDEX);
1264 diff -up elfutils-0.135/libelf/gelf_getsyminfo.c.robustify elfutils-0.135/libelf/gelf_getsyminfo.c
1265 --- elfutils-0.135/libelf/gelf_getsyminfo.c.robustify
1266 +++ elfutils-0.135/libelf/gelf_getsyminfo.c
1267 @@ -84,7 +84,8 @@ gelf_getsyminfo (data, ndx, dst)
1269 /* The data is already in the correct form. Just make sure the
1271 - if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
1272 + if (INVALID_NDX (ndx, GElf_Syminfo)
1273 + || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
1275 __libelf_seterrno (ELF_E_INVALID_INDEX);
1277 diff -up elfutils-0.135/libelf/gelf_update_symshndx.c.robustify elfutils-0.135/libelf/gelf_update_symshndx.c
1278 --- elfutils-0.135/libelf/gelf_update_symshndx.c.robustify
1279 +++ elfutils-0.135/libelf/gelf_update_symshndx.c
1280 @@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
1281 if (symdata == NULL)
1284 - if (unlikely (ndx < 0))
1286 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1290 if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
1292 /* The type of the data better should match. */
1293 @@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
1296 /* Check whether we have to resize the data buffer. */
1297 - if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
1298 + if (INVALID_NDX (ndx, Elf32_Sym)
1299 + || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
1301 __libelf_seterrno (ELF_E_INVALID_INDEX);
1303 @@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
1306 /* Check whether we have to resize the data buffer. */
1307 - if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
1308 + if (INVALID_NDX (ndx, Elf64_Sym)
1309 + || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
1311 __libelf_seterrno (ELF_E_INVALID_INDEX);
1313 diff -up elfutils-0.135/libelf/elf_getarsym.c.robustify elfutils-0.135/libelf/elf_getarsym.c
1314 --- elfutils-0.135/libelf/elf_getarsym.c.robustify
1315 +++ elfutils-0.135/libelf/elf_getarsym.c
1316 @@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
1317 size_t index_size = atol (tmpbuf);
1319 if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
1320 +#if SIZE_MAX <= 4294967295U
1321 + || n >= SIZE_MAX / sizeof (Elf_Arsym)
1323 || n * sizeof (uint32_t) > index_size)
1325 /* This index table cannot be right since it does not fit into
1326 diff -up elfutils-0.135/libelf/gelf_getsym.c.robustify elfutils-0.135/libelf/gelf_getsym.c
1327 --- elfutils-0.135/libelf/gelf_getsym.c.robustify
1328 +++ elfutils-0.135/libelf/gelf_getsym.c
1329 @@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
1330 table entries has to be adopted. The user better has provided
1331 a buffer where we can store the information. While copying the
1332 data we are converting the format. */
1333 - if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
1334 + if (INVALID_NDX (ndx, Elf32_Sym)
1335 + || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
1337 __libelf_seterrno (ELF_E_INVALID_INDEX);
1339 @@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
1341 /* The data is already in the correct form. Just make sure the
1343 - if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
1344 + if (INVALID_NDX (ndx, GElf_Sym)
1345 + || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
1347 __libelf_seterrno (ELF_E_INVALID_INDEX);
1349 diff -up elfutils-0.135/libelf/elf_begin.c.robustify elfutils-0.135/libelf/elf_begin.c
1350 --- elfutils-0.135/libelf/elf_begin.c.robustify
1351 +++ elfutils-0.135/libelf/elf_begin.c
1352 @@ -155,7 +155,8 @@ get_shnum (void *map_address, unsigned c
1354 if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
1356 - if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
1357 + if (unlikely (ehdr.e32->e_shoff >= maxsize)
1358 + || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
1359 /* Cannot read the first section header. */
1362 @@ -203,7 +204,8 @@ get_shnum (void *map_address, unsigned c
1364 if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
1366 - if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
1367 + if (unlikely (ehdr.e64->e_shoff >= maxsize)
1368 + || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
1369 /* Cannot read the first section header. */
1372 @@ -275,6 +277,15 @@ file_read_elf (int fildes, void *map_add
1373 /* Could not determine the number of sections. */
1376 + /* Check for too many sections. */
1377 + if (e_ident[EI_CLASS] == ELFCLASS32)
1379 + if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
1382 + else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
1385 /* We can now allocate the memory. */
1386 Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
1387 ELF_K_ELF, scncnt * sizeof (Elf_Scn));
1388 @@ -308,13 +319,31 @@ file_read_elf (int fildes, void *map_add
1390 /* We can use the mmapped memory. */
1391 elf->state.elf32.ehdr = ehdr;
1393 + if (unlikely (ehdr->e_shoff >= maxsize)
1394 + || unlikely (ehdr->e_shoff
1395 + + scncnt * sizeof (Elf32_Shdr) > maxsize))
1399 + __libelf_seterrno (ELF_E_INVALID_FILE);
1402 elf->state.elf32.shdr
1403 = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
1405 if (ehdr->e_phnum > 0)
1407 /* Assign a value only if there really is a program
1408 header. Otherwise the value remains NULL. */
1409 + if (unlikely (ehdr->e_phoff >= maxsize)
1410 + || unlikely (ehdr->e_phoff
1412 + * sizeof (Elf32_Phdr) > maxsize))
1413 + goto free_and_out;
1414 elf->state.elf32.phdr
1415 = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
1418 for (size_t cnt = 0; cnt < scncnt; ++cnt)
1420 @@ -396,13 +425,26 @@ file_read_elf (int fildes, void *map_add
1422 /* We can use the mmapped memory. */
1423 elf->state.elf64.ehdr = ehdr;
1425 + if (unlikely (ehdr->e_shoff >= maxsize)
1426 + || unlikely (ehdr->e_shoff
1427 + + scncnt * sizeof (Elf32_Shdr) > maxsize))
1428 + goto free_and_out;
1429 elf->state.elf64.shdr
1430 = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
1432 if (ehdr->e_phnum > 0)
1434 /* Assign a value only if there really is a program
1435 header. Otherwise the value remains NULL. */
1436 + if (unlikely (ehdr->e_phoff >= maxsize)
1437 + || unlikely (ehdr->e_phoff
1439 + * sizeof (Elf32_Phdr) > maxsize))
1440 + goto free_and_out;
1441 elf->state.elf64.phdr
1442 = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
1445 for (size_t cnt = 0; cnt < scncnt; ++cnt)
1447 diff -up elfutils-0.135/libelf/gelf_update_rel.c.robustify elfutils-0.135/libelf/gelf_update_rel.c
1448 --- elfutils-0.135/libelf/gelf_update_rel.c.robustify
1449 +++ elfutils-0.135/libelf/gelf_update_rel.c
1450 @@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
1454 - if (unlikely (ndx < 0))
1456 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1460 if (unlikely (data_scn->d.d_type != ELF_T_REL))
1462 /* The type of the data better should match. */
1463 @@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
1466 /* Check whether we have to resize the data buffer. */
1467 - if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
1468 + if (INVALID_NDX (ndx, Elf32_Rel)
1469 + || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
1471 __libelf_seterrno (ELF_E_INVALID_INDEX);
1473 @@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
1476 /* Check whether we have to resize the data buffer. */
1477 - if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
1478 + if (INVALID_NDX (ndx, Elf64_Rel)
1479 + || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
1481 __libelf_seterrno (ELF_E_INVALID_INDEX);
1483 diff -up elfutils-0.135/libelf/gelf_update_move.c.robustify elfutils-0.135/libelf/gelf_update_move.c
1484 --- elfutils-0.135/libelf/gelf_update_move.c.robustify
1485 +++ elfutils-0.135/libelf/gelf_update_move.c
1486 @@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
1487 assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
1489 /* Check whether we have to resize the data buffer. */
1490 - if (unlikely (ndx < 0)
1491 + if (INVALID_NDX (ndx, GElf_Move)
1492 || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
1494 __libelf_seterrno (ELF_E_INVALID_INDEX);
1495 diff -up elfutils-0.135/libelf/gelf_update_dyn.c.robustify elfutils-0.135/libelf/gelf_update_dyn.c
1496 --- elfutils-0.135/libelf/gelf_update_dyn.c.robustify
1497 +++ elfutils-0.135/libelf/gelf_update_dyn.c
1498 @@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
1502 - if (unlikely (ndx < 0))
1504 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1508 if (unlikely (data_scn->d.d_type != ELF_T_DYN))
1510 /* The type of the data better should match. */
1511 @@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
1514 /* Check whether we have to resize the data buffer. */
1515 - if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
1516 + if (INVALID_NDX (ndx, Elf32_Dyn)
1517 + || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
1519 __libelf_seterrno (ELF_E_INVALID_INDEX);
1521 @@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
1524 /* Check whether we have to resize the data buffer. */
1525 - if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
1526 + if (INVALID_NDX (ndx, Elf64_Dyn)
1527 + || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
1529 __libelf_seterrno (ELF_E_INVALID_INDEX);
1531 diff -up elfutils-0.135/libelf/gelf_getrela.c.robustify elfutils-0.135/libelf/gelf_getrela.c
1532 --- elfutils-0.135/libelf/gelf_getrela.c.robustify
1533 +++ elfutils-0.135/libelf/gelf_getrela.c
1534 @@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
1535 if (data_scn == NULL)
1538 - if (unlikely (ndx < 0))
1540 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1544 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
1546 __libelf_seterrno (ELF_E_INVALID_HANDLE);
1547 @@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
1548 if (scn->elf->class == ELFCLASS32)
1550 /* We have to convert the data. */
1551 - if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
1552 + if (INVALID_NDX (ndx, Elf32_Rela)
1553 + || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
1555 __libelf_seterrno (ELF_E_INVALID_INDEX);
1557 @@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
1559 /* Simply copy the data after we made sure we are actually getting
1561 - if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
1562 + if (INVALID_NDX (ndx, Elf64_Rela)
1563 + || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
1565 __libelf_seterrno (ELF_E_INVALID_INDEX);