From d73020787e8730575b91bb010f0895571887970b Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Sat, 15 May 2021 01:32:04 +0000 Subject: Revert^2 "libbinder: binder RPC - using getCalling* aborts" 15e2835588ce3a8e318c01230bda2c113a16f761 Broken code? Now you know! Fixes: 186647790 Test: binderRpcTest (on host and device) Change-Id: I994b007b76d68771519dc8279534616ec60e9d71 --- libs/binder/RpcState.cpp | 19 +++++++++++++++++++ libs/binder/tests/IBinderRpcTest.aidl | 2 ++ libs/binder/tests/binderRpcTest.cpp | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/libs/binder/RpcState.cpp b/libs/binder/RpcState.cpp index 2ba9fa2bd5..230de6f0ef 100644 --- a/libs/binder/RpcState.cpp +++ b/libs/binder/RpcState.cpp @@ -18,7 +18,9 @@ #include "RpcState.h" +#include #include +#include #include #include "Debug.h" @@ -28,6 +30,8 @@ namespace android { +using base::ScopeGuard; + RpcState::RpcState() {} RpcState::~RpcState() {} @@ -470,6 +474,21 @@ status_t RpcState::getAndExecuteCommand(const base::unique_fd& fd, const sp& session, const RpcWireHeader& command) { + IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); + IPCThreadState::SpGuard spGuard{ + .address = __builtin_frame_address(0), + .context = "processing binder RPC command", + }; + const 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 a96deb5d27..3f94df2a4c 100644 --- a/libs/binder/tests/binderRpcTest.cpp +++ b/libs/binder/tests/binderRpcTest.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -191,6 +192,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 MyBinderRpcTest::mHeldBinder; @@ -887,6 +895,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); -- cgit v1.2.3-59-g8ed1b