<p>George Joseph <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/8343">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins2: Verified
  George Joseph: Looks good to me, approved; Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">AST-2018-005: Fix tdata leaks when calling pjsip_endpt_send_response(2)<br><br>pjsip_distributor:<br>   authenticate() creates a tdata and uses it to send a challenge or<br>   failure response.  When pjsip_endpt_send_response2() succeeds, it<br>   automatically decrements the tdata ref count but when it fails, it<br>   doesn't.  Since we weren't checking for a return status, we weren't<br>   decrementing the count ourselves on error and were therefore leaking<br>   tdatas.<br><br>res_pjsip_session:<br>   session_reinvite_on_rx_request wasn't decrementing the ref count<br>   if an error happened while sending a 491 response.<br>   pre_session_setup wasn't decrementing the ref count if<br>   while sending an error after a pjsip_inv_verify_request failure.<br><br>res_pjsip:<br>   ast_sip_send_response wasn't decrementing the ref count on error.<br><br>ASTERISK-27618<br>Reported By: Sandro Gauci<br><br>Change-Id: Iab33a6c7b6fba96148ed465b690ba8534ac961bf<br>---<br>M res/res_pjsip.c<br>M res/res_pjsip/pjsip_distributor.c<br>M res/res_pjsip_session.c<br>3 files changed, 20 insertions(+), 6 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/res/res_pjsip.c b/res/res_pjsip.c<br>index 79e6cc2..df4dd47 100644<br>--- a/res/res_pjsip.c<br>+++ b/res/res_pjsip.c<br>@@ -4722,9 +4722,15 @@<br> <br> int ast_sip_send_response(pjsip_response_addr *res_addr, pjsip_tx_data *tdata, struct ast_sip_endpoint *sip_endpoint)<br> {<br>-        supplement_outgoing_response(tdata, sip_endpoint);<br>+   pj_status_t status;<br> <br>-       return pjsip_endpt_send_response(ast_sip_get_pjsip_endpoint(), res_addr, tdata, NULL, NULL);<br>+ supplement_outgoing_response(tdata, sip_endpoint);<br>+   status = pjsip_endpt_send_response(ast_sip_get_pjsip_endpoint(), res_addr, tdata, NULL, NULL);<br>+       if (status != PJ_SUCCESS) {<br>+          pjsip_tx_data_dec_ref(tdata);<br>+        }<br>+<br>+ return status == PJ_SUCCESS ? 0 : -1;<br> }<br> <br> int ast_sip_send_stateful_response(pjsip_rx_data *rdata, pjsip_tx_data *tdata, struct ast_sip_endpoint *sip_endpoint)<br>diff --git a/res/res_pjsip/pjsip_distributor.c b/res/res_pjsip/pjsip_distributor.c<br>index c239c1a..33d4bce 100644<br>--- a/res/res_pjsip/pjsip_distributor.c<br>+++ b/res/res_pjsip/pjsip_distributor.c<br>@@ -854,7 +854,9 @@<br>                case AST_SIP_AUTHENTICATION_CHALLENGE:<br>                        /* Send the 401 we created for them */<br>                        ast_sip_report_auth_challenge_sent(endpoint, rdata, tdata);<br>-                  pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);<br>+                  if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {<br>+                              pjsip_tx_data_dec_ref(tdata);<br>+                        }<br>                     return PJ_TRUE;<br>               case AST_SIP_AUTHENTICATION_SUCCESS:<br>                  /* See note in endpoint_lookup about not holding an unnecessary write lock */<br>@@ -867,7 +869,9 @@<br>            case AST_SIP_AUTHENTICATION_FAILED:<br>                   log_failed_request(rdata, "Failed to authenticate", 0, 0);<br>                  ast_sip_report_auth_failed_challenge_response(endpoint, rdata);<br>-                      pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);<br>+                  if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {<br>+                              pjsip_tx_data_dec_ref(tdata);<br>+                        }<br>                     return PJ_TRUE;<br>               case AST_SIP_AUTHENTICATION_ERROR:<br>                    log_failed_request(rdata, "Error to authenticate", 0, 0);<br>diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c<br>index 5a9134b..fcd190b 100644<br>--- a/res/res_pjsip_session.c<br>+++ b/res/res_pjsip_session.c<br>@@ -1865,7 +1865,9 @@<br> <br>            /* Otherwise this is a new re-invite, so reject it */<br>                 if (pjsip_dlg_create_response(dlg, rdata, 491, NULL, &tdata) == PJ_SUCCESS) {<br>-                    pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);<br>+                  if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {<br>+                              pjsip_tx_data_dec_ref(tdata);<br>+                        }<br>             }<br> <br>          return PJ_TRUE;<br>@@ -2855,7 +2857,9 @@<br> <br>     if (pjsip_inv_verify_request(rdata, &options, NULL, NULL, ast_sip_get_pjsip_endpoint(), &tdata) != PJ_SUCCESS) {<br>              if (tdata) {<br>-                 pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);<br>+                  if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {<br>+                              pjsip_tx_data_dec_ref(tdata);<br>+                        }<br>             } else {<br>                      pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);<br>            }<br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/8343">change 8343</a>. To unsubscribe, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/8343"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Iab33a6c7b6fba96148ed465b690ba8534ac961bf </div>
<div style="display:none"> Gerrit-Change-Number: 8343 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 </div>