[asterisk-dev] [Code Review] Fix race condition that may lead to a crash in ast_hangup() procedure.
Matthew Nicholson
mnicholson at digium.com
Wed Sep 16 07:52:54 CDT 2009
-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviewboard.asterisk.org/r/362/
-----------------------------------------------------------
(Updated 2009-09-16 07:52:54.580993)
Review request for Asterisk Developers.
Changes
-------
The change removes unnecessary locking.
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 (updated)
-----
/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