blob: 0872b9014ce1f922d8494a2984a624d7537a3526 [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
30using android::base::unexpected;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070031using android::binder::unique_fd;
Andrei Homescu74a54452021-12-10 05:30:21 +000032
33namespace android {
34
35android::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
59RpcServerTrusty::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 Moreland6a66bfd2023-04-17 23:42:19 +000069 // 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 Morelandca3f6382023-05-11 23:23:26 +000071 LOG_ALWAYS_FATAL_IF(!mRpcServer->setProtocolVersion(0));
Steven Moreland6a66bfd2023-04-17 23:42:19 +000072
Andrei Homescu74a54452021-12-10 05:30:21 +000073 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
99int 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 Homescu5e74d2d2022-08-19 22:46:38 +0000112 /* 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 Homescu74a54452021-12-10 05:30:21 +0000123 };
124
Andrei Homescu4af92ac2023-01-24 21:24:47 +0000125 // 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 Wasilczyk639490b2023-11-01 13:49:41 -0700133 unique_fd clientFd(chanDup);
Pawan3e0061c2022-08-26 21:08:34 +0000134 android::RpcTransportFd transportFd(std::move(clientFd));
Pawan49d74cb2022-08-03 21:19:11 +0000135
Andrei Homescu74a54452021-12-10 05:30:21 +0000136 std::array<uint8_t, RpcServer::kRpcAddressSize> addr;
137 constexpr size_t addrLen = sizeof(*peer);
138 memcpy(addr.data(), peer, addrLen);
Pawan49d74cb2022-08-03 21:19:11 +0000139 RpcServer::establishConnection(sp(server->mRpcServer), std::move(transportFd), addr, addrLen,
Andrei Homescu74a54452021-12-10 05:30:21 +0000140 joinFn);
141
142 return rc;
143}
144
Andrei Homescu875996f2022-08-24 04:25:11 +0000145int RpcServerTrusty::handleMessage(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) {
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000146 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 Homescu74a54452021-12-10 05:30:21 +0000154 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 Homescua04030b2023-03-31 04:45:32 +0000162void 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 Homescu74a54452021-12-10 05:30:21 +0000174
175void RpcServerTrusty::handleChannelCleanup(void* ctx) {
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000176 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 Homescu74a54452021-12-10 05:30:21 +0000183 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
184 "bad state: connection object guaranteed to be in list");
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000185
186 delete channelContext;
Andrei Homescu74a54452021-12-10 05:30:21 +0000187}
188
189} // namespace android