CVE-2016-10249: Integer Overflow or Wraparound (CWE-190)
value: Integer overflow in the jpc_dec_tiledecode function in jpc_dec.c in JasPer before 1.900.12 allows remote attackers to have unspecified impact via a crafted image file, which triggers a heap-based buffer overflow.
CWE description: The product performs a calculation that can
         produce an integer overflow or wraparound when the logic
         assumes that the resulting value will always be larger than
         the original value. This occurs when an integer value is
         incremented to a value that is too large to store in the
         associated representation. When this occurs, the value may
         become a very small or negative number.
CVSS v3 base score: 7.8
Published: 2017-03-15T14:59Z
Repository: https://github.com/mdadams/jasper
Language: C
Files changed: src/libjasper/base/jas_malloc.c, src/libjasper/base/jas_seq.c
Commit date: 2016-10-22 14:36:49 -0700
Commit message: Fixed an integer overflow problem.

Diff: diff --git a/src/libjasper/base/jas_malloc.c b/src/libjasper/base/jas_malloc.c
index 23154e3..b4b868f 100644
--- a/src/libjasper/base/jas_malloc.c
+++ b/src/libjasper/base/jas_malloc.c
@@ -236,20 +236,20 @@ void jas_free(void *ptr)
 #if !defined(JAS_DEFAULT_MAX_MEM_USAGE)
 
 void *jas_malloc(size_t size)
 {
 	void *result;
-	JAS_DBGLOG(101, ("jas_malloc called with %zu\n", size));
+	JAS_DBGLOG(101, ("jas_malloc(%zu)\n", size));
 	result = malloc(size);
 	JAS_DBGLOG(100, ("jas_malloc(%zu) -> %p\n", size, result));
 	return result;
 }
 
 void *jas_realloc(void *ptr, size_t size)
 {
 	void *result;
-	JAS_DBGLOG(101, ("jas_realloc called with %x,%zu\n", ptr, size));
+	JAS_DBGLOG(101, ("jas_realloc(%x, %zu)\n", ptr, size));
 	result = realloc(ptr, size);
 	JAS_DBGLOG(100, ("jas_realloc(%p, %zu) -> %p\n", ptr, size, result));
 	return result;
 }
 
diff --git a/src/libjasper/base/jas_seq.c b/src/libjasper/base/jas_seq.c
index cac20e2..adc7e13 100644
--- a/src/libjasper/base/jas_seq.c
+++ b/src/libjasper/base/jas_seq.c
@@ -99,39 +99,46 @@ jas_matrix_t *jas_seq2d_create(int xstart, int ystart, int xend, int yend)
 
 jas_matrix_t *jas_matrix_create(int numrows, int numcols)
 {
 	jas_matrix_t *matrix;
 	int i;
+	size_t size;
+
+	matrix = 0;
 
 	if (numrows < 0 || numcols < 0) {
-		return 0;
+		goto error;
 	}
 
 	if (!(matrix = jas_malloc(sizeof(jas_matrix_t)))) {
-		return 0;
+		goto error;
 	}
 	matrix->flags_ = 0;
 	matrix->numrows_ = numrows;
 	matrix->numcols_ = numcols;
 	matrix->rows_ = 0;
 	matrix->maxrows_ = numrows;
 	matrix->data_ = 0;
-	matrix->datasize_ = numrows * numcols;
+	matrix->datasize_ = 0;
+
+	// matrix->datasize_ = numrows * numcols;
+	if (!jas_safe_size_mul(numrows, numcols, &size)) {
+		goto error;
+	}
+	matrix->datasize_ = size;
 
 	if (matrix->maxrows_ > 0) {
 		if (!(matrix->rows_ = jas_alloc2(matrix->maxrows_,
 		  sizeof(jas_seqent_t *)))) {
-			jas_matrix_destroy(matrix);
-			return 0;
+			goto error;
 		}
 	}
 
 	if (matrix->datasize_ > 0) {
 		if (!(matrix->data_ = jas_alloc2(matrix->datasize_,
 		  sizeof(jas_seqent_t)))) {
-			jas_matrix_destroy(matrix);
-			return 0;
+			goto error;
 		}
 	}
 
 	for (i = 0; i < numrows; ++i) {
 		matrix->rows_[i] = &matrix->data_[i * matrix->numcols_];
@@ -145,10 +152,16 @@ jas_matrix_t *jas_matrix_create(int numrows, int numcols)
 	matrix->ystart_ = 0;
 	matrix->xend_ = matrix->numcols_;
 	matrix->yend_ = matrix->numrows_;
 
 	return matrix;
+
+error:
+	if (matrix) {
+		jas_matrix_destroy(matrix);
+	}
+	return 0;
 }
 
 void jas_matrix_destroy(jas_matrix_t *matrix)
 {
 	if (matrix->data_) {

Vulnerable code: 	JAS_DBGLOG(101, ("jas_malloc called with %zu\n", size));
	JAS_DBGLOG(101, ("jas_realloc called with %x,%zu\n", ptr, size));
		return 0;
		return 0;
	matrix->datasize_ = numrows * numcols;
			jas_matrix_destroy(matrix);
			return 0;
			jas_matrix_destroy(matrix);
			return 0;
Fixed code: 	JAS_DBGLOG(101, ("jas_malloc(%zu)\n", size));
	JAS_DBGLOG(101, ("jas_realloc(%x, %zu)\n", ptr, size));
	size_t size;

	matrix = 0;
		goto error;
		goto error;
	matrix->datasize_ = 0;

	// matrix->datasize_ = numrows * numcols;
	if (!jas_safe_size_mul(numrows, numcols, &size)) {
		goto error;
	}
	matrix->datasize_ = size;
			goto error;
			goto error;

error:
	if (matrix) {
		jas_matrix_destroy(matrix);
	}
	return 0;