Repository navigation
Fix native crypto memory leaks - #209
Merged
Merged
Conversation
…ipe them after usage GetStringUTFChars() was never paired with ReleaseStringUTFChars(), every call leaked a copy of the vault password and of the plaintext to encrypt. GetStringUTFRegion() writes into our own buffer instead, which is wiped after use. SJCL::decrypt() takes its arguments by reference, so there are no further copies of the password. Signed-off-by: binsky <timo@binsky.org>
The base64 results are allocated with new and their data with malloc, but only the struct was released with free(). The result of SJCL::encrypt() was never released. Signed-off-by: binsky <timo@binsky.org>
The decrypted plaintext stayed in the native heap, as well as the decoded ciphertext, salt and IV. The parsed JSON was released with free() instead of delete, which also leaked its values in the memory. Signed-off-by: binsky <timo@binsky.org>
Signed-off-by: binsky <timo@binsky.org>
Signed-off-by: binsky <timo@binsky.org>
Encrypts and decrypts a value 1000 times each and checks that the native heap doesn't grow. Before the fixes, the heap grew by about 40 KB per decryption and 48 KB per encryption of a 2 KB value. Signed-off-by: binsky <timo@binsky.org>
1 task done
Signed-off-by: binsky <timo@binsky.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The native SJCL crypto leaked memory on every encryption and decryption. Some of the leaked buffers held passwords, derived keys and decrypted plaintext, so these secrets stayed in the native heap.
GetStringUTFRegioninstead ofGetStringUTFChars. The JNI copies were never released, and they held the vault password and the plaintext. The password and plaintext are now wiped after use, andSJCL::decrypttakes the password by const reference instead of copying it.free, although it was allocated withnew) and its data leaked. The encrypted result was never freed at all.OPENSSL_cleansebefore they're freed, in both directions.NativeCryptoMemoryTestruns 1000 encryptions and 1000 decryptions of a 2 KB value with non-ASCII characters and checks the native heap growth.🤖 AI (if applicable)