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

Russell Bryant russell at digium.com
Thu Sep 17 09:24:28 CDT 2009


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

Ship it!


I have one last comment, but it's really about code that was already there.  It seems like we hold the channel list locked a whole lot longer than we need to when freeing a channel.  However, I guess we should just leave it alone, since in the majority of cases, the channel list isn't going to be touched in ast_channel_free() anymore, since you have it in ast_hangup().  Ship it!


/tags/1.6.1.1/main/channel.c
<https://reviewboard.asterisk.org/r/362/#comment2605>

    Can you think of any reason we wait this long to unlock the channel list?


- Russell


On 2009-09-16 07:52:54, Matthew Nicholson wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviewboard.asterisk.org/r/362/
> -----------------------------------------------------------
> 
> (Updated 2009-09-16 07:52:54)
> 
> 
> 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/include/asterisk/cdr.h 218291 
>   /tags/1.6.1.1/include/asterisk/channel.h 218291 
>   /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