Repository navigation
Rebuild structs embedding version-adaptive structs on libgit2 version change (fixes heap overflow in git_clone_options on libgit2 >= 1.9) - #118
Open
takano32 wants to merge 1 commit into
Conversation
…on change initializeStructures re-aliases the four version-adaptive structs and calls FFIMethodRegistry resetAll, but resetAll only rebinds FFI call sites -- it does not rebuild a structure that embeds a version-adaptive struct BY VALUE and has cached its own byteSize/layout. LGitCloneOptions embeds LGitFetchOptions. On libgit2 >= 1.9, git_remote_callbacks grew an update_refs field (120 -> 128 bytes), which dragged git_fetch_options 208 -> 216 and git_clone_options 408 -> 416. The adaptive LGitFetchOptions alias is swapped to its V190 variant correctly, but LGitCloneOptions keeps the cached 408-byte layout. git_clone_init_options then does GIT_INIT_STRUCTURE_FROM_TEMPLATE = memcpy(opts, &tmpl, sizeof(git_clone_options)) = 416 bytes into the 408-byte buffer FFI allocated -> 8-byte heap-buffer overflow on every clone-options initialization, which corrupts the glibc heap and crashes nondeterministically at a later allocation. Fix: after a version change, rebuild every LibGit structure so composites track the current adaptive members. Idempotent, and only runs when the libgit2 version actually changed. Measured on libgit2 1.9.7 (aarch64, glibc 2.43): LGitCloneOptions byteSize goes 408 -> 416 after the sweep, matching native sizeof(git_clone_options)=416; a 512-byte sentinel buffer passed to initializeWithDefaults is then modified only through byte 416 (== byteSize), i.e. no overflow. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dszsb5ALkx1ytH8atZPvEV
This branch has not been deployed
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.
Problem
On a host whose system libgit2 is 1.9.x, the Pharo 13 bindings corrupt the glibc heap during any clone (Iceberg dependency resolution crashes nondeterministically with
corrupted size vs. prev_size/malloc(): invalid size/_int_mallocassertions).LGitLibrary>>initializeStructuresversion-dispatches the four adaptive structs (LGitBlameHunk,LGitFetchOptions,LGitPushOptions,LGitRemoteCallbacks) and callsFFIMethodRegistry resetAll. ButresetAllonly rebinds FFI call sites — it does not rebuild a structure that embeds a version-adaptive struct by value and has cached its ownbyteSize/layout.LGitCloneOptionsembedsLGitFetchOptions. On libgit2 ≥ 1.9,git_remote_callbacksgained anupdate_refscallback (120→128 bytes), which draggedgit_fetch_options208→216 andgit_clone_options408→416. The adaptiveLGitFetchOptionsalias is swapped toV190correctly, butLGitCloneOptionskeeps its cached 408-byte layout.git_clone_init_optionsthen doesGIT_INIT_STRUCTURE_FROM_TEMPLATE=memcpy(opts, &tmpl, sizeof(git_clone_options))= 416 bytes into the 408-byte buffer FFI allocated → 8-byte heap-buffer overflow on every clone-options init. It lands on the neighbouring chunk'sprev_sizeword, so it detonates later at an unrelated allocation — hence the nondeterminism.Fix
After a version change, rebuild every LibGit structure so composites track the current adaptive members:
Idempotent, and guarded by the existing
LastLibgitVersioncheck so it only runs when the libgit2 version actually changed. This is more robust than namingLGitCloneOptionsexplicitly: any current or future structure that embeds an adaptive member is covered.Evidence (measured, libgit2 1.9.7, aarch64, glibc 2.43)
LGitCloneOptions byteSize= 408; nativesizeof(git_clone_options)= 416.LGitCloneOptions byteSize= 416, matching native; re-running is idempotent (stays 416).0x55-filled buffer passed toinitializeWithDefaultscomes back modified only through byte 416 (== the new byteSize) — i.e. the write now fits exactly, no overflow. On the unpatched image the same init smashes the heap and the VM hangs in its SIGABRT handler.FFIMethodRegistry resetAllandFFIStructure resetAllStructureswere both measured to leaveLGitCloneOptionsat 408 — only rebuilding the structure moves it — which is why this needs a dedicated fix on top of the existingresetAll.Notes
v3.2.1, which predates even theLastLibgitVersion/resetAllguard; this fix sits on top of that guard as it exists onPharo13).initializeStructurescurrently has no rebuild/reset at all — happy to port this there too if you'd like.LGitFetchOptionsV190omits theint depthfield libgit2 added in 1.7 (size stays 216, so no overflow;follow_redirectslands on libgit2'sdepth). That's a separate latent issue (looks like it's already addressed on the Pharo15 line) and is not part of this PR.🤖 Generated with Claude Code
https://claude.ai/code/session_01Dszsb5ALkx1ytH8atZPvEV