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

Commit 32150288 authored by Steven Moreland's avatar Steven Moreland Committed by Andrei Homescu
Browse files

libbinder: build option to disable the kernel IPC, part 1/2

Add a new BINDER_WITH_KERNEL_IPC macro to enable all code that
uses IPCThreadState or ProcessState. This macro is defined for
Android libbinder but left out for non-Android builds.

Bug: 224644083
Test: m
Change-Id: I622757fcc0f9885dbf271c0ffa84c54938d50774
parent ffa3aaac
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -174,6 +174,7 @@ cc_library {
        "-Wreorder-init-list",
        "-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION",
        "-DANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION",
        "-DBINDER_WITH_KERNEL_IPC",
    ],
    product_variables: {
        binder32bit: {
+13 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#include <linux/sched.h>
#endif

#include "BuildFlags.h"
#include "RpcState.h"

namespace android {
@@ -164,6 +165,10 @@ status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
        ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
        return INVALID_OPERATION;
    }
    if (!kEnableKernelIpc) {
        ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
        return INVALID_OPERATION;
    }

    BBinder* local = this->localBinder();
    if (local != nullptr) {
@@ -515,6 +520,10 @@ status_t BBinder::setRpcClientDebug(const Parcel& data) {
        ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
        return INVALID_OPERATION;
    }
    if (!kEnableKernelIpc) {
        ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
        return INVALID_OPERATION;
    }
    uid_t uid = IPCThreadState::self()->getCallingUid();
    if (uid != AID_ROOT) {
        ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
@@ -540,6 +549,10 @@ status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
        ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
        return INVALID_OPERATION;
    }
    if (!kEnableKernelIpc) {
        ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
        return INVALID_OPERATION;
    }

    const int socketFdForPrint = socketFd.get();
    LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
+61 −10
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@

#include <stdio.h>

#include "BuildFlags.h"

//#undef ALOGV
//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)

@@ -115,6 +117,11 @@ void BpBinder::ObjectManager::kill()
// ---------------------------------------------------------------------------

sp<BpBinder> BpBinder::create(int32_t handle) {
    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return nullptr;
    }

    int32_t trackedUid = -1;
    if (sCountByUidEnabled) {
        trackedUid = IPCThreadState::self()->getCallingUid();
@@ -177,6 +184,11 @@ BpBinder::BpBinder(Handle&& handle)
}

BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return;
    }

    mTrackedUid = trackedUid;

    ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
@@ -303,6 +315,11 @@ status_t BpBinder::transact(
            status = rpcSession()->transact(sp<IBinder>::fromExisting(this), code, data, reply,
                                            flags);
        } else {
            if constexpr (!kEnableKernelIpc) {
                LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
                return INVALID_OPERATION;
            }

            status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
        }
        if (data.dataSize() > LOG_TRANSACTIONS_OVER_SIZE) {
@@ -328,6 +345,11 @@ status_t BpBinder::linkToDeath(
{
    if (isRpcBinder()) return UNKNOWN_TRANSACTION;

    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return INVALID_OPERATION;
    }

    Obituary ob;
    ob.recipient = recipient;
    ob.cookie = cookie;
@@ -366,6 +388,11 @@ status_t BpBinder::unlinkToDeath(
{
    if (isRpcBinder()) return UNKNOWN_TRANSACTION;

    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return INVALID_OPERATION;
    }

    AutoMutex _l(mLock);

    if (mObitsSent) {
@@ -401,6 +428,11 @@ void BpBinder::sendObituary()
{
    LOG_ALWAYS_FATAL_IF(isRpcBinder(), "Cannot send obituary for remote binder.");

    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return;
    }

    ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
          mObitsSent ? "true" : "false");

@@ -469,12 +501,16 @@ BpBinder* BpBinder::remoteBinder()
    return this;
}

BpBinder::~BpBinder()
{
    ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());

BpBinder::~BpBinder() {
    if (CC_UNLIKELY(isRpcBinder())) return;

    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return;
    }

    ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());

    IPCThreadState* ipc = IPCThreadState::self();

    if (mTrackedUid >= 0) {
@@ -505,21 +541,31 @@ BpBinder::~BpBinder()
    }
}

void BpBinder::onFirstRef()
{
    ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
void BpBinder::onFirstRef() {
    if (CC_UNLIKELY(isRpcBinder())) return;

    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return;
    }

    ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
    IPCThreadState* ipc = IPCThreadState::self();
    if (ipc) ipc->incStrongHandle(binderHandle(), this);
}

void BpBinder::onLastStrongRef(const void* /*id*/)
{
    ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
void BpBinder::onLastStrongRef(const void* /*id*/) {
    if (CC_UNLIKELY(isRpcBinder())) {
        (void)rpcSession()->sendDecStrong(this);
        return;
    }

    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return;
    }

    ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
    IF_ALOGV() {
        printRefs();
    }
@@ -552,6 +598,11 @@ bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
    // RPC binder doesn't currently support inc from weak binders
    if (CC_UNLIKELY(isRpcBinder())) return false;

    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return false;
    }

    ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
    IPCThreadState* ipc = IPCThreadState::self();
    return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
+6 −0
Original line number Diff line number Diff line
@@ -22,4 +22,10 @@ constexpr bool kEnableRpcThreads = false;
constexpr bool kEnableRpcThreads = true;
#endif

#ifdef BINDER_WITH_KERNEL_IPC
constexpr bool kEnableKernelIpc = true;
#else  // BINDER_WITH_KERNEL_IPC
constexpr bool kEnableKernelIpc = false;
#endif // BINDER_WITH_KERNEL_IPC

} // namespace android
+6 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */

#include "Debug.h"
#include "BuildFlags.h"

#include <binder/ProcessState.h>

@@ -301,6 +302,11 @@ void printHexData(int32_t indent, const void *buf, size_t length,
}

ssize_t getBinderKernelReferences(size_t count, uintptr_t* buf) {
    if constexpr (!kEnableKernelIpc) {
        LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
        return 0;
    }

    sp<ProcessState> proc = ProcessState::selfOrNull();
    if (proc.get() == nullptr) {
        return 0;
Loading