Skip to content

CryptoServicesRegistrar's static initializer leaves its default parameters on the initializing thread #2536

Closed

Description

@matrei

CryptoServicesRegistrar's static initializer sets DSA_DEFAULT_PARAMS and DH_DEFAULT_PARAMS through localSetGlobalProperty:

localSetGlobalProperty(Property.DSA_DEFAULT_PARAMS, def512Params, def768Params, def1024Params, def2048Params);
localSetGlobalProperty(Property.DH_DEFAULT_PARAMS, toDH(def512Params), toDH(def768Params), toDH(def1024Params), toDH(def2048Params));

That method also puts them in the threadProperties thread-local of the current thread:

private static <T> void localSetGlobalProperty(Property property, T... propertyValue)
{
if (!property.type.isAssignableFrom(propertyValue[0].getClass()))
{
throw new IllegalArgumentException("Bad property value passed");
}
// set the property for the current thread as well to avoid mass confusion
localSetThread(property, propertyValue);
globalProperties.put(property.name, propertyValue);
}

So the thread that first uses CryptoServicesRegistrar keeps a HashMap with DSAParameters and DHParameters for as long as it lives, and through their classes, the class loader that loaded Bouncy Castle. When bcprov is in a class loader that's thrown away later, like a web application, a plugin, or JRuby embedded with jruby-openssl, that class loader can't be collected while the thread is still around, for example a pooled thread or the main thread.

This program loads bcprov in its own class loader, looks up one property, drops the class loader, and checks whether it was collected. With clear, it first calls clearThreadProperty for both properties:

BcThreadPropertiesLeak.java
import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;

// Loads bcprov in a class loader of its own, looks up a property through CryptoServicesRegistrar, drops the class
// loader and checks whether it can be collected. With "clear", it first clears the thread properties that
// CryptoServicesRegistrar's static initializer set on this thread.
//
// java BcThreadPropertiesLeak.java bcprov-jdk18on-1.86.jar [clear]
public class BcThreadPropertiesLeak {

    public static void main(String[] args) throws Exception {
        URL[] bcprov = {Path.of(args[0]).toUri().toURL()};
        boolean clear = args.length > 1 && args[1].equals("clear");
        WeakReference<ClassLoader> loader = useBouncyCastle(bcprov, clear);
        for (int i = 0; i < 10 && loader.get() != null; i++) {
            System.gc();
            Thread.sleep(100);
        }
        System.out.println((clear ? "with clearThreadProperty: " : "without: ")
            + (loader.get() == null ? "class loader collected" : "class loader still alive"));
    }

    static WeakReference<ClassLoader> useBouncyCastle(URL[] bcprov, boolean clear) throws Exception {
        try (URLClassLoader loader = new URLClassLoader(bcprov, ClassLoader.getPlatformClassLoader())) {
            Class<?> registrar = loader.loadClass("org.bouncycastle.crypto.CryptoServicesRegistrar");
            Class<?> property = loader.loadClass("org.bouncycastle.crypto.CryptoServicesRegistrar$Property");
            Object dsa = property.getField("DSA_DEFAULT_PARAMS").get(null);
            Object dh = property.getField("DH_DEFAULT_PARAMS").get(null);
            registrar.getMethod("getSizedProperty", property).invoke(null, dsa);
            if (clear) {
                registrar.getMethod("clearThreadProperty", property).invoke(null, dsa);
                registrar.getMethod("clearThreadProperty", property).invoke(null, dh);
            }
            return new WeakReference<>(loader);
        }
    }
}

With bcprov-jdk18on 1.86, on JDK 21 and 25:

$ java BcThreadPropertiesLeak.java bcprov-jdk18on-1.86.jar
without: class loader still alive
$ java BcThreadPropertiesLeak.java bcprov-jdk18on-1.86.jar clear
with clearThreadProperty: class loader collected

In the static initializer, the thread can't have values of its own yet, and lookupProperty falls back to globalProperties, so the thread copy changes nothing there. localSetGlobalProperty could update the thread's value only when the thread already has one. That keeps what the comment describes for setGlobalProperty, without leaving anything on the thread that initializes the class.

I found this while looking into class loaders that embedded JRuby leaves behind, see this JRuby discussion.

Activity

  1. dghgit commented on Oct 31, 2026

    @dghgit
    Contributor

    Thanks for the report. I think this should now be fixed and available on https://www.bouncycastle.org/betas

  2. matrei commented on Nov 2, 2026

    @matrei
    Author

    Thanks! I checked the 1.87 beta from November 1st: the class loader is collected now, with the reproducer above and in JRuby after require 'openssl', on JDK 21 and 25.

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions