T09 · Insecure Skill Coding Practices
- Location
scripts/pindou.py:68- Finding
Unbounded PNG Decompression and Image Processing Can Cause Resource Exhaustion
- Content
View full analysis
I", data[pos:pos + 4])[0] ctype = data[pos + 4:pos + 8] chunk = data[pos + 8:pos + 8 + length] if ctype == b"IHDR": width, height, bit_depth, color_type, _, _, interlace = struct.unpack(">IIBBBBB", chunk) elif ctype == b"IDAT": idat += chunk elif ctype == b"PLTE": palette = chunk elif ctype == b"tRNS": trns = chunk elif ctype == b"IEND": break pos += 12 + length ``` ```python raw = zlib.decompress(bytes(idat)) stride = width * bpp out = bytearray() prev = bytearray(stride) p = 0 for y in range(height): ftype = raw[p] ``` ### Technical Analysis The built-in PNG decoder processes attacker-controlled image files without imposing limits on: - Input file size - PNG width and height - Declared pixel count - Total compressed `IDAT` data - Decompressed scanline data - Memory allocated for scanlines and decoded pixels - CPU time spent decoding and subsequently processing the image The entire input file is first loaded into memory with `f.read()`. All `IDAT` chunks are then accumulated in another in-memory buffer, after which `zlib.decompress()` expands the complete compressed stream without a maximum output size. A highly compressible payload can therefore have a small on-disk representation while expanding to a very large memory buffer. The decoder also trusts dimensions obtained from the `IHDR` chunk when calculating `stride` and iterating over image rows. It does not verify that the decompressed length exactly matches the expected scanline length before indexing the buffer. Re ...[truncated 1794 chars]- Remediation
View remediation
