blob: 05800464accad840aa1c6065dde36f9f772a922a [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)
Andrei Homescu77112772024-03-28 05:26:23 +000063 : mRpcServer(makeRpcServer(std::move(ctx))),
Andrei Homescu74a54452021-12-10 05:30:21 +000064 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
71 if (mPortAcl) {
72 // Initialize the array of pointers to uuids.
73 // The pointers in mUuidPtrs should stay valid across moves of
74 // RpcServerTrusty (the addresses of a std::vector's elements
75 // shouldn't change when the vector is moved), but would be invalidated
76 // by a copy which is why we disable the copy constructor and assignment
77 // operator for RpcServerTrusty.
78 auto numUuids = mPortAcl->uuids.size();
79 mUuidPtrs.resize(numUuids);
80 for (size_t i = 0; i < numUuids; i++) {
81 mUuidPtrs[i] = &mPortAcl->uuids[i];
82 }
83
84 // Copy the contents of portAcl into the tipc_port_acl structure that we
85 // pass to tipc_add_service
86 mTipcPortAcl.flags = mPortAcl->flags;
87 mTipcPortAcl.uuid_num = numUuids;
88 mTipcPortAcl.uuids = mUuidPtrs.data();
89 mTipcPortAcl.extra_data = mPortAcl->extraData;
90
91 mTipcPort.acl = &mTipcPortAcl;
92 } else {
93 mTipcPort.acl = nullptr;
94 }
95}
96
97int RpcServerTrusty::handleConnect(const tipc_port* port, handle_t chan, const uuid* peer,
98 void** ctx_p) {
99 auto* server = reinterpret_cast<RpcServerTrusty*>(const_cast<void*>(port->priv));
Andrei Homescu77112772024-03-28 05:26:23 +0000100 return handleConnectInternal(server->mRpcServer.get(), chan, peer, ctx_p);
101}
102
103int RpcServerTrusty::handleConnectInternal(RpcServer* rpcServer, handle_t chan, const uuid* peer,
104 void** ctx_p) {
105 rpcServer->mShutdownTrigger = FdTrigger::make();
106 rpcServer->mConnectingThreads[rpc_this_thread::get_id()] = RpcMaybeThread();
Andrei Homescu74a54452021-12-10 05:30:21 +0000107
108 int rc = NO_ERROR;
109 auto joinFn = [&](sp<RpcSession>&& session, RpcSession::PreJoinSetupResult&& result) {
110 if (result.status != OK) {
111 rc = statusToTrusty(result.status);
112 return;
113 }
114
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000115 /* Save the session and connection for the other callbacks */
116 auto* channelContext = new (std::nothrow) ChannelContext;
117 if (channelContext == nullptr) {
118 rc = ERR_NO_MEMORY;
119 return;
120 }
121
122 channelContext->session = std::move(session);
123 channelContext->connection = std::move(result.connection);
124
125 *ctx_p = channelContext;
Andrei Homescu74a54452021-12-10 05:30:21 +0000126 };
127
Andrei Homescu4af92ac2023-01-24 21:24:47 +0000128 // We need to duplicate the channel handle here because the tipc library
129 // owns the original handle and closes is automatically on channel cleanup.
130 // We use dup() because Trusty does not have fcntl().
131 // NOLINTNEXTLINE(android-cloexec-dup)
132 handle_t chanDup = dup(chan);
133 if (chanDup < 0) {
134 return chanDup;
135 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700136 unique_fd clientFd(chanDup);
Pawan3e0061c2022-08-26 21:08:34 +0000137 android::RpcTransportFd transportFd(std::move(clientFd));
Pawan49d74cb2022-08-03 21:19:11 +0000138
Andrei Homescu74a54452021-12-10 05:30:21 +0000139 std::array<uint8_t, RpcServer::kRpcAddressSize> addr;
140 constexpr size_t addrLen = sizeof(*peer);
141 memcpy(addr.data(), peer, addrLen);
Andrei Homescu77112772024-03-28 05:26:23 +0000142 RpcServer::establishConnection(sp<RpcServer>::fromExisting(rpcServer), std::move(transportFd),
143 addr, addrLen, joinFn);
Andrei Homescu74a54452021-12-10 05:30:21 +0000144
145 return rc;
146}
147
Andrei Homescu875996f2022-08-24 04:25:11 +0000148int RpcServerTrusty::handleMessage(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) {
Andrei Homescu77112772024-03-28 05:26:23 +0000149 return handleMessageInternal(ctx);
150}
151
152int RpcServerTrusty::handleMessageInternal(void* ctx) {
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000153 auto* channelContext = reinterpret_cast<ChannelContext*>(ctx);
Andrei Homescu5a8c69f2024-07-08 23:53:40 +0000154 if (channelContext == nullptr) {
155 LOG_RPC_DETAIL("bad state: message received on uninitialized channel");
156 return ERR_BAD_STATE;
157 }
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000158
159 auto& session = channelContext->session;
160 auto& connection = channelContext->connection;
161 status_t status =
162 session->state()->drainCommands(connection, session, RpcState::CommandType::ANY);
Andrei Homescu74a54452021-12-10 05:30:21 +0000163 if (status != OK) {
164 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
165 statusToString(status).c_str());
166 }
167
168 return NO_ERROR;
169}
170
Andrei Homescua04030b2023-03-31 04:45:32 +0000171void RpcServerTrusty::handleDisconnect(const tipc_port* /*port*/, handle_t /*chan*/, void* ctx) {
Andrei Homescu77112772024-03-28 05:26:23 +0000172 return handleDisconnectInternal(ctx);
173}
174
175void RpcServerTrusty::handleDisconnectInternal(void* ctx) {
Andrei Homescua04030b2023-03-31 04:45:32 +0000176 auto* channelContext = reinterpret_cast<ChannelContext*>(ctx);
177 if (channelContext == nullptr) {
178 // Connections marked "incoming" (outgoing from the server's side)
179 // do not have a valid channel context because joinFn does not get
180 // called for them. We ignore them here.
181 return;
182 }
183
184 auto& session = channelContext->session;
185 (void)session->shutdownAndWait(false);
186}
Andrei Homescu74a54452021-12-10 05:30:21 +0000187
188void RpcServerTrusty::handleChannelCleanup(void* ctx) {
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000189 auto* channelContext = reinterpret_cast<ChannelContext*>(ctx);
190 if (channelContext == nullptr) {
191 return;
192 }
193
194 auto& session = channelContext->session;
195 auto& connection = channelContext->connection;
Andrei Homescu74a54452021-12-10 05:30:21 +0000196 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
197 "bad state: connection object guaranteed to be in list");
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000198
199 delete channelContext;
Andrei Homescu74a54452021-12-10 05:30:21 +0000200}
201
202} // namespace android