fix(xmss): prevent FFI keypair leaks on partial load and unclean shutdown#194
Merged
fix(xmss): prevent FFI keypair leaks on partial load and unclean shutdown#194
Conversation
devylongs
approved these changes
Apr 2, 2026
shaaibu7
approved these changes
Apr 2, 2026
…down Rust-allocated XMSS keypairs were leaked when loadValidatorKeys() failed mid-loop (previously loaded keys never freed) and when the process exited via os.Exit or panic (Close() never called). Changes: - Clean up previously loaded keypairs on error in loadValidatorKeys(), matching zeam's errdefer keypair.deinit() pattern (cli/src/node.zig:433) - Add defer n.Close() in main.go so keypairs are freed on all exit paths - Free XMSS keypairs in Node.Close() before closing DB and P2P - Add runtime.KeepAlive guards after all CGo calls that pass Go pointers to Rust in leansig.go (RestoreKeypair, Sign, Verify, VerifyWithKeypair) and leanmultisig.go (Aggregate, VerifyAggregated) to prevent GC from collecting slices during FFI execution
f52e22c to
fa69ccd
Compare
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.
Summary
loadValidatorKeys()fails mid-loop, preventing Rust memory leaks on partial load (zeamerrdefer keypair.deinit()pattern,cli/src/node.zig:433)
defer n.Close()inmain.goso keypairs are freed on all exit paths includingos.Exitand panicNode.Close()before closing DB and P2P servicesruntime.KeepAliveguards after all CGo calls that pass Go pointers to Rust:leansig.go:RestoreKeypair,Sign,Verify,VerifyWithKeypairleanmultisig.go:Aggregate,VerifyAggregatedContext
Rust-allocated XMSS keypairs (loaded via CGo FFI) were leaked in two scenarios:
os.Exit(1)(error path in main.go) or panic,Close()was never called and keypairs were never returned to RustThe
runtime.KeepAliveguards are a defensive hardening — without them, the Go GC could theoretically collect slices passed to C while the Rust FFI call is still executing. Unlikelyin practice since CGo pins the goroutine, but it's the accepted best practice per CGo documentation.
Test plan
go build ./...compiles cleanlygo vet ./...passesgo test ./node/... -count=1— node tests passgo test ./xmss/leanmultisig/... -count=1— multisig tests passgo test -race ./node/... ./xmss/leanmultisig/...— no racesSIGINT— verify no Rust memory warnings in logsCloses FFI keypairs leaked on partial load failure and missing runtime.KeepAlive guards #191