<i id='71loV'><tr id='71loV'><dt id='71loV'><q id='71loV'><span id='71loV'><b id='71loV'><form id='71loV'><ins id='71loV'></ins><ul id='71loV'></ul><sub id='71loV'></sub></form><legend id='71loV'></legend><bdo id='71loV'><pre id='71loV'><center id='71loV'></center></pre></bdo></b><th id='71loV'></th></span></q></dt></tr></i><div id='71loV'><tfoot id='71loV'></tfoot><dl id='71loV'><fieldset id='71loV'></fieldset></dl></div>
    <tfoot id='71loV'></tfoot>

  • <small id='71loV'></small><noframes id='71loV'>

    1. <legend id='71loV'><style id='71loV'><dir id='71loV'><q id='71loV'></q></dir></style></legend>
        <bdo id='71loV'></bdo><ul id='71loV'></ul>

        如何确保编译器优化不会带来安全风险?

        时间:2023-09-16

            <bdo id='YQVbn'></bdo><ul id='YQVbn'></ul>

            <tfoot id='YQVbn'></tfoot>
              <tbody id='YQVbn'></tbody>

                  <i id='YQVbn'><tr id='YQVbn'><dt id='YQVbn'><q id='YQVbn'><span id='YQVbn'><b id='YQVbn'><form id='YQVbn'><ins id='YQVbn'></ins><ul id='YQVbn'></ul><sub id='YQVbn'></sub></form><legend id='YQVbn'></legend><bdo id='YQVbn'><pre id='YQVbn'><center id='YQVbn'></center></pre></bdo></b><th id='YQVbn'></th></span></q></dt></tr></i><div id='YQVbn'><tfoot id='YQVbn'></tfoot><dl id='YQVbn'><fieldset id='YQVbn'></fieldset></dl></div>

                1. <small id='YQVbn'></small><noframes id='YQVbn'>

                  <legend id='YQVbn'><style id='YQVbn'><dir id='YQVbn'><q id='YQVbn'></q></dir></style></legend>
                  本文介绍了如何确保编译器优化不会带来安全风险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我必须编写一个 Windows 服务来处理机密数据(例如 PIN 码、密码等).在很短的时间内需要这些信息:通常它们几乎会立即发送到智能卡读卡器.

                  I have to write a Windows service that handles at some point confidential data (such as PIN codes, passwords, and so on). Those informations are needed for a very short amount of time: usually they are sent almost immediately to a smart card reader.

                  让我们考虑这段代码:

                  {
                    std::string password = getPassword(); // Get the password from the user
                  
                    writePasswordToSmartCard(password);
                  
                    // Okay, here we don't need password anymore.
                    // We set it all to '' so it doesn't stay in memory.
                    std::fill(password.begin(), password.end(), '');
                  }
                  

                  现在我关心的是编译器优化.这里编译器可能会检测到密码即将被删除,此时更改其值没有用,只需删除调用即可.

                  Now my concern is about compiler optimizations. Here the compiler might detect that password is about to be deleted and that changing its value at this point is useless and just remove the call.

                  我不希望我的编译器关心未来未引用内存的价值.

                  I don't expect my compiler to care about the value of future-unreferenced memory.

                  我的担忧是否合理?我怎么能确定这样一段代码不会被优化掉?

                  Are my concerns legitimate ? How can I be sure that such a piece of code won't be optimized-out ?

                  推荐答案

                  是的,您的担忧是合理的.您需要使用专门设计的函数,例如 SecureZeroMemory() 防止优化修改您的代码行为.

                  Yes, your concerns are legitimate. You need to use specifically designed function like SecureZeroMemory() to prevent optimizations from modifying your code behavior.

                  不要忘记字符串类应该是专门为处理密码而设计的.例如,如果类重新分配缓冲区以保存更长的字符串,则必须先擦除缓冲区,然后再将其返回到内存分配器.我不确定,但很可能 std::string 不会这样做(至少在默认情况下).使用不合适的字符串处理类会使您的所有担忧变得毫无价值 - 您甚至会在知道之前将密码复制到整个程序内存中.

                  Don't forget that the string class should have been specifically designed for handling passwords. For example, if the class reallocates the buffer to hold a longer string it has to erase the buffer before retunring it to the memory allocator. I'm not sure, but it's likely std::string doesn't do that (at least by default). Using an unsuitable string handling class makes all your concerns worthless - you'll have the password copied all over the program memory befoe you even know.

                  这篇关于如何确保编译器优化不会带来安全风险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我们还应该“在小范围内"进行优化吗? 下一篇:编译器是否优化了按值传递的函数参数?

                  相关文章

                  最新文章

                  <legend id='S93ko'><style id='S93ko'><dir id='S93ko'><q id='S93ko'></q></dir></style></legend>

                    <small id='S93ko'></small><noframes id='S93ko'>

                  1. <i id='S93ko'><tr id='S93ko'><dt id='S93ko'><q id='S93ko'><span id='S93ko'><b id='S93ko'><form id='S93ko'><ins id='S93ko'></ins><ul id='S93ko'></ul><sub id='S93ko'></sub></form><legend id='S93ko'></legend><bdo id='S93ko'><pre id='S93ko'><center id='S93ko'></center></pre></bdo></b><th id='S93ko'></th></span></q></dt></tr></i><div id='S93ko'><tfoot id='S93ko'></tfoot><dl id='S93ko'><fieldset id='S93ko'></fieldset></dl></div>

                    <tfoot id='S93ko'></tfoot>

                      <bdo id='S93ko'></bdo><ul id='S93ko'></ul>