Skip to content

[58_maintenance] fix(arrow-buffer): prevent use-after-free in Buffer::shrink_to_fit when MemoryReservation::resize panics - #11785

Merged
Jefffrey merged 2 commits into
apache:58_maintenancefrom
alamb:backport_10932_58
Oct 22, 2026
Merged

Jefffrey merged 2 commits into
apache:58_maintenancefrom
alamb:backport_10932_58

Conversation

@alamb

@alamb alamb commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Cherry-pick of 9a8400e

Conflicts resolved by hand:

…en MemoryReservation::resize panics (apache#10932)

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->

- Closes apache#10379.

Buffer maintains two pointers that must always agree: `Buffer::ptr` (the
raw *const u8 into the allocation) and `Bytes::ptr` (the NonNull<u8>
that owns the allocation). Before this fix, `shrink_to_fit` only updated
`Buffer::ptr` if `try_realloc` returned Ok. But `try_realloc` can panic;
specifically inside `MemoryReservation::resize`, a user-supplied
callback after the realloc syscall already succeeded and `Bytes::ptr`
was updated. That left the two pointers out of sync: `Bytes::ptr`
pointed to the new (valid) allocation, `Buffer::ptr` still pointed to
the old (freed) allocation. Any caller that used `catch_unwind` to
recover from the panic could then call `buf.as_slice()` and read freed
memory.

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

replaced the conditional if try_realloc(...).is_ok() { update ptr } with
a PtrSync drop guard that unconditionally syncs `Buffer::ptr` to
`Bytes::ptr` + offset when it drops. Since Rust runs destructors during
panic unwinding, the guard fires before the panic propagates.
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

yes. a similar test to what was used in apache#10379
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
3. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?

If this PR claims a performance improvement, please include evidence
such as benchmark results.
-->

no
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.

If there are any breaking changes to public APIs, please call them out.
-->

(cherry picked from commit 9a8400e)
@github-actions github-actions Bot added the arrow-core Core crates related to representing arrow data label Oct 21, 2026
@Jefffrey
Jefffrey merged commit 75918a6 into apache:58_maintenance Oct 22, 2026
27 checks passed
@alamb alamb added the bug Squash those bugs label Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants