[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 10:30:39 CDT 2009



> On 2009-09-15 09:15:28, Tilghman Lesher wrote:
> > Looks good!  BTW, make sure that you document this change in CHANGES, because this will break some people's (poor) designs to access channel variables in CDR code.
> 
> Russell Bryant wrote:
>     I'm not sure that I understand what you mean with regards to changes in CDR behaviour.  Can you please expand upon your comments to explain how this will change behaviour of CDR processing?
> 
> Tilghman Lesher wrote:
>     Some people have made it known that they access channel variables within CDR code, by referencing the channel name and doing a search through the channel list to find the related channel.  We created CDR variables, so they would not need to do this, but the behavior remains.  If we remove the channel from the list prior to posting the CDRs, then their poor design choices will come back to haunt them.
> 
> Matthew Nicholson wrote:
>     Ok.  I will be sure to document this.  Thanks for the prompt feedback.
> 
> Russell Bryant wrote:
>     Ah, okay.  So, you mean that this could break some third party CDR module that grabs the channel.  If that's the case, I don't think it belongs in CHANGES, because CHANGES is for listing new features.  I don't think it belongs in UPGRADE.txt, either, since that file generally only includes changes that administrators need to be aware of.  Changes in the API, or important notes about how code works should be included in doxygen.

Where exactly in doxygen should this go?  Is there a specific section for CDR modules?


- Matthew


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


On 2009-09-15 08:57:01, Matthew Nicholson wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviewboard.asterisk.org/r/362/
> -----------------------------------------------------------
> 
> (Updated 2009-09-15 08:57:01)
> 
> 
> 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