Skip to content

bug(cpp): SPRINTZ short-block header can overflow the 9-value block buffer #1113

Open

Description

@ColinLeeo

Language

C++

Description

decode_block() in every SPRINTZ decoder takes a short (RLE / Gorilla) block's value count straight from the header and writes that many values into current_buffer_, which only holds block_size_ + 1 = 9 values. The count is never validated against the buffer size, so a malformed value page produces a heap out-of-bounds write while reading a file.

A short block starts with a header byte whose MSB is set; the value count is the low seven bits:

  • cpp/src/encoding/int32_sprintz_decoder.h:134
  • cpp/src/encoding/int64_sprintz_decoder.h:134
  • cpp/src/encoding/float_sprintz_decoder.h:132
  • cpp/src/encoding/double_sprintz_decoder.h:131

so decode_size_ can be up to 127, and the loop immediately below (:136 in int32, the same shape in the other three) writes decode_size_ values into the 9-element buffer created at int32_sprintz_decoder.h:45. Both the per-value and the batch read paths reach this through decode_block().

A short block from the encoder always holds 1..8 values, but the decoder never enforces that. Int32/Int64 short blocks use the RLE decoder, whose pattern format can express a single run of up to 2^31 values in a few bytes; a crafted page therefore keeps read_int succeeding for all 127 iterations. Verified with this 6-byte page fed to Int32SprintzDecoder:

0xFF        short-block header, decode_size = 127
0x04        RLE pattern length (4 bytes)
0x01        RLE bit width
0xFE 0x01   RLE tag: run of 127
0x00        run value

Running the same pattern through Int32RleDecoder returns 127 successful reads, i.e. decode_block() would write current_buffer_[9] .. current_buffer_[126], past the end of the vector. FLOAT/DOUBLE short blocks are Gorilla-encoded and reach the same loop bound with a different crafted payload.

Java fails cleanly on this input: IntSprintzDecoder.decodeBlock() uses the same unvalidated decodeSize, but writes into a Java array, so it throws ArrayIndexOutOfBoundsException. The C++ decoder should reject the block instead of corrupting memory.

Pre-existing: found while working on #1110 (batch decode) and intentionally left out of that PR.

Steps to reproduce

  • Run the C++ test suite with -DENABLE_ASAN=ON and feed an INT32 SPRINTZ decoder a value page starting with the bytes above; ASAN reports a heap-buffer-overflow in decode_block().
  • Without a sanitizer: replicate the loop with the same page and observe 127 successful read_int/read_int32 calls, more than the 9-element buffer can hold.

Expected behavior

decode_block() returns an error (matching Java) instead of writing past current_buffer_, with a regression test that feeds an over-counted short block header for all four types.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions