Repository navigation
fix(parquet): check PLAIN byte array end offsets for overflow in read - #12215
Conversation
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.
| // `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()))?; |
There was a problem hiding this comment.
| // `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()))?; |
| // `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()))?; |
There was a problem hiding this comment.
| // `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()))?; |
| assert_eq!(view_buffer.buffers.len(), 1); | ||
| } | ||
|
|
||
| #[test] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
@hsdfat did you push the commit? the changes do not seem to have been applied |
|
Sorry, my push had failed without me noticing. 26e8475 is on the branch now. |
|
thanks @hsdfat |
Which issue does this PR close?
Rationale for this change
ByteViewArrayDecoderPlain::read_impladds the page'su32length prefix tostart_offsetwithout an overflow check. On a 32-bit target (wasm32,i686) a corrupt length such as0xFFFF_FFFCwrapsend_offsetto a value belowstart_offset, theend_offset > buf_lencheck passes, andbuf.get_unchecked(start_offset..end_offset)is called with an inverted range. #12179 fixed the same pattern inskip;readwas left out.ByteArrayDecoderPlain::readhas 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
readpaths compute the end offset withchecked_add(..).filter(|&end| end <= buf.len())and return the existingEOFerror, the same form #12179 used forskip.Are these changes tested?
Yes, one unit test per decoder reads a page whose first length is
0xFFFF_FFFCand expects anEOFerror.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):Okfrom the inverted range instead of an error;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.