<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en">
<head>
<meta content="text/html; charset=US-ASCII" http-equiv="Content-Type">
<title>
GitLab
</title>
<style>img {
max-width: 100%; height: auto;
}
</style>
</head>
<body>
<div class="content">
<p class="details" style="font-style: italic; color: #666;">
<a href="https://gitlab.com/remiolivier">remiolivier</a> created an issue <a href="https://gitlab.com/gnutls/gnutls/-/issues/1107">#1107</a>:
</p>
<div></div>
<p dir="auto">Hi,</p>
<h2 dir="auto">
<a id="user-content-description-of-problem" class="anchor" href="#description-of-problem" aria-hidden="true"></a>Description of problem:</h2>
<p dir="auto">When using GNUTLS to perform an HTTPS request with client certificate authentication, valgrind reports a memory leak issue in _gnutls_recv_server_certificate_status.</p>
<p dir="auto">There is no issue when the API is not asking for a rehandshake.</p>
<h2 dir="auto">
<a id="user-content-version-of-gnutls-used" class="anchor" href="#version-of-gnutls-used" aria-hidden="true"></a>Version of gnutls used:</h2>
<p dir="auto">3.6.15 but also in earlier versions.</p>
<h2 dir="auto">
<a id="user-content-distributor-of-gnutls-eg-ubuntu-fedora-rhel" class="anchor" href="#distributor-of-gnutls-eg-ubuntu-fedora-rhel" aria-hidden="true"></a>Distributor of gnutls (e.g., Ubuntu, Fedora, RHEL)</h2>
<p dir="auto">Ubuntu</p>
<h2 dir="auto">
<a id="user-content-how-reproducible" class="anchor" href="#how-reproducible" aria-hidden="true"></a>How reproducible:</h2>
<p dir="auto">Steps to Reproduce:</p>
<ul dir="auto">
<li>The server API is enforcing rehandshake after the data has been sent by the client</li>
<li>Use the http client example with TLS and certificate authentication</li>
<li>the client is set up with either:
gnutls_init(GNUTLS_CLIENT | GNUTLS_AUTO_REAUTH)
or
gnutls_init(GNUTLS_CLIENT | GNUTLS_NONBLOCK) and a step for rehandshake after write such as:</li>
</ul>
<pre class="code highlight js-syntax-highlight cpp" lang="cpp" v-pre="true"><code><span id="LC1" class="line" lang="cpp"><span class="kt">int</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">gnutls_record_recv</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">front</span><span class="p">.</span><span class="n">data</span><span class="p">(),</span> <span class="n">front</span><span class="p">.</span><span class="n">size</span><span class="p">());</span></span>
<span id="LC2" class="line" lang="cpp"> <span class="k">if</span> <span class="p">(</span><span class="n">ret</span> <span class="o">==</span> <span class="n">GNUTLS_E_REHANDSHAKE</span> <span class="o">&&</span> <span class="n">is_safe_renegotiation_enabled</span><span class="p">())</span></span>
<span id="LC3" class="line" lang="cpp"> <span class="n">ret</span> <span class="o">=</span> <span class="n">gnutls_handshake</span><span class="p">(</span><span class="n">session</span><span class="p">);</span></span>
<span id="LC4" class="line" lang="cpp"> <span class="c1">// ... other steps</span></span>
<span id="LC5" class="line" lang="cpp"> <span class="err">}</span></span></code></pre>
<ul dir="auto">
<li>valgrind --leak-check=full -v ./https_client</li>
</ul>
<h2 dir="auto">
<a id="user-content-actual-results" class="anchor" href="#actual-results" aria-hidden="true"></a>Actual results:</h2>
<p dir="auto">valgrind --leak-check=full -v ./https_client</p>
<pre class="code highlight js-syntax-highlight plaintext" lang="plaintext" v-pre="true"><code><span id="LC1" class="line" lang="plaintext">==9160== HEAP SUMMARY:</span>
<span id="LC2" class="line" lang="plaintext">==9160== in use at exit: 1,762 bytes in 2 blocks</span>
<span id="LC3" class="line" lang="plaintext">==9160== total heap usage: 15,866 allocs, 15,864 frees, 1,701,175 bytes allocated</span>
<span id="LC4" class="line" lang="plaintext">==9160== </span>
<span id="LC5" class="line" lang="plaintext">==9160== Searching for pointers to 2 not-freed blocks</span>
<span id="LC6" class="line" lang="plaintext">==9160== Checked 314,840 bytes</span>
<span id="LC7" class="line" lang="plaintext">==9160== </span>
<span id="LC8" class="line" lang="plaintext">==9160== 1,762 (16 direct, 1,746 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2</span>
<span id="LC9" class="line" lang="plaintext">==9160== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)</span>
<span id="LC10" class="line" lang="plaintext">==9160== by 0x4F44612: _gnutls_recv_server_certificate_status (status_request.c:497)</span>
<span id="LC11" class="line" lang="plaintext">==9160== by 0x4E84914: handshake_client (handshake.c:3011)</span>
<span id="LC12" class="line" lang="plaintext">==9160== by 0x4E84914: gnutls_handshake (handshake.c:2780)</span></code></pre>
<h2 dir="auto">
<a id="user-content-expected-results" class="anchor" href="#expected-results" aria-hidden="true"></a>Expected results:</h2>
<p dir="auto">On rehandshake a malloc is performed in status_request.c in _gnutls_recv_server_certificate_status:<br>
info->raw_ocsp_list = gnutls_malloc(sizeof(gnutls_datum_t));</p>
<p dir="auto">Whether or not it is already allocated. A check should be performed first and free or remove the previous allocated resources.</p>
<h2 dir="auto">
<a id="user-content-fix" class="anchor" href="#fix" aria-hidden="true"></a>Fix:</h2>
<ul dir="auto">
<li>A quick fix I tested and removing all the memory leak for this case is as follow:</li>
</ul>
<p dir="auto">status_request.c -> _gnutls_recv_server_certificate_status</p>
<pre class="code highlight js-syntax-highlight cpp" lang="cpp" v-pre="true"><code><span id="LC1" class="line" lang="cpp"> <span class="c1">// A handshake was already performed</span></span>
<span id="LC2" class="line" lang="cpp"> <span class="k">if</span> <span class="p">(</span><span class="n">info</span><span class="o">-></span><span class="n">raw_ocsp_list</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span></span>
<span id="LC3" class="line" lang="cpp"> <span class="p">{</span></span>
<span id="LC4" class="line" lang="cpp"> <span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o"><</span><span class="n">info</span><span class="o">-></span><span class="n">nocsp</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span></span>
<span id="LC5" class="line" lang="cpp"> <span class="n">gnutls_free</span><span class="p">(</span><span class="n">info</span><span class="o">-></span><span class="n">raw_ocsp_list</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">data</span><span class="p">);</span></span>
<span id="LC6" class="line" lang="cpp"> <span class="n">gnutls_free</span><span class="p">(</span><span class="n">info</span><span class="o">-></span><span class="n">raw_ocsp_list</span><span class="p">);</span></span>
<span id="LC7" class="line" lang="cpp"> <span class="p">}</span></span>
<span id="LC8" class="line" lang="cpp"></span>
<span id="LC9" class="line" lang="cpp"> <span class="n">info</span><span class="o">-></span><span class="n">raw_ocsp_list</span> <span class="o">=</span> <span class="n">gnutls_malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">gnutls_datum_t</span><span class="p">));</span></span></code></pre>
<p dir="auto">Thanks</p>
</div>
<div class="footer" style="margin-top: 10px;">
<p style="font-size: small; color: #666;">
—
<br>
Reply to this email directly or <a href="https://gitlab.com/gnutls/gnutls/-/issues/1107">view it on GitLab</a>.
<br>
You're receiving this email because of your account on gitlab.com.
If you'd like to receive fewer emails, you can
<a href="https://gitlab.com/-/sent_notifications/d217b5876f076dba27af0dd6763a35a8/unsubscribe">unsubscribe</a>
from this thread or
adjust your notification settings.
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ViewAction","name":"View Issue","url":"https://gitlab.com/gnutls/gnutls/-/issues/1107"}}</script>
</p>
</div>
</body>
</html>