83be9e
Backport of the upstream commit:
83be9e
83be9e
From b35a05635e56f554870ce85f64293a3868793f69 Mon Sep 17 00:00:00 2001
83be9e
From: Michael Adams <mdadams@ece.uvic.ca>
83be9e
Date: Wed, 19 Oct 2016 08:42:25 -0700
83be9e
Subject: [PATCH] Fixed potential integer overflow problem.
83be9e
83be9e
Further enhanced by a change from d42b2388f7f8e0332c846675133acea151fc557a to
83be9e
use jas_safe_size_mul3() and an explicit check to ensure that size not only
83be9e
fits into size_t, but that it also does not exceed INT_MAX.  This is similar
83be9e
approach to what upstream used in a712a2041085e7cd5f2b153e1532ac2a2954ffaa.
83be9e
83be9e
This also adds all jas_safe_size_*() functions, including changes from the
83be9e
following upstream commits:
83be9e
83be9e
f596a0766825b48cdc07b28d2051977a382cfb95
83be9e
65536647d380571d1a9a6c91fa03775fb5bbd256
83be9e
3afacc174867cc9d1f74ef2683bc780de4b0b2df
83be9e
d42b2388f7f8e0332c846675133acea151fc557a
83be9e
83be9e
diff -pruN jasper-1.900.1.orig/src/libjasper/base/jas_image.c jasper-1.900.1/src/libjasper/base/jas_image.c
83be9e
--- jasper-1.900.1.orig/src/libjasper/base/jas_image.c	2017-03-24 16:09:34.000000000 +0100
83be9e
+++ jasper-1.900.1/src/libjasper/base/jas_image.c	2017-03-24 22:28:46.620880896 +0100
83be9e
@@ -76,6 +76,7 @@
83be9e
 #include <string.h>
83be9e
 #include <assert.h>
83be9e
 #include <ctype.h>
83be9e
+#include <limits.h>
83be9e
 
83be9e
 #include "jasper/jas_math.h"
83be9e
 #include "jasper/jas_image.h"
83be9e
@@ -307,10 +308,10 @@ static jas_image_cmpt_t *jas_image_cmpt_
83be9e
   height, uint_fast16_t depth, bool sgnd, uint_fast32_t inmem)
83be9e
 {
83be9e
 	jas_image_cmpt_t *cmpt;
83be9e
-	long size;
83be9e
+	size_t size;
83be9e
 
83be9e
 	if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) {
83be9e
-		return 0;
83be9e
+		goto error;
83be9e
 	}
83be9e
 
83be9e
 	cmpt->type_ = JAS_IMAGE_CT_UNKNOWN;
83be9e
@@ -325,11 +326,14 @@ static jas_image_cmpt_t *jas_image_cmpt_
83be9e
 	cmpt->stream_ = 0;
83be9e
 	cmpt->cps_ = (depth + 7) / 8;
83be9e
 
83be9e
-	size = cmpt->width_ * cmpt->height_ * cmpt->cps_;
83be9e
+	//size = cmpt->width_ * cmpt->height_ * cmpt->cps_;
83be9e
+	if (!jas_safe_size_mul3(cmpt->width_, cmpt->height_, cmpt->cps_, &size) ||
83be9e
+		size > INT_MAX) {
83be9e
+		goto error;
83be9e
+	}
83be9e
 	cmpt->stream_ = (inmem) ? jas_stream_memopen(0, size) : jas_stream_tmpfile();
83be9e
 	if (!cmpt->stream_) {
83be9e
-		jas_image_cmpt_destroy(cmpt);
83be9e
-		return 0;
83be9e
+		goto error;
83be9e
 	}
83be9e
 
