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
|
diff -urN xzgv-0.8/ChangeLog xzgv/ChangeLog
--- xzgv-0.8/ChangeLog Tue Sep 16 15:08:42 2003
+++ ChangeLog Wed Dec 15 03:30:46 2004
@@ -1,3 +1,13 @@
+2004-11-03 Russell Marks <russell.marks@ntlworld.com>
+
+ * Added width/height limits to all native picture readers. This is
+ a crude (albeit effective) fix for heap overflow bugs - there may
+ yet be more subtle problems, but I can't really fix them until I
+ know they're there. :-) Thanks to Luke Macken for letting me know
+ about the heap overflow problems (in zgv). I suppose I should also
+ thank "infamous41md" for publishing the original advisory/exploit
+ (again for zgv), even if he didn't bother emailing me or anything.
+
2003-09-16 Russell Marks <russell.marks@ntlworld.com>
* Version 0.8.
diff -urN xzgv-0.8/src/Makefile xzgv/src/Makefile
--- xzgv-0.8/src/Makefile Tue Jan 1 05:37:45 2002
+++ src/Makefile Wed Dec 15 03:30:46 2004
@@ -84,18 +84,19 @@
logo.o: logo.c logodata.h
logoconv.o: logoconv.c
main.o: main.c backend.h readmrf.h readgif.h readpng.h readjpeg.h \
- readtiff.h resizepic.h rcfile.h filedetails.h gotodir.h updatetn.h \
- confirm.h misc.h copymove.h rename.h help.h dir_icon.xpm \
+ readtiff.h readprf.h resizepic.h rcfile.h filedetails.h gotodir.h \
+ updatetn.h confirm.h misc.h copymove.h rename.h help.h dir_icon.xpm \
dir_icon_small.xpm file_icon.xpm file_icon_small.xpm logo.h \
icon-48.xpm main.h
misc.o: misc.c misc.h
rcfile.o: rcfile.c getopt.h rcfile.h rcfile_opt.h rcfile_var.h \
rcfile_short.h
-readgif.o: readgif.c readgif.h
-readjpeg.o: readjpeg.c rcfile.h readjpeg.h
-readmrf.o: readmrf.c readmrf.h
+readgif.o: readgif.c reader.h readgif.h
+readjpeg.o: readjpeg.c rcfile.h reader.h readjpeg.h
+readmrf.o: readmrf.c reader.h readmrf.h
readpng.o: readpng.c readpng.h
-readtiff.o: readtiff.c readtiff.h
+readprf.o: readprf.c reader.h readprf.h
+readtiff.o: readtiff.c reader.h readtiff.h
rename.o: rename.c backend.h main.h rename.h
resizepic.o: resizepic.c resizepic.h
updatetn.o: updatetn.c backend.h main.h rcfile.h dither.h resizepic.h \
diff -urN xzgv-0.8/src/reader.h xzgv/src/reader.h
--- xzgv-0.8/src/reader.h Thu Jan 1 01:00:00 1970
+++ src/reader.h Wed Dec 15 03:30:46 2004
@@ -0,0 +1,15 @@
+/* xzgv 0.8 - picture viewer for X, with file selector.
+ * Copyright (C) 1999-2004 Russell Marks. See main.c for license details.
+ *
+ * reader.h
+ */
+
+/* range check on width and height as a crude way of avoiding overflows
+ * when calling malloc/calloc. 32767 is the obvious limit to use given that
+ * xzgv effectively imposes such a limit anyway.
+ * Adds an extra 2 to height for max-height check, partly to reflect what
+ * the check in zgv does but also to allow for readtiff.c allocating an
+ * extra line (so at least an extra 1 would have been needed in any case).
+ */
+#define WH_MAX 32767
+#define WH_BAD(w,h) ((w)<=0 || (w)>WH_MAX || (h)<=0 || ((h)+2)>WH_MAX)
diff -urN xzgv-0.8/src/readgif.c xzgv/src/readgif.c
--- xzgv-0.8/src/readgif.c Sun Mar 3 04:34:32 2002
+++ src/readgif.c Wed Dec 15 03:30:46 2004
@@ -8,6 +8,7 @@
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
+#include "reader.h"
#include "readgif.h"
@@ -103,7 +104,7 @@
if(local_colour_map) readcolmap(in);
- if((image=malloc(width*height*3))==NULL)
+ if(WH_BAD(width,height) || (image=malloc(width*height*3))==NULL)
{
fclose(in);
return(0);
diff -urN xzgv-0.8/src/readjpeg.c xzgv/src/readjpeg.c
--- xzgv-0.8/src/readjpeg.c Tue Sep 16 12:52:04 2003
+++ src/readjpeg.c Wed Dec 15 03:30:46 2004
@@ -13,6 +13,7 @@
#include <jpeglib.h>
#include "rcfile.h"
+#include "reader.h"
#include "readjpeg.h"
@@ -265,7 +266,7 @@
/* this one shouldn't hurt */
cinfo.do_block_smoothing=FALSE;
-if((*imagep=image=malloc(width*height*3))==NULL)
+if(WH_BAD(width,height) || (*imagep=image=malloc(width*height*3))==NULL)
longjmp(jerr.setjmp_buffer,1);
jpeg_start_decompress(&cinfo);
diff -urN xzgv-0.8/src/readmrf.c xzgv/src/readmrf.c
--- xzgv-0.8/src/readmrf.c Sat Oct 7 14:26:55 2000
+++ src/readmrf.c Wed Dec 15 03:30:46 2004
@@ -7,6 +7,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include "reader.h"
#include "readmrf.h"
@@ -91,7 +92,8 @@
w64=(w+63)/64;
h64=(h+63)/64;
-if((*bmap=malloc(w*h*3))==NULL ||
+if(WH_BAD(w64*64,h64*64) || WH_BAD(w,h) ||
+ (*bmap=malloc(w*h*3))==NULL ||
(image=calloc(w64*h64*64*64,1))==NULL)
{
if(*bmap) free(*bmap),*bmap=NULL;
diff -urN xzgv-0.8/src/readpng.c xzgv/src/readpng.c
--- xzgv-0.8/src/readpng.c Thu Jul 10 16:13:43 2003
+++ src/readpng.c Wed Dec 15 03:32:46 2004
@@ -16,6 +16,7 @@
#include <stdlib.h>
#include <png.h>
#include <setjmp.h> /* after png.h to avoid horrible thing in pngconf.h */
+#include "reader.h"
#include "readpng.h"
@@ -129,7 +130,8 @@
}
/* allocate image memory */
-if((*theimageptr=theimage=malloc(width*height*3))==NULL)
+if(WH_BAD(width,height) ||
+ (*theimageptr=theimage=malloc(width*height*3))==NULL)
{
png_read_end(png_ptr,info_ptr);
png_destroy_read_struct(&png_ptr,&info_ptr,NULL);
diff -urN xzgv-0.8/src/readprf.c xzgv/src/readprf.c
--- xzgv-0.8/src/readprf.c Mon Apr 9 19:08:19 2001
+++ src/readprf.c Wed Dec 15 03:30:46 2004
@@ -7,6 +7,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include "reader.h"
#include "readprf.h"
#define squaresize 64
@@ -164,7 +165,7 @@
bytepp=1;
n=width*squaresize;
-if((planebuf[0]=calloc(n,planes))==NULL)
+if(WH_BAD(width,height) || (planebuf[0]=calloc(n,planes))==NULL)
{
fclose(in);
return(0);
@@ -173,6 +174,7 @@
for(f=1;f<planes;f++)
planebuf[f]=planebuf[f-1]+n;
+/* width/height already checked above */
if((*theimageptr=malloc(width*height*3))==NULL)
{
free(planebuf[0]);
diff -urN xzgv-0.8/src/readtiff.c xzgv/src/readtiff.c
--- xzgv-0.8/src/readtiff.c Thu Dec 28 03:20:55 2000
+++ src/readtiff.c Wed Dec 15 03:30:46 2004
@@ -11,7 +11,7 @@
#include <setjmp.h>
#include <sys/file.h> /* for open et al */
#include <tiffio.h>
-
+#include "reader.h"
#include "readtiff.h"
@@ -36,7 +36,8 @@
* spare for the flip afterwards.
*/
numpix=width*height;
-if((image=malloc(numpix*sizeof(uint32)+width*3))==NULL)
+if(WH_BAD(width,height) ||
+ (image=malloc(numpix*sizeof(uint32)+width*3))==NULL)
{
TIFFClose(in);
return(0);
|