简体   繁体   English

Java 11-如何覆盖JVM和系统内存中的敏感信息(也许使用System.gc()?)

[英]Java 11 - How to overwrite sensitive information in JVM- AND System-Memory (perhaps using System.gc()?)

Problem: How to forcefully overwrite system-memory in Java - More specifically: When secure keys must not stay longer than a few seconds in the memory: Neither in the jvm-memory, nor in the OS-Memory? 问题:如何用Java强制覆盖系统内存- 更具体地说:当安全密钥在内存中的停留时间不得超过几秒钟时:既不在jvm内存中也不在OS-Memory中?

What I tried: Modifying the java garbage-collector implementation, so that it would for sure overwrite objects with random bytes, instead of just freeing them. 我尝试过的操作:修改java垃圾收集器实现,以确保使用随机字节覆盖对象,而不是仅仅释放它们。 However: 然而:

Everything I read about System.gc() showed me that I can never really rely on it, and that one should not use it in production. 我所读到的有关System.gc()一切都告诉我,我永远无法真正依赖它,并且不应在生产中使用它。 However, what if I know that I use a specific JVM? 但是,如果我知道我使用特定的JVM怎么办? What if I know that I want to use the OpenJDK with Java 11 and that I configure the JVM to use a specific GC Implementation (to not let the JVM choose the GC)? 如果我知道我想将OpenJDK与Java 11一起使用并且我将JVM配置为使用特定的GC实现(而不是让JVM选择GC)怎么办?

Question 1: Can I then somehow be sure that System.gc() will trigger garbage collection 100% times? 问题1:然后我可以以某种方式确定System.gc()将触发垃圾回收100%次吗?

Question 2: Can I find out, what's the maximum duration between System.gc() has been called and the actual garbage collection will start? 问题2:我能否找出System.gc()之间的最大持续时间是多少,实际的垃圾回收将开始? This is a significant part of the question! 这是问题的重要部分! I could only find answers to the garbage-collection-efficiency itself (eg the throughput vs stop-the-world pause-times), but that is NOT the answer to this question. 我只能找到垃圾收集效率本身的答案(例如,吞吐量与世界停顿的暂停时间),但这并不是该问题的答案。 (Read myself through the whole documentation here ) 在此处阅读整个文档

Question 3: If the idea with the modified garbage collector is by far the worst idea to securely overwrite each occurence of various sensible java objects in the memory, then how could I otherwise overwrite those objects in the memory? 问题3:如果使用修改后的垃圾收集器的想法是迄今为止最糟糕的想法,那就是安全地覆盖内存中各种明智的Java对象的每次出现,那么我又如何才能覆盖内存中的那些对象呢? Is it even possible with Java? Java甚至可能吗? It would be nice to be able to delete and overwrite these objects directly in the java-code, similar to freeing objects in C/C++? 能够直接在Java代码中删除和覆盖这些对象,就像在C / C ++中释放对象一样,将是很好的选择? Are there other possibilities maybe outside java where I can overwrite each occurrence of such sensible information in the memory, which would have to be triggered as instantly as possible as soon as the java object is no longer in use? 在Java之外是否还有其他可能性,我可以覆盖内存中每次出现的此类敏感信息,一旦不再使用Java对象,就必须立即立即触发这些信息?

My research so far: 到目前为止,我的研究:

As you can see those are, except for the official docs, quite old, so: 如您所见,除了官方文档,这些都是相当古老的,所以:

Question 4: Are there any newer insights available to the concern whether System.gc() behaves the same like 10 years ago?? 问题4:是否有任何新的见解可以关注System.gc()行为是否与10年前一样? Thanks! 谢谢!

*EDIT: I already use byte-arrays for the cases where those can be used. *编辑:对于已经可以使用字节数组的情况,我已经使用了字节数组。 The question is about more complex Java-Objects with various different fields and properties, which have to be cleaned completely in the memory. 问题是关于具有各种不同字段和属性的更复杂的Java对象,这些对象必须在内存中完全清除。

Assuming you can store the security key in a byte[] or other primitive array it should be enough to zero the array after the key was read: 假设您可以将安全密钥存储在byte[]或其他原始数组中,则在读取密钥后足以将数组清零:

for (int i = 0; i < key.length; i++) {
  key[i] = 0;
}

Above should result in byte[] key being fully overridden. 以上应该导致byte[] key被完全覆盖。 Relying on GC here would be a mistake as it's unpredictable. 在这里依靠GC将是一个错误,因为它是无法预测的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM