Skip to content

[java] New Rule: Catch usages of sun.misc.Unsafe or jdk.internal.misc.Unsafe #5923

Closed

Description

@thomasleplus

Proposed Rule Name: DontUseUnsafe UnsupportedJdkApiUsage

Proposed Category: Security Error Prone

Description:

Code Sample: This should include code, that should be flagged by the rule. If possible, the "correct" code
according to this new rule should also be demonstrated.

In the bad code below, 128 bytes of memory are allocated but then arbitrary strings are written which could easily go over the 128 bytes limit.

import java.lang.reflect.Field;
import sun.misc.Unsafe;

public final class UnsafePutChar {

  public static void main(final String[] args) throws NoSuchFieldException, IllegalAccessException {
    final long address = getUnsafe().allocateMemory(128);
    for (final String s : args) {
      for (final char c : s.toCharArray()) {
        getUnsafe().putChar(address, c);
      }
    }
  }

  private static Unsafe getUnsafe() throws IllegalAccessException, NoSuchFieldException {
    final Field f = Unsafe.class.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    return (Unsafe) f.get(null);
  }
}

The right approach if using off-heap memory is really necessary would be to use the Foreign Function and Memory API.

In general I think that any use of sun.misc.Unsafe or jdk.internal.misc.Unsafe should be flagged as a security concern. I know that the DontImportSun already covers sun.misc.Unsafe but it is wider in its scope (all of sun.*) and it doesn't explain the security risk so it might be easily dismissed by someone who doesn't know the particular security implications of sun.misc.Unsafe. A security-specific rule including both sun.misc.Unsafe and jdk.internal.misc.Unsafe seems more appropriate.

I can create a PR for this but I wanted to discuss it first. See a working draft of XPath rule here.

Possible Properties:

  • Should this rule be customizable via properties?

Not that I can think of.

Activity

  1. added
    a:new-rule
    on Jul 19, 2025
  2. elharo commented on Nov 13, 2025

    @elharo
    Contributor

    JDK already warns about this. I'm not sure why PMD should duplicate that.

  3. changed the title [-]Catch usages of sun.misc.Unsafe or jdk.internal.misc.Unsafe[/-] [+][java] Catch usages of sun.misc.Unsafe or jdk.internal.misc.Unsafe[/+] on Nov 20, 2025
  4. changed the title [-][java] Catch usages of sun.misc.Unsafe or jdk.internal.misc.Unsafe[/-] [+][java] New rule: Catch usages of sun.misc.Unsafe or jdk.internal.misc.Unsafe[/+] on Nov 20, 2025
  5. added this to the 7.21.0 milestone on Jan 15, 2026
  6. changed the title [-][java] New rule: Catch usages of sun.misc.Unsafe or jdk.internal.misc.Unsafe[/-] [+][java] New Rule: Catch usages of sun.misc.Unsafe or jdk.internal.misc.Unsafe[/+] on Jan 29, 2026
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

    a:new-rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions