Skip to content

Commit

Permalink
Merge tag '2.1.0'
Browse files Browse the repository at this point in the history
* tag '2.1.0': (39 commits)
  TurboJPEG: Update JPEG buf ptrs on comp/xform err
  Include TJ.FLAG_LIMITSCANS in JNI header
  OSS-Fuzz: Code comment tweaks for compr. targets
  jdhuff.h: Fix ASan regression caused by 8fa7036
  cjpeg_fuzzer: Add cov for h2v2 smooth downsampling
  Huff decs: Fix/suppress more innocuous UBSan errs
  Huff dec: Fix non-deterministic output w/bad input
  OSS-Fuzz: Check img size b4 readers allocate mem
  OSS-Fuzz: More code coverage improvements
  jchuff.c: Fix MSan error
  compress_yuv_fuzzer: Minor code coverage tweak
  cjpeg.c: Code formatting tweak
  rdbmp.c: Fix more innocuous UBSan errors
  rdbmp.c/rdppm.c: Fix more innocuous UBSan errors
  OSS-Fuzz: cjpeg fuzz target
  compress_yuv_fuzzer: Use unique filename template
  OSS-Fuzz: Fix UBSan err caused by TJFLAG_FUZZING
  OSS-Fuzz: YUV encoding/compression fuzz target
  ...
  • Loading branch information
kornelski committed Jul 21, 2021
2 parents 3234698 + c23672c commit 512a7c3
Show file tree
Hide file tree
Showing 67 changed files with 1,453 additions and 329 deletions.
31 changes: 18 additions & 13 deletions CMakeLists.txt
Expand Up @@ -5,7 +5,7 @@ if(CMAKE_EXECUTABLE_SUFFIX)
endif()

project(mozjpeg C)
set(VERSION 4.0.4)
set(VERSION 4.1.0)
string(REPLACE "." ";" VERSION_TRIPLET ${VERSION})
list(GET VERSION_TRIPLET 0 VERSION_MAJOR)
list(GET VERSION_TRIPLET 1 VERSION_MINOR)
Expand Down Expand Up @@ -62,14 +62,14 @@ if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
set(CMAKE_SYSTEM_PROCESSOR ${CPU_TYPE})
endif()
elseif(CMAKE_SYSTEM_PROCESSOR_LC STREQUAL "aarch64" OR
CMAKE_SYSTEM_PROCESSOR_LC MATCHES "arm*")
CMAKE_SYSTEM_PROCESSOR_LC MATCHES "^arm")
if(BITS EQUAL 64)
set(CPU_TYPE arm64)
else()
set(CPU_TYPE arm)
endif()
elseif(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "ppc*" OR
CMAKE_SYSTEM_PROCESSOR_LC MATCHES "powerpc*")
elseif(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "^ppc" OR
CMAKE_SYSTEM_PROCESSOR_LC MATCHES "^powerpc")
set(CPU_TYPE powerpc)
else()
set(CPU_TYPE ${CMAKE_SYSTEM_PROCESSOR_LC})
Expand Down Expand Up @@ -104,7 +104,7 @@ if(WIN32)
endif()
else()
if(NOT CMAKE_INSTALL_DEFAULT_PREFIX)
set(CMAKE_INSTALL_DEFAULT_PREFIX /opt/${CMAKE_PROJECT_NAME})
set(CMAKE_INSTALL_DEFAULT_PREFIX /opt/${CMAKE_PROJECT_NAME})
endif()
endif()
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
Expand Down Expand Up @@ -194,6 +194,7 @@ option(WITH_SIMD "Include SIMD extensions, if available for this platform" TRUE)
boolean_number(WITH_SIMD)
option(WITH_TURBOJPEG "Include the TurboJPEG API library and associated test programs" TRUE)
boolean_number(WITH_TURBOJPEG)
option(WITH_FUZZ "Build fuzz targets" FALSE)

macro(report_option var desc)
if(${var})
Expand Down Expand Up @@ -752,6 +753,10 @@ add_executable(wrjpgcom wrjpgcom.c)
# TESTS
###############################################################################

if(WITH_FUZZ)
add_subdirectory(fuzz)
endif()

add_subdirectory(md5)

