Skip to content

Possible memory leak in bc-fips related to Java finalizers #1776

Closed

Description

@selliott512

A memory analysis of a jmap memory dump revealed a significant number of java.lang.ref.Finalizer references. The referent is org.bouncycastle.crypto.fips.FipsPBKD$Parameters, created with call stack:

org.bouncycastle.jcajce.provider.ProvBCFKS$BCFIPSKeyStoreSpi.generateKey(Unknown Source)
at org.bouncycastle.jcajce.provider.ProvBCFKS$BCFIPSKeyStoreSpi.decryptData(Unknown Source)
at org.bouncycastle.jcajce.provider.ProvBCFKS$BCFIPSKeyStoreSpi.engineLoad(Unknown Source)
at java.security.KeyStore.load(java.base@11.0.23/KeyStore.java:1479)

We believe this is related to the use of finalizers (finalize methods) in bc-fips code. Finalizers are problematic, in part due to their two-pass nature.

Two possible solutions, in increasing order of difficulty:

  1. Simply remove all finalize methods.
  2. Replace the finalize methods with an alternative strategy, such as try-with-resources.

Regarding solution 1), since the finalize methods are primarily overwriting arrays with zeros, the security risk of removing them should be minimal, as most modern operating systems already ensure that freed memory isn't visible to other processes.

Additionally, issue #1536 refers to java.lang.ref.Finalizer, but it’s unclear if it's the same problem since that issue does not include the path.

The versions of the Bouncy Castle JARs used:

JAR Version
bc-fips 1.0.2.4
bctls-fips 1.0.18

The JRE is OpenJDK 11 on Red Hat Enterprise Linux 8.10.

Activity

  1. dghgit commented on May 26, 2026

    @dghgit
    Contributor

    Closing as inactive. For the record though, we've been progressively removing finalizers in the API due to the GC changes in Java 13. BC-FJA 3.0.0 will not have any finalizers in it at all. There haven't been any reports we have received where finalizers have actually caused a memory leak, but it's clear that in the post Java 13 world, things with finalize seem to have been becoming odder and odder.

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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions