]> git.pld-linux.org Git - packages/elfutils.git/blame - elfutils-robustify.patch
- updated for 0.121 (from FC)
[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.
20 * elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Fix comment pasto.
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
98f2540e
JB
68--- elfutils-0.120/libelf/elf32_getphdr.c
69+++ elfutils-0.120/libelf/elf32_getphdr.c
70@@ -115,6 +115,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
49fd4b14
JB
71
72 if (elf->map_address != NULL)
73 {
74+ /* First see whether the information in the ELF header is
75+ valid and it does not ask for too much. */
76+ if (unlikely (ehdr->e_phoff >= elf->maximum_size)
77+ || unlikely (ehdr->e_phoff + size > elf->maximum_size))
78+ {
79+ /* Something is wrong. */
80+ __libelf_seterrno (ELF_E_INVALID_PHDR);
81+ goto out;
82+ }
83+
84 /* All the data is already mapped. Use it. */
85 if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
86 && (ALLOW_UNALIGNED
98f2540e
JB
87--- elfutils-0.120/libelf/elf32_getshdr.c
88+++ elfutils-0.120/libelf/elf32_getshdr.c
89@@ -101,11 +101,12 @@ elfw2(LIBELFBITS,getshdr) (scn)
49fd4b14
JB
90 goto out;
91
92 size_t shnum;
93- if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
94+ if (INTUSE (elf_getshnum) (elf, &shnum) != 0
95+ || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
96 goto out;
97 size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
98
99- /* Allocate memory for the program headers. We know the number
100+ /* Allocate memory for the section headers. We know the number
101 of entries from the ELF header. */
102 ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
103 (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
98f2540e
JB
104@@ -128,6 +129,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
105 + ehdr->e_shoff)
49fd4b14
JB
106 & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
107
108+ /* First see whether the information in the ELF header is
109+ valid and it does not ask for too much. */
110+ if (unlikely (ehdr->e_shoff >= elf->maximum_size)
111+ || unlikely (ehdr->e_shoff + size > elf->maximum_size))
112+ {
113+ /* Something is wrong. */
114+ __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
115+ goto free_and_out;
116+ }
117+
118 /* Now copy the data and at the same time convert the byte
119 order. */
120 if (ALLOW_UNALIGNED
98f2540e
JB
121--- elfutils-0.120/libelf/elf32_newphdr.c
122+++ elfutils-0.120/libelf/elf32_newphdr.c
123@@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
49fd4b14
JB
124 else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
125 || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
0dcd74c7 126 {
49fd4b14
JB
127+ if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
128+ {
129+ result = NULL;
130+ goto out;
131+ }
132+
133 /* Allocate a new program header with the appropriate number of
134 elements. */
135 result = (ElfW2(LIBELFBITS,Phdr) *)
98f2540e
JB
136--- elfutils-0.120/libelf/elf32_updatefile.c
137+++ elfutils-0.120/libelf/elf32_updatefile.c
138@@ -201,6 +201,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
49fd4b14
JB
139 /* Write all the sections. Well, only those which are modified. */
140 if (shnum > 0)
0dcd74c7 141 {
49fd4b14 142+ if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
98f2540e 143+ return 1;
49fd4b14 144+
49fd4b14
JB
145 Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
146 Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
98f2540e
JB
147 char *const shdr_start = ((char *) elf->map_address + elf->start_offset
148@@ -545,6 +548,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
49fd4b14
JB
149 /* Write all the sections. Well, only those which are modified. */
150 if (shnum > 0)
151 {
152+ if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
153+ + sizeof (ElfW2(LIBELFBITS,Shdr)))))
154+ return 1;
155+
156 off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
157 #if EV_NUM != 2
158 xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
98f2540e
JB
159--- elfutils-0.120/libelf/elf_begin.c
160+++ elfutils-0.120/libelf/elf_begin.c
161@@ -155,7 +155,8 @@ get_shnum (void *map_address, unsigned c
49fd4b14
JB
162
163 if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
0dcd74c7 164 {
49fd4b14
JB
165- if (offset + ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
166+ if (unlikely (ehdr.e32->e_shoff >= maxsize)
167+ || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
168 /* Cannot read the first section header. */
169 return (size_t) -1l;
170
98f2540e 171@@ -198,7 +199,8 @@ get_shnum (void *map_address, unsigned c
49fd4b14
JB
172
173 if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
174 {
175- if (offset + ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
176+ if (unlikely (ehdr.e64->e_shoff >= maxsize)
177+ || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
178 /* Cannot read the first section header. */
179 return (size_t) -1l;
180
98f2540e 181@@ -265,6 +267,15 @@ file_read_elf (int fildes, void *map_add
49fd4b14
JB
182 /* Could not determine the number of sections. */
183 return NULL;
184
185+ /* Check for too many sections. */
186+ if (e_ident[EI_CLASS] == ELFCLASS32)
187+ {
188+ if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
189+ return NULL;
190+ }
191+ else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
192+ return NULL;
193+
194 /* We can now allocate the memory. */
195 Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
196 ELF_K_ELF, scncnt * sizeof (Elf_Scn));
98f2540e 197@@ -298,13 +309,31 @@ file_read_elf (int fildes, void *map_add
49fd4b14
JB
198 {
199 /* We can use the mmapped memory. */
200 elf->state.elf32.ehdr = ehdr;
201+
202+ if (unlikely (ehdr->e_shoff >= maxsize)
203+ || unlikely (ehdr->e_shoff
204+ + scncnt * sizeof (Elf32_Shdr) > maxsize))
205+ {
206+ free_and_out:
207+ free (elf);
208+ __libelf_seterrno (ELF_E_INVALID_FILE);
209+ return NULL;
210+ }
211 elf->state.elf32.shdr
212 = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
213+
214 if (ehdr->e_phnum > 0)
215- /* Assign a value only if there really is a program
216- header. Otherwise the value remains NULL. */
217- elf->state.elf32.phdr
218- = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
219+ {
220+ /* Assign a value only if there really is a program
221+ header. Otherwise the value remains NULL. */
222+ if (unlikely (ehdr->e_phoff >= maxsize)
223+ || unlikely (ehdr->e_phoff
224+ + ehdr->e_phnum
225+ * sizeof (Elf32_Phdr) > maxsize))
226+ goto free_and_out;
227+ elf->state.elf32.phdr
228+ = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
229+ }
230
231 for (size_t cnt = 0; cnt < scncnt; ++cnt)
232 {
98f2540e 233@@ -373,13 +402,26 @@ file_read_elf (int fildes, void *map_add
49fd4b14
JB
234 {
235 /* We can use the mmapped memory. */
236 elf->state.elf64.ehdr = ehdr;
237+
238+ if (unlikely (ehdr->e_shoff >= maxsize)
239+ || unlikely (ehdr->e_shoff
240+ + scncnt * sizeof (Elf32_Shdr) > maxsize))
241+ goto free_and_out;
242 elf->state.elf64.shdr
243 = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
244+
245 if (ehdr->e_phnum > 0)
246- /* Assign a value only if there really is a program
247- header. Otherwise the value remains NULL. */
248- elf->state.elf64.phdr
249- = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
250+ {
251+ /* Assign a value only if there really is a program
252+ header. Otherwise the value remains NULL. */
253+ if (unlikely (ehdr->e_phoff >= maxsize)
254+ || unlikely (ehdr->e_phoff
255+ + ehdr->e_phnum
256+ * sizeof (Elf32_Phdr) > maxsize))
257+ goto free_and_out;
258+ elf->state.elf64.phdr
259+ = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
260+ }
261
262 for (size_t cnt = 0; cnt < scncnt; ++cnt)
263 {
98f2540e
JB
264--- elfutils-0.120/libelf/elf_getarsym.c
265+++ elfutils-0.120/libelf/elf_getarsym.c
266@@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
49fd4b14
JB
267 size_t index_size = atol (tmpbuf);
268
269 if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
270+#if SIZE_MAX <= 4294967295U
271+ || n >= SIZE_MAX / sizeof (Elf_Arsym)
272+#endif
273 || n * sizeof (uint32_t) > index_size)
274 {
275 /* This index table cannot be right since it does not fit into
98f2540e
JB
276--- elfutils-0.120/libelf/elf_getshstrndx.c
277+++ elfutils-0.120/libelf/elf_getshstrndx.c
278@@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
49fd4b14
JB
279 if (elf->map_address != NULL
280 && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
281 && (ALLOW_UNALIGNED
282- || (((size_t) ((char *) elf->map_address + offset))
283+ || (((size_t) ((char *) elf->map_address
284+ + elf->start_offset + offset))
285 & (__alignof__ (Elf32_Shdr) - 1)) == 0))
286- /* We can directly access the memory. */
287- num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
288+ {
289+ /* First see whether the information in the ELF header is
290+ valid and it does not ask for too much. */
291+ if (unlikely (offset + sizeof (Elf32_Shdr)
292+ > elf->maximum_size))
293+ {
294+ /* Something is wrong. */
295+ __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
296+ result = -1;
297+ goto out;
298+ }
299+
300+ /* We can directly access the memory. */
301+ num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
302+ + offset))->sh_link;
303+ }
304 else
305 {
306 /* We avoid reading in all the section headers. Just read
98f2540e 307@@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
49fd4b14
JB
308 if (elf->map_address != NULL
309 && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
310 && (ALLOW_UNALIGNED
311- || (((size_t) ((char *) elf->map_address + offset))
312+ || (((size_t) ((char *) elf->map_address
313+ + elf->start_offset + offset))
314 & (__alignof__ (Elf64_Shdr) - 1)) == 0))
315- /* We can directly access the memory. */
316- num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
317+ {
318+ /* First see whether the information in the ELF header is
319+ valid and it does not ask for too much. */
320+ if (unlikely (offset + sizeof (Elf64_Shdr)
321+ > elf->maximum_size))
322+ {
323+ /* Something is wrong. */
324+ __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
325+ result = -1;
326+ goto out;
327+ }
328+
329+ /* We can directly access the memory. */
330+ num = ((Elf64_Shdr *) (elf->map_address
331+ + elf->start_offset + offset))->sh_link;
332+ }
333 else
334 {
335 /* We avoid reading in all the section headers. Just read
98f2540e
JB
336--- elfutils-0.120/libelf/elf_newscn.c
337+++ elfutils-0.120/libelf/elf_newscn.c
338@@ -104,13 +104,21 @@ elf_newscn (elf)
49fd4b14 339 else
0dcd74c7 340 {
49fd4b14
JB
341 /* We must allocate a new element. */
342- Elf_ScnList *newp;
343+ Elf_ScnList *newp = NULL;
344
345 assert (elf->state.elf.scnincr > 0);
346
347- newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
348- + ((elf->state.elf.scnincr *= 2)
349- * sizeof (Elf_Scn)), 1);
350+ if (
351+#if SIZE_MAX <= 4294967295U
352+ likely (elf->state.elf.scnincr
353+ < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
354+#else
355+ 1
356+#endif
357+ )
358+ newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
359+ + ((elf->state.elf.scnincr *= 2)
360+ * sizeof (Elf_Scn)), 1);
361 if (newp == NULL)
0dcd74c7 362 {
49fd4b14 363 __libelf_seterrno (ELF_E_NOMEM);
98f2540e
JB
364--- elfutils-0.120/libelf/gelf_getdyn.c
365+++ elfutils-0.120/libelf/gelf_getdyn.c
366@@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
0dcd74c7
JR
367 table entries has to be adopted. The user better has provided
368 a buffer where we can store the information. While copying the
369 data we are converting the format. */
49fd4b14
JB
370- if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
371+ if (INVALID_NDX (ndx, Elf32_Dyn)
372+ || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
0dcd74c7
JR
373 {
374 __libelf_seterrno (ELF_E_INVALID_INDEX);
375 goto out;
98f2540e 376@@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
0dcd74c7
JR
377
378 /* The data is already in the correct form. Just make sure the
379 index is OK. */
49fd4b14
JB
380- if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
381+ if (INVALID_NDX (ndx, GElf_Dyn)
382+ || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
0dcd74c7
JR
383 {
384 __libelf_seterrno (ELF_E_INVALID_INDEX);
385 goto out;
98f2540e
JB
386--- elfutils-0.120/libelf/gelf_getlib.c
387+++ elfutils-0.120/libelf/gelf_getlib.c
388@@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
49fd4b14
JB
389 /* The data is already in the correct form. Just make sure the
390 index is OK. */
391 GElf_Lib *result = NULL;
392- if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
393+ if (INVALID_NDX (ndx, GElf_Lib)
394+ || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
395 __libelf_seterrno (ELF_E_INVALID_INDEX);
396 else
397 {
98f2540e
JB
398--- elfutils-0.120/libelf/gelf_getmove.c
399+++ elfutils-0.120/libelf/gelf_getmove.c
400@@ -83,7 +83,8 @@ gelf_getmove (data, ndx, dst)
0dcd74c7 401
49fd4b14
JB
402 /* The data is already in the correct form. Just make sure the
403 index is OK. */
404- if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
405+ if (INVALID_NDX (ndx, GElf_Move)
406+ || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
407 {
408 __libelf_seterrno (ELF_E_INVALID_INDEX);
409 goto out;
98f2540e
JB
410--- elfutils-0.120/libelf/gelf_getrela.c
411+++ elfutils-0.120/libelf/gelf_getrela.c
412@@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
0dcd74c7
JR
413 if (data_scn == NULL)
414 return NULL;
415
416- if (unlikely (ndx < 0))
417- {
418- __libelf_seterrno (ELF_E_INVALID_INDEX);
419- return NULL;
420- }
421-
422 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
423 {
424 __libelf_seterrno (ELF_E_INVALID_HANDLE);
98f2540e 425@@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
0dcd74c7
JR
426 if (scn->elf->class == ELFCLASS32)
427 {
428 /* We have to convert the data. */
429- if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
430+ if (INVALID_NDX (ndx, Elf32_Rela)
431+ || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
432 {
433 __libelf_seterrno (ELF_E_INVALID_INDEX);
434 result = NULL;
98f2540e 435@@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
0dcd74c7
JR
436 {
437 /* Simply copy the data after we made sure we are actually getting
438 correct data. */
439- if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
440+ if (INVALID_NDX (ndx, Elf64_Rela)
441+ || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
442 {
443 __libelf_seterrno (ELF_E_INVALID_INDEX);
444 result = NULL;
98f2540e
JB
445--- elfutils-0.120/libelf/gelf_getrel.c
446+++ elfutils-0.120/libelf/gelf_getrel.c
447@@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
49fd4b14
JB
448 if (data_scn == NULL)
449 return NULL;
0dcd74c7
JR
450
451- if (unlikely (ndx < 0))
452- {
453- __libelf_seterrno (ELF_E_INVALID_INDEX);
49fd4b14 454- return NULL;
0dcd74c7
JR
455- }
456-
49fd4b14 457 if (unlikely (data_scn->d.d_type != ELF_T_REL))
0dcd74c7 458 {
49fd4b14 459 __libelf_seterrno (ELF_E_INVALID_HANDLE);
98f2540e 460@@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
49fd4b14 461 if (scn->elf->class == ELFCLASS32)
0dcd74c7 462 {
49fd4b14
JB
463 /* We have to convert the data. */
464- if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
465+ if (INVALID_NDX (ndx, Elf32_Rel)
466+ || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
467 {
468 __libelf_seterrno (ELF_E_INVALID_INDEX);
469 result = NULL;
98f2540e 470@@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
0dcd74c7 471 {
49fd4b14
JB
472 /* Simply copy the data after we made sure we are actually getting
473 correct data. */
474- if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
475+ if (INVALID_NDX (ndx, Elf64_Rel)
476+ || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
0dcd74c7 477 {
49fd4b14
JB
478 __libelf_seterrno (ELF_E_INVALID_INDEX);
479 result = NULL;
98f2540e
JB
480--- elfutils-0.120/libelf/gelf_getsym.c
481+++ elfutils-0.120/libelf/gelf_getsym.c
482@@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
49fd4b14
JB
483 table entries has to be adopted. The user better has provided
484 a buffer where we can store the information. While copying the
485 data we are converting the format. */
486- if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
487+ if (INVALID_NDX (ndx, Elf32_Sym)
488+ || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
489 {
490 __libelf_seterrno (ELF_E_INVALID_INDEX);
491 goto out;
98f2540e 492@@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
0dcd74c7 493
49fd4b14
JB
494 /* The data is already in the correct form. Just make sure the
495 index is OK. */
496- if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
497+ if (INVALID_NDX (ndx, GElf_Sym)
498+ || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
499 {
500 __libelf_seterrno (ELF_E_INVALID_INDEX);
501 goto out;
98f2540e
JB
502--- elfutils-0.120/libelf/gelf_getsyminfo.c
503+++ elfutils-0.120/libelf/gelf_getsyminfo.c
504@@ -84,7 +84,8 @@ gelf_getsyminfo (data, ndx, dst)
0dcd74c7
JR
505
506 /* The data is already in the correct form. Just make sure the
507 index is OK. */
49fd4b14
JB
508- if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
509+ if (INVALID_NDX (ndx, GElf_Syminfo)
510+ || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
0dcd74c7
JR
511 {
512 __libelf_seterrno (ELF_E_INVALID_INDEX);
513 goto out;
98f2540e
JB
514--- elfutils-0.120/libelf/gelf_getsymshndx.c
515+++ elfutils-0.120/libelf/gelf_getsymshndx.c
516@@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, nd
0dcd74c7
JR
517 section index table. */
518 if (likely (shndxdata_scn != NULL))
519 {
520- if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
521+ if (INVALID_NDX (ndx, Elf32_Word)
522+ || unlikely ((ndx + 1) * sizeof (Elf32_Word)
523+ > shndxdata_scn->d.d_size))
524 {
525 __libelf_seterrno (ELF_E_INVALID_INDEX);
526 goto out;
98f2540e 527@@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
0dcd74c7
JR
528 table entries has to be adopted. The user better has provided
529 a buffer where we can store the information. While copying the
530 data we are converting the format. */
531- if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
532+ if (INVALID_NDX (ndx, Elf32_Sym)
533+ || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
534 {
535 __libelf_seterrno (ELF_E_INVALID_INDEX);
536 goto out;
98f2540e 537@@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
0dcd74c7
JR
538
539 /* The data is already in the correct form. Just make sure the
540 index is OK. */
541- if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
542+ if (INVALID_NDX (ndx, GElf_Sym)
543+ || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
544 {
545 __libelf_seterrno (ELF_E_INVALID_INDEX);
546 goto out;
98f2540e
JB
547--- elfutils-0.120/libelf/gelf_getversym.c
548+++ elfutils-0.120/libelf/gelf_getversym.c
549@@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
49fd4b14
JB
550
551 /* The data is already in the correct form. Just make sure the
552 index is OK. */
553- if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
554+ if (INVALID_NDX (ndx, GElf_Versym)
555+ || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
556 {
557 __libelf_seterrno (ELF_E_INVALID_INDEX);
558 result = NULL;
98f2540e
JB
559--- elfutils-0.120/libelf/gelf_update_dyn.c
560+++ elfutils-0.120/libelf/gelf_update_dyn.c
561@@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
49fd4b14
JB
562 if (data == NULL)
563 return 0;
564
565- if (unlikely (ndx < 0))
566- {
567- __libelf_seterrno (ELF_E_INVALID_INDEX);
568- return 0;
569- }
570-
571 if (unlikely (data_scn->d.d_type != ELF_T_DYN))
572 {
573 /* The type of the data better should match. */
98f2540e 574@@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
49fd4b14
JB
575 }
576
577 /* Check whether we have to resize the data buffer. */
578- if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
579+ if (INVALID_NDX (ndx, Elf32_Dyn)
580+ || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
581 {
582 __libelf_seterrno (ELF_E_INVALID_INDEX);
583 goto out;
98f2540e 584@@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
49fd4b14
JB
585 else
586 {
587 /* Check whether we have to resize the data buffer. */
588- if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
589+ if (INVALID_NDX (ndx, Elf64_Dyn)
590+ || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
591 {
592 __libelf_seterrno (ELF_E_INVALID_INDEX);
593 goto out;
98f2540e
JB
594--- elfutils-0.120/libelf/gelf_update_lib.c
595+++ elfutils-0.120/libelf/gelf_update_lib.c
596@@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
49fd4b14
JB
597 if (data == NULL)
598 return 0;
599
600- if (unlikely (ndx < 0))
601- {
602- __libelf_seterrno (ELF_E_INVALID_INDEX);
603- return 0;
604- }
605-
606 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
607 if (unlikely (data_scn->d.d_type != ELF_T_LIB))
608 {
98f2540e 609@@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
49fd4b14
JB
610
611 /* Check whether we have to resize the data buffer. */
612 int result = 0;
613- if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
614+ if (INVALID_NDX (ndx, Elf64_Lib)
615+ || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
616 __libelf_seterrno (ELF_E_INVALID_INDEX);
617 else
618 {
98f2540e
JB
619--- elfutils-0.120/libelf/gelf_update_move.c
620+++ elfutils-0.120/libelf/gelf_update_move.c
621@@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
0dcd74c7
JR
622 assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
623
624 /* Check whether we have to resize the data buffer. */
625- if (unlikely (ndx < 0)
626+ if (INVALID_NDX (ndx, GElf_Move)
627 || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
628 {
629 __libelf_seterrno (ELF_E_INVALID_INDEX);
98f2540e
JB
630--- elfutils-0.120/libelf/gelf_update_rela.c
631+++ elfutils-0.120/libelf/gelf_update_rela.c
632@@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx
49fd4b14 633 if (dst == NULL)
0dcd74c7
JR
634 return 0;
635
636- if (unlikely (ndx < 0))
637- {
638- __libelf_seterrno (ELF_E_INVALID_INDEX);
639- return 0;
640- }
641-
49fd4b14 642 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
0dcd74c7
JR
643 {
644 /* The type of the data better should match. */
98f2540e 645@@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
0dcd74c7
JR
646 }
647
648 /* Check whether we have to resize the data buffer. */
49fd4b14
JB
649- if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
650+ if (INVALID_NDX (ndx, Elf32_Rela)
651+ || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
0dcd74c7
JR
652 {
653 __libelf_seterrno (ELF_E_INVALID_INDEX);
654 goto out;
98f2540e 655@@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
0dcd74c7
JR
656 else
657 {
658 /* Check whether we have to resize the data buffer. */
49fd4b14
JB
659- if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
660+ if (INVALID_NDX (ndx, Elf64_Rela)
661+ || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
0dcd74c7
JR
662 {
663 __libelf_seterrno (ELF_E_INVALID_INDEX);
664 goto out;
98f2540e
JB
665--- elfutils-0.120/libelf/gelf_update_rel.c
666+++ elfutils-0.120/libelf/gelf_update_rel.c
667@@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
49fd4b14 668 if (dst == NULL)
0dcd74c7
JR
669 return 0;
670
671- if (unlikely (ndx < 0))
672- {
673- __libelf_seterrno (ELF_E_INVALID_INDEX);
674- return 0;
675- }
676-
49fd4b14 677 if (unlikely (data_scn->d.d_type != ELF_T_REL))
0dcd74c7
JR
678 {
679 /* The type of the data better should match. */
98f2540e 680@@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
0dcd74c7
JR
681 }
682
683 /* Check whether we have to resize the data buffer. */
49fd4b14
JB
684- if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
685+ if (INVALID_NDX (ndx, Elf32_Rel)
686+ || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
0dcd74c7
JR
687 {
688 __libelf_seterrno (ELF_E_INVALID_INDEX);
689 goto out;
98f2540e 690@@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
0dcd74c7
JR
691 else
692 {
693 /* Check whether we have to resize the data buffer. */
49fd4b14
JB
694- if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
695+ if (INVALID_NDX (ndx, Elf64_Rel)
696+ || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
0dcd74c7
JR
697 {
698 __libelf_seterrno (ELF_E_INVALID_INDEX);
699 goto out;
98f2540e
JB
700--- elfutils-0.120/libelf/gelf_update_sym.c
701+++ elfutils-0.120/libelf/gelf_update_sym.c
702@@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
49fd4b14 703 if (data == NULL)
0dcd74c7
JR
704 return 0;
705
706- if (unlikely (ndx < 0))
707- {
708- __libelf_seterrno (ELF_E_INVALID_INDEX);
709- return 0;
710- }
711-
49fd4b14 712 if (unlikely (data_scn->d.d_type != ELF_T_SYM))
0dcd74c7
JR
713 {
714 /* The type of the data better should match. */
98f2540e 715@@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
0dcd74c7
JR
716 }
717
718 /* Check whether we have to resize the data buffer. */
49fd4b14
JB
719- if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
720+ if (INVALID_NDX (ndx, Elf32_Sym)
721+ || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
0dcd74c7
JR
722 {
723 __libelf_seterrno (ELF_E_INVALID_INDEX);
724 goto out;
98f2540e 725@@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
0dcd74c7
JR
726 else
727 {
728 /* Check whether we have to resize the data buffer. */
49fd4b14
JB
729- if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
730+ if (INVALID_NDX (ndx, Elf64_Sym)
731+ || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
0dcd74c7
JR
732 {
733 __libelf_seterrno (ELF_E_INVALID_INDEX);
734 goto out;
98f2540e
JB
735--- elfutils-0.120/libelf/gelf_update_syminfo.c
736+++ elfutils-0.120/libelf/gelf_update_syminfo.c
737@@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
49fd4b14
JB
738 if (data == NULL)
739 return 0;
740
741- if (unlikely (ndx < 0))
742- {
743- __libelf_seterrno (ELF_E_INVALID_INDEX);
744- return 0;
745- }
746-
747 if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
748 {
749 /* The type of the data better should match. */
98f2540e 750@@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
49fd4b14 751 rwlock_wrlock (scn->elf->lock);
0dcd74c7
JR
752
753 /* Check whether we have to resize the data buffer. */
49fd4b14
JB
754- if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
755+ if (INVALID_NDX (ndx, GElf_Syminfo)
756+ || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
0dcd74c7
JR
757 {
758 __libelf_seterrno (ELF_E_INVALID_INDEX);
49fd4b14 759 goto out;
98f2540e
JB
760--- elfutils-0.120/libelf/gelf_update_symshndx.c
761+++ elfutils-0.120/libelf/gelf_update_symshndx.c
762@@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
49fd4b14 763 if (symdata == NULL)
0dcd74c7
JR
764 return 0;
765
766- if (unlikely (ndx < 0))
767- {
768- __libelf_seterrno (ELF_E_INVALID_INDEX);
769- return 0;
770- }
771-
49fd4b14 772 if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
0dcd74c7
JR
773 {
774 /* The type of the data better should match. */
98f2540e 775@@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
0dcd74c7
JR
776 }
777
778 /* Check whether we have to resize the data buffer. */
49fd4b14
JB
779- if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
780+ if (INVALID_NDX (ndx, Elf32_Sym)
781+ || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
0dcd74c7
JR
782 {
783 __libelf_seterrno (ELF_E_INVALID_INDEX);
784 goto out;
98f2540e 785@@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
0dcd74c7
JR
786 else
787 {
49fd4b14
JB
788 /* Check whether we have to resize the data buffer. */
789- if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
790+ if (INVALID_NDX (ndx, Elf64_Sym)
791+ || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
792 {
793 __libelf_seterrno (ELF_E_INVALID_INDEX);
794 goto out;
98f2540e
JB
795--- elfutils-0.120/libelf/gelf_update_versym.c
796+++ elfutils-0.120/libelf/gelf_update_versym.c
797@@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
49fd4b14
JB
798 assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
799
800 /* Check whether we have to resize the data buffer. */
801- if (unlikely (ndx < 0)
802+ if (INVALID_NDX (ndx, GElf_Versym)
803 || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
804 {
805 __libelf_seterrno (ELF_E_INVALID_INDEX);
98f2540e
JB
806--- elfutils-0.120/libelf/libelfP.h
807+++ elfutils-0.120/libelf/libelfP.h
808@@ -569,4 +569,13 @@ extern uint32_t __libelf_crc32 (uint32_t
49fd4b14
JB
809 } while (0)
810 #endif
811
812+/* Convenience macro. Assumes int NDX and TYPE with size at least
813+ 2 bytes. */
814+#if SIZE_MAX > 4294967295U
815+# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
816+#else
817+# define INVALID_NDX(ndx, type) \
818+ unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
819+#endif
820+
821 #endif /* libelfP.h */
98f2540e
JB
822--- elfutils-0.120/src/elflint.c
823+++ elfutils-0.120/src/elflint.c
824@@ -123,6 +123,9 @@ static uint32_t shstrndx;
0dcd74c7
JR
825 /* Array to count references in section groups. */
826 static int *scnref;
827
828+/* Number of sections. */
829+static unsigned int shnum;
830+
831
832 int
833 main (int argc, char *argv[])
98f2540e 834@@ -312,10 +315,19 @@ section_name (Ebl *ebl, int idx)
0dcd74c7
JR
835 {
836 GElf_Shdr shdr_mem;
837 GElf_Shdr *shdr;
838+ const char *ret;
839+
840+ if ((unsigned int) idx > shnum)
841+ return "<invalid>";
842
843 shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
49fd4b14
JB
844+ if (shdr == NULL)
845+ return "<invalid>";
0dcd74c7
JR
846
847- return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
848+ ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
849+ if (ret == NULL)
850+ return "<invalid>";
851+ return ret;
852 }
853
854
98f2540e 855@@ -337,10 +349,6 @@ static const int valid_e_machine[] =
0dcd74c7
JR
856 (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
857
858
859-/* Number of sections. */
860-static unsigned int shnum;
861-
862-
863 static void
864 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
865 {
98f2540e 866@@ -603,7 +611,8 @@ section [%2d] '%s': symbol table cannot
49fd4b14
JB
867 }
868 }
0dcd74c7
JR
869
870- if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
871+ size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
872+ if (shdr->sh_entsize != sh_entsize)
873 ERROR (gettext ("\
874 section [%2zu] '%s': entry size is does not match ElfXX_Sym\n"),
875 cnt, section_name (ebl, cnt));
98f2540e 876@@ -641,7 +650,7 @@ section [%2d] '%s': XINDEX for zeroth en
0dcd74c7
JR
877 xndxscnidx, section_name (ebl, xndxscnidx));
878 }
879
880- for (cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
881+ for (cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
882 {
883 sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
884 if (sym == NULL)
98f2540e 885@@ -659,7 +668,8 @@ section [%2d] '%s': symbol %zu: invalid
0dcd74c7
JR
886 else
887 {
888 name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
889- assert (name != NULL);
890+ assert (name != NULL
891+ || strshdr->sh_type != SHT_STRTAB);
892 }
893
894 if (sym->st_shndx == SHN_XINDEX)
98f2540e 895@@ -970,7 +980,7 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
0dcd74c7
JR
896 const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
897 assert (rcshdr != NULL);
898
899- if (rcshdr->sh_type == SHT_DYNAMIC)
900+ if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
901 {
902 /* Found the dynamic section. Look through it. */
903 Elf_Data *d = elf_getdata (scn, NULL);
98f2540e 904@@ -980,14 +990,17 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
0dcd74c7
JR
905 {
906 GElf_Dyn dyn_mem;
907 GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
908- assert (dyn != NULL);
909+
910+ if (dyn == NULL)
911+ break;
912
913 if (dyn->d_tag == DT_RELCOUNT)
914 {
915 /* Found it. One last check: does the number
916 specified number of relative relocations exceed
917 the total number of relocations? */
918- if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
919+ if (shdr->sh_entsize
920+ && dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
921 ERROR (gettext ("\
922 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
923 idx, section_name (ebl, idx),
98f2540e 924@@ -1062,7 +1075,8 @@ section [%2d] '%s': no relocations for m
0dcd74c7
JR
925 }
926 }
927
49fd4b14
JB
928- if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
929+ size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
0dcd74c7 930+ if (shdr->sh_entsize != sh_entsize)
49fd4b14
JB
931 ERROR (gettext (reltype == ELF_T_RELA ? "\
932 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
0dcd74c7 933 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
98f2540e 934@@ -1280,7 +1294,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
0dcd74c7 935 Elf_Data *symdata = elf_getdata (symscn, NULL);
49fd4b14 936 enum load_state state = state_undecided;
0dcd74c7 937
49fd4b14
JB
938- for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
939+ size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
940+ for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
941 {
942 GElf_Rela rela_mem;
943 GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
98f2540e 944@@ -1330,7 +1345,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
49fd4b14
JB
945 Elf_Data *symdata = elf_getdata (symscn, NULL);
946 enum load_state state = state_undecided;
0dcd74c7 947
49fd4b14
JB
948- for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
949+ size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
950+ for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
951 {
952 GElf_Rel rel_mem;
953 GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
98f2540e 954@@ -1432,7 +1448,8 @@ section [%2d] '%s': referenced as string
49fd4b14
JB
955 shdr->sh_link, section_name (ebl, shdr->sh_link),
956 idx, section_name (ebl, idx));
0dcd74c7 957
49fd4b14
JB
958- if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
959+ size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
960+ if (shdr->sh_entsize != sh_entsize)
961 ERROR (gettext ("\
962 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
963 idx, section_name (ebl, idx));
98f2540e 964@@ -1442,7 +1459,7 @@ section [%2d] '%s': section entry size d
49fd4b14 965 idx, section_name (ebl, idx));
0dcd74c7 966
49fd4b14
JB
967 bool non_null_warned = false;
968- for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
969+ for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
970 {
971 GElf_Dyn dyn_mem;
972 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
98f2540e 973@@ -1633,6 +1650,8 @@ section [%2d] '%s': entry size does not
49fd4b14 974 idx, section_name (ebl, idx));
0dcd74c7 975
49fd4b14
JB
976 if (symshdr != NULL
977+ && shdr->sh_entsize
978+ && symshdr->sh_entsize
979 && (shdr->sh_size / shdr->sh_entsize
980 < symshdr->sh_size / symshdr->sh_entsize))
981 ERROR (gettext ("\
98f2540e 982@@ -1659,6 +1678,12 @@ section [%2d] '%s': extended section ind
49fd4b14
JB
983 }
984
985 Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
986+ if (data == NULL)
987+ {
988+ ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
989+ idx, section_name (ebl, idx));
990+ return;
991+ }
992
993 if (*((Elf32_Word *) data->d_buf) != 0)
994 ERROR (gettext ("symbol 0 should have zero extended section index\n"));
98f2540e 995@@ -1739,23 +1764,30 @@ section [%2d] '%s': hash table section i
49fd4b14
JB
996 idx, section_name (ebl, idx), (long int) shdr->sh_size,
997 (long int) ((2 + nbucket + nchain) * shdr->sh_entsize));
0dcd74c7 998
49fd4b14
JB
999- if (symshdr != NULL)
1000+ if (symshdr != NULL && symshdr->sh_entsize)
0dcd74c7
JR
1001 {
1002 size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
1003 size_t cnt;
1004+ Elf32_Word *buf, *end;
1005
1006 if (nchain < symshdr->sh_size / symshdr->sh_entsize)
1007 ERROR (gettext ("section [%2d] '%s': chain array not large enough\n"),
1008 idx, section_name (ebl, idx));
1009
1010+ buf = ((Elf32_Word *) data->d_buf) + 2;
1011+ end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
1012 for (cnt = 2; cnt < 2 + nbucket; ++cnt)
1013- if (((Elf32_Word *) data->d_buf)[cnt] >= symsize)
1014+ if (buf >= end)
1015+ return;
1016+ else if (*buf++ >= symsize)
1017 ERROR (gettext ("\
1018 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
1019 idx, section_name (ebl, idx), cnt - 2);
1020
49fd4b14
JB
1021 for (; cnt < 2 + nbucket + nchain; ++cnt)
1022- if (((Elf32_Word *) data->d_buf)[cnt] >= symsize)
1023+ if (buf >= end)
1024+ return;
1025+ else if (*buf++ >= symsize)
1026 ERROR (gettext ("\
1027 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
1028 idx, section_name (ebl, idx), cnt - 2 - nbucket);
98f2540e 1029@@ -2097,8 +2129,9 @@ section [%2d] '%s' refers in sh_link to
49fd4b14
JB
1030
1031 /* The number of elements in the version symbol table must be the
1032 same as the number of symbols. */
1033- if (shdr->sh_size / shdr->sh_entsize
1034- != symshdr->sh_size / symshdr->sh_entsize)
1035+ if (shdr->sh_entsize && symshdr->sh_entsize
1036+ && (shdr->sh_size / shdr->sh_entsize
1037+ != symshdr->sh_size / symshdr->sh_entsize))
1038 ERROR (gettext ("\
1039 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
1040 idx, section_name (ebl, idx),
98f2540e 1041@@ -3002,6 +3035,8 @@ phdr[%d]: no note entries defined for th
49fd4b14
JB
1042 return;
1043
1044 char *notemem = gelf_rawchunk (ebl->elf, phdr->p_offset, phdr->p_filesz);
1045+ if (notemem == NULL)
1046+ return;
1047
1048 /* ELF64 files often use note section entries in the 32-bit format.
1049 The p_align field is set to 8 in case the 64-bit format is used.
98f2540e
JB
1050--- elfutils-0.120/src/readelf.c
1051+++ elfutils-0.120/src/readelf.c
1052@@ -958,6 +958,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
feba504d 1053 Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
49fd4b14 1054
feba504d
JB
1055 GElf_Sym sym_mem;
1056+ GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
49fd4b14 1057+
49fd4b14 1058 printf ((grpref[0] & GRP_COMDAT)
feba504d
JB
1059 ? ngettext ("\
1060 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
98f2540e 1061@@ -970,8 +972,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
49fd4b14
JB
1062 data->d_size / sizeof (Elf32_Word) - 1),
1063 elf_ndxscn (scn),
1064 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1065- elf_strptr (ebl->elf, symshdr->sh_link,
1066- gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
1067+ (sym == NULL ? NULL
1068+ : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1069 ?: gettext ("<INVALID SYMBOL>"),
1070 data->d_size / sizeof (Elf32_Word) - 1);
1071
98f2540e 1072@@ -1122,7 +1124,8 @@ static void
49fd4b14
JB
1073 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1074 {
1075 int class = gelf_getclass (ebl->elf);
1076- GElf_Shdr glink;
1077+ GElf_Shdr glink_mem;
1078+ GElf_Shdr *glink;
1079 Elf_Data *data;
1080 size_t cnt;
1081 size_t shstrndx;
98f2540e 1082@@ -1137,6 +1140,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
49fd4b14
JB
1083 error (EXIT_FAILURE, 0,
1084 gettext ("cannot get section header string table index"));
1085
1086+ glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1087+ if (glink == NULL)
1088+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1089+ elf_ndxscn (scn));
1090+
1091 printf (ngettext ("\
1092 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1093 "\
98f2540e 1094@@ -1146,9 +1154,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
49fd4b14
JB
1095 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1096 shdr->sh_offset,
1097 (int) shdr->sh_link,
1098- elf_strptr (ebl->elf, shstrndx,
1099- gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1100- &glink)->sh_name));
1101+ elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1102 fputs_unlocked (gettext (" Type Value\n"), stdout);
1103
1104 for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
98f2540e 1105@@ -1666,6 +1672,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
49fd4b14
JB
1106 error (EXIT_FAILURE, 0,
1107 gettext ("cannot get section header string table index"));
1108
feba504d
JB
1109+ GElf_Shdr glink_mem;
1110+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1111+ &glink_mem);
49fd4b14
JB
1112+ if (glink == NULL)
1113+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1114+ elf_ndxscn (scn));
1115+
1116 /* Now we can compute the number of entries in the section. */
feba504d
JB
1117 unsigned int nsyms = data->d_size / (class == ELFCLASS32
1118 ? sizeof (Elf32_Sym)
98f2540e 1119@@ -1676,15 +1689,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
feba504d
JB
1120 nsyms),
1121 (unsigned int) elf_ndxscn (scn),
1122 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
1123- GElf_Shdr glink;
1124 printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
1125 " %lu local symbols String table: [%2u] '%s'\n",
49fd4b14
JB
1126 shdr->sh_info),
1127 (unsigned long int) shdr->sh_info,
1128 (unsigned int) shdr->sh_link,
1129- elf_strptr (ebl->elf, shstrndx,
1130- gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1131- &glink)->sh_name));
1132+ elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1133
1134 fputs_unlocked (class == ELFCLASS32
1135 ? gettext ("\
98f2540e 1136@@ -1920,7 +1930,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
49fd4b14
JB
1137 error (EXIT_FAILURE, 0,
1138 gettext ("cannot get section header string table index"));
1139
1140- GElf_Shdr glink;
1141+ GElf_Shdr glink_mem;
1142+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1143+ &glink_mem);
1144+ if (glink == NULL)
1145+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1146+ elf_ndxscn (scn));
1147+
1148 printf (ngettext ("\
1149 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1150 "\
98f2540e 1151@@ -1931,9 +1947,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
49fd4b14
JB
1152 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1153 shdr->sh_offset,
1154 (unsigned int) shdr->sh_link,
1155- elf_strptr (ebl->elf, shstrndx,
1156- gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1157- &glink)->sh_name));
1158+ elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1159
1160 unsigned int offset = 0;
1161 for (int cnt = shdr->sh_info; --cnt >= 0; )
98f2540e 1162@@ -1986,8 +2000,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
49fd4b14
JB
1163 error (EXIT_FAILURE, 0,
1164 gettext ("cannot get section header string table index"));
1165
1166+ GElf_Shdr glink_mem;
1167+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1168+ &glink_mem);
1169+ if (glink == NULL)
1170+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1171+ elf_ndxscn (scn));
1172+
1173 int class = gelf_getclass (ebl->elf);
1174- GElf_Shdr glink;
1175 printf (ngettext ("\
1176 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1177 "\
98f2540e 1178@@ -1999,9 +2019,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
49fd4b14
JB
1179 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1180 shdr->sh_offset,
1181 (unsigned int) shdr->sh_link,
1182- elf_strptr (ebl->elf, shstrndx,
1183- gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1184- &glink)->sh_name));
1185+ elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1186
1187 unsigned int offset = 0;
1188 for (int cnt = shdr->sh_info; --cnt >= 0; )
98f2540e 1189@@ -2263,8 +2281,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
49fd4b14
JB
1190 filename = NULL;
1191 }
1192
1193+ GElf_Shdr glink_mem;
1194+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1195+ &glink_mem);
1196+ if (glink == NULL)
1197+ error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1198+ elf_ndxscn (scn));
1199+
1200 /* Print the header. */
feba504d 1201- GElf_Shdr glink;
49fd4b14
JB
1202 printf (ngettext ("\
1203 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
feba504d 1204 "\
98f2540e 1205@@ -2276,9 +2300,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
49fd4b14
JB
1206 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1207 shdr->sh_offset,
1208 (unsigned int) shdr->sh_link,
1209- elf_strptr (ebl->elf, shstrndx,
1210- gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1211- &glink)->sh_name));
1212+ elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1213
1214 /* Now we can finally look at the actual contents of this section. */
1215 for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
98f2540e 1216@@ -2353,7 +2375,17 @@ handle_hash (Ebl *ebl)
feba504d
JB
1217 Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
1218 Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
49fd4b14 1219
feba504d
JB
1220- GElf_Shdr glink;
1221+ GElf_Shdr glink_mem;
1222+ GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
1223+ shdr->sh_link),
1224+ &glink_mem);
1225+ if (glink == NULL)
1226+ {
1227+ error (0, 0, gettext ("invalid sh_link value in section %Zu"),
1228+ elf_ndxscn (scn));
1229+ continue;
1230+ }
49fd4b14 1231+
feba504d
JB
1232 printf (ngettext ("\
1233 \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",
1234 "\
98f2540e 1235@@ -2366,10 +2398,7 @@ handle_hash (Ebl *ebl)
49fd4b14
JB
1236 shdr->sh_addr,
1237 shdr->sh_offset,
1238 (unsigned int) shdr->sh_link,
1239- elf_strptr (ebl->elf, shstrndx,
1240- gelf_getshdr (elf_getscn (ebl->elf,
1241- shdr->sh_link),
1242- &glink)->sh_name));
1243+ elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1244
feba504d
JB
1245 uint32_t *lengths = (uint32_t *) xcalloc (nbucket,
1246 sizeof (uint32_t));
98f2540e 1247@@ -3510,6 +3539,16 @@ print_debug_aranges_section (Ebl *ebl __
49fd4b14
JB
1248 return;
1249 }
0dcd74c7 1250
49fd4b14
JB
1251+ GElf_Shdr glink_mem;
1252+ GElf_Shdr *glink;
1253+ glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1254+ if (glink == NULL)
1255+ {
1256+ error (0, 0, gettext ("invalid sh_link value in section %Zu"),
1257+ elf_ndxscn (scn));
1258+ return;
1259+ }
1260+
1261 printf (ngettext ("\
1262 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
1263 "\
98f2540e
JB
1264--- elfutils-0.120/src/strip.c
1265+++ elfutils-0.120/src/strip.c
1266@@ -412,6 +412,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1267 Elf_Data debuglink_crc_data;
1268 bool any_symtab_changes = false;
1269 Elf_Data *shstrtab_data = NULL;
1270+ size_t shdridx = 0;
1271
1272 /* Create the full name of the file. */
1273 if (prefix != NULL)
98f2540e 1274@@ -542,6 +543,11 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1275 goto fail_close;
1276 }
1277
1278+ if (shstrndx >= shnum)
1279+ goto illformed;
1280+
1281+#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
1282+
1283 /* Storage for section information. We leave room for two more
1284 entries since we unconditionally create a section header string
1285 table. Maybe some weird tool created an ELF file without one.
98f2540e 1286@@ -563,7 +569,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1287 {
1288 /* This should always be true (i.e., there should not be any
1289 holes in the numbering). */
1290- assert (elf_ndxscn (scn) == cnt);
1291+ elf_assert (elf_ndxscn (scn) == cnt);
1292
1293 shdr_info[cnt].scn = scn;
1294
98f2540e 1295@@ -576,6 +582,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1296 shdr_info[cnt].shdr.sh_name);
1297 if (shdr_info[cnt].name == NULL)
1298 {
1299+ illformed:
1300 error (0, 0, gettext ("illformed file '%s'"), fname);
1301 goto fail_close;
1302 }
98f2540e 1303@@ -585,6 +592,8 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1304
1305 /* Remember the shdr.sh_link value. */
1306 shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
1307+ if (shdr_info[cnt].old_sh_link >= shnum)
1308+ goto illformed;
1309
1310 /* Sections in files other than relocatable object files which
1311 are not loaded can be freely moved by us. In relocatable
98f2540e 1312@@ -597,7 +606,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1313 appropriate reference. */
1314 if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
1315 {
1316- assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
1317+ elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
1318 shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
1319 }
1320 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
98f2540e 1321@@ -614,7 +623,12 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1322 for (inner = 1;
1323 inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1324 ++inner)
1325- shdr_info[grpref[inner]].group_idx = cnt;
1326+ {
1327+ if (grpref[inner] < shnum)
1328+ shdr_info[grpref[inner]].group_idx = cnt;
1329+ else
1330+ goto illformed;
1331+ }
1332
1333 if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
1334 /* If the section group contains only one element and this
98f2540e 1335@@ -625,7 +639,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1336 }
1337 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
1338 {
1339- assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
1340+ elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
1341 shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
1342 }
1343
98f2540e 1344@@ -633,7 +647,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1345 discarded right away. */
1346 if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
1347 {
1348- assert (shdr_info[cnt].group_idx != 0);
1349+ elf_assert (shdr_info[cnt].group_idx != 0);
1350
1351 if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
1352 {
98f2540e 1353@@ -708,10 +722,14 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1354 {
1355 /* If a relocation section is marked as being removed make
1356 sure the section it is relocating is removed, too. */
1357- if ((shdr_info[cnt].shdr.sh_type == SHT_REL
1358- || shdr_info[cnt].shdr.sh_type == SHT_RELA)
1359- && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
1360- shdr_info[cnt].idx = 1;
1361+ if (shdr_info[cnt].shdr.sh_type == SHT_REL
1362+ || shdr_info[cnt].shdr.sh_type == SHT_RELA)
1363+ {
1364+ if (shdr_info[cnt].shdr.sh_info >= shnum)
1365+ goto illformed;
1366+ else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
1367+ shdr_info[cnt].idx = 1;
1368+ }
1369 }
1370
1371 if (shdr_info[cnt].idx == 1)
98f2540e 1372@@ -737,7 +755,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1373 if (shdr_info[cnt].symtab_idx != 0
1374 && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
1375 {
1376- assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
1377+ elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
1378
1379 shdr_info[shdr_info[cnt].symtab_idx].data
1380 = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
98f2540e 1381@@ -777,6 +795,9 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1382 else if (scnidx == SHN_XINDEX)
1383 scnidx = xndx;
1384
1385+ if (scnidx >= shnum)
1386+ goto illformed;
1387+
1388 if (shdr_info[scnidx].idx == 0)
1389 {
1390 /* Mark this section as used. */
98f2540e 1391@@ -808,11 +829,15 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1392 }
1393
1394 /* Handle references through sh_info. */
1395- if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
1396- && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
1397+ if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1398 {
1399- shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
1400- changes |= shdr_info[cnt].shdr.sh_info < cnt;
1401+ if (shdr_info[cnt].shdr.sh_info >= shnum)
1402+ goto illformed;
1403+ else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
1404+ {
1405+ shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
1406+ changes |= shdr_info[cnt].shdr.sh_info < cnt;
1407+ }
1408 }
1409
1410 /* Mark the section as investigated. */
98f2540e 1411@@ -912,7 +937,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1412 error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
1413 elf_errmsg (-1));
1414
1415- assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1416+ elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1417
1418 /* Add this name to the section header string table. */
1419 shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
98f2540e 1420@@ -949,7 +974,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1421 error (EXIT_FAILURE, 0,
1422 gettext ("while create section header section: %s"),
1423 elf_errmsg (-1));
1424- assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1425+ elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1426
1427 shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
1428 if (shdr_info[cnt].data == NULL)
98f2540e 1429@@ -980,7 +1005,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1430 }
1431
1432 /* Index of the section header table in the shdr_info array. */
1433- size_t shdridx = cnt;
1434+ shdridx = cnt;
1435
1436 /* Add the section header string table section name. */
1437 shdr_info[cnt].se = ebl_strtabadd (shst, ".shstrtab", 10);
98f2540e 1438@@ -1005,7 +1030,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1439 error (EXIT_FAILURE, 0,
1440 gettext ("while create section header section: %s"),
1441 elf_errmsg (-1));
1442- assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
1443+ elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
1444
1445 /* Finalize the string table and fill in the correct indices in the
1446 section headers. */
98f2540e 1447@@ -1095,21 +1120,21 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1448 shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
1449 NULL);
1450
1451- assert ((versiondata->d_size / sizeof (Elf32_Word))
1452- >= shdr_info[cnt].data->d_size / elsize);
1453+ elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
1454+ >= shdr_info[cnt].data->d_size / elsize);
1455 }
1456
1457 if (shdr_info[cnt].version_idx != 0)
1458 {
1459- assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
1460+ elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
1461 /* This section has associated version
1462 information. We have to modify that
1463 information, too. */
1464 versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
1465 NULL);
1466
1467- assert ((versiondata->d_size / sizeof (GElf_Versym))
1468- >= shdr_info[cnt].data->d_size / elsize);
1469+ elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
1470+ >= shdr_info[cnt].data->d_size / elsize);
1471 }
1472
1473 shdr_info[cnt].newsymidx
98f2540e 1474@@ -1163,7 +1188,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1475 sec = shdr_info[sym->st_shndx].idx;
1476 else
1477 {
1478- assert (shndxdata != NULL);
1479+ elf_assert (shndxdata != NULL);
1480
1481 sec = shdr_info[xshndx].idx;
1482 }
98f2540e 1483@@ -1184,7 +1209,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1484 nxshndx = sec;
1485 }
1486
1487- assert (sec < SHN_LORESERVE || shndxdata != NULL);
1488+ elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
1489
1490 if ((inner != destidx || nshndx != sym->st_shndx
1491 || (shndxdata != NULL && nxshndx != xshndx))
98f2540e 1492@@ -1207,7 +1232,7 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1493 else
1494 /* This is a section symbol for a section which has
1495 been removed. */
1496- assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
1497+ elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
1498 }
1499
1500 if (destidx != inner)
98f2540e 1501@@ -1371,11 +1396,11 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1502 {
1503 GElf_Sym sym_mem;
1504 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
1505- assert (sym != NULL);
1506+ elf_assert (sym != NULL);
1507
1508 const char *name = elf_strptr (elf, strshndx,
1509 sym->st_name);
1510- assert (name != NULL);
1511+ elf_assert (name != NULL);
1512 size_t hidx = elf_hash (name) % nbucket;
1513
1514 if (bucket[hidx] == 0)
98f2540e 1515@@ -1394,8 +1419,8 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1516 else
1517 {
1518 /* Alpha and S390 64-bit use 64-bit SHT_HASH entries. */
1519- assert (shdr_info[cnt].shdr.sh_entsize
1520- == sizeof (Elf64_Xword));
1521+ elf_assert (shdr_info[cnt].shdr.sh_entsize
1522+ == sizeof (Elf64_Xword));
1523
1524 Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
1525
98f2540e 1526@@ -1428,11 +1453,11 @@ handle_elf (int fd, Elf *elf, const char
0dcd74c7
JR
1527 {
1528 GElf_Sym sym_mem;
1529 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
1530- assert (sym != NULL);
1531+ elf_assert (sym != NULL);
1532
1533 const char *name = elf_strptr (elf, strshndx,
1534 sym->st_name);
1535- assert (name != NULL);
1536+ elf_assert (name != NULL);
1537 size_t hidx = elf_hash (name) % nbucket;
1538
1539 if (bucket[hidx] == 0)
This page took 0.373256 seconds and 4 git commands to generate.