From 9c278174c850be6b613dc2d87136a9d2b9e01ebc Mon Sep 17 00:00:00 2001 From: Edwin Wong Date: Fri, 22 Jan 2021 22:46:42 -0800 Subject: [PATCH] [RESTRICT AUTOMERGE] Fix WVCryptoPlugin use after free vulnerability. The shared memory buffer used by srcPtr can be freed by another thread because it is not protected by a mutex. Subsequently, a use after free AIGABRT can occur in a race condition. SafetyNet logging is not added to avoid log spamming. The mutex lock is called to setup for decryption, which is called frequently. The crash was reproduced on the device before the fix. Verified the test passes after the fix. Test: sts sts-tradefed run sts-engbuild-no-spl-lock -m StsHostTestCases --test android.security.sts.Bug_176495665#testPocBug_176495665 Test: push to device with target_hwasan-userdebug build adb shell /data/local/tmp/Bug-176495665_sts64 Bug: 176495665 Bug: 176444161 Change-Id: Ie1aca0ceacb4b7a1b6e473b823541607a36d8cb4 --- libwvdrmengine/mediacrypto/Android.mk | 2 ++ .../mediacrypto/include_hidl/WVCryptoPlugin.h | 11 ++++++++--- .../mediacrypto/src_hidl/WVCryptoPlugin.cpp | 7 ++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/libwvdrmengine/mediacrypto/Android.mk b/libwvdrmengine/mediacrypto/Android.mk index 7aba4c6a..3e01acd8 100644 --- a/libwvdrmengine/mediacrypto/Android.mk +++ b/libwvdrmengine/mediacrypto/Android.mk @@ -72,6 +72,8 @@ LOCAL_SHARED_LIBRARIES := \ libhidlmemory \ liblog +LOCAL_CFLAGS := -Wthread-safety + LOCAL_MODULE := libwvdrmcryptoplugin_hidl LOCAL_PROPRIETARY_MODULE := true diff --git a/libwvdrmengine/mediacrypto/include_hidl/WVCryptoPlugin.h b/libwvdrmengine/mediacrypto/include_hidl/WVCryptoPlugin.h index a7570731..47cfd5c0 100644 --- a/libwvdrmengine/mediacrypto/include_hidl/WVCryptoPlugin.h +++ b/libwvdrmengine/mediacrypto/include_hidl/WVCryptoPlugin.h @@ -7,11 +7,14 @@ #ifndef WV_CRYPTO_PLUGIN_H_ #define WV_CRYPTO_PLUGIN_H_ +#include #include +#include + #include "HidlTypes.h" -#include "wv_content_decryption_module.h" #include "WVTypes.h" +#include "wv_content_decryption_module.h" namespace wvdrm { namespace hardware { @@ -59,13 +62,13 @@ struct WVCryptoPlugin : public ICryptoPlugin { const SharedBuffer& source, uint64_t offset, const DestinationBuffer& destination, - decrypt_1_2_cb _hidl_cb) override; + decrypt_1_2_cb _hidl_cb) override NO_THREAD_SAFETY_ANALYSIS; // use unique_lock private: WVDRM_DISALLOW_COPY_AND_ASSIGN_AND_NEW(WVCryptoPlugin); wvcdm::CdmSessionId mSessionId; - std::map > mSharedBufferMap; + std::map > mSharedBufferMap GUARDED_BY(mSharedBufferLock); sp const mCDM; @@ -73,6 +76,8 @@ struct WVCryptoPlugin : public ICryptoPlugin { const wvcdm::CdmDecryptionParameters& params, bool haveEncryptedSubsamples, std::string* errorDetailMsg); static void incrementIV(uint64_t increaseBy, std::vector* ivPtr); + + std::mutex mSharedBufferLock; }; } // namespace widevine diff --git a/libwvdrmengine/mediacrypto/src_hidl/WVCryptoPlugin.cpp b/libwvdrmengine/mediacrypto/src_hidl/WVCryptoPlugin.cpp index cef2933e..20c40dd7 100644 --- a/libwvdrmengine/mediacrypto/src_hidl/WVCryptoPlugin.cpp +++ b/libwvdrmengine/mediacrypto/src_hidl/WVCryptoPlugin.cpp @@ -108,6 +108,8 @@ Return WVCryptoPlugin::setSharedBufferBase( const hidl_memory& base, uint32_t bufferId) { sp hidlMemory = mapMemory(base); + std::lock_guard shared_buffer_lock(mSharedBufferLock); + // allow mapMemory to return nullptr mSharedBufferMap[bufferId] = hidlMemory; return Void(); @@ -156,7 +158,7 @@ Return WVCryptoPlugin::decrypt_1_2( uint64_t offset, const DestinationBuffer& destination, decrypt_1_2_cb _hidl_cb) { - + std::unique_lock lock(mSharedBufferLock); if (mSharedBufferMap.find(source.bufferId) == mSharedBufferMap.end()) { _hidl_cb(Status_V1_2::ERROR_DRM_CANNOT_HANDLE, 0, "source decrypt buffer base not set"); @@ -224,6 +226,9 @@ Return WVCryptoPlugin::decrypt_1_2( destPtr = static_cast(handle); } + // release mSharedBufferLock + lock.unlock(); + // Calculate the output buffer size and determine if any subsamples are // encrypted. size_t destSize = 0;