<p>Friendly Automation would like Kevin Harwell to <strong>review</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/12835">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">AST-2019-004 - res_pjsip_t38.c: Add NULL checks before using session media<br><br>After receiving a 200 OK with a declined stream in response to a T.38<br>initiated re-invite Asterisk would crash when attempting to dereference<br>a NULL session media object.<br><br>This patch checks to make sure the session media object is not NULL before<br>attempting to use it.<br><br>ASTERISK-28495<br>patches:<br> ast-2019-004.patch submitted by Alexei Gradinari (license 5691)<br><br>Change-Id: I168f45f4da29cfe739acf87e597baa2aae7aa572<br>---<br>M res/res_pjsip_t38.c<br>1 file changed, 45 insertions(+), 27 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/35/12835/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_pjsip_t38.c b/res/res_pjsip_t38.c</span><br><span>index 11804e2..e5c6090 100644</span><br><span>--- a/res/res_pjsip_t38.c</span><br><span>+++ b/res/res_pjsip_t38.c</span><br><span>@@ -203,7 +203,6 @@</span><br><span> {</span><br><span> RAII_VAR(struct ast_sip_session *, session, obj, ao2_cleanup);</span><br><span> RAII_VAR(struct ast_datastore *, datastore, ast_sip_session_get_datastore(session, "t38"), ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_sip_session_media *session_media;</span><br><span> </span><br><span> if (!datastore) {</span><br><span> return 0;</span><br><span>@@ -212,8 +211,7 @@</span><br><span> ast_debug(2, "Automatically rejecting T.38 request on channel '%s'\n",</span><br><span> session->channel ? ast_channel_name(session->channel) : "<gone>");</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- session_media = session->pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(session, session_media, datastore->data, T38_REJECTED);</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(session, NULL, datastore->data, T38_REJECTED);</span><br><span> ast_sip_session_resume_reinvite(session);</span><br><span> </span><br><span> return 0;</span><br><span>@@ -322,28 +320,37 @@</span><br><span> int index;</span><br><span> </span><br><span> session_media = session->active_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(session, session_media, state, T38_ENABLED);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!session_media) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Received %d response to T.38 re-invite on '%s' but no active session media\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ status.code, session->channel ? ast_channel_name(session->channel) : "unknown channel");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(session, session_media, state, T38_ENABLED);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Stop all the streams in the stored away active state, they'll go back to being active once</span><br><span style="color: hsl(0, 100%, 40%);">- * we reinvite back.</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">- for (index = 0; index < AST_VECTOR_SIZE(&state->media_state->sessions); ++index) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_sip_session_media *session_media = AST_VECTOR_GET(&state->media_state->sessions, index);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Stop all the streams in the stored away active state, they'll go back to being active once</span><br><span style="color: hsl(120, 100%, 40%);">+ * we reinvite back.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ for (index = 0; index < AST_VECTOR_SIZE(&state->media_state->sessions); ++index) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_session_media *session_media = AST_VECTOR_GET(&state->media_state->sessions, index);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (session_media && session_media->handler && session_media->handler->stream_stop) {</span><br><span style="color: hsl(0, 100%, 40%);">- session_media->handler->stream_stop(session_media);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (session_media && session_media->handler && session_media->handler->stream_stop) {</span><br><span style="color: hsl(120, 100%, 40%);">+ session_media->handler->stream_stop(session_media);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span> }</span><br><span> } else {</span><br><span> session_media = session->pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(session, session_media, state, T38_REJECTED);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Abort this attempt at switching to T.38 by resetting the pending state and freeing our stored away active state */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sip_session_media_state_free(state->media_state);</span><br><span style="color: hsl(0, 100%, 40%);">- state->media_state = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sip_session_media_state_reset(session->pending_media_state);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* If no session_media then response contained a declined stream, so disable */</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(session, NULL, state, session_media ? T38_REJECTED : T38_DISABLED);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Abort this attempt at switching to T.38 by resetting the pending state and freeing our stored away active state */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_media_state_free(state->media_state);</span><br><span style="color: hsl(120, 100%, 40%);">+ state->media_state = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_media_state_reset(session->pending_media_state);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span>@@ -426,12 +433,10 @@</span><br><span> /* Negotiation can not take place without a valid max_ifp value. */</span><br><span> if (!parameters->max_ifp) {</span><br><span> if (data->session->t38state == T38_PEER_REINVITE) {</span><br><span style="color: hsl(0, 100%, 40%);">- session_media = data->session->pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(data->session, session_media, state, T38_REJECTED);</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(data->session, NULL, state, T38_REJECTED);</span><br><span> ast_sip_session_resume_reinvite(data->session);</span><br><span> } else if (data->session->t38state == T38_ENABLED) {</span><br><span style="color: hsl(0, 100%, 40%);">- session_media = data->session->active_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(data->session, session_media, state, T38_DISABLED);</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(data->session, NULL, state, T38_DISABLED);</span><br><span> ast_sip_session_refresh(data->session, NULL, NULL, NULL,</span><br><span> AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1, state->media_state);</span><br><span> state->media_state = NULL;</span><br><span>@@ -454,6 +459,11 @@</span><br><span> state->our_parms.version = MIN(state->our_parms.version, state->their_parms.version);</span><br><span> state->our_parms.rate_management = state->their_parms.rate_management;</span><br><span> session_media = data->session->pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!session_media) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to negotiate parameters for reinvite on channel '%s' (No pending session media).\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ data->session->channel ? ast_channel_name(data->session->channel) : "unknown channel");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> ast_udptl_set_local_max_ifp(session_media->udptl, state->our_parms.max_ifp);</span><br><span> t38_change_state(data->session, session_media, state, T38_ENABLED);</span><br><span> ast_sip_session_resume_reinvite(data->session);</span><br><span>@@ -468,8 +478,13 @@</span><br><span> }</span><br><span> state->our_parms = *parameters;</span><br><span> session_media = media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!session_media) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to negotiate parameters on channel '%s' (No default session media).\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ data->session->channel ? ast_channel_name(data->session->channel) : "unknown channel");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> ast_udptl_set_local_max_ifp(session_media->udptl, state->our_parms.max_ifp);</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(data->session, session_media, state, T38_LOCAL_REINVITE);</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(data->session, NULL, state, T38_LOCAL_REINVITE);</span><br><span> ast_sip_session_refresh(data->session, NULL, t38_reinvite_sdp_cb, t38_reinvite_response_cb,</span><br><span> AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1, media_state);</span><br><span> }</span><br><span>@@ -478,12 +493,10 @@</span><br><span> case AST_T38_REFUSED:</span><br><span> case AST_T38_REQUEST_TERMINATE: /* Shutdown T38 */</span><br><span> if (data->session->t38state == T38_PEER_REINVITE) {</span><br><span style="color: hsl(0, 100%, 40%);">- session_media = data->session->pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(data->session, session_media, state, T38_REJECTED);</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(data->session, NULL, state, T38_REJECTED);</span><br><span> ast_sip_session_resume_reinvite(data->session);</span><br><span> } else if (data->session->t38state == T38_ENABLED) {</span><br><span style="color: hsl(0, 100%, 40%);">- session_media = data->session->active_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(data->session, session_media, state, T38_DISABLED);</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(data->session, NULL, state, T38_DISABLED);</span><br><span> ast_sip_session_refresh(data->session, NULL, NULL, NULL, AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1, state->media_state);</span><br><span> state->media_state = NULL;</span><br><span> }</span><br><span>@@ -493,6 +506,11 @@</span><br><span> </span><br><span> if (data->session->t38state == T38_PEER_REINVITE) {</span><br><span> session_media = data->session->pending_media_state->default_session[AST_MEDIA_TYPE_IMAGE];</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!session_media) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to request parameters for reinvite on channel '%s' (No pending session media).\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ data->session->channel ? ast_channel_name(data->session->channel) : "unknown channel");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> parameters.max_ifp = ast_udptl_get_far_max_ifp(session_media->udptl);</span><br><span> parameters.request_response = AST_T38_REQUEST_NEGOTIATE;</span><br><span> ast_queue_control_data(data->session->channel, AST_CONTROL_T38_PARAMETERS, ¶meters, sizeof(parameters));</span><br><span>@@ -788,7 +806,7 @@</span><br><span> </span><br><span> if ((session->t38state == T38_REJECTED) || (session->t38state == T38_DISABLED)) {</span><br><span> ast_debug(3, "Declining; T.38 state is rejected or declined\n");</span><br><span style="color: hsl(0, 100%, 40%);">- t38_change_state(session, session_media, state, T38_DISABLED);</span><br><span style="color: hsl(120, 100%, 40%);">+ t38_change_state(session, NULL, state, T38_DISABLED);</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/12835">change 12835</a>. To unsubscribe, or for help writing mail filters, 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/c/asterisk/+/12835"/><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-Change-Id: I168f45f4da29cfe739acf87e597baa2aae7aa572 </div>
<div style="display:none"> Gerrit-Change-Number: 12835 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>