aboutsummaryrefslogtreecommitdiff
path: root/gnu/packages/patches/libtiff-CVE-2017-9935.patch
blob: 5685d81f6890d4c7dc07338f646507c3019eda4f (about) (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
Fix CVE-2017-9935

https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-9935
http://bugzilla.maptools.org/show_bug.cgi?id=2704

Patch copied from upstream source repository:

https://gitlab.com/libtiff/libtiff/commit/3dd8f6a357981a4090f126ab9025056c938b6940

From 3dd8f6a357981a4090f126ab9025056c938b6940 Mon Sep 17 00:00:00 2001
From: Brian May <brian@linuxpenguins.xyz>
Date: Thu, 7 Dec 2017 07:46:47 +1100
Subject: [PATCH] tiff2pdf: Fix CVE-2017-9935

Fix for http://bugzilla.maptools.org/show_bug.cgi?id=2704

This vulnerability - at least for the supplied test case - is because we
assume that a tiff will only have one transfer function that is the same
for all pages. This is not required by the TIFF standards.

We than read the transfer function for every page.  Depending on the
transfer function, we allocate either 2 or 4 bytes to the XREF buffer.
We allocate this memory after we read in the transfer function for the
page.

For the first exploit - POC1, this file has 3 pages. For the first page
we allocate 2 extra extra XREF entries. Then for the next page 2 more
entries. Then for the last page the transfer function changes and we
allocate 4 more entries.

When we read the file into memory, we assume we have 4 bytes extra for
each and every page (as per the last transfer function we read). Which
is not correct, we only have 2 bytes extra for the first 2 pages. As a
result, we end up writing past the end of the buffer.

There are also some related issues that this also fixes. For example,
TIFFGetField can return uninitalized pointer values, and the logic to
detect a N=3 vs N=1 transfer function seemed rather strange.

It is also strange that we declare the transfer functions to be of type
float, when the standard says they are unsigned 16 bit values. This is
fixed in another patch.

This patch will check to ensure that the N value for every transfer
function is the same for every page. If this changes, we abort with an
error. In theory, we should perhaps check that the transfer function
itself is identical for every page, however we don't do that due to the
confusion of the type of the data in the transfer function.
---
 libtiff/tif_dir.c |  3 +++
 tools/tiff2pdf.c  | 65 +++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
index 2ccaf448..cbf2b693 100644
--- a/libtiff/tif_dir.c
+++ b/libtiff/tif_dir.c
@@ -1065,6 +1065,9 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
 			if (td->td_samplesperpixel - td->td_extrasamples > 1) {
 				*va_arg(ap, uint16**) = td->td_transferfunction[1];
 				*va_arg(ap, uint16**) = td->td_transferfunction[2];
+			} else {
+				*va_arg(ap, uint16**) = NULL;
+				*va_arg(ap, uint16**) = NULL;
 			}
 			break;
 		case TIFFTAG_REFERENCEBLACKWHITE:
diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
index d1a9b095..c3ec0746 100644
--- a/tools/tiff2pdf.c
+++ b/tools/tiff2pdf.c
@@ -1047,6 +1047,8 @@ void t2p_read_tiff_init(T2P* t2p, TIFF* input){
 	uint16 pagen=0;
 	uint16 paged=0;
 	uint16 xuint16=0;
+	uint16 tiff_transferfunctioncount=0;
+	float* tiff_transferfunction[3];
 
 	directorycount=TIFFNumberOfDirectories(input);
 	t2p->tiff_pages = (T2P_PAGE*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,directorycount,sizeof(T2P_PAGE)));
@@ -1147,26 +1149,48 @@ void t2p_read_tiff_init(T2P* t2p, TIFF* input){
                 }
 #endif
 		if (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,
-                                 &(t2p->tiff_transferfunction[0]),
-                                 &(t2p->tiff_transferfunction[1]),
-                                 &(t2p->tiff_transferfunction[2]))) {
-			if((t2p->tiff_transferfunction[1] != (float*) NULL) &&
-                           (t2p->tiff_transferfunction[2] != (float*) NULL) &&
-                           (t2p->tiff_transferfunction[1] !=
-                            t2p->tiff_transferfunction[0])) {
-				t2p->tiff_transferfunctioncount = 3;
-				t2p->tiff_pages[i].page_extra += 4;
-				t2p->pdf_xrefcount += 4;
-			} else {
-				t2p->tiff_transferfunctioncount = 1;
-				t2p->tiff_pages[i].page_extra += 2;
-				t2p->pdf_xrefcount += 2;
-			}
-			if(t2p->pdf_minorversion < 2)
-				t2p->pdf_minorversion = 2;
+                                 &(tiff_transferfunction[0]),
+                                 &(tiff_transferfunction[1]),
+                                 &(tiff_transferfunction[2]))) {
+
+                        if((tiff_transferfunction[1] != (float*) NULL) &&
+                           (tiff_transferfunction[2] != (float*) NULL)
+                          ) {
+                            tiff_transferfunctioncount=3;
+                        } else {
+                            tiff_transferfunctioncount=1;
+                        }
                 } else {
-			t2p->tiff_transferfunctioncount=0;
+			tiff_transferfunctioncount=0;
 		}
