From fcfb7dac1b844896b5f7397992b5238194a1ce4b Mon Sep 17 00:00:00 2001 From: Dmitriy Filchenko Date: Mon, 25 Mar 2024 20:46:07 +0000 Subject: libbinder: Send ancillary FDs to Trusty Bug: 319300676 Test: atest binderRpcToTrustyTest Change-Id: Idbfcae0b2ff3c945b156aba1612c0d394f60386b --- libs/binder/RpcTransportTipcAndroid.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'libs/binder') diff --git a/libs/binder/RpcTransportTipcAndroid.cpp b/libs/binder/RpcTransportTipcAndroid.cpp index 3819fb6472..14c0bde7c5 100644 --- a/libs/binder/RpcTransportTipcAndroid.cpp +++ b/libs/binder/RpcTransportTipcAndroid.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "FdTrigger.h" #include "RpcState.h" @@ -32,6 +33,9 @@ using android::binder::unique_fd; namespace android { +// Corresponds to IPC_MAX_MSG_HANDLES in the Trusty kernel +constexpr size_t kMaxTipcHandles = 8; + // RpcTransport for writing Trusty IPC clients in Android. class RpcTransportTipcAndroid : public RpcTransport { public: @@ -78,12 +82,28 @@ public: FdTrigger* fdTrigger, iovec* iovs, int niovs, const std::optional>& altPoll, const std::vector>* ancillaryFds) override { + bool sentFds = false; auto writeFn = [&](iovec* iovs, size_t niovs) -> ssize_t { - // TODO: send ancillaryFds. For now, we just abort if anyone tries - // to send any. - LOG_ALWAYS_FATAL_IF(ancillaryFds != nullptr && !ancillaryFds->empty(), - "File descriptors are not supported on Trusty yet"); - return TEMP_FAILURE_RETRY(tipc_send(mSocket.fd.get(), iovs, niovs, nullptr, 0)); + trusty_shm shms[kMaxTipcHandles] = {{0}}; + ssize_t shm_count = 0; + + if (!sentFds && ancillaryFds != nullptr && !ancillaryFds->empty()) { + if (ancillaryFds->size() > kMaxTipcHandles) { + ALOGE("Too many file descriptors for TIPC: %zu", ancillaryFds->size()); + errno = EINVAL; + return -1; + } + for (const auto& fdVariant : *ancillaryFds) { + shms[shm_count++] = {std::visit([](const auto& fd) { return fd.get(); }, + fdVariant), + TRUSTY_SEND_SECURE_OR_SHARE}; + } + } + + auto ret = TEMP_FAILURE_RETRY(tipc_send(mSocket.fd.get(), iovs, niovs, + (shm_count == 0) ? nullptr : shms, shm_count)); + sentFds |= ret >= 0; + return ret; }; status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, writeFn, -- cgit v1.2.3-59-g8ed1b