Skip to content

crypto: call CRYPTO_secure_malloc_done - #36976

Closed
jasnell wants to merge 2 commits into
nodejs:masterfrom
jasnell:try-secure-heap-asan-fix
Closed

jasnell wants to merge 2 commits into
nodejs:masterfrom
jasnell:try-secure-heap-asan-fix

Conversation

@jasnell

@jasnell jasnell commented Jan 17, 2021 •

Copy link
Copy Markdown
Member

See if this fixes the asan error in test-crypto-secure-heap

Do not land this yet... as it's not ready to go, despite the sign offs

@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Jan 17, 2021
@jasnell
jasnell marked this pull request as draft January 17, 2021 18:49
@jasnell
jasnell marked this pull request as ready for review January 17, 2021 18:55
@jasnell
jasnell force-pushed the try-secure-heap-asan-fix branch 2 times, most recently from 2fbc5f4 to b545f9d Compare January 17, 2021 19:14
@jasnell
jasnell marked this pull request as draft January 17, 2021 22:04
@jasnell

jasnell commented Jan 17, 2021

Copy link
Copy Markdown
Member Author

@richsalz ... would you have any suggestions on this ... looks like when using the secure heap allocator the DBRG is holding on to some memory causing our asan test to fail...

https://github.com/nodejs/node/pull/36976/checks?check_run_id=1717756455

Indirect leak of 264 byte(s) in 1 object(s) allocated from:
    #0 0xa46a1d in malloc (/home/runner/work/node/node/out/Release/node+0xa46a1d)
    #1 0x302388b in CRYPTO_zalloc (/home/runner/work/node/node/out/Release/node+0x302388b)
    #2 0x2ff6b4a in EVP_CipherInit_ex (/home/runner/work/node/node/out/Release/node+0x2ff6b4a)
    #3 0x3080513 in drbg_ctr_init (/home/runner/work/node/node/out/Release/node+0x3080513)
    #4 0x30890ad in RAND_DRBG_set (/home/runner/work/node/node/out/Release/node+0x30890ad)
    #5 0x3089524 in rand_drbg_new (/home/runner/work/node/node/out/Release/node+0x3089524)
    #6 0x308c845 in RAND_DRBG_get0_private (/home/runner/work/node/node/out/Release/node+0x308c845)
    #7 0x308f483 in RAND_priv_bytes (/home/runner/work/node/node/out/Release/node+0x308f483)
    #8 0x2ed8547 in bnrand (/home/runner/work/node/node/out/Release/node+0x2ed8547)
    #9 0x2ed8aaf in bnrand_range (/home/runner/work/node/node/out/Release/node+0x2ed8aaf)
    #10 0x2ed6063 in BN_is_prime_fasttest_ex (/home/runner/work/node/node/out/Release/node+0x2ed6063)
    #11 0x2ed5084 in BN_generate_prime_ex (/home/runner/work/node/node/out/Release/node+0x2ed5084)
    #12 0x2f390b4 in DH_generate_parameters_ex (/home/runner/work/node/node/out/Release/node+0x2f390b4)
    #13 0x10d7272 in node::crypto::DiffieHellman::New(v8::FunctionCallbackInfo<v8::Value> const&) (/home/runner/work/node/node/out/Release/node+0x10d7272)

@jasnell
jasnell marked this pull request as ready for review January 17, 2021 22:12
@richsalz

Copy link
Copy Markdown

Do you load openssl dynamically? If so, do you explicilty unload it? I can ping folks for details on how to cleanup, but first those questions.

@jasnell

jasnell commented Jan 17, 2021

Copy link
Copy Markdown
Member Author

Do you load openssl dynamically? If so, do you explicilty unload it? I can ping folks for details on how to cleanup, but first those questions.

Here's the init logic... https://github.com/nodejs/node/blob/master/src/crypto/crypto_util.cc#L102-L163

There is no explicit cleanup unless I've missed something (/cc @addaleax @joyeecheung)

@jasnell
jasnell marked this pull request as draft January 17, 2021 23:48
@richsalz

Copy link
Copy Markdown

Ping @paulidale and @mattcaswell; maybe they can help with freeing up secure heap?

@paulidale

Copy link
Copy Markdown

Nothing jumps out at me as forcing a provider or library context reference count increment.
I think that this is the most likely culprit.

If @mattcaswell doesn't have a look, I can once my vacation finishes.

@jasnell

jasnell commented Jan 18, 2021

Copy link
Copy Markdown
Member Author

@paulidale ... Is there a simple way of checking the current ref count?

@paulidale

Copy link
Copy Markdown

Sadly, no. We should have a reference count trace capability given the time these issues take to track down.

@mattcaswell

Copy link
Copy Markdown

Which OpenSSL version is this?

@targos

targos commented Jan 18, 2021

Copy link
Copy Markdown
Member

It's version 1.1.1i

@mattcaswell

Copy link
Copy Markdown

You shouldn't need to call CRYPTO_secure_malloc_done explicitly. It should get called automatically by the OpenSSL auto-deinit code when the process dies.

I tried to create a standalone C reproducer of this leak, but I was unsuccessful. If anyone can help me with that then I can investigate further. Otherwise it might need @paulidale to return from vacation.

@jasnell

jasnell commented Jan 18, 2021

Copy link
Copy Markdown
Member Author

Thanks @mattcaswell and @paulidale ... I'll keep digging but anything does happen to spring to mind, please do let me know. My plan currently is to inject some tracing logic into openssl and see if I can spot anything. At the very least I want to check whether the implicit call to CRYPTO_secure_malloc_done is actually happening.

@jasnell
jasnell marked this pull request as ready for review January 18, 2021 16:23
@jasnell
jasnell force-pushed the try-secure-heap-asan-fix branch from 8f808f2 to 9699aad Compare January 18, 2021 17:42
@jasnell

jasnell commented Jan 18, 2021

Copy link
Copy Markdown
Member Author

Ok, well... I've been able to at least verify that the leak (a) is definitely being caused by enabling the secure heap and (b) the implicit cleanup is happening with secure heap use reporting 0 at cleanup... making it even less clear exactly why we're getting the failure report here. Still investigating.

@jasnell

jasnell commented Jan 18, 2021

Copy link
Copy Markdown
Member Author

At this point I've really no idea why the asan failure is happening. @addaleax ... you might be able to have some better luck with it but as far as I can see, all the memory is being cleaned up correctly.

@jasnell
jasnell marked this pull request as draft January 18, 2021 20:57
@jasnell jasnell closed this Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants