aboutsummaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/binutils/binutils/binutils_CVE-2014-8502_1.patch
blob: 9e0c9c8b3c42c37a578a9372f054470905252e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
Upstream-Status: Backport

CVE-2014-8502 supporting patch.

[YOCTO #7084]

Signed-off-by: Armin Kuster <akuster808@gmail.com>

From bf67003b4567600ed3022a439207ac8f26454f91 Mon Sep 17 00:00:00 2001
From: Nick Clifton <nickc@redhat.com>
Date: Mon, 27 Oct 2014 18:05:37 +0000
Subject: [PATCH] This fixes more seg-faults in tools like "strings" and
 "objdump" when presented with corrupt binaries.

	PR binutils/17512
	* elf.c (bfd_section_from_shdr): Detect and warn about ELF
	binaries with a group of sections linked by the string table
	indicies.
	* peXXigen.c (pe_print_edata): Detect out of range rvas and
	entry counts for the Export Address table, Name Pointer table
	 and Ordinal table.
---
 bfd/ChangeLog  |   5 ++
 bfd/elf.c      | 194 ++++++++++++++++++++++++++++++++++++++-------------------
 bfd/peXXigen.c |  18 +++++-
 3 files changed, 150 insertions(+), 67 deletions(-)

Index: binutils-2.24/bfd/elf.c
===================================================================
--- binutils-2.24.orig/bfd/elf.c
+++ binutils-2.24/bfd/elf.c
@@ -1574,38 +1574,67 @@ bfd_section_from_shdr (bfd *abfd, unsign
   Elf_Internal_Ehdr *ehdr;
   const struct elf_backend_data *bed;
   const char *name;
+  bfd_boolean ret = TRUE;
+  static bfd_boolean * sections_being_created = NULL;
+  static unsigned int nesting = 0;
 
   if (shindex >= elf_numsections (abfd))
     return FALSE;
 
+  if (++ nesting > 3)
+    {
+      /* PR17512: A corrupt ELF binary might contain a recursive group of
+	 sections, each the string indicies pointing to the next in the
+	 loop.  Detect this here, by refusing to load a section that we are
+	 already in the process of loading.  We only trigger this test if
+	 we have nested at least three sections deep as normal ELF binaries
+	 can expect to recurse at least once.  */
+      
+      if (sections_being_created == NULL)
+	{
+	  /* FIXME: It would be more efficient to attach this array to the bfd somehow.  */
+	  sections_being_created = (bfd_boolean *)
+	    bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
+	}
+      if (sections_being_created [shindex])
+	{
+	  (*_bfd_error_handler)
+	    (_("%B: warning: loop in section dependencies detected"), abfd);
+	  return FALSE;
+	}
+      sections_being_created [shindex] = TRUE;
+    }
+
   hdr = elf_elfsections (abfd)[shindex];
   ehdr = elf_elfheader (abfd);
   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
 					  hdr->sh_name);
   if (name == NULL)
-    return FALSE;
+    goto fail;
 
   bed = get_elf_backend_data (abfd);
   switch (hdr->sh_type)
     {
     case SHT_NULL:
       /* Inactive section. Throw it away.  */
-      return TRUE;
+      goto success;
 
-    case SHT_PROGBITS:	/* Normal section with contents.  */
-    case SHT_NOBITS:	/* .bss section.  */
-    case SHT_HASH:	/* .hash section.  */
-    case SHT_NOTE:	/* .note section.  */
+    case SHT_PROGBITS:		/* Normal section with contents.  */
+    case SHT_NOBITS:		/* .bss section.  */
+    case SHT_HASH:		/* .hash section.  */
+    case SHT_NOTE:		/* .note section.  */
     case SHT_INIT_ARRAY:	/* .init_array section.  */
     case SHT_FINI_ARRAY:	/* .fini_array section.  */
     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
     case SHT_GNU_HASH:		/* .gnu.hash section.  */
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      goto success;
 
     case SHT_DYNAMIC:	/* Dynamic linking information.  */
       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
-	return FALSE;
+	goto fail;
+
       if (hdr->sh_link > elf_numsections (abfd))
 	{
 	  /* PR 10478: Accept Solaris binaries with a sh_link
@@ -1619,11 +1648,11 @@ bfd_section_from_shdr (bfd *abfd, unsign
 		break;
 	      /* Otherwise fall through.  */
 	    default:
-	      return FALSE;
+	      goto fail;
 	    }
 	}
       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
-	return FALSE;
+	goto fail;
       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
 	{
 	  Elf_Internal_Shdr *dynsymhdr;
@@ -1652,24 +1681,26 @@ bfd_section_from_shdr (bfd *abfd, unsign
 		}
 	    }
 	}
