Skip to content

Commit b5401b9

Browse files
authored
Fix heap buffer overflow in decode_png (#7691)
1 parent 5178a2e commit b5401b9

File tree

3 files changed

+4
-0
lines changed

3 files changed

+4
-0
lines changed

test/assets/toosmall_png/heapbof.png

7 Bytes
Loading

test/test_image.py

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
DAMAGED_PNG = os.path.join(IMAGE_ROOT, "damaged_png")
3333
ENCODE_JPEG = os.path.join(IMAGE_ROOT, "encode_jpeg")
3434
INTERLACED_PNG = os.path.join(IMAGE_ROOT, "interlaced_png")
35+
TOOSMALL_PNG = os.path.join(IMAGE_ROOT, "toosmall_png")
3536
IS_WINDOWS = sys.platform in ("win32", "cygwin")
3637
PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split("."))
3738

@@ -193,6 +194,8 @@ def test_decode_png_errors():
193194
decode_png(torch.randint(3, 5, (300,), dtype=torch.uint8))
194195
with pytest.raises(RuntimeError, match="Out of bound read in decode_png"):
195196
decode_png(read_file(os.path.join(DAMAGED_PNG, "sigsegv.png")))
197+
with pytest.raises(RuntimeError, match="Content is too small for png"):
198+
decode_png(read_file(os.path.join(TOOSMALL_PNG, "heapbof.png")))
196199

197200

198201
@pytest.mark.parametrize(

torchvision/csrc/io/image/cpu/decode_png.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ torch::Tensor decode_png(
4949
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
5050
TORCH_CHECK(false, "Internal error.");
5151
}
52+
TORCH_CHECK(datap_len >= 8, "Content is too small for png!")
5253
auto is_png = !png_sig_cmp(datap, 0, 8);
5354
TORCH_CHECK(is_png, "Content is not png!")
5455

0 commit comments

Comments
 (0)