[prev in list] [next in list] [prev in thread] [next in thread] 

List:       asterisk-dev
Subject:    Re: [asterisk-dev] [Code Review] 2783: Fix SIP/TLS reading - random connection drop
From:       "David Lee" <reviewboard () asterisk ! org>
Date:       2013-08-29 20:25:58
Message-ID: 20130829202558.23400.37328 () hotblack ! digium ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviewboard.asterisk.org/r/2783/#review9562
-----------------------------------------------------------


If you can get your hands on a copy of "Network Security with
OpenSSL"[1], check out the section on non-blocking I/O (somewhere in the
depths of Chapter 5 in my edition).

 [1]: http://shop.oreilly.com/product/9780596002701.do


/trunk/main/tcptls.c
<https://reviewboard.asterisk.org/r/2783/#comment18678>

    Maybe it happens less often, but ssl_write would have the same problem.



/trunk/main/tcptls.c
<https://reviewboard.asterisk.org/r/2783/#comment18676>

    An SSL_read() may, ironically, fail with an SSL_ERROR_WANT_WRITE. A
    read may require a renegotiation, which can happen at any time.



/trunk/main/tcptls.c
<https://reviewboard.asterisk.org/r/2783/#comment18677>

    This has the possibility of turning what's normally a non-blocking
    read into a blocking read. I have no idea what the broader
    implications of that might be, if any.


- David Lee


On Aug. 21, 2013, 3:24 p.m., Tzafrir Cohen wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviewboard.asterisk.org/r/2783/
> -----------------------------------------------------------
> 
> (Updated Aug. 21, 2013, 3:24 p.m.)
> 
> 
> Review request for Asterisk Developers.
> 
> 
> Bugs: ASTERISK-18345
> https://issues.asterisk.org/jira/browse/ASTERISK-18345
> 
> 
> Repository: Asterisk
> 
> 
> Description
> -------
> 
> Symptom: Asterisk drops a SIP/TLS connection: debugging reports that it has failed \
> to read it. 
> I can reproduce this on my system when the TLS client is Asterisk 11.5 (installed \
> from the Debian package) set with 'allow=all' to get a long list of codecs. 
> Calling ast_wait_for_input before every fgets is not sufficient.
> Function fgets internally calls read (=> SSL_read) until either "\n" or
> eof is found. And because the socket is polled only before the first
> SSL_read call, consequent calls can fail and return <=0 even though the
> data are on the way.
> 
> This is fixed by adding a read() loop inside the ssl_read() hook.
> 
> I came accross this patch today and it looks like it fixes my problem (see my \
> comment at the end). The patch I used is by Filip Jenicek. See the bug report for \
> the full log. 
> 
> Diffs
> -----
> 
> /trunk/main/tcptls.c 397346 
> 
> Diff: https://reviewboard.asterisk.org/r/2783/diff/
> 
> 
> Testing
> -------
> 
> Work on trunk.
> 
> 
> Thanks,
> 
> Tzafrir Cohen
> 
> 


[Attachment #5 (text/html)]

<html>
 <body>
  <div style="font-family: Verdana, Arial, Helvetica, Sans-Serif;">
   <table bgcolor="#f9f3c9" width="100%" cellpadding="8" style="border: 1px #c9c399 \
solid;">  <tr>
     <td>
      This is an automatically generated e-mail. To reply, visit:
      <a href="https://reviewboard.asterisk.org/r/2783/">https://reviewboard.asterisk.org/r/2783/</a>
  </td>
    </tr>
   </table>
   <br />





 <pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: \
-pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">If you can get your \
hands on a copy of &quot;Network Security with OpenSSL&quot;[1], check out the \
section on non-blocking I/O (somewhere in the depths of Chapter 5 in my edition).

 [1]: http://shop.oreilly.com/product/9780596002701.do</pre>
 <br />







<div>




<table width="100%" border="0" bgcolor="white" style="border: 1px solid #C0C0C0; \
border-collapse: collapse; margin: 2px padding: 2px;">  <thead>
  <tr>
   <th colspan="4" bgcolor="#F0F0F0" style="border-bottom: 1px solid #C0C0C0; \
font-size: 9pt; padding: 4px 8px; text-align: left;">  <a \
href="https://reviewboard.asterisk.org/r/2783/diff/1/?file=44334#file44334line59" \
style="color: black; font-weight: bold; text-decoration: \
underline;">/trunk/main/tcptls.c</a>  <span style="font-weight: normal;">

     (Diff revision 1)

    </span>
   </th>
  </tr>
 </thead>

 <tbody style="background-color: #e4d9cb; padding: 4px 8px; text-align: center;">
  <tr>

   <td colspan="4"><pre style="font-size: 8pt; line-height: 140%; margin: 0; \
">ASTERISK_FILE_VERSION(__FILE__, &quot;$Revision$&quot;)</pre></td>

  </tr>
 </tbody>



 
 

 <tbody>

  <tr>
    <th bgcolor="#f0f0f0" style="border-right: 1px solid #C0C0C0;" \
align="right"><font size="2">59</font></th>  <td bgcolor="#ffffff" width="50%"><pre \
style="font-size: 8pt; line-height: 140%; margin: 0; "><span class="k">static</span> \
<span class="n">HOOK_T</span> <span class="n">ssl_read</span><span \
class="p">(</span><span class="kt">void</span> <span class="o">*</span><span \
class="n">cookie</span><span class="p">,</span> <span class="kt">char</span> <span \
class="o">*</span><span class="n">buf</span><span class="p">,</span> <span \
class="n">LEN_T</span> <span class="n">len</span><span class="p">)</span></pre></td>  \
<th bgcolor="#f0f0f0" style="border-left: 1px solid #C0C0C0; border-right: 1px solid \
#C0C0C0;" align="right"><font size="2">59</font></th>  <td bgcolor="#ffffff" \
width="50%"><pre style="font-size: 8pt; line-height: 140%; margin: 0; "><span \
class="k">static</span> <span class="n">HOOK_T</span> <span \
class="n">ssl_read</span><span class="p">(</span><span class="kt">void</span> <span \
class="o">*</span><span class="n">cookie</span><span class="p">,</span> <span \
class="kt">char</span> <span class="o">*</span><span class="n">buf</span><span \
class="p">,</span> <span class="n">LEN_T</span> <span class="n">len</span><span \
class="p">)</span></pre></td>  </tr>

 </tbody>

</table>

<pre style="margin-left: 2em; white-space: pre-wrap; white-space: -moz-pre-wrap; \
white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">Maybe it \
happens less often, but ssl_write would have the same problem.</pre> </div>
<br />

<div>




<table width="100%" border="0" bgcolor="white" style="border: 1px solid #C0C0C0; \
border-collapse: collapse; margin: 2px padding: 2px;">  <thead>
  <tr>
   <th colspan="4" bgcolor="#F0F0F0" style="border-bottom: 1px solid #C0C0C0; \
font-size: 9pt; padding: 4px 8px; text-align: left;">  <a \
href="https://reviewboard.asterisk.org/r/2783/diff/1/?file=44334#file44334line64" \
style="color: black; font-weight: bold; text-decoration: \
underline;">/trunk/main/tcptls.c</a>  <span style="font-weight: normal;">

     (Diff revision 1)

    </span>
   </th>
  </tr>
 </thead>

 <tbody style="background-color: #e4d9cb; padding: 4px 8px; text-align: center;">
  <tr>

   <td colspan="4"><pre style="font-size: 8pt; line-height: 140%; margin: 0; \
">ASTERISK_FILE_VERSION(__FILE__, &quot;$Revision$&quot;)</pre></td>

  </tr>
 </tbody>



 
 

 <tbody>

  <tr>
    <th bgcolor="#b1ebb0" style="border-right: 1px solid #C0C0C0;" \
align="right"><font size="2"></font></th>  <td bgcolor="#c5ffc4" width="50%"><pre \
style="font-size: 8pt; line-height: 140%; margin: 0; "></pre></td>  <th \
bgcolor="#b1ebb0" style="border-left: 1px solid #C0C0C0; border-right: 1px solid \
#C0C0C0;" align="right"><font size="2">64</font></th>  <td bgcolor="#c5ffc4" \
width="50%"><pre style="font-size: 8pt; line-height: 140%; margin: 0; "><span \
class="tb">	</span><span class="k">while</span> <span class="p">(</span><span \
class="n">i</span> <span class="o">&lt;</span> <span class="mi">0</span> <span \
class="o">&amp;&amp;</span> <span class="n">SSL_get_error</span><span \
class="p">(</span><span class="n">cookie</span><span class="p">,</span> <span \
class="n">i</span><span class="p">)</span> <span class="o">==</span> <span \
class="n">SSL_ERROR_WANT_READ</span><span class="p">)</span> <span \
class="p">{</span></pre></td>  </tr>

 </tbody>

</table>

<pre style="margin-left: 2em; white-space: pre-wrap; white-space: -moz-pre-wrap; \
white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">An \
SSL_read() may, ironically, fail with an SSL_ERROR_WANT_WRITE. A read may require a \
renegotiation, which can happen at any time.</pre> </div>
<br />

<div>




<table width="100%" border="0" bgcolor="white" style="border: 1px solid #C0C0C0; \
border-collapse: collapse; margin: 2px padding: 2px;">  <thead>
  <tr>
   <th colspan="4" bgcolor="#F0F0F0" style="border-bottom: 1px solid #C0C0C0; \
font-size: 9pt; padding: 4px 8px; text-align: left;">  <a \
href="https://reviewboard.asterisk.org/r/2783/diff/1/?file=44334#file44334line66" \
style="color: black; font-weight: bold; text-decoration: \
underline;">/trunk/main/tcptls.c</a>  <span style="font-weight: normal;">

     (Diff revision 1)

    </span>
   </th>
  </tr>
 </thead>

 <tbody style="background-color: #e4d9cb; padding: 4px 8px; text-align: center;">
  <tr>

   <td colspan="4"><pre style="font-size: 8pt; line-height: 140%; margin: 0; \
">ASTERISK_FILE_VERSION(__FILE__, &quot;$Revision$&quot;)</pre></td>

  </tr>
 </tbody>



 
 

 <tbody>

  <tr>
    <th bgcolor="#b1ebb0" style="border-right: 1px solid #C0C0C0;" \
align="right"><font size="2"></font></th>  <td bgcolor="#c5ffc4" width="50%"><pre \
style="font-size: 8pt; line-height: 140%; margin: 0; "></pre></td>  <th \
bgcolor="#b1ebb0" style="border-left: 1px solid #C0C0C0; border-right: 1px solid \
#C0C0C0;" align="right"><font size="2">66</font></th>  <td bgcolor="#c5ffc4" \
width="50%"><pre style="font-size: 8pt; line-height: 140%; margin: 0; "><span \
class="tb">	</span><span class="tb">	</span><span class="k">if</span> <span \
class="p">(</span><span class="n">ast_wait_for_input</span><span \
class="p">(</span><span class="n">SSL_get_fd</span><span class="p">(</span><span \
class="n">cookie</span><span class="p">),</span> <span class="mi">5000</span><span \
class="p">)</span> <span class="o">&lt;=</span> <span class="mi">0</span><span \
class="p">)</span> <span class="k">return</span> <span class="mi">0</span><span \
class="p">;</span></pre></td>  </tr>

 </tbody>

</table>

<pre style="margin-left: 2em; white-space: pre-wrap; white-space: -moz-pre-wrap; \
white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">This has \
the possibility of turning what&#39;s normally a non-blocking read into a blocking \
read. I have no idea what the broader implications of that might be, if any.</pre>
</div>
<br />



<p>- David</p>


<br />
<p>On August 21st, 2013, 3:24 p.m. CDT, Tzafrir Cohen wrote:</p>








<table bgcolor="#fefadf" width="100%" cellspacing="0" cellpadding="8" \
style="background-image: \
url('https://reviewboard.asterisk.org/static/rb/images/review_request_box_top_bg.png'); \
background-position: left top; background-repeat: repeat-x; border: 1px black \
solid;">  <tr>
  <td>

<div>Review request for Asterisk Developers.</div>
<div>By Tzafrir Cohen.</div>


<p style="color: grey;"><i>Updated Aug. 21, 2013, 3:24 p.m.</i></p>







<div style="margin-top: 1.5em;">
 <b style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Bugs: </b>


 <a href="https://issues.asterisk.org/jira/browse/ASTERISK-18345">ASTERISK-18345</a>


</div>



<div style="margin-top: 1.5em;">
 <b style="color: #575012; font-size: 10pt;">Repository: </b>
Asterisk
</div>


<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Description </h1>
 <table width="100%" bgcolor="#ffffff" cellspacing="0" cellpadding="10" \
style="border: 1px solid #b8b5a0">  <tr>
  <td>
   <pre style="margin: 0; padding: 0; white-space: pre-wrap; white-space: \
-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: \
break-word;">Symptom: Asterisk drops a SIP/TLS connection: debugging reports that it \
has failed to read it.

I can reproduce this on my system when the TLS client is Asterisk 11.5 (installed \
from the Debian package) set with &#39;allow=all&#39; to get a long list of codecs.

Calling ast_wait_for_input before every fgets is not sufficient.
Function fgets internally calls read (=&gt; SSL_read) until either &quot;\n&quot; or
eof is found. And because the socket is polled only before the first
SSL_read call, consequent calls can fail and return &lt;=0 even though the
data are on the way.
    
This is fixed by adding a read() loop inside the ssl_read() hook.

I came accross this patch today and it looks like it fixes my problem (see my comment \
at the end). The patch I used is by Filip Jenicek. See the bug report for the full \
log.</pre>  </td>
 </tr>
</table>


<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Testing </h1>
<table width="100%" bgcolor="#ffffff" cellspacing="0" cellpadding="10" style="border: \
1px solid #b8b5a0">  <tr>
  <td>
   <pre style="margin: 0; padding: 0; white-space: pre-wrap; white-space: \
-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: \
break-word;">Work on trunk.</pre>  </td>
 </tr>
</table>


<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Diffs</b> </h1>
<ul style="margin-left: 3em; padding-left: 0;">

 <li>/trunk/main/tcptls.c <span style="color: grey">(397346)</span></li>

</ul>

<p><a href="https://reviewboard.asterisk.org/r/2783/diff/" style="margin-left: \
3em;">View Diff</a></p>







  </td>
 </tr>
</table>








  </div>
 </body>
</html>



--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --

asterisk-dev mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/asterisk-dev

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic