CVE-2016-10251: Integer Overflow or Wraparound (CWE-190)
value: Integer overflow in the jpc_pi_nextcprl function in jpc_t2cod.c in JasPer before 1.900.20 allows remote attackers to have unspecified impact via a crafted file, which triggers use of an uninitialized value.
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/jpc/jpc_t2cod.c, src/libjasper/jpc/jpc_t2cod.h
Commit date: 2016-11-04 07:20:23 -0700
Commit message: Fixed an integer overflow problem in the JPC codec that later resulted
in the use of uninitialized data.

Diff: diff --git a/src/libjasper/jpc/jpc_t2cod.c b/src/libjasper/jpc/jpc_t2cod.c
index 08315dd..174442a 100644
--- a/src/libjasper/jpc/jpc_t2cod.c
+++ b/src/libjasper/jpc/jpc_t2cod.c
@@ -430,22 +430,22 @@ static int jpc_pi_nextcprl(register jpc_pi_t *pi)
 
 	for (pi->compno = pchg->compnostart, pi->picomp =
 	  &pi->picomps[pi->compno]; pi->compno < JAS_CAST(int, pchg->compnoend) && pi->compno < pi->numcomps; ++pi->compno,
 	  ++pi->picomp) {
 		pirlvl = pi->picomp->pirlvls;
-		pi->xstep = pi->picomp->hsamp * (1 << (pirlvl->prcwidthexpn +
-		  pi->picomp->numrlvls - 1));
-		pi->ystep = pi->picomp->vsamp * (1 << (pirlvl->prcheightexpn +
-		  pi->picomp->numrlvls - 1));
+		pi->xstep = pi->picomp->hsamp * (JAS_CAST(uint_fast32_t, 1) <<
+		  (pirlvl->prcwidthexpn + pi->picomp->numrlvls - 1));
+		pi->ystep = pi->picomp->vsamp * (JAS_CAST(uint_fast32_t, 1) <<
+		  (pirlvl->prcheightexpn + pi->picomp->numrlvls - 1));
 		for (rlvlno = 1, pirlvl = &pi->picomp->pirlvls[1];
 		  rlvlno < pi->picomp->numrlvls; ++rlvlno, ++pirlvl) {
-			pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp * (1 <<
-			  (pirlvl->prcwidthexpn + pi->picomp->numrlvls -
-			  rlvlno - 1)));
-			pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp * (1 <<
-			  (pirlvl->prcheightexpn + pi->picomp->numrlvls -
-			  rlvlno - 1)));
+			pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp *
+			  (JAS_CAST(uint_fast32_t, 1) << (pirlvl->prcwidthexpn +
+			  pi->picomp->numrlvls - rlvlno - 1)));
+			pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp *
+			  (JAS_CAST(uint_fast32_t, 1) << (pirlvl->prcheightexpn +
+			  pi->picomp->numrlvls - rlvlno - 1)));
 		}
 		for (pi->y = pi->ystart; pi->y < pi->yend;
 		  pi->y += pi->ystep - (pi->y % pi->ystep)) {
 			for (pi->x = pi->xstart; pi->x < pi->xend;
 			  pi->x += pi->xstep - (pi->x % pi->xstep)) {
diff --git a/src/libjasper/jpc/jpc_t2cod.h b/src/libjasper/jpc/jpc_t2cod.h
index 0a176c9..690e031 100644
--- a/src/libjasper/jpc/jpc_t2cod.h
+++ b/src/libjasper/jpc/jpc_t2cod.h
@@ -127,14 +127,14 @@ typedef struct {
 
 	/* The per-resolution-level information. */
 	jpc_pirlvl_t *pirlvls;
 
 	/* The horizontal sampling period. */
-	int hsamp;
+	uint_fast32_t hsamp;
 
 	/* The vertical sampling period. */
-	int vsamp;
+	uint_fast32_t vsamp;
 
 } jpc_picomp_t;
 
 /* Packet iterator class. */
 
@@ -169,36 +169,36 @@ typedef struct {
 
 	/* The number of the current layer. */
 	int lyrno;
 
 	/* The x-coordinate of the current position. */
-	int x;
+	uint_fast32_t x;
 
 	/* The y-coordinate of the current position. */
-	int y;
+	uint_fast32_t y;
 
 	/* The horizontal step size. */
-	int xstep;
+	uint_fast32_t xstep;
 
 	/* The vertical step size. */
-	int ystep;
+	uint_fast32_t ystep;
 
 	/* The x-coordinate of the top-left corner of the tile on the reference
 	  grid. */
-	int xstart;
+	uint_fast32_t xstart;
 
 	/* The y-coordinate of the top-left corner of the tile on the reference
 	  grid. */
-	int ystart;
+	uint_fast32_t ystart;
 
 	/* The x-coordinate of the bottom-right corner of the tile on the
 	  reference grid (plus one). */
-	int xend;
+	uint_fast32_t xend;
 
 	/* The y-coordinate of the bottom-right corner of the tile on the
 	  reference grid (plus one). */
-	int yend;
+	uint_fast32_t yend;
 
 	/* The current progression change. */
 	jpc_pchg_t *pchg;
 
 	/* The progression change list. */

Vulnerable code: 		pi->xstep = pi->picomp->hsamp * (1 << (pirlvl->prcwidthexpn +
		  pi->picomp->numrlvls - 1));
		pi->ystep = pi->picomp->vsamp * (1 << (pirlvl->prcheightexpn +
		  pi->picomp->numrlvls - 1));
			pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp * (1 <<
			  (pirlvl->prcwidthexpn + pi->picomp->numrlvls -
			  rlvlno - 1)));
			pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp * (1 <<
			  (pirlvl->prcheightexpn + pi->picomp->numrlvls -
			  rlvlno - 1)));
	int hsamp;
	int vsamp;
	int x;
	int y;
	int xstep;
	int ystep;
	int xstart;
	int ystart;
	int xend;
	int yend;
Fixed code: 		pi->xstep = pi->picomp->hsamp * (JAS_CAST(uint_fast32_t, 1) <<
		  (pirlvl->prcwidthexpn + pi->picomp->numrlvls - 1));
		pi->ystep = pi->picomp->vsamp * (JAS_CAST(uint_fast32_t, 1) <<
		  (pirlvl->prcheightexpn + pi->picomp->numrlvls - 1));
			pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp *
			  (JAS_CAST(uint_fast32_t, 1) << (pirlvl->prcwidthexpn +
			  pi->picomp->numrlvls - rlvlno - 1)));
			pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp *
			  (JAS_CAST(uint_fast32_t, 1) << (pirlvl->prcheightexpn +
			  pi->picomp->numrlvls - rlvlno - 1)));
	uint_fast32_t hsamp;
	uint_fast32_t vsamp;
	uint_fast32_t x;
	uint_fast32_t y;
	uint_fast32_t xstep;
	uint_fast32_t ystep;
	uint_fast32_t xstart;
	uint_fast32_t ystart;
	uint_fast32_t xend;
	uint_fast32_t yend;