-      break;
+      goto success;
 
-    case SHT_SYMTAB:		/* A symbol table */
+    case SHT_SYMTAB:		/* A symbol table.  */
       if (elf_onesymtab (abfd) == shindex)
-	return TRUE;
+	goto success;
 
       if (hdr->sh_entsize != bed->s->sizeof_sym)
-	return FALSE;
+	goto fail;
+
       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
 	{
 	  if (hdr->sh_size != 0)
-	    return FALSE;
+	    goto fail;
 	  /* Some assemblers erroneously set sh_info to one with a
 	     zero sh_size.  ld sees this as a global symbol count
 	     of (unsigned) -1.  Fix it here.  */
 	  hdr->sh_info = 0;
-	  return TRUE;
+	  goto success;
 	}
+
       BFD_ASSERT (elf_onesymtab (abfd) == 0);
       elf_onesymtab (abfd) = shindex;
       elf_tdata (abfd)->symtab_hdr = *hdr;
@@ -1686,7 +1717,7 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	  && (abfd->flags & DYNAMIC) != 0
 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
 						shindex))
-	return FALSE;
+	goto fail;
 
       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
 	 can't read symbols without that section loaded as well.  It
@@ -1712,26 +1743,29 @@ bfd_section_from_shdr (bfd *abfd, unsign
 		  break;
 	      }
 	  if (i != shindex)
-	    return bfd_section_from_shdr (abfd, i);
+	    ret = bfd_section_from_shdr (abfd, i);
 	}
-      return TRUE;
+      goto success;
 
-    case SHT_DYNSYM:		/* A dynamic symbol table */
+    case SHT_DYNSYM:		/* A dynamic symbol table.  */
       if (elf_dynsymtab (abfd) == shindex)
-	return TRUE;
+	goto success;
 
       if (hdr->sh_entsize != bed->s->sizeof_sym)
-	return FALSE;
+	goto fail;
+
       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
 	{
 	  if (hdr->sh_size != 0)
-	    return FALSE;
+	    goto fail;
+
 	  /* Some linkers erroneously set sh_info to one with a
 	     zero sh_size.  ld sees this as a global symbol count
 	     of (unsigned) -1.  Fix it here.  */
 	  hdr->sh_info = 0;
-	  return TRUE;
+	  goto success;
 	}
+
       BFD_ASSERT (elf_dynsymtab (abfd) == 0);
       elf_dynsymtab (abfd) = shindex;
       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
@@ -1740,34 +1774,38 @@ bfd_section_from_shdr (bfd *abfd, unsign
 
       /* Besides being a symbol table, we also treat this as a regular
 	 section, so that objcopy can handle it.  */
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      goto success;
 
-    case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections */
+    case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections.  */
       if (elf_symtab_shndx (abfd) == shindex)
-	return TRUE;
+	goto success;
 
       BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
       elf_symtab_shndx (abfd) = shindex;
       elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
-      return TRUE;
+      goto success;
 
-    case SHT_STRTAB:		/* A string table */
+    case SHT_STRTAB:		/* A string table.  */
       if (hdr->bfd_section != NULL)
-	return TRUE;
+	goto success;
+
       if (ehdr->e_shstrndx == shindex)
 	{
 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
-	  return TRUE;
+	  goto success;
 	}
+
       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
 	{
 	symtab_strtab:
 	  elf_tdata (abfd)->strtab_hdr = *hdr;
 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
-	  return TRUE;
+	  goto success;
 	}
+
       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
 	{
 	dynsymtab_strtab:
@@ -1776,8 +1814,9 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	  elf_elfsections (abfd)[shindex] = hdr;
 	  /* We also treat this as a regular section, so that objcopy
 	     can handle it.  */
-	  return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
-						  shindex);
+	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
+						 shindex);
+	  goto success;
 	}
 
       /* If the string table isn't one of the above, then treat it as a
@@ -1795,9 +1834,9 @@ bfd_section_from_shdr (bfd *abfd, unsign
 		{
 		  /* Prevent endless recursion on broken objects.  */
 		  if (i == shindex)
