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.127/src/readelf.c.robustify
69 +++ elfutils-0.127/src/readelf.c
70 @@ -958,6 +958,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
71 Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
74 + GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
76 printf ((grpref[0] & GRP_COMDAT)
78 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
79 @@ -970,8 +972,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
80 data->d_size / sizeof (Elf32_Word) - 1),
82 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
83 - elf_strptr (ebl->elf, symshdr->sh_link,
84 - gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
86 + : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
87 ?: gettext ("<INVALID SYMBOL>"),
88 data->d_size / sizeof (Elf32_Word) - 1);
90 @@ -1122,7 +1124,8 @@ static void
91 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
93 int class = gelf_getclass (ebl->elf);
95 + GElf_Shdr glink_mem;
100 @@ -1137,6 +1140,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
101 error (EXIT_FAILURE, 0,
102 gettext ("cannot get section header string table index"));
104 + glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
106 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
110 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
112 @@ -1146,9 +1154,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
113 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
116 - elf_strptr (ebl->elf, shstrndx,
117 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
118 - &glink)->sh_name));
119 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
120 fputs_unlocked (gettext (" Type Value\n"), stdout);
122 for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
123 @@ -1666,6 +1672,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
124 error (EXIT_FAILURE, 0,
125 gettext ("cannot get section header string table index"));
127 + GElf_Shdr glink_mem;
128 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
131 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
134 /* Now we can compute the number of entries in the section. */
135 unsigned int nsyms = data->d_size / (class == ELFCLASS32
137 @@ -1676,15 +1689,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
139 (unsigned int) elf_ndxscn (scn),
140 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
142 printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
143 " %lu local symbols String table: [%2u] '%s'\n",
145 (unsigned long int) shdr->sh_info,
146 (unsigned int) shdr->sh_link,
147 - elf_strptr (ebl->elf, shstrndx,
148 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
149 - &glink)->sh_name));
150 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
152 fputs_unlocked (class == ELFCLASS32
154 @@ -1920,7 +1930,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
155 error (EXIT_FAILURE, 0,
156 gettext ("cannot get section header string table index"));
159 + GElf_Shdr glink_mem;
160 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
163 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
167 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
169 @@ -1931,9 +1947,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
170 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
172 (unsigned int) shdr->sh_link,
173 - elf_strptr (ebl->elf, shstrndx,
174 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
175 - &glink)->sh_name));
176 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
178 unsigned int offset = 0;
179 for (int cnt = shdr->sh_info; --cnt >= 0; )
180 @@ -1986,8 +2000,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
181 error (EXIT_FAILURE, 0,
182 gettext ("cannot get section header string table index"));
184 + GElf_Shdr glink_mem;
185 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
188 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
191 int class = gelf_getclass (ebl->elf);
194 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
196 @@ -1999,9 +2019,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
197 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
199 (unsigned int) shdr->sh_link,
200 - elf_strptr (ebl->elf, shstrndx,
201 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
202 - &glink)->sh_name));
203 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
205 unsigned int offset = 0;
206 for (int cnt = shdr->sh_info; --cnt >= 0; )
207 @@ -2263,8 +2281,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
211 + GElf_Shdr glink_mem;
212 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
215 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
218 /* Print the header. */
221 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
223 @@ -2276,9 +2300,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
224 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
226 (unsigned int) shdr->sh_link,
227 - elf_strptr (ebl->elf, shstrndx,
228 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
229 - &glink)->sh_name));
230 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
232 /* Now we can finally look at the actual contents of this section. */
233 for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
234 @@ -2330,7 +2352,17 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
235 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
236 ++counts[lengths[cnt]];
239 + GElf_Shdr glink_mem;
240 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
245 + error (0, 0, gettext ("invalid sh_link value in section %Zu"),
251 \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",
253 @@ -2343,9 +2375,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
256 (unsigned int) shdr->sh_link,
257 - elf_strptr (ebl->elf, shstrndx,
258 - gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
259 - &glink)->sh_name));
260 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
262 if (extrastr != NULL)
263 fputs (extrastr, stdout);
264 @@ -3655,6 +3685,16 @@ print_debug_aranges_section (Ebl *ebl __
268 + GElf_Shdr glink_mem;
270 + glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
273 + error (0, 0, gettext ("invalid sh_link value in section %Zu"),
279 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
281 --- elfutils-0.127/src/strip.c.robustify
282 +++ elfutils-0.127/src/strip.c
283 @@ -412,6 +412,7 @@ handle_elf (int fd, Elf *elf, const char
284 Elf_Data debuglink_crc_data;
285 bool any_symtab_changes = false;
286 Elf_Data *shstrtab_data = NULL;
287 + size_t shdridx = 0;
289 /* Create the full name of the file. */
291 @@ -542,6 +543,11 @@ handle_elf (int fd, Elf *elf, const char
295 + if (shstrndx >= shnum)
298 +#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
300 /* Storage for section information. We leave room for two more
301 entries since we unconditionally create a section header string
302 table. Maybe some weird tool created an ELF file without one.
303 @@ -563,7 +569,7 @@ handle_elf (int fd, Elf *elf, const char
305 /* This should always be true (i.e., there should not be any
306 holes in the numbering). */
307 - assert (elf_ndxscn (scn) == cnt);
308 + elf_assert (elf_ndxscn (scn) == cnt);
310 shdr_info[cnt].scn = scn;
312 @@ -576,6 +582,7 @@ handle_elf (int fd, Elf *elf, const char
313 shdr_info[cnt].shdr.sh_name);
314 if (shdr_info[cnt].name == NULL)
317 error (0, 0, gettext ("illformed file '%s'"), fname);
320 @@ -585,6 +592,8 @@ handle_elf (int fd, Elf *elf, const char
322 /* Remember the shdr.sh_link value. */
323 shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
324 + if (shdr_info[cnt].old_sh_link >= shnum)
327 /* Sections in files other than relocatable object files which
328 are not loaded can be freely moved by us. In relocatable
329 @@ -597,7 +606,7 @@ handle_elf (int fd, Elf *elf, const char
330 appropriate reference. */
331 if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
333 - assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
334 + elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
335 shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
337 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
338 @@ -614,7 +623,12 @@ handle_elf (int fd, Elf *elf, const char
340 inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
343 + if (grpref[inner] < shnum)
344 shdr_info[grpref[inner]].group_idx = cnt;
349 if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
350 /* If the section group contains only one element and this
351 @@ -625,7 +639,7 @@ handle_elf (int fd, Elf *elf, const char
353 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
355 - assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
356 + elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
357 shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
360 @@ -633,7 +647,7 @@ handle_elf (int fd, Elf *elf, const char
361 discarded right away. */
362 if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
364 - assert (shdr_info[cnt].group_idx != 0);
365 + elf_assert (shdr_info[cnt].group_idx != 0);
367 if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
369 @@ -708,11 +722,15 @@ handle_elf (int fd, Elf *elf, const char
371 /* If a relocation section is marked as being removed make
372 sure the section it is relocating is removed, too. */
373 - if ((shdr_info[cnt].shdr.sh_type == SHT_REL
374 + if (shdr_info[cnt].shdr.sh_type == SHT_REL
375 || shdr_info[cnt].shdr.sh_type == SHT_RELA)
376 - && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
378 + if (shdr_info[cnt].shdr.sh_info >= shnum)
380 + else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
381 shdr_info[cnt].idx = 1;
385 if (shdr_info[cnt].idx == 1)
387 @@ -737,7 +755,7 @@ handle_elf (int fd, Elf *elf, const char
388 if (shdr_info[cnt].symtab_idx != 0
389 && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
391 - assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
392 + elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
394 shdr_info[shdr_info[cnt].symtab_idx].data
395 = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
396 @@ -777,6 +795,9 @@ handle_elf (int fd, Elf *elf, const char
397 else if (scnidx == SHN_XINDEX)
400 + if (scnidx >= shnum)
403 if (shdr_info[scnidx].idx == 0)
405 /* Mark this section as used. */
406 @@ -808,12 +829,16 @@ handle_elf (int fd, Elf *elf, const char
409 /* Handle references through sh_info. */
410 - if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
411 - && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
412 + if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
414 + if (shdr_info[cnt].shdr.sh_info >= shnum)
416 + else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
418 shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
419 changes |= shdr_info[cnt].shdr.sh_info < cnt;
423 /* Mark the section as investigated. */
424 shdr_info[cnt].idx = 2;
425 @@ -912,7 +937,7 @@ handle_elf (int fd, Elf *elf, const char
426 error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
429 - assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
430 + elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
432 /* Add this name to the section header string table. */
433 shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
434 @@ -949,7 +974,7 @@ handle_elf (int fd, Elf *elf, const char
435 error (EXIT_FAILURE, 0,
436 gettext ("while create section header section: %s"),
438 - assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
439 + elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
441 shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
442 if (shdr_info[cnt].data == NULL)
443 @@ -980,7 +1005,7 @@ handle_elf (int fd, Elf *elf, const char
446 /* Index of the section header table in the shdr_info array. */
447 - size_t shdridx = cnt;
450 /* Add the section header string table section name. */
451 shdr_info[cnt].se = ebl_strtabadd (shst, ".shstrtab", 10);
452 @@ -1005,7 +1030,7 @@ handle_elf (int fd, Elf *elf, const char
453 error (EXIT_FAILURE, 0,
454 gettext ("while create section header section: %s"),
456 - assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
457 + elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
459 /* Finalize the string table and fill in the correct indices in the
461 @@ -1095,20 +1120,20 @@ handle_elf (int fd, Elf *elf, const char
462 shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
465 - assert ((versiondata->d_size / sizeof (Elf32_Word))
466 + elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
467 >= shdr_info[cnt].data->d_size / elsize);
470 if (shdr_info[cnt].version_idx != 0)
472 - assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
473 + elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
474 /* This section has associated version
475 information. We have to modify that
477 versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
480 - assert ((versiondata->d_size / sizeof (GElf_Versym))
481 + elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
482 >= shdr_info[cnt].data->d_size / elsize);
485 @@ -1163,7 +1188,7 @@ handle_elf (int fd, Elf *elf, const char
486 sec = shdr_info[sym->st_shndx].idx;
489 - assert (shndxdata != NULL);
490 + elf_assert (shndxdata != NULL);
492 sec = shdr_info[xshndx].idx;
494 @@ -1184,7 +1209,7 @@ handle_elf (int fd, Elf *elf, const char
498 - assert (sec < SHN_LORESERVE || shndxdata != NULL);
499 + elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
501 if ((inner != destidx || nshndx != sym->st_shndx
502 || (shndxdata != NULL && nxshndx != xshndx))
503 @@ -1207,7 +1232,7 @@ handle_elf (int fd, Elf *elf, const char
505 /* This is a section symbol for a section which has
507 - assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
508 + elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
511 if (destidx != inner)
512 @@ -1371,11 +1396,11 @@ handle_elf (int fd, Elf *elf, const char
515 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
516 - assert (sym != NULL);
517 + elf_assert (sym != NULL);
519 const char *name = elf_strptr (elf, strshndx,
521 - assert (name != NULL);
522 + elf_assert (name != NULL);
523 size_t hidx = elf_hash (name) % nbucket;
525 if (bucket[hidx] == 0)
526 @@ -1394,7 +1419,7 @@ handle_elf (int fd, Elf *elf, const char
529 /* Alpha and S390 64-bit use 64-bit SHT_HASH entries. */
530 - assert (shdr_info[cnt].shdr.sh_entsize
531 + elf_assert (shdr_info[cnt].shdr.sh_entsize
532 == sizeof (Elf64_Xword));
534 Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
535 @@ -1428,11 +1453,11 @@ handle_elf (int fd, Elf *elf, const char
538 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
539 - assert (sym != NULL);
540 + elf_assert (sym != NULL);
542 const char *name = elf_strptr (elf, strshndx,
544 - assert (name != NULL);
545 + elf_assert (name != NULL);
546 size_t hidx = elf_hash (name) % nbucket;
548 if (bucket[hidx] == 0)
549 --- elfutils-0.127/src/elflint.c.robustify
550 +++ elfutils-0.127/src/elflint.c
551 @@ -123,6 +123,9 @@ static uint32_t shstrndx;
552 /* Array to count references in section groups. */
555 +/* Number of sections. */
556 +static unsigned int shnum;
560 main (int argc, char *argv[])
561 @@ -312,10 +315,19 @@ section_name (Ebl *ebl, int idx)
567 + if ((unsigned int) idx > shnum)
568 + return "<invalid>";
570 shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
572 + return "<invalid>";
574 - return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
575 + ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
577 + return "<invalid>";
582 @@ -337,10 +349,6 @@ static const int valid_e_machine[] =
583 (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
586 -/* Number of sections. */
587 -static unsigned int shnum;
591 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
593 @@ -603,7 +611,8 @@ section [%2d] '%s': symbol table cannot
597 - if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
598 + size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
599 + if (shdr->sh_entsize != sh_entsize)
601 section [%2zu] '%s': entry size is does not match ElfXX_Sym\n"),
602 cnt, section_name (ebl, cnt));
603 @@ -641,7 +650,7 @@ section [%2d] '%s': XINDEX for zeroth en
604 xndxscnidx, section_name (ebl, xndxscnidx));
607 - for (cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
608 + for (cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
610 sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
612 @@ -659,7 +668,8 @@ section [%2d] '%s': symbol %zu: invalid
615 name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
616 - assert (name != NULL);
617 + assert (name != NULL
618 + || strshdr->sh_type != SHT_STRTAB);
621 if (sym->st_shndx == SHN_XINDEX)
622 @@ -981,9 +991,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
624 GElf_Shdr rcshdr_mem;
625 const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
626 - assert (rcshdr != NULL);
628 - if (rcshdr->sh_type == SHT_DYNAMIC)
629 + if (rcshdr == NULL)
632 + if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
634 /* Found the dynamic section. Look through it. */
635 Elf_Data *d = elf_getdata (scn, NULL);
636 @@ -993,7 +1005,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
639 GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
640 - assert (dyn != NULL);
645 if (dyn->d_tag == DT_RELCOUNT)
647 @@ -1007,7 +1021,9 @@ section [%2d] '%s': DT_RELCOUNT used for
648 /* Does the number specified number of relative
649 relocations exceed the total number of
651 - if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
652 + if (shdr->sh_entsize != 0
653 + && dyn->d_un.d_val > (shdr->sh_size
654 + / shdr->sh_entsize))
656 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
657 idx, section_name (ebl, idx),
658 @@ -1167,7 +1183,8 @@ section [%2d] '%s': no relocations for m
662 - if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
663 + size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
664 + if (shdr->sh_entsize != sh_entsize)
665 ERROR (gettext (reltype == ELF_T_RELA ? "\
666 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
667 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
668 @@ -1389,7 +1406,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
669 Elf_Data *symdata = elf_getdata (symscn, NULL);
670 enum load_state state = state_undecided;
672 - for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
673 + size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
674 + for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
677 GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
678 @@ -1439,7 +1457,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
679 Elf_Data *symdata = elf_getdata (symscn, NULL);
680 enum load_state state = state_undecided;
682 - for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
683 + size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
684 + for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
687 GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
688 @@ -1543,7 +1562,8 @@ section [%2d] '%s': referenced as string
689 shdr->sh_link, section_name (ebl, shdr->sh_link),
690 idx, section_name (ebl, idx));
692 - if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
693 + size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
694 + if (shdr->sh_entsize != sh_entsize)
696 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
697 idx, section_name (ebl, idx));
698 @@ -1553,7 +1573,7 @@ section [%2d] '%s': section entry size d
699 idx, section_name (ebl, idx));
701 bool non_null_warned = false;
702 - for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
703 + for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
706 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
707 @@ -1834,6 +1854,8 @@ section [%2d] '%s': entry size does not
708 idx, section_name (ebl, idx));
711 + && shdr->sh_entsize
712 + && symshdr->sh_entsize
713 && (shdr->sh_size / shdr->sh_entsize
714 < symshdr->sh_size / symshdr->sh_entsize))
716 @@ -1860,6 +1882,12 @@ section [%2d] '%s': extended section ind
719 Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
722 + ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
723 + idx, section_name (ebl, idx));
727 if (*((Elf32_Word *) data->d_buf) != 0)
728 ERROR (gettext ("symbol 0 should have zero extended section index\n"));
729 @@ -1902,7 +1930,7 @@ section [%2d] '%s': hash table section i
731 size_t maxidx = nchain;
733 - if (symshdr != NULL)
734 + if (symshdr != NULL && symshdr->sh_entsize != 0)
736 size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
738 @@ -1913,18 +1941,28 @@ section [%2d] '%s': hash table section i
742 + Elf32_Word *buf = (Elf32_Word *) data->d_buf;
743 + Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
745 for (cnt = 2; cnt < 2 + nbucket; ++cnt)
746 - if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
748 + if (buf + cnt >= end)
750 + else if (buf[cnt] >= maxidx)
752 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
753 idx, section_name (ebl, idx), cnt - 2);
756 for (; cnt < 2 + nbucket + nchain; ++cnt)
757 - if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
759 + if (buf + cnt >= end)
761 + else if (buf[cnt] >= maxidx)
763 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
764 idx, section_name (ebl, idx), cnt - 2 - nbucket);
769 @@ -1954,18 +1992,28 @@ section [%2d] '%s': hash table section i
773 + Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
774 + Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
776 for (cnt = 2; cnt < 2 + nbucket; ++cnt)
777 - if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
779 + if (buf + cnt >= end)
781 + else if (buf[cnt] >= maxidx)
783 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
784 idx, section_name (ebl, idx), cnt - 2);
787 for (; cnt < 2 + nbucket + nchain; ++cnt)
788 - if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
790 + if (buf + cnt >= end)
792 + else if (buf[cnt] >= maxidx)
794 section [%2d] '%s': hash chain reference %" PRIu64 " out of bounds\n"),
795 - idx, section_name (ebl, idx), (uint64_t) (cnt - 2 - nbucket));
796 + idx, section_name (ebl, idx), (uint64_t) cnt - 2 - nbucket);
801 @@ -1990,7 +2038,7 @@ section [%2d] '%s': bitmask size not pow
802 if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
805 -section [%2d] '%s': hash table section is too small (is %ld, expected at least%ld)\n"),
806 +section [%2d] '%s': hash table section is too small (is %ld, expected at least %ld)\n"),
807 idx, section_name (ebl, idx), (long int) shdr->sh_size,
808 (long int) ((4 + bitmask_words + nbuckets) * sizeof (Elf32_Word)));
810 @@ -2644,8 +2692,9 @@ section [%2d] '%s' refers in sh_link to
812 /* The number of elements in the version symbol table must be the
813 same as the number of symbols. */
814 - if (shdr->sh_size / shdr->sh_entsize
815 - != symshdr->sh_size / symshdr->sh_entsize)
816 + if (shdr->sh_entsize && symshdr->sh_entsize
817 + && (shdr->sh_size / shdr->sh_entsize
818 + != symshdr->sh_size / symshdr->sh_entsize))
820 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
821 idx, section_name (ebl, idx),
822 @@ -3574,6 +3623,8 @@ phdr[%d]: no note entries defined for th
825 char *notemem = gelf_rawchunk (ebl->elf, phdr->p_offset, phdr->p_filesz);
826 + if (notemem == NULL)
829 /* ELF64 files often use note section entries in the 32-bit format.
830 The p_align field is set to 8 in case the 64-bit format is used.
831 --- elfutils-0.127/libelf/elf_begin.c.robustify
832 +++ elfutils-0.127/libelf/elf_begin.c
833 @@ -155,7 +155,8 @@ get_shnum (void *map_address, unsigned c
835 if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
837 - if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
838 + if (unlikely (ehdr.e32->e_shoff >= maxsize)
839 + || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
840 /* Cannot read the first section header. */
843 @@ -203,7 +204,8 @@ get_shnum (void *map_address, unsigned c
845 if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
847 - if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
848 + if (unlikely (ehdr.e64->e_shoff >= maxsize)
849 + || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
850 /* Cannot read the first section header. */
853 @@ -275,6 +277,15 @@ file_read_elf (int fildes, void *map_add
854 /* Could not determine the number of sections. */
857 + /* Check for too many sections. */
858 + if (e_ident[EI_CLASS] == ELFCLASS32)
860 + if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
863 + else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
866 /* We can now allocate the memory. */
867 Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
868 ELF_K_ELF, scncnt * sizeof (Elf_Scn));
869 @@ -308,13 +319,31 @@ file_read_elf (int fildes, void *map_add
871 /* We can use the mmapped memory. */
872 elf->state.elf32.ehdr = ehdr;
874 + if (unlikely (ehdr->e_shoff >= maxsize)
875 + || unlikely (ehdr->e_shoff
876 + + scncnt * sizeof (Elf32_Shdr) > maxsize))
880 + __libelf_seterrno (ELF_E_INVALID_FILE);
883 elf->state.elf32.shdr
884 = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
886 if (ehdr->e_phnum > 0)
888 /* Assign a value only if there really is a program
889 header. Otherwise the value remains NULL. */
890 + if (unlikely (ehdr->e_phoff >= maxsize)
891 + || unlikely (ehdr->e_phoff
893 + * sizeof (Elf32_Phdr) > maxsize))
895 elf->state.elf32.phdr
896 = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
899 for (size_t cnt = 0; cnt < scncnt; ++cnt)
901 @@ -383,13 +412,26 @@ file_read_elf (int fildes, void *map_add
903 /* We can use the mmapped memory. */
904 elf->state.elf64.ehdr = ehdr;
906 + if (unlikely (ehdr->e_shoff >= maxsize)
907 + || unlikely (ehdr->e_shoff
908 + + scncnt * sizeof (Elf32_Shdr) > maxsize))
910 elf->state.elf64.shdr
911 = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
913 if (ehdr->e_phnum > 0)
915 /* Assign a value only if there really is a program
916 header. Otherwise the value remains NULL. */
917 + if (unlikely (ehdr->e_phoff >= maxsize)
918 + || unlikely (ehdr->e_phoff
920 + * sizeof (Elf32_Phdr) > maxsize))
922 elf->state.elf64.phdr
923 = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
926 for (size_t cnt = 0; cnt < scncnt; ++cnt)
928 --- elfutils-0.127/libelf/libelfP.h.robustify
929 +++ elfutils-0.127/libelf/libelfP.h
930 @@ -574,4 +574,13 @@ extern uint32_t __libelf_crc32 (uint32_t
934 +/* Convenience macro. Assumes int NDX and TYPE with size at least
936 +#if SIZE_MAX > 4294967295U
937 +# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
939 +# define INVALID_NDX(ndx, type) \
940 + unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
943 #endif /* libelfP.h */
944 --- elfutils-0.127/libelf/gelf_update_move.c.robustify
945 +++ elfutils-0.127/libelf/gelf_update_move.c
946 @@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
947 assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
949 /* Check whether we have to resize the data buffer. */
950 - if (unlikely (ndx < 0)
951 + if (INVALID_NDX (ndx, GElf_Move)
952 || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
954 __libelf_seterrno (ELF_E_INVALID_INDEX);
955 --- elfutils-0.127/libelf/gelf_getsym.c.robustify
956 +++ elfutils-0.127/libelf/gelf_getsym.c
957 @@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
958 table entries has to be adopted. The user better has provided
959 a buffer where we can store the information. While copying the
960 data we are converting the format. */
961 - if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
962 + if (INVALID_NDX (ndx, Elf32_Sym)
963 + || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
965 __libelf_seterrno (ELF_E_INVALID_INDEX);
967 @@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
969 /* The data is already in the correct form. Just make sure the
971 - if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
972 + if (INVALID_NDX (ndx, GElf_Sym)
973 + || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
975 __libelf_seterrno (ELF_E_INVALID_INDEX);
977 --- elfutils-0.127/libelf/gelf_getrela.c.robustify
978 +++ elfutils-0.127/libelf/gelf_getrela.c
979 @@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
980 if (data_scn == NULL)
983 - if (unlikely (ndx < 0))
985 - __libelf_seterrno (ELF_E_INVALID_INDEX);
989 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
991 __libelf_seterrno (ELF_E_INVALID_HANDLE);
992 @@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
993 if (scn->elf->class == ELFCLASS32)
995 /* We have to convert the data. */
996 - if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
997 + if (INVALID_NDX (ndx, Elf32_Rela)
998 + || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
1000 __libelf_seterrno (ELF_E_INVALID_INDEX);
1002 @@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
1004 /* Simply copy the data after we made sure we are actually getting
1006 - if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
1007 + if (INVALID_NDX (ndx, Elf64_Rela)
1008 + || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
1010 __libelf_seterrno (ELF_E_INVALID_INDEX);
1012 --- elfutils-0.127/libelf/gelf_getmove.c.robustify
1013 +++ elfutils-0.127/libelf/gelf_getmove.c
1014 @@ -83,7 +83,8 @@ gelf_getmove (data, ndx, dst)
1016 /* The data is already in the correct form. Just make sure the
1018 - if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
1019 + if (INVALID_NDX (ndx, GElf_Move)
1020 + || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
1022 __libelf_seterrno (ELF_E_INVALID_INDEX);
1024 --- elfutils-0.127/libelf/gelf_update_symshndx.c.robustify
1025 +++ elfutils-0.127/libelf/gelf_update_symshndx.c
1026 @@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
1027 if (symdata == NULL)
1030 - if (unlikely (ndx < 0))
1032 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1036 if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
1038 /* The type of the data better should match. */
1039 @@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
1042 /* Check whether we have to resize the data buffer. */
1043 - if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
1044 + if (INVALID_NDX (ndx, Elf32_Sym)
1045 + || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
1047 __libelf_seterrno (ELF_E_INVALID_INDEX);
1049 @@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
1052 /* Check whether we have to resize the data buffer. */
1053 - if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
1054 + if (INVALID_NDX (ndx, Elf64_Sym)
1055 + || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
1057 __libelf_seterrno (ELF_E_INVALID_INDEX);
1059 --- elfutils-0.127/libelf/gelf_update_dyn.c.robustify
1060 +++ elfutils-0.127/libelf/gelf_update_dyn.c
1061 @@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
1065 - if (unlikely (ndx < 0))
1067 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1071 if (unlikely (data_scn->d.d_type != ELF_T_DYN))
1073 /* The type of the data better should match. */
1074 @@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
1077 /* Check whether we have to resize the data buffer. */
1078 - if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
1079 + if (INVALID_NDX (ndx, Elf32_Dyn)
1080 + || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
1082 __libelf_seterrno (ELF_E_INVALID_INDEX);
1084 @@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
1087 /* Check whether we have to resize the data buffer. */
1088 - if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
1089 + if (INVALID_NDX (ndx, Elf64_Dyn)
1090 + || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
1092 __libelf_seterrno (ELF_E_INVALID_INDEX);
1094 --- elfutils-0.127/libelf/gelf_update_rela.c.robustify
1095 +++ elfutils-0.127/libelf/gelf_update_rela.c
1096 @@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx
1100 - if (unlikely (ndx < 0))
1102 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1106 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
1108 /* The type of the data better should match. */
1109 @@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
1112 /* Check whether we have to resize the data buffer. */
1113 - if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
1114 + if (INVALID_NDX (ndx, Elf32_Rela)
1115 + || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
1117 __libelf_seterrno (ELF_E_INVALID_INDEX);
1119 @@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
1122 /* Check whether we have to resize the data buffer. */
1123 - if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
1124 + if (INVALID_NDX (ndx, Elf64_Rela)
1125 + || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
1127 __libelf_seterrno (ELF_E_INVALID_INDEX);
1129 --- elfutils-0.127/libelf/gelf_getsymshndx.c.robustify
1130 +++ elfutils-0.127/libelf/gelf_getsymshndx.c
1131 @@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, nd
1132 section index table. */
1133 if (likely (shndxdata_scn != NULL))
1135 - if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
1136 + if (INVALID_NDX (ndx, Elf32_Word)
1137 + || unlikely ((ndx + 1) * sizeof (Elf32_Word)
1138 + > shndxdata_scn->d.d_size))
1140 __libelf_seterrno (ELF_E_INVALID_INDEX);
1142 @@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
1143 table entries has to be adopted. The user better has provided
1144 a buffer where we can store the information. While copying the
1145 data we are converting the format. */
1146 - if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
1147 + if (INVALID_NDX (ndx, Elf32_Sym)
1148 + || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
1150 __libelf_seterrno (ELF_E_INVALID_INDEX);
1152 @@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
1154 /* The data is already in the correct form. Just make sure the
1156 - if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
1157 + if (INVALID_NDX (ndx, GElf_Sym)
1158 + || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
1160 __libelf_seterrno (ELF_E_INVALID_INDEX);
1162 --- elfutils-0.127/libelf/elf32_newphdr.c.robustify
1163 +++ elfutils-0.127/libelf/elf32_newphdr.c
1164 @@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
1165 else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
1166 || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
1168 + if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
1174 /* Allocate a new program header with the appropriate number of
1176 result = (ElfW2(LIBELFBITS,Phdr) *)
1177 --- elfutils-0.127/libelf/gelf_update_sym.c.robustify
1178 +++ elfutils-0.127/libelf/gelf_update_sym.c
1179 @@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
1183 - if (unlikely (ndx < 0))
1185 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1189 if (unlikely (data_scn->d.d_type != ELF_T_SYM))
1191 /* The type of the data better should match. */
1192 @@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
1195 /* Check whether we have to resize the data buffer. */
1196 - if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
1197 + if (INVALID_NDX (ndx, Elf32_Sym)
1198 + || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
1200 __libelf_seterrno (ELF_E_INVALID_INDEX);
1202 @@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
1205 /* Check whether we have to resize the data buffer. */
1206 - if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
1207 + if (INVALID_NDX (ndx, Elf64_Sym)
1208 + || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
1210 __libelf_seterrno (ELF_E_INVALID_INDEX);
1212 --- elfutils-0.127/libelf/gelf_getsyminfo.c.robustify
1213 +++ elfutils-0.127/libelf/gelf_getsyminfo.c
1214 @@ -84,7 +84,8 @@ gelf_getsyminfo (data, ndx, dst)
1216 /* The data is already in the correct form. Just make sure the
1218 - if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
1219 + if (INVALID_NDX (ndx, GElf_Syminfo)
1220 + || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
1222 __libelf_seterrno (ELF_E_INVALID_INDEX);
1224 --- elfutils-0.127/libelf/gelf_getlib.c.robustify
1225 +++ elfutils-0.127/libelf/gelf_getlib.c
1226 @@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
1227 /* The data is already in the correct form. Just make sure the
1229 GElf_Lib *result = NULL;
1230 - if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
1231 + if (INVALID_NDX (ndx, GElf_Lib)
1232 + || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
1233 __libelf_seterrno (ELF_E_INVALID_INDEX);
1236 --- elfutils-0.127/libelf/elf32_updatefile.c.robustify
1237 +++ elfutils-0.127/libelf/elf32_updatefile.c
1238 @@ -201,6 +201,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
1239 /* Write all the sections. Well, only those which are modified. */
1242 + if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
1245 Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
1246 Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
1247 char *const shdr_start = ((char *) elf->map_address + elf->start_offset
1248 @@ -571,6 +574,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
1249 /* Write all the sections. Well, only those which are modified. */
1252 + if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
1253 + + sizeof (ElfW2(LIBELFBITS,Shdr)))))
1256 off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
1258 xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
1259 --- elfutils-0.127/libelf/gelf_getversym.c.robustify
1260 +++ elfutils-0.127/libelf/gelf_getversym.c
1261 @@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
1263 /* The data is already in the correct form. Just make sure the
1265 - if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
1266 + if (INVALID_NDX (ndx, GElf_Versym)
1267 + || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
1269 __libelf_seterrno (ELF_E_INVALID_INDEX);
1271 --- elfutils-0.127/libelf/elf_getarsym.c.robustify
1272 +++ elfutils-0.127/libelf/elf_getarsym.c
1273 @@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
1274 size_t index_size = atol (tmpbuf);
1276 if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
1277 +#if SIZE_MAX <= 4294967295U
1278 + || n >= SIZE_MAX / sizeof (Elf_Arsym)
1280 || n * sizeof (uint32_t) > index_size)
1282 /* This index table cannot be right since it does not fit into
1283 --- elfutils-0.127/libelf/gelf_getrel.c.robustify
1284 +++ elfutils-0.127/libelf/gelf_getrel.c
1285 @@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
1286 if (data_scn == NULL)
1289 - if (unlikely (ndx < 0))
1291 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1295 if (unlikely (data_scn->d.d_type != ELF_T_REL))
1297 __libelf_seterrno (ELF_E_INVALID_HANDLE);
1298 @@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
1299 if (scn->elf->class == ELFCLASS32)
1301 /* We have to convert the data. */
1302 - if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
1303 + if (INVALID_NDX (ndx, Elf32_Rel)
1304 + || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
1306 __libelf_seterrno (ELF_E_INVALID_INDEX);
1308 @@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
1310 /* Simply copy the data after we made sure we are actually getting
1312 - if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
1313 + if (INVALID_NDX (ndx, Elf64_Rel)
1314 + || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
1316 __libelf_seterrno (ELF_E_INVALID_INDEX);
1318 --- elfutils-0.127/libelf/gelf_update_versym.c.robustify
1319 +++ elfutils-0.127/libelf/gelf_update_versym.c
1320 @@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
1321 assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
1323 /* Check whether we have to resize the data buffer. */
1324 - if (unlikely (ndx < 0)
1325 + if (INVALID_NDX (ndx, GElf_Versym)
1326 || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
1328 __libelf_seterrno (ELF_E_INVALID_INDEX);
1329 --- elfutils-0.127/libelf/gelf_getdyn.c.robustify
1330 +++ elfutils-0.127/libelf/gelf_getdyn.c
1331 @@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
1332 table entries has to be adopted. The user better has provided
1333 a buffer where we can store the information. While copying the
1334 data we are converting the format. */
1335 - if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
1336 + if (INVALID_NDX (ndx, Elf32_Dyn)
1337 + || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
1339 __libelf_seterrno (ELF_E_INVALID_INDEX);
1341 @@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
1343 /* The data is already in the correct form. Just make sure the
1345 - if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
1346 + if (INVALID_NDX (ndx, GElf_Dyn)
1347 + || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
1349 __libelf_seterrno (ELF_E_INVALID_INDEX);
1351 --- elfutils-0.127/libelf/gelf_update_syminfo.c.robustify
1352 +++ elfutils-0.127/libelf/gelf_update_syminfo.c
1353 @@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
1357 - if (unlikely (ndx < 0))
1359 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1363 if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
1365 /* The type of the data better should match. */
1366 @@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
1367 rwlock_wrlock (scn->elf->lock);
1369 /* Check whether we have to resize the data buffer. */
1370 - if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
1371 + if (INVALID_NDX (ndx, GElf_Syminfo)
1372 + || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
1374 __libelf_seterrno (ELF_E_INVALID_INDEX);
1376 --- elfutils-0.127/libelf/gelf_update_rel.c.robustify
1377 +++ elfutils-0.127/libelf/gelf_update_rel.c
1378 @@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
1382 - if (unlikely (ndx < 0))
1384 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1388 if (unlikely (data_scn->d.d_type != ELF_T_REL))
1390 /* The type of the data better should match. */
1391 @@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
1394 /* Check whether we have to resize the data buffer. */
1395 - if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
1396 + if (INVALID_NDX (ndx, Elf32_Rel)
1397 + || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
1399 __libelf_seterrno (ELF_E_INVALID_INDEX);
1401 @@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
1404 /* Check whether we have to resize the data buffer. */
1405 - if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
1406 + if (INVALID_NDX (ndx, Elf64_Rel)
1407 + || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
1409 __libelf_seterrno (ELF_E_INVALID_INDEX);
1411 --- elfutils-0.127/libelf/elf_newscn.c.robustify
1412 +++ elfutils-0.127/libelf/elf_newscn.c
1413 @@ -104,10 +104,18 @@ elf_newscn (elf)
1416 /* We must allocate a new element. */
1417 - Elf_ScnList *newp;
1418 + Elf_ScnList *newp = NULL;
1420 assert (elf->state.elf.scnincr > 0);
1423 +#if SIZE_MAX <= 4294967295U
1424 + likely (elf->state.elf.scnincr
1425 + < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
1430 newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
1431 + ((elf->state.elf.scnincr *= 2)
1432 * sizeof (Elf_Scn)), 1);
1433 --- elfutils-0.127/libelf/gelf_update_lib.c.robustify
1434 +++ elfutils-0.127/libelf/gelf_update_lib.c
1435 @@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
1439 - if (unlikely (ndx < 0))
1441 - __libelf_seterrno (ELF_E_INVALID_INDEX);
1445 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
1446 if (unlikely (data_scn->d.d_type != ELF_T_LIB))
1448 @@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
1450 /* Check whether we have to resize the data buffer. */
1452 - if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
1453 + if (INVALID_NDX (ndx, Elf64_Lib)
1454 + || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
1455 __libelf_seterrno (ELF_E_INVALID_INDEX);
1458 --- elfutils-0.127/libelf/elf_getshstrndx.c.robustify
1459 +++ elfutils-0.127/libelf/elf_getshstrndx.c
1460 @@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
1461 if (elf->map_address != NULL
1462 && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
1464 - || (((size_t) ((char *) elf->map_address + offset))
1465 + || (((size_t) ((char *) elf->map_address
1466 + + elf->start_offset + offset))
1467 & (__alignof__ (Elf32_Shdr) - 1)) == 0))
1469 + /* First see whether the information in the ELF header is
1470 + valid and it does not ask for too much. */
1471 + if (unlikely (offset + sizeof (Elf32_Shdr)
1472 + > elf->maximum_size))
1474 + /* Something is wrong. */
1475 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
1480 /* We can directly access the memory. */
1481 - num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
1482 + num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
1483 + + offset))->sh_link;
1487 /* We avoid reading in all the section headers. Just read
1488 @@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
1489 if (elf->map_address != NULL
1490 && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
1492 - || (((size_t) ((char *) elf->map_address + offset))
1493 + || (((size_t) ((char *) elf->map_address
1494 + + elf->start_offset + offset))
1495 & (__alignof__ (Elf64_Shdr) - 1)) == 0))
1497 + /* First see whether the information in the ELF header is
1498 + valid and it does not ask for too much. */
1499 + if (unlikely (offset + sizeof (Elf64_Shdr)
1500 + > elf->maximum_size))
1502 + /* Something is wrong. */
1503 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
1508 /* We can directly access the memory. */
1509 - num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
1510 + num = ((Elf64_Shdr *) (elf->map_address
1511 + + elf->start_offset + offset))->sh_link;
1515 /* We avoid reading in all the section headers. Just read
1516 --- elfutils-0.127/libelf/elf32_getshdr.c.robustify
1517 +++ elfutils-0.127/libelf/elf32_getshdr.c
1518 @@ -101,7 +101,8 @@ elfw2(LIBELFBITS,getshdr) (scn)
1522 - if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
1523 + if (INTUSE (elf_getshnum) (elf, &shnum) != 0
1524 + || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
1526 size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
1528 @@ -118,6 +119,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
1530 if (elf->map_address != NULL)
1532 + /* First see whether the information in the ELF header is
1533 + valid and it does not ask for too much. */
1534 + if (unlikely (ehdr->e_shoff >= elf->maximum_size)
1535 + || unlikely (ehdr->e_shoff + size > elf->maximum_size))
1537 + /* Something is wrong. */
1538 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
1539 + goto free_and_out;
1542 ElfW2(LIBELFBITS,Shdr) *notcvt;
1544 /* All the data is already mapped. If we could use it
1545 --- elfutils-0.127/libelf/elf32_getphdr.c.robustify
1546 +++ elfutils-0.127/libelf/elf32_getphdr.c
1547 @@ -116,6 +116,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
1549 if (elf->map_address != NULL)
1551 + /* First see whether the information in the ELF header is
1552 + valid and it does not ask for too much. */
1553 + if (unlikely (ehdr->e_phoff >= elf->maximum_size)
1554 + || unlikely (ehdr->e_phoff + size > elf->maximum_size))
1556 + /* Something is wrong. */
1557 + __libelf_seterrno (ELF_E_INVALID_PHDR);
1561 /* All the data is already mapped. Use it. */
1562 void *file_phdr = ((char *) elf->map_address
1563 + elf->start_offset + ehdr->e_phoff);