-
Notifications
You must be signed in to change notification settings - Fork 717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
s2n_drbg: de-allocate thread-local allocations at thread exit #3530
Conversation
s2n_init_drbgs allocates, via s2n_drbg_instantiate, dynamic memory to fields of a static, thread-local struct. For this reason, the de-allocation/clean-up of these fields can only happen through the same thread that created them. Else the allocations will leak. Hence track these allocations as thread-specific data, using an allocation key and a destructor function that gets called at thread exit. This resolves aws#3525 (tested via LSAN sanitizer).
Thanks for the submission, we'll take a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you include a test the reproduces and then fixes the memory leak that you're describing?
@lrstewart - I added an --- a/utils/s2n_random.c
+++ b/utils/s2n_random.c
@@ -136,7 +136,7 @@ S2N_RESULT s2n_get_mix_entropy(struct s2n_blob *blob)
static void s2n_drbg_destructor(void *_unused_argument) {
(void)_unused_argument;
- s2n_result_ignore(s2n_rand_cleanup_thread());
+ //s2n_result_ignore(s2n_rand_cleanup_thread());
}
static void s2n_drbg_make_alloc_key(void) |
Condition can be fixed in
|
s2n_init_drbgs
allocates, vias2n_drbg_instantiate
, dynamic memory to fields of a static, thread-local struct. For this reason, the de-allocation of these fields should be done by the same thread that created them. Else the allocations will leak.Hence track these allocations as thread-specific data, using an allocation key and a destructor function called at thread exit.
Resolved issues: Resolves #3525.
Description of changes:
Ensure that the same thread that dynamically allocates memory to fields of the thread-local storage allocated by
s2n_drbg_instantiate (and the functions it calls) is de-allocated by the same thread.
Currently the de-allocation happens from another thread, which does not help.
Call-outs:
Please revise the use of
s2n_rand_cleanup_thread
with regard tos2n_drbg_wipe
. Thedrbg
de-allocation couldhappen entirely in the destructor. That would render
s2n_rand_cleanup_thread
either empty, or it could be usedfor other clean-up tasks.
Testing:
Ran the test suite. Ran tests with LSAN (before/after) to ensure that the leaks have been fixed.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.