How to cleanse (overwrite with random bytes) std::string internal buffer?

后端 未结 3 1798
后悔当初
后悔当初 2021-02-12 06:00

Consider a scenario, where std::string is used to store a secret. Once it is consumed and is no longer needed, it would be good to cleanse it, i.e overwrit

相关标签:
3条回答
  • 2021-02-12 06:12

    It is probably safe. But not guaranteed.

    However, since C++11, a std::string must be implemented as contiguous data so you can safely access its internal array using the address of its first element &secretString[0].

    if(!secretString.empty()) // avoid UB
    {
        char* modifiable = &secretString[0];
        OpenSSL_cleanse(modifiable, secretString.size());
    }
    
    0 讨论(0)
  • 2021-02-12 06:13

    The standard explicitly says you must not write to the const char* returned by data(), so don't do that.

    There are perfectly safe ways to get a modifiable pointer instead:

    if (secretString.size())
      OpenSSL_cleanse(&secretString.front(), secretString.size());
    

    Or if the string might have been shrunk already and you want to ensure its entire capacity is wiped:

    if (secretString.capacity()) {
      secretString.resize(secretString.capacity());
      OpenSSL_cleanse(&secretString.front(), secretString.size());
    }
    
    0 讨论(0)
  • 2021-02-12 06:30

    std::string is a poor choice to store secrets. Since strings are copyable and sometimes copies go unnoticed, your secret may "get legs". Furthermore, string expansion techniques may cause multiple copies of fragments (or all of) your secrets.

    Experience dictates a movable, non-copyable, wiped clean on destroy, unintelligent (no tricky copies under-the-hood) class.

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