Skip to content

Commit

Permalink
CRYPTO-158 Illegal reflective access
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbASF committed Dec 1, 2023
1 parent 8e4d4ec commit b51442e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<body>
<release version="1.2.1" date="202X-MM-DD" description="Minor release (Java 8, OpenSSL 1.1.1)">
<!-- FIX -->
<action issue="CRYPTO-158" type="fix" due-to=" Juan F Arjona" dev="sebb">Illegal reflective access</action>
<action issue="CRYPTO-179" type="fix" dev="sebb">Crash on macOS with default crypto library</action>
<action issue="CRYPTO-166" type="fix" dev="sebb">Library is reloaded multiple times</action>
<action issue="CRYPTO-175" type="fix" dev="sebb">JNA tests rely on JNI code</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.security.GeneralSecurityException;
Expand Down Expand Up @@ -96,38 +95,6 @@ static void checkStreamCipher(final CryptoCipher cipher) throws IOException {
}
}

/**
* Forcibly free the direct buffer.
*
* @param buffer the bytebuffer to be freed.
*/
static void freeDirectBuffer(final ByteBuffer buffer) {
if (buffer != null) {
try {
/*
* Using reflection to implement sun.nio.ch.DirectBuffer.cleaner() .clean();
*/
final String SUN_CLASS = "sun.nio.ch.DirectBuffer";
final Class<?>[] interfaces = buffer.getClass().getInterfaces();
final Object[] EMPTY_OBJECT_ARRAY = {};

for (final Class<?> clazz : interfaces) {
if (clazz.getName().equals(SUN_CLASS)) {
/* DirectBuffer#cleaner() */
final Method getCleaner = Class.forName(SUN_CLASS).getMethod("cleaner");
final Object cleaner = getCleaner.invoke(buffer, EMPTY_OBJECT_ARRAY);
/* Cleaner#clean() */
final Method cleanMethod = Class.forName("sun.misc.Cleaner").getMethod("clean");
cleanMethod.invoke(cleaner, EMPTY_OBJECT_ARRAY);
return;
}
}
} catch (final ReflectiveOperationException e) { // NOPMD
// Ignore the Reflection exception.
}
}
}

/**
* Reads crypto buffer size.
*
Expand Down Expand Up @@ -418,8 +385,8 @@ protected int decryptMore() throws IOException {

/** Forcibly free the direct buffers. */
protected void freeBuffers() {
CryptoInputStream.freeDirectBuffer(inBuffer);
CryptoInputStream.freeDirectBuffer(outBuffer);
inBuffer.clear();
outBuffer.clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ public void flush() throws IOException {

/** Forcibly free the direct buffers. */
protected void freeBuffers() {
CryptoInputStream.freeDirectBuffer(inBuffer);
CryptoInputStream.freeDirectBuffer(outBuffer);
inBuffer.clear();
outBuffer.clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected PositionedCryptoInputStream(final Properties properties, final Input i
private void cleanByteBufferPool() {
ByteBuffer buf;
while ((buf = byteBufferPool.poll()) != null) {
CryptoInputStream.freeDirectBuffer(buf);
buf.clear();
}
}

Expand Down

0 comments on commit b51442e

Please sign in to comment.