Skip to content

Digest leaks its EVP_MD_CTX when final() is never called #74

Description

@SeanTAllen

Digest allocates an EVP_MD_CTX in every constructor and frees it in exactly one place: inside final(). There is no _final anywhere in ssl/crypto, and no dispose. _ctx is a let field, so it cannot be nulled. A Digest that is created and dropped without calling final() leaks its OpenSSL context.

This is not an error path. It is what happens when a caller builds a digest, appends to it, and then abandons it — because the request was cancelled, because an error was raised, or because the code simply stopped early.

Measurement

Dropping Digest objects without calling final(), with the work chunked across behaviors so Pony collects the Pony objects each chunk. Peak RSS:

dropped digests peak RSS
calls final() 2,000,000 7,424 KB
drops without final() 500,000 108,416 KB
drops without final() 2,000,000 413,056 KB

The control stays flat. The leak grows linearly, about 207 bytes per dropped digest, which is EVP_MD_CTX plus the SHA-256 state it points at.

Scope

Digest alone. It is the only type in ssl/crypto that holds a C resource across calls. HmacSha256, Pbkdf2Sha256, RandBytes and the HashFn primitives each make a single one-shot call that allocates nothing lasting. SSL and SSLContext both have finalizers.

Fix direction

A _final cannot simply free _ctx, because final() already freed it on the normal path and _ctx is let and cannot be nulled. The finalizer has to know whether final() ran. Freeing in _final only when the digest has not been finalized would do it, which means _ctx becomes var and gets nulled by final(), the same shape SSL.dispose uses.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions