Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dc207544 authored by Parth Sane's avatar Parth Sane
Browse files

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
parent bd28005f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -67,6 +67,8 @@ class ServiceManagerMock : public IServiceManager {
    MOCK_METHOD2(unregisterForNotifications, status_t(const String16&,
                                             const sp<LocalRegistrationCallback>&));
    MOCK_METHOD0(getServiceDebugInfo, std::vector<ServiceDebugInfo>());
    MOCK_METHOD1(enableAddServiceCache, void(bool));

  protected:
    MOCK_METHOD0(onAsBinder, IBinder*());
};
+26 −1
Original line number Diff line number Diff line
@@ -477,9 +477,34 @@ libbinder_client_cache_config {
    },
}

soong_config_module_type {
    name: "libbinder_addservice_cache_config",
    module_type: "cc_defaults",
    config_namespace: "libbinder",
    bool_variables: ["release_libbinder_addservice_cache"],
    properties: [
        "cflags",
    ],
}

libbinder_addservice_cache_config {
    name: "libbinder_addservice_cache_flag",
    soong_config_variables: {
        release_libbinder_addservice_cache: {
            cflags: ["-DLIBBINDER_ADDSERVICE_CACHE"],
            conditions_default: {
                cflags: ["-DNO_LIBBINDER_ADDSERVICE_CACHE"],
            },
        },
    },
}

cc_defaults {
    name: "libbinder_kernel_defaults",
    defaults: ["libbinder_client_cache_flag"],
    defaults: [
        "libbinder_client_cache_flag",
        "libbinder_addservice_cache_flag",
    ],
    srcs: [
        "BufferedTextOutput.cpp",
        "BackendUnifiedServiceManager.cpp",
+37 −21
Original line number Diff line number Diff line
@@ -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,14 +131,20 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
    if (!kUseCache) {
        return Status::ok();
    }

    if (service.getTag() == os::Service::Tag::binder) {
        return updateCache(serviceName, service.get<os::Service::Tag::binder>());
    }
    return Status::ok();
}

Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
                                                 const sp<IBinder>& 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<IBinder> binder = service.get<os::Service::Tag::binder>();
    if (!binder) {
        binder::ScopedTrace
                aidlTrace(ATRACE_TAG_AIDL,
@@ -150,7 +162,6 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
                                      "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<IBinder>& 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) {
+4 −2
Original line number Diff line number Diff line
@@ -105,8 +105,7 @@ public:
        binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
                                      "BinderCacheWithInvalidation::setItem Successfully Cached");
        std::lock_guard<std::mutex> lock(mCacheMutex);
        Entry entry = {.service = item, .deathRecipient = deathRecipient};
        mCache[key] = entry;
        mCache[key] = {.service = item, .deathRecipient = deathRecipient};
        return binder::Status::ok();
    }

@@ -147,17 +146,20 @@ public:
                                        const sp<IBinder>& service) override;
    binder::Status getServiceDebugInfo(::std::vector<os::ServiceDebugInfo>* _aidl_return) override;

    void enableAddServiceCache(bool value) { mEnableAddServiceCache = value; }
    // for legacy ABI
    const String16& getInterfaceDescriptor() const override {
        return mTheRealServiceManager->getInterfaceDescriptor();
    }

private:
    bool mEnableAddServiceCache = true;
    std::shared_ptr<BinderCacheWithInvalidation> mCacheForGetService;
    sp<os::IServiceManager> mTheRealServiceManager;
    binder::Status toBinderService(const ::std::string& name, const os::Service& in,
                                   os::Service* _out);
    binder::Status updateCache(const std::string& serviceName, const os::Service& service);
    binder::Status updateCache(const std::string& serviceName, const sp<IBinder>& binder);
    bool returnIfCached(const std::string& serviceName, os::Service* _out);
};

+2 −0
Original line number Diff line number Diff line
@@ -127,6 +127,8 @@ public:
    }
    IBinder* onAsBinder() override { return IInterface::asBinder(mUnifiedServiceManager).get(); }

    void enableAddServiceCache(bool value) { mUnifiedServiceManager->enableAddServiceCache(value); }

protected:
    sp<BackendUnifiedServiceManager> mUnifiedServiceManager;
    // AidlRegistrationCallback -> services that its been registered for
Loading