sodiumpp icon indicating copy to clipboard operation
sodiumpp copied to clipboard

Usage of mlock() happens after the data is written to unlocked mem

Open tigusoft-vm opened this issue 9 years ago • 1 comments

In cases like below, we first write the secret data into variable k (in initialization list), and after that we lock this memory location.

This means that for a short time the secret data is not protected. For example if at exactly that time system would happen to swap the memory page with k, it could land in swap space etc.

It would require a very bad luck, but maybe with ephemeral keys generated many times per minute eventually it could happen in practice sometimes.

For example in https://github.com/rubendv/sodiumpp/blob/b3aab639ec9c38855d1a93b0b3df3d59a1cad77a/sodiumpp/include/sodiumpp/sodiumpp.h#L520-L522

    unboxer(const box_public_key& pk, const box_secret_key& sk, const encoded_bytes& nonce_constant) : k(crypto_box_beforenm(pk.get().to_binary(), sk.get().to_binary())), n(nonce_constant, pk.get().to_binary() > sk.pk.get().to_binary()) {
        mlock(k);
    }

tigusoft-vm avatar Apr 28 '16 10:04 tigusoft-vm

We're now writing a fix to this.

tigusoft-vm avatar Apr 28 '16 10:04 tigusoft-vm