if(MSVC_IDE OR XCODE)
Expand All @@ -772,7 +777,7 @@ if(WITH_12BIT)
set(MD5_JPEG_440_ISLOW e25c1912e38367be505a89c410c1c2d2)
set(MD5_PPM_440_ISLOW e7d2e26288870cfcb30f3114ad01e380)
set(MD5_PPM_422M_IFAST 07737bfe8a7c1c87aaa393a0098d16b0)
set(MD5_JPEG_420_IFAST_Q100_PROG 008ab68d6ddbba04a8f01deee4e0f9f8)
set(MD5_JPEG_420_IFAST_Q100_PROG 9447cef4803d9b0f74bcf333cc710a29)
set(MD5_PPM_420_Q100_IFAST 1b3730122709f53d007255e8dfd3305e)
set(MD5_PPM_420M_Q100_IFAST 980a1a3c5bf9510022869d30b7d26566)
set(MD5_JPEG_GRAY_ISLOW 235c90707b16e2e069f37c888b2636d9)
Expand Down Expand Up @@ -820,12 +825,12 @@ else()
set(MD5_BMP_RGB_ISLOW_565D 4cfa0928ef3e6bb626d7728c924cfda4)
set(MD5_JPEG_422_IFAST_OPT 2540287b79d913f91665e660303ab2c8)
set(MD5_PPM_422_IFAST 35bd6b3f833bad23de82acea847129fa)
set(MD5_JPEG_440_ISLOW 538bc02bd4b4658fd85de6ece6cbeda6)
set(MD5_PPM_440_ISLOW 11e7eab7ef7ef3276934bb7e7b6bb377)
set(MD5_JPEG_440_ISLOW 368200a98a9d5041170a6232491522f9)
set(MD5_PPM_440_ISLOW 59d718725c83d37a0b59b7e4e355d2fb)
set(MD5_PPM_422M_IFAST 8dbc65323d62cca7c91ba02dd1cfa81d)
set(MD5_BMP_422M_IFAST_565 3294bd4d9a1f2b3d08ea6020d0db7065)
set(MD5_BMP_422M_IFAST_565D da98c9c7b6039511be4a79a878a9abc1)
set(MD5_JPEG_420_IFAST_Q100_PROG e59bb462016a8d9a748c330a3474bb55)
set(MD5_JPEG_420_IFAST_Q100_PROG 0ba15f9dab81a703505f835f9dbbac6d)
set(MD5_PPM_420_Q100_IFAST 5a732542015c278ff43635e473a8a294)
set(MD5_PPM_420M_Q100_IFAST ff692ee9323a3b424894862557c092f1)
set(MD5_JPEG_GRAY_ISLOW 72b51f894b8f4a10b3ee3066770aa38d)
Expand All @@ -851,7 +856,7 @@ else()
set(MD5_PPM_3x2_IFAST fd283664b3b49127984af0a7f118fccd)
set(MD5_JPEG_420_ISLOW_ARI e986fb0a637a8d833d96e8a6d6d84ea1)
set(MD5_JPEG_444_ISLOW_PROGARI 0a8f1c8f66e113c3cf635df0a475a617)
set(MD5_PPM_420M_IFAST_ARI 57251da28a35b46eecb7177d82d10e0e)
set(MD5_PPM_420M_IFAST_ARI 57251da28a35b46eecb7177d82d10e0e)
set(MD5_JPEG_420_ISLOW 9a68f56bc76e466aa7e52f415d0f4a5f)
set(MD5_PPM_420M_ISLOW_2_1 9f9de8c0612f8d06869b960b05abf9c9)
set(MD5_PPM_420M_ISLOW_15_8 b6875bc070720b899566cc06459b63b7)
Expand Down Expand Up @@ -1224,9 +1229,9 @@ foreach(libtype ${TEST_LIBTYPES})

if(WITH_ARITH_DEC)
# CC: RGB->YCC SAMP: h2v2 merged IDCT: ifast ENT: arith
add_bittest(djpeg 420m-ifast-ari "-fast;-skip;1,20;-ppm"
testout_420m_ifast_ari.ppm ${TESTIMAGES}/testimgari.jpg
${MD5_PPM_420M_IFAST_ARI})
add_bittest(djpeg 420m-ifast-ari "-fast;-skip;1,20;-ppm"
testout_420m_ifast_ari.ppm ${TESTIMAGES}/testimgari.jpg
${MD5_PPM_420M_IFAST_ARI})

