[asterisk-dev] [Code Review] Fix race condition that may lead to a crash in ast_hangup() procedure.

Matthew Nicholson mnicholson at digium.com
Tue Sep 15 12:33:04 CDT 2009



> On 2009-09-15 12:18:49, Russell Bryant wrote:
> > I thought of a problem with this approach.  This change moves the removal of the channel from the channel list from ast_channel_free() to ast_hangup().  The problem with that is that _all_ allocated channels will have ast_channel_free() executed on them.  The same is not true of ast_hangup().  This will result in some freed channels that are still in the channel list, causing crashes.
> 
> Matthew Nicholson wrote:
>     Good catch, I'll look into this further.
> 
> Matthew Nicholson wrote:
>     Removing the channel from the channel list is both places should resolve that issue.
> 
> Russell Bryant wrote:
>     I'm not thrilled with that as a solution.  Each attempt at the removal of the channel is a traversal of the channel list.

There are a few other ways to address this.  Moving all of the channel tear down code to ast_channel_free() is one way.  Also adding a flag to the channel structure or simply an argument to ast_channel_free() indicating if the channel needs to be removed from the channel list would solve this problem too.


- Matthew


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


On 2009-09-15 12:30:26, Matthew Nicholson wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviewboard.asterisk.org/r/362/
> -----------------------------------------------------------
> 
> (Updated 2009-09-15 12:30:26)
> 
> 
> Review request for Asterisk Developers.
> 
> 
> Summary
> -------
> 
> This patch fixes a race condition that can occur if a channel is retrieved from the channel list while it is being hung up.  This situation can be caused if the pbx thread calls ast_hangup on a channel while another thread attempts to retrieve the channel from the channel list.  One example where this occurs involves the 'Bridge' manager action.
> 
> An approximation of the hangup procedure in ast_hangup() follows:
>  1. lock the channel
>  2. free some fields in the channel
>  3. call chan->tech->hangup()
>  4. unlock the channel
>  5. generate the 'Hangup' manager event 
>  6. do CDR stuff
>  7. remove the channel from the channel list
>  8. lock and unlock the channel
>  9. free the remaining channel fields
> 
> During step 3, the channel tech_pvt field will be freed and set to NULL.  After step 4 and before step 7, the channel may be retrieved from the channel list and used.  Problems can occur if any of the freed fields are used.  In this specific instance the tech_pvt field is dereferenced causing asterisk to crash.  My purposal for fixing the problem involves moving step 7 and inserting it before step 3.
> 
> This problem potentially exists in all supported versions of asterisk.
> 
> 
> This addresses bug 15316.
>     https://issues.asterisk.org/view.php?id=15316
> 
> 
> Diffs
> -----
> 
>   /tags/1.6.1.1/main/channel.c 218291 
> 
> Diff: https://reviewboard.asterisk.org/r/362/diff
> 
> 
> Testing
> -------
> 
> To test this, I replicated the customer's environment from bug 15316 (this involves sending a Bridge manager action at the proper time).  In order to achieve the proper timing for the race condition to manifest itself, I added a sleep() command after step 3 in the procedure above and a shorter sleep withing the Bridge action (action_bridge()) just before retrieving a channel from the channel list.  While the Bridge action thread is sleeping, I hang up the channel requested for the bridge action causing the hangup procedure to run through step 3 then sleep.  The Bridge action thread then wakes and retrieves the channel from the channel list while the hangup thread is sleeping.  The Bridge thread now has a partially torn down channel with a NULL tech_pvt.  Eventually the Bridge thread executes ast_rtp_new_source() which attempts to access the tech_pvt and crashes asterisk.
> 
> After moving step 7 and inserting it before step 3, the crash no longer occurs and the Bridge thread never finds the channel in the channel list.
> 
> 
> Thanks,
> 
> Matthew
> 
>




More information about the asterisk-dev mailing list