[Asterisk-code-review] rtp engine.c: Fix deadlock potential copying RTP payload maps. (asterisk[13])

Jenkins2 asteriskteam at digium.com
Tue May 2 09:15:11 CDT 2017


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/5561 )

Change subject: rtp_engine.c: Fix deadlock potential copying RTP payload maps.
......................................................................


rtp_engine.c: Fix deadlock potential copying RTP payload maps.

There is a theoretical potential to deadlock in
ast_rtp_codecs_payloads_copy() because it locks two different
ast_rtp_codecs locks.  It is theoretical because the callers of the
function are either copying between a local ast_rtp_codecs struct and a
RTP instance of the ast_rtp_codecs struct.  Or they are copying between
the caller and callee channel RTP instances before initiating the call to
the callee.  Neither of these situations could actually result in a
deadlock because there cannot be another thread involved at the time.

* Add deadlock avoidance code to ast_rtp_codecs_payloads_copy() since it
locks two ast_rtp_codecs locks to perform a copy.

This only affects v13 since this deadlock avoidance code is already in
newer branches.

Change-Id: I1aa0b168f94049bd59bbd74a85bd1e78718f09e5
---
M main/rtp_engine.c
1 file changed, 8 insertions(+), 2 deletions(-)

Approvals:
  George Joseph: Looks good to me, approved
  Jenkins2: Approved for Submit
  Joshua Colp: Looks good to me, but someone else must approve



diff --git a/main/rtp_engine.c b/main/rtp_engine.c
index f1da873..3377087 100644
--- a/main/rtp_engine.c
+++ b/main/rtp_engine.c
@@ -708,8 +708,14 @@
 {
 	int i;
 
-	ast_rwlock_rdlock(&src->codecs_lock);
 	ast_rwlock_wrlock(&dest->codecs_lock);
+
+	/* Deadlock avoidance because of held write lock. */
+	while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
+		ast_rwlock_unlock(&dest->codecs_lock);
+		sched_yield();
+		ast_rwlock_wrlock(&dest->codecs_lock);
+	}
 
 	for (i = 0; i < AST_VECTOR_SIZE(&src->payloads); i++) {
 		struct ast_rtp_payload_type *type;
@@ -732,8 +738,8 @@
 		}
 	}
 	dest->framing = src->framing;
-	ast_rwlock_unlock(&dest->codecs_lock);
 	ast_rwlock_unlock(&src->codecs_lock);
+	ast_rwlock_unlock(&dest->codecs_lock);
 }
 
 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)

-- 
To view, visit https://gerrit.asterisk.org/5561
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I1aa0b168f94049bd59bbd74a85bd1e78718f09e5
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>



More information about the asterisk-code-review mailing list