GH-50380: [C++][Gandiva] fix out-of-bounds read in byte_substr past end#50381
Open
Arawoof06 wants to merge 2 commits into
Open
GH-50380: [C++][Gandiva] fix out-of-bounds read in byte_substr past end#50381Arawoof06 wants to merge 2 commits into
Arawoof06 wants to merge 2 commits into
Conversation
…past end Signed-off-by: abdul rawoof <abdulr@bugqore.com>
|
|
Member
|
@dmitry-chirkov-dremio @lriggs @akravchukdremio @xxlaykxx Could you review this? |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Gandiva byte_substr edge case where a user-supplied offset beyond the input length could produce a negative out_len and trigger an out-of-bounds memcpy, and adds regression coverage.
Changes:
- Add a guard in
byte_substr_binary_int32_int32to return an empty result whenstartPos >= text_len. - Add a unit test case covering “offset past end” behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| cpp/src/gandiva/precompiled/string_ops.cc | Adds an early-return guard to prevent negative-length truncation and OOB copy in byte_substr. |
| cpp/src/gandiva/precompiled/string_ops_test.cc | Adds a regression test for offsets past the end returning an empty result. |
Comment on lines
+2490
to
+2494
| // an offset past the end of the text leaves nothing to copy; without this the | ||
| // truncation below yields a negative *out_len that memcpy reads as a huge size | ||
| if (startPos >= text_len) { | ||
| *out_len = 0; | ||
| return ""; |
Author
There was a problem hiding this comment.
Good catch. Moved the startPos computation and the past-end check above gdv_fn_context_arena_malloc, so an offset beyond the text now returns empty without allocating or touching the OOM path.
…in byte_substr Move the past-end bounds check ahead of the arena_malloc so an offset beyond text_len returns early without allocating an output buffer or risking a spurious OOM error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
byte_substr_binary_int32_int32derivesstartPosfrom the offset argument but never confirms it falls inside the text. A positiveoffsetlarger thantext_lenleavesstartPos >= text_len, sotext_len - startPosis negative and that value is stored in*out_len. The followingmemcpyreads it as a largesize_tand runs off the end of both the source text and thetext_len-sized output buffer. The offset comes straight from user SQL, sobyte_substr(col, n, m)withnpast the row length trips it. Under AddressSanitizer,byte_substr("TestString", 10, 15, 10)reportsnegative-size-param (size=-4)reading 4 bytes past the 10-byte region.What changes are included in this PR?
Return an empty result when
startPos >= text_len, before the length truncation can go negative. The check sits in the callee next to wherestartPosis computed so every caller is covered.Are these changes tested?
Yes,
TestByteSubstrgains a case with the offset past the end asserting an empty result and no error. The existing cases are unchanged.Are there any user-facing changes?
No.
This PR contains a "Critical Fix". It fixes an out-of-bounds read (and oversized copy) in
byte_substrreachable from user-supplied offsets.