Browse code

binutils: fix CVE-2018-19931 and CVE-2018-1000876

Change-Id: If31b9d2a7b275790b9e3bd239aa839f8dd4a136a
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/6699
Tested-by: gerrit-photon <photon-checkins@vmware.com>
Reviewed-by: Anish Swaminathan <anishs@vmware.com>

Alexey Makhalov authored on 2019/02/14 09:16:03
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,174 @@
0
+From 3a551c7a1b80fca579461774860574eabfd7f18f Mon Sep 17 00:00:00 2001
1
+From: Alan Modra <amodra@gmail.com>
2
+Date: Sun, 16 Dec 2018 23:02:50 +1030
3
+Subject: [PATCH] PR23994, libbfd integer overflow
4
+
5
+	PR 23994
6
+	* aoutx.h: Include limits.h.
7
+	(get_reloc_upper_bound): Detect long overflow and return a file
8
+	too big error if it occurs.
9
+	* elf.c: Include limits.h.
10
+	(_bfd_elf_get_symtab_upper_bound): Detect long overflow and return
11
+	a file too big error if it occurs.
12
+	(_bfd_elf_get_dynamic_symtab_upper_bound): Likewise.
13
+	(_bfd_elf_get_dynamic_reloc_upper_bound): Likewise.
14
+---
15
+ bfd/aoutx.h   | 40 +++++++++++++++++++++-------------------
16
+ bfd/elf.c     | 32 ++++++++++++++++++++++++--------
17
+ 2 files changed, 45 insertions(+), 27 deletions(-)
18
+
19
+diff --git a/bfd/aoutx.h b/bfd/aoutx.h
20
+index 023843b..78eaa9c 100644
21
+--- a/bfd/aoutx.h
22
+@@ -117,6 +117,7 @@ DESCRIPTION
23
+ #define KEEPIT udata.i
24
+ 
25
+ #include "sysdep.h"
26
++#include <limits.h>
27
+ #include "bfd.h"
28
+ #include "safe-ctype.h"
29
+ #include "bfdlink.h"
30
+@@ -2491,6 +2492,8 @@ NAME (aout, canonicalize_reloc) (bfd *abfd,
31
+ long
32
+ NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
33
+ {
34
++  bfd_size_type count;
35
++
36
+   if (bfd_get_format (abfd) != bfd_object)
37
+     {
38
+       bfd_set_error (bfd_error_invalid_operation);
39
+@@ -2498,26 +2501,25 @@ NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
40
+     }
41
+ 
42
+   if (asect->flags & SEC_CONSTRUCTOR)
43
+-    return sizeof (arelent *) * (asect->reloc_count + 1);
44
+-
45
+-  if (asect == obj_datasec (abfd))
46
+-    return sizeof (arelent *)
47
+-      * ((exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd))
48
+-	 + 1);
49
+-
50
+-  if (asect == obj_textsec (abfd))
51
+-    return sizeof (arelent *)
52
+-      * ((exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd))
53
+-	 + 1);
54
+-
55
+-  if (asect == obj_bsssec (abfd))
56
+-    return sizeof (arelent *);
57
+-
58
+-  if (asect == obj_bsssec (abfd))
59
+-    return 0;
60
++    count = asect->reloc_count;
61
++  else if (asect == obj_datasec (abfd))
62
++    count = exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
63
++  else if (asect == obj_textsec (abfd))
64
++    count = exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
65
++  else if (asect == obj_bsssec (abfd))
66
++    count = 0;
67
++  else
68
++    {
69
++      bfd_set_error (bfd_error_invalid_operation);
70
++      return -1;
71
++    }
72
+ 
73
+-  bfd_set_error (bfd_error_invalid_operation);
74
+-  return -1;
75
++  if (count >= LONG_MAX / sizeof (arelent *))
76
++    {
77
++      bfd_set_error (bfd_error_file_too_big);
78
++      return -1;
79
++    }
80
++  return (count + 1) * sizeof (arelent *);
81
+ }
82
+ 
83
+ long
84
+diff --git a/bfd/elf.c b/bfd/elf.c
85
+index 688429b..b10dcd8 100644
86
+--- a/bfd/elf.c
87
+@@ -35,6 +35,7 @@ SECTION
88
+ /* For sparc64-cross-sparc32.  */
89
+ #define _SYSCALL32
90
+ #include "sysdep.h"
91
++#include <limits.h>
92
+ #include "bfd.h"
93
+ #include "bfdlink.h"
94
+ #include "libbfd.h"
95
+@@ -8215,11 +8216,16 @@ error_return:
96
+ long
97
+ _bfd_elf_get_symtab_upper_bound (bfd *abfd)
98
+ {
99
+-  long symcount;
100
++  bfd_size_type symcount;
101
+   long symtab_size;
102
+   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
103
+ 
104
+   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
105
++  if (symcount >= LONG_MAX / sizeof (asymbol *))
106
++    {
107
++      bfd_set_error (bfd_error_file_too_big);
108
++      return -1;
109
++    }
110
+   symtab_size = (symcount + 1) * (sizeof (asymbol *));
111
+   if (symcount > 0)
112
+     symtab_size -= sizeof (asymbol *);
113
+@@ -8230,7 +8236,7 @@ _bfd_elf_get_symtab_upper_bound (bfd *abfd)
114
+ long
115
+ _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
116
+ {
117
+-  long symcount;
118
++  bfd_size_type symcount;
119
+   long symtab_size;
120
+   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
121
+ 
122
+@@ -8241,6 +8247,11 @@ _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
123
+     }
124
+ 
125
+   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
126
++  if (symcount >= LONG_MAX / sizeof (asymbol *))
127
++    {
128
++      bfd_set_error (bfd_error_file_too_big);
129
++      return -1;
130
++    }
131
+   symtab_size = (symcount + 1) * (sizeof (asymbol *));
132
+   if (symcount > 0)
133
+     symtab_size -= sizeof (asymbol *);
134
+@@ -8310,7 +8321,7 @@ _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
135
+ long
136
+ _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
137
+ {
138
+-  long ret;
139
++  bfd_size_type count;
140
+   asection *s;
141
+ 
142
+   if (elf_dynsymtab (abfd) == 0)
143
+@@ -8319,15 +8330,20 @@ _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
144
+       return -1;
145
+     }
146
+ 
147
+-  ret = sizeof (arelent *);
148
++  count = 1;
149
+   for (s = abfd->sections; s != NULL; s = s->next)
150
+     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
151
+ 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
152
+ 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
153
+-      ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
154
+-	      * sizeof (arelent *));
155
+-
156
+-  return ret;
157
++      {
158
++	count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
159
++	if (count > LONG_MAX / sizeof (arelent *))
160
++	  {
161
++	    bfd_set_error (bfd_error_file_too_big);
162
++	    return -1;
163
++	  }
164
++      }
165
++  return count * sizeof (arelent *);
166
+ }
167
+ 
168
+ /* Canonicalize the dynamic relocation entries.  Note that we return the
169
+-- 
170
+2.9.3
171
+
0 172
new file mode 100644
... ...
@@ -0,0 +1,31 @@
0
+From 5f60af5d24d181371d67534fa273dd221df20c07 Mon Sep 17 00:00:00 2001
1
+From: Nick Clifton <nickc@redhat.com>
2
+Date: Fri, 30 Nov 2018 11:45:33 +0000
3
+Subject: [PATCH] Fix a memory exhaustion bug when attempting to allocate room
4
+ for an impossible number of program headers.
5
+
6
+	* elfcode.h (elf_object_p): Check for corrupt input files with
7
+	more program headers than can actually fit in the file.
8
+---
9
+ bfd/elfcode.h | 5 +++++
10
+ 1 file changed, 5 insertions(+)
11
+
12
+diff --git a/bfd/elfcode.h b/bfd/elfcode.h
13
+index f224c8b..16ed8e5 100644
14
+--- a/bfd/elfcode.h
15
+@@ -784,6 +784,11 @@ elf_object_p (bfd *abfd)
16
+       if (i_ehdrp->e_phnum > ((bfd_size_type) -1) / sizeof (*i_phdr))
17
+ 	goto got_wrong_format_error;
18
+ #endif
19
++      /* Check for a corrupt input file with an impossibly large number
20
++	 of program headers.  */
21
++      if (bfd_get_file_size (abfd) > 0
22
++	  && i_ehdrp->e_phnum > bfd_get_file_size (abfd))
23
++	goto got_no_match;
24
+       amt = (bfd_size_type) i_ehdrp->e_phnum * sizeof (*i_phdr);
25
+       elf_tdata (abfd)->phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
26
+       if (elf_tdata (abfd)->phdr == NULL)
27
+-- 
28
+2.9.3
29
+
... ...
@@ -1,7 +1,7 @@
1 1
 Summary:        Contains a linker, an assembler, and other tools