83be9e
 	/* Zero the component data.  This isn't necessary, but it is
83be9e
@@ -337,11 +341,16 @@ static jas_image_cmpt_t *jas_image_cmpt_
83be9e
 	if (jas_stream_seek(cmpt->stream_, size - 1, SEEK_SET) < 0 ||
83be9e
 	  jas_stream_putc(cmpt->stream_, 0) == EOF ||
83be9e
 	  jas_stream_seek(cmpt->stream_, 0, SEEK_SET) < 0) {
83be9e
-		jas_image_cmpt_destroy(cmpt);
83be9e
-		return 0;
83be9e
+		goto error;
83be9e
 	}
83be9e
 
83be9e
 	return cmpt;
83be9e
+
83be9e
+error:
83be9e
+	if (cmpt) {
83be9e
+		jas_image_cmpt_destroy(cmpt);
83be9e
+	}
83be9e
+	return 0;
83be9e
 }
83be9e
 
83be9e
 static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt)
83be9e
diff -pruN jasper-1.900.1.orig/src/libjasper/include/jasper/jas_math.h jasper-1.900.1/src/libjasper/include/jasper/jas_math.h
83be9e
--- jasper-1.900.1.orig/src/libjasper/include/jasper/jas_math.h	2007-01-19 22:43:04.000000000 +0100
83be9e
+++ jasper-1.900.1/src/libjasper/include/jasper/jas_math.h	2017-03-24 22:29:36.085024105 +0100
83be9e
@@ -76,9 +76,13 @@
83be9e
 
83be9e
 #include <jasper/jas_config.h>
83be9e
 
83be9e
-#include	<assert.h>
83be9e
-#include	<stdio.h>
83be9e
-#include	<string.h>
83be9e
+#include <jasper/jas_types.h>
83be9e
+
83be9e
+#include <assert.h>
83be9e
+#include <stdio.h>
83be9e
+#include <string.h>
83be9e
+#include <stdbool.h>
83be9e
+#include <stdint.h>
83be9e
 
83be9e
 #ifdef __cplusplus
83be9e
 extern "C" {
83be9e
@@ -110,6 +114,62 @@ extern "C" {
83be9e
 #define	JAS_ONES(n) \
83be9e
   ((1 << (n)) - 1)
83be9e
 
83be9e
+/******************************************************************************\
83be9e
+* Safe integer arithmetic (i.e., with overflow checking).
83be9e
+\******************************************************************************/
83be9e
+
83be9e
+/* Compute the product of two size_t integers with overflow checking. */
83be9e
+inline static bool jas_safe_size_mul(size_t x, size_t y, size_t *result)
83be9e
+{
83be9e
+	/* Check if overflow would occur */
83be9e
+	if (x && y > SIZE_MAX / x) {
83be9e
+		/* Overflow would occur. */
83be9e
+		return false;
83be9e
+	}
83be9e
+	if (result) {
83be9e
+		*result = x * y;
83be9e
+	}
83be9e
+	return true;
83be9e
+}
83be9e
+
83be9e
+inline static bool jas_safe_size_mul3(size_t a, size_t b, size_t c,
83be9e
+  size_t *result)
83be9e
+{
83be9e
+	size_t tmp;
83be9e
+	if (!jas_safe_size_mul(a, b, &tmp) ||
83be9e
+	  !jas_safe_size_mul(tmp, c, &tmp)) {
83be9e
+		return false;
83be9e
+	}
83be9e
+	if (result) {
83be9e
+		*result = tmp;
83be9e
+	}
83be9e
+	return true;
83be9e
+}
83be9e
+
83be9e
+/* Compute the sum of two size_t integer with overflow checking. */
83be9e
+inline static bool jas_safe_size_add(size_t x, size_t y, size_t *result)
83be9e
+{
83be9e
+	if (y > SIZE_MAX - x) {
83be9e
+		return false;
83be9e
+	}
83be9e
+	if (result) {
83be9e
+		*result = x + y;
83be9e
+	}
83be9e
+	return true;
83be9e
+}
83be9e
+
83be9e
+/* Compute the difference of two size_t integer with overflow checking. */
83be9e
+inline static bool jas_safe_size_sub(size_t x, size_t y, size_t *result)
83be9e
+{
83be9e
+	if (y > x) {
83be9e
+		return false;
83be9e
+	}
83be9e
+	if (result) {
83be9e
+		*result = x - y;
83be9e
+	}
83be9e
+	return true;
83be9e
+}
83be9e
+
83be9e
 #ifdef __cplusplus
83be9e
 }
83be9e
 #endif