-		    return FALSE;
+		    goto fail;
 		  if (! bfd_section_from_shdr (abfd, i))
-		    return FALSE;
+		    goto fail;
 		  if (elf_onesymtab (abfd) == i)
 		    goto symtab_strtab;
 		  if (elf_dynsymtab (abfd) == i)
@@ -1805,7 +1844,8 @@ bfd_section_from_shdr (bfd *abfd, unsign
 		}
 	    }
 	}
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      goto success;
 
     case SHT_REL:
     case SHT_RELA:
@@ -1820,7 +1860,7 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	if (hdr->sh_entsize
 	    != (bfd_size_type) (hdr->sh_type == SHT_REL
 				? bed->s->sizeof_rel : bed->s->sizeof_rela))
-	  return FALSE;
+	  goto fail;
 
 	/* Check for a bogus link to avoid crashing.  */
 	if (hdr->sh_link >= num_sec)
@@ -1828,8 +1868,9 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	    ((*_bfd_error_handler)
 	     (_("%B: invalid link %lu for reloc section %s (index %u)"),
 	      abfd, hdr->sh_link, name, shindex));
-	    return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
-						    shindex);
+	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
+						   shindex);
+	    goto success;
 	  }
 
 	/* For some incomprehensible reason Oracle distributes
@@ -1870,7 +1911,7 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
-	  return FALSE;
+	  goto fail;
 
 	/* If this reloc section does not use the main symbol table we
 	   don't treat it as a reloc section.  BFD can't adequately
@@ -1885,14 +1926,18 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	    || hdr->sh_info >= num_sec
 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
-	  return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
-						  shindex);
+	  {
+	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
+						   shindex);
+	    goto success;
+	  }
 
 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
-	  return FALSE;
+	  goto fail;
+
 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
 	if (target_sect == NULL)
-	  return FALSE;
+	  goto fail;
 
 	esdt = elf_section_data (target_sect);
 	if (hdr->sh_type == SHT_RELA)
@@ -1904,7 +1949,7 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	amt = sizeof (*hdr2);
 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
 	if (hdr2 == NULL)
-	  return FALSE;
+	  goto fail;
 	*hdr2 = *hdr;
 	*p_hdr = hdr2;
 	elf_elfsections (abfd)[shindex] = hdr2;
@@ -1920,34 +1965,40 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	      target_sect->use_rela_p = 1;
 	  }
 	abfd->flags |= HAS_RELOC;
-	return TRUE;
+	goto success;
       }
 
     case SHT_GNU_verdef:
       elf_dynverdef (abfd) = shindex;
       elf_tdata (abfd)->dynverdef_hdr = *hdr;
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      goto success;
 
     case SHT_GNU_versym:
       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
-	return FALSE;
+	goto fail;
+
       elf_dynversym (abfd) = shindex;
       elf_tdata (abfd)->dynversym_hdr = *hdr;
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      goto success;
 
     case SHT_GNU_verneed:
       elf_dynverref (abfd) = shindex;
       elf_tdata (abfd)->dynverref_hdr = *hdr;
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+      goto success;
 
     case SHT_SHLIB:
-      return TRUE;
+      goto success;
 
     case SHT_GROUP:
       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
-	return FALSE;
+	goto fail;
+
       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
-	return FALSE;
+	goto fail;
+
       if (hdr->contents != NULL)
 	{
 	  Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
@@ -1973,7 +2024,7 @@ bfd_section_from_shdr (bfd *abfd, unsign
 		}
 	    }
 	}
-      break;
+      goto success;
 
     default:
       /* Possibly an attributes section.  */
@@ -1981,14 +2032,14 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	  || hdr->sh_type == bed->obj_attrs_section_type)
 	{
 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
-	    return FALSE;
+	    goto fail;
 	  _bfd_elf_parse_attributes (abfd, hdr);
-	  return TRUE;
+	  goto success;
 	}
 
       /* Check for any processor-specific section types.  */
       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
-	return TRUE;
+	goto success;
 
       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
 	{
@@ -2000,9 +2051,12 @@ bfd_section_from_shdr (bfd *abfd, unsign
 		 "specific section `%s' [0x%8x]"),
 	       abfd, name, hdr->sh_type);
 	  else
