Skip to content

Commit bb62bec

Browse files
committed
zero dek
1 parent 4c2d85f commit bb62bec

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/main/java/com/ironcorelabs/tenantsecurity/kms/v1/CachedKey.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.security.SecureRandom;
66
import java.time.Duration;
77
import java.time.Instant;
8+
import java.lang.invoke.MethodHandles;
9+
import java.lang.invoke.VarHandle;
810
import java.util.Arrays;
911
import java.util.Map;
1012
import java.util.concurrent.CompletableFuture;
@@ -178,7 +180,6 @@ public int getOperationCount() {
178180
/**
179181
* Guard an operation with usability checks and operation counting. Verifies the cached key is not
180182
* closed or expired before running the operation, and increments the operation count on success.
181-
*
182183
* @param operation The operation to perform
183184
* @param countOps Extracts the number of successful operations from the result
184185
* @param counter The counter to increment on success
@@ -312,14 +313,15 @@ public void close() {
312313
}
313314

314315
/**
315-
* Zero a DEK byte array with a subsequent access to prevent the JIT from eliminating the fill as
316-
* a dead store. The volatile write after the fill ensures the zeroing is not optimized away.
316+
* Zero a DEK byte array using opaque stores to prevent the JIT from eliminating the writes as
317+
* dead stores. VarHandle.setOpaque is the lightest memory ordering mode that guarantees the
318+
* writes actually happen per the Java Memory Model spec.
317319
*/
318-
@SuppressWarnings("unused")
319-
private static volatile byte ZERO_FENCE;
320+
private static final VarHandle BYTE_ARRAY = MethodHandles.arrayElementVarHandle(byte[].class);
320321

321322
static void zeroDek(byte[] dek) {
322-
Arrays.fill(dek, (byte) 0);
323-
ZERO_FENCE = dek[0];
323+
for (int i = 0; i < dek.length; i++) {
324+
BYTE_ARRAY.setOpaque(dek, i, (byte) 0);
325+
}
324326
}
325327
}

0 commit comments

Comments
 (0)