Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "RpcServerTrusty" |
| 18 | |
| 19 | #include <binder/Parcel.h> |
| 20 | #include <binder/RpcServer.h> |
| 21 | #include <binder/RpcServerTrusty.h> |
| 22 | #include <binder/RpcThreads.h> |
| 23 | #include <binder/RpcTransportTipcTrusty.h> |
| 24 | #include <log/log.h> |
| 25 | |
| 26 | #include "../FdTrigger.h" |
| 27 | #include "../RpcState.h" |
| 28 | #include "TrustyStatus.h" |
| 29 | |
| 30 | using android::base::unexpected; |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 31 | using android::binder::unique_fd; |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | android::base::expected<sp<RpcServerTrusty>, int> RpcServerTrusty::make( |
| 36 | tipc_hset* handleSet, std::string&& portName, std::shared_ptr<const PortAcl>&& portAcl, |
| 37 | size_t msgMaxSize, std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory) { |
| 38 | // Default is without TLS. |
| 39 | if (rpcTransportCtxFactory == nullptr) |
| 40 | rpcTransportCtxFactory = RpcTransportCtxFactoryTipcTrusty::make(); |
| 41 | auto ctx = rpcTransportCtxFactory->newServerCtx(); |
| 42 | if (ctx == nullptr) { |
| 43 | return unexpected(ERR_NO_MEMORY); |
| 44 | } |
| 45 | |
| 46 | auto srv = sp<RpcServerTrusty>::make(std::move(ctx), std::move(portName), std::move(portAcl), |
| 47 | msgMaxSize); |
| 48 | if (srv == nullptr) { |
| 49 | return unexpected(ERR_NO_MEMORY); |
| 50 | } |
| 51 | |
| 52 | int rc = tipc_add_service(handleSet, &srv->mTipcPort, 1, 0, &kTipcOps); |
| 53 | if (rc != NO_ERROR) { |
| 54 | return unexpected(rc); |
| 55 | } |
| 56 | return srv; |
| 57 | } |
| 58 | |
| 59 | RpcServerTrusty::RpcServerTrusty(std::unique_ptr<RpcTransportCtx> ctx, std::string&& portName, |
| 60 | std::shared_ptr<const PortAcl>&& portAcl, size_t msgMaxSize) |
| 61 | : mRpcServer(sp<RpcServer>::make(std::move(ctx))), |
| 62 | mPortName(std::move(portName)), |
| 63 | mPortAcl(std::move(portAcl)) { |
| 64 | mTipcPort.name = mPortName.c_str(); |
| 65 | mTipcPort.msg_max_size = msgMaxSize; |
| 66 | mTipcPort.msg_queue_len = 6; // Three each way |
| 67 | mTipcPort.priv = this; |
| 68 | |
Steven Moreland | 6a66bfd | 2023-04-17 23:42:19 +0000 | [diff] [blame] | 69 | // TODO(b/266741352): follow-up to prevent needing this in the future |
| 70 | // Trusty needs to be set to the latest stable version that is in prebuilts there. |
Steven Moreland | ca3f638 | 2023-05-11 23:23:26 +0000 | [diff] [blame] | 71 | LOG_ALWAYS_FATAL_IF(!mRpcServer->setProtocolVersion(0)); |
Steven Moreland | 6a66bfd | 2023-04-17 23:42:19 +0000 | [diff] [blame] | 72 | |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 73 | if (mPortAcl) { |
| 74 | // Initialize the array of pointers to uuids. |
| 75 | // The pointers in mUuidPtrs should stay valid across moves of |
| 76 | // RpcServerTrusty (the addresses of a std::vector's elements |
| 77 | // shouldn't change when the vector is moved), but would be invalidated |
| 78 | // by a copy which is why we disable the copy constructor and assignment |
| 79 | // operator for RpcServerTrusty. |
| 80 | auto numUuids = mPortAcl->uuids.size(); |
| 81 | mUuidPtrs.resize(numUuids); |
| 82 | for (size_t i = 0; i < numUuids; i++) { |
| 83 | mUuidPtrs[i] = &mPortAcl->uuids[i]; |
| 84 | } |
| 85 | |
| 86 | // Copy the contents of portAcl into the tipc_port_acl structure that we |
| 87 | // pass to tipc_add_service |
| 88 | mTipcPortAcl.flags = mPortAcl->flags; |
| 89 | mTipcPortAcl.uuid_num = numUuids; |
| 90 | mTipcPortAcl.uuids = mUuidPtrs.data(); |
| 91 | mTipcPortAcl.extra_data = mPortAcl->extraData; |
| 92 | |
| 93 | mTipcPort.acl = &mTipcPortAcl; |
| 94 | } else { |
| 95 | mTipcPort.acl = nullptr; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | int RpcServerTrusty::handleConnect(const tipc_port* port, handle_t chan, const uuid* peer, |
| 100 | void** ctx_p) { |
| 101 | auto* server = reinterpret_cast<RpcServerTrusty*>(const_cast<void*>(port->priv)); |
| 102 | server->mRpcServer->mShutdownTrigger = FdTrigger::make(); |
| 103 | server->mRpcServer->mConnectingThreads[rpc_this_thread::get_id()] = RpcMaybeThread(); |
| 104 | |
| 105 | int rc = NO_ERROR; |
| 106 | auto joinFn = [&](sp<RpcSession>&& session, RpcSession::PreJoinSetupResult&& result) { |
| 107 | if (result.status != OK) { |
| 108 | rc = statusToTrusty(result.status); |
| 109 | return; |
| 110 | } |
| 111 | |
Andrei Homescu | 5e74d2d | 2022-08-19 22:46:38 +0000 | [diff] [blame] | 112 | /* Save the session and connection for the other callbacks */ |
| 113 | auto* channelContext = new (std::nothrow) ChannelContext; |
| 114 | if (channelContext == nullptr) { |
| 115 | rc = ERR_NO_MEMORY; |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | channelContext->session = std::move(session); |
| 120 | channelContext->connection = std::move(result.connection); |
| 121 | |
| 122 | *ctx_p = channelContext; |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Andrei Homescu | 4af92ac | 2023-01-24 21:24:47 +0000 | [diff] [blame] | 125 | // We need to duplicate the channel handle here because the tipc library |
| 126 | // owns the original handle and closes is automatically on channel cleanup. |
| 127 | // We use dup() because Trusty does not have fcntl(). |
| 128 | // NOLINTNEXTLINE(android-cloexec-dup) |
| 129 | handle_t chanDup = dup(chan); |
| 130 | if (chanDup < 0) { |
| 131 | return chanDup; |
| 132 | } |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 133 | unique_fd clientFd(chanDup); |
Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 134 | android::RpcTransportFd transportFd(std::move(clientFd)); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 135 | |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 136 | std::array<uint8_t, RpcServer::kRpcAddressSize> addr; |
| 137 | constexpr size_t addrLen = sizeof(*peer); |
| 138 | memcpy(addr.data(), peer, addrLen); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 139 | RpcServer::establishConnection(sp(server->mRpcServer), std::move(transportFd), addr, addrLen, |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 140 | joinFn); |
| 141 | |
| 142 | return rc; |
| 143 | } |
| 144 | |
Andrei Homescu | 875996f | 2022-08-24 04:25:11 +0000 | [diff] [blame] | 145 | int RpcServerTrusty::handleMessage(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) { |
Andrei Homescu | 5e74d2d | 2022-08-19 22:46:38 +0000 | [diff] [blame] | 146 | auto* channelContext = reinterpret_cast<ChannelContext*>(ctx); |
| 147 | LOG_ALWAYS_FATAL_IF(channelContext == nullptr, |
| 148 | "bad state: message received on uninitialized channel"); |
| 149 | |
| 150 | auto& session = channelContext->session; |
| 151 | auto& connection = channelContext->connection; |
| 152 | status_t status = |
| 153 | session->state()->drainCommands(connection, session, RpcState::CommandType::ANY); |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 154 | if (status != OK) { |
| 155 | LOG_RPC_DETAIL("Binder connection thread closing w/ status %s", |
| 156 | statusToString(status).c_str()); |
| 157 | } |
| 158 | |
| 159 | return NO_ERROR; |
| 160 | } |
| 161 | |
Andrei Homescu | a04030b | 2023-03-31 04:45:32 +0000 | [diff] [blame] | 162 | void RpcServerTrusty::handleDisconnect(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) { |
| 163 | auto* channelContext = reinterpret_cast<ChannelContext*>(ctx); |
| 164 | if (channelContext == nullptr) { |
| 165 | // Connections marked "incoming" (outgoing from the server's side) |
| 166 | // do not have a valid channel context because joinFn does not get |
| 167 | // called for them. We ignore them here. |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | auto& session = channelContext->session; |
| 172 | (void)session->shutdownAndWait(false); |
| 173 | } |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 174 | |
| 175 | void RpcServerTrusty::handleChannelCleanup(void* ctx) { |
Andrei Homescu | 5e74d2d | 2022-08-19 22:46:38 +0000 | [diff] [blame] | 176 | auto* channelContext = reinterpret_cast<ChannelContext*>(ctx); |
| 177 | if (channelContext == nullptr) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | auto& session = channelContext->session; |
| 182 | auto& connection = channelContext->connection; |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 183 | LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection), |
| 184 | "bad state: connection object guaranteed to be in list"); |
Andrei Homescu | 5e74d2d | 2022-08-19 22:46:38 +0000 | [diff] [blame] | 185 | |
| 186 | delete channelContext; |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } // namespace android |