From 69a0c998716f736d3a55dd1997d65b377babd8dd Mon Sep 17 00:00:00 2001 From: Frederick Mayle Date: Thu, 26 May 2022 20:38:39 +0000 Subject: binder: Add FD support to RPC Binder Bug: 185909244 Test: TH Change-Id: Ic4fc1b1edfe9d69984e785553cd1aaca97a07da3 --- libs/binder/RpcServer.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'libs/binder/RpcServer.cpp') diff --git a/libs/binder/RpcServer.cpp b/libs/binder/RpcServer.cpp index 528341ed26..3bb21adde8 100644 --- a/libs/binder/RpcServer.cpp +++ b/libs/binder/RpcServer.cpp @@ -122,6 +122,14 @@ void RpcServer::setProtocolVersion(uint32_t version) { mProtocolVersion = version; } +void RpcServer::setSupportedFileDescriptorTransportModes( + const std::vector& modes) { + mSupportedFileDescriptorTransportModes.reset(); + for (RpcSession::FileDescriptorTransportMode mode : modes) { + mSupportedFileDescriptorTransportModes.set(static_cast(mode)); + } +} + void RpcServer::setRootObject(const sp& binder) { std::lock_guard _l(mLock); mRootObjectFactory = nullptr; @@ -292,7 +300,7 @@ void RpcServer::establishConnection(sp&& server, base::unique_fd clie if (status == OK) { iovec iov{&header, sizeof(header)}; status = client->interruptableReadFully(server->mShutdownTrigger.get(), &iov, 1, - std::nullopt); + std::nullopt, /*enableAncillaryFds=*/false); if (status != OK) { ALOGE("Failed to read ID for client connecting to RPC server: %s", statusToString(status).c_str()); @@ -307,7 +315,7 @@ void RpcServer::establishConnection(sp&& server, base::unique_fd clie sessionId.resize(header.sessionIdSize); iovec iov{sessionId.data(), sessionId.size()}; status = client->interruptableReadFully(server->mShutdownTrigger.get(), &iov, 1, - std::nullopt); + std::nullopt, /*enableAncillaryFds=*/false); if (status != OK) { ALOGE("Failed to read session ID for client connecting to RPC server: %s", statusToString(status).c_str()); @@ -338,7 +346,7 @@ void RpcServer::establishConnection(sp&& server, base::unique_fd clie iovec iov{&response, sizeof(response)}; status = client->interruptableWriteFully(server->mShutdownTrigger.get(), &iov, 1, - std::nullopt); + std::nullopt, nullptr); if (status != OK) { ALOGE("Failed to send new session response: %s", statusToString(status).c_str()); // still need to cleanup before we can return @@ -396,6 +404,17 @@ void RpcServer::establishConnection(sp&& server, base::unique_fd clie session->setMaxIncomingThreads(server->mMaxThreads); if (!session->setProtocolVersion(protocolVersion)) return; + if (server->mSupportedFileDescriptorTransportModes.test( + header.fileDescriptorTransportMode)) { + session->setFileDescriptorTransportMode( + static_cast( + header.fileDescriptorTransportMode)); + } else { + ALOGE("Rejecting connection: FileDescriptorTransportMode is not supported: %hhu", + header.fileDescriptorTransportMode); + return; + } + // if null, falls back to server root sp sessionSpecificRoot; if (server->mRootObjectFactory != nullptr) { -- cgit v1.2.3-59-g8ed1b