Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jp2image.cpp: check size before allocation to avoid out-of-memory errors #1814

Merged
merged 3 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/jp2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ static void boxes_check(size_t b,size_t m)
<< " length: " << box.length
<< std::endl;
#endif
enforce(box.length <= sizeof(box)+io_->size()-io_->tell() , Exiv2::kerCorruptedMetadata);

if (box.length == 0) return ;

Expand Down Expand Up @@ -349,6 +350,7 @@ static void boxes_check(size_t b,size_t m)
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Exiv2::Jp2Image::readMetadata: Exif data found" << std::endl ;
#endif
enforce(box.length >= sizeof(box) + sizeof(uuid), kerCorruptedMetadata);
rawData.alloc(box.length - (sizeof(box) + sizeof(uuid)));
bufRead = io_->read(rawData.pData_, rawData.size_);
if (io_->error()) throw Error(kerFailedToReadImageData);
Expand Down Expand Up @@ -403,6 +405,7 @@ static void boxes_check(size_t b,size_t m)
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Exiv2::Jp2Image::readMetadata: Iptc data found" << std::endl;
#endif
enforce(box.length >= sizeof(box) + sizeof(uuid), kerCorruptedMetadata);
rawData.alloc(box.length - (sizeof(box) + sizeof(uuid)));
bufRead = io_->read(rawData.pData_, rawData.size_);
if (io_->error()) throw Error(kerFailedToReadImageData);
Expand All @@ -422,6 +425,7 @@ static void boxes_check(size_t b,size_t m)
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Exiv2::Jp2Image::readMetadata: Xmp data found" << std::endl;
#endif
enforce(box.length >= sizeof(box) + sizeof(uuid), kerCorruptedMetadata);
rawData.alloc(box.length - (uint32_t)(sizeof(box) + sizeof(uuid)));
bufRead = io_->read(rawData.pData_, rawData.size_);
if (io_->error()) throw Error(kerFailedToReadImageData);
Expand Down Expand Up @@ -498,7 +502,7 @@ static void boxes_check(size_t b,size_t m)
position = io_->tell();
box.length = getLong((byte*)&box.length, bigEndian);
box.type = getLong((byte*)&box.type, bigEndian);
enforce(box.length <= io_->size()-io_->tell() , Exiv2::kerCorruptedMetadata);
enforce(box.length <= sizeof(box)+io_->size()-io_->tell() , Exiv2::kerCorruptedMetadata);

if (bPrint) {
out << Internal::stringFormat("%8ld | %8ld | ", (size_t)(position - sizeof(box)),
Expand All @@ -518,7 +522,7 @@ static void boxes_check(size_t b,size_t m)
while (io_->read((byte*)&subBox, sizeof(subBox)) == sizeof(subBox) &&
io_->tell() < position + (long)box.length) // don't read beyond the box!
{
int address = io_->tell() - sizeof(subBox);
const size_t address = io_->tell() - sizeof(subBox);
subBox.length = getLong((byte*)&subBox.length, bigEndian);
subBox.type = getLong((byte*)&subBox.type, bigEndian);

Expand All @@ -538,7 +542,7 @@ static void boxes_check(size_t b,size_t m)

if (subBox.type == kJp2BoxTypeColorHeader) {
long pad = 3; // don't know why there are 3 padding bytes
enforce(data.size_ >= pad, kerCorruptedMetadata);
enforce(data.size_ >= pad + 4, kerCorruptedMetadata);
hassec marked this conversation as resolved.
Show resolved Hide resolved
if (bPrint) {
out << " | pad:";
for (int i = 0; i < 3; i++)
Expand Down Expand Up @@ -577,6 +581,7 @@ static void boxes_check(size_t b,size_t m)
}

DataBuf rawData;
enforce(box.length >= sizeof(uuid) + sizeof(box), kerCorruptedMetadata);
rawData.alloc(box.length - sizeof(uuid) - sizeof(box));
long bufRead = io_->read(rawData.pData_, rawData.size_);
if (io_->error())
Expand Down
Binary file added test/data/issue_1812_poc.jp2
Binary file not shown.
14 changes: 5 additions & 9 deletions tests/bugfixes/github/test_issue_1522.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
class issue_1522_exif_asan(metaclass=system_tests.CaseMeta):
url = "https://github.com/Exiv2/exiv2/issues/1522"
filename = "$data_path/poc_1522.jp2"
commands = ["$exiv2 $filename"
commands = ["$exiv2 -q $filename"
,"$exiv2 -pS $filename"
]
retval = [ 253,1 ]
stderr = [ """Warning: Failed to decode Exif metadata.
$filename: No Exif data found in the file
retval = [ 1,1 ]
stderr = [ """$exiv2_exception_message $filename:
$kerCorruptedMetadata
""","""$exiv2_exception_message $filename:
$kerCorruptedMetadata
"""]
stdout = ["""File name : $filename
File size : 268 Bytes
MIME type : image/jp2
Image size : 0 x 0
""","""STRUCTURE OF JPEG2000 FILE: $filename
stdout = ["","""STRUCTURE OF JPEG2000 FILE: $filename
address | length | box | data
0 | 12 | jP |
12 | 25 | uuid | Exif: .
Expand Down
24 changes: 24 additions & 0 deletions tests/bugfixes/github/test_issue_1812.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-

from system_tests import CaseMeta, path, check_no_ASAN_UBSAN_errors


class OutOfMemoryInJp2ImageReadMetadata(metaclass=CaseMeta):
"""
Regression test for the bug described in:
https://github.com/Exiv2/exiv2/issues/1812
Due to a missing bounds check, this test triggers a 4GB memory
allocation. So the test will fail with a std::bad_alloc exception
if less than 4GB is available. On Linux, you can use `ulimit -v
4000000` to reduce the available memory to slightly less than 4GB.
"""
url = "https://github.com/Exiv2/exiv2/issues/1812"

filename = path("$data_path/issue_1812_poc.jp2")
commands = ["$exiv2 $filename"]
stdout = [""]
stderr = ["""$exiv2_exception_message $filename:
$kerCorruptedMetadata
"""]
retval = [1]
9 changes: 3 additions & 6 deletions tests/bugfixes/github/test_issue_ghsa_7569_phvm_vwc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ class Jp2ImageDoWriteMetadataOutOfBoundsRead(metaclass=CaseMeta):

filename1 = path("$data_path/issue_ghsa_7569_phvm_vwc2_poc.jp2")
filename2 = path("$data_path/issue_ghsa_7569_phvm_vwc2_poc.exv")
commands = ["$exiv2 in $filename1"]
commands = ["$exiv2 -q in $filename1"]
stdout = [""]
stderr = [
"""Warning: Directory Thumbnail, entry 0x1000 has unknown Exif (TIFF) type 28928; setting type size 1.
Error: Directory Thumbnail: IFD entry 1 lies outside of the data buffer.
Warning: Directory Thumbnail, entry 0x1000 has unknown Exif (TIFF) type 28928; setting type size 1.
Error: Offset of directory Thumbnail, entry 0x1000 is out of bounds: Offset = 0x2020506a; truncating the entry
$filename1: Could not write metadata to file: $kerCorruptedMetadata
"""Exiv2 exception in insert action for file $filename1:
$kerCorruptedMetadata
"""]
retval = [1]