Skip to content

🐞 Revert LibZip.flzDecompress on a truncated FastLZ instruction - #1566

Open
gomesalexandre wants to merge 6 commits into
Vectorized:mainfrom
gomesalexandre:fix/flz-decompress-truncated
Open

gomesalexandre wants to merge 6 commits into
Vectorized:mainfrom
gomesalexandre:fix/flz-decompress-truncated

Conversation

@gomesalexandre

Copy link
Copy Markdown

Description

LibZip.flzDecompress treated a FastLZ control byte inside the buffer as a complete instruction. The rest of the instruction was read with mload, so bytes past the payload were the zero padding after the bytes allocation. A truncated literal or match came back as a successful short string.

On main at 258f67cc, a forge probe against the unfixed function returned hex"00" for payload hex"00" (one literal byte, none present) and hex"410000" for payload hex"0241" (three literal bytes, only 0x41 present). The same inputs now revert with TruncatedFastLZ() (0x8bb83a49).

The check matches the byte count the old loop already advanced: a literal of control c is c + 2 bytes, a short match is 2, and a long match (t == 7) is 3. A stream that ends exactly on an instruction boundary still decodes. Empty input still returns empty. The Osaka copy in src/utils/clz/LibZip.sol had the same walk, so it now reverts the same way. That file compiles with solc 0.8.36 at --evm-version osaka.

Checklist

  • Ran forge fmt?
  • Ran forge test?

FOUNDRY_DISABLE_NIGHTLY_WARNING=1 forge test --match-contract LibZipTest --no-match-path 'test/clz/*' on the fixed tree: 31 passed, 0 failed, including testFlzDecompressTruncatedReverts and the 256-run testFlzCompressDecompress(bytes) round trip.

Made with Cursor

gomesalexandre and others added 4 commits November 19, 2026 22:28
Incomplete literal and match headers were decoded by reading past the payload, so callers received a short zero-padded bytes string. Those inputs now revert with TruncatedFastLZ.

Co-authored-by: Cursor <cursoragent@cursor.com>
The Osaka copy walks FastLZ the same way as src/utils/LibZip.sol, so a short instruction still returned zero padding there.

Co-authored-by: Cursor <cursoragent@cursor.com>
A payload that ends on the 0x00 marker loaded the missing control byte from past the end and appended a run of zeros or 0xff. That marker is now left unexpanded.

Co-authored-by: Cursor <cursoragent@cursor.com>
The Osaka copy read the missing control byte from past the payload, same as src/utils/LibZip.sol.

Co-authored-by: Cursor <cursoragent@cursor.com>
@gomesalexandre

Copy link
Copy Markdown
Author

Follow-up on this branch, commits 983d362 and 9f5831b.

cdDecompress had the same class of read as the FastLZ fix. A payload that ends on the 0x00 RLE marker loaded the missing control byte from past the end. On the unfixed function, cdDecompress(hex"eeddccbb00") returned 0x1122334400 and cdDecompress(hex"ff") returned 128 bytes starting with 0xff. With the guard those are 0x11223344 and empty. A run whose control byte is the last payload byte still decodes (hex"ffff" is one zero, hex"ff7f" is one 0xff). The function still does not revert, which testDecompressWontRevert requires.

forge test --match-test 'testCdDecompressTruncated|testFlzDecompressTruncated|testDecompressWontRevert|testCdCompress' --no-match-path 'test/clz/*': 11 passed. The Osaka copy in src/utils/clz/LibZip.sol got the same stop. cdFallback is unchanged: calldataload past the end is zeros, so a missing control there is a one-byte zero run rather than a neighbor from memory.

gomesalexandre and others added 2 commits November 21, 2026 12:13
Calldata that ends on the 0x00 marker loaded the missing control byte from past the end and appended a run. That marker is now left unexpanded.

Co-authored-by: Cursor <cursoragent@cursor.com>
The Osaka copy loaded the missing control byte from past calldata, same as src/utils/LibZip.sol.

Co-authored-by: Cursor <cursoragent@cursor.com>
@gomesalexandre

Copy link
Copy Markdown
Author

Follow-up on this branch, commits a773fbd and 9951036.

cdFallback had the same truncated-marker read as cdDecompress, through calldataload past the end of calldata. On the unfixed function, stored fefdfcfb00 (logical 01020304 plus a marker) returned 0x0102030400. With the guard it returns 0x01020304. A control byte that is the last calldata byte still expands: fefdfcfb0000 is 0102030400. forge test --match-test testCdFallback --no-match-path 'test/clz/*' passed, including the new case and the existing fallback tests. The Osaka copy got the same stop.

@atarpara

atarpara commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

@gomesalexandre Thanks for the PR and the detailed repros!

After some marinating, I think it's better to leave the decompress functions as they are.

  • flzDecompress and cdDecompress are intended to be used on the outputs of flzCompress and cdCompress. The lack of input validation is intentional.
  • The truncation check alone doesn't make it validated tho. E.g. hex"e0173f" is a complete instruction (long match, offset 64, length 32), and it returns the 32 bytes of whatever was allocated before the call. Fully validating means a bounds check on every match too, which is a different (and slower) function.
  • Malformed input only causes extra reads. All writes go to free memory, so it's still memory-safe. Worst case is unspecified bytes returned.
  • cdFallback reading zeros past calldata is well defined, and it's on the hot path. Every valid call would pay for the check.
  • flzDecompress reverting is a behavior change. Some callers may depend on it never reverting.

If someone needs to handle untrusted compressed data, checking a hash of the decompressed result on their end is cheaper than making everyone pay.

I think a note in the natspec is enough here: the input is assumed to be valid and is not validated. Happy to see this trimmed down to just the notes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants