diff options
| author | Even Rouault <even.rouault@mines-paris.org> | 2018-12-07 21:27:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-12-07 21:27:38 +0100 |
| commit | e7640f58f122d1228f3d750864543ad4703e18fc (patch) | |
| tree | 68316a9f949981f3df66cf9698ed485d239e7669 /src/lib | |
| parent | e0f5212888c0c1abc5e060a75a3a4a5ff99afd1a (diff) | |
| parent | 05be3084460e46282ee63f04c72c451f3271fd28 (diff) | |
Merge pull request #1168 from Young-X/fix_dev
Fix multiple potential vulnerabilities and bugs
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/openjp2/pi.c | 19 | ||||
| -rw-r--r-- | src/lib/openjp3d/pi.c | 24 | ||||
| -rw-r--r-- | src/lib/openmj2/pi.c | 52 |
3 files changed, 87 insertions, 8 deletions
diff --git a/src/lib/openjp2/pi.c b/src/lib/openjp2/pi.c index 91642ee4..4a6ed68e 100644 --- a/src/lib/openjp2/pi.c +++ b/src/lib/openjp2/pi.c @@ -748,6 +748,9 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image, /* position in x and y of tile */ OPJ_UINT32 p, q; + /* non-corrected (in regard to image offset) tile offset */ + OPJ_UINT32 l_tx0, l_ty0; + /* preconditions */ assert(p_cp != 00); assert(p_image != 00); @@ -763,14 +766,14 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image, q = p_tileno / p_cp->tw; /* find extent of tile */ - *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), - (OPJ_INT32)p_image->x0); - *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), - (OPJ_INT32)p_image->x1); - *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), - (OPJ_INT32)p_image->y0); - *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), - (OPJ_INT32)p_image->y1); + l_tx0 = p_cp->tx0 + p * + p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */ + *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0); + *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1); + l_ty0 = p_cp->ty0 + q * + p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */ + *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0); + *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1); /* max precision is 0 (can only grow) */ *p_max_prec = 0; diff --git a/src/lib/openjp3d/pi.c b/src/lib/openjp3d/pi.c index a03be45e..a58ebcc7 100644 --- a/src/lib/openjp3d/pi.c +++ b/src/lib/openjp3d/pi.c @@ -223,6 +223,14 @@ static bool pi_next_rpcl(opj_pi_iterator_t * pi) rpx = res->pdx + levelnox; rpy = res->pdy + levelnoy; rpz = res->pdz + levelnoz; + + /* To avoid divisions by zero / undefined behaviour on shift */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy || + rpz >= 31 || ((comp->dz << rpz) >> rpz) != comp->dz) { + continue; + } + if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) { continue; @@ -329,6 +337,14 @@ static bool pi_next_pcrl(opj_pi_iterator_t * pi) rpx = res->pdx + levelnox; rpy = res->pdy + levelnoy; rpz = res->pdz + levelnoz; + + /* To avoid divisions by zero / undefined behaviour on shift */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy || + rpz >= 31 || ((comp->dz << rpz) >> rpz) != comp->dz) { + continue; + } + if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) { continue; @@ -432,6 +448,14 @@ static bool pi_next_cprl(opj_pi_iterator_t * pi) rpx = res->pdx + levelnox; rpy = res->pdy + levelnoy; rpz = res->pdz + levelnoz; + + /* To avoid divisions by zero / undefined behaviour on shift */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy || + rpz >= 31 || ((comp->dz << rpz) >> rpz) != comp->dz) { + continue; + } + if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) { continue; diff --git a/src/lib/openmj2/pi.c b/src/lib/openmj2/pi.c index d0fdb9b1..f74dc6af 100644 --- a/src/lib/openmj2/pi.c +++ b/src/lib/openmj2/pi.c @@ -85,6 +85,12 @@ static opj_bool pi_next_cprl(opj_pi_iterator_t * pi); ========================================================== */ +static void opj_pi_emit_error(opj_pi_iterator_t * pi, const char* msg) +{ + (void)pi; + (void)msg; +} + static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) { opj_pi_comp_t *comp = NULL; @@ -114,6 +120,11 @@ static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; + /* Avoids index out of bounds access with include*/ + if (index >= pi->include_size) { + opj_pi_emit_error(pi, "Invalid access to pi->include"); + return OPJ_FALSE; + } if (!pi->include[index]) { pi->include[index] = 1; return OPJ_TRUE; @@ -156,6 +167,11 @@ static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; + /* Avoids index out of bounds access with include*/ + if (index >= pi->include_size) { + opj_pi_emit_error(pi, "Invalid access to pi->include"); + return OPJ_FALSE; + } if (!pi->include[index]) { pi->include[index] = 1; return OPJ_TRUE; @@ -224,6 +240,13 @@ static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) try1 = int_ceildiv(pi->ty1, comp->dy << levelno); rpx = res->pdx + levelno; rpy = res->pdy + levelno; + + /* To avoid divisions by zero / undefined behaviour on shift */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) { + continue; + } + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))) { continue; @@ -249,6 +272,11 @@ static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; + /* Avoids index out of bounds access with include*/ + if (index >= pi->include_size) { + opj_pi_emit_error(pi, "Invalid access to pi->include"); + return OPJ_FALSE; + } if (!pi->include[index]) { pi->include[index] = 1; return OPJ_TRUE; @@ -317,6 +345,13 @@ static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) try1 = int_ceildiv(pi->ty1, comp->dy << levelno); rpx = res->pdx + levelno; rpy = res->pdy + levelno; + + /* To avoid divisions by zero / undefined behaviour on shift */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) { + continue; + } + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))) { continue; @@ -342,6 +377,11 @@ static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; + /* Avoids index out of bounds access with include*/ + if (index >= pi->include_size) { + opj_pi_emit_error(pi, "Invalid access to pi->include"); + return OPJ_FALSE; + } if (!pi->include[index]) { pi->include[index] = 1; return OPJ_TRUE; @@ -408,6 +448,13 @@ static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) try1 = int_ceildiv(pi->ty1, comp->dy << levelno); rpx = res->pdx + levelno; rpy = res->pdy + levelno; + + /* To avoid divisions by zero / undefined behaviour on shift */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) { + continue; + } + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))) { continue; @@ -433,6 +480,11 @@ static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; + /* Avoids index out of bounds access with include*/ + if (index >= pi->include_size) { + opj_pi_emit_error(pi, "Invalid access to pi->include"); + return OPJ_FALSE; + } if (!pi->include[index]) { pi->include[index] = 1; return OPJ_TRUE; |
