Free HMAC context when garbage collected #38
Merged
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.
This PR fix a memory leak on
NewHMAC()
. The returned instance owns aC.GO_HMAC_CTX_PTR
allocated usingC.malloc
. The logic to free the context memory resides inopensslHMAC.finalize
, which should be called automatically when the hmac instance is garbage collected.The problem is that Go doesn't know it has to call
opensslHMAC.finalize
, we have to instruct it by callingruntime.SetFinalizer(hmac, (*opensslHMAC).finalize)
just after instantiating the hmac. And we are not doing that. We used to do it when we initially port RedHat codebase but, at some point, that call was unintentionally removed.A
C.GO_HMAC_CTX_PTR
weights around 300 bytes, so we are leaking that amount on everyNewHMAC()
call, which means ~1GB of memory is lost every 3 milions calls.Found via fuzzing.