Skip to content

Commit

Permalink
Avoid heap buffer overflow in function pnmtoimage of convert.c, and u…
Browse files Browse the repository at this point in the history
…nsigned integer overflow in opj_image_create() (CVE-2016-9118, #861)
  • Loading branch information
rouault committed Jul 30, 2017
1 parent 83342f2 commit c22cbd8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/bin/jp2/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

#include "openjpeg.h"
#include "convert.h"
Expand Down Expand Up @@ -1731,6 +1732,15 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
return NULL;
}

/* This limitation could be removed by making sure to use size_t below */
if (header_info.height != 0 &&
header_info.width > INT_MAX / header_info.height) {
fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n",
header_info.width, header_info.height);
fclose(fp);
return NULL;
}

format = header_info.format;

switch (format) {
Expand Down
8 changes: 7 additions & 1 deletion src/lib/openjp2/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts,
comp->prec = cmptparms[compno].prec;
comp->bpp = cmptparms[compno].bpp;
comp->sgnd = cmptparms[compno].sgnd;
comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32));
if (comp->h != 0 && (OPJ_SIZE_T)comp->w > SIZE_MAX / comp->h) {
// TODO event manager
opj_image_destroy(image);
return NULL;
}
comp->data = (OPJ_INT32*) opj_calloc((OPJ_SIZE_T)comp->w * comp->h,
sizeof(OPJ_INT32));
if (!comp->data) {
/* TODO replace with event manager, breaks API */
/* fprintf(stderr,"Unable to allocate memory for image.\n"); */
Expand Down

0 comments on commit c22cbd8

Please sign in to comment.