Fixed race condition in closeSession

Merged from http://go/wvgerrit/165498
poc: http://go/ag/20978761

Fix race that corrupts mCryptoSessions std::map,
and race that occurs when CryptoSessions are used after free.

Test: poc
Test: atest MediaDrmParameterizedTests
Test: atest GtsMediaTestCases

Bug: 258189255
Change-Id: I298d3e0770ace9cd590dfaacaa4c52a0732c2fe3
Merged-In: I298d3e0770ace9cd590dfaacaa4c52a0732c2fe3
This commit is contained in:
Edwin Wong
2023-01-04 01:56:05 +00:00
committed by Rahul Frias
parent 18be093969
commit 08acec9c3a
4 changed files with 173 additions and 103 deletions

View File

@@ -7,15 +7,17 @@
#ifndef WV_DRM_PLUGIN_H_
#define WV_DRM_PLUGIN_H_
#include <stdint.h>
#include <map>
#include <mutex>
#include <stdint.h>
#include "OEMCryptoCENC.h"
#include "WVGenericCryptoInterface.h"
#include "cdm_client_property_set.h"
#include "cdm_identifier.h"
#include "media/drm/DrmAPI.h"
#include "media/stagefright/foundation/ABase.h"
#include "media/stagefright/foundation/AString.h"
#include "OEMCryptoCENC.h"
#include "utils/Errors.h"
#include "utils/KeyedVector.h"
#include "utils/List.h"
@@ -24,7 +26,6 @@
#include "utils/Vector.h"
#include "wv_cdm_event_listener.h"
#include "wv_content_decryption_module.h"
#include "WVGenericCryptoInterface.h"
namespace wvdrm {
@@ -36,8 +37,8 @@ using android::Vector;
using std::map;
using wvcdm::CdmIdentifier;
using wvcdm::CdmKeyStatusMap;
using wvcdm::CdmSessionId;
using wvcdm::CdmResponseType;
using wvcdm::CdmSessionId;
using wvcdm::WvContentDecryptionModule;
const OEMCrypto_Algorithm kInvalidCryptoAlgorithm =
@@ -265,9 +266,46 @@ class WVDrmPlugin : public android::DrmPlugin,
const std::string mEmptyString;
} mPropertySet;
class CryptoSessionMap {
public:
std::map<CdmSessionId, std::shared_ptr<CryptoSession>> clear() {
std::unique_lock<std::mutex> auto_lock(mLock);
auto copy = mMap;
mMap.clear();
return copy;
}
std::shared_ptr<CryptoSession> get(const CdmSessionId& sid) {
std::unique_lock<std::mutex> auto_lock(mLock);
if (mMap.count(sid)) {
return mMap[sid];
}
return nullptr;
}
bool empty() {
std::unique_lock<std::mutex> auto_lock(mLock);
return mMap.empty();
}
void erase(const CdmSessionId& sid) {
std::unique_lock<std::mutex> auto_lock(mLock);
mMap.erase(sid);
}
void insert(const CdmSessionId& sid, OEMCrypto_SESSION osid) {
std::unique_lock<std::mutex> auto_lock(mLock);
mMap[sid] = std::make_shared<CryptoSession>(osid);
}
private:
std::mutex mLock;
std::map<CdmSessionId, std::shared_ptr<CryptoSession>> mMap;
};
android::sp<wvcdm::WvContentDecryptionModule> const mCDM;
WVGenericCryptoInterface* mCrypto;
map<CdmSessionId, CryptoSession> mCryptoSessions;
CryptoSessionMap mCryptoSessions;
CdmIdentifier mCdmIdentifier;