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

Catch images broken by AFL #975

Merged
merged 3 commits into from
Aug 8, 2017
Merged

Catch images broken by AFL #975

merged 3 commits into from
Aug 8, 2017

Conversation

szukw000
Copy link
Contributor

@rouault ,
the minimal archive is attached.
winfried
openjpeg-crashes-2017-07-30.zip

@szukw000
Copy link
Contributor Author

@rouault ,
I have called 'cmake -DWITH_ASTYLE=on ..', then called 'make', then - from the BUILD directory -
called '../ scripts/prepare-commit.sh'.
winfried

@rouault rouault mentioned this pull request Jul 31, 2017
@rouault
Copy link
Collaborator

rouault commented Jul 31, 2017

I can see that there are new test failures. Do you know if they are regressions or issues that are actually fixed ?

NR-DEC-issue414.jp2-110-decode

NR-DEC-issue414.jp2-110-decode-md5

NR-DEC-issue458.jp2-120-decode

NR-DEC-issue458.jp2-120-decode-md5

@szukw000
Copy link
Contributor Author

@rouault ,
issue458.jp2: Csiz(4)

comp sign prec dx dy

0     0    5   1   1
1     0    5   1   1
2     0    5   1   1
3     0    1   1   1

issue414.jp2: Csiz(5)

comp sign prec dx  dy

0     0    8   1   1
1     0    8   1   1
2     0    8   1   1
3     0    8   1   1
4     0    8   1   1

Changed code in j2k.c, line 2341:

if (l_image->numcomps > 3 //== 4 /* RGBA */

winfried

@rouault
Copy link
Collaborator

rouault commented Jul 31, 2017

Changed code in j2k.c, line 2341:

I don't think all the checks you added in j2k.c related to color space and number of channels should be put there. The standard doesn't mention them (as far as I can see in "I.5.3.3 Colour Specification box"), so I guess we should accept images with apparently inconsistant channel number vs color space. We can reject them at the conversion stage, but in the library itself, we should probably allow such weird images, ie the color space is just a hint

@@ -711,8 +709,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
TIFFClose(tif);
return 1;
}
buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(
OPJ_INT32));
buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(width * numcomps * sizeof(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your new version, uint32 overflow might occur on both 32 and 64 bit builds. In the previous version, it would only occur on 32 bit builds. So checking for width > UINT_MAX / numcomps || width * numcomps > UINT_MAX / sizeof(OPJ_INT32) might be needed

@@ -1421,8 +1445,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
opj_image_destroy(image);
return NULL;
}
rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
rowStride = (tmsize_t)((tiWidth * tiSpp * tiBps + 7U) / 8U);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, potential uint32 overflow

@@ -577,6 +577,8 @@ int main(int argc, char *argv[])
opj_set_warning_handler(l_codec, warning_callback, 00);
opj_set_error_handler(l_codec, error_callback, 00);

parameters.dump_state = 1; /* AFL test */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFL test comment to remove and in other locations

@@ -2276,6 +2305,122 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
++l_img_comp;
}
if (!p_j2k->dump_state) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in another comment, I don't think those checks should be put in the library code

