Bug 694362 - Seg faults found by fuzzing in jbig2_image_compose (jbig2_image.c:292)
Summary: Seg faults found by fuzzing in jbig2_image_compose (jbig2_image.c:292)
Status: RESOLVED FIXED
Alias: None
Product: MuPDF
Classification: Unclassified
Component: fuzzing (show other bugs)
Version: master
Hardware: PC Linux
: P4 normal
Assignee: Henry Stiles
URL:
Keywords: bountiable
Depends on:
Blocks:
 
Reported: 2013-06-19 18:41 UTC by Marcos H. Woehrmann
Modified: 2013-11-28 10:25 UTC (History)
2 users (show)

See Also:
Customer:
Word Size: ---


Attachments
log.txt (21.72 KB, text/plain)
2013-06-19 18:41 UTC, Marcos H. Woehrmann
Details
Patch jbig2dec in mupdf/thirdparty (8.83 KB, patch)
2013-11-27 12:52 UTC, Shailesh Mistry
Details | Diff
Patch for gs/jbig2dec (1.16 KB, patch)
2013-11-27 12:56 UTC, Shailesh Mistry
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Marcos H. Woehrmann 2013-06-19 18:41:22 UTC
Created attachment 9999 [details]
log.txt

Seg faults in the 64 bit build of mupdf were found by fuzzing in jbig2_image_compose (jbig2_image.c:292) while reading these files. See the attached log.txt for details.

mupdf__3324.pdf.asan.50.2585.pgmraw.200.0
Comment 1 zeniko 2013-07-05 12:58:26 UTC
See bug 694111 for a fix (might be the same issue).
Comment 2 Henry Stiles 2013-07-15 20:52:34 UTC
$100.00 bounty
Comment 3 zeniko 2013-10-07 01:54:20 UTC
I'm unable to reproduce this crash (under 32-bit Windows 7).
Comment 4 Robin Watts 2013-11-25 17:27:24 UTC
This does still occur on 64bit linux.
Comment 5 Shailesh Mistry 2013-11-27 12:52:33 UTC
Created attachment 10427 [details]
Patch jbig2dec in mupdf/thirdparty

As requested, I tested this on 64 bit windows and Linux. The crash and valgrind errors have been resolved by the attached patch. 

This patch also addresses differences between the jbig2dec folder in gs with the one in mupdf/thirdparty.
Comment 6 Shailesh Mistry 2013-11-27 12:56:11 UTC
Created attachment 10428 [details]
Patch for gs/jbig2dec

Difference were found between gs/jbig2dec and mupdf/thirdparty/jbig2dec. The attached patch addresses updates required to gs/jbig2dec to bring it inline.
Comment 7 zeniko 2013-11-27 13:59:51 UTC
(In reply to comment #6)
> Difference were found between gs/jbig2dec and mupdf/thirdparty/jbig2dec. The
> attached patch addresses updates required to gs/jbig2dec to bring it inline.

Thanks for looking into the differences between gs and mupdf/thirdparty (shouldn't they be sync'ed automatically anyway?). You're fixing these at the wrong end, though: The first two changes would reintroduce a warning with MSVC (unreachable code) and with the third change, cleanup code would be skipped.
Comment 8 Robin Watts 2013-11-28 09:16:31 UTC
MuPDF now gets its jbig2dec from the standalone jbig2dec repo. This is generated automatically from the ghostscript copy daily - hence fixes should be applied to the ghostscript one, and then the MuPDF submodule should be updated.

All of the first patches fixes were already in the gs and standalone jbig2decs, but the MuPDF submodule had not been moved on.

I've pushed the final patch back into the gs one, and that's regenerated the standalone one. As soon as Tor reviews it, I will update the submodule and the fix should be pulled into MuPDF too. This does indeed seem to solve the problem.

Thanks!
Comment 9 zeniko 2013-11-28 09:27:16 UTC
(In reply to comment #8)
> I've pushed the final patch back into the gs one, and that's regenerated the
> standalone one.

Did you see comment #7? That patch reintroduces two bugs which were fixed with http://git.ghostscript.com/?p=jbig2dec.git;a=commitdiff;h=b009e994db066f204f85cb7935ebd5e7a0e1cb6c . Please back it out again.
Comment 10 Robin Watts 2013-11-28 09:41:38 UTC
Fixed with:

commit 7bb202b88e012d57d73463b239dae82cbb7d457d
Author: Robin Watts <robin@peeves.(none)>
Date:   Thu Nov 28 09:15:29 2013 -0800

    Pull in the latest jbig2dec changes.

    These were the last few fixes that went back from mupdf to gs.
    This seems to solve bug 694362. Thanks to Shelly for this.
Comment 11 Robin Watts 2013-11-28 09:42:36 UTC
(In reply to comment #9)
> (In reply to comment #8)
> > I've pushed the final patch back into the gs one, and that's regenerated the
> > standalone one.
> 
> Did you see comment #7? That patch reintroduces two bugs which were fixed
> with
> http://git.ghostscript.com/?p=jbig2dec.git;a=commitdiff;
> h=b009e994db066f204f85cb7935ebd5e7a0e1cb6c . Please back it out again.

I believe that all your changes are properly in there now. I will double check again now. If I have missed something, please let me know.
Comment 12 Robin Watts 2013-11-28 09:52:15 UTC
Simon, you are right in that I have effectively backed out part of the commit you mention.

I've put back 2 'return 0;' statements, which are indeed unreachable and will cause warnings. This was wrong of me, and I will take them out again.

The other change is actually the key thing that solves this bug. In jbig2_symbol_dict.c your patch does:

@@ -950,7 +953,7 @@ jbig2_symbol_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment,
         break;
       case 2:
       default:
-       return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+       jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
            "symbol dictionary specified invalid huffman table");
        break;
     }

i.e. we no longer return after the fatal error. Putting this return back in is what solves the problem.

If this was done to solve a warning about the 'break;' being unreachable, would it not be better to leave the return in, and take the break out? This is the solution I will pursue now, unless you can tell me that this is wrong too.

Or should it maybe have a 'goto cleanup;' in there ?

Thanks!
Comment 13 zeniko 2013-11-28 10:03:21 UTC
(In reply to comment #12)
> Or should it maybe have a 'goto cleanup;' in there ?

"goto cleanup;" was what I was expecting to happen in the check right after the switch. It looks like the actual issue is that the local params variable is never properly initialized which may lead to freeing random memory during cleanup. Does

Jbig2SymbolDictParams params = { 0 };

and then replacing the added returns with either "break" or "goto cleanup" also fix the issue for you?
Comment 14 Robin Watts 2013-11-28 10:25:19 UTC
OK, humble pie time. It looks like something must have gone wrong with my testing earlier. The lack of the return here is not the determining factor on whether this file crashes or not. I must have rebuilt the wrong thing or something.

I will put the code back as it was. You already blank params via a memset call, so the additional init = { 0 } is not required.

Sorry.