<p>Joshua Colp <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/10637">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve; Approved for Submit
George Joseph: Looks good to me, approved
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">test_res_pjsip_scheduler: Fix possible write after free in scheduler_policy.<br><br>It's possible for a 4th task to be spawned before we cancel. This<br>results in a write to the already freed test_data1. Wait long enough to<br>verify success of the cancelation before freeing test_data1.<br><br>Change-Id: I057e2fcbe97f8a175e50890be89c28c20490a20f<br>---<br>M tests/test_res_pjsip_scheduler.c<br>1 file changed, 34 insertions(+), 5 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/tests/test_res_pjsip_scheduler.c b/tests/test_res_pjsip_scheduler.c</span><br><span>index d0c2d90..cecb742 100644</span><br><span>--- a/tests/test_res_pjsip_scheduler.c</span><br><span>+++ b/tests/test_res_pjsip_scheduler.c</span><br><span>@@ -53,6 +53,7 @@</span><br><span> int interval;</span><br><span> int sleep;</span><br><span> int done;</span><br><span style="color: hsl(120, 100%, 40%);">+ int no_clear_done;</span><br><span> struct ast_test *test;</span><br><span> };</span><br><span> </span><br><span>@@ -63,7 +64,9 @@</span><br><span> {</span><br><span> struct test_data *test = data;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- test->done = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!test->no_clear_done) {</span><br><span style="color: hsl(120, 100%, 40%);">+ test->done = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> test->task_start = ast_tvnow();</span><br><span> test->tid = pthread_self();</span><br><span> test->is_servant = ast_sip_thread_is_servant();</span><br><span>@@ -71,7 +74,7 @@</span><br><span> test->task_end = ast_tvnow();</span><br><span> </span><br><span> ast_mutex_lock(&test->lock);</span><br><span style="color: hsl(0, 100%, 40%);">- test->done = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ test->done++;</span><br><span> ast_mutex_unlock(&test->lock);</span><br><span> ast_cond_signal(&test->cond);</span><br><span> </span><br><span>@@ -345,11 +348,12 @@</span><br><span> test_data1->test_start = ast_tvnow();</span><br><span> test_data1->interval = 1000;</span><br><span> test_data1->sleep = 500;</span><br><span style="color: hsl(120, 100%, 40%);">+ test_data1->no_clear_done = 1;</span><br><span> ast_mutex_init(&test_data1->lock);</span><br><span> ast_cond_init(&test_data1->cond, NULL);</span><br><span> </span><br><span> ast_test_status_update(test, "This test will take about %3.1f seconds\n",</span><br><span style="color: hsl(0, 100%, 40%);">- ((test_data1->interval * 3) + test_data1->sleep) / 1000.0);</span><br><span style="color: hsl(120, 100%, 40%);">+ ((test_data1->interval * 4) + test_data1->sleep) / 1000.0);</span><br><span> </span><br><span> task = ast_sip_schedule_task(NULL, test_data1->interval, task_1, "test_1", test_data1,</span><br><span> AST_SIP_SCHED_TASK_DATA_NO_CLEANUP | AST_SIP_SCHED_TASK_PERIODIC);</span><br><span>@@ -368,8 +372,33 @@</span><br><span> ast_test_validate(test, when > test_data1->interval * 3 * 0.9 && when < test_data1->interval * 3 * 1.1);</span><br><span> </span><br><span> ast_sip_sched_task_cancel(task);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(task, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- task = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Wait a full interval in case a 4th call to test_1 happened before the cancel */</span><br><span style="color: hsl(120, 100%, 40%);">+ usleep(M2U(test_data1->interval));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&test_data1->lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (test_data1->done) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int done = test_data1->done;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ test_data1->done = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&test_data1->lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, done == 1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Wait two full intervals to be certain no further calls to test_1. */</span><br><span style="color: hsl(120, 100%, 40%);">+ usleep(M2U(test_data1->interval * 2));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&test_data1->lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (test_data1->done != 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&test_data1->lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* The cancelation failed so we need to prevent cleanup of</span><br><span style="color: hsl(120, 100%, 40%);">+ * test_data1 to prevent a crash from write-after-free. */</span><br><span style="color: hsl(120, 100%, 40%);">+ test_data1 = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_status_update(test, "Failed to cancel task");</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_FAIL;</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_mutex_unlock(&test_data1->lock);</span><br><span> </span><br><span> return AST_TEST_PASS;</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10637">change 10637</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/10637"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: I057e2fcbe97f8a175e50890be89c28c20490a20f </div>
<div style="display:none"> Gerrit-Change-Number: 10637 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 (1000185) </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>