Skip to content

Commit 564bc47

Browse files
committed
tiff: reject IFDs whose data is longer than int.
Fixes golang/go#10596 Change-Id: Ib5035569e84c67868c7f278281620f6c9b11b470 Reviewed-on: https://go-review.googlesource.com/9378 Reviewed-by: Nigel Tao <[email protected]>
1 parent d0d57ad commit 564bc47

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

tiff/reader.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"image/color"
1616
"io"
1717
"io/ioutil"
18+
"math"
1819

1920
"golang.org/x/image/tiff/lzw"
2021
)
@@ -72,6 +73,9 @@ func (d *decoder) ifdUint(p []byte) (u []uint, err error) {
7273
var raw []byte
7374
datatype := d.byteOrder.Uint16(p[2:4])
7475
count := d.byteOrder.Uint32(p[4:8])
76+
if count > math.MaxInt32/lengths[datatype] {
77+
return nil, FormatError("IFD data too large")
78+
}
7579
if datalen := lengths[datatype] * count; datalen > 4 {
7680
// The IFD contains a pointer to the real value.
7781
raw = make([]byte, datalen)

tiff/reader_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ func TestZeroSizedImages(t *testing.T) {
214214
}
215215
}
216216

217+
// TestLargeIFDEntry verifies that a large IFD entry does not cause Decode
218+
// to panic.
219+
// Issue 10596.
220+
func TestLargeIFDEntry(t *testing.T) {
221+
testdata := "II*\x00\x08\x00\x00\x00\f\x000000000000" +
222+
"00000000000000000000" +
223+
"00000000000000000000" +
224+
"00000000000000000000" +
225+
"00000000000000\x17\x01\x04\x00\x01\x00" +
226+
"\x00\xc0000000000000000000" +
227+
"00000000000000000000" +
228+
"00000000000000000000" +
229+
"000000"
230+
_, err := Decode(strings.NewReader(testdata))
231+
if err == nil {
232+
t.Fatal("Decode with large IFD entry: got nil error, want non-nil")
233+
}
234+
}
235+
217236
// benchmarkDecode benchmarks the decoding of an image.
218237
func benchmarkDecode(b *testing.B, filename string) {
219238
b.StopTimer()

0 commit comments

Comments
 (0)