Repository navigation
fix: check small-crate length and offset arithmetic - #12340
Conversation
9bd1fa8 to
b69f481
Compare
b69f481 to
04061d5
Compare
|
I audited these crates for realistic externally controlled arithmetic rather than replacing arithmetic mechanically. The changed callsites are worth guarding because their operands cross an external boundary and feed a narrow representation, allocation, or slice:
I left arithmetic alone where it is dominated by an existing slice or buffer length, uses fixed small enum ranges, is a loop counter bounded by an allocated collection, or is an intentional physical/logical integer reinterpretation. Those cases do not have the same overflow path and changing them would make this a mechanical audit rather than a targeted one. |
|
beware this pr is 100% AI generated. I think its a good sign that we can close #12325 relatively soon and reduce the number of PR's that are just replacing arithmetic with overflow checks. I did review it as well. |
|
Thanks @Rich-T-kid. Here's my 100% AI generated review 😄 Take with a P2 — Capacity checks still allow allocation-layout panicsThe new checks validate usize arithmetic, but vectors also impose byte-capacity limits.
I reproduced both without attempting huge allocations. These are pre-existing gaps left uncovered, not new regressions. They belong within the PR’s claimed capacity audit. Suggested fix: Check vector byte/layout limits before allocation, or use fallible reservation. For CSV, preflight both buffers before resizing either. Coverage gap — No regression tests for the new error pathsThe explanation that tests would require maximum-size fixtures isn’t convincing. Small cases can cover:
Existing happy-path tests don’t exercise these guards. |
|
More seriously, I like the idea of getting out in front of these valid-but-too-many PRs. I tried a similar audit in the parquet crate lately, but a second look seems warranted since that missed several of the recent issues you list in #12325. Let me know if you want help dividing the work. |
i left this out on purpose. I think we dont need the test for PR's like these, the error path is very clear and the test just restate the code.
yea that would be great, could you handle the parquet crate related ones? |
I agree, but AI's tend to be insistent on these things 😅
Starting a fast bounded search. I'll do a deeper dive if that seems to bear fruit. |
alamb
left a comment
There was a problem hiding this comment.
looks like an improvement to me -- thanks @Rich-T-kid and @etseidl
I have some crazy idea on how to improve things, but it may be more complicated and not worth it
| self.offsets | ||
| .resize(self.offsets_len + to_read * self.num_columns, 0); | ||
| let offsets_capacity = to_read | ||
| .checked_mul(self.num_columns) |
There was a problem hiding this comment.
I wonder if we can avoid spreading this copy/pasta around the codebase a bit with a small helper like
trait ArrowCheckedArith {
fn mul_or_err(&self, v: Self, msg: &'static str) -> Result<..> {
...
}
}Then callsites like this could be like
let offsets_capacity = to_read
.mul_or_err(self.num_columns, "CSV decoder capacity overflow")
.map(|capacity| self.offsets_len.add_or_err(capacity, "CSV decoder capacity overflow"))?We could also drop the msg to keep it more concise but less nice error messages
There was a problem hiding this comment.
I think this could be an option, the main concerns are the total LOC and any possible perf regressions this may cause. As noted in #11973 there can be signifcant regressions introduced when we add this checked logic to hot loops, adding a possible extra layer of indirection for dynamic dispatch may make that worse.
There was a problem hiding this comment.
Indeed we would have to measure it -- I think the way this PR structures things though it will add a bunch more code in the related methods (which may be prevent inlining, etc)
There was a problem hiding this comment.
Just a FYI, I've been working on #12099 trying to prevent overruns without impacting performance. The code involved is seriously sensitive to inlining, and at what level it's inlined. For some of these I think we'll need to discuss if the fix is worth it. In my case, I don't think there's a reported panic, just later errors thrown, or at worst wrong data, but this gets to Andrew's point in the Parquet security discussion. If a file is maliciously altered, what is the "correct" output?
There was a problem hiding this comment.
In my mind, if the file is maliciously altered or doesn't conform to the spec there is no well defined answer and it is ok to
- Return an error
- Return some data (unspecified)
- Panic
Ideally it wouldn't panic and it would return a nice error message, but those aren't absolute necessities in my mind, especially for wacky corrupted input
Which issue does this PR close?
Rationale for this change
Several small support crates trust externally supplied lengths, offsets, or counts before narrowing them or using them for allocation. Oversized inputs can wrap, panic, or request an invalid capacity instead of returning an error.
What changes are included in this PR?
arrow-integration-testingconverts batch row counts toi64and uses checked addition forFlightInfo.total_records.arrow-csvchecks decoder buffer-size arithmetic before resizing.arrow-integration-testchecks narrow JSON integers, offsets, view ranges, fixed-size binary capacity, and Decimal256 width.arrow-pyarrowuses fallible reservation for Python iterable length hints.This completes the overflow audit for these four small crates.
Are these changes tested?
The existing crate tests cover the affected decoding and conversion paths. The new checks are direct checked arithmetic and checked conversions, so this PR does not add synthetic maximum-size fixtures.
Are there any significant user-facing changes?
Yes. Invalid or oversized integration JSON that previously truncated values or panicked now returns
ParseErrororJsonError.