+
+                if (i > 0){
+                    if (tiff_transferfunctioncount != t2p->tiff_transferfunctioncount){
+                        TIFFError(
+                            TIFF2PDF_MODULE,
+                            "Different transfer function on page %d",
+                            i);
+                        t2p->t2p_error = T2P_ERR_ERROR;
+                        return;
+                    }
+                }
+
+                t2p->tiff_transferfunctioncount = tiff_transferfunctioncount;
+                t2p->tiff_transferfunction[0] = tiff_transferfunction[0];
+                t2p->tiff_transferfunction[1] = tiff_transferfunction[1];
+                t2p->tiff_transferfunction[2] = tiff_transferfunction[2];
+                if(tiff_transferfunctioncount == 3){
+                        t2p->tiff_pages[i].page_extra += 4;
+                        t2p->pdf_xrefcount += 4;
+                        if(t2p->pdf_minorversion < 2)
+                                t2p->pdf_minorversion = 2;
+                } else if (tiff_transferfunctioncount == 1){
+                        t2p->tiff_pages[i].page_extra += 2;
+                        t2p->pdf_xrefcount += 2;
+                        if(t2p->pdf_minorversion < 2)
+                                t2p->pdf_minorversion = 2;
+                }
+
 		if( TIFFGetField(
 			input, 
 			TIFFTAG_ICCPROFILE, 
@@ -1828,9 +1852,8 @@ void t2p_read_tiff_data(T2P* t2p, TIFF* input){
 			 &(t2p->tiff_transferfunction[1]),
 			 &(t2p->tiff_transferfunction[2]))) {
 		if((t2p->tiff_transferfunction[1] != (float*) NULL) &&
-                   (t2p->tiff_transferfunction[2] != (float*) NULL) &&
-                   (t2p->tiff_transferfunction[1] !=
-                    t2p->tiff_transferfunction[0])) {
+                   (t2p->tiff_transferfunction[2] != (float*) NULL)
+                  ) {
 			t2p->tiff_transferfunctioncount=3;
 		} else {
 			t2p->tiff_transferfunctioncount=1;
-- 
2.16.1

s='msg-tooltip'>* gnu/packages/calendar.scm (libical, khal, remind, libhdate): Use license: prefix. Marius Bakke 2017-12-19gnu: Remove redundant cmake libdir configure flags....* gnu/packages/bioinformatics.scm (imp)[arguments]: Remove CMAKE_INSTALL_LIBDIR flag. * gnu/packages/calendar.scm (libical): Same. * gnu/packages/engineering.scm (kicad): Same. * gnu/packages/games.scm (openrct2, mgba): Same. * gnu/packages/gl.scm (virtualgl): Same. * gnu/packages/image-processing.scm (mia): Same. * gnu/packages/linux.scm (rdma-core): Same. * gnu/packages/machine-learning.scm (dlib): Same. * gnu/packages/maths.scm (lapack, superlu, z3): Same. * gnu/packages/password-utils.scm (keepassxc): Same. * gnu/packages/photo.scm (darktable): Same. * gnu/packages/rdesktop.scm (freerdp): Same. Efraim Flashner 2017-11-19Merge branch 'master' into core-updatesMarius Bakke 2017-11-18gnu: Move date/time packages from python.scm to time.scm....* gnu/packages/python.scm (python-pytz, python2-pytz, python-dateutil, python2-dateutil, python-parsedatetime, python2-parsedatetime, python-tzlocal, python-isodate, python2-isodate, python-iso8601, python2-iso8601, python-monotonic, python2-monotonic, python-pyrfc3339, python2-pyrfc3339, python-arrow, python2-arrow, python-aniso8601, python2-aniso8601): Move from here... * gnu/packages/time.scm: ...to here. * gnu/packages/calendar.scm, gnu/packages/check.scm, gnu/packages/django.scm, gnu/packages/ebook.scm, gnu/packages/gnome.scm, gnu/packages/irc.scm, gnu/packages/jrnl.scm, gnu/packages/mail.scm, gnu/packages/openstack.scm, gnu/packages/package-management.scm, gnu/packages/python-crypto.scm, gnu/packages/python-web.scm, gnu/packages/rdf.scm, gnu/packages/statistics.scm, gnu/packages/tls.scm, gnu/packages/tryton.scm, gnu/packages/version-control.scm: Adjust accordingly. Ricardo Wurmus 2017-11-17gnu: Move testing packages from python.scm to check.scm....* gnu/packages/python.scm (python-behave-web-api, python2-behave-web-api, python-mock, python2-mock, python-mock-2, python-nose, python2-nose, python-nose2, python2-nose2, python-unittest2, python2-unittest2, python-pytest, python2-pytest, python-pytest-3.0, python2-pytest-3.0, python-pytest-cov, python2-pytest-cov, python-pytest-runner, python2-pytest-runner, python-pytest-mock, python2-pytest-mock, python-pytest-xdist, python2-pytest-xdist, python-scripttest, python2-scripttest, python-testtools, python2-testtools, python-testscenarios, python2-testscenarios, python-testresources, python2-testresources, python-subunit, python2-subunit, python-fixtures, python2-fixtures, python-testrepository, python2-testrepository, python-coverage, python2-coverage, python-cov-core, python2-cov-core, python-testpath, python2-testpath, python-testlib, python2-testlib, python-pytest-cache, python2-pytest-cache, python-pytest-localserver, python-pytest-xprocess, python-pytest-subtesthack, python2-pytest-subtesthack, python-hypothesis, python2-hypothesis, python-lit, python2-lit, python-pytest-pep8, python2-pytest-pep8, python-pytest-flakes, python2-pytest-flakes, python2-coverage-test-runner, python-pylint, python2-pylint, python-paramunittest, python2-python-paramunittest, python-pytest-warnings, python2-pytest-warnings, python-pytest-capturelog, python2-pytest-capturelog, python-pytest-catchlog, python2-pytest-catchlog, python-nosexcover, python2-nosexcover, python-discover, python2-discover, behave, python-rednose, python2-rednose, python-nose-randomly, python2-nose-randomly, python-nose-timer, python2-nose-timer): Move from here... * gnu/packages/check.scm: ...to here. * gnu/packages/admin.scm, gnu/packages/android.scm, gnu/packages/backup.scm, gnu/packages/bioinformatics.scm, gnu/packages/calendar.scm, gnu/packages/dav.scm, gnu/packages/django.scm, gnu/packages/freedesktop.scm, gnu/packages/haskell.scm, gnu/packages/image.scm, gnu/packages/irc.scm, gnu/packages/jrnl.scm, gnu/packages/ldc.scm, gnu/packages/libffi.scm, gnu/packages/mail.scm, gnu/packages/mpd.scm, gnu/packages/openstack.scm, gnu/packages/package-management.scm, gnu/packages/password-utils.scm, gnu/packages/python-crypto.scm, gnu/packages/python-web.scm, gnu/packages/rdf.scm, gnu/packages/statistics.scm, gnu/packages/storage.scm, gnu/packages/time.scm, gnu/packages/tls.scm, gnu/packages/tor.scm, gnu/packages/tryton.scm: Adjust accordingly. Ricardo Wurmus 2017-10-19gnu: Remove python-pytest@2....* gnu/packages/python.scm (python-pytest): Update to 3.0.7. [source]: Move substitution to ... [arguments]: ... here. Incorporate phase from pytest-3.0. [native-inputs]: Add PYTHON-HYPOTHESIS. (python-pytest-3.0): Remove variable. (python2-pytest-3.0): Rename to ... (python2-pytest): ... this. [native-inputs]: Add PYTHON2-ENUM34. * gnu/packages/backup.scm (borg)[native-inputs]: Replace PYTHON-PYTEST-3.0 with PYTHON-PYTEST. * gnu/packages/calendar.scm (khal): Likewise. * gnu/packages/databases.scm (python-orator): Likewise. * gnu/packages/python.scm (python-sphinx-1.6, python-sphinx-1.5.3, python-pexpect, python-cryptography, python-pyopenssl, python-pytest-warnings, python-pytest-capturelog, python-pytest-catchlog, python-marshmallow, python-apispec, python-flasgger, python-pyjwt, python-grako, python-honcho): Likewise. * gnu/packages/web.scm (python-internetarchive): Likewise. Marius Bakke 2017-10-07gnu: khal: Update to 0.9.8....* gnu/packages/calendar.scm (khal): Update to 0.9.8. [arguments]: Make sure to stop building if the documentation fails to build. Skip some failing tests. Leo Famulari 2017-09-28gnu: libical: Fix libdir install path....Reported by sadiq on IRC. * gnu/packages/calendar.scm (libical)[arguments]: Remove configure flag adding /lib64 to rpath, add flag to force libdir to /lib. Efraim Flashner 2017-08-29Update e-mail for Kei Kebreau....* .mailmap: Map kei@openmailbox.org to current address. * gnu/local.mk: Replace kei@openmailbox.org to current address. * gnu/packages/backup.scm: Likewise. * gnu/packages/calendar.scm: Likewise. * gnu/packages/check.scm: Likewise. * gnu/packages/compression.scm: Likewise. * gnu/packages/documentation.scm: Likewise. * gnu/packages/emacs.scm: Likewise. * gnu/packages/fltk.scm: Likewise. * gnu/packages/freedesktop.scm: Likewise. * gnu/packages/game-development.scm: Likewise. * gnu/packages/games.scm: Likewise. * gnu/packages/gnome.scm: Likewise. * gnu/packages/gnustep.scm: Likewise. * gnu/packages/gtk.scm: Likewise. * gnu/packages/hexedit.scm: Likewise. * gnu/packages/image.scm: Likewise. * gnu/packages/maths.scm: Likewise. * gnu/packages/music.scm: Likewise. * gnu/packages/xfce.scm: Likewise. * gnu/packages/python.scm: Likewise. * gnu/packages/tcl.scm: Likewise. * gnu/packages/textutils.scm: Likewise. * gnu/packages/video.scm: Likewise. * gnu/packages/w3m.scm: Likewise. * gnu/packages/web.scm: Likewise. * gnu/packages/web-browsers.scm: Likewise. * gnu/packages/patches/wmfire-update-for-new-gdk-versions.patch: Likewise. Kei Kebreau