-	    /* Allow sections reserved for applications.  */
-	    return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
-						    shindex);
+	    {
+	      /* Allow sections reserved for applications.  */
+	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
+						     shindex);
+	      goto success;
+	    }
 	}
       else if (hdr->sh_type >= SHT_LOPROC
 	       && hdr->sh_type <= SHT_HIPROC)
@@ -2023,8 +2077,11 @@ bfd_section_from_shdr (bfd *abfd, unsign
 		 "`%s' [0x%8x]"),
 	       abfd, name, hdr->sh_type);
 	  else
-	    /* Otherwise it should be processed.  */
-	    return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+	    {
+	      /* Otherwise it should be processed.  */
+	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
+	      goto success;
+	    }
 	}
       else
 	/* FIXME: We should handle this section.  */
@@ -2032,10 +2089,17 @@ bfd_section_from_shdr (bfd *abfd, unsign
 	  (_("%B: don't know how to handle section `%s' [0x%8x]"),
 	   abfd, name, hdr->sh_type);
 
-      return FALSE;
+      goto fail;
     }
 
-  return TRUE;
+ fail:
+  ret = FALSE;
+ success:
+  if (sections_being_created)
+    sections_being_created [shindex] = FALSE;
+  if (-- nesting == 0)
+    sections_being_created = NULL;
+  return ret;
 }
 
 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
Index: binutils-2.24/bfd/peXXigen.c
===================================================================
--- binutils-2.24.orig/bfd/peXXigen.c
+++ binutils-2.24/bfd/peXXigen.c
@@ -1528,7 +1528,12 @@ pe_print_edata (bfd * abfd, void * vfile
 	  _("\nExport Address Table -- Ordinal Base %ld\n"),
 	  edt.base);
 
-  for (i = 0; i < edt.num_functions; ++i)
+  /* PR 17512: Handle corrupt PE binaries.  */
+  if (edt.eat_addr + (edt.num_functions * 4) - adj >= datasize)
+    fprintf (file, _("\tInvalid Export Address Table rva (0x%lx) or entry count (0x%lx)\n"),
+	     (long) edt.eat_addr,
+	     (long) edt.num_functions);
+  else for (i = 0; i < edt.num_functions; ++i)
     {
       bfd_vma eat_member = bfd_get_32 (abfd,
 				       data + edt.eat_addr + (i * 4) - adj);
@@ -1564,7 +1569,16 @@ pe_print_edata (bfd * abfd, void * vfile
   fprintf (file,
 	   _("\n[Ordinal/Name Pointer] Table\n"));
 
-  for (i = 0; i < edt.num_names; ++i)
+  /* PR 17512: Handle corrupt PE binaries.  */
+  if (edt.npt_addr + (edt.num_names * 4) - adj >= datasize)
+    fprintf (file, _("\tInvalid Name Pointer Table rva (0x%lx) or entry count (0x%lx)\n"),
+	     (long) edt.npt_addr,
+	     (long) edt.num_names);
+  else if (edt.ot_addr + (edt.num_names * 2) - adj >= datasize)
+    fprintf (file, _("\tInvalid Ordinal Table rva (0x%lx) or entry count (0x%lx)\n"),
+	     (long) edt.ot_addr,
+	     (long) edt.num_names);
+  else for (i = 0; i < edt.num_names; ++i)
     {
       bfd_vma name_ptr = bfd_get_32 (abfd,
 				    data +
Index: binutils-2.24/bfd/ChangeLog
===================================================================
--- binutils-2.24.orig/bfd/ChangeLog
+++ binutils-2.24/bfd/ChangeLog
@@ -1,8 +1,13 @@
 2014-10-27  Nick Clifton  <nickc@redhat.com>
 
        PR binutils/17512
+       * elf.c (bfd_section_from_shdr): Detect and warn about ELF
+       binaries with a group of sections linked by the string table
+       indicies.
        * peXXigen.c (_bfd_XXi_swap_aouthdr_in): Handle corrupt binaries
        with an invalid value for NumberOfRvaAndSizes.
+       (pe_print_edata): Detect out of range rvas and entry counts for
+       the Export Address table, Name Pointer table and Ordinal table.
 
        PR binutils/17510
        * elf.c (setup_group): Improve handling of corrupt group