2 2005-06-09 Roland McGrath <roland@redhat.com>
4 * readelf.c (handle_dynamic, handle_symtab): Check for bogus sh_link.
5 (handle_verneed, handle_verdef, handle_versym, handle_hash): Likewise.
6 (handle_scngrp): Check for bogus sh_info.
8 * strip.c (handle_elf): Check for bogus values in sh_link, sh_info,
9 st_shndx, e_shstrndx, and SHT_GROUP or SHT_SYMTAB_SHNDX data.
10 Don't use assert on input values, instead bail with "illformed" error.
12 2005-05-17 Jakub Jelinek <jakub@redhat.com>
15 * elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
16 table fits into object's bounds.
17 * elf_getshstrndx.c (elf_getshstrndx): Add elf->start_offset to
18 elf->map_address. Check if first section header fits into object's
20 * elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Fix comment pasto.
21 Check if section header table fits into object's bounds.
22 * elf_begin.c (get_shnum): Ensure section headers fits into
24 (file_read_elf): Make sure scncnt is small enough to allocate both
25 ElfXX_Shdr and Elf_Scn array. Make sure section and program header
26 tables fit into object's bounds. Avoid memory leak on failure.
29 * elflint.c (check_hash): Don't check entries beyond end of section.
30 (check_note): Don't crash if gelf_rawchunk fails.
31 (section_name): Return <invalid> if gelf_getshdr returns NULL.
33 2005-05-14 Jakub Jelinek <jakub@redhat.com>
36 * libelfP.h (INVALID_NDX): Define.
37 * gelf_getdyn.c (gelf_getdyn): Use it. Remove ndx < 0 test if any.
38 * gelf_getlib.c (gelf_getlib): Likewise.
39 * gelf_getmove.c (gelf_getmove): Likewise.
40 * gelf_getrel.c (gelf_getrel): Likewise.
41 * gelf_getrela.c (gelf_getrela): Likewise.
42 * gelf_getsym.c (gelf_getsym): Likewise.
43 * gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
44 * gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
45 * gelf_getversym.c (gelf_getversym): Likewise.
46 * gelf_update_dyn.c (gelf_update_dyn): Likewise.
47 * gelf_update_lib.c (gelf_update_lib): Likewise.
48 * gelf_update_move.c (gelf_update_move): Likewise.
49 * gelf_update_rel.c (gelf_update_rel): Likewise.
50 * gelf_update_rela.c (gelf_update_rela): Likewise.
51 * gelf_update_sym.c (gelf_update_sym): Likewise.
52 * gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
53 * gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
54 * gelf_update_versym.c (gelf_update_versym): Likewise.
55 * elf_newscn.c (elf_newscn): Check for overflow.
56 * elf32_updatefile.c (__elfw2(LIBELFBITS,updatemmap)): Likewise.
57 (__elfw2(LIBELFBITS,updatefile)): Likewise.
58 * elf_begin.c (file_read_elf): Likewise.
59 * elf32_newphdr.c (elfw2(LIBELFBITS,newphdr)): Likewise.
60 * elf_getarsym.c (elf_getarsym): Likewise.
61 * elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Likewise.
63 * elflint.c (section_name): Return "<invalid>" instead of
64 crashing on invalid section name.
65 (check_symtab, is_rel_dyn, check_rela, check_rel, check_dynamic,
66 check_symtab_shndx, check_hash, check_versym): Robustify.
68 --- elfutils-0.120/libelf/elf32_getphdr.c
69 +++ elfutils-0.120/libelf/elf32_getphdr.c
70 @@ -115,6 +115,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
72 if (elf->map_address != NULL)
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))
79 + /* Something is wrong. */
80 + __libelf_seterrno (ELF_E_INVALID_PHDR);
84 /* All the data is already mapped. Use it. */
85 if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
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)
93 - if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
94 + if (INTUSE (elf_getshnum) (elf, &shnum) != 0
95 + || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
97 size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
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);
104 @@ -128,6 +129,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
106 & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
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))
113 + /* Something is wrong. */
114 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
118 /* Now copy the data and at the same time convert the byte
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)
124 else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
125 || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
127 + if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
133 /* Allocate a new program header with the appropriate number of
135 result = (ElfW2(LIBELFBITS,Phdr) *)
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
139 /* Write all the sections. Well, only those which are modified. */
142 + if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
145 Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
146 Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
147 char *const shdr_start = ((char *) elf->map_address + elf->start_offset
148 @@ -545,6 +548,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
149 /* Write all the sections. Well, only those which are modified. */
152 + if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
153 + + sizeof (ElfW2(LIBELFBITS,Shdr)))))
156 off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
158 xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
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
163 if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
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. */
171 @@ -198,7 +199,8 @@ get_shnum (void *map_address, unsigned c
173 if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
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. */
181 @@ -265,6 +267,15 @@ file_read_elf (int fildes, void *map_add
182 /* Could not determine the number of sections. */
185 + /* Check for too many sections. */
186 + if (e_ident[EI_CLASS] == ELFCLASS32)
188 + if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
191 + else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
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));
197 @@ -298,13 +309,31 @@ file_read_elf (int fildes, void *map_add
199 /* We can use the mmapped memory. */
200 elf->state.elf32.ehdr = ehdr;
202 + if (unlikely (ehdr->e_shoff >= maxsize)
203 + || unlikely (ehdr->e_shoff
204 + + scncnt * sizeof (Elf32_Shdr) > maxsize))
208 + __libelf_seterrno (ELF_E_INVALID_FILE);
211 elf->state.elf32.shdr
212 = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
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);
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
225 + * sizeof (Elf32_Phdr) > maxsize))
227 + elf->state.elf32.phdr
228 + = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
231 for (size_t cnt = 0; cnt < scncnt; ++cnt)
233 @@ -373,13 +402,26 @@ file_read_elf (int fildes, void *map_add
235 /* We can use the mmapped memory. */
236 elf->state.elf64.ehdr = ehdr;
238 + if (unlikely (ehdr->e_shoff >= maxsize)
239 + || unlikely (ehdr->e_shoff
240 + + scncnt * sizeof (Elf32_Shdr) > maxsize))
242 elf->state.elf64.shdr
243 = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
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);
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
256 + * sizeof (Elf32_Phdr) > maxsize))
258 + elf->state.elf64.phdr
259 + = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
262 for (size_t cnt = 0; cnt < scncnt; ++cnt)
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)
267 size_t index_size = atol (tmpbuf);
269 if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
270 +#if SIZE_MAX <= 4294967295U
271 + || n >= SIZE_MAX / sizeof (Elf_Arsym)
273 || n * sizeof (uint32_t) > index_size)
275 /* This index table cannot be right since it does not fit into
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)
279 if (elf->map_address != NULL
280 && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
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;
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))
294 + /* Something is wrong. */
295 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
300 + /* We can directly access the memory. */
301 + num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
302 + + offset))->sh_link;
306 /* We avoid reading in all the section headers. Just read
307 @@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
308 if (elf->map_address != NULL
309 && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
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;
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))
323 + /* Something is wrong. */
324 + __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
329 + /* We can directly access the memory. */
330 + num = ((Elf64_Shdr *) (elf->map_address
331 + + elf->start_offset + offset))->sh_link;
335 /* We avoid reading in all the section headers. Just read
336 --- elfutils-0.120/libelf/elf_newscn.c
337 +++ elfutils-0.120/libelf/elf_newscn.c
338 @@ -104,13 +104,21 @@ elf_newscn (elf)
341 /* We must allocate a new element. */
343 + Elf_ScnList *newp = NULL;
345 assert (elf->state.elf.scnincr > 0);
347 - newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
348 - + ((elf->state.elf.scnincr *= 2)
349 - * sizeof (Elf_Scn)), 1);
351 +#if SIZE_MAX <= 4294967295U
352 + likely (elf->state.elf.scnincr
353 + < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
358 + newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
359 + + ((elf->state.elf.scnincr *= 2)
360 + * sizeof (Elf_Scn)), 1);
363 __libelf_seterrno (ELF_E_NOMEM);
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)
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. */
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))
374 __libelf_seterrno (ELF_E_INVALID_INDEX);
376 @@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
378 /* The data is already in the correct form. Just make sure the
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))
384 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
389 /* The data is already in the correct form. Just make sure the
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);
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)
402 /* The data is already in the correct form. Just make sure the
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))
408 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
413 if (data_scn == NULL)
416 - if (unlikely (ndx < 0))
418 - __libelf_seterrno (ELF_E_INVALID_INDEX);
422 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
424 __libelf_seterrno (ELF_E_INVALID_HANDLE);
425 @@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
426 if (scn->elf->class == ELFCLASS32)
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))
433 __libelf_seterrno (ELF_E_INVALID_INDEX);
435 @@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
437 /* Simply copy the data after we made sure we are actually getting
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))
443 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
448 if (data_scn == NULL)
451 - if (unlikely (ndx < 0))
453 - __libelf_seterrno (ELF_E_INVALID_INDEX);
457 if (unlikely (data_scn->d.d_type != ELF_T_REL))
459 __libelf_seterrno (ELF_E_INVALID_HANDLE);
460 @@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
461 if (scn->elf->class == ELFCLASS32)
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))
468 __libelf_seterrno (ELF_E_INVALID_INDEX);
470 @@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
472 /* Simply copy the data after we made sure we are actually getting
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))
478 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
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))
490 __libelf_seterrno (ELF_E_INVALID_INDEX);
492 @@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
494 /* The data is already in the correct form. Just make sure the
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))
500 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
506 /* The data is already in the correct form. Just make sure the
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))
512 __libelf_seterrno (ELF_E_INVALID_INDEX);
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
517 section index table. */
518 if (likely (shndxdata_scn != NULL))
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))
525 __libelf_seterrno (ELF_E_INVALID_INDEX);
527 @@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
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))
535 __libelf_seterrno (ELF_E_INVALID_INDEX);
537 @@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
539 /* The data is already in the correct form. Just make sure the
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))
545 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
551 /* The data is already in the correct form. Just make sure the
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))
557 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
565 - if (unlikely (ndx < 0))
567 - __libelf_seterrno (ELF_E_INVALID_INDEX);
571 if (unlikely (data_scn->d.d_type != ELF_T_DYN))
573 /* The type of the data better should match. */
574 @@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
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))
582 __libelf_seterrno (ELF_E_INVALID_INDEX);
584 @@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
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))
592 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
600 - if (unlikely (ndx < 0))
602 - __libelf_seterrno (ELF_E_INVALID_INDEX);
606 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
607 if (unlikely (data_scn->d.d_type != ELF_T_LIB))
609 @@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
611 /* Check whether we have to resize the data buffer. */
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);
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)
622 assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
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))
629 __libelf_seterrno (ELF_E_INVALID_INDEX);
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
636 - if (unlikely (ndx < 0))
638 - __libelf_seterrno (ELF_E_INVALID_INDEX);
642 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
644 /* The type of the data better should match. */
645 @@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
648 /* Check whether we have to resize the data buffer. */
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))
653 __libelf_seterrno (ELF_E_INVALID_INDEX);
655 @@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
658 /* Check whether we have to resize the data buffer. */
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))
663 __libelf_seterrno (ELF_E_INVALID_INDEX);
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,
671 - if (unlikely (ndx < 0))
673 - __libelf_seterrno (ELF_E_INVALID_INDEX);
677 if (unlikely (data_scn->d.d_type != ELF_T_REL))
679 /* The type of the data better should match. */
680 @@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
683 /* Check whether we have to resize the data buffer. */
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))
688 __libelf_seterrno (ELF_E_INVALID_INDEX);
690 @@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
693 /* Check whether we have to resize the data buffer. */
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))
698 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
706 - if (unlikely (ndx < 0))
708 - __libelf_seterrno (ELF_E_INVALID_INDEX);
712 if (unlikely (data_scn->d.d_type != ELF_T_SYM))
714 /* The type of the data better should match. */
715 @@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
718 /* Check whether we have to resize the data buffer. */
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))
723 __libelf_seterrno (ELF_E_INVALID_INDEX);
725 @@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
728 /* Check whether we have to resize the data buffer. */
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))
733 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
741 - if (unlikely (ndx < 0))
743 - __libelf_seterrno (ELF_E_INVALID_INDEX);
747 if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
749 /* The type of the data better should match. */
750 @@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
751 rwlock_wrlock (scn->elf->lock);
753 /* Check whether we have to resize the data buffer. */
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))
758 __libelf_seterrno (ELF_E_INVALID_INDEX);
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
766 - if (unlikely (ndx < 0))
768 - __libelf_seterrno (ELF_E_INVALID_INDEX);
772 if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
774 /* The type of the data better should match. */
775 @@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
778 /* Check whether we have to resize the data buffer. */
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))
783 __libelf_seterrno (ELF_E_INVALID_INDEX);
785 @@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
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))
793 __libelf_seterrno (ELF_E_INVALID_INDEX);
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)
798 assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
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))
805 __libelf_seterrno (ELF_E_INVALID_INDEX);
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
812 +/* Convenience macro. Assumes int NDX and TYPE with size at least
814 +#if SIZE_MAX > 4294967295U
815 +# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
817 +# define INVALID_NDX(ndx, type) \
818 + unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
821 #endif /* libelfP.h */
822 --- elfutils-0.120/src/elflint.c
823 +++ elfutils-0.120/src/elflint.c
824 @@ -123,6 +123,9 @@ static uint32_t shstrndx;
825 /* Array to count references in section groups. */
828 +/* Number of sections. */
829 +static unsigned int shnum;
833 main (int argc, char *argv[])
834 @@ -312,10 +315,19 @@ section_name (Ebl *ebl, int idx)
840 + if ((unsigned int) idx > shnum)
841 + return "<invalid>";
843 shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
845 + return "<invalid>";
847 - return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
848 + ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
850 + return "<invalid>";
855 @@ -337,10 +349,6 @@ static const int valid_e_machine[] =
856 (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
859 -/* Number of sections. */
860 -static unsigned int shnum;
864 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
866 @@ -603,7 +611,8 @@ section [%2d] '%s': symbol table cannot
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)
874 section [%2zu] '%s': entry size is does not match ElfXX_Sym\n"),
875 cnt, section_name (ebl, cnt));
876 @@ -641,7 +650,7 @@ section [%2d] '%s': XINDEX for zeroth en
877 xndxscnidx, section_name (ebl, xndxscnidx));
880 - for (cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
881 + for (cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
883 sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
885 @@ -659,7 +668,8 @@ section [%2d] '%s': symbol %zu: invalid
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);
894 if (sym->st_shndx == SHN_XINDEX)
895 @@ -970,7 +980,7 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
896 const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
897 assert (rcshdr != NULL);
899 - if (rcshdr->sh_type == SHT_DYNAMIC)
900 + if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
902 /* Found the dynamic section. Look through it. */
903 Elf_Data *d = elf_getdata (scn, NULL);
904 @@ -980,14 +990,17 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
907 GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
908 - assert (dyn != NULL);
913 if (dyn->d_tag == DT_RELCOUNT)
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)
922 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
923 idx, section_name (ebl, idx),
924 @@ -1062,7 +1075,8 @@ section [%2d] '%s': no relocations for m
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);
930 + if (shdr->sh_entsize != sh_entsize)
931 ERROR (gettext (reltype == ELF_T_RELA ? "\
932 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
933 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
934 @@ -1280,7 +1294,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
935 Elf_Data *symdata = elf_getdata (symscn, NULL);
936 enum load_state state = state_undecided;
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)
943 GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
944 @@ -1330,7 +1345,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
945 Elf_Data *symdata = elf_getdata (symscn, NULL);
946 enum load_state state = state_undecided;
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)
953 GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
954 @@ -1432,7 +1448,8 @@ section [%2d] '%s': referenced as string
955 shdr->sh_link, section_name (ebl, shdr->sh_link),
956 idx, section_name (ebl, idx));
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)
962 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
963 idx, section_name (ebl, idx));
964 @@ -1442,7 +1459,7 @@ section [%2d] '%s': section entry size d
965 idx, section_name (ebl, idx));
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)
972 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
973 @@ -1633,6 +1650,8 @@ section [%2d] '%s': entry size does not
974 idx, section_name (ebl, idx));
977 + && shdr->sh_entsize
978 + && symshdr->sh_entsize
979 && (shdr->sh_size / shdr->sh_entsize
980 < symshdr->sh_size / symshdr->sh_entsize))
982 @@ -1659,6 +1678,12 @@ section [%2d] '%s': extended section ind
985 Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
988 + ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
989 + idx, section_name (ebl, idx));
993 if (*((Elf32_Word *) data->d_buf) != 0)
994 ERROR (gettext ("symbol 0 should have zero extended section index\n"));
995 @@ -1739,23 +1764,30 @@ section [%2d] '%s': hash table section i
996 idx, section_name (ebl, idx), (long int) shdr->sh_size,
997 (long int) ((2 + nbucket + nchain) * shdr->sh_entsize));
999 - if (symshdr != NULL)
1000 + if (symshdr != NULL && symshdr->sh_entsize)
1002 size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
1004 + Elf32_Word *buf, *end;
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));
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)
1016 + else if (*buf++ >= symsize)
1018 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
1019 idx, section_name (ebl, idx), cnt - 2);
1021 for (; cnt < 2 + nbucket + nchain; ++cnt)
1022 - if (((Elf32_Word *) data->d_buf)[cnt] >= symsize)
1025 + else if (*buf++ >= symsize)
1027 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
1028 idx, section_name (ebl, idx), cnt - 2 - nbucket);
1029 @@ -2097,8 +2129,9 @@ section [%2d] '%s' refers in sh_link to
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))
1039 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
1040 idx, section_name (ebl, idx),
1041 @@ -3002,6 +3035,8 @@ phdr[%d]: no note entries defined for th
1044 char *notemem = gelf_rawchunk (ebl->elf, phdr->p_offset, phdr->p_filesz);
1045 + if (notemem == NULL)
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.
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
1053 Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1056 + GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1058 printf ((grpref[0] & GRP_COMDAT)
1060 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1061 @@ -970,8 +972,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
1062 data->d_size / sizeof (Elf32_Word) - 1),
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);
1072 @@ -1122,7 +1124,8 @@ static void
1073 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1075 int class = gelf_getclass (ebl->elf);
1077 + GElf_Shdr glink_mem;
1082 @@ -1137,6 +1140,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
1083 error (EXIT_FAILURE, 0,
1084 gettext ("cannot get section header string table index"));
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));
1091 printf (ngettext ("\
1092 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1094 @@ -1146,9 +1154,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn,
1095 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
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);
1104 for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
1105 @@ -1666,6 +1672,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
1106 error (EXIT_FAILURE, 0,
1107 gettext ("cannot get section header string table index"));
1109 + GElf_Shdr glink_mem;
1110 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1112 + if (glink == NULL)
1113 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1114 + elf_ndxscn (scn));
1116 /* Now we can compute the number of entries in the section. */
1117 unsigned int nsyms = data->d_size / (class == ELFCLASS32
1118 ? sizeof (Elf32_Sym)
1119 @@ -1676,15 +1689,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
1121 (unsigned int) elf_ndxscn (scn),
1122 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
1124 printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
1125 " %lu local symbols String table: [%2u] '%s'\n",
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));
1134 fputs_unlocked (class == ELFCLASS32
1136 @@ -1920,7 +1930,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
1137 error (EXIT_FAILURE, 0,
1138 gettext ("cannot get section header string table index"));
1141 + GElf_Shdr glink_mem;
1142 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1144 + if (glink == NULL)
1145 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1146 + elf_ndxscn (scn));
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",
1151 @@ -1931,9 +1947,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn,
1152 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
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));
1160 unsigned int offset = 0;
1161 for (int cnt = shdr->sh_info; --cnt >= 0; )
1162 @@ -1986,8 +2000,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
1163 error (EXIT_FAILURE, 0,
1164 gettext ("cannot get section header string table index"));
1166 + GElf_Shdr glink_mem;
1167 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1169 + if (glink == NULL)
1170 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1171 + elf_ndxscn (scn));
1173 int class = gelf_getclass (ebl->elf);
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",
1178 @@ -1999,9 +2019,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
1179 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
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));
1187 unsigned int offset = 0;
1188 for (int cnt = shdr->sh_info; --cnt >= 0; )
1189 @@ -2263,8 +2281,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
1193 + GElf_Shdr glink_mem;
1194 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
1196 + if (glink == NULL)
1197 + error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
1198 + elf_ndxscn (scn));
1200 /* Print the header. */
1202 printf (ngettext ("\
1203 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
1205 @@ -2276,9 +2300,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
1206 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
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));
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)
1216 @@ -2353,7 +2375,17 @@ handle_hash (Ebl *ebl)
1217 Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
1218 Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
1221 + GElf_Shdr glink_mem;
1222 + GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
1225 + if (glink == NULL)
1227 + error (0, 0, gettext ("invalid sh_link value in section %Zu"),
1228 + elf_ndxscn (scn));
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",
1235 @@ -2366,10 +2398,7 @@ handle_hash (Ebl *ebl)
1238 (unsigned int) shdr->sh_link,
1239 - elf_strptr (ebl->elf, shstrndx,
1240 - gelf_getshdr (elf_getscn (ebl->elf,
1242 - &glink)->sh_name));
1243 + elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1245 uint32_t *lengths = (uint32_t *) xcalloc (nbucket,
1247 @@ -3510,6 +3539,16 @@ print_debug_aranges_section (Ebl *ebl __
1251 + GElf_Shdr glink_mem;
1253 + glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1254 + if (glink == NULL)
1256 + error (0, 0, gettext ("invalid sh_link value in section %Zu"),
1257 + elf_ndxscn (scn));
1261 printf (ngettext ("\
1262 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
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
1267 Elf_Data debuglink_crc_data;
1268 bool any_symtab_changes = false;
1269 Elf_Data *shstrtab_data = NULL;
1270 + size_t shdridx = 0;
1272 /* Create the full name of the file. */
1274 @@ -542,6 +543,11 @@ handle_elf (int fd, Elf *elf, const char
1278 + if (shstrndx >= shnum)
1281 +#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
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.
1286 @@ -563,7 +569,7 @@ handle_elf (int fd, Elf *elf, const char
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);
1293 shdr_info[cnt].scn = scn;
1295 @@ -576,6 +582,7 @@ handle_elf (int fd, Elf *elf, const char
1296 shdr_info[cnt].shdr.sh_name);
1297 if (shdr_info[cnt].name == NULL)
1300 error (0, 0, gettext ("illformed file '%s'"), fname);
1303 @@ -585,6 +592,8 @@ handle_elf (int fd, Elf *elf, const char
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)
1310 /* Sections in files other than relocatable object files which
1311 are not loaded can be freely moved by us. In relocatable
1312 @@ -597,7 +606,7 @@ handle_elf (int fd, Elf *elf, const char
1313 appropriate reference. */
1314 if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
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;
1320 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
1321 @@ -614,7 +623,12 @@ handle_elf (int fd, Elf *elf, const char
1323 inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1325 - shdr_info[grpref[inner]].group_idx = cnt;
1327 + if (grpref[inner] < shnum)
1328 + shdr_info[grpref[inner]].group_idx = cnt;
1333 if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
1334 /* If the section group contains only one element and this
1335 @@ -625,7 +639,7 @@ handle_elf (int fd, Elf *elf, const char
1337 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
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;
1344 @@ -633,7 +647,7 @@ handle_elf (int fd, Elf *elf, const char
1345 discarded right away. */
1346 if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
1348 - assert (shdr_info[cnt].group_idx != 0);
1349 + elf_assert (shdr_info[cnt].group_idx != 0);
1351 if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
1353 @@ -708,10 +722,14 @@ handle_elf (int fd, Elf *elf, const char
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)
1364 + if (shdr_info[cnt].shdr.sh_info >= shnum)
1366 + else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
1367 + shdr_info[cnt].idx = 1;
1371 if (shdr_info[cnt].idx == 1)
1372 @@ -737,7 +755,7 @@ handle_elf (int fd, Elf *elf, const char
1373 if (shdr_info[cnt].symtab_idx != 0
1374 && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
1376 - assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
1377 + elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
1379 shdr_info[shdr_info[cnt].symtab_idx].data
1380 = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
1381 @@ -777,6 +795,9 @@ handle_elf (int fd, Elf *elf, const char
1382 else if (scnidx == SHN_XINDEX)
1385 + if (scnidx >= shnum)
1388 if (shdr_info[scnidx].idx == 0)
1390 /* Mark this section as used. */
1391 @@ -808,11 +829,15 @@ handle_elf (int fd, Elf *elf, const char
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))
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)
1403 + else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
1405 + shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
1406 + changes |= shdr_info[cnt].shdr.sh_info < cnt;
1410 /* Mark the section as investigated. */
1411 @@ -912,7 +937,7 @@ handle_elf (int fd, Elf *elf, const char
1412 error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
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);
1418 /* Add this name to the section header string table. */
1419 shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
1420 @@ -949,7 +974,7 @@ handle_elf (int fd, Elf *elf, const char
1421 error (EXIT_FAILURE, 0,
1422 gettext ("while create section header section: %s"),
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);
1427 shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
1428 if (shdr_info[cnt].data == NULL)
1429 @@ -980,7 +1005,7 @@ handle_elf (int fd, Elf *elf, const char
1432 /* Index of the section header table in the shdr_info array. */
1433 - size_t shdridx = cnt;
1436 /* Add the section header string table section name. */
1437 shdr_info[cnt].se = ebl_strtabadd (shst, ".shstrtab", 10);
1438 @@ -1005,7 +1030,7 @@ handle_elf (int fd, Elf *elf, const char
1439 error (EXIT_FAILURE, 0,
1440 gettext ("while create section header section: %s"),
1442 - assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
1443 + elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
1445 /* Finalize the string table and fill in the correct indices in the
1447 @@ -1095,21 +1120,21 @@ handle_elf (int fd, Elf *elf, const char
1448 shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
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);
1457 if (shdr_info[cnt].version_idx != 0)
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,
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);
1473 shdr_info[cnt].newsymidx
1474 @@ -1163,7 +1188,7 @@ handle_elf (int fd, Elf *elf, const char
1475 sec = shdr_info[sym->st_shndx].idx;
1478 - assert (shndxdata != NULL);
1479 + elf_assert (shndxdata != NULL);
1481 sec = shdr_info[xshndx].idx;
1483 @@ -1184,7 +1209,7 @@ handle_elf (int fd, Elf *elf, const char
1487 - assert (sec < SHN_LORESERVE || shndxdata != NULL);
1488 + elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
1490 if ((inner != destidx || nshndx != sym->st_shndx
1491 || (shndxdata != NULL && nxshndx != xshndx))
1492 @@ -1207,7 +1232,7 @@ handle_elf (int fd, Elf *elf, const char
1494 /* This is a section symbol for a section which has
1496 - assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
1497 + elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
1500 if (destidx != inner)
1501 @@ -1371,11 +1396,11 @@ handle_elf (int fd, Elf *elf, const char
1504 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
1505 - assert (sym != NULL);
1506 + elf_assert (sym != NULL);
1508 const char *name = elf_strptr (elf, strshndx,
1510 - assert (name != NULL);
1511 + elf_assert (name != NULL);
1512 size_t hidx = elf_hash (name) % nbucket;
1514 if (bucket[hidx] == 0)
1515 @@ -1394,8 +1419,8 @@ handle_elf (int fd, Elf *elf, const char
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));
1524 Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
1526 @@ -1428,11 +1453,11 @@ handle_elf (int fd, Elf *elf, const char
1529 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
1530 - assert (sym != NULL);
1531 + elf_assert (sym != NULL);
1533 const char *name = elf_strptr (elf, strshndx,
1535 - assert (name != NULL);
1536 + elf_assert (name != NULL);
1537 size_t hidx = elf_hash (name) % nbucket;
1539 if (bucket[hidx] == 0)