diff options
author | 2021-05-14 03:20:06 +0000 | |
---|---|---|
committer | 2021-05-14 03:23:11 +0000 | |
commit | eb258ff0ce3fde7db201c0c9063a057c9d5e06ae (patch) | |
tree | 66c49e819e513a799525d7607c76d0468c38352f | |
parent | 8e5f3b455afd2783788a6c68fbed37ce1ea36952 (diff) |
libbinder: binder RPC - using getCalling* aborts
Broken code? Now you know!
Fixes: 186647790
Test: binderRpcTest (on host and device)
Change-Id: Id8fc889f4998b98f8c3a5ae0e054741e0e83c785
-rw-r--r-- | libs/binder/RpcState.cpp | 16 | ||||
-rw-r--r-- | libs/binder/tests/IBinderRpcTest.aidl | 2 | ||||
-rw-r--r-- | libs/binder/tests/binderRpcTest.cpp | 21 |
3 files changed, 39 insertions, 0 deletions
diff --git a/libs/binder/RpcState.cpp b/libs/binder/RpcState.cpp index 2ba9fa2bd5..e5a6026f3b 100644 --- a/libs/binder/RpcState.cpp +++ b/libs/binder/RpcState.cpp @@ -18,7 +18,9 @@ #include "RpcState.h" +#include <android-base/scopeguard.h> #include <binder/BpBinder.h> +#include <binder/IPCThreadState.h> #include <binder/RpcServer.h> #include "Debug.h" @@ -28,6 +30,8 @@ namespace android { +using base::ScopeGuard; + RpcState::RpcState() {} RpcState::~RpcState() {} @@ -470,6 +474,18 @@ status_t RpcState::getAndExecuteCommand(const base::unique_fd& fd, const sp<RpcS status_t RpcState::processServerCommand(const base::unique_fd& fd, const sp<RpcSession>& session, const RpcWireHeader& command) { + IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); + IPCThreadState::SpGuard spGuard{"processing binder RPC command"}; + IPCThreadState::SpGuard* origGuard; + if (kernelBinderState != nullptr) { + origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); + } + ScopeGuard guardUnguard = [&]() { + if (kernelBinderState != nullptr) { + kernelBinderState->restoreGetCallingSpGuard(origGuard); + } + }; + switch (command.command) { case RPC_COMMAND_TRANSACT: return processTransact(fd, session, command); diff --git a/libs/binder/tests/IBinderRpcTest.aidl b/libs/binder/tests/IBinderRpcTest.aidl index ef4198d8f2..41daccc1cf 100644 --- a/libs/binder/tests/IBinderRpcTest.aidl +++ b/libs/binder/tests/IBinderRpcTest.aidl @@ -55,4 +55,6 @@ interface IBinderRpcTest { oneway void sleepMsAsync(int ms); void die(boolean cleanup); + + void useKernelBinderCallingId(); } diff --git a/libs/binder/tests/binderRpcTest.cpp b/libs/binder/tests/binderRpcTest.cpp index 260be570f9..9c263b1f21 100644 --- a/libs/binder/tests/binderRpcTest.cpp +++ b/libs/binder/tests/binderRpcTest.cpp @@ -23,6 +23,7 @@ #include <android/binder_libbinder.h> #include <binder/Binder.h> #include <binder/BpBinder.h> +#include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> #include <binder/ProcessState.h> #include <binder/RpcServer.h> @@ -178,6 +179,13 @@ public: _exit(1); } } + Status useKernelBinderCallingId() override { + // this is WRONG! It does not make sense when using RPC binder, and + // because it is SO wrong, and so much code calls this, it should abort! + + (void)IPCThreadState::self()->getCallingPid(); + return Status::ok(); + } }; sp<IBinder> MyBinderRpcTest::mHeldBinder; @@ -874,6 +882,19 @@ TEST_P(BinderRpc, Die) { } } +TEST_P(BinderRpc, UseKernelBinderCallingId) { + auto proc = createRpcTestSocketServerProcess(1); + + // we can't allocate IPCThreadState so actually the first time should + // succeed :( + EXPECT_OK(proc.rootIface->useKernelBinderCallingId()); + + // second time! we catch the error :) + EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError()); + + proc.expectInvalid = true; +} + TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { auto proc = createRpcTestSocketServerProcess(1); |