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