Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
jpc_enc: jpc_abstorelstepsize() returns error instead of aborting
Browse files Browse the repository at this point in the history
Fixes CVE-2018-9252

Closes #16
  • Loading branch information
MaxKellermann committed Jun 24, 2020
1 parent e2f2e5f commit 6cd1e1d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/libjasper/jpc/jpc_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ static jpc_enc_cp_t *cp_create(const char *optstr, jas_image_t *image);
static void jpc_enc_cp_destroy(jpc_enc_cp_t *cp);
static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn);

/**
* @return UINT_FAST32_MAX on error
*/
static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn)
{
int p;
Expand All @@ -171,16 +174,18 @@ static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn)
int n;

if (absdelta < 0) {
abort();
return UINT_FAST32_MAX;
}

p = jpc_fix_firstone(absdelta) - JPC_FIX_FRACBITS;
n = 11 - jpc_fix_firstone(absdelta);
mant = ((n < 0) ? (absdelta >> (-n)) : (absdelta << n)) & 0x7ff;
expn = scaleexpn - p;
if (scaleexpn < p) {
abort();
return UINT_FAST32_MAX;
}
if (expn >= 0x1f)
return UINT_FAST32_MAX;
return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant);
}

Expand Down Expand Up @@ -991,9 +996,12 @@ startoff = jas_stream_getrwcount(enc->out);
} else {
absstepsize = jpc_inttofix(1);
}
cp->ccps[cmptno].stepsizes[bandno] =
const uint_fast32_t stepsize =
jpc_abstorelstepsize(absstepsize,
cp->ccps[cmptno].prec + analgain);
if (stepsize == UINT_FAST32_MAX)
return -1;
cp->ccps[cmptno].stepsizes[bandno] = stepsize;
}
cp->ccps[cmptno].numstepsizes = numbands;
}
Expand Down Expand Up @@ -1234,9 +1242,12 @@ jas_eprintf("%d %d mag=%d actual=%d numgbits=%d\n", cp->ccps[cmptno].prec, band-
} else {
band->absstepsize = jpc_inttofix(1);
}
band->stepsize = jpc_abstorelstepsize(
const uint_fast32_t stepsize = jpc_abstorelstepsize(
band->absstepsize, cp->ccps[cmptno].prec +
band->analgain);
if (stepsize == UINT_FAST32_MAX)
return -1;
band->stepsize = stepsize;
band->numbps = cp->tccp.numgbits +
JPC_QCX_GETEXPN(band->stepsize) - 1;

Expand Down

0 comments on commit 6cd1e1d

Please sign in to comment.