Skip to content

[BUG] ClassLoader Leak in AesGcmEncryption due to uncleaned ThreadLocal #58

Description

@QiuYucheng2003

Description:

Summary
The AesGcmEncryption class uses a ThreadLocal to cache encryption handles. In Web container environments (e.g., Tomcat), this leads to ClassLoader Pinning because the Cipher object, loaded by the WebappClassLoader, is strongly referenced by long-lived container threads.

Technical Details
Vulnerable Field: private ThreadLocal cipherWrapper.

Mechanism: When getCipher() is called, a Cipher instance is bound to the current thread's ThreadLocalMap.

The Leak: There is no cipherWrapper.remove() call in the codebase. This creates a reference chain: Thread -> ThreadLocalMap -> Cipher -> WebappClassLoader, preventing the application from being fully undeployed.

Impact
Repeated hot redeployments will cause Metaspace/PermGen exhaustion (OOM), requiring a full JVM restart to recover.

Suggested Fix

  1. Remove ThreadLocal: Modern JVMs handle Cipher.getInstance() efficiently; caching may not be necessary.

  2. Explicit Cleanup: If caching is required, implement a cleanup mechanism (e.g., a destroy() method) that invokes cipherWrapper.remove() on active threads.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions