From a80a9fef26b7e695e4072293aad8f6b7bdce613d Mon Sep 17 00:00:00 2001 From: Alex Dale Date: Fri, 22 Apr 2022 13:53:11 -0700 Subject: [PATCH] Added mutex for reserved_license_ids_. [ Merge of http://go/wvgerrit/150630 ] DeviceFiles uses a static variable for tracking license IDs which have been reserved by a CDM session before officially storing the license on the device. This variable was not protected by a mutex, and a rare race condition would arise, either crashing the service or getting it stuck in a loop. This CL adds a mutex for protecting the set of reserved IDs. Bug: 226555704 Test: device_files_unittest Change-Id: Icdea88673c76c267b4b7db79697ec52ae8e2581e --- libwvdrmengine/cdm/core/include/device_files.h | 2 ++ libwvdrmengine/cdm/core/src/device_files.cpp | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/libwvdrmengine/cdm/core/include/device_files.h b/libwvdrmengine/cdm/core/include/device_files.h index d23a5d45..8b3926ef 100644 --- a/libwvdrmengine/cdm/core/include/device_files.h +++ b/libwvdrmengine/cdm/core/include/device_files.h @@ -5,6 +5,7 @@ #ifndef WVCDM_CORE_DEVICE_FILES_H_ #define WVCDM_CORE_DEVICE_FILES_H_ +#include #include #include #include @@ -369,6 +370,7 @@ class DeviceFiles { #endif static std::set reserved_license_ids_; + static std::mutex reserved_license_ids_mutex_; wvutil::FileSystem* file_system_; CdmSecurityLevel security_level_; diff --git a/libwvdrmengine/cdm/core/src/device_files.cpp b/libwvdrmengine/cdm/core/src/device_files.cpp index 2602171b..e18fac0a 100644 --- a/libwvdrmengine/cdm/core/src/device_files.cpp +++ b/libwvdrmengine/cdm/core/src/device_files.cpp @@ -87,6 +87,8 @@ using video_widevine_client::sdk:: } namespace wvcdm { +using UniqueLock = std::unique_lock; + namespace { const char kEmptyFileName[] = ""; const char kFalse[] = "false"; @@ -364,6 +366,7 @@ const char* DeviceFiles::ResponseTypeToString(ResponseType type) { // static std::set DeviceFiles::reserved_license_ids_; +std::mutex DeviceFiles::reserved_license_ids_mutex_; DeviceFiles::DeviceFiles(wvutil::FileSystem* file_system) : file_system_(file_system), @@ -847,6 +850,7 @@ bool DeviceFiles::StoreLicense(const CdmLicenseData& license_data, std::string serialized_file; file.SerializeToString(&serialized_file); + UniqueLock lock(reserved_license_ids_mutex_); reserved_license_ids_.erase(license_data.key_set_id); *result = StoreFileWithHash(license_data.key_set_id + kLicenseFileNameExt, serialized_file); @@ -984,18 +988,21 @@ bool DeviceFiles::DeleteAllFiles() { bool DeviceFiles::LicenseExists(const std::string& key_set_id) { RETURN_FALSE_IF_UNINITIALIZED(); + UniqueLock lock(reserved_license_ids_mutex_); return reserved_license_ids_.count(key_set_id) || FileExists(key_set_id + kLicenseFileNameExt); } bool DeviceFiles::ReserveLicenseId(const std::string& key_set_id) { RETURN_FALSE_IF_UNINITIALIZED(); + UniqueLock lock(reserved_license_ids_mutex_); reserved_license_ids_.insert(key_set_id); return true; } bool DeviceFiles::UnreserveLicenseId(const std::string& key_set_id) { RETURN_FALSE_IF_UNINITIALIZED(); + UniqueLock lock(reserved_license_ids_mutex_); reserved_license_ids_.erase(key_set_id); return true; }