Skip to content

Commit

Permalink
Fix heap buffer overflow in decode_png (#7691)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobrineli authored Jun 22, 2023
1 parent 5178a2e commit b5401b9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 0 deletions.
Binary file added test/assets/toosmall_png/heapbof.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
DAMAGED_PNG = os.path.join(IMAGE_ROOT, "damaged_png")
ENCODE_JPEG = os.path.join(IMAGE_ROOT, "encode_jpeg")
INTERLACED_PNG = os.path.join(IMAGE_ROOT, "interlaced_png")
TOOSMALL_PNG = os.path.join(IMAGE_ROOT, "toosmall_png")
IS_WINDOWS = sys.platform in ("win32", "cygwin")
PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split("."))

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


@pytest.mark.parametrize(
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/io/image/cpu/decode_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ torch::Tensor decode_png(
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
TORCH_CHECK(false, "Internal error.");
}
TORCH_CHECK(datap_len >= 8, "Content is too small for png!")
auto is_png = !png_sig_cmp(datap, 0, 8);
TORCH_CHECK(is_png, "Content is not png!")

Expand Down

0 comments on commit b5401b9

Please sign in to comment.