From e70028e43fded41310d41cc93b90f2e689c04725 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Fri, 29 May 2015 01:20:52 +0200 Subject: [PATCH] Protect access to shared list of SRTP sessions. This is a follow up to https://webrtc-codereview.appspot.com/47319004/ and locks access to the static list of SRTP sessions to prevent potential race conditions. BUG=4042 R=juberti@google.com Review URL: https://webrtc-codereview.appspot.com/52609004 Cr-Commit-Position: refs/heads/master@{#9326} --- talk/session/media/srtpfilter.cc | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/talk/session/media/srtpfilter.cc b/talk/session/media/srtpfilter.cc index 5fc694623..33e42c09d 100644 --- a/talk/session/media/srtpfilter.cc +++ b/talk/session/media/srtpfilter.cc @@ -474,6 +474,8 @@ bool SrtpFilter::ParseKeyParams(const std::string& key_params, #ifdef HAVE_SRTP bool SrtpSession::inited_ = false; + +// This lock protects SrtpSession::inited_ and SrtpSession::sessions_. rtc::GlobalLockPod SrtpSession::lock_; SrtpSession::SrtpSession() @@ -482,12 +484,18 @@ SrtpSession::SrtpSession() rtcp_auth_tag_len_(0), srtp_stat_(new SrtpStat()), last_send_seq_num_(-1) { - sessions()->push_back(this); + { + rtc::GlobalLockScope ls(&lock_); + sessions()->push_back(this); + } SignalSrtpError.repeat(srtp_stat_->SignalSrtpError); } SrtpSession::~SrtpSession() { - sessions()->erase(std::find(sessions()->begin(), sessions()->end(), this)); + { + rtc::GlobalLockScope ls(&lock_); + sessions()->erase(std::find(sessions()->begin(), sessions()->end(), this)); + } if (session_) { srtp_dealloc(session_); } @@ -766,6 +774,8 @@ void SrtpSession::HandleEvent(const srtp_event_data_t* ev) { } void SrtpSession::HandleEventThunk(srtp_event_data_t* ev) { + rtc::GlobalLockScope ls(&lock_); + for (std::list::iterator it = sessions()->begin(); it != sessions()->end(); ++it) { if ((*it)->session_ == ev->session) {