Skip to content

Commit 4c2d85f

Browse files
committed
Add zeroDek function to protect against JIT
1 parent 0dbd731 commit 4c2d85f

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ public CompletableFuture<BatchResult<PlaintextDocument>> decryptBatch(
301301
@Override
302302
public void close() {
303303
if (closed.compareAndSet(false, true)) {
304-
// Zero out the DEK bytes for security
305-
Arrays.fill(dek, (byte) 0);
304+
zeroDek(dek);
306305
// Report operations to TSP
307306
int encrypts = encryptCount.get();
308307
int decrypts = decryptCount.get();
@@ -311,4 +310,16 @@ public void close() {
311310
}
312311
}
313312
}
313+
314+
/**
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.
317+
*/
318+
@SuppressWarnings("unused")
319+
private static volatile byte ZERO_FENCE;
320+
321+
static void zeroDek(byte[] dek) {
322+
Arrays.fill(dek, (byte) 0);
323+
ZERO_FENCE = dek[0];
324+
}
314325
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.util.concurrent.CompletableFuture;
1313
import java.util.concurrent.ConcurrentHashMap;
1414
import java.util.concurrent.ConcurrentMap;
15-
import java.util.Arrays;
1615
import java.util.concurrent.ExecutorService;
1716
import java.util.concurrent.Executors;
1817
import java.util.function.Function;
@@ -493,7 +492,7 @@ private CompletableFuture<CachedKey> newCachedKeyFromUnwrap(String edek,
493492
return this.encryptionService.unwrapKey(edek, metadata).thenApply(dekBytes -> {
494493
CachedKey cachedKey = new CachedKey(dekBytes, edek, this.encryptionExecutor,
495494
this.secureRandom, this.encryptionService, metadata);
496-
Arrays.fill(dekBytes, (byte) 0);
495+
CachedKey.zeroDek(dekBytes);
497496
return cachedKey;
498497
});
499498
}
@@ -503,7 +502,7 @@ private CompletableFuture<CachedKey> newCachedKeyFromWrap(DocumentMetadata metad
503502
byte[] dekBytes = wrappedKey.getDekBytes();
504503
CachedKey cachedKey = new CachedKey(dekBytes, wrappedKey.getEdek(), this.encryptionExecutor,
505504
this.secureRandom, this.encryptionService, metadata);
506-
Arrays.fill(dekBytes, (byte) 0);
505+
CachedKey.zeroDek(dekBytes);
507506
return cachedKey;
508507
});
509508
}

0 commit comments

Comments
 (0)