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; |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | android::base::expected<sp<RpcServerTrusty>, int> RpcServerTrusty::make( |
| 35 | tipc_hset* handleSet, std::string&& portName, std::shared_ptr<const PortAcl>&& portAcl, |
| 36 | size_t msgMaxSize, std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory) { |
| 37 | // Default is without TLS. |
| 38 | if (rpcTransportCtxFactory == nullptr) |
| 39 | rpcTransportCtxFactory = RpcTransportCtxFactoryTipcTrusty::make(); |
| 40 | auto ctx = rpcTransportCtxFactory->newServerCtx(); |
| 41 | if (ctx == nullptr) { |
| 42 | return unexpected(ERR_NO_MEMORY); |
| 43 | } |
| 44 | |
| 45 | auto srv = sp<RpcServerTrusty>::make(std::move(ctx), std::move(portName), std::move(portAcl), |
| 46 | msgMaxSize); |
| 47 | if (srv == nullptr) { |
| 48 | return unexpected(ERR_NO_MEMORY); |
| 49 | } |
| 50 | |
| 51 | int rc = tipc_add_service(handleSet, &srv->mTipcPort, 1, 0, &kTipcOps); |
| 52 | if (rc != NO_ERROR) { |
| 53 | return unexpected(rc); |
| 54 | } |
| 55 | return srv; |
| 56 | } |
| 57 | |
| 58 | RpcServerTrusty::RpcServerTrusty(std::unique_ptr<RpcTransportCtx> ctx, std::string&& portName, |
| 59 | std::shared_ptr<const PortAcl>&& portAcl, size_t msgMaxSize) |
| 60 | : mRpcServer(sp<RpcServer>::make(std::move(ctx))), |
| 61 | mPortName(std::move(portName)), |
| 62 | mPortAcl(std::move(portAcl)) { |
| 63 | mTipcPort.name = mPortName.c_str(); |
| 64 | mTipcPort.msg_max_size = msgMaxSize; |
| 65 | mTipcPort.msg_queue_len = 6; // Three each way |
| 66 | mTipcPort.priv = this; |
| 67 | |
| 68 | if (mPortAcl) { |
| 69 | // Initialize the array of pointers to uuids. |
| 70 | // The pointers in mUuidPtrs should stay valid across moves of |
| 71 | // RpcServerTrusty (the addresses of a std::vector's elements |
| 72 | // shouldn't change when the vector is moved), but would be invalidated |
| 73 | // by a copy which is why we disable the copy constructor and assignment |
| 74 | // operator for RpcServerTrusty. |
| 75 | auto numUuids = mPortAcl->uuids.size(); |
| 76 | mUuidPtrs.resize(numUuids); |
| 77 | for (size_t i = 0; i < numUuids; i++) { |
| 78 | mUuidPtrs[i] = &mPortAcl->uuids[i]; |
| 79 | } |
| 80 | |
| 81 | // Copy the contents of portAcl into the tipc_port_acl structure that we |
| 82 | // pass to tipc_add_service |
| 83 | mTipcPortAcl.flags = mPortAcl->flags; |
| 84 | mTipcPortAcl.uuid_num = numUuids; |
| 85 | mTipcPortAcl.uuids = mUuidPtrs.data(); |
| 86 | mTipcPortAcl.extra_data = mPortAcl->extraData; |
| 87 | |
| 88 | mTipcPort.acl = &mTipcPortAcl; |
| 89 | } else { |
| 90 | mTipcPort.acl = nullptr; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | int RpcServerTrusty::handleConnect(const tipc_port* port, handle_t chan, const uuid* peer, |
| 95 | void** ctx_p) { |
| 96 | auto* server = reinterpret_cast<RpcServerTrusty*>(const_cast<void*>(port->priv)); |
| 97 | server->mRpcServer->mShutdownTrigger = FdTrigger::make(); |
| 98 | server->mRpcServer->mConnectingThreads[rpc_this_thread::get_id()] = RpcMaybeThread(); |
| 99 | |
| 100 | int rc = NO_ERROR; |
| 101 | auto joinFn = [&](sp<RpcSession>&& session, RpcSession::PreJoinSetupResult&& result) { |
| 102 | if (result.status != OK) { |
| 103 | rc = statusToTrusty(result.status); |
| 104 | return; |
| 105 | } |
| 106 | |
Andrei Homescu | 5e74d2d | 2022-08-19 22:46:38 +0000 | [diff] [blame] | 107 | /* Save the session and connection for the other callbacks */ |
| 108 | auto* channelContext = new (std::nothrow) ChannelContext; |
| 109 | if (channelContext == nullptr) { |
| 110 | rc = ERR_NO_MEMORY; |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | channelContext->session = std::move(session); |
| 115 | channelContext->connection = std::move(result.connection); |
| 116 | |
| 117 | *ctx_p = channelContext; |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Andrei Homescu | 4af92ac | 2023-01-24 21:24:47 +0000 | [diff] [blame] | 120 | // We need to duplicate the channel handle here because the tipc library |
| 121 | // owns the original handle and closes is automatically on channel cleanup. |
| 122 | // We use dup() because Trusty does not have fcntl(). |
| 123 | // NOLINTNEXTLINE(android-cloexec-dup) |
| 124 | handle_t chanDup = dup(chan); |
| 125 | if (chanDup < 0) { |
| 126 | return chanDup; |
| 127 | } |
| 128 | base::unique_fd clientFd(chanDup); |
Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 129 | android::RpcTransportFd transportFd(std::move(clientFd)); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 130 | |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 131 | std::array<uint8_t, RpcServer::kRpcAddressSize> addr; |
| 132 | constexpr size_t addrLen = sizeof(*peer); |
| 133 | memcpy(addr.data(), peer, addrLen); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 134 | RpcServer::establishConnection(sp(server->mRpcServer), std::move(transportFd), addr, addrLen, |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 135 | joinFn); |
| 136 | |
| 137 | return rc; |
| 138 | } |
| 139 | |
Andrei Homescu | 875996f | 2022-08-24 04:25:11 +0000 | [diff] [blame] | 140 | int RpcServerTrusty::handleMessage(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) { |
Andrei Homescu | 5e74d2d | 2022-08-19 22:46:38 +0000 | [diff] [blame] | 141 | auto* channelContext = reinterpret_cast<ChannelContext*>(ctx); |
| 142 | LOG_ALWAYS_FATAL_IF(channelContext == nullptr, |
| 143 | "bad state: message received on uninitialized channel"); |
| 144 | |
| 145 | auto& session = channelContext->session; |
| 146 | auto& connection = channelContext->connection; |
| 147 | status_t status = |
| 148 | session->state()->drainCommands(connection, session, RpcState::CommandType::ANY); |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 149 | if (status != OK) { |
| 150 | LOG_RPC_DETAIL("Binder connection thread closing w/ status %s", |
| 151 | statusToString(status).c_str()); |
| 152 | } |
| 153 | |
| 154 | return NO_ERROR; |
| 155 | } |
| 156 | |
Andrei Homescu | a04030b | 2023-03-31 04:45:32 +0000 | [diff] [blame^] | 157 | void RpcServerTrusty::handleDisconnect(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) { |
| 158 | auto* channelContext = reinterpret_cast<ChannelContext*>(ctx); |
| 159 | if (channelContext == nullptr) { |
| 160 | // Connections marked "incoming" (outgoing from the server's side) |
| 161 | // do not have a valid channel context because joinFn does not get |
| 162 | // called for them. We ignore them here. |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | auto& session = channelContext->session; |
| 167 | (void)session->shutdownAndWait(false); |
| 168 | } |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 169 | |
| 170 | void RpcServerTrusty::handleChannelCleanup(void* ctx) { |
Andrei Homescu | 5e74d2d | 2022-08-19 22:46:38 +0000 | [diff] [blame] | 171 | auto* channelContext = reinterpret_cast<ChannelContext*>(ctx); |
| 172 | if (channelContext == nullptr) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | auto& session = channelContext->session; |
| 177 | auto& connection = channelContext->connection; |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 178 | LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection), |
| 179 | "bad state: connection object guaranteed to be in list"); |
Andrei Homescu | 5e74d2d | 2022-08-19 22:46:38 +0000 | [diff] [blame] | 180 | |
| 181 | delete channelContext; |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | } // namespace android |