diff --git a/pixie.nimble b/pixie.nimble index 73284861..b218e9ee 100644 --- a/pixie.nimble +++ b/pixie.nimble @@ -8,7 +8,7 @@ srcDir = "src" requires "nim >= 2.0.0" requires "vmath >= 3.0.0" requires "chroma >= 1.0.0" -requires "zippy >= 0.10.16" +requires "https://github.com/treeform/zippy#head" requires "flatty >= 0.3.4" requires "nimsimd >= 1.3.2" requires "bumpy >= 1.1.3" diff --git a/src/pixie/fileformats/gif.nim b/src/pixie/fileformats/gif.nim index 40052e8d..9f5a49b8 100644 --- a/src/pixie/fileformats/gif.nim +++ b/src/pixie/fileformats/gif.nim @@ -163,6 +163,7 @@ proc decodeGif*(data: string): Gif {.raises: [PixieError].} = let clearCode = 1 shl minCodeSize endCode = clearCode + 1 + pixelCount = imageWidth * imageHeight var b = BitStreamReader( @@ -194,26 +195,34 @@ proc decodeGif*(data: string): Gif {.raises: [PixieError].} = let start = colorIndexes.len if code < table.len: # If we have seen the code before if code < clearCode: + if colorIndexes.len >= pixelCount: + failInvalid() colorIndexes.add(code) - if prev.len > 0: + if prev.len > 0 and table.len < 4096: table.add((prev.offset, prev.len + 1)) prev = (start, 1) else: let (offset, len) = table[code] + if len > pixelCount - colorIndexes.len: + failInvalid() for i in 0 ..< len: colorIndexes.add(colorIndexes[offset + i]) - table.add((prev.offset, prev.len + 1)) + if table.len < 4096: + table.add((prev.offset, prev.len + 1)) prev = (start, len) else: if prev[1] == 0: failInvalid() + if prev.len >= pixelCount - colorIndexes.len: + failInvalid() for i in 0 ..< prev[1]: colorIndexes.add(colorIndexes[prev[0] + i]) colorIndexes.add(colorIndexes[prev[0]]) - table.add((start, prev.len + 1)) + if table.len < 4096: + table.add((start, prev.len + 1)) prev = (start, prev.len + 1) - if colorIndexes.len != imageWidth * imageHeight: + if colorIndexes.len != pixelCount: failInvalid() let image = newImage(imageWidth, imageHeight) diff --git a/src/pixie/fileformats/png.nim b/src/pixie/fileformats/png.nim index 1cc08781..5f4f31cc 100644 --- a/src/pixie/fileformats/png.nim +++ b/src/pixie/fileformats/png.nim @@ -1,5 +1,5 @@ import chroma, flatty/binny, ../common, ../images, ../internal, - ../simd, zippy, crunchy + ../simd, zippy, zippy/inflate, crunchy # See http://www.libpng.org/pub/png/spec/1.2/PNG-Contents.html @@ -416,21 +416,77 @@ proc writePixels16( else: discard # 16-bit paletted PNG is not a valid color/bit-depth combo. +proc filteredSize(header: PngHeader): int {.raises: [PixieError].} = + ## Computes the exact filtered byte count before decompressing image data. + var size = 0 + proc addPass(width, height: int) {.raises: [PixieError].} = + ## Adds one nonempty scanline pass without overflowing the output bound. + if width == 0 or height == 0: + return + let rowBytes = + (width.int64 * header.valuesPerPixel * header.bitDepth.int + 7) div 8 + 1 + if rowBytes > int.high - 16 or + height.int64 > (int.high - 16 - size).int64 div rowBytes: + failInvalid() + size += rowBytes.int * height + if header.interlaceMethod == 0: + addPass(header.width, header.height) + else: + const + StartXs = [0, 4, 0, 2, 0, 1, 0] + StartYs = [0, 0, 4, 0, 2, 0, 1] + StepXs = [8, 8, 4, 4, 2, 2, 1] + StepYs = [8, 8, 8, 4, 4, 2, 2] + for pass in 0 ..< 7: + addPass( + adam7Size(header.width, StartXs[pass], StepXs[pass]), + adam7Size(header.height, StartYs[pass], StepYs[pass]) + ) + size + +proc uncompressZlib( + data: ptr UncheckedArray[uint8], + len, expected: int +): string {.raises: [PixieError].} = + ## Validates zlib framing and bounds inflation to the declared PNG pixels. + if len < 6: + failInvalid() + let + cmf = data[0] + flg = data[1] + if (cmf and 15) != 8 or (cmf shr 4) > 7 or + (cmf.uint16 * 256 + flg.uint16) mod 31 != 0 or (flg and 32) != 0: + failInvalid() + try: + inflate(result, data, len, 2, expected) + if result.len != expected or + adler32(result) != data.readUint32(len - 4).swap: + failInvalid() + except ZippyError, CrunchyError: + failInvalid() + proc uncompressIdats( - data: ptr UncheckedArray[uint8], idats: seq[(int, int)] -): string = + data: ptr UncheckedArray[uint8], + idats: seq[(int, int)], + expected: int +): string {.raises: [PixieError].} = + ## Combines IDAT chunks and inflates no more than the declared pixel data. if idats.len > 1: var imageData: string for (start, len) in idats: let op = imageData.len imageData.setLen(imageData.len + len) copyMem(imageData[op].addr, data[start].addr, len) - result = try: uncompress(imageData) except ZippyError: failInvalid() + result = uncompressZlib( + cast[ptr UncheckedArray[uint8]](imageData.cstring), + imageData.len, + expected + ) else: let (start, len) = idats[0] p = data[start].unsafeAddr - result = try: uncompress(p, len) except ZippyError: failInvalid() + result = uncompressZlib(cast[ptr UncheckedArray[uint8]](p), len, expected) proc decodeImageData( data: ptr UncheckedArray[uint8], @@ -442,9 +498,9 @@ proc decodeImageData( if idats.len == 0: failInvalid() - result.setLen(header.width * header.height) + let uncompressed = uncompressIdats(data, idats, header.filteredSize) - let uncompressed = uncompressIdats(data, idats) + result.setLen(header.width * header.height) if header.interlaceMethod == 0: let @@ -516,9 +572,9 @@ proc decodeImageData16( if idats.len == 0: failInvalid() - result.setLen(header.width * header.height) + let uncompressed = uncompressIdats(data, idats, header.filteredSize) - let uncompressed = uncompressIdats(data, idats) + result.setLen(header.width * header.height) if header.interlaceMethod == 0: let diff --git a/tests/test_decode_limits.nim b/tests/test_decode_limits.nim new file mode 100644 index 00000000..0c6aabc8 --- /dev/null +++ b/tests/test_decode_limits.nim @@ -0,0 +1,117 @@ +import + std/[os, osproc, strutils, tempfiles], + crunchy, pixie, zippy, pixie/fileformats/[gif, png] + +proc addBigEndian(data: var string, value: uint32) = + ## Appends an unsigned PNG length or checksum in network byte order. + for shift in countdown(24, 0, 8): + data.add(char((value shr shift) and 255)) + +proc addChunk(data: var string, kind, content: string) = + ## Appends one CRC-checked PNG chunk for a controlled decoder fixture. + data.addBigEndian(content.len.uint32) + data.add(kind) + data.add(content) + data.addBigEndian(crc32(kind & content)) + +proc pngFixture(inflated: string, split = false, interlaced = false): string = + ## Encodes a one-pixel header with independently chosen compressed data. + result = "\x89PNG\r\n\x1a\n" + var header = "\0\0\0\1\0\0\0\1\x08\x06\0\0\0" + if interlaced: + header[^1] = '\1' + result.addChunk("IHDR", header) + let compressed = compress(inflated, dataFormat = dfZlib) + if split: + let middle = compressed.len div 2 + result.addChunk("IDAT", compressed[0 ..< middle]) + result.addChunk("IDAT", compressed[middle .. ^1]) + else: + result.addChunk("IDAT", compressed) + result.addChunk("IEND", "") + +proc gifFixture(pixels: int): string = + ## Emits repeated clear/literal codes for a declared one-pixel GIF frame. + result = "GIF89a\1\0\1\0\x80\0\0\0\0\0\xFF\xFF\xFF" & + "\x2C\0\0\0\0\1\0\1\0\0\x02" + var + compressed = "" + pending = 0 + count = 0 + proc addCode(code: int) = + ## Packs one three-bit LZW code, with clear codes keeping the width fixed. + pending = pending or (code shl count) + count += 3 + if count >= 8: + compressed.add(char(pending and 255)) + pending = pending shr 8 + count -= 8 + for i in 0 ..< pixels: + addCode(4) + addCode(0) + addCode(5) + if count > 0: + compressed.add(char(pending and 255)) + var offset = 0 + while offset < compressed.len: + let size = min(255, compressed.len - offset) + result.add(char(size)) + result.add(compressed[offset ..< offset + size]) + offset += size + result.add("\0\x3B") + +proc checkBoundedFailure(path: string) = + ## Requires malformed compressed data to fail before allocating its expansion. + let data = readFile(path) + when declared(getMaxMem): + let before = getMaxMem() + var failed = false + try: + discard decodeImage(data) + except PixieError: + failed = true + doAssert failed + when declared(getMaxMem): + doAssert getMaxMem() - before < 4 * 1024 * 1024, + "Malformed compressed data allocated beyond its declared image size." + +if paramCount() == 2 and paramStr(1) == "--decode-limit": + checkBoundedFailure(paramStr(2)) + quit(0) + +block: + echo "Testing bounded PNG and GIF expansion" + let directory = createTempDir("pixie-image-limits-", "") + defer: + removeDir(directory) + for split in [false, true]: + for interlaced in [false, true]: + let + valid = pngFixture("\0\xFF\0\0\xFF", split, interlaced) + image = decodeImage(valid) + path = directory / ($split & "-" & $interlaced & ".png") + doAssert image.width == 1 and image.height == 1 + doAssert image[0, 0] == rgbx(255, 0, 0, 255) + writeFile( + path, + pngFixture(repeat('x', 8 * 1024 * 1024), split, interlaced) + ) + let child = startProcess( + getAppFilename(), args = ["--decode-limit", path], + options = {poUsePath, poParentStreams} + ) + doAssert child.waitForExit() == 0 + child.close() + let + gif = gifFixture(1) + path = directory / "oversized.gif" + doAssert decodeGif(gif).frames[0].width == 1 + writeFile(path, gifFixture(1024 * 1024)) + let child = startProcess( + getAppFilename(), args = ["--decode-limit", path], + options = {poUsePath, poParentStreams} + ) + doAssert child.waitForExit() == 0 + child.close() + +echo "All compressed image limit tests passed."