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

Add bounds-check to prevent out-of-bounds read in memcmp #1816

Merged
merged 2 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 15 additions & 8 deletions src/jpgimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,28 +957,35 @@ namespace Exiv2 {
assert(mHasLength[marker]);
assert(size >= 2); // Because this marker has a length field.
insertPos = count + 1;
} else if (skipApp1Exif == notfound && marker == app1_ && memcmp(buf.pData_ + 2, exifId_, 6) == 0) {
enforce(size >= 8, kerNoImageInInputData);
} else if (skipApp1Exif == notfound &&
marker == app1_ &&
size >= 8 && // prevent out-of-bounds read in memcmp on next line
memcmp(buf.pData_ + 2, exifId_, 6) == 0) {
skipApp1Exif = count;
++search;
rawExif.alloc(size - 8);
memcpy(rawExif.pData_, buf.pData_ + 8, size - 8);
} else if (skipApp1Xmp == notfound && marker == app1_ && memcmp(buf.pData_ + 2, xmpId_, 29) == 0) {
enforce(size >= 31, kerNoImageInInputData);
} else if (skipApp1Xmp == notfound &&
marker == app1_ &&
size >= 31 && // prevent out-of-bounds read in memcmp on next line
memcmp(buf.pData_ + 2, xmpId_, 29) == 0) {
skipApp1Xmp = count;
++search;
} else if (marker == app2_ && memcmp(buf.pData_ + 2, iccId_, 11) == 0) {
enforce(size >= 31, kerNoImageInInputData);
} else if (marker == app2_ &&
size >= 13 && // prevent out-of-bounds read in memcmp on next line
memcmp(buf.pData_ + 2, iccId_, 11) == 0) {
skipApp2Icc.push_back(count);
if (!foundIccData) {
++search;
foundIccData = true;
}
} else if (!foundCompletePsData && marker == app13_ && memcmp(buf.pData_ + 2, Photoshop::ps3Id_, 14) == 0) {
} else if (!foundCompletePsData &&
marker == app13_ &&
size >= 16 && // prevent out-of-bounds read in memcmp on next line
memcmp(buf.pData_ + 2, Photoshop::ps3Id_, 14) == 0) {
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "Found APP13 Photoshop PS3 segment\n";
#endif
enforce(size >= 16, kerNoImageInInputData);
skipApp13Ps3.push_back(count);
// Append to psBlob
append(psBlob, buf.pData_ + 16, size - 16);
Expand Down
Binary file added test/data/issue_1815_poc.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/bugfixes/github/test_issue_1815.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-

from system_tests import CaseMeta, CopyTmpFiles, path
@CopyTmpFiles("$data_path/issue_1815_poc.jpg")

class JpgImageDoWriteMetadataOutOfBoundsRead(metaclass=CaseMeta):
"""
Regression test for the bug described in:
https://github.com/Exiv2/exiv2/issues/1815
"""
url = "https://github.com/Exiv2/exiv2/issues/1815"

filename = path("$tmp_path/issue_1815_poc.jpg")
commands = ["$exiv2 rm $filename"]
stdout = [""]
stderr = [""]
retval = [0]