<div dir="ltr">Hi all,<div><br></div><div>I think I found the way to fix the memory leak in my application. Just floating it here in case it helps out someone else.</div><div>The answer was on the wiki page for <span style="background-color:rgb(249,249,249);color:rgb(0,0,0);font-family:monospace,Courier;font-size:12.7px;white-space:pre-wrap">SSL_CTX_add_extra_chain_cert():</span></div><div><span style="background-color:rgb(249,249,249);color:rgb(0,0,0);font-family:monospace,Courier;font-size:12.7px;white-space:pre-wrap"><br></span></div><div><span style="background-color:rgb(249,249,249);color:rgb(0,0,0);font-family:monospace,Courier;font-size:12.7px;white-space:pre-wrap">"</span><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px">The </span><b style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px">x509</b><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px"> certificate provided to SSL_CTX_add_extra_chain_cert() will be freed by the library when the </span><b style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px">SSL_CTX</b><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px"> is destroyed. An application </span><b style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px">should not</b><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px"> free the </span><b style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px">x509</b><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.7px"> object.</span><span style="background-color:rgb(249,249,249);color:rgb(0,0,0);font-family:monospace,Courier;font-size:12.7px;white-space:pre-wrap">"</span></div><div><span style="background-color:rgb(249,249,249);color:rgb(0,0,0);font-family:monospace,Courier;font-size:12.7px;white-space:pre-wrap"><br></span></div><div>The trick was to realize that the cert added via SSL_CTX_use_certificate() can be (and should be, I think) free'd manually right after this call. Otherwise you've got a memory leak on your hands. But the certs added using SSL_CTX_add_extra_chain_cert() should not be free'd up manually - those are cleaned up SSL_CTX_free later at the end of the application.</div><div><br></div><div>After doing this, the memory leak and the crash both went away.</div><div><br></div><div>Thanks,</div><div>-Pratyush.</div><div class="gmail_extra"><br><div class="gmail_quote"><br></div><div class="gmail_quote">On Sun, Jan 28, 2018 at 10:20 PM, J Decker <span dir="ltr"><<a href="mailto:d3ck0r@gmail.com" target="_blank">d3ck0r@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="gmail-">On Sun, Jan 28, 2018 at 7:05 PM, pratyush parimal <span dir="ltr"><<a href="mailto:pratyush.parimal@gmail.com" target="_blank">pratyush.parimal@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi all,<div><br></div><div>I'm trying to write an application in which I create an instance of SSL_CTX* using SSL_CTX_new(), and set the following things in it:</div><div><br></div><div>(1) An EVP_PKEY* :</div><div>1a> created with PEM_read_bio_PrivateKey()<wbr>.</div><div>1b> set in the ctx using SSL_CTX_use_PrivateKey().</div></div></blockquote></span><div>after setting key, free key </div><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>(2) A number of X509* instances (cuz chained certs) :</div><div>2a> all created with PEM_read_bio_X509().</div><div>2b> set in the ctx using SSL_CTX_use_certificate() or SSL_CTX_add_extra_chain_cert()<wbr>.</div></div></blockquote></span><div>after setting certs, free certs. </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-"><div dir="ltr"><div><br></div><div>At the end, I use SSL_CTX_free() to free up the ctx. According to the man page for SSL_CTX_free():</div><div><br></div><div><div>"SSL_CTX_free() decrements the reference count of ctx, and removes the SSL_CTX object pointed to by ctx and frees up the allocated memory if the the reference count has reached 0.</div><div>       It also calls the free()ing procedures for indirectly affected items, if applicable: the session cache, the list of ciphers, the list of Client CAs, the certificates and keys. "</div></div><div><br></div><div>... which tells me that freeing the SSL_CTX should free up its memory as well as the things I set inside of it (unless I'm interpreting it super wrong?) like " ... certificates and keys".</div><div>The problem is, when run my application under valgrind, I keep seeing memory leaks for both steps (1a) and (2a).</div><div><br></div><div>I tried to get rid of them, by using EVP_PKEY_free() after I'm done setting in step (1b). This works, and the leak for step (1a) goes away.</div><div>When I try to do the same for step (2), i.e. calling X509_free() after every successful "set" call, I get a coredump (backtrace is attached: bt_1.txt), coming out of SSL_CTX_free, suggesting that I did something wrong.</div><div><br></div><div><br></div><div>Which brings me to my question, does anyone know the correct way to free memory in SSL_CTX ? Or, what's wrong with my steps? The application doesn't even perform SSL yet, I'm just trying to create/destroy SSL_CTX objects without leaks first. Any help would be appreciated!</div><div><br></div><div><br></div><div>Thanks in advance,</div><div>-Pratyush.</div></div>
<br></span><span class="gmail-HOEnZb"><font color="#888888">--<br>
openssl-users mailing list<br>
To unsubscribe: <a href="https://mta.openssl.org/mailman/listinfo/openssl-users" rel="noreferrer" target="_blank">https://mta.openssl.org/mailma<wbr>n/listinfo/openssl-users</a><br>
<br></font></span></blockquote></div><br></div></div>
<br>--<br>
openssl-users mailing list<br>
To unsubscribe: <a href="https://mta.openssl.org/mailman/listinfo/openssl-users" rel="noreferrer" target="_blank">https://mta.openssl.org/<wbr>mailman/listinfo/openssl-users</a><br>
<br></blockquote></div><br></div></div>