Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free HMAC context when garbage collected #38

Merged
merged 1 commit into from
Sep 23, 2022
Merged

Conversation

qmuntal
Copy link
Contributor

@qmuntal qmuntal commented Sep 22, 2022

This PR fix a memory leak on NewHMAC(). The returned instance owns a C.GO_HMAC_CTX_PTR allocated using C.malloc. The logic to free the context memory resides in opensslHMAC.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 calling runtime.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 every NewHMAC() call, which means ~1GB of memory is lost every 3 milions calls.

Found via fuzzing.

@qmuntal qmuntal merged commit 0e9fe57 into main Sep 23, 2022
@qmuntal qmuntal deleted the dev/qmuntal/hmacleak branch September 23, 2022 06:43
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.

3 participants