2 2
 Name:           binutils
3 3
 Version:        2.31
4
-Release:        2%{?dist}
4
+Release:        3%{?dist}
5 5
 License:        GPLv2+
6 6
 URL:            http://www.gnu.org/software/binutils
7 7
 Group:          System Environment/Base
... ...
@@ -13,6 +13,8 @@ Patch0:         binutils-CVE-2018-17794-18700-18701-18484.patch
13 13
 Patch1:         binutils-CVE-2018-18605.patch
14 14
 Patch2:         binutils-CVE-2018-18607.patch
15 15
 Patch3:         binutils-CVE-2018-18606.patch
16
+Patch4:         binutils-CVE-2018-19931.patch
17
+Patch5:         binutils-CVE-2018-1000876.patch
16 18
 
17 19
 %description
18 20
 The Binutils package contains a linker, an assembler,
... ...
@@ -31,6 +33,8 @@ for handling compiled objects.
31 31
 %patch1 -p1
32 32
 %patch2 -p1
33 33
 %patch3 -p1
34
+%patch4 -p1
35
+%patch5 -p1
34 36
 
35 37
 %build
36 38
 install -vdm 755 ../binutils-build
... ...
@@ -119,6 +123,8 @@ make %{?_smp_mflags} check
119 119
 %{_libdir}/libopcodes.so
120 120
 
121 121
 %changelog
122
+*   Wed Feb 13 2019 Alexey Makhalov <amakhalov@vmware.com> 2.31-3
123
+-   Fix CVE-2018-19931 and CVE-2018-1000876
122 124
 *   Wed Jan 02 2019 Ankit Jain <ankitja@vmware.com> 2.31-2
123 125
 -   Fixes for CVE-2018-17794, CVE-2018-18700, CVE-2018-18701
124 126
 -   CVE-2018-18484, CVE-2018-18605, CVE-2018-18606, CVE-2018-18607