4

I'm using nlohmann/json library to represent sensitive information. Once the needed processing has been completed, I'm interested in securely erasing the keys of the json type.

Example:

json test;
test["key1"] = "value1";
test["key2"] = "value2";

for (auto item = test.begin(); item != test.end(); ++item) {
    // Processing going on here
    // Let's print this information to simulate that it's "used"
    // In the real application no data is printed / stored here,
    // just processed.
    std::cout << item.value().dump();
    std::cout << item.key().dump();
    // This particular key is not needed anymore here
}

// Keys are not needed here. How can I be sure 
// that "key1" and "key2" are guaranteed 
// not present in memory in any shape or form?

// Will this achieve my goal?
test.erase(test.begin(), test.end());

The result I'm trying to achieve is similar to what can be accomplished with the memset_s method or SecureZeroMemory on Microsoft platform.

Alternatively, a string contents can be replaced with some arbitrary information with the help of the std::fill algorithm:

std::fill(string.begin(), string.end(), 0);

My question is, would the proposed approach of calling erase on the json completely remove the keys from the memory, or is there a chance that contents of those strings still will be present in the memory?

Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
  • 2
    Just a couple of notes: It doesn't matter if you print the secret information to a terminal or write to a file, it will probably stay in in-memory buffers for a while. And unless you write to an encrypted file (or filesystem) then the data will also be available in plain-text on disk. Lastly, security through obscurity only makes life harder for the programmers and users, and almost never for potential "hackers". – Some programmer dude Oct 27 '21 at 10:57
  • 1
    Printing sensitive information to a terminal is added just as a reference that the information is used in some way in that loop. It is obviously not printed nor written to a file. – Richard Topchii Oct 27 '21 at 10:58

0 Answers0