hn.today

Right Code, Wrong Comment: The Pointer Chase Inside Zlib

dataisland.org3 points0 comments
Screenshot of Right Code, Wrong Comment: The Pointer Chase Inside Zlib

The investigation examines a safety invariant in zlib’s DEFLATE compressor: output written to a shared buffer must not overwrite symbols that the compressor has yet to read. DEFLATE records each symbol in a three-byte intermediate representation, then builds Huffman trees and encodes the block into the same buffer. Because a recorded match can expand from 24 bits to as many as 48 output bits, the output cursor can advance faster than unread symbols move out of its way. If overwritten bytes become an invalid match distance, zlib’s lookup into the fixed `_dist_code` table can run beyond its bounds and trigger further out-of-bounds reads, making the invariant relevant to memory safety, not merely compressed-output correctness.

The authors argue that zlib’s 38-line comment claiming the overlap cannot occur is mathematically flawed: an average code-length bound cannot rule out a collision partway through a block. Their machine-checked Lean 4 analysis nevertheless finds that constructing a real exploit is obstructed by DEFLATE’s Huffman constraints. It models the C pointer and bit-buffer updates as inequalities over code lengths, using an algebraic cancellation to establish safety for the smallest memory levels without brute-force search. The analysis also compares mathematical bounds with executions of unmodified zlib: synthesized cases approach roughly 980 units, while the implementation’s physical limit is 720. It describes this discrepancy as a remaining proof gap and outlines how to narrow it, alongside nine separate upstream zlib defects found during the broader verification effort.

Read on dataisland.org0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Security

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.