Any reason to use SecureZeroMemory() instead of memset() or ZeroMemory() when security is not an issue?

前提是你 提交于 2019-12-22 03:25:24

问题


This MSND article says SecureZeroMemory() is good for cases when sensitive data stored in memory for a while should be for sure overwritten as soon as possible when no longer needed. Answers to this SO question explain why this can make a difference.

Now is there any sence in using SecureZeroMemory() for initializing just every memory block? For example in one project I see code like the following:

ICONINFO ii; 
::SecureZeroMemory(&ii, sizeof(ICONINFO)); 
if (::GetIconInfo(hIcon, &ii)) 
{
    //do stuff, then 
    //release bitmaps 
    if(ii.hbmMask) 
        ::DeleteObject(ii.hbmMask); 
    if(ii.hbmColor) 
        ::DeleteObject(ii.hbmColor); 
} 

why use SecureZeroMemory() here instead of ZeroMemory(), memset() or value initialization? I mean if the compiler decides initialization is unnecessary and wants to optimize it out - why would I enforce it? Is there any reason to use SecureZeroMemory() here?


回答1:


It makes no sense to use SecureZeroMemory to initialize an icon info structure. It can only overwrite bytes on the stack frame that should have been securely erased elsewhere. That horse already escaped the barn. It doesn't even make sense to initialize it at all, the return value of GetIconInfo() tells you that it got initialized.

SecureZeroMemory() only makes sense after memory was filled with secure data.




回答2:


SecureZeroMemory is never optimized-away by a compiler. That is important if you need to worry about the contents of your memory to be cleaned, say if it contains very sensitive user info, e.g. banking software, passwords, etc. Obviously if there's no need for you to worry about such things, you can use any other way of cleaning memory buffers or not cleaning at all if it's not necessary.



来源:https://stackoverflow.com/questions/2012602/any-reason-to-use-securezeromemory-instead-of-memset-or-zeromemory-when-se

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