When should SecureZeroMemory() be used?

后端 未结 2 1722
深忆病人
深忆病人 2021-01-20 18:41

I found some sample code that looks like:

addrinfo hints;
SecureZeroMemory(&hints, sizeof(hints));

Is there a reason to use SecureZeroM

相关标签:
2条回答
  • 2021-01-20 19:20

    From MSDN:

    Use this function instead of ZeroMemory when you want to ensure that your data will be overwritten promptly, as some C++ compilers can optimize a call to ZeroMemory by removing it entirely.

    Additionally a simple assignment, w/o a barrier, may be cached by hardware and not make it to the RAM for long time. Or a local variable may be optimized away. SecureZeroMemory makes sure none of that happens.

    As to why use for initializing addrinfo, a clearly non-security related concern, beats me.

    0 讨论(0)
  • 2021-01-20 19:43

    If (for example) the variable is not used anymore in the present scope (or in any other scenario where the compiler proves it doesn't change the internal coherence the program), the compiler could optimize away the zeroing statement. For security-critical memory, this could compromise the security of the application when it comes to external processes examining the memory of yours. SecureZeroMemory is written so that it does not get optimized away.

    I can't tell for sure why this particular code snippet chooses that function over other methods of zeroing a memory range. It could be a misunderstanding of its purpose by the code's author or a misguided company policy.

    0 讨论(0)
提交回复
热议问题