]> git.pld-linux.org Git - packages/elfutils.git/blame - elfutils-robustify.patch
- disable run-elflint-self.sh on sparcs and alpha
[packages/elfutils.git] / elfutils-robustify.patch
CommitLineData
0dcd74c7
JR
1src/
22005-06-09 Roland McGrath <roland@redhat.com>
3
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.
7
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.
11
122005-05-17 Jakub Jelinek <jakub@redhat.com>
13
14libelf/
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
19 bounds.
e78e431a 20 * elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)):
0dcd74c7 21 Check if section header table fits into object's bounds.
49fd4b14
JB
22 * elf_begin.c (get_shnum): Ensure section headers fits into
23 object's bounds.
0dcd74c7
JR
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.
27
28src/
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.
32
332005-05-14 Jakub Jelinek <jakub@redhat.com>
34
35libelf/
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.
62src/
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.
67
a8ca41f5
JB
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;
49fd4b14 72
a8ca41f5
JB
73 GElf_Sym sym_mem;
74+ GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
49fd4b14 75+
a8ca41f5
JB
76 printf ((grpref[0] & GRP_COMDAT)
77 ? ngettext ("\
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),
81 elf_ndxscn (scn),
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)
85+ (sym == NULL ? NULL
86+ : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
87 ?: gettext ("<INVALID SYMBOL>"),
88 data->d_size / sizeof (Elf32_Word) - 1);
49fd4b14 89
a8ca41f5
JB
90@@ -1122,7 +1124,8 @@ static void
91 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
92 {
93 int class = gelf_getclass (ebl->elf);
94- GElf_Shdr glink;
95+ GElf_Shdr glink_mem;
96+ GElf_Shdr *glink;
97 Elf_Data *data;
98 size_t cnt;
99 size_t shstrndx;
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"));
49fd4b14 103
a8ca41f5
JB
104+ glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
105+ if (glink == NULL)
106+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
107+ elf_ndxscn (scn));
49fd4b14 108+
a8ca41f5
JB
109 printf (ngettext ("\
110 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
111 "\
112@@ -1146,9 +1154,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
113 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
114 shdr->sh_offset,
115 (int) shdr->sh_link,
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);
e78e431a 121
a8ca41f5
JB
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"));
126
127+ GElf_Shdr glink_mem;
128+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
129+ &glink_mem);
130+ if (glink == NULL)
131+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
132+ elf_ndxscn (scn));
49fd4b14 133+
a8ca41f5
JB
134 /* Now we can compute the number of entries in the section. */
135 unsigned int nsyms = data->d_size / (class == ELFCLASS32
136 ? sizeof (Elf32_Sym)
137@@ -1676,15 +1689,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
138 nsyms),
139 (unsigned int) elf_ndxscn (scn),
140 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
141- GElf_Shdr glink;
142 printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
143 " %lu local symbols String table: [%2u] '%s'\n",
144 shdr->sh_info),
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));
151
152 fputs_unlocked (class == ELFCLASS32
153 ? gettext ("\
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"));
157
158- GElf_Shdr glink;
159+ GElf_Shdr glink_mem;
160+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
161+ &glink_mem);
162+ if (glink == NULL)
163+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
164+ elf_ndxscn (scn));
49fd4b14 165+
a8ca41f5
JB
166 printf (ngettext ("\
167 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
168 "\
169@@ -1931,9 +1947,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
170 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
171 shdr->sh_offset,
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));
49fd4b14 177
a8ca41f5
JB
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"));
183
184+ GElf_Shdr glink_mem;
185+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
186+ &glink_mem);
187+ if (glink == NULL)
188+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
189+ elf_ndxscn (scn));
190+
191 int class = gelf_getclass (ebl->elf);
192- GElf_Shdr glink;
193 printf (ngettext ("\
194 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
195 "\
196@@ -1999,9 +2019,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
197 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
198 shdr->sh_offset,
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));
49fd4b14 204
a8ca41f5
JB
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
208 filename = NULL;
209 }
49fd4b14 210
a8ca41f5
JB
211+ GElf_Shdr glink_mem;
212+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
213+ &glink_mem);
214+ if (glink == NULL)
215+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
216+ elf_ndxscn (scn));
217+
218 /* Print the header. */
219- GElf_Shdr glink;
220 printf (ngettext ("\
221 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
222 "\
223@@ -2276,9 +2300,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
224 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
225 shdr->sh_offset,
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));
49fd4b14 231
a8ca41f5
JB
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]];
49fd4b14 237
a8ca41f5
JB
238- GElf_Shdr glink;
239+ GElf_Shdr glink_mem;
240+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
241+ shdr->sh_link),
242+ &glink_mem);
243+ if (glink == NULL)
49fd4b14 244+ {
a8ca41f5
JB
245+ error (0, 0, gettext ("invalid sh_link value in section %Zu"),
246+ elf_ndxscn (scn));
247+ return;
49fd4b14 248+ }
49fd4b14 249+
a8ca41f5
JB
250 printf (ngettext ("\
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",
252 "\
253@@ -2343,9 +2375,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
254 shdr->sh_addr,
255 shdr->sh_offset,
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));
49fd4b14 261
a8ca41f5
JB
262 if (extrastr != NULL)
263 fputs (extrastr, stdout);
264@@ -3655,6 +3685,16 @@ print_debug_aranges_section (Ebl *ebl __
265 return;
266 }
267
268+ GElf_Shdr glink_mem;
269+ GElf_Shdr *glink;
270+ glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
271+ if (glink == NULL)
272+ {
273+ error (0, 0, gettext ("invalid sh_link value in section %Zu"),
274+ elf_ndxscn (scn));
275+ return;
276+ }
49fd4b14 277+
a8ca41f5
JB
278 printf (ngettext ("\
279 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
280 "\
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;
49fd4b14 288
a8ca41f5
JB
289 /* Create the full name of the file. */
290 if (prefix != NULL)
291@@ -542,6 +543,11 @@ handle_elf (int fd, Elf *elf, const char
292 goto fail_close;
293 }
49fd4b14 294
a8ca41f5
JB
295+ if (shstrndx >= shnum)
296+ goto illformed;
49fd4b14 297+
a8ca41f5 298+#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
49fd4b14 299+
a8ca41f5
JB
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
0dcd74c7 304 {
a8ca41f5
JB
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);
49fd4b14 309
a8ca41f5 310 shdr_info[cnt].scn = scn;
0dcd74c7 311
a8ca41f5
JB
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)
0dcd74c7 315 {
a8ca41f5
JB
316+ illformed:
317 error (0, 0, gettext ("illformed file '%s'"), fname);
318 goto fail_close;
319 }
320@@ -585,6 +592,8 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7 321
a8ca41f5
JB
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)
325+ goto illformed;
0dcd74c7 326
a8ca41f5
JB
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))
0dcd74c7 332 {
a8ca41f5
JB
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;
336 }
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
339 for (inner = 1;
340 inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
341 ++inner)
342+ {
343+ if (grpref[inner] < shnum)
344 shdr_info[grpref[inner]].group_idx = cnt;
345+ else
346+ goto illformed;
347+ }
0dcd74c7 348
a8ca41f5
JB
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
352 }
353 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
49fd4b14 354 {
a8ca41f5
JB
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;
358 }
0dcd74c7 359
a8ca41f5
JB
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)
49fd4b14 363 {
a8ca41f5
JB
364- assert (shdr_info[cnt].group_idx != 0);
365+ elf_assert (shdr_info[cnt].group_idx != 0);
0dcd74c7 366
a8ca41f5
JB
367 if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
368 {
369@@ -708,11 +722,15 @@ handle_elf (int fd, Elf *elf, const char
370 {
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)
377+ {
378+ if (shdr_info[cnt].shdr.sh_info >= shnum)
379+ goto illformed;
380+ else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
381 shdr_info[cnt].idx = 1;
382 }
383+ }
0dcd74c7 384
a8ca41f5
JB
385 if (shdr_info[cnt].idx == 1)
386 {
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)
390 {
391- assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
392+ elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
49fd4b14 393
a8ca41f5
JB
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)
398 scnidx = xndx;
49fd4b14 399
a8ca41f5
JB
400+ if (scnidx >= shnum)
401+ goto illformed;
402+
403 if (shdr_info[scnidx].idx == 0)
404 {
405 /* Mark this section as used. */
406@@ -808,12 +829,16 @@ handle_elf (int fd, Elf *elf, const char
407 }
49fd4b14 408
a8ca41f5
JB
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))
413+ {
414+ if (shdr_info[cnt].shdr.sh_info >= shnum)
415+ goto illformed;
416+ else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
417 {
418 shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
419 changes |= shdr_info[cnt].shdr.sh_info < cnt;
420 }
421+ }
49fd4b14 422
a8ca41f5
JB
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"),
427 elf_errmsg (-1));
49fd4b14 428
a8ca41f5
JB
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);
0dcd74c7 431
a8ca41f5
JB
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"),
437 elf_errmsg (-1));
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);
0dcd74c7 440
a8ca41f5
JB
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
444 }
0dcd74c7 445
a8ca41f5
JB
446 /* Index of the section header table in the shdr_info array. */
447- size_t shdridx = cnt;
448+ shdridx = cnt;
0dcd74c7 449
a8ca41f5
JB
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"),
455 elf_errmsg (-1));
456- assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
457+ elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
0dcd74c7 458
a8ca41f5
JB
459 /* Finalize the string table and fill in the correct indices in the
460 section headers. */
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,
463 NULL);
0dcd74c7 464
a8ca41f5
JB
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);
468 }
0dcd74c7 469
a8ca41f5
JB
470 if (shdr_info[cnt].version_idx != 0)
471 {
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
476 information, too. */
477 versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
478 NULL);
49fd4b14 479
a8ca41f5
JB
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);
483 }
0dcd74c7 484
a8ca41f5
JB
485@@ -1163,7 +1188,7 @@ handle_elf (int fd, Elf *elf, const char
486 sec = shdr_info[sym->st_shndx].idx;
487 else
488 {
489- assert (shndxdata != NULL);
490+ elf_assert (shndxdata != NULL);
0dcd74c7 491
a8ca41f5
JB
492 sec = shdr_info[xshndx].idx;
493 }
494@@ -1184,7 +1209,7 @@ handle_elf (int fd, Elf *elf, const char
495 nxshndx = sec;
496 }
0dcd74c7 497
a8ca41f5
JB
498- assert (sec < SHN_LORESERVE || shndxdata != NULL);
499+ elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
49fd4b14 500
a8ca41f5
JB
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
504 else
505 /* This is a section symbol for a section which has
506 been removed. */
507- assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
508+ elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
509 }
49fd4b14 510
a8ca41f5
JB
511 if (destidx != inner)
512@@ -1371,11 +1396,11 @@ handle_elf (int fd, Elf *elf, const char
513 {
514 GElf_Sym sym_mem;
515 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
516- assert (sym != NULL);
517+ elf_assert (sym != NULL);
0dcd74c7 518
a8ca41f5
JB
519 const char *name = elf_strptr (elf, strshndx,
520 sym->st_name);
521- assert (name != NULL);
522+ elf_assert (name != NULL);
523 size_t hidx = elf_hash (name) % nbucket;
0dcd74c7 524
a8ca41f5
JB
525 if (bucket[hidx] == 0)
526@@ -1394,7 +1419,7 @@ handle_elf (int fd, Elf *elf, const char
527 else
528 {
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));
533
534 Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
535@@ -1428,11 +1453,11 @@ handle_elf (int fd, Elf *elf, const char
536 {
537 GElf_Sym sym_mem;
538 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
539- assert (sym != NULL);
540+ elf_assert (sym != NULL);
541
542 const char *name = elf_strptr (elf, strshndx,
543 sym->st_name);
544- assert (name != NULL);
545+ elf_assert (name != NULL);
546 size_t hidx = elf_hash (name) % nbucket;
547
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. */
553 static int *scnref;
554
555+/* Number of sections. */
556+static unsigned int shnum;
557+
558
559 int
560 main (int argc, char *argv[])
561@@ -312,10 +315,19 @@ section_name (Ebl *ebl, int idx)
562 {
563 GElf_Shdr shdr_mem;
564 GElf_Shdr *shdr;
0dcd74c7
JR
565+ const char *ret;
566+
567+ if ((unsigned int) idx > shnum)
568+ return "<invalid>";
569
570 shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
49fd4b14
JB
571+ if (shdr == NULL)
572+ return "<invalid>";
0dcd74c7
JR
573
574- return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
575+ ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
576+ if (ret == NULL)
577+ return "<invalid>";
578+ return ret;
579 }
580
581
98f2540e 582@@ -337,10 +349,6 @@ static const int valid_e_machine[] =
0dcd74c7
JR
583 (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
584
585
586-/* Number of sections. */
587-static unsigned int shnum;
588-
589-
590 static void
591 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
592 {
98f2540e 593@@ -603,7 +611,8 @@ section [%2d] '%s': symbol table cannot
49fd4b14
JB
594 }
595 }
0dcd74c7
JR
596
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)
600 ERROR (gettext ("\
601 section [%2zu] '%s': entry size is does not match ElfXX_Sym\n"),
602 cnt, section_name (ebl, cnt));
98f2540e 603@@ -641,7 +650,7 @@ section [%2d] '%s': XINDEX for zeroth en
0dcd74c7
JR
604 xndxscnidx, section_name (ebl, xndxscnidx));
605 }
606
607- for (cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
608+ for (cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
609 {
610 sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
611 if (sym == NULL)
98f2540e 612@@ -659,7 +668,8 @@ section [%2d] '%s': symbol %zu: invalid
0dcd74c7
JR
613 else
614 {
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);
619 }
620
621 if (sym->st_shndx == SHN_XINDEX)
a8ca41f5 622@@ -981,9 +991,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
acd2260d
JB
623 {
624 GElf_Shdr rcshdr_mem;
0dcd74c7 625 const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
acd2260d 626- assert (rcshdr != NULL);
0dcd74c7
JR
627
628- if (rcshdr->sh_type == SHT_DYNAMIC)
acd2260d
JB
629+ if (rcshdr == NULL)
630+ break;
631+
0dcd74c7
JR
632+ if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
633 {
634 /* Found the dynamic section. Look through it. */
635 Elf_Data *d = elf_getdata (scn, NULL);
a8ca41f5 636@@ -993,7 +1005,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
0dcd74c7
JR
637 {
638 GElf_Dyn dyn_mem;
639 GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
640- assert (dyn != NULL);
641+
642+ if (dyn == NULL)
643+ break;
644
645 if (dyn->d_tag == DT_RELCOUNT)
646 {
a8ca41f5 647@@ -1007,7 +1021,9 @@ section [%2d] '%s': DT_RELCOUNT used for
acd2260d
JB
648 /* Does the number specified number of relative
649 relocations exceed the total number of
650 relocations? */
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))
655 ERROR (gettext ("\
0dcd74c7 656 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
acd2260d 657 idx, section_name (ebl, idx),
a8ca41f5 658@@ -1167,7 +1183,8 @@ section [%2d] '%s': no relocations for m
0dcd74c7
JR
659 }
660 }
661
49fd4b14
JB
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);
0dcd74c7 664+ if (shdr->sh_entsize != sh_entsize)
49fd4b14
JB
665 ERROR (gettext (reltype == ELF_T_RELA ? "\
666 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
0dcd74c7 667 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
a8ca41f5 668@@ -1389,7 +1406,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
0dcd74c7 669 Elf_Data *symdata = elf_getdata (symscn, NULL);
49fd4b14 670 enum load_state state = state_undecided;
0dcd74c7 671
49fd4b14
JB
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)
675 {
676 GElf_Rela rela_mem;
677 GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
a8ca41f5 678@@ -1439,7 +1457,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
49fd4b14
JB
679 Elf_Data *symdata = elf_getdata (symscn, NULL);
680 enum load_state state = state_undecided;
0dcd74c7 681
49fd4b14
JB
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)
685 {
686 GElf_Rel rel_mem;
687 GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
a8ca41f5 688@@ -1543,7 +1562,8 @@ section [%2d] '%s': referenced as string
49fd4b14
JB
689 shdr->sh_link, section_name (ebl, shdr->sh_link),
690 idx, section_name (ebl, idx));
0dcd74c7 691
49fd4b14
JB
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)
695 ERROR (gettext ("\
696 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
697 idx, section_name (ebl, idx));
a8ca41f5 698@@ -1553,7 +1573,7 @@ section [%2d] '%s': section entry size d
49fd4b14 699 idx, section_name (ebl, idx));
0dcd74c7 700
49fd4b14
JB
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)
704 {
705 GElf_Dyn dyn_mem;
706 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
a8ca41f5 707@@ -1834,6 +1854,8 @@ section [%2d] '%s': entry size does not
49fd4b14 708 idx, section_name (ebl, idx));
0dcd74c7 709
49fd4b14
JB
710 if (symshdr != NULL
711+ && shdr->sh_entsize
712+ && symshdr->sh_entsize
713 && (shdr->sh_size / shdr->sh_entsize
714 < symshdr->sh_size / symshdr->sh_entsize))
715 ERROR (gettext ("\
a8ca41f5 716@@ -1860,6 +1882,12 @@ section [%2d] '%s': extended section ind
49fd4b14
JB
717 }
718
719 Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
720+ if (data == NULL)
721+ {
722+ ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
723+ idx, section_name (ebl, idx));
724+ return;
725+ }
726
727 if (*((Elf32_Word *) data->d_buf) != 0)
728 ERROR (gettext ("symbol 0 should have zero extended section index\n"));
a8ca41f5 729@@ -1902,7 +1930,7 @@ section [%2d] '%s': hash table section i
acd2260d
JB
730
731 size_t maxidx = nchain;
0dcd74c7 732
49fd4b14 733- if (symshdr != NULL)
acd2260d 734+ if (symshdr != NULL && symshdr->sh_entsize != 0)
0dcd74c7
JR
735 {
736 size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
acd2260d 737
a8ca41f5 738@@ -1913,18 +1941,28 @@ section [%2d] '%s': hash table section i
acd2260d
JB
739 maxidx = symsize;
740 }
741
742+ Elf32_Word *buf = (Elf32_Word *) data->d_buf;
743+ Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
744 size_t cnt;
745 for (cnt = 2; cnt < 2 + nbucket; ++cnt)
746- if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
acd2260d
JB
747+ {
748+ if (buf + cnt >= end)
749+ break;
750+ else if (buf[cnt] >= maxidx)
a8ca41f5 751 ERROR (gettext ("\
0dcd74c7 752 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
a8ca41f5 753 idx, section_name (ebl, idx), cnt - 2);
acd2260d
JB
754+ }
755
756 for (; cnt < 2 + nbucket + nchain; ++cnt)
757- if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
acd2260d
JB
758+ {
759+ if (buf + cnt >= end)
760+ break;
761+ else if (buf[cnt] >= maxidx)
a8ca41f5 762 ERROR (gettext ("\
49fd4b14 763 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
a8ca41f5 764 idx, section_name (ebl, idx), cnt - 2 - nbucket);
acd2260d
JB
765+ }
766 }
767
768
a8ca41f5 769@@ -1954,18 +1992,28 @@ section [%2d] '%s': hash table section i
acd2260d
JB
770 maxidx = symsize;
771 }
772
773+ Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
774+ Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
775 size_t cnt;
776 for (cnt = 2; cnt < 2 + nbucket; ++cnt)
777- if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
acd2260d
JB
778+ {
779+ if (buf + cnt >= end)
780+ break;
781+ else if (buf[cnt] >= maxidx)
a8ca41f5 782 ERROR (gettext ("\
acd2260d 783 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
a8ca41f5 784 idx, section_name (ebl, idx), cnt - 2);
acd2260d
JB
785+ }
786
787 for (; cnt < 2 + nbucket + nchain; ++cnt)
788- if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
acd2260d
JB
789+ {
790+ if (buf + cnt >= end)
791+ break;
792+ else if (buf[cnt] >= maxidx)
a8ca41f5 793 ERROR (gettext ("\
acd2260d
JB
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);
797+ }
798 }
799
800
a8ca41f5 801@@ -1990,7 +2038,7 @@ section [%2d] '%s': bitmask size not pow
acd2260d
JB
802 if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
803 {
804 ERROR (gettext ("\
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)));
809 return;
a8ca41f5 810@@ -2644,8 +2692,9 @@ section [%2d] '%s' refers in sh_link to
49fd4b14
JB
811
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))
819 ERROR (gettext ("\
820 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
821 idx, section_name (ebl, idx),
a8ca41f5 822@@ -3574,6 +3623,8 @@ phdr[%d]: no note entries defined for th
49fd4b14
JB
823 return;
824
825 char *notemem = gelf_rawchunk (ebl->elf, phdr->p_offset, phdr->p_filesz);
826+ if (notemem == NULL)
827+ return;
828
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.
a8ca41f5
JB
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
49fd4b14 834
a8ca41f5
JB
835 if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
836 {
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. */
841 return 0;
49fd4b14 842
a8ca41f5 843@@ -203,7 +204,8 @@ get_shnum (void *map_address, unsigned c
49fd4b14 844
a8ca41f5
JB
845 if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
846 {
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. */
851 return 0;
49fd4b14 852
a8ca41f5
JB
853@@ -275,6 +277,15 @@ file_read_elf (int fildes, void *map_add
854 /* Could not determine the number of sections. */
855 return NULL;
49fd4b14 856
a8ca41f5
JB
857+ /* Check for too many sections. */
858+ if (e_ident[EI_CLASS] == ELFCLASS32)
859+ {
860+ if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
861+ return NULL;
862+ }
863+ else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
864+ return NULL;
49fd4b14 865+
a8ca41f5
JB
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
870 {
871 /* We can use the mmapped memory. */
872 elf->state.elf32.ehdr = ehdr;
873+
874+ if (unlikely (ehdr->e_shoff >= maxsize)
875+ || unlikely (ehdr->e_shoff
876+ + scncnt * sizeof (Elf32_Shdr) > maxsize))
877+ {
878+ free_and_out:
879+ free (elf);
880+ __libelf_seterrno (ELF_E_INVALID_FILE);
881+ return NULL;
882+ }
883 elf->state.elf32.shdr
884 = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
885+
886 if (ehdr->e_phnum > 0)
887+ {
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
892+ + ehdr->e_phnum
893+ * sizeof (Elf32_Phdr) > maxsize))
894+ goto free_and_out;
895 elf->state.elf32.phdr
896 = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
897+ }
49fd4b14 898
a8ca41f5
JB
899 for (size_t cnt = 0; cnt < scncnt; ++cnt)
900 {
901@@ -383,13 +412,26 @@ file_read_elf (int fildes, void *map_add
902 {
903 /* We can use the mmapped memory. */
904 elf->state.elf64.ehdr = ehdr;
49fd4b14 905+
a8ca41f5
JB
906+ if (unlikely (ehdr->e_shoff >= maxsize)
907+ || unlikely (ehdr->e_shoff
908+ + scncnt * sizeof (Elf32_Shdr) > maxsize))
909+ goto free_and_out;
910 elf->state.elf64.shdr
911 = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
912+
913 if (ehdr->e_phnum > 0)
914+ {
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
919+ + ehdr->e_phnum
920+ * sizeof (Elf32_Phdr) > maxsize))
921+ goto free_and_out;
922 elf->state.elf64.phdr
923 = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
924+ }
49fd4b14 925
a8ca41f5
JB
926 for (size_t cnt = 0; cnt < scncnt; ++cnt)
927 {
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
931 } \
932 } while (0)
49fd4b14 933
a8ca41f5
JB
934+/* Convenience macro. Assumes int NDX and TYPE with size at least
935+ 2 bytes. */
936+#if SIZE_MAX > 4294967295U
937+# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
938+#else
939+# define INVALID_NDX(ndx, type) \
940+ unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
941+#endif
49fd4b14 942+
a8ca41f5
JB
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));
49fd4b14 948
a8ca41f5
JB
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))
0dcd74c7 953 {
a8ca41f5
JB
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))
964 {
965 __libelf_seterrno (ELF_E_INVALID_INDEX);
966 goto out;
967@@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
0dcd74c7 968
a8ca41f5
JB
969 /* The data is already in the correct form. Just make sure the
970 index is OK. */
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))
974 {
975 __libelf_seterrno (ELF_E_INVALID_INDEX);
976 goto out;
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)
981 return NULL;
0dcd74c7 982
a8ca41f5
JB
983- if (unlikely (ndx < 0))
984- {
985- __libelf_seterrno (ELF_E_INVALID_INDEX);
986- return NULL;
987- }
988-
989 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
990 {
991 __libelf_seterrno (ELF_E_INVALID_HANDLE);
992@@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
993 if (scn->elf->class == ELFCLASS32)
994 {
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))
0dcd74c7 999 {
a8ca41f5
JB
1000 __libelf_seterrno (ELF_E_INVALID_INDEX);
1001 result = NULL;
1002@@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
1003 {
1004 /* Simply copy the data after we made sure we are actually getting
1005 correct data. */
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))
1009 {
1010 __libelf_seterrno (ELF_E_INVALID_INDEX);
1011 result = NULL;
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)
0dcd74c7 1015
a8ca41f5
JB
1016 /* The data is already in the correct form. Just make sure the
1017 index is OK. */
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))
1021 {
1022 __libelf_seterrno (ELF_E_INVALID_INDEX);
1023 goto out;
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)
1028 return 0;
0dcd74c7 1029
a8ca41f5
JB
1030- if (unlikely (ndx < 0))
1031- {
1032- __libelf_seterrno (ELF_E_INVALID_INDEX);
1033- return 0;
1034- }
1035-
1036 if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
1037 {
1038 /* The type of the data better should match. */
1039@@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
0dcd74c7 1040 }
0dcd74c7 1041
a8ca41f5
JB
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))
0dcd74c7 1046 {
a8ca41f5
JB
1047 __libelf_seterrno (ELF_E_INVALID_INDEX);
1048 goto out;
1049@@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
1050 else
1051 {
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))
1056 {
1057 __libelf_seterrno (ELF_E_INVALID_INDEX);
1058 goto out;
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)
1062 if (data == NULL)
1063 return 0;
1064
1065- if (unlikely (ndx < 0))
1066- {
1067- __libelf_seterrno (ELF_E_INVALID_INDEX);
1068- return 0;
1069- }
1070-
1071 if (unlikely (data_scn->d.d_type != ELF_T_DYN))
1072 {
1073 /* The type of the data better should match. */
1074@@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
0dcd74c7
JR
1075 }
1076
a8ca41f5
JB
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))
0dcd74c7 1081 {
a8ca41f5
JB
1082 __libelf_seterrno (ELF_E_INVALID_INDEX);
1083 goto out;
1084@@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
1085 else
1086 {
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))
1091 {
1092 __libelf_seterrno (ELF_E_INVALID_INDEX);
1093 goto out;
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
1097 if (dst == NULL)
1098 return 0;
0dcd74c7 1099
a8ca41f5
JB
1100- if (unlikely (ndx < 0))
1101- {
1102- __libelf_seterrno (ELF_E_INVALID_INDEX);
1103- return 0;
1104- }
1105-
1106 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
1107 {
1108 /* The type of the data better should match. */
1109@@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
1110 }
0dcd74c7 1111
a8ca41f5
JB
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))
1116 {
1117 __libelf_seterrno (ELF_E_INVALID_INDEX);
1118 goto out;
1119@@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
1120 else
1121 {
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))
1126 {
1127 __libelf_seterrno (ELF_E_INVALID_INDEX);
1128 goto out;
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))
1134 {
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))
1139 {
1140 __libelf_seterrno (ELF_E_INVALID_INDEX);
1141 goto out;
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))
1149 {
1150 __libelf_seterrno (ELF_E_INVALID_INDEX);
1151 goto out;
1152@@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
0dcd74c7 1153
a8ca41f5
JB
1154 /* The data is already in the correct form. Just make sure the
1155 index is OK. */
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))
1159 {
1160 __libelf_seterrno (ELF_E_INVALID_INDEX);
1161 goto out;
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)
1167 {
1168+ if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
1169+ {
1170+ result = NULL;
1171+ goto out;
1172+ }
0dcd74c7 1173+
a8ca41f5
JB
1174 /* Allocate a new program header with the appropriate number of
1175 elements. */
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)
1180 if (data == NULL)
1181 return 0;
0dcd74c7 1182
a8ca41f5
JB
1183- if (unlikely (ndx < 0))
1184- {
1185- __libelf_seterrno (ELF_E_INVALID_INDEX);
1186- return 0;
1187- }
1188-
1189 if (unlikely (data_scn->d.d_type != ELF_T_SYM))
1190 {
1191 /* The type of the data better should match. */
1192@@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
1193 }
0dcd74c7 1194
a8ca41f5
JB
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))
1199 {
1200 __libelf_seterrno (ELF_E_INVALID_INDEX);
1201 goto out;
1202@@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
1203 else
1204 {
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))
1209 {
1210 __libelf_seterrno (ELF_E_INVALID_INDEX);
1211 goto out;
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)
0dcd74c7 1215
a8ca41f5
JB
1216 /* The data is already in the correct form. Just make sure the
1217 index is OK. */
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))
1221 {
1222 __libelf_seterrno (ELF_E_INVALID_INDEX);
1223 goto out;
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
1228 index is OK. */
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);
1234 else
1235 {
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. */
1240 if (shnum > 0)
1241 {
1242+ if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
1243+ return 1;
1244+
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. */
1250 if (shnum > 0)
1251 {
1252+ if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
1253+ + sizeof (ElfW2(LIBELFBITS,Shdr)))))
1254+ return 1;
1255+
1256 off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
1257 #if EV_NUM != 2
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)
0dcd74c7 1262
a8ca41f5
JB
1263 /* The data is already in the correct form. Just make sure the
1264 index is OK. */
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))
1268 {
1269 __libelf_seterrno (ELF_E_INVALID_INDEX);
1270 result = NULL;
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);
0dcd74c7 1275
a8ca41f5
JB
1276 if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
1277+#if SIZE_MAX <= 4294967295U
1278+ || n >= SIZE_MAX / sizeof (Elf_Arsym)
1279+#endif
1280 || n * sizeof (uint32_t) > index_size)
1281 {
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)
1287 return NULL;
0dcd74c7 1288
a8ca41f5
JB
1289- if (unlikely (ndx < 0))
1290- {
1291- __libelf_seterrno (ELF_E_INVALID_INDEX);
1292- return NULL;
1293- }
1294-
1295 if (unlikely (data_scn->d.d_type != ELF_T_REL))
1296 {
1297 __libelf_seterrno (ELF_E_INVALID_HANDLE);
1298@@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
1299 if (scn->elf->class == ELFCLASS32)
1300 {
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))
1305 {
1306 __libelf_seterrno (ELF_E_INVALID_INDEX);
1307 result = NULL;
1308@@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
1309 {
1310 /* Simply copy the data after we made sure we are actually getting
1311 correct data. */
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))
1315 {
1316 __libelf_seterrno (ELF_E_INVALID_INDEX);
1317 result = NULL;
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));
0dcd74c7 1322
a8ca41f5
JB
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))
1327 {
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))
1338 {
1339 __libelf_seterrno (ELF_E_INVALID_INDEX);
1340 goto out;
1341@@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
0dcd74c7 1342
a8ca41f5
JB
1343 /* The data is already in the correct form. Just make sure the
1344 index is OK. */
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))
1348 {
1349 __libelf_seterrno (ELF_E_INVALID_INDEX);
1350 goto out;
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)
1354 if (data == NULL)
1355 return 0;
0dcd74c7 1356
a8ca41f5
JB
1357- if (unlikely (ndx < 0))
1358- {
1359- __libelf_seterrno (ELF_E_INVALID_INDEX);
1360- return 0;
1361- }
1362-
1363 if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
1364 {
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);
0dcd74c7 1368
a8ca41f5
JB
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))
1373 {
1374 __libelf_seterrno (ELF_E_INVALID_INDEX);
1375 goto out;
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,
1379 if (dst == NULL)
1380 return 0;
0dcd74c7 1381
a8ca41f5
JB
1382- if (unlikely (ndx < 0))
1383- {
1384- __libelf_seterrno (ELF_E_INVALID_INDEX);
1385- return 0;
1386- }
1387-
1388 if (unlikely (data_scn->d.d_type != ELF_T_REL))
1389 {
1390 /* The type of the data better should match. */
1391@@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
1392 }
0dcd74c7 1393
a8ca41f5
JB
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))
1398 {
1399 __libelf_seterrno (ELF_E_INVALID_INDEX);
1400 goto out;
1401@@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
1402 else
1403 {
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))
1408 {
1409 __libelf_seterrno (ELF_E_INVALID_INDEX);
1410 goto out;
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)
1414 else
1415 {
1416 /* We must allocate a new element. */
1417- Elf_ScnList *newp;
1418+ Elf_ScnList *newp = NULL;
0dcd74c7 1419
a8ca41f5 1420 assert (elf->state.elf.scnincr > 0);
0dcd74c7 1421
a8ca41f5
JB
1422+ if (
1423+#if SIZE_MAX <= 4294967295U
1424+ likely (elf->state.elf.scnincr
1425+ < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
1426+#else
1427+ 1
1428+#endif
1429+ )
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)
1436 if (data == NULL)
1437 return 0;
0dcd74c7 1438
a8ca41f5
JB
1439- if (unlikely (ndx < 0))
1440- {
1441- __libelf_seterrno (ELF_E_INVALID_INDEX);
1442- return 0;
1443- }
1444-
1445 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
1446 if (unlikely (data_scn->d.d_type != ELF_T_LIB))
1447 {
1448@@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
1449
1450 /* Check whether we have to resize the data buffer. */
1451 int result = 0;
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);
1456 else
1457 {
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
1463 && (ALLOW_UNALIGNED
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))
1468+ {
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))
1473+ {
1474+ /* Something is wrong. */
1475+ __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
1476+ result = -1;
1477+ goto out;
1478+ }
1479+
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;
1484+ }
0dcd74c7
JR
1485 else
1486 {
a8ca41f5
JB
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
1491 && (ALLOW_UNALIGNED
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))
1496+ {
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))
1501+ {
1502+ /* Something is wrong. */
1503+ __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
1504+ result = -1;
1505+ goto out;
1506+ }
1507+
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;
1512+ }
1513 else
1514 {
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)
1519 goto out;
0dcd74c7 1520
a8ca41f5
JB
1521 size_t shnum;
1522- if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
1523+ if (INTUSE (elf_getshnum) (elf, &shnum) != 0
1524+ || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
1525 goto out;
1526 size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
0dcd74c7 1527
a8ca41f5 1528@@ -118,6 +119,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
0dcd74c7 1529
a8ca41f5
JB
1530 if (elf->map_address != NULL)
1531 {
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))
1536+ {
1537+ /* Something is wrong. */
1538+ __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
1539+ goto free_and_out;
1540+ }
1541+
1542 ElfW2(LIBELFBITS,Shdr) *notcvt;
1543
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)
1548
1549 if (elf->map_address != NULL)
1550 {
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))
1555+ {
1556+ /* Something is wrong. */
1557+ __libelf_seterrno (ELF_E_INVALID_PHDR);
1558+ goto out;
1559+ }
1560+
1561 /* All the data is already mapped. Use it. */
1562 void *file_phdr = ((char *) elf->map_address
1563 + elf->start_offset + ehdr->e_phoff);
This page took 0.398919 seconds and 4 git commands to generate.