]> granicus.if.org Git - libjpeg-turbo/commitdiff
tjLoadImage(): Fix int overflow/segfault w/big BMP
authorDRC <information@libjpeg-turbo.org>
Wed, 2 Jan 2019 00:57:36 +0000 (18:57 -0600)
committerDRC <information@libjpeg-turbo.org>
Wed, 2 Jan 2019 02:48:09 +0000 (20:48 -0600)
Fixes #304

ChangeLog.md
turbojpeg.c

index 4d185bff1b81b86569582d459d14e1cf1f51c236..bd5e0d373bba3fecb45c6a29fb35806adb32ab13 100644 (file)
@@ -10,6 +10,10 @@ executables for macOS and iOS.  This caused a fatal error of the form
 unless `DYLD_LIBRARY_PATH` was explicitly set to the location of the
 libjpeg-turbo shared libraries.
 
+2. Fixed an integer overflow and subsequent segfault (CVE-2018-20330) that
+occurred when attempting to load a BMP file with more than 1 billion pixels
+using the `tjLoadImage()` function.
+
 
 2.0.1
 =====
index 90a9ce6a0be81955a3d73341760467d713e53747..3f7cd640677f68f1a61152a3efb1324ba5ca7b98 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C)2009-2018 D. R. Commander.  All Rights Reserved.
+ * Copyright (C)2009-2019 D. R. Commander.  All Rights Reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -1960,7 +1960,8 @@ DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
                                      int align, int *height, int *pixelFormat,
                                      int flags)
 {
-  int retval = 0, tempc, pitch;
+  int retval = 0, tempc;
+  size_t pitch;
   tjhandle handle = NULL;
   tjinstance *this;
   j_compress_ptr cinfo = NULL;
@@ -2013,7 +2014,9 @@ DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
   *pixelFormat = cs2pf[cinfo->in_color_space];
 
   pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
-  if ((dstBuf = (unsigned char *)malloc(pitch * (*height))) == NULL)
+  if ((unsigned long long)pitch * (unsigned long long)(*height) >
+      (unsigned long long)((size_t)-1) ||
+      (dstBuf = (unsigned char *)malloc(pitch * (*height))) == NULL)
     _throwg("tjLoadImage(): Memory allocation failure");
 
   if (setjmp(this->jerr.setjmp_buffer)) {