<!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/dueno">Daiki Ueno</a> created an issue <a href="https://gitlab.com/gnutls/gnutls/-/issues/1194">#1194</a>:
</p>
<div></div>
<p dir="auto">We currently have bounds check logic in several places where data is read or written. A typical pattern is using one of the <code>DECR_LEN</code> macros:</p>
<pre class="code highlight js-syntax-highlight c" lang="c" v-pre="true"><code><span id="LC1" class="line" lang="c"><span class="kt">uint8_t</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="n">data</span><span class="p">;</span></span>
<span id="LC2" class="line" lang="c"><span class="kt">size_t</span> <span class="n">data_size</span> <span class="o">=</span> <span class="n">_data_size</span><span class="p">;</span></span>
<span id="LC3" class="line" lang="c"><span class="p">...</span></span>
<span id="LC4" class="line" lang="c"><span class="n">DECR_LEN</span><span class="p">(</span><span class="n">data_size</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span></span>
<span id="LC5" class="line" lang="c"><span class="n">size</span> <span class="o">=</span> <span class="n">_gnutls_read_uint24</span><span class="p">(</span><span class="n">p</span><span class="p">);</span></span>
<span id="LC6" class="line" lang="c"><span class="n">p</span> <span class="o">+=</span> <span class="mi">3</span><span class="p">;</span></span></code></pre>
<p dir="auto">While this is better than manual checks like <code>if (data_size < 3) goto error; data_size -= 3;</code>, it doesn't provide a way to enforce the check in new code.</p>
<p dir="auto">I would suggest providing a simpler (internal) API, and discouraging direct access to pointer and the length. Maybe we could reuse the existing <code>gnutls_buffer_st</code>, with a couple of new constructors for statically provided data buffers:</p>
<pre class="code highlight js-syntax-highlight c" lang="c" v-pre="true"><code><span id="LC1" class="line" lang="c"><span class="kt">void</span> <span class="nf">_gnutls_buffer_static_for_read</span><span class="p">(</span><span class="n">gnutls_buffer_st</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="k">const</span> <span class="kt">uint8_t</span> <span class="o">*</span><span class="n">data</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">size</span><span class="p">);</span></span>
<span id="LC2" class="line" lang="c"><span class="kt">void</span> <span class="nf">_gnutls_buffer_static_for_write</span><span class="p">(</span><span class="n">gnutls_buffer_st</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="k">const</span> <span class="kt">uint8_t</span> <span class="o">*</span><span class="n">data</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">max_size</span><span class="p">);</span></span></code></pre>
<p dir="auto">then the above example can be rewritten as:</p>
<pre class="code highlight js-syntax-highlight c" lang="c" v-pre="true"><code><span id="LC1" class="line" lang="c"><span class="n">gnutls_buffer_st</span> <span class="n">buf</span><span class="p">;</span></span>
<span id="LC2" class="line" lang="c"><span class="p">...</span></span>
<span id="LC3" class="line" lang="c"><span class="n">_gnutls_buffer_static_for_read</span><span class="p">(</span><span class="o">&</span><span class="n">buf</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">_data_size</span><span class="p">);</span></span>
<span id="LC4" class="line" lang="c"><span class="p">...</span></span>
<span id="LC5" class="line" lang="c"><span class="kt">size_t</span> <span class="n">size</span><span class="p">;</span></span>
<span id="LC6" class="line" lang="c"><span class="n">ret</span> <span class="o">=</span> <span class="n">_gnutls_buffer_pop_prefix24</span><span class="p">(</span><span class="o">&</span><span class="n">buf</span><span class="p">,</span> <span class="o">&</span><span class="n">size</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span></span>
<span id="LC7" class="line" lang="c"><span class="k">if</span> <span class="p">(</span><span class="n">ret</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span></span>
<span id="LC8" class="line" lang="c"> <span class="k">goto</span> <span class="n">cleanup</span><span class="p">;</span></span>
<span id="LC9" class="line" lang="c"><span class="p">}</span></span></code></pre>
<p dir="auto">Writing is similarly done:</p>
<pre class="code highlight js-syntax-highlight c" lang="c" v-pre="true"><code><span id="LC1" class="line" lang="c"><span class="n">gnutls_buffer_st</span> <span class="n">buf</span><span class="p">;</span></span>
<span id="LC2" class="line" lang="c"><span class="p">...</span></span>
<span id="LC3" class="line" lang="c"><span class="n">_gnutls_buffer_static_for_write</span><span class="p">(</span><span class="o">&</span><span class="n">buf</span><span class="p">,</span> <span class="n">ptr</span><span class="p">,</span> <span class="n">max_size</span><span class="p">);</span></span>
<span id="LC4" class="line" lang="c"><span class="p">...</span></span>
<span id="LC5" class="line" lang="c"><span class="n">ret</span> <span class="o">=</span> <span class="n">_gnutls_buffer_append_prefix</span><span class="p">(</span><span class="o">&</span><span class="n">buf</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">size</span><span class="p">);</span></span>
<span id="LC6" class="line" lang="c"><span class="k">if</span> <span class="p">(</span><span class="n">ret</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span></span>
<span id="LC7" class="line" lang="c"> <span class="k">goto</span> <span class="n">cleanup</span><span class="p">;</span></span>
<span id="LC8" class="line" lang="c"><span class="p">}</span></span></code></pre>
</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/1194">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/7d030fa2ed835717f09a75b51e310f2d/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/1194"}}</script>
</p>
</div>
</body>
</html>