Erasing sensitive information from memory

梦想的初衷 提交于 2019-12-13 15:14:27

问题


After reading this question I'm curious how one would do this in C. When receiving the information from another program, we probably have to assume that the memory is writable.

I have found this stating that a regular memset maybe optimized out and this comment stating that memsets are the wrong way to do it.


回答1:


The example you have provided is not quite valid: the compiler can optimize out a variable setting operation when it can detect that there are no side effects and the value is no longer used.

So, if your code uses some shared buffer, accessible from multiple locations, the memset would work fine. Almost.

Different processors use different caching policies, so you might have to use memory barriers to ensure the data (zero's) have reached memory chip from the cache.

So, if you are not worried about hardware level details, making sure compiler can't optimize out operation is sufficient. For example, memsetting block before releasing it would be executed.

If you want to ensure the data is removed from all hardware items, you need to check how the data caching is implemented on your platform and use appropriate code to force cache flush, which can be non-trivial on multi-core machine.



来源:https://stackoverflow.com/questions/28483713/erasing-sensitive-information-from-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!