add_bittest(jpegtran 420-islow "-revert"
testout_420_islow.jpg ${TESTIMAGES}/testimgari.jpg
Expand Down
41 changes: 36 additions & 5 deletions ChangeLog.md
@@ -1,5 +1,5 @@
2.1 post-beta
=============
2.1.0
=====

### Significant changes relative to 2.1 beta1

Expand All @@ -15,9 +15,40 @@ block smoothing algorithm to read from uninitialized memory.
encoders to generate incorrect results when using the Clang compiler with
Visual Studio.

4. Fixed a floating point exception that occurred when attempting to compress a
specially-crafted malformed GIF image with a specified image width of 0 using
cjpeg.
4. Fixed a floating point exception (CVE-2021-20205) that occurred when
attempting to compress a specially-crafted malformed GIF image with a specified
image width of 0 using cjpeg.

5. Fixed a regression introduced by 2.0 beta1[15] whereby attempting to
generate a progressive JPEG image on an SSE2-capable CPU using a scan script
containing one or more scans with lengths divisible by 32 and non-zero
successive approximation low bit positions would, under certain circumstances,
result in an error ("Missing Huffman code table entry") and an invalid JPEG
image.

6. Introduced a new flag (`TJFLAG_LIMITSCANS` in the TurboJPEG C API and
`TJ.FLAG_LIMIT_SCANS` in the TurboJPEG Java API) and a corresponding TJBench
command-line argument (`-limitscans`) that causes the TurboJPEG decompression
and transform functions/operations to return/throw an error if a progressive
JPEG image contains an unreasonably large number of scans. This allows
applications that use the TurboJPEG API to guard against an exploit of the
progressive JPEG format described in the report
["Two Issues with the JPEG Standard"](https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf).

7. The PPM reader now throws an error, rather than segfaulting (due to a buffer
overrun) or generating incorrect pixels, if an application attempts to use the
`tjLoadImage()` function to load a 16-bit binary PPM file (a binary PPM file
with a maximum value greater than 255) into a grayscale image buffer or to load
a 16-bit binary PGM file into an RGB image buffer.

8. Fixed an issue in the PPM reader that caused incorrect pixels to be
generated when using the `tjLoadImage()` function to load a 16-bit binary PPM
file into an extended RGB image buffer.

9. Fixed an issue whereby, if a JPEG buffer was automatically re-allocated by
one of the TurboJPEG compression or transform functions and an error
subsequently occurred during compression or transformation, the JPEG buffer
pointer passed by the application was not updated when the function returned.


2.0.90 (2.1 beta1)
Expand Down
6 changes: 4 additions & 2 deletions cdjpeg.h
Expand Up @@ -5,7 +5,7 @@
* Copyright (C) 1994-1997, Thomas G. Lane.
* Modified 2019 by Guido Vollbeding.
* libjpeg-turbo Modifications:
* Copyright (C) 2017, 2019, D. R. Commander.
* Copyright (C) 2017, 2019, 2021, D. R. Commander.
* mozjpeg Modifications:
* Copyright (C) 2014, Mozilla Corporation.
* For conditions of distribution and use, see the accompanying README.ijg file.
Expand Down Expand Up @@ -38,7 +38,9 @@ struct cjpeg_source_struct {

JSAMPARRAY buffer;
JDIMENSION buffer_height;

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
JDIMENSION max_pixels;
#endif
#if JPEG_RAW_READER
// For reading JPEG
JSAMPARRAY plane_pointer[4];
Expand Down
75 changes: 69 additions & 6 deletions cjpeg.c
Expand Up @@ -5,7 +5,7 @@
* Copyright (C) 1991-1998, Thomas G. Lane.
* Modified 2003-2011 by Guido Vollbeding.
* libjpeg-turbo Modifications:
* Copyright (C) 2010, 2013-2014, 2017, 2019-2020, D. R. Commander.
* Copyright (C) 2010, 2013-2014, 2017, 2019-2021, D. R. Commander.
* mozjpeg Modifications:
* Copyright (C) 2014, Mozilla Corporation.
* For conditions of distribution and use, see the accompanying README file.
Expand All @@ -28,6 +28,9 @@
* works regardless of which command line style is used.
*/

#ifdef CJPEG_FUZZER
#define JPEG_INTERNALS
#endif
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
#include "jversion.h" /* for version message */
#include "jconfigint.h"
Expand Down Expand Up @@ -157,6 +160,45 @@ boolean memdst; /* for -memdst switch */
boolean report; /* for -report switch */


#ifdef CJPEG_FUZZER

#include <setjmp.h>

struct my_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};

void my_error_exit(j_common_ptr cinfo)
{
struct my_error_mgr *myerr = (struct my_error_mgr *)cinfo->err;

longjmp(myerr->setjmp_buffer, 1);
}

static void my_emit_message(j_common_ptr cinfo, int msg_level)
{
if (msg_level < 0)
cinfo->err->num_warnings++;
}

#define HANDLE_ERROR() { \
if (cinfo.global_state > CSTATE_START) { \
if (memdst && outbuffer) \
(*cinfo.dest->term_destination) (&cinfo); \
jpeg_abort_compress(&cinfo); \
} \
jpeg_destroy_compress(&cinfo); \
if (input_file != stdin && input_file != NULL) \
fclose(input_file); \
if (memdst) \
free(outbuffer); \
return EXIT_FAILURE; \
}

#endif


LOCAL(void)
usage(void)
/* complain about bad command line */
Expand Down Expand Up @@ -662,11 +704,16 @@ int
main(int argc, char **argv)
{
struct jpeg_compress_struct cinfo;
#ifdef CJPEG_FUZZER
struct my_error_mgr myerr;
struct jpeg_error_mgr &jerr = myerr.pub;
#else
struct jpeg_error_mgr jerr;
#endif
struct cdjpeg_progress_mgr progress;
int file_index;
cjpeg_source_ptr src_mgr;
FILE *input_file;
FILE *input_file = NULL;
FILE *icc_file;
JOCTET *icc_profile = NULL;
long icc_len = 0;
Expand Down Expand Up @@ -784,14 +831,24 @@ main(int argc, char **argv)
fclose(icc_file);
}

#ifdef CJPEG_FUZZER
jerr.error_exit = my_error_exit;
jerr.emit_message = my_emit_message;
if (setjmp(myerr.setjmp_buffer))
HANDLE_ERROR()
#endif

if (report) {
start_progress_monitor((j_common_ptr)&cinfo, &progress);
start_progress_monitor((j_common_ptr)&cinfo, &progress);
progress.report = report;
}

/* Figure out the input file format, and set up to read it. */
src_mgr = select_file_type(&cinfo, input_file);
src_mgr->input_file = input_file;
#ifdef CJPEG_FUZZER
src_mgr->max_pixels = 1048576;
#endif

/* Read the input file header to obtain file size & colorspace. */
(*src_mgr->start_input) (&cinfo, src_mgr);
Expand All @@ -813,6 +870,11 @@ main(int argc, char **argv)
#endif
jpeg_stdio_dest(&cinfo, output_file);

#ifdef CJPEG_FUZZER
if (setjmp(myerr.setjmp_buffer))
HANDLE_ERROR()
#endif

/* Start compressor */
jpeg_start_compress(&cinfo, TRUE);

Expand Down Expand Up @@ -874,16 +936,17 @@ main(int argc, char **argv)
fclose(output_file);

if (report)
end_progress_monitor((j_common_ptr)&cinfo);
end_progress_monitor((j_common_ptr)&cinfo);

if (memdst) {
#ifndef CJPEG_FUZZER
fprintf(stderr, "Compressed size: %lu bytes\n", outsize);
#endif
free(outbuffer);
}

free(icc_profile);

