Skip to content

fix(parquet): check PLAIN byte array end offsets for overflow in read - #12215

Merged
Rich-T-kid merged 2 commits into
apache:mainfrom
hsdfat:fix-byte-view-plain-read-overflow-12200
Nov 13, 2026
Merged

Rich-T-kid merged 2 commits into
apache:mainfrom
hsdfat:fix-byte-view-plain-read-overflow-12200

Conversation

@hsdfat

@hsdfat hsdfat commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

ByteViewArrayDecoderPlain::read_impl adds the page's u32 length prefix to start_offset without an overflow check. On a 32-bit target (wasm32, i686) a corrupt length such as 0xFFFF_FFFC wraps end_offset to a value below start_offset, the end_offset > buf_len check passes, and buf.get_unchecked(start_offset..end_offset) is called with an inverted range. #12179 fixed the same pattern in skip; read was left out.

ByteArrayDecoderPlain::read has the same unchecked addition. It uses a checked slice, so there it is a panic rather than UB, but it is the same bug on the same input, so I fixed both.

What changes are included in this PR?

Both read paths compute the end offset with checked_add(..).filter(|&end| end <= buf.len()) and return the existing EOF error, the same form #12179 used for skip.

Are these changes tested?

Yes, one unit test per decoder reads a page whose first length is 0xFFFF_FFFC and expects an EOF error.

On 64-bit the addition cannot wrap, so the tests pass there with and without the fix. I ran them on i686-unknown-linux-gnu (cargo test -p parquet --lib --no-default-features --features arrow --target i686-unknown-linux-gnu):

  • without the fix, debug: both tests panic with an add overflow at the changed lines;
  • without the fix, release: the offset decoder panics on the slice, and the view decoder returns Ok from the inverted range instead of an error;
  • with the fix: both pass.

I did not run Miri (not available in my toolchain); CI runs it.

Are there any significant user-facing changes?

No. A corrupt page that could panic or hit undefined behaviour on 32-bit targets now returns an error.


AI disclosure: the patch and tests were drafted with AI assistance (Claude); I reviewed every line and ran fmt, clippy and the parquet array reader tests, plus the i686 runs above.

The length prefix comes from the page. On a 32-bit target a corrupt
length could wrap the end offset below the start offset and pass the
bounds check, reaching get_unchecked with an inverted range in the view
decoder and a slice panic in the offset decoder. skip() was fixed the
same way in apache#12179.
@boring-cyborg boring-cyborg Bot added the parquet parquet crate label Nov 12, 2026
Comment on lines +393 to +398
// `checked_add` because the length comes from the page: on a 32-bit
// target a corrupt one could wrap `end_offset` below `start_offset`.
let end_offset = start_offset
.checked_add(len as usize)
.filter(|&end| end <= buf.len())
.ok_or_else(|| ParquetError::EOF("eof decoding byte array".into()))?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// `checked_add` because the length comes from the page: on a 32-bit
// target a corrupt one could wrap `end_offset` below `start_offset`.
let end_offset = start_offset
.checked_add(len as usize)
.filter(|&end| end <= buf.len())
.ok_or_else(|| ParquetError::EOF("eof decoding byte array".into()))?;
let end_offset = start_offset
.checked_add(len as usize)
.filter(|&end| end <= buf.len())
.ok_or_else(|| ParquetError::EOF("eof decoding byte array".into()))?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 26e8475, thanks.

Comment on lines +367 to +373
// `checked_add` because the length comes from the page: on a 32-bit
// target a corrupt one could wrap `end_offset` below `start_offset`
// and pass the bounds check below.
end_offset = start_offset
.checked_add(len as usize)
.filter(|&end| end <= buf_len)
.ok_or_else(|| ParquetError::EOF("eof decoding byte array".into()))?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// `checked_add` because the length comes from the page: on a 32-bit
// target a corrupt one could wrap `end_offset` below `start_offset`
// and pass the bounds check below.
end_offset = start_offset
.checked_add(len as usize)
.filter(|&end| end <= buf_len)
.ok_or_else(|| ParquetError::EOF("eof decoding byte array".into()))?;
end_offset = start_offset
.checked_add(len as usize)
.filter(|&end| end <= buf_len)
.ok_or_else(|| ParquetError::EOF("eof decoding byte array".into()))?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 26e8475, thanks.

assert_eq!(view_buffer.buffers.len(), 1);
}

#[test]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the test, the change is straightforward enough to not warrant them.
maybe just one if we are very adamant about it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep them, unless a committer prefers otherwise. There is one per decoder because the two read paths are separate code, and they are what shows the wrap on a 32-bit target (I ran them on i686-unknown-linux-gnu, where they fail without the change). On 64-bit they pass either way, so they cost little, but I am fine with dropping to one if you feel strongly.

@Rich-T-kid Rich-T-kid Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhhm, im just not sure they are all that needed. as you mentioned they are low cost so which ever one you go with is fine

@Rich-T-kid

Copy link
Copy Markdown
Contributor

@hsdfat did you push the commit? the changes do not seem to have been applied

@hsdfat

hsdfat commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Sorry, my push had failed without me noticing. 26e8475 is on the branch now.

@Rich-T-kid

Copy link
Copy Markdown
Contributor

thanks @hsdfat

@Rich-T-kid
Rich-T-kid added this pull request to the merge queue Nov 13, 2026
Merged via the queue into apache:main with commit 4454c7d Nov 13, 2026
22 checks passed
@Jefffrey Jefffrey added the bug Squash those bugs label Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Squash those bugs parquet parquet crate

Projects

None yet

3 participants