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