/* All done. */
exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
return 0; /* suppress no-return-value warnings */
return (jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
}
2 changes: 1 addition & 1 deletion doc/html/annotated.html
Expand Up @@ -23,7 +23,7 @@
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">TurboJPEG
&#160;<span id="projectnumber">2.0</span>
&#160;<span id="projectnumber">2.1</span>
</div>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion doc/html/classes.html
Expand Up @@ -23,7 +23,7 @@
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">TurboJPEG
&#160;<span id="projectnumber">2.0</span>
&#160;<span id="projectnumber">2.1</span>
</div>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion doc/html/functions.html
Expand Up @@ -23,7 +23,7 @@
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">TurboJPEG
&#160;<span id="projectnumber">2.0</span>
&#160;<span id="projectnumber">2.1</span>
</div>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion doc/html/functions_vars.html
Expand Up @@ -23,7 +23,7 @@
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">TurboJPEG
&#160;<span id="projectnumber">2.0</span>
&#160;<span id="projectnumber">2.1</span>
</div>
</td>
</tr>
Expand Down
22 changes: 21 additions & 1 deletion doc/html/group___turbo_j_p_e_g.html
Expand Up @@ -23,7 +23,7 @@
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">TurboJPEG
&#160;<span id="projectnumber">2.0</span>
&#160;<span id="projectnumber">2.1</span>
</div>
</td>
</tr>
Expand Down Expand Up @@ -122,6 +122,9 @@
<tr class="memitem:ga43b426750b46190a25d34a67ef76df1b"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___turbo_j_p_e_g.html#ga43b426750b46190a25d34a67ef76df1b">TJFLAG_PROGRESSIVE</a></td></tr>
<tr class="memdesc:ga43b426750b46190a25d34a67ef76df1b"><td class="mdescLeft">&#160;</td><td class="mdescRight">Use progressive entropy coding in JPEG images generated by the compression and transform functions. <a href="group___turbo_j_p_e_g.html#ga43b426750b46190a25d34a67ef76df1b">More...</a><br /></td></tr>
<tr class="separator:ga43b426750b46190a25d34a67ef76df1b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga163e6482dc5096831feef9c79ff3f805"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___turbo_j_p_e_g.html#ga163e6482dc5096831feef9c79ff3f805">TJFLAG_LIMITSCANS</a></td></tr>
<tr class="memdesc:ga163e6482dc5096831feef9c79ff3f805"><td class="mdescLeft">&#160;</td><td class="mdescRight">Limit the number of progressive JPEG scans that the decompression and transform functions will process. <a href="group___turbo_j_p_e_g.html#ga163e6482dc5096831feef9c79ff3f805">More...</a><br /></td></tr>
<tr class="separator:ga163e6482dc5096831feef9c79ff3f805"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga79bde1b4a3e2351e00887e47781b966e"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___turbo_j_p_e_g.html#ga79bde1b4a3e2351e00887e47781b966e">TJ_NUMERR</a></td></tr>
<tr class="memdesc:ga79bde1b4a3e2351e00887e47781b966e"><td class="mdescLeft">&#160;</td><td class="mdescRight">The number of error codes. <a href="group___turbo_j_p_e_g.html#ga79bde1b4a3e2351e00887e47781b966e">More...</a><br /></td></tr>
<tr class="separator:ga79bde1b4a3e2351e00887e47781b966e"><td class="memSeparator" colspan="2">&#160;</td></tr>
Expand Down Expand Up @@ -497,6 +500,23 @@ <h2 class="memtitle"><span class="permalink"><a href="#ga4ee4506c81177a06f77e250
<p>When decompressing an image that was compressed using chrominance subsampling, use the fastest chrominance upsampling algorithm available in the underlying codec. </p>
<p>The default is to use smooth upsampling, which creates a smooth transition between neighboring chrominance components in order to reduce upsampling artifacts in the decompressed image. </p>

</div>
</div>
<a id="ga163e6482dc5096831feef9c79ff3f805"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga163e6482dc5096831feef9c79ff3f805">&#9670;&nbsp;</a></span>TJFLAG_LIMITSCANS</h2>

<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define TJFLAG_LIMITSCANS</td>
</tr>
</table>
</div><div class="memdoc">

<p>Limit the number of progressive JPEG scans that the decompression and transform functions will process. </p>
<p>If a progressive JPEG image contains an unreasonably large number of scans, then this flag will cause the decompression and transform functions to return an error. The primary purpose of this is to allow security-critical applications to guard against an exploit of the progressive JPEG format described in <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>. </p>

</div>
</div>
<a id="ga8808d403c68b62aaa58a4c1e58e98963"></a>
Expand Down

0 comments on commit 512a7c3

Please sign in to comment.