asterisk: Audit locking of channel when manipulating flags.

When manipulating flags on a channel the channel has to be
locked to guarantee that nothing else is also manipulating
the flags. This change introduces locking where necessary to
guarantee this. It also adds helper functions that manipulate
channel flags and lock to reduce repeated code.

ASTERISK-26789

Change-Id: I489280662dba0f4c50981bfc5b5a7073fef2db10
This commit is contained in:
Joshua Colp
2017-05-13 16:40:00 +00:00
parent 6383d9214a
commit 1618203964
15 changed files with 96 additions and 55 deletions

View File

@@ -1296,8 +1296,10 @@ int ast_channel_defer_dtmf(struct ast_channel *chan)
int pre = 0;
if (chan) {
ast_channel_lock(chan);
pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
ast_channel_unlock(chan);
}
return pre;
}
@@ -1305,8 +1307,9 @@ int ast_channel_defer_dtmf(struct ast_channel *chan)
/*! \brief Unset defer DTMF flag on channel */
void ast_channel_undefer_dtmf(struct ast_channel *chan)
{
if (chan)
ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
if (chan) {
ast_channel_clear_flag(chan, AST_FLAG_DEFER_DTMF);
}
}
struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
@@ -3518,7 +3521,7 @@ int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, in
return -1;
/* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
ast_channel_set_flag(c, AST_FLAG_END_DTMF_ONLY);
/* Wait for a digit, no more than timeout_ms milliseconds total.
* Or, wait indefinitely if timeout_ms is <0.
@@ -3537,12 +3540,12 @@ int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, in
if (errno == 0 || errno == EINTR)
continue;
ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
return -1;
} else if (outfd > -1) {
/* The FD we were watching has something waiting */
ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
return 1;
} else if (rchan) {
int res;
@@ -3556,13 +3559,13 @@ int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, in
case AST_FRAME_DTMF_END:
res = f->subclass.integer;
ast_frfree(f);
ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
return res;
case AST_FRAME_CONTROL:
switch (f->subclass.integer) {
case AST_CONTROL_HANGUP:
ast_frfree(f);
ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
return -1;
case AST_CONTROL_STREAM_STOP:
case AST_CONTROL_STREAM_SUSPEND:
@@ -3573,7 +3576,7 @@ int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, in
* that perform stream control will handle this. */
res = f->subclass.integer;
ast_frfree(f);
ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
return res;
case AST_CONTROL_PVT_CAUSE_CODE:
case AST_CONTROL_RINGING:
@@ -3608,7 +3611,7 @@ int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, in
}
}
ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
return 0; /* Time is up */
}
@@ -5873,9 +5876,9 @@ struct ast_channel *ast_call_forward(struct ast_channel *caller, struct ast_chan
} else if (caller) { /* no outgoing helper so use caller if available */
call_forward_inherit(new_chan, caller, orig);
}
ast_set_flag(ast_channel_flags(new_chan), AST_FLAG_ORIGINATED);
ast_channel_lock_both(orig, new_chan);
ast_channel_set_flag(new_chan, AST_FLAG_ORIGINATED);
pbx_builtin_setvar_helper(new_chan, "FORWARDERNAME", forwarder);
ast_party_connected_line_copy(ast_channel_connected(new_chan), ast_channel_connected(orig));
ast_party_redirecting_copy(ast_channel_redirecting(new_chan), ast_channel_redirecting(orig));
@@ -10996,3 +10999,18 @@ enum ast_channel_error ast_channel_errno(void)
{
return ast_channel_internal_errno();
}
void ast_channel_set_flag(struct ast_channel *chan, unsigned int flag)
{
ast_channel_lock(chan);
ast_set_flag(ast_channel_flags(chan), flag);
ast_channel_unlock(chan);
}
void ast_channel_clear_flag(struct ast_channel *chan, unsigned int flag)
{
ast_channel_lock(chan);
ast_clear_flag(ast_channel_flags(chan), flag);
ast_channel_unlock(chan);
}