@@ -564,6 +564,7 @@ typedef struct opj_dparameters {
/* <<UniPG */

unsigned int flags;
unsigned int dump_state; /* AFL test */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will change the ABI. An alternative way would be to add a OPJ_DPARAMETERS_DUMP_FLAG 0x0002 flag (see existing OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG)

@rouault
Copy link
Collaborator

rouault commented Jul 31, 2017

(note in case you're not familiar with github & pull requests : you can add new commits to your changes-for-afl-tests branch and push it, and they will be added to this pull request)

@szukw000
Copy link
Contributor Author

szukw000 commented Aug 1, 2017 via email

@rouault
Copy link
Collaborator

rouault commented Aug 1, 2017

@szukw000 Do changes in your local tree and git commit them as usual, and then git push your branch to your github account as you did for the initial pull request. github will realize that your branch was the source of this pull request, so it will queue the new commits to it

@rouault
Copy link
Collaborator

rouault commented Aug 4, 2017

@szukw000 2 issues:

  • There's a style issue (see https://travis-ci.org/uclouvain/openjpeg/jobs/260252276) that you can fix by running "scripts/astyle.sh src/bin/jp2/converttif.c"
  • There's a compilation issue related to " error: unknown type name ‘tmsize_t’" on one target that haveOPJ_CI_PROFILE=1 and do a run with "cmake "-DCMAKE_C_FLAGS=-pg -O3" -DCMAKE_EXE_LINKER_FLAGS=-pg -DCMAKE_SHARED_LINKER_FLAGS=-pg -DBUILD_SHARED_LIBS=OFF .." ( see end of tools/travis-ci/run.sh). From the log, the system libtiff is used (instead of the one in third_party) and it must be a libtiff 3.X version that doesn't have tmsize_t. It would be good if openjpeg could still build against libtiff 3.X. It doesn't look like your change of tsize_t to tmsize_t is needed since in libtiff 4.X both types are equivalent because of "typedef tmsize_t tsize_t" in tiffio.h. See https://travis-ci.org/uclouvain/openjpeg/jobs/260252281 for full log

@szukw000
Copy link
Contributor Author

szukw000 commented Aug 6, 2017 via email

@rouault
Copy link
Collaborator

rouault commented Aug 6, 2017

I suppose there is a problem with libtiff < 4.x.y on 64-bit machines:

Not really, on libtiff 3.X, that's per design: "typedef int32 tsize_t; /* i/o size in bytes */" .

Why did your change to tmsize_t was supposed to fix ?

@szukw000
Copy link
Contributor Author

szukw000 commented Aug 6, 2017 via email

@rouault
Copy link
Collaborator

rouault commented Aug 6, 2017

But an int32 type can not contain an UINT_MAX value but an INT_MAX value (2147483647) only.

I agree, but I'm lost at what you are suggesting to do. What if we revert back to using tsize_t ? I'd say that even if there are some potential vulnerability with libtiff 3.X due to using its tsize_t, it is somehow OK. What matters is that we work fine with libtiff 4.X (IMHO)

@szukw000
Copy link
Contributor Author

szukw000 commented Aug 6, 2017 via email

@rouault
Copy link
Collaborator

rouault commented Aug 6, 2017

one could replace 'tsize_t' or 'tmsize_t' with 'int64_t'

Sounds good

@szukw000
Copy link
Contributor Author

szukw000 commented Aug 7, 2017

@rouault ,
I have now changed 'src/bin/jp2/converttif.c'. The changes look clumsy. I have tested
the file with linux32bit and linux64bit.

winfried

@rouault
Copy link
Collaborator

rouault commented Aug 7, 2017

@detonin The changes looks good to me. I let you look at them

@detonin detonin merged commit 0394f8d into uclouvain:master Aug 8, 2017
@detonin
Copy link
Contributor

detonin commented Aug 8, 2017

Looks good indeed. Merging.
@szukw000 Thanks for the contribution !

detonin added a commit that referenced this pull request Aug 9, 2017
format specifier mismatch in #975
rouault added a commit that referenced this pull request Aug 9, 2017
PR #975 introduced a check that rejects images that have different bit depth/sign
per compoment in SIZ marker if the JP2 IHDR box has BPC != 255
This didn't work properly if decoding a .j2k file since the new bit added in
opj_cp_t wasn't initialized to the right value.
For clarity, tThis new bit has also been renamed to allow_different_bit_depth_sign

But looking closer at the code, it seems we were already tolerant to inconsistencies.
For example we parsed a JP2 BPCC box even if BPC != 255 (just a warning is emitted)
So failing hard in opj_j2k_read_siz() wouldn't be very inconsistent, and that
alone cannot protect against other issues, so just emit a warning if BPC != 255
and the SIZ marker contains different bit depth/sign per component.

Note: we could also check that the content of JP2 BPCC box is consistant with the one
of the SIZ marker.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants