--- cups-1.1.23/pdftops/Stream.h.CVE-2005-3625,6,7 2004-02-02 22:41:09.000000000 +0000 +++ cups-1.1.23/pdftops/Stream.h 2006-01-04 14:43:36.000000000 +0000 @@ -231,6 +231,8 @@ ~StreamPredictor(); + GBool isOk() { return ok; } + int lookChar(); int getChar(); @@ -248,6 +250,7 @@ int rowBytes; // bytes per line Guchar *predLine; // line buffer int predIdx; // current index in predLine + GBool ok; }; //------------------------------------------------------------------------ --- cups-1.1.23/pdftops/Stream.cxx.CVE-2005-3625,6,7 2004-02-02 22:41:09.000000000 +0000 +++ cups-1.1.23/pdftops/Stream.cxx 2006-01-04 14:43:36.000000000 +0000 @@ -15,6 +15,7 @@ #include #include #include +#include #ifndef WIN32 #include #endif @@ -409,13 +410,28 @@ width = widthA; nComps = nCompsA; nBits = nBitsA; + predLine = NULL; + ok = gFalse; + if (width <= 0 || nComps <= 0 || nBits <= 0 || + nComps >= INT_MAX/nBits || + width >= INT_MAX/nComps/nBits) { + return; + } nVals = width * nComps; + if (nVals * nBits + 7 <= 0) { + return; + } pixBytes = (nComps * nBits + 7) >> 3; rowBytes = ((nVals * nBits + 7) >> 3) + pixBytes; + if (rowBytes < 0) { + return; + } predLine = (Guchar *)gmalloc(rowBytes); memset(predLine, 0, rowBytes); predIdx = rowBytes; + + ok = gTrue; } StreamPredictor::~StreamPredictor() { @@ -991,6 +1007,10 @@ FilterStream(strA) { if (predictor != 1) { pred = new StreamPredictor(this, predictor, columns, colors, bits); + if (!pred->isOk()) { + delete pred; + pred = NULL; + } } else { pred = NULL; } @@ -1239,6 +1259,10 @@ endOfLine = endOfLineA; byteAlign = byteAlignA; columns = columnsA; + if (columns < 1 || columns >= INT_MAX / sizeof(short)) { + error(-1, "invalid number of columns"); + exit(1); + } rows = rowsA; endOfBlock = endOfBlockA; black = blackA; @@ -2891,6 +2915,11 @@ height = read16(); width = read16(); numComps = str->getChar(); + if (numComps <= 0 || numComps > 4) { + numComps = 0; + error(getPos(), "Bad number of components in DCT stream"); + return gFalse; + } if (prec != 8) { error(getPos(), "Bad DCT precision %d", prec); return gFalse; @@ -2917,6 +2946,11 @@ height = read16(); width = read16(); numComps = str->getChar(); + if (numComps <= 0 || numComps > 4) { + numComps = 0; + error(getPos(), "Bad number of components in DCT stream"); + return gFalse; + } if (prec != 8) { error(getPos(), "Bad DCT precision %d", prec); return gFalse; @@ -2939,6 +2973,10 @@ length = read16() - 2; scanInfo.numComps = str->getChar(); + if (scanInfo.numComps <= 0 || scanInfo.numComps > 4) { + error(getPos(), "Bad number of components in DCT stream"); + return gFalse; + } --length; if (length != 2 * scanInfo.numComps + 3) { error(getPos(), "Bad DCT scan info block"); @@ -3013,12 +3051,12 @@ while (length > 0) { index = str->getChar(); --length; - if ((index & 0x0f) >= 4) { + if ((index & ~0x10) >= 4 || (index & ~0x10) < 0) { error(getPos(), "Bad DCT Huffman table"); return gFalse; } if (index & 0x10) { - index &= 0x0f; + index &= 0x03; if (index >= numACHuffTables) numACHuffTables = index+1; tbl = &acHuffTables[index]; @@ -3136,9 +3174,11 @@ do { do { c = str->getChar(); + if(c == EOF) return EOF; } while (c != 0xff); do { c = str->getChar(); + if(c == EOF) return EOF; } while (c == 0xff); } while (c == 0x00); return c; @@ -3249,6 +3289,10 @@ FilterStream(strA) { if (predictor != 1) { pred = new StreamPredictor(this, predictor, columns, colors, bits); + if (!pred->isOk()) { + delete pred; + pred = NULL; + } } else { pred = NULL; } --- cups-1.1.23/pdftops/JBIG2Stream.cxx.CVE-2005-3625,6,7 2004-02-02 22:41:09.000000000 +0000 +++ cups-1.1.23/pdftops/JBIG2Stream.cxx 2006-01-04 14:44:11.000000000 +0000 @@ -7,6 +7,7 @@ //======================================================================== #include +#include #ifdef USE_GCC_PRAGMAS #pragma implementation @@ -1001,7 +1002,16 @@ w = wA; h = hA; line = (wA + 7) >> 3; - data = (Guchar *)gmalloc(h * line); + + if (h < 0 || line <= 0 || h >= (INT_MAX - 1) / line) { + error(-1, "invalid width/height"); + data = NULL; + return; + } + + // need to allocate one extra guard byte for use in combine() + data = (Guchar *)gmalloc(h * line + 1); + data[h * line] = 0; } JBIG2Bitmap::JBIG2Bitmap(Guint segNumA, JBIG2Bitmap *bitmap): @@ -1010,8 +1020,17 @@ w = bitmap->w; h = bitmap->h; line = bitmap->line; - data = (Guchar *)gmalloc(h * line); + + if (h < 0 || line <= 0 || h >= (INT_MAX - 1) / line) { + error(-1, "invalid width/height"); + data = NULL; + return; + } + + // need to allocate one extra guard byte for use in combine() + data = (Guchar *)gmalloc(h * line + 1); memcpy(data, bitmap->data, h * line); + data[h * line] = 0; } JBIG2Bitmap::~JBIG2Bitmap() { @@ -1036,10 +1055,14 @@ } void JBIG2Bitmap::expand(int newH, Guint pixel) { - if (newH <= h) { + if (newH <= h || line <= 0 || newH >= (INT_MAX - 1)/ line) { + error(-1, "invalid width/height"); + gfree(data); + data = NULL; return; } - data = (Guchar *)grealloc(data, newH * line); + // need to allocate one extra guard byte for use in combine() + data = (Guchar *)grealloc(data, newH * line + 1); if (pixel) { memset(data + h * line, 0xff, (newH - h) * line); } else { @@ -2576,6 +2599,15 @@ error(getPos(), "Bad symbol dictionary reference in JBIG2 halftone segment"); return; } + if (gridH == 0 || gridW >= INT_MAX / gridH) { + error(getPos(), "Bad size in JBIG2 halftone segment"); + return; + } + if (w == 0 || h >= INT_MAX / w) { + error(getPos(), "Bad size in JBIG2 bitmap segment"); + return; + } + patternDict = (JBIG2PatternDict *)seg; bpp = 0; i = 1; @@ -3207,6 +3239,11 @@ JBIG2BitmapPtr tpgrCXPtr0, tpgrCXPtr1, tpgrCXPtr2; int x, y, pix; + if (w < 0 || h <= 0 || w >= INT_MAX / h) { + error(-1, "invalid width/height"); + return NULL; + } + bitmap = new JBIG2Bitmap(0, w, h); bitmap->clearToZero();