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 on allocation size #846

Merged
merged 2 commits into from May 14, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/pngchunk_int.cpp
Expand Up @@ -625,8 +625,12 @@ namespace Exiv2 {
const char *sp = (char*) text.pData_+1; // current byte (space pointer)
const char *eot = (char*) text.pData_+text.size_; // end of text

if (sp >= eot) {
return DataBuf();
}

// Look for newline
while (*sp != '\n' && sp < eot )
while (*sp != '\n')
{
sp++;
if ( sp == eot )
Expand All @@ -635,9 +639,12 @@ namespace Exiv2 {
}
}
sp++ ; // step over '\n'
if (sp == eot) {
return DataBuf();
}

// Look for length
while ( (*sp == '\0' || *sp == ' ' || *sp == '\n') && sp < eot )
while (*sp == '\0' || *sp == ' ' || *sp == '\n')
{
sp++;
if (sp == eot )
Expand All @@ -647,7 +654,7 @@ namespace Exiv2 {
}

const char* startOfLength = sp;
while ( ('0' <= *sp && *sp <= '9') && sp < eot)
while ('0' <= *sp && *sp <= '9')
{
sp++;
if (sp == eot )
Expand All @@ -656,8 +663,12 @@ namespace Exiv2 {
}
}
sp++ ; // step over '\n'
if (sp == eot) {
return DataBuf();
}

long length = (long) atol(startOfLength);
enforce(0 <= length && length <= (eot - sp)/2, Exiv2::kerCorruptedMetadata);

// Allocate space
if (length == 0)
Expand All @@ -682,6 +693,7 @@ namespace Exiv2 {

for (long i = 0; i < (long) nibbles; i++)
{
enforce(sp < eot, Exiv2::kerCorruptedMetadata);
while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f')
{
if (*sp == '\0')
Expand All @@ -693,6 +705,7 @@ namespace Exiv2 {
}

sp++;
enforce(sp < eot, Exiv2::kerCorruptedMetadata);
}

if (i%2 == 0)
Expand Down
Binary file added test/data/issue_845_poc.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions tests/bugfixes/github/test_issue_845.py
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-

from system_tests import CaseMeta, path


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

An unchecked allocation size causes a std::bad_alloc to
be thrown.
"""
url = "https://github.com/Exiv2/exiv2/issues/845"

filename = path("$data_path/issue_845_poc.png")
commands = ["$exiv2 $filename"]
stdout = [""]
stderr = [
"""$exiv2_exception_message $filename:
$kerCorruptedMetadata
"""]
retval = [1]