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
|
# HG changeset patch
# User bae
# Date 1361531665 -14400
# Node ID cf93d3828aa8f85003f3668f069646205c39c087
# Parent ddd9e6df700fa59366b4560e58a3b7b0a16b4746
8007617: Better validation of images
Reviewed-by: prr, jgodinez
diff --git openjdk/jdk/src/share/classes/sun/awt/image/ImageRepresentation.java openjdk/jdk/src/share/classes/sun/awt/image/ImageRepresentation.java
--- jdk/src/share/classes/sun/awt/image/ImageRepresentation.java
+++ jdk/src/share/classes/sun/awt/image/ImageRepresentation.java
@@ -333,10 +333,10 @@ public class ImageRepresentation extends
hints = h;
}
- private native void setICMpixels(int x, int y, int w, int h, int[] lut,
+ private native boolean setICMpixels(int x, int y, int w, int h, int[] lut,
byte[] pix, int off, int scansize,
IntegerComponentRaster ict);
- private native int setDiffICM(int x, int y, int w, int h, int[] lut,
+ private native boolean setDiffICM(int x, int y, int w, int h, int[] lut,
int transPix, int numLut, IndexColorModel icm,
byte[] pix, int off, int scansize,
ByteComponentRaster bct, int chanOff);
@@ -426,10 +426,10 @@ public class ImageRepresentation extends
IndexColorModel icm = (IndexColorModel) model;
ByteComponentRaster bct = (ByteComponentRaster) biRaster;
int numlut = numSrcLUT;
- if (setDiffICM(x, y, w, h, srcLUT, srcLUTtransIndex,
+ if (!setDiffICM(x, y, w, h, srcLUT, srcLUTtransIndex,
numSrcLUT, icm,
pix, off, scansize, bct,
- bct.getDataOffset(0)) == 0) {
+ bct.getDataOffset(0))) {
convertToRGB();
}
else {
@@ -470,9 +470,14 @@ public class ImageRepresentation extends
if (s_useNative) {
// Note that setICMpixels modifies the raster directly
// so we must mark it as changed afterwards
- setICMpixels(x, y, w, h, srcLUT, pix, off, scansize,
- iraster);
- iraster.markDirty();
+ if (setICMpixels(x, y, w, h, srcLUT, pix, off, scansize,
+ iraster))
+ {
+ iraster.markDirty();
+ } else {
+ abort();
+ return;
+ }
}
else {
int[] storage = new int[w*h];
diff --git openjdk/jdk/src/share/native/sun/awt/image/awt_ImageRep.c openjdk/jdk/src/share/native/sun/awt/image/awt_ImageRep.c
--- jdk/src/share/native/sun/awt/image/awt_ImageRep.c
+++ jdk/src/share/native/sun/awt/image/awt_ImageRep.c
@@ -45,6 +45,53 @@ static int findIdx(unsigned int rgb, uns
# define TRUE 1
#endif
+#define CHECK_STRIDE(yy, hh, ss) \
+ if ((ss) != 0) { \
+ int limit = 0x7fffffff / ((ss) > 0 ? (ss) : -(ss)); \
+ if (limit < (yy) || limit < ((yy) + (hh) - 1)) { \
+ /* integer oveflow */ \
+ return JNI_FALSE; \
+ } \
+ } \
+
+#define CHECK_SRC() \
+ do { \
+ int pixeloffset; \
+ if (off < 0 || off >= srcDataLength) { \
+ return JNI_FALSE; \
+ } \
+ CHECK_STRIDE(0, h, scansize); \
+ \
+ /* check scansize */ \
+ pixeloffset = scansize * (h - 1); \
+ if ((w - 1) > (0x7fffffff - pixeloffset)) { \
+ return JNI_FALSE; \
+ } \
+ pixeloffset += (w - 1); \
+ \
+ if (off > (0x7fffffff - pixeloffset)) { \
+ return JNI_FALSE; \
+ } \
+ } while (0) \
+
+#define CHECK_DST(xx, yy) \
+ do { \
+ int soffset = (yy) * sStride; \
+ int poffset = (xx) * pixelStride; \
+ if (poffset > (0x7fffffff - soffset)) { \
+ return JNI_FALSE; \
+ } \
+ poffset += soffset; \
+ if (dstDataOff > (0x7fffffff - poffset)) { \
+ return JNI_FALSE; \
+ } \
+ poffset += dstDataOff; \
+ \
+ if (poffset < 0 || poffset >= dstDataLength) { \
+ return JNI_FALSE; \
+ } \
+ } while (0) \
+
static jfieldID s_JnumSrcLUTID;
static jfieldID s_JsrcLUTtransIndexID;
@@ -58,7 +105,7 @@ Java_sun_awt_image_ImageRepresentation_i
/*
* This routine is used to draw ICM pixels into a default color model
*/
-JNIEXPORT void JNICALL
+JNIEXPORT jboolean JNICALL
Java_sun_awt_image_ImageRepresentation_setICMpixels(JNIEnv *env, jclass cls,
jint x, jint y, jint w,
jint h, jintArray jlut,
@@ -67,7 +114,10 @@ Java_sun_awt_image_ImageRepresentation_s
jobject jict)
{
unsigned char *srcData = NULL;
+ jint srcDataLength;
int *dstData;
+ jint dstDataLength;
+ jint dstDataOff;
int *dstP, *dstyP;
unsigned char *srcyP, *srcP;
int *srcLUT = NULL;
@@ -80,12 +130,20 @@ Java_sun_awt_image_ImageRepresentation_s
if (JNU_IsNull(env, jlut)) {
JNU_ThrowNullPointerException(env, "NullPointerException");
- return;
+ return JNI_FALSE;
}
if (JNU_IsNull(env, jpix)) {
JNU_ThrowNullPointerException(env, "NullPointerException");
- return;
+ return JNI_FALSE;
+ }
+
+ if (x < 0 || w < 1 || (0x7fffffff - x) < w) {
+ return JNI_FALSE;
+ }
+
+ if (y < 0 || h < 1 || (0x7fffffff - y) < h) {
+ return JNI_FALSE;
}
sStride = (*env)->GetIntField(env, jict, g_ICRscanstrID);
@@ -93,10 +151,47 @@ Java_sun_awt_image_ImageRepresentation_s
joffs = (*env)->GetObjectField(env, jict, g_ICRdataOffsetsID);
jdata = (*env)->GetObjectField(env, jict, g_ICRdataID);
+ if (JNU_IsNull(env, jdata)) {
+ /* no destination buffer */
+ return JNI_FALSE;
+ }
+
+ if (JNU_IsNull(env, joffs) || (*env)->GetArrayLength(env, joffs) < 1) {
+ /* invalid data offstes in raster */
+ return JNI_FALSE;
+ }
+
+ srcDataLength = (*env)->GetArrayLength(env, jpix);
+ dstDataLength = (*env)->GetArrayLength(env, jdata);
+
+ cOffs = (int *) (*env)->GetPrimitiveArrayCritical(env, joffs, NULL);
+ if (cOffs == NULL) {
+ JNU_ThrowNullPointerException(env, "Null channel offset array");
+ return JNI_FALSE;
+ }
+
+ dstDataOff = cOffs[0];
+
+ /* the offset array is not needed anymore and can be released */
+ (*env)->ReleasePrimitiveArrayCritical(env, joffs, cOffs, JNI_ABORT);
+ joffs = NULL;
+ cOffs = NULL;
+
+ /* do basic validation: make sure that offsets for
+ * first pixel and for last pixel are safe to calculate and use */
+ CHECK_STRIDE(y, h, sStride);
+ CHECK_STRIDE(x, w, pixelStride);
+
+ CHECK_DST(x, y);
+ CHECK_DST(x + w -1, y + h - 1);
+
+ /* check source array */
+ CHECK_SRC();
+
srcLUT = (int *) (*env)->GetPrimitiveArrayCritical(env, jlut, NULL);
if (srcLUT == NULL) {
JNU_ThrowNullPointerException(env, "Null IndexColorModel LUT");
- return;
+ return JNI_FALSE;
}
srcData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jpix,
@@ -104,27 +199,18 @@ Java_sun_awt_image_ImageRepresentation_s
if (srcData == NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
JNU_ThrowNullPointerException(env, "Null data array");
- return;
- }
-
- cOffs = (int *) (*env)->GetPrimitiveArrayCritical(env, joffs, NULL);
- if (cOffs == NULL) {
- (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
- (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
- JNU_ThrowNullPointerException(env, "Null channel offset array");
- return;
+ return JNI_FALSE;
}
dstData = (int *) (*env)->GetPrimitiveArrayCritical(env, jdata, NULL);
if (dstData == NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
(*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
- (*env)->ReleasePrimitiveArrayCritical(env, joffs, cOffs, JNI_ABORT);
JNU_ThrowNullPointerException(env, "Null tile data array");
- return;
+ return JNI_FALSE;
}
- dstyP = dstData + cOffs[0] + y*sStride + x*pixelStride;
+ dstyP = dstData + dstDataOff + y*sStride + x*pixelStride;
srcyP = srcData + off;
for (yIdx = 0; yIdx < h; yIdx++, srcyP += scansize, dstyP+=sStride) {
srcP = srcyP;
@@ -137,12 +223,12 @@ Java_sun_awt_image_ImageRepresentation_s
/* Release the locked arrays */
(*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
(*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
- (*env)->ReleasePrimitiveArrayCritical(env, joffs, cOffs, JNI_ABORT);
(*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
+ return JNI_TRUE;
}
-JNIEXPORT jint JNICALL
+JNIEXPORT jboolean JNICALL
Java_sun_awt_image_ImageRepresentation_setDiffICM(JNIEnv *env, jclass cls,
jint x, jint y, jint w,
jint h, jintArray jlut,
@@ -150,7 +236,7 @@ Java_sun_awt_image_ImageRepresentation_s
jobject jicm,
jbyteArray jpix, jint off,
jint scansize,
- jobject jbct, jint chanOff)
+ jobject jbct, jint dstDataOff)
{
unsigned int *srcLUT = NULL;
unsigned int *newLUT = NULL;
@@ -159,6 +245,8 @@ Java_sun_awt_image_ImageRepresentation_s
int mapSize;
jobject jdata = NULL;
jobject jnewlut = NULL;
+ jint srcDataLength;
+ jint dstDataLength;
unsigned char *srcData;
unsigned char *dstData;
unsigned char *dataP;
@@ -174,13 +262,22 @@ Java_sun_awt_image_ImageRepresentation_s
if (JNU_IsNull(env, jlut)) {
JNU_ThrowNullPointerException(env, "NullPointerException");
- return 0;
+ return JNI_FALSE;
}
if (JNU_IsNull(env, jpix)) {
JNU_ThrowNullPointerException(env, "NullPointerException");
- return 0;
+ return JNI_FALSE;
}
+
+ if (x < 0 || w < 1 || (0x7fffffff - x) < w) {
+ return JNI_FALSE;
+ }
+
+ if (y < 0 || h < 1 || (0x7fffffff - y) < h) {
+ return JNI_FALSE;
+ }
+
sStride = (*env)->GetIntField(env, jbct, g_BCRscanstrID);
pixelStride =(*env)->GetIntField(env, jbct, g_BCRpixstrID);
@@ -193,14 +290,31 @@ Java_sun_awt_image_ImageRepresentation_s
of byte data type, so we have to convert the image data
to default representation.
*/
- return 0;
+ return JNI_FALSE;
}
+ if (JNU_IsNull(env, jdata)) {
+ /* no destination buffer */
+ return JNI_FALSE;
+ }
+
+ srcDataLength = (*env)->GetArrayLength(env, jpix);
+ dstDataLength = (*env)->GetArrayLength(env, jdata);
+
+ CHECK_STRIDE(y, h, sStride);
+ CHECK_STRIDE(x, w, pixelStride);
+
+ CHECK_DST(x, y);
+ CHECK_DST(x + w -1, y + h - 1);
+
+ /* check source array */
+ CHECK_SRC();
+
srcLUT = (unsigned int *) (*env)->GetPrimitiveArrayCritical(env, jlut,
NULL);
if (srcLUT == NULL) {
/* out of memory error already thrown */
- return 0;
+ return JNI_FALSE;
}
newLUT = (unsigned int *) (*env)->GetPrimitiveArrayCritical(env, jnewlut,
@@ -208,7 +323,7 @@ Java_sun_awt_image_ImageRepresentation_s
(*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,
JNI_ABORT);
/* out of memory error already thrown */
- return 0;
+ return JNI_FALSE;
}
newNumLut = numLut;
@@ -219,7 +334,7 @@ Java_sun_awt_image_ImageRepresentation_s
(*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,
JNI_ABORT);
(*env)->ReleasePrimitiveArrayCritical(env, jnewlut, newLUT, JNI_ABORT);
- return 0;
+ return JNI_FALSE;
}
/* Don't need these any more */
@@ -239,7 +354,7 @@ Java_sun_awt_image_ImageRepresentation_s
NULL);
if (srcData == NULL) {
/* out of memory error already thrown */
- return 0;
+ return JNI_FALSE;
}
dstData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jdata,
@@ -247,10 +362,10 @@ Java_sun_awt_image_ImageRepresentation_s
if (dstData == NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
/* out of memory error already thrown */
- return 0;
+ return JNI_FALSE;
}
- ydataP = dstData + chanOff + y*sStride + x*pixelStride;
+ ydataP = dstData + dstDataOff + y*sStride + x*pixelStride;
ypixP = srcData + off;
for (i=0; i < h; i++) {
@@ -268,7 +383,7 @@ Java_sun_awt_image_ImageRepresentation_s
(*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
(*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
- return 1;
+ return JNI_TRUE;
}
static int compareLUTs(unsigned int *lut1, int numLut1, int transIdx,
|