blob: 1f857a0edb30686aea51716ed0b56f69d331d7a5 [file] [log] [blame]
Andrei Homescu74a54452021-12-10 05:30:21 +00001/*
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
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070030using android::binder::unique_fd;
Andrei Homescu74a54452021-12-10 05:30:21 +000031
32namespace android {
33
Tomasz Wasilczykbe685c02023-11-30 20:20:53 -080034sp<RpcServerTrusty> RpcServerTrusty::make(
Andrei Homescu74a54452021-12-10 05:30:21 +000035 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) {
Tomasz Wasilczykbe685c02023-11-30 20:20:53 -080042 ALOGE("Failed to create RpcServerTrusty: can't create server context");
43 return nullptr;
Andrei Homescu74a54452021-12-10 05:30:21 +000044 }
45
46 auto srv = sp<RpcServerTrusty>::make(std::move(ctx), std::move(portName), std::move(portAcl),
47 msgMaxSize);
48 if (srv == nullptr) {
Tomasz Wasilczykbe685c02023-11-30 20:20:53 -080049 ALOGE("Failed to create RpcServerTrusty: can't create server object");
50 return nullptr;
Andrei Homescu74a54452021-12-10 05:30:21 +000051 }
52
53 int rc = tipc_add_service(handleSet, &srv->mTipcPort, 1, 0, &kTipcOps);
54 if (rc != NO_ERROR) {
Tomasz Wasilczykbe685c02023-11-30 20:20:53 -080055 ALOGE("Failed to create RpcServerTrusty: can't add service: %d", rc);
56 return nullptr;
Andrei Homescu74a54452021-12-10 05:30:21 +000057 }
58 return srv;
59}
60
61RpcServerTrusty::RpcServerTrusty(std::unique_ptr<RpcTransportCtx> ctx, std::string&& portName,
62 std::shared_ptr<const PortAcl>&& portAcl, size_t msgMaxSize)
63 : mRpcServer(sp<RpcServer>::make(std::move(ctx))),
64 mPortName(std::move(portName)),
65 mPortAcl(std::move(portAcl)) {
66 mTipcPort.name = mPortName.c_str();
67 mTipcPort.msg_max_size = msgMaxSize;
68 mTipcPort.msg_queue_len = 6; // Three each way
69 mTipcPort.priv = this;
70
Steven Moreland6a66bfd2023-04-17 23:42:19 +000071 // TODO(b/266741352): follow-up to prevent needing this in the future
72 // Trusty needs to be set to the latest stable version that is in prebuilts there.
Steven Morelandca3f6382023-05-11 23:23:26 +000073 LOG_ALWAYS_FATAL_IF(!mRpcServer->setProtocolVersion(0));
Steven Moreland6a66bfd2023-04-17 23:42:19 +000074
Andrei Homescu74a54452021-12-10 05:30:21 +000075 if (mPortAcl) {
76 // Initialize the array of pointers to uuids.
77 // The pointers in mUuidPtrs should stay valid across moves of
78 // RpcServerTrusty (the addresses of a std::vector's elements
79 // shouldn't change when the vector is moved), but would be invalidated
80 // by a copy which is why we disable the copy constructor and assignment
81 // operator for RpcServerTrusty.
82 auto numUuids = mPortAcl->uuids.size();
83 mUuidPtrs.resize(numUuids);
84 for (size_t i = 0; i < numUuids; i++) {
85 mUuidPtrs[i] = &mPortAcl->uuids[i];
86 }
87
88 // Copy the contents of portAcl into the tipc_port_acl structure that we
89 // pass to tipc_add_service
90 mTipcPortAcl.flags = mPortAcl->flags;
91 mTipcPortAcl.uuid_num = numUuids;
92 mTipcPortAcl.uuids = mUuidPtrs.data();
93 mTipcPortAcl.extra_data = mPortAcl->extraData;
94
95 mTipcPort.acl = &mTipcPortAcl;
96 } else {
97 mTipcPort.acl = nullptr;
98 }
99}
100
101int RpcServerTrusty::handleConnect(const tipc_port* port, handle_t chan, const uuid* peer,
102 void** ctx_p) {
103 auto* server = reinterpret_cast<RpcServerTrusty*>(const_cast<void*>(port->priv));
104 server->mRpcServer->mShutdownTrigger = FdTrigger::make();
105 server->mRpcServer->mConnectingThreads[rpc_this_thread::get_id()] = RpcMaybeThread();
106
107 int rc = NO_ERROR;
108 auto joinFn = [&](sp<RpcSession>&& session, RpcSession::PreJoinSetupResult&& result) {
109 if (result.status != OK) {
110 rc = statusToTrusty(result.status);
111 return;
112 }
113
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000114 /* Save the session and connection for the other callbacks */
115 auto* channelContext = new (std::nothrow) ChannelContext;
116 if (channelContext == nullptr) {
117 rc = ERR_NO_MEMORY;
118 return;
119 }
120
121 channelContext->session = std::move(session);
122 channelContext->connection = std::move(result.connection);
123
124 *ctx_p = channelContext;
Andrei Homescu74a54452021-12-10 05:30:21 +0000125 };
126
Andrei Homescu4af92ac2023-01-24 21:24:47 +0000127 // We need to duplicate the channel handle here because the tipc library
128 // owns the original handle and closes is automatically on channel cleanup.
129 // We use dup() because Trusty does not have fcntl().
130 // NOLINTNEXTLINE(android-cloexec-dup)
131 handle_t chanDup = dup(chan);
132 if (chanDup < 0) {
133 return chanDup;
134 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700135 unique_fd clientFd(chanDup);
Pawan3e0061c2022-08-26 21:08:34 +0000136 android::RpcTransportFd transportFd(std::move(clientFd));
Pawan49d74cb2022-08-03 21:19:11 +0000137
Andrei Homescu74a54452021-12-10 05:30:21 +0000138 std::array<uint8_t, RpcServer::kRpcAddressSize> addr;
139 constexpr size_t addrLen = sizeof(*peer);
140 memcpy(addr.data(), peer, addrLen);
Pawan49d74cb2022-08-03 21:19:11 +0000141 RpcServer::establishConnection(sp(server->mRpcServer), std::move(transportFd), addr, addrLen,
Andrei Homescu74a54452021-12-10 05:30:21 +0000142 joinFn);
143
144 return rc;
145}
146
Andrei Homescu875996f2022-08-24 04:25:11 +0000147int RpcServerTrusty::handleMessage(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) {
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000148 auto* channelContext = reinterpret_cast<ChannelContext*>(ctx);
149 LOG_ALWAYS_FATAL_IF(channelContext == nullptr,
150 "bad state: message received on uninitialized channel");
151
152 auto& session = channelContext->session;
153 auto& connection = channelContext->connection;
154 status_t status =
155 session->state()->drainCommands(connection, session, RpcState::CommandType::ANY);
Andrei Homescu74a54452021-12-10 05:30:21 +0000156 if (status != OK) {
157 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
158 statusToString(status).c_str());
159 }
160
161 return NO_ERROR;
162}
163
Andrei Homescua04030b2023-03-31 04:45:32 +0000164void RpcServerTrusty::handleDisconnect(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) {
165 auto* channelContext = reinterpret_cast<ChannelContext*>(ctx);
166 if (channelContext == nullptr) {
167 // Connections marked "incoming" (outgoing from the server's side)
168 // do not have a valid channel context because joinFn does not get
169 // called for them. We ignore them here.
170 return;
171 }
172
173 auto& session = channelContext->session;
174 (void)session->shutdownAndWait(false);
175}
Andrei Homescu74a54452021-12-10 05:30:21 +0000176
177void RpcServerTrusty::handleChannelCleanup(void* ctx) {
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000178 auto* channelContext = reinterpret_cast<ChannelContext*>(ctx);
179 if (channelContext == nullptr) {
180 return;
181 }
182
183 auto& session = channelContext->session;
184 auto& connection = channelContext->connection;
Andrei Homescu74a54452021-12-10 05:30:21 +0000185 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
186 "bad state: connection object guaranteed to be in list");
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000187
188 delete channelContext;
Andrei Homescu74a54452021-12-10 05:30:21 +0000189}
190
191} // namespace android