From dc207544897a30fe412ffcc0deab07dd974358d1 Mon Sep 17 00:00:00 2001 From: Parth Sane Date: Thu, 14 Nov 2024 11:49:08 +0000 Subject: Add binder to libbinder cache after addService This will prevent system_server and other applications from calling servicemanager for local binders. Flag: RELEASE_LIBBINDER_CLIENT_CACHE Bug: 333854840 Test: atest binderCacheUnitTest Change-Id: I2693f21a3f5b7a5770481e5ac79719444284524d --- libs/binder/BackendUnifiedServiceManager.cpp | 58 ++++++++++++++++++---------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'libs/binder/BackendUnifiedServiceManager.cpp') diff --git a/libs/binder/BackendUnifiedServiceManager.cpp b/libs/binder/BackendUnifiedServiceManager.cpp index d32eecdc60..123dfd2c05 100644 --- a/libs/binder/BackendUnifiedServiceManager.cpp +++ b/libs/binder/BackendUnifiedServiceManager.cpp @@ -30,6 +30,12 @@ constexpr bool kUseCache = true; constexpr bool kUseCache = false; #endif +#ifdef LIBBINDER_ADDSERVICE_CACHE +constexpr bool kUseCacheInAddService = true; +#else +constexpr bool kUseCacheInAddService = false; +#endif + using AidlServiceManager = android::os::IServiceManager; using android::os::IAccessor; using binder::Status; @@ -125,31 +131,36 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, if (!kUseCache) { return Status::ok(); } + + if (service.getTag() == os::Service::Tag::binder) { + return updateCache(serviceName, service.get()); + } + return Status::ok(); +} + +Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, + const sp& binder) { std::string traceStr; if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) { traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName; } binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str()); - - if (service.getTag() == os::Service::Tag::binder) { - sp binder = service.get(); - if (!binder) { - binder::ScopedTrace - aidlTrace(ATRACE_TAG_AIDL, - "BinderCacheWithInvalidation::updateCache failed: binder_null"); - } else if (!binder->isBinderAlive()) { - binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, - "BinderCacheWithInvalidation::updateCache failed: " - "isBinderAlive_false"); - } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) { - binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, - "BinderCacheWithInvalidation::updateCache successful"); - return mCacheForGetService->setItem(serviceName, binder); - } else { - binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, - "BinderCacheWithInvalidation::updateCache failed: " - "caching_not_enabled"); - } + if (!binder) { + binder::ScopedTrace + aidlTrace(ATRACE_TAG_AIDL, + "BinderCacheWithInvalidation::updateCache failed: binder_null"); + } else if (!binder->isBinderAlive()) { + binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, + "BinderCacheWithInvalidation::updateCache failed: " + "isBinderAlive_false"); + } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) { + binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, + "BinderCacheWithInvalidation::updateCache successful"); + return mCacheForGetService->setItem(serviceName, binder); + } else { + binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, + "BinderCacheWithInvalidation::updateCache failed: " + "caching_not_enabled"); } return Status::ok(); } @@ -277,7 +288,12 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name, Status BackendUnifiedServiceManager::addService(const ::std::string& name, const sp& service, bool allowIsolated, int32_t dumpPriority) { - return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); + Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); + // mEnableAddServiceCache is true by default. + if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) { + return updateCache(name, service); + } + return status; } Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) { -- cgit v1.2.3-59-g8ed1b