<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/14885">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, approved
  George Joseph: Looks good to me, but someone else must approve
  Friendly Automation: Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">Bridging: Use a ref to bridge_channel's channel to prevent crash.<br><br>There's a race condition with bridging where a bridge can be torn down<br>causing the bridge_channel's ast_channel to become NULL when it's still<br>needed. This particular case happened with attended transfers, but the<br>crash occurred when trying to publish a stasis message. Now, the<br>bridge_channel is locked, a ref to the ast_channel is obtained, and that<br>ref is passed down the chain.<br><br>Change-Id: Ic48715c0c041615d17d286790ae3e8c61bb28814<br>---<br>M include/asterisk/bridge_channel.h<br>M main/bridge.c<br>M main/bridge_channel.c<br>3 files changed, 52 insertions(+), 3 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/bridge_channel.h b/include/asterisk/bridge_channel.h</span><br><span>index a16695e..e8a3fcf 100644</span><br><span>--- a/include/asterisk/bridge_channel.h</span><br><span>+++ b/include/asterisk/bridge_channel.h</span><br><span>@@ -177,6 +177,20 @@</span><br><span> };</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Get a ref to the bridge_channel's ast_channel</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param bridge_channel The bridge channel</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note The returned channel NEEDS to be unref'd once you are done with it. In general, this</span><br><span style="color: hsl(120, 100%, 40%);">+ * function is best used when accessing the bridge_channel chan from outside of a bridging</span><br><span style="color: hsl(120, 100%, 40%);">+ * thread.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval ref'd ast_channel on success</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval NULL otherwise</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_channel *ast_bridge_channel_get_chan(struct ast_bridge_channel *bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span>  * \brief Try locking the bridge_channel.</span><br><span>  *</span><br><span>  * \param bridge_channel What to try locking</span><br><span>diff --git a/main/bridge.c b/main/bridge.c</span><br><span>index 23f682c..351aaf1 100644</span><br><span>--- a/main/bridge.c</span><br><span>+++ b/main/bridge.c</span><br><span>@@ -1719,7 +1719,10 @@</span><br><span>      ast_channel_lock(chan);</span><br><span>      ast_channel_internal_bridge_channel_set(chan, NULL);</span><br><span>         ast_channel_unlock(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+     /* Due to a race condition, we lock the bridge channel here for ast_bridge_channel_get_chan */</span><br><span style="color: hsl(120, 100%, 40%);">+        ao2_lock(bridge_channel);</span><br><span>    bridge_channel->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+       ao2_unlock(bridge_channel);</span><br><span>  /* If bridge_channel->swap is not NULL then the join failed. */</span><br><span>   ao2_t_cleanup(bridge_channel->swap, "Bridge complete: join failed");</span><br><span>    bridge_channel->swap = NULL;</span><br><span>@@ -1788,7 +1791,10 @@</span><br><span>     ast_channel_lock(chan);</span><br><span>      ast_channel_internal_bridge_channel_set(chan, NULL);</span><br><span>         ast_channel_unlock(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+     /* Lock here for ast_bridge_channel_get_chan */</span><br><span style="color: hsl(120, 100%, 40%);">+       ao2_lock(bridge_channel);</span><br><span>    bridge_channel->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+       ao2_unlock(bridge_channel);</span><br><span>  /* If bridge_channel->swap is not NULL then the join failed. */</span><br><span>   ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Independent impart join failed");</span><br><span>         bridge_channel->swap = NULL;</span><br><span>@@ -1889,7 +1895,10 @@</span><br><span>             ast_channel_lock(chan);</span><br><span>              ast_channel_internal_bridge_channel_set(chan, NULL);</span><br><span>                 ast_channel_unlock(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+             /* Lock here for ast_bridge_channel_get_chan */</span><br><span style="color: hsl(120, 100%, 40%);">+               ao2_lock(bridge_channel);</span><br><span>            bridge_channel->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+               ao2_unlock(bridge_channel);</span><br><span>          ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Impart failed");</span><br><span>          bridge_channel->swap = NULL;</span><br><span>              ast_bridge_features_destroy(bridge_channel->features);</span><br><span>@@ -4679,14 +4688,22 @@</span><br><span> </span><br><span>      if (to_transferee_bridge_channel) {</span><br><span>          /* Take off hold if they are on hold. */</span><br><span style="color: hsl(0, 100%, 40%);">-                ast_bridge_channel_write_unhold(to_transferee_bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+                if (ast_bridge_channel_write_unhold(to_transferee_bridge_channel)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  ast_log(LOG_ERROR, "Transferee channel disappeared during transfer!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+                    res = AST_BRIDGE_TRANSFER_FAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+                       goto end;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span>    }</span><br><span> </span><br><span>        if (to_target_bridge_channel) {</span><br><span>              const char *target_complete_sound;</span><br><span> </span><br><span>               /* Take off hold if they are on hold. */</span><br><span style="color: hsl(0, 100%, 40%);">-                ast_bridge_channel_write_unhold(to_target_bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+            if (ast_bridge_channel_write_unhold(to_target_bridge_channel)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      ast_log(LOG_ERROR, "Target channel disappeared during transfer!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+                        res = AST_BRIDGE_TRANSFER_FAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+                       goto end;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span> </span><br><span>                /* Is there a courtesy sound to play to the target? */</span><br><span>               ast_channel_lock(to_transfer_target);</span><br><span>diff --git a/main/bridge_channel.c b/main/bridge_channel.c</span><br><span>index b6ee820..fc03413 100644</span><br><span>--- a/main/bridge_channel.c</span><br><span>+++ b/main/bridge_channel.c</span><br><span>@@ -209,6 +209,17 @@</span><br><span>        ast_sem_post(&sync_struct->sem);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_channel *ast_bridge_channel_get_chan(struct ast_bridge_channel *bridge_channel)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct ast_channel *chan;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   ao2_lock(bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+     chan = ao2_bump(bridge_channel->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+     ao2_unlock(bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return chan;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)</span><br><span> {</span><br><span>     struct ast_bridge *bridge;</span><br><span>@@ -1096,7 +1107,14 @@</span><br><span> </span><br><span> int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-  ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct ast_channel *chan = ast_bridge_channel_get_chan(bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     if (!chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   ast_channel_publish_cached_blob(chan, ast_channel_unhold_type(), NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+       ao2_ref(chan, -1);</span><br><span> </span><br><span>       return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/14885">change 14885</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/+/14885"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-Change-Id: Ic48715c0c041615d17d286790ae3e8c61bb28814 </div>
<div style="display:none"> Gerrit-Change-Number: 14885 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>