<p>Fix buffer overflow in READ call by making a SAFE_READ that checks<br>
the actual length of the buffer.</p>

<p>In the buffer overflow case parse_hname2 is called with 'begin' set to<br>
the string "Reason:". This string was originally allocated in in<br>
rval_get_str as length 6, contents "Reason\0'. The actual pkg_malloc<br>
is size of 7 to account for the null terminator.</p>

<p>In the caller to parse_hname2 (modules/textops/textops.c line 2229)<br>
the null terminator is replaced with a ':' character.</p>

<p>parse_hname2 hits the FIRST_QUARTERNIONS macro which expands to a<br>
bunch of case statements. The one for the Reason string looks like<br>
(macro expanded):</p>

<pre><code>   case _reas_:
    p += 4;
    val = READ(p);
    switch(LOWER_DWORD(val)) {
            case _on1_:
                    hdr->type = HDR_REASON_T;
                    hdr->name.len = 6;
                    return (p + 3);
</code></pre>

<p>The overflow occurs in the READ call. READ is:</p>

<pre><code>(*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
</code></pre>

<p>With 'p' pointing to "Reason:", then p+4 is "on:". That's only three<br>
characters of allocated memory left(the : was originally the null<br>
character as explained above and the total pkg_malloc allocated length<br>
was 7). READ accesses 4 bytes so we go one past the end of the<br>
allocated area.</p>

<p>The error is noticeable in a DBG_SYS_MALLOC build but not a PKG_MALLOC<br>
build - I assume the latter has a large arena allocated making the<br>
buffer overflow still valid memory.</p>

<p>There are likely other buffer overflows in the READ usage in other cases in this function. I've <a href="http://lists.sip-router.org/pipermail/sr-dev/2015-August/030529.html">posted to the mailing list</a> about the issue and whether a more general fix is possible:</p>

<hr>

<h4>You can view, comment on, or merge this pull request online at:</h4>
<p>  <a href='https://github.com/kamailio/kamailio/pull/308'>https://github.com/kamailio/kamailio/pull/308</a></p>

<h4>Commit Summary</h4>
<ul>
  <li>Fix read buffer overflow in parse_hname2</li>
</ul>

<h4>File Changes</h4>
<ul>
  <li>
    <strong>M</strong>
    <a href="https://github.com/kamailio/kamailio/pull/308/files#diff-0">parser/case_reas.h</a>
    (2)
  </li>
  <li>
    <strong>M</strong>
    <a href="https://github.com/kamailio/kamailio/pull/308/files#diff-1">parser/parse_hname2.c</a>
    (19)
  </li>
</ul>

<h4>Patch Links:</h4>
<ul>
  <li><a href='https://github.com/kamailio/kamailio/pull/308.patch'>https://github.com/kamailio/kamailio/pull/308.patch</a></li>
  <li><a href='https://github.com/kamailio/kamailio/pull/308.diff'>https://github.com/kamailio/kamailio/pull/308.diff</a></li>
</ul>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br>Reply to this email directly or <a href="https://github.com/kamailio/kamailio/pull/308">view it on GitHub</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AF36ZXnQ9n2oLAd20Js-lLHrDfe-eM8Xks5os7u-gaJpZM4F0yRV.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/kamailio/kamailio/pull/308"></link>
  <meta itemprop="name" content="View Pull Request"></meta>
</div>
<meta itemprop="description" content="View this Pull Request on GitHub"></meta>
</div>