Skip to content

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
pharo-vcs:Pharo13from
takano32:fix/clone-options-stale-layout
Open

takano32 wants to merge 1 commit into
pharo-vcs:Pharo13from
takano32:fix/clone-options-stale-layout

Conversation

@takano32

Copy link
Copy Markdown

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_malloc assertions).

LGitLibrary>>initializeStructures version-dispatches the four adaptive structs (LGitBlameHunk, LGitFetchOptions, LGitPushOptions, LGitRemoteCallbacks) 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 gained an update_refs callback (120→128 bytes), which dragged git_fetch_options 208→216 and git_clone_options 408→416. The adaptive LGitFetchOptions alias is swapped to V190 correctly, but LGitCloneOptions keeps its 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 init. It lands on the neighbouring chunk's prev_size word, 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:

LGitExternalStructure allSubclasses do: [ :each | each rebuildFieldAccessors ]

Idempotent, and guarded by the existing LastLibgitVersion check so it only runs when the libgit2 version actually changed. This is more robust than naming LGitCloneOptions explicitly: any current or future structure that embeds an adaptive member is covered.

Evidence (measured, libgit2 1.9.7, aarch64, glibc 2.43)

  • Before the sweep: LGitCloneOptions byteSize = 408; native sizeof(git_clone_options) = 416.
  • After the sweep: LGitCloneOptions byteSize = 416, matching native; re-running is idempotent (stays 416).
  • Sentinel check: a 512-byte 0x55-filled buffer passed to initializeWithDefaults comes 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 resetAll and FFIStructure resetAllStructures were both measured to leave LGitCloneOptions at 408 — only rebuilding the structure moves it — which is why this needs a dedicated fix on top of the existing resetAll.

Notes

  • Verified on the Pharo13 branch (the version an in-the-wild Pharo 13 image loads is v3.2.1, which predates even the LastLibgitVersion/resetAll guard; this fix sits on top of that guard as it exists on Pharo13).
  • The same class of bug is present on Pharo14/Pharo15, whose initializeStructures currently has no rebuild/reset at all — happy to port this there too if you'd like.
  • Bystander observation while investigating: LGitFetchOptionsV190 omits the int depth field libgit2 added in 1.7 (size stays 216, so no overflow; follow_redirects lands on libgit2's depth). 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

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant